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