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