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