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/Core/Debugger.h"
13 
14 #include <map>
15 
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/Type.h"
18 #include "llvm/ADT/StringRef.h"
19 
20 #include "lldb/lldb-private.h"
21 #include "lldb/Core/ConnectionFileDescriptor.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/PluginManager.h"
24 #include "lldb/Core/RegisterValue.h"
25 #include "lldb/Core/State.h"
26 #include "lldb/Core/StreamAsynchronousIO.h"
27 #include "lldb/Core/StreamCallback.h"
28 #include "lldb/Core/StreamFile.h"
29 #include "lldb/Core/StreamString.h"
30 #include "lldb/Core/StructuredData.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/DataFormatters/TypeSummary.h"
37 #include "lldb/Host/DynamicLibrary.h"
38 #include "lldb/Host/Terminal.h"
39 #include "lldb/Interpreter/CommandInterpreter.h"
40 #include "lldb/Interpreter/OptionValueSInt64.h"
41 #include "lldb/Interpreter/OptionValueString.h"
42 #include "lldb/Symbol/ClangASTContext.h"
43 #include "lldb/Symbol/CompileUnit.h"
44 #include "lldb/Symbol/Function.h"
45 #include "lldb/Symbol/Symbol.h"
46 #include "lldb/Symbol/VariableList.h"
47 #include "lldb/Target/TargetList.h"
48 #include "lldb/Target/Process.h"
49 #include "lldb/Target/RegisterContext.h"
50 #include "lldb/Target/SectionLoadList.h"
51 #include "lldb/Target/StopInfo.h"
52 #include "lldb/Target/Target.h"
53 #include "lldb/Target/Thread.h"
54 #include "lldb/Utility/AnsiTerminal.h"
55 
56 using namespace lldb;
57 using namespace lldb_private;
58 
59 
60 static uint32_t g_shared_debugger_refcount = 0;
61 static lldb::user_id_t g_unique_id = 1;
62 
63 #pragma mark Static Functions
64 
65 static Mutex &
66 GetDebuggerListMutex ()
67 {
68     static Mutex g_mutex(Mutex::eMutexTypeRecursive);
69     return g_mutex;
70 }
71 
72 typedef std::vector<DebuggerSP> DebuggerList;
73 
74 static DebuggerList &
75 GetDebuggerList()
76 {
77     // hide the static debugger list inside a singleton accessor to avoid
78     // global init contructors
79     static DebuggerList g_list;
80     return g_list;
81 }
82 
83 OptionEnumValueElement
84 g_show_disassembly_enum_values[] =
85 {
86     { Debugger::eStopDisassemblyTypeNever,    "never",     "Never show disassembly when displaying a stop context."},
87     { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
88     { Debugger::eStopDisassemblyTypeAlways,   "always",    "Always show disassembly when displaying a stop context."},
89     { 0, NULL, NULL }
90 };
91 
92 OptionEnumValueElement
93 g_language_enumerators[] =
94 {
95     { eScriptLanguageNone,      "none",     "Disable scripting languages."},
96     { eScriptLanguagePython,    "python",   "Select python as the default scripting language."},
97     { eScriptLanguageDefault,   "default",  "Select the lldb default as the default scripting language."},
98     { 0, NULL, NULL }
99 };
100 
101 #define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
102 #define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
103 
104 #define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
105     "{, ${frame.pc}}"\
106     MODULE_WITH_FUNC\
107     FILE_AND_LINE\
108     "{, name = '${thread.name}'}"\
109     "{, queue = '${thread.queue}'}"\
110     "{, activity = '${thread.info.activity.name}'}" \
111     "{, ${thread.info.trace_messages} messages}" \
112     "{, stop reason = ${thread.stop-reason}}"\
113     "{\\nReturn value: ${thread.return-value}}"\
114     "\\n"
115 
116 #define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
117     MODULE_WITH_FUNC\
118     FILE_AND_LINE\
119     "\\n"
120 
121 
122 
123 static PropertyDefinition
124 g_properties[] =
125 {
126 {   "auto-confirm",             OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
127 {   "frame-format",             OptionValue::eTypeString , true, 0    , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
128 {   "notify-void",              OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
129 {   "prompt",                   OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
130 {   "script-lang",              OptionValue::eTypeEnum   , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
131 {   "stop-disassembly-count",   OptionValue::eTypeSInt64 , true, 4    , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
132 {   "stop-disassembly-display", OptionValue::eTypeEnum   , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
133 {   "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." },
134 {   "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." },
135 {   "term-width",               OptionValue::eTypeSInt64 , true, 80   , NULL, NULL, "The maximum number of columns to use for displaying text." },
136 {   "thread-format",            OptionValue::eTypeString , true, 0    , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
137 {   "use-external-editor",      OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
138 {   "use-color",                OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
139 {   "auto-one-line-summaries",     OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
140 
141     {   NULL,                       OptionValue::eTypeInvalid, true, 0    , NULL, NULL, NULL }
142 };
143 
144 enum
145 {
146     ePropertyAutoConfirm = 0,
147     ePropertyFrameFormat,
148     ePropertyNotiftVoid,
149     ePropertyPrompt,
150     ePropertyScriptLanguage,
151     ePropertyStopDisassemblyCount,
152     ePropertyStopDisassemblyDisplay,
153     ePropertyStopLineCountAfter,
154     ePropertyStopLineCountBefore,
155     ePropertyTerminalWidth,
156     ePropertyThreadFormat,
157     ePropertyUseExternalEditor,
158     ePropertyUseColor,
159     ePropertyAutoOneLineSummaries
160 };
161 
162 Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
163 
164 Error
165 Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
166                             VarSetOperationType op,
167                             const char *property_path,
168                             const char *value)
169 {
170     bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
171     TargetSP target_sp;
172     LoadScriptFromSymFile load_script_old_value;
173     if (is_load_script && exe_ctx->GetTargetSP())
174     {
175         target_sp = exe_ctx->GetTargetSP();
176         load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
177     }
178     Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
179     if (error.Success())
180     {
181         // FIXME it would be nice to have "on-change" callbacks for properties
182         if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
183         {
184             const char *new_prompt = GetPrompt();
185             std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
186             if (str.length())
187                 new_prompt = str.c_str();
188             GetCommandInterpreter().UpdatePrompt(new_prompt);
189             EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
190             GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
191         }
192         else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
193         {
194 			// use-color changed. Ping the prompt so it can reset the ansi terminal codes.
195             SetPrompt (GetPrompt());
196         }
197         else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
198         {
199             if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
200             {
201                 std::list<Error> errors;
202                 StreamString feedback_stream;
203                 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
204                 {
205                     StreamFileSP stream_sp (GetErrorFile());
206                     if (stream_sp)
207                     {
208                         for (auto error : errors)
209                         {
210                             stream_sp->Printf("%s\n",error.AsCString());
211                         }
212                         if (feedback_stream.GetSize())
213                             stream_sp->Printf("%s",feedback_stream.GetData());
214                     }
215                 }
216             }
217         }
218     }
219     return error;
220 }
221 
222 bool
223 Debugger::GetAutoConfirm () const
224 {
225     const uint32_t idx = ePropertyAutoConfirm;
226     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
227 }
228 
229 const char *
230 Debugger::GetFrameFormat() const
231 {
232     const uint32_t idx = ePropertyFrameFormat;
233     return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
234 }
235 
236 bool
237 Debugger::GetNotifyVoid () const
238 {
239     const uint32_t idx = ePropertyNotiftVoid;
240     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
241 }
242 
243 const char *
244 Debugger::GetPrompt() const
245 {
246     const uint32_t idx = ePropertyPrompt;
247     return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
248 }
249 
250 void
251 Debugger::SetPrompt(const char *p)
252 {
253     const uint32_t idx = ePropertyPrompt;
254     m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
255     const char *new_prompt = GetPrompt();
256     std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
257     if (str.length())
258         new_prompt = str.c_str();
259     GetCommandInterpreter().UpdatePrompt(new_prompt);
260 }
261 
262 const char *
263 Debugger::GetThreadFormat() const
264 {
265     const uint32_t idx = ePropertyThreadFormat;
266     return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
267 }
268 
269 lldb::ScriptLanguage
270 Debugger::GetScriptLanguage() const
271 {
272     const uint32_t idx = ePropertyScriptLanguage;
273     return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
274 }
275 
276 bool
277 Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
278 {
279     const uint32_t idx = ePropertyScriptLanguage;
280     return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
281 }
282 
283 uint32_t
284 Debugger::GetTerminalWidth () const
285 {
286     const uint32_t idx = ePropertyTerminalWidth;
287     return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
288 }
289 
290 bool
291 Debugger::SetTerminalWidth (uint32_t term_width)
292 {
293     const uint32_t idx = ePropertyTerminalWidth;
294     return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
295 }
296 
297 bool
298 Debugger::GetUseExternalEditor () const
299 {
300     const uint32_t idx = ePropertyUseExternalEditor;
301     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
302 }
303 
304 bool
305 Debugger::SetUseExternalEditor (bool b)
306 {
307     const uint32_t idx = ePropertyUseExternalEditor;
308     return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
309 }
310 
311 bool
312 Debugger::GetUseColor () const
313 {
314     const uint32_t idx = ePropertyUseColor;
315     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
316 }
317 
318 bool
319 Debugger::SetUseColor (bool b)
320 {
321     const uint32_t idx = ePropertyUseColor;
322     bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
323     SetPrompt (GetPrompt());
324     return ret;
325 }
326 
327 uint32_t
328 Debugger::GetStopSourceLineCount (bool before) const
329 {
330     const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
331     return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
332 }
333 
334 Debugger::StopDisassemblyType
335 Debugger::GetStopDisassemblyDisplay () const
336 {
337     const uint32_t idx = ePropertyStopDisassemblyDisplay;
338     return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
339 }
340 
341 uint32_t
342 Debugger::GetDisassemblyLineCount () const
343 {
344     const uint32_t idx = ePropertyStopDisassemblyCount;
345     return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
346 }
347 
348 bool
349 Debugger::GetAutoOneLineSummaries () const
350 {
351     const uint32_t idx = ePropertyAutoOneLineSummaries;
352     return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
353 
354 }
355 
356 #pragma mark Debugger
357 
358 //const DebuggerPropertiesSP &
359 //Debugger::GetSettings() const
360 //{
361 //    return m_properties_sp;
362 //}
363 //
364 
365 int
366 Debugger::TestDebuggerRefCount ()
367 {
368     return g_shared_debugger_refcount;
369 }
370 
371 void
372 Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
373 {
374     g_load_plugin_callback = load_plugin_callback;
375     if (g_shared_debugger_refcount++ == 0)
376         lldb_private::Initialize();
377 }
378 
379 void
380 Debugger::Terminate ()
381 {
382     if (g_shared_debugger_refcount > 0)
383     {
384         g_shared_debugger_refcount--;
385         if (g_shared_debugger_refcount == 0)
386         {
387             lldb_private::WillTerminate();
388             lldb_private::Terminate();
389 
390             // Clear our master list of debugger objects
391             Mutex::Locker locker (GetDebuggerListMutex ());
392             GetDebuggerList().clear();
393         }
394     }
395 }
396 
397 void
398 Debugger::SettingsInitialize ()
399 {
400     Target::SettingsInitialize ();
401 }
402 
403 void
404 Debugger::SettingsTerminate ()
405 {
406     Target::SettingsTerminate ();
407 }
408 
409 bool
410 Debugger::LoadPlugin (const FileSpec& spec, Error& error)
411 {
412     if (g_load_plugin_callback)
413     {
414         lldb::DynamicLibrarySP dynlib_sp = g_load_plugin_callback (shared_from_this(), spec, error);
415         if (dynlib_sp)
416         {
417             m_loaded_plugins.push_back(dynlib_sp);
418             return true;
419         }
420     }
421     else
422     {
423         // The g_load_plugin_callback is registered in SBDebugger::Initialize()
424         // and if the public API layer isn't available (code is linking against
425         // all of the internal LLDB static libraries), then we can't load plugins
426         error.SetErrorString("Public API layer is not available");
427     }
428     return false;
429 }
430 
431 static FileSpec::EnumerateDirectoryResult
432 LoadPluginCallback
433 (
434  void *baton,
435  FileSpec::FileType file_type,
436  const FileSpec &file_spec
437  )
438 {
439     Error error;
440 
441     static ConstString g_dylibext("dylib");
442     static ConstString g_solibext("so");
443 
444     if (!baton)
445         return FileSpec::eEnumerateDirectoryResultQuit;
446 
447     Debugger *debugger = (Debugger*)baton;
448 
449     // If we have a regular file, a symbolic link or unknown file type, try
450     // and process the file. We must handle unknown as sometimes the directory
451     // enumeration might be enumerating a file system that doesn't have correct
452     // file type information.
453     if (file_type == FileSpec::eFileTypeRegular         ||
454         file_type == FileSpec::eFileTypeSymbolicLink    ||
455         file_type == FileSpec::eFileTypeUnknown          )
456     {
457         FileSpec plugin_file_spec (file_spec);
458         plugin_file_spec.ResolvePath ();
459 
460         if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
461             plugin_file_spec.GetFileNameExtension() != g_solibext)
462         {
463             return FileSpec::eEnumerateDirectoryResultNext;
464         }
465 
466         Error plugin_load_error;
467         debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
468 
469         return FileSpec::eEnumerateDirectoryResultNext;
470     }
471 
472     else if (file_type == FileSpec::eFileTypeUnknown     ||
473         file_type == FileSpec::eFileTypeDirectory   ||
474         file_type == FileSpec::eFileTypeSymbolicLink )
475     {
476         // Try and recurse into anything that a directory or symbolic link.
477         // We must also do this for unknown as sometimes the directory enumeration
478         // might be enurating a file system that doesn't have correct file type
479         // information.
480         return FileSpec::eEnumerateDirectoryResultEnter;
481     }
482 
483     return FileSpec::eEnumerateDirectoryResultNext;
484 }
485 
486 void
487 Debugger::InstanceInitialize ()
488 {
489     FileSpec dir_spec;
490     const bool find_directories = true;
491     const bool find_files = true;
492     const bool find_other = true;
493     char dir_path[PATH_MAX];
494     if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
495     {
496         if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
497         {
498             FileSpec::EnumerateDirectory (dir_path,
499                                           find_directories,
500                                           find_files,
501                                           find_other,
502                                           LoadPluginCallback,
503                                           this);
504         }
505     }
506 
507     if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
508     {
509         if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
510         {
511             FileSpec::EnumerateDirectory (dir_path,
512                                           find_directories,
513                                           find_files,
514                                           find_other,
515                                           LoadPluginCallback,
516                                           this);
517         }
518     }
519 
520     PluginManager::DebuggerInitialize (*this);
521 }
522 
523 DebuggerSP
524 Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
525 {
526     DebuggerSP debugger_sp (new Debugger(log_callback, baton));
527     if (g_shared_debugger_refcount > 0)
528     {
529         Mutex::Locker locker (GetDebuggerListMutex ());
530         GetDebuggerList().push_back(debugger_sp);
531     }
532     debugger_sp->InstanceInitialize ();
533     return debugger_sp;
534 }
535 
536 void
537 Debugger::Destroy (DebuggerSP &debugger_sp)
538 {
539     if (debugger_sp.get() == NULL)
540         return;
541 
542     debugger_sp->Clear();
543 
544     if (g_shared_debugger_refcount > 0)
545     {
546         Mutex::Locker locker (GetDebuggerListMutex ());
547         DebuggerList &debugger_list = GetDebuggerList ();
548         DebuggerList::iterator pos, end = debugger_list.end();
549         for (pos = debugger_list.begin (); pos != end; ++pos)
550         {
551             if ((*pos).get() == debugger_sp.get())
552             {
553                 debugger_list.erase (pos);
554                 return;
555             }
556         }
557     }
558 }
559 
560 DebuggerSP
561 Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
562 {
563     DebuggerSP debugger_sp;
564     if (g_shared_debugger_refcount > 0)
565     {
566         Mutex::Locker locker (GetDebuggerListMutex ());
567         DebuggerList &debugger_list = GetDebuggerList();
568         DebuggerList::iterator pos, end = debugger_list.end();
569 
570         for (pos = debugger_list.begin(); pos != end; ++pos)
571         {
572             if ((*pos).get()->m_instance_name == instance_name)
573             {
574                 debugger_sp = *pos;
575                 break;
576             }
577         }
578     }
579     return debugger_sp;
580 }
581 
582 TargetSP
583 Debugger::FindTargetWithProcessID (lldb::pid_t pid)
584 {
585     TargetSP target_sp;
586     if (g_shared_debugger_refcount > 0)
587     {
588         Mutex::Locker locker (GetDebuggerListMutex ());
589         DebuggerList &debugger_list = GetDebuggerList();
590         DebuggerList::iterator pos, end = debugger_list.end();
591         for (pos = debugger_list.begin(); pos != end; ++pos)
592         {
593             target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
594             if (target_sp)
595                 break;
596         }
597     }
598     return target_sp;
599 }
600 
601 TargetSP
602 Debugger::FindTargetWithProcess (Process *process)
603 {
604     TargetSP target_sp;
605     if (g_shared_debugger_refcount > 0)
606     {
607         Mutex::Locker locker (GetDebuggerListMutex ());
608         DebuggerList &debugger_list = GetDebuggerList();
609         DebuggerList::iterator pos, end = debugger_list.end();
610         for (pos = debugger_list.begin(); pos != end; ++pos)
611         {
612             target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
613             if (target_sp)
614                 break;
615         }
616     }
617     return target_sp;
618 }
619 
620 Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
621     UserID (g_unique_id++),
622     Properties(OptionValuePropertiesSP(new OptionValueProperties())),
623     m_input_file_sp (new StreamFile (stdin, false)),
624     m_output_file_sp (new StreamFile (stdout, false)),
625     m_error_file_sp (new StreamFile (stderr, false)),
626     m_terminal_state (),
627     m_target_list (*this),
628     m_platform_list (),
629     m_listener ("lldb.Debugger"),
630     m_source_manager_ap(),
631     m_source_file_cache(),
632     m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
633     m_input_reader_stack (),
634     m_instance_name (),
635     m_loaded_plugins (),
636     m_event_handler_thread (LLDB_INVALID_HOST_THREAD),
637     m_io_handler_thread (LLDB_INVALID_HOST_THREAD)
638 {
639     char instance_cstr[256];
640     snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
641     m_instance_name.SetCString(instance_cstr);
642     if (log_callback)
643         m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
644     m_command_interpreter_ap->Initialize ();
645     // Always add our default platform to the platform list
646     PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
647     assert (default_platform_sp.get());
648     m_platform_list.Append (default_platform_sp, true);
649 
650     m_collection_sp->Initialize (g_properties);
651     m_collection_sp->AppendProperty (ConstString("target"),
652                                      ConstString("Settings specify to debugging targets."),
653                                      true,
654                                      Target::GetGlobalProperties()->GetValueProperties());
655     if (m_command_interpreter_ap.get())
656     {
657         m_collection_sp->AppendProperty (ConstString("interpreter"),
658                                          ConstString("Settings specify to the debugger's command interpreter."),
659                                          true,
660                                          m_command_interpreter_ap->GetValueProperties());
661     }
662     OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
663     term_width->SetMinimumValue(10);
664     term_width->SetMaximumValue(1024);
665 
666     // Turn off use-color if this is a dumb terminal.
667     const char *term = getenv ("TERM");
668     if (term && !strcmp (term, "dumb"))
669         SetUseColor (false);
670 }
671 
672 Debugger::~Debugger ()
673 {
674     Clear();
675 }
676 
677 void
678 Debugger::Clear()
679 {
680     ClearIOHandlers();
681     StopIOHandlerThread();
682     StopEventHandlerThread();
683     m_listener.Clear();
684     int num_targets = m_target_list.GetNumTargets();
685     for (int i = 0; i < num_targets; i++)
686     {
687         TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
688         if (target_sp)
689         {
690             ProcessSP process_sp (target_sp->GetProcessSP());
691             if (process_sp)
692                 process_sp->Finalize();
693             target_sp->Destroy();
694         }
695     }
696     BroadcasterManager::Clear ();
697 
698     // Close the input file _before_ we close the input read communications class
699     // as it does NOT own the input file, our m_input_file does.
700     m_terminal_state.Clear();
701     if (m_input_file_sp)
702         m_input_file_sp->GetFile().Close ();
703 
704     m_command_interpreter_ap->Clear();
705 }
706 
707 bool
708 Debugger::GetCloseInputOnEOF () const
709 {
710 //    return m_input_comm.GetCloseOnEOF();
711     return false;
712 }
713 
714 void
715 Debugger::SetCloseInputOnEOF (bool b)
716 {
717 //    m_input_comm.SetCloseOnEOF(b);
718 }
719 
720 bool
721 Debugger::GetAsyncExecution ()
722 {
723     return !m_command_interpreter_ap->GetSynchronous();
724 }
725 
726 void
727 Debugger::SetAsyncExecution (bool async_execution)
728 {
729     m_command_interpreter_ap->SetSynchronous (!async_execution);
730 }
731 
732 
733 void
734 Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
735 {
736     if (m_input_file_sp)
737         m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
738     else
739         m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
740 
741     File &in_file = m_input_file_sp->GetFile();
742     if (in_file.IsValid() == false)
743         in_file.SetStream (stdin, true);
744 
745     // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
746     SaveInputTerminalState ();
747 }
748 
749 void
750 Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
751 {
752     if (m_output_file_sp)
753         m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
754     else
755         m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
756 
757     File &out_file = m_output_file_sp->GetFile();
758     if (out_file.IsValid() == false)
759         out_file.SetStream (stdout, false);
760 
761     // do not create the ScriptInterpreter just for setting the output file handle
762     // as the constructor will know how to do the right thing on its own
763     const bool can_create = false;
764     ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
765     if (script_interpreter)
766         script_interpreter->ResetOutputFileHandle (fh);
767 }
768 
769 void
770 Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
771 {
772     if (m_error_file_sp)
773         m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
774     else
775         m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
776 
777     File &err_file = m_error_file_sp->GetFile();
778     if (err_file.IsValid() == false)
779         err_file.SetStream (stderr, false);
780 }
781 
782 void
783 Debugger::SaveInputTerminalState ()
784 {
785     if (m_input_file_sp)
786     {
787         File &in_file = m_input_file_sp->GetFile();
788         if (in_file.GetDescriptor() != File::kInvalidDescriptor)
789             m_terminal_state.Save(in_file.GetDescriptor(), true);
790     }
791 }
792 
793 void
794 Debugger::RestoreInputTerminalState ()
795 {
796     m_terminal_state.Restore();
797 }
798 
799 ExecutionContext
800 Debugger::GetSelectedExecutionContext ()
801 {
802     ExecutionContext exe_ctx;
803     TargetSP target_sp(GetSelectedTarget());
804     exe_ctx.SetTargetSP (target_sp);
805 
806     if (target_sp)
807     {
808         ProcessSP process_sp (target_sp->GetProcessSP());
809         exe_ctx.SetProcessSP (process_sp);
810         if (process_sp && process_sp->IsRunning() == false)
811         {
812             ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
813             if (thread_sp)
814             {
815                 exe_ctx.SetThreadSP (thread_sp);
816                 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
817                 if (exe_ctx.GetFramePtr() == NULL)
818                     exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
819             }
820         }
821     }
822     return exe_ctx;
823 }
824 
825 void
826 Debugger::DispatchInputInterrupt ()
827 {
828     Mutex::Locker locker (m_input_reader_stack.GetMutex());
829     IOHandlerSP reader_sp (m_input_reader_stack.Top());
830     if (reader_sp)
831         reader_sp->Interrupt();
832 }
833 
834 void
835 Debugger::DispatchInputEndOfFile ()
836 {
837     Mutex::Locker locker (m_input_reader_stack.GetMutex());
838     IOHandlerSP reader_sp (m_input_reader_stack.Top());
839     if (reader_sp)
840         reader_sp->GotEOF();
841 }
842 
843 void
844 Debugger::ClearIOHandlers ()
845 {
846     // The bottom input reader should be the main debugger input reader.  We do not want to close that one here.
847     Mutex::Locker locker (m_input_reader_stack.GetMutex());
848     while (m_input_reader_stack.GetSize() > 1)
849     {
850         IOHandlerSP reader_sp (m_input_reader_stack.Top());
851         if (reader_sp)
852         {
853             m_input_reader_stack.Pop();
854             reader_sp->SetIsDone(true);
855             reader_sp->Cancel();
856         }
857     }
858 }
859 
860 void
861 Debugger::ExecuteIOHanders()
862 {
863 
864     while (1)
865     {
866         IOHandlerSP reader_sp(m_input_reader_stack.Top());
867         if (!reader_sp)
868             break;
869 
870         reader_sp->Activate();
871         reader_sp->Run();
872         reader_sp->Deactivate();
873 
874         // Remove all input readers that are done from the top of the stack
875         while (1)
876         {
877             IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
878             if (top_reader_sp && top_reader_sp->GetIsDone())
879                 m_input_reader_stack.Pop();
880             else
881                 break;
882         }
883     }
884     ClearIOHandlers();
885 }
886 
887 bool
888 Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
889 {
890     return m_input_reader_stack.IsTop (reader_sp);
891 }
892 
893 
894 ConstString
895 Debugger::GetTopIOHandlerControlSequence(char ch)
896 {
897     return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
898 }
899 
900 void
901 Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
902 {
903     Mutex::Locker locker (m_input_reader_stack.GetMutex());
904     PushIOHandler (reader_sp);
905     reader_sp->Activate();
906     reader_sp->Run();
907     PopIOHandler (reader_sp);
908 }
909 
910 void
911 Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
912 {
913     // Before an IOHandler runs, it must have in/out/err streams.
914     // This function is called when one ore more of the streams
915     // are NULL. We use the top input reader's in/out/err streams,
916     // or fall back to the debugger file handles, or we fall back
917     // onto stdin/stdout/stderr as a last resort.
918 
919     Mutex::Locker locker (m_input_reader_stack.GetMutex());
920     IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
921     // If no STDIN has been set, then set it appropriately
922     if (!in)
923     {
924         if (top_reader_sp)
925             in = top_reader_sp->GetInputStreamFile();
926         else
927             in = GetInputFile();
928 
929         // If there is nothing, use stdin
930         if (!in)
931             in = StreamFileSP(new StreamFile(stdin, false));
932     }
933     // If no STDOUT has been set, then set it appropriately
934     if (!out)
935     {
936         if (top_reader_sp)
937             out = top_reader_sp->GetOutputStreamFile();
938         else
939             out = GetOutputFile();
940 
941         // If there is nothing, use stdout
942         if (!out)
943             out = StreamFileSP(new StreamFile(stdout, false));
944     }
945     // If no STDERR has been set, then set it appropriately
946     if (!err)
947     {
948         if (top_reader_sp)
949             err = top_reader_sp->GetErrorStreamFile();
950         else
951             err = GetErrorFile();
952 
953         // If there is nothing, use stderr
954         if (!err)
955             err = StreamFileSP(new StreamFile(stdout, false));
956 
957     }
958 }
959 
960 void
961 Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
962 {
963     if (!reader_sp)
964         return;
965 
966     // Got the current top input reader...
967     IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
968 
969     // Don't push the same IO handler twice...
970     if (reader_sp.get() != top_reader_sp.get())
971     {
972         // Push our new input reader
973         m_input_reader_stack.Push (reader_sp);
974 
975         // Interrupt the top input reader to it will exit its Run() function
976         // and let this new input reader take over
977         if (top_reader_sp)
978             top_reader_sp->Deactivate();
979     }
980 }
981 
982 bool
983 Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
984 {
985     bool result = false;
986 
987     Mutex::Locker locker (m_input_reader_stack.GetMutex());
988 
989     // The reader on the stop of the stack is done, so let the next
990     // read on the stack referesh its prompt and if there is one...
991     if (!m_input_reader_stack.IsEmpty())
992     {
993         IOHandlerSP reader_sp(m_input_reader_stack.Top());
994 
995         if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
996         {
997             reader_sp->Deactivate();
998             reader_sp->Cancel();
999             m_input_reader_stack.Pop ();
1000 
1001             reader_sp = m_input_reader_stack.Top();
1002             if (reader_sp)
1003                 reader_sp->Activate();
1004 
1005             result = true;
1006         }
1007     }
1008     return result;
1009 }
1010 
1011 bool
1012 Debugger::HideTopIOHandler()
1013 {
1014     Mutex::Locker locker;
1015 
1016     if (locker.TryLock(m_input_reader_stack.GetMutex()))
1017     {
1018         IOHandlerSP reader_sp(m_input_reader_stack.Top());
1019         if (reader_sp)
1020             reader_sp->Hide();
1021         return true;
1022     }
1023     return false;
1024 }
1025 
1026 void
1027 Debugger::RefreshTopIOHandler()
1028 {
1029     IOHandlerSP reader_sp(m_input_reader_stack.Top());
1030     if (reader_sp)
1031         reader_sp->Refresh();
1032 }
1033 
1034 
1035 StreamSP
1036 Debugger::GetAsyncOutputStream ()
1037 {
1038     return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1039                                                CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1040 }
1041 
1042 StreamSP
1043 Debugger::GetAsyncErrorStream ()
1044 {
1045     return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1046                                                CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1047 }
1048 
1049 size_t
1050 Debugger::GetNumDebuggers()
1051 {
1052     if (g_shared_debugger_refcount > 0)
1053     {
1054         Mutex::Locker locker (GetDebuggerListMutex ());
1055         return GetDebuggerList().size();
1056     }
1057     return 0;
1058 }
1059 
1060 lldb::DebuggerSP
1061 Debugger::GetDebuggerAtIndex (size_t index)
1062 {
1063     DebuggerSP debugger_sp;
1064 
1065     if (g_shared_debugger_refcount > 0)
1066     {
1067         Mutex::Locker locker (GetDebuggerListMutex ());
1068         DebuggerList &debugger_list = GetDebuggerList();
1069 
1070         if (index < debugger_list.size())
1071             debugger_sp = debugger_list[index];
1072     }
1073 
1074     return debugger_sp;
1075 }
1076 
1077 DebuggerSP
1078 Debugger::FindDebuggerWithID (lldb::user_id_t id)
1079 {
1080     DebuggerSP debugger_sp;
1081 
1082     if (g_shared_debugger_refcount > 0)
1083     {
1084         Mutex::Locker locker (GetDebuggerListMutex ());
1085         DebuggerList &debugger_list = GetDebuggerList();
1086         DebuggerList::iterator pos, end = debugger_list.end();
1087         for (pos = debugger_list.begin(); pos != end; ++pos)
1088         {
1089             if ((*pos).get()->GetID() == id)
1090             {
1091                 debugger_sp = *pos;
1092                 break;
1093             }
1094         }
1095     }
1096     return debugger_sp;
1097 }
1098 
1099 #if 0
1100 static void
1101 TestPromptFormats (StackFrame *frame)
1102 {
1103     if (frame == NULL)
1104         return;
1105 
1106     StreamString s;
1107     const char *prompt_format =
1108     "{addr = '${addr}'\n}"
1109     "{process.id = '${process.id}'\n}"
1110     "{process.name = '${process.name}'\n}"
1111     "{process.file.basename = '${process.file.basename}'\n}"
1112     "{process.file.fullpath = '${process.file.fullpath}'\n}"
1113     "{thread.id = '${thread.id}'\n}"
1114     "{thread.index = '${thread.index}'\n}"
1115     "{thread.name = '${thread.name}'\n}"
1116     "{thread.queue = '${thread.queue}'\n}"
1117     "{thread.stop-reason = '${thread.stop-reason}'\n}"
1118     "{target.arch = '${target.arch}'\n}"
1119     "{module.file.basename = '${module.file.basename}'\n}"
1120     "{module.file.fullpath = '${module.file.fullpath}'\n}"
1121     "{file.basename = '${file.basename}'\n}"
1122     "{file.fullpath = '${file.fullpath}'\n}"
1123     "{frame.index = '${frame.index}'\n}"
1124     "{frame.pc = '${frame.pc}'\n}"
1125     "{frame.sp = '${frame.sp}'\n}"
1126     "{frame.fp = '${frame.fp}'\n}"
1127     "{frame.flags = '${frame.flags}'\n}"
1128     "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1129     "{frame.reg.rip = '${frame.reg.rip}'\n}"
1130     "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1131     "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1132     "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1133     "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1134     "{frame.reg.carp = '${frame.reg.carp}'\n}"
1135     "{function.id = '${function.id}'\n}"
1136     "{function.name = '${function.name}'\n}"
1137     "{function.name-with-args = '${function.name-with-args}'\n}"
1138     "{function.addr-offset = '${function.addr-offset}'\n}"
1139     "{function.line-offset = '${function.line-offset}'\n}"
1140     "{function.pc-offset = '${function.pc-offset}'\n}"
1141     "{line.file.basename = '${line.file.basename}'\n}"
1142     "{line.file.fullpath = '${line.file.fullpath}'\n}"
1143     "{line.number = '${line.number}'\n}"
1144     "{line.start-addr = '${line.start-addr}'\n}"
1145     "{line.end-addr = '${line.end-addr}'\n}"
1146 ;
1147 
1148     SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1149     ExecutionContext exe_ctx;
1150     frame->CalculateExecutionContext(exe_ctx);
1151     if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
1152     {
1153         printf("%s\n", s.GetData());
1154     }
1155     else
1156     {
1157         printf ("what we got: %s\n", s.GetData());
1158     }
1159 }
1160 #endif
1161 
1162 static bool
1163 ScanFormatDescriptor (const char* var_name_begin,
1164                       const char* var_name_end,
1165                       const char** var_name_final,
1166                       const char** percent_position,
1167                       Format* custom_format,
1168                       ValueObject::ValueObjectRepresentationStyle* val_obj_display)
1169 {
1170     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1171     *percent_position = ::strchr(var_name_begin,'%');
1172     if (!*percent_position || *percent_position > var_name_end)
1173     {
1174         if (log)
1175             log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
1176         *var_name_final = var_name_end;
1177     }
1178     else
1179     {
1180         *var_name_final = *percent_position;
1181         std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
1182         if (log)
1183             log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
1184         if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
1185                                                   true,
1186                                                   *custom_format) )
1187         {
1188             if (log)
1189                 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
1190 
1191             switch (format_name.front())
1192             {
1193                 case '@':             // if this is an @ sign, print ObjC description
1194                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1195                     break;
1196                 case 'V': // if this is a V, print the value using the default format
1197                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1198                     break;
1199                 case 'L': // if this is an L, print the location of the value
1200                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1201                     break;
1202                 case 'S': // if this is an S, print the summary after all
1203                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1204                     break;
1205                 case '#': // if this is a '#', print the number of children
1206                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1207                     break;
1208                 case 'T': // if this is a 'T', print the type
1209                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1210                     break;
1211                 case 'N': // if this is a 'N', print the name
1212                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1213                     break;
1214                 case '>': // if this is a '>', print the name
1215                     *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1216                     break;
1217                 default:
1218                     if (log)
1219                         log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
1220                     break;
1221             }
1222         }
1223         // a good custom format tells us to print the value using it
1224         else
1225         {
1226             if (log)
1227                 log->Printf("[ScanFormatDescriptor] will display value for this VO");
1228             *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1229         }
1230     }
1231     if (log)
1232         log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
1233                     *custom_format,
1234                     *val_obj_display);
1235     return true;
1236 }
1237 
1238 static bool
1239 ScanBracketedRange (const char* var_name_begin,
1240                     const char* var_name_end,
1241                     const char* var_name_final,
1242                     const char** open_bracket_position,
1243                     const char** separator_position,
1244                     const char** close_bracket_position,
1245                     const char** var_name_final_if_array_range,
1246                     int64_t* index_lower,
1247                     int64_t* index_higher)
1248 {
1249     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1250     *open_bracket_position = ::strchr(var_name_begin,'[');
1251     if (*open_bracket_position && *open_bracket_position < var_name_final)
1252     {
1253         *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1254         *close_bracket_position = ::strchr(*open_bracket_position,']');
1255         // as usual, we assume that [] will come before %
1256         //printf("trying to expand a []\n");
1257         *var_name_final_if_array_range = *open_bracket_position;
1258         if (*close_bracket_position - *open_bracket_position == 1)
1259         {
1260             if (log)
1261                 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
1262             *index_lower = 0;
1263         }
1264         else if (*separator_position == NULL || *separator_position > var_name_end)
1265         {
1266             char *end = NULL;
1267             *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1268             *index_higher = *index_lower;
1269             if (log)
1270                 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
1271         }
1272         else if (*close_bracket_position && *close_bracket_position < var_name_end)
1273         {
1274             char *end = NULL;
1275             *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1276             *index_higher = ::strtoul (*separator_position+1, &end, 0);
1277             if (log)
1278                 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
1279         }
1280         else
1281         {
1282             if (log)
1283                 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
1284             return false;
1285         }
1286         if (*index_lower > *index_higher && *index_higher > 0)
1287         {
1288             if (log)
1289                 log->Printf("[ScanBracketedRange] swapping indices");
1290             int64_t temp = *index_lower;
1291             *index_lower = *index_higher;
1292             *index_higher = temp;
1293         }
1294     }
1295     else if (log)
1296             log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
1297     return true;
1298 }
1299 
1300 template <typename T>
1301 static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1302 {
1303     if (script_interpreter)
1304     {
1305         Error script_error;
1306         std::string script_output;
1307 
1308         if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1309         {
1310             s.Printf("%s", script_output.c_str());
1311             return true;
1312         }
1313         else
1314         {
1315             s.Printf("<error: %s>",script_error.AsCString());
1316         }
1317     }
1318     return false;
1319 }
1320 
1321 static ValueObjectSP
1322 ExpandIndexedExpression (ValueObject* valobj,
1323                          size_t index,
1324                          StackFrame* frame,
1325                          bool deref_pointer)
1326 {
1327     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1328     const char* ptr_deref_format = "[%d]";
1329     std::string ptr_deref_buffer(10,0);
1330     ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
1331     if (log)
1332         log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
1333     const char* first_unparsed;
1334     ValueObject::GetValueForExpressionPathOptions options;
1335     ValueObject::ExpressionPathEndResultType final_value_type;
1336     ValueObject::ExpressionPathScanEndReason reason_to_stop;
1337     ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
1338     ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
1339                                                           &first_unparsed,
1340                                                           &reason_to_stop,
1341                                                           &final_value_type,
1342                                                           options,
1343                                                           &what_next);
1344     if (!item)
1345     {
1346         if (log)
1347             log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
1348                " final_value_type %d",
1349                first_unparsed, reason_to_stop, final_value_type);
1350     }
1351     else
1352     {
1353         if (log)
1354             log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1355                " final_value_type %d",
1356                first_unparsed, reason_to_stop, final_value_type);
1357     }
1358     return item;
1359 }
1360 
1361 static inline bool
1362 IsToken(const char *var_name_begin, const char *var)
1363 {
1364     return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1365 }
1366 
1367 static bool
1368 IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1369     const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1370 {
1371     int var_len = strlen(var);
1372     if (::strncmp (var_name_begin, var, var_len) == 0)
1373     {
1374         var_name_begin += var_len;
1375         if (*var_name_begin == '}')
1376         {
1377             format = default_format;
1378             return true;
1379         }
1380         else if (*var_name_begin == '%')
1381         {
1382             // Allow format specifiers: x|X|u with optional width specifiers.
1383             //   ${thread.id%x}    ; hex
1384             //   ${thread.id%X}    ; uppercase hex
1385             //   ${thread.id%u}    ; unsigned decimal
1386             //   ${thread.id%8.8X} ; width.precision + specifier
1387             //   ${thread.id%tid}  ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1388             int dot_count = 0;
1389             const char *specifier = NULL;
1390             int width_precision_length = 0;
1391             const char *width_precision = ++var_name_begin;
1392             while (isdigit(*var_name_begin) || *var_name_begin == '.')
1393             {
1394                 dot_count += (*var_name_begin == '.');
1395                 if (dot_count > 1)
1396                     break;
1397                 var_name_begin++;
1398                 width_precision_length++;
1399             }
1400 
1401             if (IsToken (var_name_begin, "tid}"))
1402             {
1403                 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1404                 if (target)
1405                 {
1406                     ArchSpec arch (target->GetArchitecture ());
1407                     llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1408                     if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1409                         specifier = PRIu64;
1410                 }
1411                 if (!specifier)
1412                 {
1413                     format = default_format;
1414                     return true;
1415                 }
1416             }
1417             else if (IsToken (var_name_begin, "x}"))
1418                 specifier = PRIx64;
1419             else if (IsToken (var_name_begin, "X}"))
1420                 specifier = PRIX64;
1421             else if (IsToken (var_name_begin, "u}"))
1422                 specifier = PRIu64;
1423 
1424             if (specifier)
1425             {
1426                 format = "%";
1427                 if (width_precision_length)
1428                     format += std::string(width_precision, width_precision_length);
1429                 format += specifier;
1430                 return true;
1431             }
1432         }
1433     }
1434     return false;
1435 }
1436 
1437 // Find information for the "thread.info.*" specifiers in a format string
1438 static bool
1439 FormatThreadExtendedInfoRecurse
1440 (
1441     const char *var_name_begin,
1442     StructuredData::ObjectSP thread_info_dictionary,
1443     const SymbolContext *sc,
1444     const ExecutionContext *exe_ctx,
1445     Stream &s
1446 )
1447 {
1448     bool var_success = false;
1449     std::string token_format;
1450 
1451     llvm::StringRef var_name(var_name_begin);
1452     size_t percent_idx = var_name.find('%');
1453     size_t close_curly_idx = var_name.find('}');
1454     llvm::StringRef path = var_name;
1455     llvm::StringRef formatter = var_name;
1456 
1457     // 'path' will be the dot separated list of objects to transverse up until we hit
1458     // a close curly brace, a percent sign, or an end of string.
1459     if (percent_idx != llvm::StringRef::npos || close_curly_idx != llvm::StringRef::npos)
1460     {
1461         if (percent_idx != llvm::StringRef::npos && close_curly_idx != llvm::StringRef::npos)
1462         {
1463             if (percent_idx < close_curly_idx)
1464             {
1465                 path = var_name.slice(0, percent_idx);
1466                 formatter = var_name.substr (percent_idx);
1467             }
1468             else
1469             {
1470                 path = var_name.slice(0, close_curly_idx);
1471                 formatter = var_name.substr (close_curly_idx);
1472             }
1473         }
1474         else if (percent_idx != llvm::StringRef::npos)
1475         {
1476             path = var_name.slice(0, percent_idx);
1477             formatter = var_name.substr (percent_idx);
1478         }
1479         else if (close_curly_idx != llvm::StringRef::npos)
1480         {
1481             path = var_name.slice(0, close_curly_idx);
1482             formatter = var_name.substr (close_curly_idx);
1483         }
1484     }
1485 
1486     StructuredData::ObjectSP value = thread_info_dictionary->GetObjectForDotSeparatedPath (path);
1487 
1488     if (value.get())
1489     {
1490         if (value->GetType() == StructuredData::Type::eTypeInteger)
1491         {
1492             if (IsTokenWithFormat (formatter.str().c_str(), "", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
1493             {
1494                 s.Printf(token_format.c_str(), value->GetAsInteger()->GetValue());
1495                 var_success = true;
1496             }
1497         }
1498         else if (value->GetType() == StructuredData::Type::eTypeFloat)
1499         {
1500             s.Printf ("%f", value->GetAsFloat()->GetValue());
1501             var_success = true;
1502         }
1503         else if (value->GetType() == StructuredData::Type::eTypeString)
1504         {
1505             s.Printf("%s", value->GetAsString()->GetValue().c_str());
1506             var_success = true;
1507         }
1508         else if (value->GetType() == StructuredData::Type::eTypeArray)
1509         {
1510             if (value->GetAsArray()->GetSize() > 0)
1511             {
1512                 s.Printf ("%zu", value->GetAsArray()->GetSize());
1513                 var_success = true;
1514             }
1515         }
1516         else if (value->GetType() == StructuredData::Type::eTypeDictionary)
1517         {
1518             s.Printf ("%zu", value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize());
1519             var_success = true;
1520         }
1521     }
1522 
1523     return var_success;
1524 }
1525 
1526 
1527 static bool
1528 FormatPromptRecurse
1529 (
1530     const char *format,
1531     const SymbolContext *sc,
1532     const ExecutionContext *exe_ctx,
1533     const Address *addr,
1534     Stream &s,
1535     const char **end,
1536     ValueObject* valobj
1537 )
1538 {
1539     ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
1540     bool success = true;
1541     const char *p;
1542     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1543 
1544     for (p = format; *p != '\0'; ++p)
1545     {
1546         if (realvalobj)
1547         {
1548             valobj = realvalobj;
1549             realvalobj = NULL;
1550         }
1551         size_t non_special_chars = ::strcspn (p, "${}\\");
1552         if (non_special_chars > 0)
1553         {
1554             if (success)
1555                 s.Write (p, non_special_chars);
1556             p += non_special_chars;
1557         }
1558 
1559         if (*p == '\0')
1560         {
1561             break;
1562         }
1563         else if (*p == '{')
1564         {
1565             // Start a new scope that must have everything it needs if it is to
1566             // to make it into the final output stream "s". If you want to make
1567             // a format that only prints out the function or symbol name if there
1568             // is one in the symbol context you can use:
1569             //      "{function =${function.name}}"
1570             // The first '{' starts a new scope that end with the matching '}' at
1571             // the end of the string. The contents "function =${function.name}"
1572             // will then be evaluated and only be output if there is a function
1573             // or symbol with a valid name.
1574             StreamString sub_strm;
1575 
1576             ++p;  // Skip the '{'
1577 
1578             if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
1579             {
1580                 // The stream had all it needed
1581                 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1582             }
1583             if (*p != '}')
1584             {
1585                 success = false;
1586                 break;
1587             }
1588         }
1589         else if (*p == '}')
1590         {
1591             // End of a enclosing scope
1592             break;
1593         }
1594         else if (*p == '$')
1595         {
1596             // We have a prompt variable to print
1597             ++p;
1598             if (*p == '{')
1599             {
1600                 ++p;
1601                 const char *var_name_begin = p;
1602                 const char *var_name_end = ::strchr (p, '}');
1603 
1604                 if (var_name_end && var_name_begin < var_name_end)
1605                 {
1606                     // if we have already failed to parse, skip this variable
1607                     if (success)
1608                     {
1609                         const char *cstr = NULL;
1610                         std::string token_format;
1611                         Address format_addr;
1612                         bool calculate_format_addr_function_offset = false;
1613                         // Set reg_kind and reg_num to invalid values
1614                         RegisterKind reg_kind = kNumRegisterKinds;
1615                         uint32_t reg_num = LLDB_INVALID_REGNUM;
1616                         FileSpec format_file_spec;
1617                         const RegisterInfo *reg_info = NULL;
1618                         RegisterContext *reg_ctx = NULL;
1619                         bool do_deref_pointer = false;
1620                         ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1621                         ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
1622 
1623                         // Each variable must set success to true below...
1624                         bool var_success = false;
1625                         switch (var_name_begin[0])
1626                         {
1627                         case '*':
1628                         case 'v':
1629                         case 's':
1630                             {
1631                                 if (!valobj)
1632                                     break;
1633 
1634                                 if (log)
1635                                     log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
1636 
1637                                 // check for *var and *svar
1638                                 if (*var_name_begin == '*')
1639                                 {
1640                                     do_deref_pointer = true;
1641                                     var_name_begin++;
1642                                     if (log)
1643                                         log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
1644                                 }
1645 
1646                                 if (*var_name_begin == 's')
1647                                 {
1648                                     if (!valobj->IsSynthetic())
1649                                         valobj = valobj->GetSyntheticValue().get();
1650                                     if (!valobj)
1651                                         break;
1652                                     var_name_begin++;
1653                                     if (log)
1654                                         log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
1655                                 }
1656 
1657                                 // should be a 'v' by now
1658                                 if (*var_name_begin != 'v')
1659                                     break;
1660 
1661                                 if (log)
1662                                     log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
1663 
1664                                 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
1665                                                                                   ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
1666                                 ValueObject::GetValueForExpressionPathOptions options;
1667                                 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
1668                                 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1669                                 ValueObject* target = NULL;
1670                                 Format custom_format = eFormatInvalid;
1671                                 const char* var_name_final = NULL;
1672                                 const char* var_name_final_if_array_range = NULL;
1673                                 const char* close_bracket_position = NULL;
1674                                 int64_t index_lower = -1;
1675                                 int64_t index_higher = -1;
1676                                 bool is_array_range = false;
1677                                 const char* first_unparsed;
1678                                 bool was_plain_var = false;
1679                                 bool was_var_format = false;
1680                                 bool was_var_indexed = false;
1681 
1682                                 if (!valobj) break;
1683                                 // simplest case ${var}, just print valobj's value
1684                                 if (IsToken (var_name_begin, "var}"))
1685                                 {
1686                                     was_plain_var = true;
1687                                     target = valobj;
1688                                     val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1689                                 }
1690                                 else if (IsToken (var_name_begin,"var%"))
1691                                 {
1692                                     was_var_format = true;
1693                                     // this is a variable with some custom format applied to it
1694                                     const char* percent_position;
1695                                     target = valobj;
1696                                     val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1697                                     ScanFormatDescriptor (var_name_begin,
1698                                                           var_name_end,
1699                                                           &var_name_final,
1700                                                           &percent_position,
1701                                                           &custom_format,
1702                                                           &val_obj_display);
1703                                 }
1704                                     // this is ${var.something} or multiple .something nested
1705                                 else if (IsToken (var_name_begin, "var"))
1706                                 {
1707                                     if (IsToken (var_name_begin, "var["))
1708                                         was_var_indexed = true;
1709                                     const char* percent_position;
1710                                     ScanFormatDescriptor (var_name_begin,
1711                                                           var_name_end,
1712                                                           &var_name_final,
1713                                                           &percent_position,
1714                                                           &custom_format,
1715                                                           &val_obj_display);
1716 
1717                                     const char* open_bracket_position;
1718                                     const char* separator_position;
1719                                     ScanBracketedRange (var_name_begin,
1720                                                         var_name_end,
1721                                                         var_name_final,
1722                                                         &open_bracket_position,
1723                                                         &separator_position,
1724                                                         &close_bracket_position,
1725                                                         &var_name_final_if_array_range,
1726                                                         &index_lower,
1727                                                         &index_higher);
1728 
1729                                     Error error;
1730 
1731                                     std::string expr_path(var_name_final-var_name_begin-1,0);
1732                                     memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1733 
1734                                     if (log)
1735                                         log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1736 
1737                                     target = valobj->GetValueForExpressionPath(expr_path.c_str(),
1738                                                                              &first_unparsed,
1739                                                                              &reason_to_stop,
1740                                                                              &final_value_type,
1741                                                                              options,
1742                                                                              &what_next).get();
1743 
1744                                     if (!target)
1745                                     {
1746                                         if (log)
1747                                             log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
1748                                                " final_value_type %d",
1749                                                first_unparsed, reason_to_stop, final_value_type);
1750                                         break;
1751                                     }
1752                                     else
1753                                     {
1754                                         if (log)
1755                                             log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1756                                                " final_value_type %d",
1757                                                first_unparsed, reason_to_stop, final_value_type);
1758                                     }
1759                                 }
1760                                 else
1761                                     break;
1762 
1763                                 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1764                                                   final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
1765 
1766                                 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
1767 
1768                                 if (do_deref_pointer && !is_array_range)
1769                                 {
1770                                     // I have not deref-ed yet, let's do it
1771                                     // this happens when we are not going through GetValueForVariableExpressionPath
1772                                     // to get to the target ValueObject
1773                                     Error error;
1774                                     target = target->Dereference(error).get();
1775                                     if (error.Fail())
1776                                     {
1777                                         if (log)
1778                                             log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
1779                                         break;
1780                                     }
1781                                     do_deref_pointer = false;
1782                                 }
1783 
1784                                 if (!target)
1785                                 {
1786                                     if (log)
1787                                         log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
1788                                     break;
1789                                 }
1790 
1791                                 // we do not want to use the summary for a bitfield of type T:n
1792                                 // if we were originally dealing with just a T - that would get
1793                                 // us into an endless recursion
1794                                 if (target->IsBitfield() && was_var_indexed)
1795                                 {
1796                                     // TODO: check for a (T:n)-specific summary - we should still obey that
1797                                     StreamString bitfield_name;
1798                                     bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1799                                     lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1800                                     if (!DataVisualization::GetSummaryForType(type_sp))
1801                                         val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1802                                 }
1803 
1804                                 // TODO use flags for these
1805                                 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1806                                 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1807                                 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1808                                 bool is_aggregate = target->GetClangType().IsAggregateType();
1809 
1810                                 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions
1811                                 {
1812                                     StreamString str_temp;
1813                                     if (log)
1814                                         log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
1815 
1816                                     if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
1817                                     {
1818                                         // try to use the special cases
1819                                         var_success = target->DumpPrintableRepresentation(str_temp,
1820                                                                                           val_obj_display,
1821                                                                                           custom_format);
1822                                         if (log)
1823                                             log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
1824 
1825                                         // should not happen
1826                                         if (var_success)
1827                                             s << str_temp.GetData();
1828                                         var_success = true;
1829                                         break;
1830                                     }
1831                                     else
1832                                     {
1833                                         if (was_plain_var) // if ${var}
1834                                         {
1835                                             s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1836                                         }
1837                                         else if (is_pointer) // if pointer, value is the address stored
1838                                         {
1839                                             target->DumpPrintableRepresentation (s,
1840                                                                                  val_obj_display,
1841                                                                                  custom_format,
1842                                                                                  ValueObject::ePrintableRepresentationSpecialCasesDisable);
1843                                         }
1844                                         var_success = true;
1845                                         break;
1846                                     }
1847                                 }
1848 
1849                                 // if directly trying to print ${var}, and this is an aggregate, display a nice
1850                                 // type @ location message
1851                                 if (is_aggregate && was_plain_var)
1852                                 {
1853                                     s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1854                                     var_success = true;
1855                                     break;
1856                                 }
1857 
1858                                 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it
1859                                 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
1860                                 {
1861                                     s << "<invalid use of aggregate type>";
1862                                     var_success = true;
1863                                     break;
1864                                 }
1865 
1866                                 if (!is_array_range)
1867                                 {
1868                                     if (log)
1869                                         log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
1870                                     var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1871                                 }
1872                                 else
1873                                 {
1874                                     if (log)
1875                                         log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
1876                                     if (!is_array && !is_pointer)
1877                                         break;
1878                                     if (log)
1879                                         log->Printf("[Debugger::FormatPrompt] handle as array");
1880                                     const char* special_directions = NULL;
1881                                     StreamString special_directions_writer;
1882                                     if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1883                                     {
1884                                         ConstString additional_data;
1885                                         additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1886                                         special_directions_writer.Printf("${%svar%s}",
1887                                                                          do_deref_pointer ? "*" : "",
1888                                                                          additional_data.GetCString());
1889                                         special_directions = special_directions_writer.GetData();
1890                                     }
1891 
1892                                     // let us display items index_lower thru index_higher of this array
1893                                     s.PutChar('[');
1894                                     var_success = true;
1895 
1896                                     if (index_higher < 0)
1897                                         index_higher = valobj->GetNumChildren() - 1;
1898 
1899                                     uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
1900 
1901                                     for (;index_lower<=index_higher;index_lower++)
1902                                     {
1903                                         ValueObject* item = ExpandIndexedExpression (target,
1904                                                                                      index_lower,
1905                                                                                      exe_ctx->GetFramePtr(),
1906                                                                                      false).get();
1907 
1908                                         if (!item)
1909                                         {
1910                                             if (log)
1911                                                 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
1912                                         }
1913                                         else
1914                                         {
1915                                             if (log)
1916                                                 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
1917                                         }
1918 
1919                                         if (!special_directions)
1920                                             var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1921                                         else
1922                                             var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
1923 
1924                                         if (--max_num_children == 0)
1925                                         {
1926                                             s.PutCString(", ...");
1927                                             break;
1928                                         }
1929 
1930                                         if (index_lower < index_higher)
1931                                             s.PutChar(',');
1932                                     }
1933                                     s.PutChar(']');
1934                                 }
1935                             }
1936                             break;
1937                         case 'a':
1938                             if (IsToken (var_name_begin, "addr}"))
1939                             {
1940                                 if (addr && addr->IsValid())
1941                                 {
1942                                     var_success = true;
1943                                     format_addr = *addr;
1944                                 }
1945                             }
1946                             break;
1947 
1948                         case 'p':
1949                             if (IsToken (var_name_begin, "process."))
1950                             {
1951                                 if (exe_ctx)
1952                                 {
1953                                     Process *process = exe_ctx->GetProcessPtr();
1954                                     if (process)
1955                                     {
1956                                         var_name_begin += ::strlen ("process.");
1957                                         if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
1958                                         {
1959                                             s.Printf(token_format.c_str(), process->GetID());
1960                                             var_success = true;
1961                                         }
1962                                         else if ((IsToken (var_name_begin, "name}")) ||
1963                                                 (IsToken (var_name_begin, "file.basename}")) ||
1964                                                 (IsToken (var_name_begin, "file.fullpath}")))
1965                                         {
1966                                             Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1967                                             if (exe_module)
1968                                             {
1969                                                 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1970                                                 {
1971                                                     format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1972                                                     var_success = (bool)format_file_spec;
1973                                                 }
1974                                                 else
1975                                                 {
1976                                                     format_file_spec = exe_module->GetFileSpec();
1977                                                     var_success = (bool)format_file_spec;
1978                                                 }
1979                                             }
1980                                         }
1981                                         else if (IsToken (var_name_begin, "script:"))
1982                                         {
1983                                             var_name_begin += ::strlen("script:");
1984                                             std::string script_name(var_name_begin,var_name_end);
1985                                             ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
1986                                             if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
1987                                                 var_success = true;
1988                                         }
1989                                     }
1990                                 }
1991                             }
1992                             break;
1993 
1994                         case 't':
1995                            if (IsToken (var_name_begin, "thread."))
1996                             {
1997                                 if (exe_ctx)
1998                                 {
1999                                     Thread *thread = exe_ctx->GetThreadPtr();
2000                                     if (thread)
2001                                     {
2002                                         var_name_begin += ::strlen ("thread.");
2003                                         if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
2004                                         {
2005                                             s.Printf(token_format.c_str(), thread->GetID());
2006                                             var_success = true;
2007                                         }
2008                                         else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
2009                                         {
2010                                             s.Printf(token_format.c_str(), thread->GetProtocolID());
2011                                             var_success = true;
2012                                         }
2013                                         else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
2014                                         {
2015                                             s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
2016                                             var_success = true;
2017                                         }
2018                                         else if (IsToken (var_name_begin, "name}"))
2019                                         {
2020                                             cstr = thread->GetName();
2021                                             var_success = cstr && cstr[0];
2022                                             if (var_success)
2023                                                 s.PutCString(cstr);
2024                                         }
2025                                         else if (IsToken (var_name_begin, "queue}"))
2026                                         {
2027                                             cstr = thread->GetQueueName();
2028                                             var_success = cstr && cstr[0];
2029                                             if (var_success)
2030                                                 s.PutCString(cstr);
2031                                         }
2032                                         else if (IsToken (var_name_begin, "stop-reason}"))
2033                                         {
2034                                             StopInfoSP stop_info_sp = thread->GetStopInfo ();
2035                                             if (stop_info_sp && stop_info_sp->IsValid())
2036                                             {
2037                                                 cstr = stop_info_sp->GetDescription();
2038                                                 if (cstr && cstr[0])
2039                                                 {
2040                                                     s.PutCString(cstr);
2041                                                     var_success = true;
2042                                                 }
2043                                             }
2044                                         }
2045                                         else if (IsToken (var_name_begin, "return-value}"))
2046                                         {
2047                                             StopInfoSP stop_info_sp = thread->GetStopInfo ();
2048                                             if (stop_info_sp && stop_info_sp->IsValid())
2049                                             {
2050                                                 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2051                                                 if (return_valobj_sp)
2052                                                 {
2053                                                     return_valobj_sp->Dump(s);
2054                                                     var_success = true;
2055                                                 }
2056                                             }
2057                                         }
2058                                         else if (IsToken (var_name_begin, "script:"))
2059                                         {
2060                                             var_name_begin += ::strlen("script:");
2061                                             std::string script_name(var_name_begin,var_name_end);
2062                                             ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
2063                                             if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
2064                                                 var_success = true;
2065                                         }
2066                                         else if (IsToken (var_name_begin, "info."))
2067                                         {
2068                                             var_name_begin += ::strlen("info.");
2069                                             StructuredData::ObjectSP object_sp = thread->GetExtendedInfo();
2070                                             if (object_sp && object_sp->GetType() == StructuredData::Type::eTypeDictionary)
2071                                             {
2072                                                 var_success = FormatThreadExtendedInfoRecurse (var_name_begin, object_sp, sc, exe_ctx, s);
2073                                             }
2074                                         }
2075                                     }
2076                                 }
2077                             }
2078                             else if (IsToken (var_name_begin, "target."))
2079                             {
2080                                 // TODO: hookup properties
2081 //                                if (!target_properties_sp)
2082 //                                {
2083 //                                    Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2084 //                                    if (target)
2085 //                                        target_properties_sp = target->GetProperties();
2086 //                                }
2087 //
2088 //                                if (target_properties_sp)
2089 //                                {
2090 //                                    var_name_begin += ::strlen ("target.");
2091 //                                    const char *end_property = strchr(var_name_begin, '}');
2092 //                                    if (end_property)
2093 //                                    {
2094 //                                        ConstString property_name(var_name_begin, end_property - var_name_begin);
2095 //                                        std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2096 //                                        if (!property_value.empty())
2097 //                                        {
2098 //                                            s.PutCString (property_value.c_str());
2099 //                                            var_success = true;
2100 //                                        }
2101 //                                    }
2102 //                                }
2103                                 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2104                                 if (target)
2105                                 {
2106                                     var_name_begin += ::strlen ("target.");
2107                                     if (IsToken (var_name_begin, "arch}"))
2108                                     {
2109                                         ArchSpec arch (target->GetArchitecture ());
2110                                         if (arch.IsValid())
2111                                         {
2112                                             s.PutCString (arch.GetArchitectureName());
2113                                             var_success = true;
2114                                         }
2115                                     }
2116                                     else if (IsToken (var_name_begin, "script:"))
2117                                     {
2118                                         var_name_begin += ::strlen("script:");
2119                                         std::string script_name(var_name_begin,var_name_end);
2120                                         ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
2121                                         if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2122                                             var_success = true;
2123                                     }
2124                                 }
2125                             }
2126                             break;
2127 
2128 
2129                         case 'm':
2130                            if (IsToken (var_name_begin, "module."))
2131                             {
2132                                 if (sc && sc->module_sp.get())
2133                                 {
2134                                     Module *module = sc->module_sp.get();
2135                                     var_name_begin += ::strlen ("module.");
2136 
2137                                     if (IsToken (var_name_begin, "file."))
2138                                     {
2139                                         if (module->GetFileSpec())
2140                                         {
2141                                             var_name_begin += ::strlen ("file.");
2142 
2143                                             if (IsToken (var_name_begin, "basename}"))
2144                                             {
2145                                                 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2146                                                 var_success = (bool)format_file_spec;
2147                                             }
2148                                             else if (IsToken (var_name_begin, "fullpath}"))
2149                                             {
2150                                                 format_file_spec = module->GetFileSpec();
2151                                                 var_success = (bool)format_file_spec;
2152                                             }
2153                                         }
2154                                     }
2155                                 }
2156                             }
2157                             break;
2158 
2159 
2160                         case 'f':
2161                            if (IsToken (var_name_begin, "file."))
2162                             {
2163                                 if (sc && sc->comp_unit != NULL)
2164                                 {
2165                                     var_name_begin += ::strlen ("file.");
2166 
2167                                     if (IsToken (var_name_begin, "basename}"))
2168                                     {
2169                                         format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2170                                         var_success = (bool)format_file_spec;
2171                                     }
2172                                     else if (IsToken (var_name_begin, "fullpath}"))
2173                                     {
2174                                         format_file_spec = *sc->comp_unit;
2175                                         var_success = (bool)format_file_spec;
2176                                     }
2177                                 }
2178                             }
2179                            else if (IsToken (var_name_begin, "frame."))
2180                             {
2181                                 if (exe_ctx)
2182                                 {
2183                                     StackFrame *frame = exe_ctx->GetFramePtr();
2184                                     if (frame)
2185                                     {
2186                                         var_name_begin += ::strlen ("frame.");
2187                                         if (IsToken (var_name_begin, "index}"))
2188                                         {
2189                                             s.Printf("%u", frame->GetFrameIndex());
2190                                             var_success = true;
2191                                         }
2192                                         else if (IsToken (var_name_begin, "pc}"))
2193                                         {
2194                                             reg_kind = eRegisterKindGeneric;
2195                                             reg_num = LLDB_REGNUM_GENERIC_PC;
2196                                             var_success = true;
2197                                         }
2198                                         else if (IsToken (var_name_begin, "sp}"))
2199                                         {
2200                                             reg_kind = eRegisterKindGeneric;
2201                                             reg_num = LLDB_REGNUM_GENERIC_SP;
2202                                             var_success = true;
2203                                         }
2204                                         else if (IsToken (var_name_begin, "fp}"))
2205                                         {
2206                                             reg_kind = eRegisterKindGeneric;
2207                                             reg_num = LLDB_REGNUM_GENERIC_FP;
2208                                             var_success = true;
2209                                         }
2210                                         else if (IsToken (var_name_begin, "flags}"))
2211                                         {
2212                                             reg_kind = eRegisterKindGeneric;
2213                                             reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2214                                             var_success = true;
2215                                         }
2216                                         else if (IsToken (var_name_begin, "reg."))
2217                                         {
2218                                             reg_ctx = frame->GetRegisterContext().get();
2219                                             if (reg_ctx)
2220                                             {
2221                                                 var_name_begin += ::strlen ("reg.");
2222                                                 if (var_name_begin < var_name_end)
2223                                                 {
2224                                                     std::string reg_name (var_name_begin, var_name_end);
2225                                                     reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2226                                                     if (reg_info)
2227                                                         var_success = true;
2228                                                 }
2229                                             }
2230                                         }
2231                                         else if (IsToken (var_name_begin, "script:"))
2232                                         {
2233                                             var_name_begin += ::strlen("script:");
2234                                             std::string script_name(var_name_begin,var_name_end);
2235                                             ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
2236                                             if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2237                                                 var_success = true;
2238                                         }
2239                                     }
2240                                 }
2241                             }
2242                             else if (IsToken (var_name_begin, "function."))
2243                             {
2244                                 if (sc && (sc->function != NULL || sc->symbol != NULL))
2245                                 {
2246                                     var_name_begin += ::strlen ("function.");
2247                                     if (IsToken (var_name_begin, "id}"))
2248                                     {
2249                                         if (sc->function)
2250                                             s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
2251                                         else
2252                                             s.Printf("symbol[%u]", sc->symbol->GetID());
2253 
2254                                         var_success = true;
2255                                     }
2256                                     else if (IsToken (var_name_begin, "name}"))
2257                                     {
2258                                         if (sc->function)
2259                                             cstr = sc->function->GetName().AsCString (NULL);
2260                                         else if (sc->symbol)
2261                                             cstr = sc->symbol->GetName().AsCString (NULL);
2262                                         if (cstr)
2263                                         {
2264                                             s.PutCString(cstr);
2265 
2266                                             if (sc->block)
2267                                             {
2268                                                 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2269                                                 if (inline_block)
2270                                                 {
2271                                                     const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2272                                                     if (inline_info)
2273                                                     {
2274                                                         s.PutCString(" [inlined] ");
2275                                                         inline_info->GetName().Dump(&s);
2276                                                     }
2277                                                 }
2278                                             }
2279                                             var_success = true;
2280                                         }
2281                                     }
2282                                     else if (IsToken (var_name_begin, "name-with-args}"))
2283                                     {
2284                                         // Print the function name with arguments in it
2285 
2286                                         if (sc->function)
2287                                         {
2288                                             var_success = true;
2289                                             ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2290                                             cstr = sc->function->GetName().AsCString (NULL);
2291                                             if (cstr)
2292                                             {
2293                                                 const InlineFunctionInfo *inline_info = NULL;
2294                                                 VariableListSP variable_list_sp;
2295                                                 bool get_function_vars = true;
2296                                                 if (sc->block)
2297                                                 {
2298                                                     Block *inline_block = sc->block->GetContainingInlinedBlock ();
2299 
2300                                                     if (inline_block)
2301                                                     {
2302                                                         get_function_vars = false;
2303                                                         inline_info = sc->block->GetInlinedFunctionInfo();
2304                                                         if (inline_info)
2305                                                             variable_list_sp = inline_block->GetBlockVariableList (true);
2306                                                     }
2307                                                 }
2308 
2309                                                 if (get_function_vars)
2310                                                 {
2311                                                     variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2312                                                 }
2313 
2314                                                 if (inline_info)
2315                                                 {
2316                                                     s.PutCString (cstr);
2317                                                     s.PutCString (" [inlined] ");
2318                                                     cstr = inline_info->GetName().GetCString();
2319                                                 }
2320 
2321                                                 VariableList args;
2322                                                 if (variable_list_sp)
2323                                                     variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
2324                                                 if (args.GetSize() > 0)
2325                                                 {
2326                                                     const char *open_paren = strchr (cstr, '(');
2327                                                     const char *close_paren = NULL;
2328                                                     if (open_paren)
2329                                                     {
2330                                                         if (IsToken (open_paren, "(anonymous namespace)"))
2331                                                         {
2332                                                             open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2333                                                             if (open_paren)
2334                                                                 close_paren = strchr (open_paren, ')');
2335                                                         }
2336                                                         else
2337                                                             close_paren = strchr (open_paren, ')');
2338                                                     }
2339 
2340                                                     if (open_paren)
2341                                                         s.Write(cstr, open_paren - cstr + 1);
2342                                                     else
2343                                                     {
2344                                                         s.PutCString (cstr);
2345                                                         s.PutChar ('(');
2346                                                     }
2347                                                     const size_t num_args = args.GetSize();
2348                                                     for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2349                                                     {
2350                                                         std::string buffer;
2351 
2352                                                         VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2353                                                         ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2354                                                         const char *var_representation = nullptr;
2355                                                         const char *var_name = var_value_sp->GetName().GetCString();
2356                                                         if (var_value_sp->GetClangType().IsAggregateType() &&
2357                                                             DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
2358                                                         {
2359                                                             static StringSummaryFormat format(TypeSummaryImpl::Flags()
2360                                                                                               .SetHideItemNames(false)
2361                                                                                               .SetShowMembersOneLiner(true),
2362                                                                                               "");
2363                                                             format.FormatObject(var_value_sp.get(), buffer);
2364                                                             var_representation = buffer.c_str();
2365                                                         }
2366                                                         else
2367                                                             var_representation = var_value_sp->GetValueAsCString();
2368                                                         if (arg_idx > 0)
2369                                                             s.PutCString (", ");
2370                                                         if (var_value_sp->GetError().Success())
2371                                                         {
2372                                                             if (var_representation)
2373                                                                 s.Printf ("%s=%s", var_name, var_representation);
2374                                                             else
2375                                                                 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2376                                                         }
2377                                                         else
2378                                                             s.Printf ("%s=<unavailable>", var_name);
2379                                                     }
2380 
2381                                                     if (close_paren)
2382                                                         s.PutCString (close_paren);
2383                                                     else
2384                                                         s.PutChar(')');
2385 
2386                                                 }
2387                                                 else
2388                                                 {
2389                                                     s.PutCString(cstr);
2390                                                 }
2391                                             }
2392                                         }
2393                                         else if (sc->symbol)
2394                                         {
2395                                             cstr = sc->symbol->GetName().AsCString (NULL);
2396                                             if (cstr)
2397                                             {
2398                                                 s.PutCString(cstr);
2399                                                 var_success = true;
2400                                             }
2401                                         }
2402                                     }
2403                                     else if (IsToken (var_name_begin, "addr-offset}"))
2404                                     {
2405                                         var_success = addr != NULL;
2406                                         if (var_success)
2407                                         {
2408                                             format_addr = *addr;
2409                                             calculate_format_addr_function_offset = true;
2410                                         }
2411                                     }
2412                                     else if (IsToken (var_name_begin, "line-offset}"))
2413                                     {
2414                                         var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2415                                         if (var_success)
2416                                         {
2417                                             format_addr = sc->line_entry.range.GetBaseAddress();
2418                                             calculate_format_addr_function_offset = true;
2419                                         }
2420                                     }
2421                                     else if (IsToken (var_name_begin, "pc-offset}"))
2422                                     {
2423                                         StackFrame *frame = exe_ctx->GetFramePtr();
2424                                         var_success = frame != NULL;
2425                                         if (var_success)
2426                                         {
2427                                             format_addr = frame->GetFrameCodeAddress();
2428                                             calculate_format_addr_function_offset = true;
2429                                         }
2430                                     }
2431                                 }
2432                             }
2433                             break;
2434 
2435                         case 'l':
2436                             if (IsToken (var_name_begin, "line."))
2437                             {
2438                                 if (sc && sc->line_entry.IsValid())
2439                                 {
2440                                     var_name_begin += ::strlen ("line.");
2441                                     if (IsToken (var_name_begin, "file."))
2442                                     {
2443                                         var_name_begin += ::strlen ("file.");
2444 
2445                                         if (IsToken (var_name_begin, "basename}"))
2446                                         {
2447                                             format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2448                                             var_success = (bool)format_file_spec;
2449                                         }
2450                                         else if (IsToken (var_name_begin, "fullpath}"))
2451                                         {
2452                                             format_file_spec = sc->line_entry.file;
2453                                             var_success = (bool)format_file_spec;
2454                                         }
2455                                     }
2456                                     else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
2457                                     {
2458                                         var_success = true;
2459                                         s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
2460                                     }
2461                                     else if ((IsToken (var_name_begin, "start-addr}")) ||
2462                                              (IsToken (var_name_begin, "end-addr}")))
2463                                     {
2464                                         var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2465                                         if (var_success)
2466                                         {
2467                                             format_addr = sc->line_entry.range.GetBaseAddress();
2468                                             if (var_name_begin[0] == 'e')
2469                                                 format_addr.Slide (sc->line_entry.range.GetByteSize());
2470                                         }
2471                                     }
2472                                 }
2473                             }
2474                             break;
2475                         }
2476 
2477                         if (var_success)
2478                         {
2479                             // If format addr is valid, then we need to print an address
2480                             if (reg_num != LLDB_INVALID_REGNUM)
2481                             {
2482                                 StackFrame *frame = exe_ctx->GetFramePtr();
2483                                 // We have a register value to display...
2484                                 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2485                                 {
2486                                     format_addr = frame->GetFrameCodeAddress();
2487                                 }
2488                                 else
2489                                 {
2490                                     if (reg_ctx == NULL)
2491                                         reg_ctx = frame->GetRegisterContext().get();
2492 
2493                                     if (reg_ctx)
2494                                     {
2495                                         if (reg_kind != kNumRegisterKinds)
2496                                             reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2497                                         reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2498                                         var_success = reg_info != NULL;
2499                                     }
2500                                 }
2501                             }
2502 
2503                             if (reg_info != NULL)
2504                             {
2505                                 RegisterValue reg_value;
2506                                 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2507                                 if (var_success)
2508                                 {
2509                                     reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
2510                                 }
2511                             }
2512 
2513                             if (format_file_spec)
2514                             {
2515                                 s << format_file_spec;
2516                             }
2517 
2518                             // If format addr is valid, then we need to print an address
2519                             if (format_addr.IsValid())
2520                             {
2521                                 var_success = false;
2522 
2523                                 if (calculate_format_addr_function_offset)
2524                                 {
2525                                     Address func_addr;
2526 
2527                                     if (sc)
2528                                     {
2529                                         if (sc->function)
2530                                         {
2531                                             func_addr = sc->function->GetAddressRange().GetBaseAddress();
2532                                             if (sc->block)
2533                                             {
2534                                                 // Check to make sure we aren't in an inline
2535                                                 // function. If we are, use the inline block
2536                                                 // range that contains "format_addr" since
2537                                                 // blocks can be discontiguous.
2538                                                 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2539                                                 AddressRange inline_range;
2540                                                 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2541                                                     func_addr = inline_range.GetBaseAddress();
2542                                             }
2543                                         }
2544                                         else if (sc->symbol && sc->symbol->ValueIsAddress())
2545                                             func_addr = sc->symbol->GetAddress();
2546                                     }
2547 
2548                                     if (func_addr.IsValid())
2549                                     {
2550                                         if (func_addr.GetSection() == format_addr.GetSection())
2551                                         {
2552                                             addr_t func_file_addr = func_addr.GetFileAddress();
2553                                             addr_t addr_file_addr = format_addr.GetFileAddress();
2554                                             if (addr_file_addr > func_file_addr)
2555                                                 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
2556                                             else if (addr_file_addr < func_file_addr)
2557                                                 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
2558                                             var_success = true;
2559                                         }
2560                                         else
2561                                         {
2562                                             Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2563                                             if (target)
2564                                             {
2565                                                 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2566                                                 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2567                                                 if (addr_load_addr > func_load_addr)
2568                                                     s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
2569                                                 else if (addr_load_addr < func_load_addr)
2570                                                     s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
2571                                                 var_success = true;
2572                                             }
2573                                         }
2574                                     }
2575                                 }
2576                                 else
2577                                 {
2578                                     Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2579                                     addr_t vaddr = LLDB_INVALID_ADDRESS;
2580                                     if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2581                                         vaddr = format_addr.GetLoadAddress (target);
2582                                     if (vaddr == LLDB_INVALID_ADDRESS)
2583                                         vaddr = format_addr.GetFileAddress ();
2584 
2585                                     if (vaddr != LLDB_INVALID_ADDRESS)
2586                                     {
2587                                         int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
2588                                         if (addr_width == 0)
2589                                             addr_width = 16;
2590                                         s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
2591                                         var_success = true;
2592                                     }
2593                                 }
2594                             }
2595                         }
2596 
2597                         if (var_success == false)
2598                             success = false;
2599                     }
2600                     p = var_name_end;
2601                 }
2602                 else
2603                     break;
2604             }
2605             else
2606             {
2607                 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2608                 s.PutChar(*p);
2609             }
2610         }
2611         else if (*p == '\\')
2612         {
2613             ++p; // skip the slash
2614             switch (*p)
2615             {
2616             case 'a': s.PutChar ('\a'); break;
2617             case 'b': s.PutChar ('\b'); break;
2618             case 'f': s.PutChar ('\f'); break;
2619             case 'n': s.PutChar ('\n'); break;
2620             case 'r': s.PutChar ('\r'); break;
2621             case 't': s.PutChar ('\t'); break;
2622             case 'v': s.PutChar ('\v'); break;
2623             case '\'': s.PutChar ('\''); break;
2624             case '\\': s.PutChar ('\\'); break;
2625             case '0':
2626                 // 1 to 3 octal chars
2627                 {
2628                     // Make a string that can hold onto the initial zero char,
2629                     // up to 3 octal digits, and a terminating NULL.
2630                     char oct_str[5] = { 0, 0, 0, 0, 0 };
2631 
2632                     int i;
2633                     for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2634                         oct_str[i] = p[i];
2635 
2636                     // We don't want to consume the last octal character since
2637                     // the main for loop will do this for us, so we advance p by
2638                     // one less than i (even if i is zero)
2639                     p += i - 1;
2640                     unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2641                     if (octal_value <= UINT8_MAX)
2642                     {
2643                         s.PutChar((char)octal_value);
2644                     }
2645                 }
2646                 break;
2647 
2648             case 'x':
2649                 // hex number in the format
2650                 if (isxdigit(p[1]))
2651                 {
2652                     ++p;    // Skip the 'x'
2653 
2654                     // Make a string that can hold onto two hex chars plus a
2655                     // NULL terminator
2656                     char hex_str[3] = { 0,0,0 };
2657                     hex_str[0] = *p;
2658                     if (isxdigit(p[1]))
2659                     {
2660                         ++p; // Skip the first of the two hex chars
2661                         hex_str[1] = *p;
2662                     }
2663 
2664                     unsigned long hex_value = strtoul (hex_str, NULL, 16);
2665                     if (hex_value <= UINT8_MAX)
2666                         s.PutChar ((char)hex_value);
2667                 }
2668                 else
2669                 {
2670                     s.PutChar('x');
2671                 }
2672                 break;
2673 
2674             default:
2675                 // Just desensitize any other character by just printing what
2676                 // came after the '\'
2677                 s << *p;
2678                 break;
2679 
2680             }
2681 
2682         }
2683     }
2684     if (end)
2685         *end = p;
2686     return success;
2687 }
2688 
2689 bool
2690 Debugger::FormatPrompt
2691 (
2692     const char *format,
2693     const SymbolContext *sc,
2694     const ExecutionContext *exe_ctx,
2695     const Address *addr,
2696     Stream &s,
2697     ValueObject* valobj
2698 )
2699 {
2700     bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2701     std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2702     if (format_str.length())
2703         format = format_str.c_str();
2704     return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
2705 }
2706 
2707 void
2708 Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2709 {
2710     // For simplicity's sake, I am not going to deal with how to close down any
2711     // open logging streams, I just redirect everything from here on out to the
2712     // callback.
2713     m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2714 }
2715 
2716 bool
2717 Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2718 {
2719     Log::Callbacks log_callbacks;
2720 
2721     StreamSP log_stream_sp;
2722     if (m_log_callback_stream_sp)
2723     {
2724         log_stream_sp = m_log_callback_stream_sp;
2725         // For now when using the callback mode you always get thread & timestamp.
2726         log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2727     }
2728     else if (log_file == NULL || *log_file == '\0')
2729     {
2730         log_stream_sp = GetOutputFile();
2731     }
2732     else
2733     {
2734         LogStreamMap::iterator pos = m_log_streams.find(log_file);
2735         if (pos != m_log_streams.end())
2736             log_stream_sp = pos->second.lock();
2737         if (!log_stream_sp)
2738         {
2739             log_stream_sp.reset (new StreamFile (log_file));
2740             m_log_streams[log_file] = log_stream_sp;
2741         }
2742     }
2743     assert (log_stream_sp.get());
2744 
2745     if (log_options == 0)
2746         log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2747 
2748     if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
2749     {
2750         log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2751         return true;
2752     }
2753     else
2754     {
2755         LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2756         if (log_channel_sp)
2757         {
2758             if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2759             {
2760                 return true;
2761             }
2762             else
2763             {
2764                 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2765                 return false;
2766             }
2767         }
2768         else
2769         {
2770             error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2771             return false;
2772         }
2773     }
2774     return false;
2775 }
2776 
2777 SourceManager &
2778 Debugger::GetSourceManager ()
2779 {
2780     if (m_source_manager_ap.get() == NULL)
2781         m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2782     return *m_source_manager_ap;
2783 }
2784 
2785 
2786 
2787 // This function handles events that were broadcast by the process.
2788 void
2789 Debugger::HandleBreakpointEvent (const EventSP &event_sp)
2790 {
2791     using namespace lldb;
2792     const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
2793 
2794 //    if (event_type & eBreakpointEventTypeAdded
2795 //        || event_type & eBreakpointEventTypeRemoved
2796 //        || event_type & eBreakpointEventTypeEnabled
2797 //        || event_type & eBreakpointEventTypeDisabled
2798 //        || event_type & eBreakpointEventTypeCommandChanged
2799 //        || event_type & eBreakpointEventTypeConditionChanged
2800 //        || event_type & eBreakpointEventTypeIgnoreChanged
2801 //        || event_type & eBreakpointEventTypeLocationsResolved)
2802 //    {
2803 //        // Don't do anything about these events, since the breakpoint commands already echo these actions.
2804 //    }
2805 //
2806     if (event_type & eBreakpointEventTypeLocationsAdded)
2807     {
2808         uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
2809         if (num_new_locations > 0)
2810         {
2811             BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
2812             StreamFileSP output_sp (GetOutputFile());
2813             if (output_sp)
2814             {
2815                 output_sp->Printf("%d location%s added to breakpoint %d\n",
2816                                   num_new_locations,
2817                                   num_new_locations == 1 ? "" : "s",
2818                                   breakpoint->GetID());
2819                 RefreshTopIOHandler();
2820             }
2821         }
2822     }
2823 //    else if (event_type & eBreakpointEventTypeLocationsRemoved)
2824 //    {
2825 //        // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
2826 //    }
2827 //    else if (event_type & eBreakpointEventTypeLocationsResolved)
2828 //    {
2829 //        // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
2830 //    }
2831 }
2832 
2833 size_t
2834 Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
2835 {
2836     size_t total_bytes = 0;
2837     if (stream == NULL)
2838         stream = GetOutputFile().get();
2839 
2840     if (stream)
2841     {
2842         //  The process has stuff waiting for stdout; get it and write it out to the appropriate place.
2843         if (process == NULL)
2844         {
2845             TargetSP target_sp = GetTargetList().GetSelectedTarget();
2846             if (target_sp)
2847                 process = target_sp->GetProcessSP().get();
2848         }
2849         if (process)
2850         {
2851             Error error;
2852             size_t len;
2853             char stdio_buffer[1024];
2854             while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2855             {
2856                 stream->Write(stdio_buffer, len);
2857                 total_bytes += len;
2858             }
2859         }
2860         stream->Flush();
2861     }
2862     return total_bytes;
2863 }
2864 
2865 size_t
2866 Debugger::GetProcessSTDERR (Process *process, Stream *stream)
2867 {
2868     size_t total_bytes = 0;
2869     if (stream == NULL)
2870         stream = GetOutputFile().get();
2871 
2872     if (stream)
2873     {
2874         //  The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2875         if (process == NULL)
2876         {
2877             TargetSP target_sp = GetTargetList().GetSelectedTarget();
2878             if (target_sp)
2879                 process = target_sp->GetProcessSP().get();
2880         }
2881         if (process)
2882         {
2883             Error error;
2884             size_t len;
2885             char stdio_buffer[1024];
2886             while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2887             {
2888                 stream->Write(stdio_buffer, len);
2889                 total_bytes += len;
2890             }
2891         }
2892         stream->Flush();
2893     }
2894     return total_bytes;
2895 }
2896 
2897 // This function handles events that were broadcast by the process.
2898 void
2899 Debugger::HandleProcessEvent (const EventSP &event_sp)
2900 {
2901     using namespace lldb;
2902     const uint32_t event_type = event_sp->GetType();
2903     ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
2904 
2905     StreamString output_stream;
2906     StreamString error_stream;
2907     const bool gui_enabled = IsForwardingEvents();
2908 
2909     if (!gui_enabled)
2910     {
2911         bool pop_process_io_handler = false;
2912         assert (process_sp);
2913 
2914         if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
2915         {
2916             GetProcessSTDOUT (process_sp.get(), &output_stream);
2917         }
2918 
2919         if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
2920         {
2921             GetProcessSTDERR (process_sp.get(), &error_stream);
2922         }
2923 
2924         if (event_type & Process::eBroadcastBitStateChanged)
2925         {
2926 
2927             // Drain all stout and stderr so we don't see any output come after
2928             // we print our prompts
2929             // Something changed in the process;  get the event and report the process's current status and location to
2930             // the user.
2931             StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
2932             if (event_state == eStateInvalid)
2933                 return;
2934 
2935             switch (event_state)
2936             {
2937                 case eStateInvalid:
2938                 case eStateUnloaded:
2939                 case eStateConnected:
2940                 case eStateAttaching:
2941                 case eStateLaunching:
2942                 case eStateStepping:
2943                 case eStateDetached:
2944                     {
2945                         output_stream.Printf("Process %" PRIu64 " %s\n",
2946                                              process_sp->GetID(),
2947                                              StateAsCString (event_state));
2948 
2949                         if (event_state == eStateDetached)
2950                             pop_process_io_handler = true;
2951                     }
2952                     break;
2953 
2954                 case eStateRunning:
2955                     // Don't be chatty when we run...
2956                     break;
2957 
2958                 case eStateExited:
2959                     process_sp->GetStatus(output_stream);
2960                     pop_process_io_handler = true;
2961                     break;
2962 
2963                 case eStateStopped:
2964                 case eStateCrashed:
2965                 case eStateSuspended:
2966                     // Make sure the program hasn't been auto-restarted:
2967                     if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get()))
2968                     {
2969                         size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get());
2970                         if (num_reasons > 0)
2971                         {
2972                             // FIXME: Do we want to report this, or would that just be annoyingly chatty?
2973                             if (num_reasons == 1)
2974                             {
2975                                 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0);
2976                                 output_stream.Printf("Process %" PRIu64 " stopped and restarted: %s\n",
2977                                                      process_sp->GetID(),
2978                                                      reason ? reason : "<UNKNOWN REASON>");
2979                             }
2980                             else
2981                             {
2982                                 output_stream.Printf("Process %" PRIu64 " stopped and restarted, reasons:\n",
2983                                                      process_sp->GetID());
2984 
2985 
2986                                 for (size_t i = 0; i < num_reasons; i++)
2987                                 {
2988                                     const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i);
2989                                     output_stream.Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>");
2990                                 }
2991                             }
2992                         }
2993                     }
2994                     else
2995                     {
2996                         // Lock the thread list so it doesn't change on us, this is the scope for the locker:
2997                         {
2998                             ThreadList &thread_list = process_sp->GetThreadList();
2999                             Mutex::Locker locker (thread_list.GetMutex());
3000 
3001                             ThreadSP curr_thread (thread_list.GetSelectedThread());
3002                             ThreadSP thread;
3003                             StopReason curr_thread_stop_reason = eStopReasonInvalid;
3004                             if (curr_thread)
3005                                 curr_thread_stop_reason = curr_thread->GetStopReason();
3006                             if (!curr_thread ||
3007                                 !curr_thread->IsValid() ||
3008                                 curr_thread_stop_reason == eStopReasonInvalid ||
3009                                 curr_thread_stop_reason == eStopReasonNone)
3010                             {
3011                                 // Prefer a thread that has just completed its plan over another thread as current thread.
3012                                 ThreadSP plan_thread;
3013                                 ThreadSP other_thread;
3014                                 const size_t num_threads = thread_list.GetSize();
3015                                 size_t i;
3016                                 for (i = 0; i < num_threads; ++i)
3017                                 {
3018                                     thread = thread_list.GetThreadAtIndex(i);
3019                                     StopReason thread_stop_reason = thread->GetStopReason();
3020                                     switch (thread_stop_reason)
3021                                     {
3022                                         case eStopReasonInvalid:
3023                                         case eStopReasonNone:
3024                                             break;
3025 
3026                                         case eStopReasonTrace:
3027                                         case eStopReasonBreakpoint:
3028                                         case eStopReasonWatchpoint:
3029                                         case eStopReasonSignal:
3030                                         case eStopReasonException:
3031                                         case eStopReasonExec:
3032                                         case eStopReasonThreadExiting:
3033                                             if (!other_thread)
3034                                                 other_thread = thread;
3035                                             break;
3036                                         case eStopReasonPlanComplete:
3037                                             if (!plan_thread)
3038                                                 plan_thread = thread;
3039                                             break;
3040                                     }
3041                                 }
3042                                 if (plan_thread)
3043                                     thread_list.SetSelectedThreadByID (plan_thread->GetID());
3044                                 else if (other_thread)
3045                                     thread_list.SetSelectedThreadByID (other_thread->GetID());
3046                                 else
3047                                 {
3048                                     if (curr_thread && curr_thread->IsValid())
3049                                         thread = curr_thread;
3050                                     else
3051                                         thread = thread_list.GetThreadAtIndex(0);
3052 
3053                                     if (thread)
3054                                         thread_list.SetSelectedThreadByID (thread->GetID());
3055                                 }
3056                             }
3057                         }
3058                         // Drop the ThreadList mutex by here, since GetThreadStatus below might have to run code,
3059                         // e.g. for Data formatters, and if we hold the ThreadList mutex, then the process is going to
3060                         // have a hard time restarting the process.
3061 
3062                         if (GetTargetList().GetSelectedTarget().get() == &process_sp->GetTarget())
3063                         {
3064                             const bool only_threads_with_stop_reason = true;
3065                             const uint32_t start_frame = 0;
3066                             const uint32_t num_frames = 1;
3067                             const uint32_t num_frames_with_source = 1;
3068                             process_sp->GetStatus(output_stream);
3069                             process_sp->GetThreadStatus (output_stream,
3070                                                          only_threads_with_stop_reason,
3071                                                          start_frame,
3072                                                          num_frames,
3073                                                          num_frames_with_source);
3074                         }
3075                         else
3076                         {
3077                             uint32_t target_idx = GetTargetList().GetIndexOfTarget(process_sp->GetTarget().shared_from_this());
3078                             if (target_idx != UINT32_MAX)
3079                                 output_stream.Printf ("Target %d: (", target_idx);
3080                             else
3081                                 output_stream.Printf ("Target <unknown index>: (");
3082                             process_sp->GetTarget().Dump (&output_stream, eDescriptionLevelBrief);
3083                             output_stream.Printf (") stopped.\n");
3084                         }
3085 
3086                         // Pop the process IO handler
3087                         pop_process_io_handler = true;
3088                     }
3089                     break;
3090             }
3091         }
3092 
3093         if (output_stream.GetSize() || error_stream.GetSize())
3094         {
3095             StreamFileSP error_stream_sp (GetOutputFile());
3096             bool top_io_handler_hid = false;
3097 
3098             if (process_sp->ProcessIOHandlerIsActive() == false)
3099                 top_io_handler_hid = HideTopIOHandler();
3100 
3101             if (output_stream.GetSize())
3102             {
3103                 StreamFileSP output_stream_sp (GetOutputFile());
3104                 if (output_stream_sp)
3105                     output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
3106             }
3107 
3108             if (error_stream.GetSize())
3109             {
3110                 StreamFileSP error_stream_sp (GetErrorFile());
3111                 if (error_stream_sp)
3112                     error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
3113             }
3114 
3115             if (top_io_handler_hid)
3116                 RefreshTopIOHandler();
3117         }
3118 
3119         if (pop_process_io_handler)
3120             process_sp->PopProcessIOHandler();
3121     }
3122 }
3123 
3124 void
3125 Debugger::HandleThreadEvent (const EventSP &event_sp)
3126 {
3127     // At present the only thread event we handle is the Frame Changed event,
3128     // and all we do for that is just reprint the thread status for that thread.
3129     using namespace lldb;
3130     const uint32_t event_type = event_sp->GetType();
3131     if (event_type == Thread::eBroadcastBitStackChanged   ||
3132         event_type == Thread::eBroadcastBitThreadSelected )
3133     {
3134         ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
3135         if (thread_sp)
3136         {
3137             HideTopIOHandler();
3138             StreamFileSP stream_sp (GetOutputFile());
3139             thread_sp->GetStatus(*stream_sp, 0, 1, 1);
3140             RefreshTopIOHandler();
3141         }
3142     }
3143 }
3144 
3145 bool
3146 Debugger::IsForwardingEvents ()
3147 {
3148     return (bool)m_forward_listener_sp;
3149 }
3150 
3151 void
3152 Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
3153 {
3154     m_forward_listener_sp = listener_sp;
3155 }
3156 
3157 void
3158 Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
3159 {
3160     m_forward_listener_sp.reset();
3161 }
3162 
3163 
3164 void
3165 Debugger::DefaultEventHandler()
3166 {
3167     Listener& listener(GetListener());
3168     ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
3169     ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
3170     ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
3171     BroadcastEventSpec target_event_spec (broadcaster_class_target,
3172                                           Target::eBroadcastBitBreakpointChanged);
3173 
3174     BroadcastEventSpec process_event_spec (broadcaster_class_process,
3175                                            Process::eBroadcastBitStateChanged   |
3176                                            Process::eBroadcastBitSTDOUT         |
3177                                            Process::eBroadcastBitSTDERR);
3178 
3179     BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
3180                                           Thread::eBroadcastBitStackChanged     |
3181                                           Thread::eBroadcastBitThreadSelected   );
3182 
3183     listener.StartListeningForEventSpec (*this, target_event_spec);
3184     listener.StartListeningForEventSpec (*this, process_event_spec);
3185     listener.StartListeningForEventSpec (*this, thread_event_spec);
3186     listener.StartListeningForEvents (m_command_interpreter_ap.get(),
3187                                       CommandInterpreter::eBroadcastBitQuitCommandReceived      |
3188                                       CommandInterpreter::eBroadcastBitAsynchronousOutputData   |
3189                                       CommandInterpreter::eBroadcastBitAsynchronousErrorData    );
3190 
3191     bool done = false;
3192     while (!done)
3193     {
3194 //        Mutex::Locker locker;
3195 //        if (locker.TryLock(m_input_reader_stack.GetMutex()))
3196 //        {
3197 //            if (m_input_reader_stack.IsEmpty())
3198 //                break;
3199 //        }
3200 //
3201         EventSP event_sp;
3202         if (listener.WaitForEvent(NULL, event_sp))
3203         {
3204             if (event_sp)
3205             {
3206                 Broadcaster *broadcaster = event_sp->GetBroadcaster();
3207                 if (broadcaster)
3208                 {
3209                     uint32_t event_type = event_sp->GetType();
3210                     ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
3211                     if (broadcaster_class == broadcaster_class_process)
3212                     {
3213                         HandleProcessEvent (event_sp);
3214                     }
3215                     else if (broadcaster_class == broadcaster_class_target)
3216                     {
3217                         if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
3218                         {
3219                             HandleBreakpointEvent (event_sp);
3220                         }
3221                     }
3222                     else if (broadcaster_class == broadcaster_class_thread)
3223                     {
3224                         HandleThreadEvent (event_sp);
3225                     }
3226                     else if (broadcaster == m_command_interpreter_ap.get())
3227                     {
3228                         if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
3229                         {
3230                             done = true;
3231                         }
3232                         else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
3233                         {
3234                             const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3235                             if (data && data[0])
3236                             {
3237                                 StreamFileSP error_sp (GetErrorFile());
3238                                 if (error_sp)
3239                                 {
3240                                     HideTopIOHandler();
3241                                     error_sp->PutCString(data);
3242                                     error_sp->Flush();
3243                                     RefreshTopIOHandler();
3244                                 }
3245                             }
3246                         }
3247                         else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
3248                         {
3249                             const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3250                             if (data && data[0])
3251                             {
3252                                 StreamFileSP output_sp (GetOutputFile());
3253                                 if (output_sp)
3254                                 {
3255                                     HideTopIOHandler();
3256                                     output_sp->PutCString(data);
3257                                     output_sp->Flush();
3258                                     RefreshTopIOHandler();
3259                                 }
3260                             }
3261                         }
3262                     }
3263                 }
3264 
3265                 if (m_forward_listener_sp)
3266                     m_forward_listener_sp->AddEvent(event_sp);
3267             }
3268         }
3269     }
3270 }
3271 
3272 lldb::thread_result_t
3273 Debugger::EventHandlerThread (lldb::thread_arg_t arg)
3274 {
3275     ((Debugger *)arg)->DefaultEventHandler();
3276     return NULL;
3277 }
3278 
3279 bool
3280 Debugger::StartEventHandlerThread()
3281 {
3282     if (!IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3283         m_event_handler_thread = Host::ThreadCreate("lldb.debugger.event-handler", EventHandlerThread, this, NULL);
3284     return IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread);
3285 }
3286 
3287 void
3288 Debugger::StopEventHandlerThread()
3289 {
3290     if (IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3291     {
3292         GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
3293         Host::ThreadJoin(m_event_handler_thread, NULL, NULL);
3294         m_event_handler_thread = LLDB_INVALID_HOST_THREAD;
3295     }
3296 }
3297 
3298 
3299 lldb::thread_result_t
3300 Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3301 {
3302     Debugger *debugger = (Debugger *)arg;
3303     debugger->ExecuteIOHanders();
3304     debugger->StopEventHandlerThread();
3305     return NULL;
3306 }
3307 
3308 bool
3309 Debugger::StartIOHandlerThread()
3310 {
3311     if (!IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3312         m_io_handler_thread = Host::ThreadCreate("lldb.debugger.io-handler", IOHandlerThread, this, NULL);
3313     return IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread);
3314 }
3315 
3316 void
3317 Debugger::StopIOHandlerThread()
3318 {
3319     if (IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3320     {
3321         if (m_input_file_sp)
3322             m_input_file_sp->GetFile().Close();
3323         Host::ThreadJoin(m_io_handler_thread, NULL, NULL);
3324         m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
3325     }
3326 }
3327 
3328 
3329