blob: b6e4f811062b4da75ecb6aa9d5896f4829c84816 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Greg Clayton4a33d312011-06-23 17:59:56 +000010#include "lldb/Core/Debugger.h"
11
12#include <map>
13
Enrico Granata4becb372011-06-29 22:27:15 +000014#include "clang/AST/DeclCXX.h"
15#include "clang/AST/Type.h"
16
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/lldb-private.h"
18#include "lldb/Core/ConnectionFileDescriptor.h"
Enrico Granataa777dc22012-05-08 21:49:57 +000019#include "lldb/Core/DataVisualization.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000020#include "lldb/Core/FormatManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/InputReader.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000023#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000025#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000026#include "lldb/Core/StreamCallback.h"
Greg Clayton1b654882010-09-19 02:33:57 +000027#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000029#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000030#include "lldb/Core/ValueObjectVariable.h"
Greg Claytona3406612011-02-07 23:24:47 +000031#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000032#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000033#include "lldb/Interpreter/OptionValueSInt64.h"
34#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000035#include "lldb/Symbol/ClangASTContext.h"
36#include "lldb/Symbol/CompileUnit.h"
37#include "lldb/Symbol/Function.h"
38#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000039#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Target/TargetList.h"
41#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000042#include "lldb/Target/RegisterContext.h"
43#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000045#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
47using namespace lldb;
48using namespace lldb_private;
49
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
Greg Clayton1b654882010-09-19 02:33:57 +000051static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000052static lldb::user_id_t g_unique_id = 1;
53
Greg Clayton1b654882010-09-19 02:33:57 +000054#pragma mark Static Functions
55
56static Mutex &
57GetDebuggerListMutex ()
58{
59 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
60 return g_mutex;
61}
62
63typedef std::vector<DebuggerSP> DebuggerList;
64
65static DebuggerList &
66GetDebuggerList()
67{
68 // hide the static debugger list inside a singleton accessor to avoid
69 // global init contructors
70 static DebuggerList g_list;
71 return g_list;
72}
Greg Claytone372b982011-11-21 21:44:34 +000073
74OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000075g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000076{
Greg Clayton67cc0632012-08-22 17:17:09 +000077 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
78 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
79 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000080 { 0, NULL, NULL }
81};
82
Greg Clayton67cc0632012-08-22 17:17:09 +000083OptionEnumValueElement
84g_language_enumerators[] =
85{
86 { eScriptLanguageNone, "none", "Disable scripting languages."},
87 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
88 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000089 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000090};
Greg Claytone372b982011-11-21 21:44:34 +000091
Greg Clayton67cc0632012-08-22 17:17:09 +000092#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
93#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
94
95#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
96 "{, ${frame.pc}}"\
97 MODULE_WITH_FUNC\
98 FILE_AND_LINE\
99 "{, stop reason = ${thread.stop-reason}}"\
100 "{\\nReturn value: ${thread.return-value}}"\
101 "\\n"
102
103#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
104 MODULE_WITH_FUNC\
105 FILE_AND_LINE\
106 "\\n"
107
108
109
Greg Clayton754a9362012-08-23 00:22:02 +0000110static PropertyDefinition
111g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000112{
113{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
114{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
115{ "notify-void", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
Greg Clayton4c054102012-09-01 00:38:36 +0000116{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000117{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
118{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
119{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
120{ "stop-line-count-after", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come after the current source line when displaying a stopped context." },
121{ "stop-line-count-before", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come before the current source line when displaying a stopped context." },
122{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
123{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
124{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
125{ NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
126};
127
128enum
129{
130 ePropertyAutoConfirm = 0,
131 ePropertyFrameFormat,
132 ePropertyNotiftVoid,
133 ePropertyPrompt,
134 ePropertyScriptLanguage,
135 ePropertyStopDisassemblyCount,
136 ePropertyStopDisassemblyDisplay,
137 ePropertyStopLineCountAfter,
138 ePropertyStopLineCountBefore,
139 ePropertyTerminalWidth,
140 ePropertyThreadFormat,
141 ePropertyUseExternalEditor
142};
143
144//
145//const char *
146//Debugger::GetFrameFormat() const
147//{
148// return m_properties_sp->GetFrameFormat();
149//}
150//const char *
151//Debugger::GetThreadFormat() const
152//{
153// return m_properties_sp->GetThreadFormat();
154//}
155//
Greg Clayton4c054102012-09-01 00:38:36 +0000156
157
158Error
159Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
160 VarSetOperationType op,
161 const char *property_path,
162 const char *value)
163{
164 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
165 if (error.Success())
166 {
167 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
168 {
169 const char *new_prompt = GetPrompt();
170 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
171 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
172 }
173 }
174 return error;
175}
176
Greg Clayton67cc0632012-08-22 17:17:09 +0000177bool
178Debugger::GetAutoConfirm () const
179{
180 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000181 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000182}
183
184const char *
185Debugger::GetFrameFormat() const
186{
187 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000188 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000189}
190
191bool
192Debugger::GetNotifyVoid () const
193{
194 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000195 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000196}
197
198const char *
199Debugger::GetPrompt() const
200{
201 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000202 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000203}
204
205void
206Debugger::SetPrompt(const char *p)
207{
208 const uint32_t idx = ePropertyPrompt;
209 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
210 const char *new_prompt = GetPrompt();
211 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
212 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
213}
214
215const char *
216Debugger::GetThreadFormat() const
217{
218 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000219 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000220}
221
222lldb::ScriptLanguage
223Debugger::GetScriptLanguage() const
224{
225 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000226 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000227}
228
229bool
230Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
231{
232 const uint32_t idx = ePropertyScriptLanguage;
233 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
234}
235
236uint32_t
237Debugger::GetTerminalWidth () const
238{
239 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000240 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000241}
242
243bool
244Debugger::SetTerminalWidth (uint32_t term_width)
245{
246 const uint32_t idx = ePropertyTerminalWidth;
247 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
248}
249
250bool
251Debugger::GetUseExternalEditor () const
252{
253 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000254 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000255}
256
257bool
258Debugger::SetUseExternalEditor (bool b)
259{
260 const uint32_t idx = ePropertyUseExternalEditor;
261 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
262}
263
264uint32_t
265Debugger::GetStopSourceLineCount (bool before) const
266{
267 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000268 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000269}
270
271Debugger::StopDisassemblyType
272Debugger::GetStopDisassemblyDisplay () const
273{
274 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000275 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000276}
277
278uint32_t
279Debugger::GetDisassemblyLineCount () const
280{
281 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000282 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000283}
Greg Claytone372b982011-11-21 21:44:34 +0000284
Greg Clayton1b654882010-09-19 02:33:57 +0000285#pragma mark Debugger
286
Greg Clayton67cc0632012-08-22 17:17:09 +0000287//const DebuggerPropertiesSP &
288//Debugger::GetSettings() const
289//{
290// return m_properties_sp;
291//}
292//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000293
Caroline Tice2f88aad2011-01-14 00:29:16 +0000294int
295Debugger::TestDebuggerRefCount ()
296{
297 return g_shared_debugger_refcount;
298}
299
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300void
301Debugger::Initialize ()
302{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000303 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000304 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305}
306
307void
308Debugger::Terminate ()
309{
Greg Clayton66111032010-06-23 01:19:29 +0000310 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 {
Greg Clayton66111032010-06-23 01:19:29 +0000312 g_shared_debugger_refcount--;
313 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314 {
Greg Claytondbe54502010-11-19 03:46:01 +0000315 lldb_private::WillTerminate();
316 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000317
318 // Clear our master list of debugger objects
319 Mutex::Locker locker (GetDebuggerListMutex ());
320 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 }
323}
324
Caroline Tice20bd37f2011-03-10 22:14:10 +0000325void
326Debugger::SettingsInitialize ()
327{
Greg Clayton6920b522012-08-22 18:39:03 +0000328 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000329}
330
331void
332Debugger::SettingsTerminate ()
333{
Greg Clayton6920b522012-08-22 18:39:03 +0000334 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000335}
336
Greg Clayton66111032010-06-23 01:19:29 +0000337DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000338Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000339{
Jim Ingham228063c2012-02-21 02:23:08 +0000340 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000341 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000342 {
343 Mutex::Locker locker (GetDebuggerListMutex ());
344 GetDebuggerList().push_back(debugger_sp);
345 }
346 return debugger_sp;
347}
348
Caroline Ticee02657b2011-01-22 01:02:07 +0000349void
Greg Clayton4d122c42011-09-17 08:33:22 +0000350Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000351{
352 if (debugger_sp.get() == NULL)
353 return;
354
Jim Ingham8314c522011-09-15 21:36:42 +0000355 debugger_sp->Clear();
356
Greg Claytonc15f55e2012-03-30 20:53:46 +0000357 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000358 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000359 Mutex::Locker locker (GetDebuggerListMutex ());
360 DebuggerList &debugger_list = GetDebuggerList ();
361 DebuggerList::iterator pos, end = debugger_list.end();
362 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000363 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000364 if ((*pos).get() == debugger_sp.get())
365 {
366 debugger_list.erase (pos);
367 return;
368 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000369 }
370 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000371}
372
Greg Clayton4d122c42011-09-17 08:33:22 +0000373DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000374Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
375{
Greg Clayton4d122c42011-09-17 08:33:22 +0000376 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000377 if (g_shared_debugger_refcount > 0)
378 {
379 Mutex::Locker locker (GetDebuggerListMutex ());
380 DebuggerList &debugger_list = GetDebuggerList();
381 DebuggerList::iterator pos, end = debugger_list.end();
382
383 for (pos = debugger_list.begin(); pos != end; ++pos)
384 {
385 if ((*pos).get()->m_instance_name == instance_name)
386 {
387 debugger_sp = *pos;
388 break;
389 }
390 }
391 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000392 return debugger_sp;
393}
Greg Clayton66111032010-06-23 01:19:29 +0000394
395TargetSP
396Debugger::FindTargetWithProcessID (lldb::pid_t pid)
397{
Greg Clayton4d122c42011-09-17 08:33:22 +0000398 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000399 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000400 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000401 Mutex::Locker locker (GetDebuggerListMutex ());
402 DebuggerList &debugger_list = GetDebuggerList();
403 DebuggerList::iterator pos, end = debugger_list.end();
404 for (pos = debugger_list.begin(); pos != end; ++pos)
405 {
406 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
407 if (target_sp)
408 break;
409 }
Greg Clayton66111032010-06-23 01:19:29 +0000410 }
411 return target_sp;
412}
413
Greg Claytone4e45922011-11-16 05:37:56 +0000414TargetSP
415Debugger::FindTargetWithProcess (Process *process)
416{
417 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000418 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000419 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000420 Mutex::Locker locker (GetDebuggerListMutex ());
421 DebuggerList &debugger_list = GetDebuggerList();
422 DebuggerList::iterator pos, end = debugger_list.end();
423 for (pos = debugger_list.begin(); pos != end; ++pos)
424 {
425 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
426 if (target_sp)
427 break;
428 }
Greg Claytone4e45922011-11-16 05:37:56 +0000429 }
430 return target_sp;
431}
432
Jim Ingham228063c2012-02-21 02:23:08 +0000433Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000434 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000435 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000436 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 m_input_file (),
438 m_output_file (),
439 m_error_file (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000440 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000441 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442 m_listener ("lldb.Debugger"),
Jim Inghame37d6052011-09-13 00:29:56 +0000443 m_source_manager(*this),
444 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000445 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000446 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000447 m_input_reader_data (),
448 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449{
Greg Clayton67cc0632012-08-22 17:17:09 +0000450 char instance_cstr[256];
451 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
452 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000453 if (log_callback)
454 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000455 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000456 // Always add our default platform to the platform list
457 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
458 assert (default_platform_sp.get());
459 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000460
Greg Clayton754a9362012-08-23 00:22:02 +0000461 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000462 m_collection_sp->AppendProperty (ConstString("target"),
463 ConstString("Settings specify to debugging targets."),
464 true,
465 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000466 if (m_command_interpreter_ap.get())
467 {
468 m_collection_sp->AppendProperty (ConstString("interpreter"),
469 ConstString("Settings specify to the debugger's command interpreter."),
470 true,
471 m_command_interpreter_ap->GetValueProperties());
472 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000473 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
474 term_width->SetMinimumValue(10);
475 term_width->SetMaximumValue(1024);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476}
477
478Debugger::~Debugger ()
479{
Jim Ingham8314c522011-09-15 21:36:42 +0000480 Clear();
481}
482
483void
484Debugger::Clear()
485{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000486 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000487 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000488 int num_targets = m_target_list.GetNumTargets();
489 for (int i = 0; i < num_targets; i++)
490 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000491 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
492 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000493 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000494 ProcessSP process_sp (target_sp->GetProcessSP());
495 if (process_sp)
496 {
497 if (process_sp->GetShouldDetach())
498 process_sp->Detach();
499 }
500 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000501 }
Greg Clayton66111032010-06-23 01:19:29 +0000502 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000503 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000504
505 // Close the input file _before_ we close the input read communications class
506 // as it does NOT own the input file, our m_input_file does.
507 GetInputFile().Close ();
508 // Now that we have closed m_input_file, we can now tell our input communication
509 // class to close down. Its read thread should quickly exit after we close
510 // the input file handle above.
511 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000512}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513
514bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000515Debugger::GetCloseInputOnEOF () const
516{
517 return m_input_comm.GetCloseOnEOF();
518}
519
520void
521Debugger::SetCloseInputOnEOF (bool b)
522{
523 m_input_comm.SetCloseOnEOF(b);
524}
525
526bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527Debugger::GetAsyncExecution ()
528{
Greg Clayton66111032010-06-23 01:19:29 +0000529 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530}
531
532void
533Debugger::SetAsyncExecution (bool async_execution)
534{
Greg Clayton66111032010-06-23 01:19:29 +0000535 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536}
537
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538
539void
540Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
541{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000542 File &in_file = GetInputFile();
543 in_file.SetStream (fh, tranfer_ownership);
544 if (in_file.IsValid() == false)
545 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546
547 // Disconnect from any old connection if we had one
548 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000549 // Pass false as the second argument to ConnectionFileDescriptor below because
550 // our "in_file" above will already take ownership if requested and we don't
551 // want to objects trying to own and close a file descriptor.
552 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
554
555 Error error;
556 if (m_input_comm.StartReadThread (&error) == false)
557 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000558 File &err_file = GetErrorFile();
559
560 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
561 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563}
564
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565void
566Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
567{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000568 File &out_file = GetOutputFile();
569 out_file.SetStream (fh, tranfer_ownership);
570 if (out_file.IsValid() == false)
571 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000572
573 GetCommandInterpreter().GetScriptInterpreter()->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574}
575
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576void
577Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
578{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000579 File &err_file = GetErrorFile();
580 err_file.SetStream (fh, tranfer_ownership);
581 if (err_file.IsValid() == false)
582 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583}
584
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000586Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587{
588 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000589 TargetSP target_sp(GetSelectedTarget());
590 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591
592 if (target_sp)
593 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000594 ProcessSP process_sp (target_sp->GetProcessSP());
595 exe_ctx.SetProcessSP (process_sp);
596 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000598 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
599 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000601 exe_ctx.SetThreadSP (thread_sp);
602 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
603 if (exe_ctx.GetFramePtr() == NULL)
604 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 }
606 }
607 }
608 return exe_ctx;
609
610}
611
Caroline Ticeb44880c2011-02-10 01:15:13 +0000612InputReaderSP
613Debugger::GetCurrentInputReader ()
614{
615 InputReaderSP reader_sp;
616
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000617 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000618 {
619 // Clear any finished readers from the stack
620 while (CheckIfTopInputReaderIsDone()) ;
621
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000622 if (!m_input_reader_stack.IsEmpty())
623 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000624 }
625
626 return reader_sp;
627}
628
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629void
630Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
631{
Caroline Ticeefed6132010-11-19 20:47:54 +0000632 if (bytes_len > 0)
633 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
634 else
635 ((Debugger *)baton)->DispatchInputEndOfFile ();
636}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637
638
639void
640Debugger::DispatchInput (const char *bytes, size_t bytes_len)
641{
Caroline Ticeefed6132010-11-19 20:47:54 +0000642 if (bytes == NULL || bytes_len == 0)
643 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644
645 WriteToDefaultReader (bytes, bytes_len);
646}
647
648void
Caroline Ticeefed6132010-11-19 20:47:54 +0000649Debugger::DispatchInputInterrupt ()
650{
651 m_input_reader_data.clear();
652
Caroline Ticeb44880c2011-02-10 01:15:13 +0000653 InputReaderSP reader_sp (GetCurrentInputReader ());
654 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000655 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000656 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000657
Caroline Ticeb44880c2011-02-10 01:15:13 +0000658 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000659 while (CheckIfTopInputReaderIsDone ()) ;
660 }
661}
662
663void
664Debugger::DispatchInputEndOfFile ()
665{
666 m_input_reader_data.clear();
667
Caroline Ticeb44880c2011-02-10 01:15:13 +0000668 InputReaderSP reader_sp (GetCurrentInputReader ());
669 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000670 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000671 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000672
Caroline Ticeb44880c2011-02-10 01:15:13 +0000673 // If notifying the reader of the end-of-file finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000674 while (CheckIfTopInputReaderIsDone ()) ;
675 }
676}
677
678void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000679Debugger::CleanUpInputReaders ()
680{
681 m_input_reader_data.clear();
682
Caroline Ticeb44880c2011-02-10 01:15:13 +0000683 // The bottom input reader should be the main debugger input reader. We do not want to close that one here.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000684 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000685 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000686 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000687 if (reader_sp)
688 {
689 reader_sp->Notify (eInputReaderEndOfFile);
690 reader_sp->SetIsDone (true);
691 }
692 }
693}
694
695void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000696Debugger::NotifyTopInputReader (InputReaderAction notification)
697{
698 InputReaderSP reader_sp (GetCurrentInputReader());
699 if (reader_sp)
700 {
701 reader_sp->Notify (notification);
702
703 // Flush out any input readers that are done.
704 while (CheckIfTopInputReaderIsDone ())
705 /* Do nothing. */;
706 }
707}
708
Caroline Tice9088b062011-05-09 23:06:58 +0000709bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000710Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000711{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000712 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000713
Caroline Ticed61c10b2011-06-16 16:27:19 +0000714 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000715}
716
717
Caroline Tice969ed3d2011-05-02 20:41:46 +0000718void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
720{
721 if (bytes && bytes_len)
722 m_input_reader_data.append (bytes, bytes_len);
723
724 if (m_input_reader_data.empty())
725 return;
726
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000727 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000730 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 if (!reader_sp)
732 break;
733
Greg Clayton471b31c2010-07-20 22:52:08 +0000734 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 m_input_reader_data.size());
736 if (bytes_handled)
737 {
738 m_input_reader_data.erase (0, bytes_handled);
739 }
740 else
741 {
742 // No bytes were handled, we might not have reached our
743 // granularity, just return and wait for more data
744 break;
745 }
746 }
747
Caroline Ticeb44880c2011-02-10 01:15:13 +0000748 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749 while (CheckIfTopInputReaderIsDone ())
750 /* Do nothing. */;
751
752}
753
754void
755Debugger::PushInputReader (const InputReaderSP& reader_sp)
756{
757 if (!reader_sp)
758 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000759
760 // Deactivate the old top reader
761 InputReaderSP top_reader_sp (GetCurrentInputReader ());
762
763 if (top_reader_sp)
764 top_reader_sp->Notify (eInputReaderDeactivate);
765
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000766 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767 reader_sp->Notify (eInputReaderActivate);
768 ActivateInputReader (reader_sp);
769}
770
771bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000772Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773{
774 bool result = false;
775
776 // The reader on the stop of the stack is done, so let the next
777 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000778 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000780 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000781 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782
783 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
784 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000785 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 reader_sp->Notify (eInputReaderDeactivate);
787 reader_sp->Notify (eInputReaderDone);
788 result = true;
789
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000790 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000792 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793 if (reader_sp)
794 {
795 ActivateInputReader (reader_sp);
796 reader_sp->Notify (eInputReaderReactivate);
797 }
798 }
799 }
800 }
801 return result;
802}
803
804bool
805Debugger::CheckIfTopInputReaderIsDone ()
806{
807 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000808 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000810 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000811 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812
813 if (reader_sp && reader_sp->IsDone())
814 {
815 result = true;
816 PopInputReader (reader_sp);
817 }
818 }
819 return result;
820}
821
822void
823Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
824{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000825 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000827 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000829 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000830
831 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832
Greg Claytona3406612011-02-07 23:24:47 +0000833 switch (reader_sp->GetGranularity())
834 {
835 case eInputReaderGranularityByte:
836 case eInputReaderGranularityWord:
837 tty.SetCanonical (false);
838 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839
Greg Claytona3406612011-02-07 23:24:47 +0000840 case eInputReaderGranularityLine:
841 case eInputReaderGranularityAll:
842 tty.SetCanonical (true);
843 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844
Greg Claytona3406612011-02-07 23:24:47 +0000845 default:
846 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 }
848 }
849}
Greg Clayton66111032010-06-23 01:19:29 +0000850
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000851StreamSP
852Debugger::GetAsyncOutputStream ()
853{
854 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
855 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
856}
857
858StreamSP
859Debugger::GetAsyncErrorStream ()
860{
861 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
862 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
863}
864
Enrico Granata061858c2012-02-15 02:34:21 +0000865uint32_t
866Debugger::GetNumDebuggers()
867{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000868 if (g_shared_debugger_refcount > 0)
869 {
870 Mutex::Locker locker (GetDebuggerListMutex ());
871 return GetDebuggerList().size();
872 }
873 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +0000874}
875
876lldb::DebuggerSP
877Debugger::GetDebuggerAtIndex (uint32_t index)
878{
879 DebuggerSP debugger_sp;
880
Greg Claytonc15f55e2012-03-30 20:53:46 +0000881 if (g_shared_debugger_refcount > 0)
882 {
883 Mutex::Locker locker (GetDebuggerListMutex ());
884 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +0000885
Greg Claytonc15f55e2012-03-30 20:53:46 +0000886 if (index < debugger_list.size())
887 debugger_sp = debugger_list[index];
888 }
889
Enrico Granata061858c2012-02-15 02:34:21 +0000890 return debugger_sp;
891}
892
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000893DebuggerSP
894Debugger::FindDebuggerWithID (lldb::user_id_t id)
895{
Greg Clayton4d122c42011-09-17 08:33:22 +0000896 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000897
Greg Claytonc15f55e2012-03-30 20:53:46 +0000898 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000899 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000900 Mutex::Locker locker (GetDebuggerListMutex ());
901 DebuggerList &debugger_list = GetDebuggerList();
902 DebuggerList::iterator pos, end = debugger_list.end();
903 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000904 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000905 if ((*pos).get()->GetID() == id)
906 {
907 debugger_sp = *pos;
908 break;
909 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000910 }
911 }
912 return debugger_sp;
913}
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000914
Greg Clayton1b654882010-09-19 02:33:57 +0000915static void
916TestPromptFormats (StackFrame *frame)
917{
918 if (frame == NULL)
919 return;
920
921 StreamString s;
922 const char *prompt_format =
923 "{addr = '${addr}'\n}"
924 "{process.id = '${process.id}'\n}"
925 "{process.name = '${process.name}'\n}"
926 "{process.file.basename = '${process.file.basename}'\n}"
927 "{process.file.fullpath = '${process.file.fullpath}'\n}"
928 "{thread.id = '${thread.id}'\n}"
929 "{thread.index = '${thread.index}'\n}"
930 "{thread.name = '${thread.name}'\n}"
931 "{thread.queue = '${thread.queue}'\n}"
932 "{thread.stop-reason = '${thread.stop-reason}'\n}"
933 "{target.arch = '${target.arch}'\n}"
934 "{module.file.basename = '${module.file.basename}'\n}"
935 "{module.file.fullpath = '${module.file.fullpath}'\n}"
936 "{file.basename = '${file.basename}'\n}"
937 "{file.fullpath = '${file.fullpath}'\n}"
938 "{frame.index = '${frame.index}'\n}"
939 "{frame.pc = '${frame.pc}'\n}"
940 "{frame.sp = '${frame.sp}'\n}"
941 "{frame.fp = '${frame.fp}'\n}"
942 "{frame.flags = '${frame.flags}'\n}"
943 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
944 "{frame.reg.rip = '${frame.reg.rip}'\n}"
945 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
946 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
947 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
948 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
949 "{frame.reg.carp = '${frame.reg.carp}'\n}"
950 "{function.id = '${function.id}'\n}"
951 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +0000952 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +0000953 "{function.addr-offset = '${function.addr-offset}'\n}"
954 "{function.line-offset = '${function.line-offset}'\n}"
955 "{function.pc-offset = '${function.pc-offset}'\n}"
956 "{line.file.basename = '${line.file.basename}'\n}"
957 "{line.file.fullpath = '${line.file.fullpath}'\n}"
958 "{line.number = '${line.number}'\n}"
959 "{line.start-addr = '${line.start-addr}'\n}"
960 "{line.end-addr = '${line.end-addr}'\n}"
961;
962
963 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
964 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +0000965 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +0000966 const char *end = NULL;
967 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
968 {
969 printf("%s\n", s.GetData());
970 }
971 else
972 {
973 printf ("error: at '%s'\n", end);
974 printf ("what we got: %s\n", s.GetData());
975 }
976}
977
Enrico Granata9fc19442011-07-06 02:13:41 +0000978static bool
Enrico Granatadc940732011-08-23 00:32:52 +0000979ScanFormatDescriptor (const char* var_name_begin,
980 const char* var_name_end,
981 const char** var_name_final,
982 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +0000983 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +0000984 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +0000985{
Enrico Granatae992a082011-07-22 17:03:19 +0000986 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000987 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +0000988 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +0000989 {
990 if (log)
991 log->Printf("no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +0000992 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +0000993 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000994 else
995 {
996 *var_name_final = *percent_position;
997 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
998 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +0000999 if (log)
1000 log->Printf("parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001001 if ( !FormatManager::GetFormatFromCString(format_name,
1002 true,
1003 *custom_format) )
1004 {
Enrico Granatae992a082011-07-22 17:03:19 +00001005 if (log)
1006 log->Printf("%s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001007 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +00001008 if (*format_name == '@')
Enrico Granata86cc9822012-03-19 22:58:49 +00001009 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
Enrico Granata9fc19442011-07-06 02:13:41 +00001010 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +00001011 else if (*format_name == 'V')
Enrico Granata86cc9822012-03-19 22:58:49 +00001012 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatad55546b2011-07-22 00:16:08 +00001013 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +00001014 else if (*format_name == 'L')
Enrico Granata86cc9822012-03-19 22:58:49 +00001015 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +00001016 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +00001017 else if (*format_name == 'S')
Enrico Granata86cc9822012-03-19 22:58:49 +00001018 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Enrico Granata5dfd49c2011-08-04 02:34:29 +00001019 else if (*format_name == '#')
Enrico Granata86cc9822012-03-19 22:58:49 +00001020 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001021 else if (*format_name == 'T')
Enrico Granata86cc9822012-03-19 22:58:49 +00001022 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
Enrico Granatae992a082011-07-22 17:03:19 +00001023 else if (log)
1024 log->Printf("%s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001025 }
1026 // a good custom format tells us to print the value using it
1027 else
Enrico Granatae992a082011-07-22 17:03:19 +00001028 {
1029 if (log)
1030 log->Printf("will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001031 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001032 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001033 delete format_name;
1034 }
Enrico Granatae992a082011-07-22 17:03:19 +00001035 if (log)
1036 log->Printf("final format description outcome: custom_format = %d, val_obj_display = %d",
1037 *custom_format,
1038 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001039 return true;
1040}
1041
1042static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001043ScanBracketedRange (const char* var_name_begin,
1044 const char* var_name_end,
1045 const char* var_name_final,
1046 const char** open_bracket_position,
1047 const char** separator_position,
1048 const char** close_bracket_position,
1049 const char** var_name_final_if_array_range,
1050 int64_t* index_lower,
1051 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001052{
Enrico Granatae992a082011-07-22 17:03:19 +00001053 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001054 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001055 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001056 {
1057 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1058 *close_bracket_position = ::strchr(*open_bracket_position,']');
1059 // as usual, we assume that [] will come before %
1060 //printf("trying to expand a []\n");
1061 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001062 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001063 {
Enrico Granatae992a082011-07-22 17:03:19 +00001064 if (log)
1065 log->Printf("[] detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001066 *index_lower = 0;
1067 }
1068 else if (*separator_position == NULL || *separator_position > var_name_end)
1069 {
1070 char *end = NULL;
1071 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1072 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001073 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001074 log->Printf("[%lld] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001075 }
Greg Clayton34132752011-07-06 04:07:21 +00001076 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001077 {
1078 char *end = NULL;
1079 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1080 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001081 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001082 log->Printf("[%lld-%lld] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001083 }
1084 else
Enrico Granatae992a082011-07-22 17:03:19 +00001085 {
1086 if (log)
1087 log->Printf("expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001088 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001089 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001090 if (*index_lower > *index_higher && *index_higher > 0)
1091 {
Enrico Granatae992a082011-07-22 17:03:19 +00001092 if (log)
1093 log->Printf("swapping indices");
Enrico Granata9fc19442011-07-06 02:13:41 +00001094 int temp = *index_lower;
1095 *index_lower = *index_higher;
1096 *index_higher = temp;
1097 }
1098 }
Enrico Granatae992a082011-07-22 17:03:19 +00001099 else if (log)
1100 log->Printf("no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001101 return true;
1102}
1103
Enrico Granata9fc19442011-07-06 02:13:41 +00001104static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001105ExpandIndexedExpression (ValueObject* valobj,
1106 uint32_t index,
1107 StackFrame* frame,
1108 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001109{
Enrico Granatae992a082011-07-22 17:03:19 +00001110 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001111 const char* ptr_deref_format = "[%d]";
1112 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
1113 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001114 if (log)
1115 log->Printf("name to deref: %s",ptr_deref_buffer.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001116 const char* first_unparsed;
1117 ValueObject::GetValueForExpressionPathOptions options;
1118 ValueObject::ExpressionPathEndResultType final_value_type;
1119 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001120 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatac482a192011-08-17 22:13:59 +00001121 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001122 &first_unparsed,
1123 &reason_to_stop,
1124 &final_value_type,
1125 options,
1126 &what_next);
1127 if (!item)
1128 {
Enrico Granatae992a082011-07-22 17:03:19 +00001129 if (log)
1130 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1131 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001132 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001133 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001134 else
1135 {
Enrico Granatae992a082011-07-22 17:03:19 +00001136 if (log)
1137 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1138 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001139 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001140 }
1141 return item;
1142}
1143
Greg Clayton1b654882010-09-19 02:33:57 +00001144bool
1145Debugger::FormatPrompt
1146(
1147 const char *format,
1148 const SymbolContext *sc,
1149 const ExecutionContext *exe_ctx,
1150 const Address *addr,
1151 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001152 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001153 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001154)
1155{
Enrico Granatac482a192011-08-17 22:13:59 +00001156 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001157 bool success = true;
1158 const char *p;
Enrico Granatae992a082011-07-22 17:03:19 +00001159 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001160 for (p = format; *p != '\0'; ++p)
1161 {
Enrico Granatac482a192011-08-17 22:13:59 +00001162 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001163 {
Enrico Granatac482a192011-08-17 22:13:59 +00001164 valobj = realvalobj;
1165 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001166 }
Greg Clayton1b654882010-09-19 02:33:57 +00001167 size_t non_special_chars = ::strcspn (p, "${}\\");
1168 if (non_special_chars > 0)
1169 {
1170 if (success)
1171 s.Write (p, non_special_chars);
1172 p += non_special_chars;
1173 }
1174
1175 if (*p == '\0')
1176 {
1177 break;
1178 }
1179 else if (*p == '{')
1180 {
1181 // Start a new scope that must have everything it needs if it is to
1182 // to make it into the final output stream "s". If you want to make
1183 // a format that only prints out the function or symbol name if there
1184 // is one in the symbol context you can use:
1185 // "{function =${function.name}}"
1186 // The first '{' starts a new scope that end with the matching '}' at
1187 // the end of the string. The contents "function =${function.name}"
1188 // will then be evaluated and only be output if there is a function
1189 // or symbol with a valid name.
1190 StreamString sub_strm;
1191
1192 ++p; // Skip the '{'
1193
Enrico Granatac482a192011-08-17 22:13:59 +00001194 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001195 {
1196 // The stream had all it needed
1197 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1198 }
1199 if (*p != '}')
1200 {
1201 success = false;
1202 break;
1203 }
1204 }
1205 else if (*p == '}')
1206 {
1207 // End of a enclosing scope
1208 break;
1209 }
1210 else if (*p == '$')
1211 {
1212 // We have a prompt variable to print
1213 ++p;
1214 if (*p == '{')
1215 {
1216 ++p;
1217 const char *var_name_begin = p;
1218 const char *var_name_end = ::strchr (p, '}');
1219
1220 if (var_name_end && var_name_begin < var_name_end)
1221 {
1222 // if we have already failed to parse, skip this variable
1223 if (success)
1224 {
1225 const char *cstr = NULL;
1226 Address format_addr;
1227 bool calculate_format_addr_function_offset = false;
1228 // Set reg_kind and reg_num to invalid values
1229 RegisterKind reg_kind = kNumRegisterKinds;
1230 uint32_t reg_num = LLDB_INVALID_REGNUM;
1231 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001232 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001233 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001234 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001235 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1236 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001237
Greg Clayton1b654882010-09-19 02:33:57 +00001238 // Each variable must set success to true below...
1239 bool var_success = false;
1240 switch (var_name_begin[0])
1241 {
Enrico Granata4becb372011-06-29 22:27:15 +00001242 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001243 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001244 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001245 {
Enrico Granatac482a192011-08-17 22:13:59 +00001246 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001247 break;
1248
Enrico Granatac3e320a2011-08-02 17:27:39 +00001249 if (log)
1250 log->Printf("initial string: %s",var_name_begin);
1251
Enrico Granata6f3533f2011-07-29 19:53:35 +00001252 // check for *var and *svar
1253 if (*var_name_begin == '*')
1254 {
1255 do_deref_pointer = true;
1256 var_name_begin++;
1257 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001258
1259 if (log)
1260 log->Printf("initial string: %s",var_name_begin);
1261
Enrico Granata6f3533f2011-07-29 19:53:35 +00001262 if (*var_name_begin == 's')
1263 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001264 if (!valobj->IsSynthetic())
1265 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001266 if (!valobj)
1267 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001268 var_name_begin++;
1269 }
1270
Enrico Granatac3e320a2011-08-02 17:27:39 +00001271 if (log)
1272 log->Printf("initial string: %s",var_name_begin);
1273
Enrico Granata6f3533f2011-07-29 19:53:35 +00001274 // should be a 'v' by now
1275 if (*var_name_begin != 'v')
1276 break;
1277
Enrico Granatac3e320a2011-08-02 17:27:39 +00001278 if (log)
1279 log->Printf("initial string: %s",var_name_begin);
1280
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001281 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001282 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001283 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001284 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001285 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001286 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001287 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001288 const char* var_name_final = NULL;
1289 const char* var_name_final_if_array_range = NULL;
1290 const char* close_bracket_position = NULL;
1291 int64_t index_lower = -1;
1292 int64_t index_higher = -1;
1293 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001294 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001295 bool was_plain_var = false;
1296 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001297 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001298
Enrico Granatac482a192011-08-17 22:13:59 +00001299 if (!valobj) break;
1300 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001301 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001302 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001303 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001304 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001305 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001306 }
1307 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1308 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001309 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001310 // this is a variable with some custom format applied to it
1311 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001312 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001313 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001314 ScanFormatDescriptor (var_name_begin,
1315 var_name_end,
1316 &var_name_final,
1317 &percent_position,
1318 &custom_format,
1319 &val_obj_display);
1320 }
1321 // this is ${var.something} or multiple .something nested
1322 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1323 {
Enrico Granataa777dc22012-05-08 21:49:57 +00001324 if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1325 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001326 const char* percent_position;
1327 ScanFormatDescriptor (var_name_begin,
1328 var_name_end,
1329 &var_name_final,
1330 &percent_position,
1331 &custom_format,
1332 &val_obj_display);
1333
1334 const char* open_bracket_position;
1335 const char* separator_position;
1336 ScanBracketedRange (var_name_begin,
1337 var_name_end,
1338 var_name_final,
1339 &open_bracket_position,
1340 &separator_position,
1341 &close_bracket_position,
1342 &var_name_final_if_array_range,
1343 &index_lower,
1344 &index_higher);
1345
1346 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001347
1348 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1349 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1350 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1351
Enrico Granatae992a082011-07-22 17:03:19 +00001352 if (log)
1353 log->Printf("symbol to expand: %s",expr_path.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001354
Enrico Granatac482a192011-08-17 22:13:59 +00001355 target = valobj->GetValueForExpressionPath(expr_path.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001356 &first_unparsed,
1357 &reason_to_stop,
1358 &final_value_type,
1359 options,
1360 &what_next).get();
1361
1362 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001363 {
Enrico Granatae992a082011-07-22 17:03:19 +00001364 if (log)
1365 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1366 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001367 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001368 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001369 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001370 else
1371 {
Enrico Granatae992a082011-07-22 17:03:19 +00001372 if (log)
1373 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1374 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001375 first_unparsed, reason_to_stop, final_value_type);
1376 }
Enrico Granata4becb372011-06-29 22:27:15 +00001377 }
Greg Clayton34132752011-07-06 04:07:21 +00001378 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001379 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001380
Enrico Granata86cc9822012-03-19 22:58:49 +00001381 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1382 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001383
Enrico Granata86cc9822012-03-19 22:58:49 +00001384 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001385
Enrico Granataa7187d02011-07-06 19:27:11 +00001386 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001387 {
Greg Clayton34132752011-07-06 04:07:21 +00001388 // I have not deref-ed yet, let's do it
1389 // this happens when we are not going through GetValueForVariableExpressionPath
1390 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001391 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001392 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001393 if (error.Fail())
1394 {
1395 if (log)
1396 log->Printf("ERROR: %s\n", error.AsCString("unknown")); \
1397 break;
1398 }
Greg Clayton34132752011-07-06 04:07:21 +00001399 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001400 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001401
Enrico Granataa777dc22012-05-08 21:49:57 +00001402 // <rdar://problem/11338654>
1403 // we do not want to use the summary for a bitfield of type T:n
1404 // if we were originally dealing with just a T - that would get
1405 // us into an endless recursion
1406 if (target->IsBitfield() && was_var_indexed)
1407 {
1408 // TODO: check for a (T:n)-specific summary - we should still obey that
1409 StreamString bitfield_name;
1410 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1411 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1412 if (!DataVisualization::GetSummaryForType(type_sp))
1413 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1414 }
1415
Enrico Granata85933ed2011-08-18 16:38:26 +00001416 // TODO use flags for these
Enrico Granataf4efecd2011-07-12 22:56:10 +00001417 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1418 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001419 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001420
Enrico Granata86cc9822012-03-19 22:58:49 +00001421 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions
Enrico Granataf4efecd2011-07-12 22:56:10 +00001422 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001423 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001424 if (log)
1425 log->Printf("I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001426
Enrico Granata86cc9822012-03-19 22:58:49 +00001427 if (target->HasSpecialPrintableRepresentation(val_obj_display,
1428 custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001429 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001430 // try to use the special cases
1431 var_success = target->DumpPrintableRepresentation(str_temp,
1432 val_obj_display,
1433 custom_format);
1434 if (log)
1435 log->Printf("special cases did%s match", var_success ? "" : "n't");
1436
1437 // should not happen
1438 if (!var_success)
1439 s << "<invalid usage of pointer value as object>";
1440 else
1441 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001442 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001443 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001444 }
1445 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001446 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001447 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001448 {
1449 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1450 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001451 else if (is_pointer) // if pointer, value is the address stored
1452 {
Greg Clayton23f59502012-07-17 03:23:13 +00001453 target->DumpPrintableRepresentation (s,
1454 val_obj_display,
1455 custom_format,
1456 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001457 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001458 else
1459 {
1460 s << "<invalid usage of pointer value as object>";
1461 }
1462 var_success = true;
1463 break;
1464 }
1465 }
1466
1467 // if directly trying to print ${var}, and this is an aggregate, display a nice
1468 // type @ location message
1469 if (is_aggregate && was_plain_var)
1470 {
1471 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1472 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001473 break;
1474 }
1475
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001476 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it
Enrico Granata86cc9822012-03-19 22:58:49 +00001477 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001478 {
1479 s << "<invalid use of aggregate type>";
1480 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001481 break;
1482 }
Greg Clayton34132752011-07-06 04:07:21 +00001483
1484 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001485 {
1486 if (log)
1487 log->Printf("dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001488 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001489 }
Greg Clayton34132752011-07-06 04:07:21 +00001490 else
Enrico Granatae992a082011-07-22 17:03:19 +00001491 {
1492 if (log)
1493 log->Printf("checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001494 if (!is_array && !is_pointer)
1495 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001496 if (log)
1497 log->Printf("handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001498 const char* special_directions = NULL;
1499 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001500 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1501 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001502 ConstString additional_data;
1503 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1504 special_directions_writer.Printf("${%svar%s}",
1505 do_deref_pointer ? "*" : "",
1506 additional_data.GetCString());
1507 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001508 }
1509
1510 // let us display items index_lower thru index_higher of this array
1511 s.PutChar('[');
1512 var_success = true;
1513
1514 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001515 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001516
Greg Claytoncc4d0142012-02-17 07:49:44 +00001517 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001518
Greg Clayton34132752011-07-06 04:07:21 +00001519 for (;index_lower<=index_higher;index_lower++)
1520 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001521 ValueObject* item = ExpandIndexedExpression (target,
1522 index_lower,
1523 exe_ctx->GetFramePtr(),
1524 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001525
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001526 if (!item)
1527 {
Enrico Granatae992a082011-07-22 17:03:19 +00001528 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001529 log->Printf("ERROR in getting child item at index %lld", index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001530 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001531 else
1532 {
Enrico Granatae992a082011-07-22 17:03:19 +00001533 if (log)
1534 log->Printf("special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001535 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001536
Greg Clayton34132752011-07-06 04:07:21 +00001537 if (!special_directions)
1538 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1539 else
1540 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1541
Enrico Granata22c55d12011-08-12 02:00:06 +00001542 if (--max_num_children == 0)
1543 {
1544 s.PutCString(", ...");
1545 break;
1546 }
1547
Greg Clayton34132752011-07-06 04:07:21 +00001548 if (index_lower < index_higher)
1549 s.PutChar(',');
1550 }
1551 s.PutChar(']');
1552 }
Enrico Granata4becb372011-06-29 22:27:15 +00001553 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001554 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001555 case 'a':
1556 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1557 {
1558 if (addr && addr->IsValid())
1559 {
1560 var_success = true;
1561 format_addr = *addr;
1562 }
1563 }
Greg Clayton5a314712011-10-14 07:41:33 +00001564 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1565 {
1566 var_success = true;
1567 var_name_begin += strlen("ansi."); // Skip the "ansi."
1568 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1569 {
1570 var_name_begin += strlen("fg."); // Skip the "fg."
1571 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1572 {
1573 s.Printf ("%s%s%s",
1574 lldb_utility::ansi::k_escape_start,
1575 lldb_utility::ansi::k_fg_black,
1576 lldb_utility::ansi::k_escape_end);
1577 }
1578 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1579 {
1580 s.Printf ("%s%s%s",
1581 lldb_utility::ansi::k_escape_start,
1582 lldb_utility::ansi::k_fg_red,
1583 lldb_utility::ansi::k_escape_end);
1584 }
1585 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1586 {
1587 s.Printf ("%s%s%s",
1588 lldb_utility::ansi::k_escape_start,
1589 lldb_utility::ansi::k_fg_green,
1590 lldb_utility::ansi::k_escape_end);
1591 }
1592 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1593 {
1594 s.Printf ("%s%s%s",
1595 lldb_utility::ansi::k_escape_start,
1596 lldb_utility::ansi::k_fg_yellow,
1597 lldb_utility::ansi::k_escape_end);
1598 }
1599 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1600 {
1601 s.Printf ("%s%s%s",
1602 lldb_utility::ansi::k_escape_start,
1603 lldb_utility::ansi::k_fg_blue,
1604 lldb_utility::ansi::k_escape_end);
1605 }
1606 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1607 {
1608 s.Printf ("%s%s%s",
1609 lldb_utility::ansi::k_escape_start,
1610 lldb_utility::ansi::k_fg_purple,
1611 lldb_utility::ansi::k_escape_end);
1612 }
1613 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1614 {
1615 s.Printf ("%s%s%s",
1616 lldb_utility::ansi::k_escape_start,
1617 lldb_utility::ansi::k_fg_cyan,
1618 lldb_utility::ansi::k_escape_end);
1619 }
1620 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1621 {
1622 s.Printf ("%s%s%s",
1623 lldb_utility::ansi::k_escape_start,
1624 lldb_utility::ansi::k_fg_white,
1625 lldb_utility::ansi::k_escape_end);
1626 }
1627 else
1628 {
1629 var_success = false;
1630 }
1631 }
1632 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1633 {
1634 var_name_begin += strlen("bg."); // Skip the "bg."
1635 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1636 {
1637 s.Printf ("%s%s%s",
1638 lldb_utility::ansi::k_escape_start,
1639 lldb_utility::ansi::k_bg_black,
1640 lldb_utility::ansi::k_escape_end);
1641 }
1642 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1643 {
1644 s.Printf ("%s%s%s",
1645 lldb_utility::ansi::k_escape_start,
1646 lldb_utility::ansi::k_bg_red,
1647 lldb_utility::ansi::k_escape_end);
1648 }
1649 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1650 {
1651 s.Printf ("%s%s%s",
1652 lldb_utility::ansi::k_escape_start,
1653 lldb_utility::ansi::k_bg_green,
1654 lldb_utility::ansi::k_escape_end);
1655 }
1656 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1657 {
1658 s.Printf ("%s%s%s",
1659 lldb_utility::ansi::k_escape_start,
1660 lldb_utility::ansi::k_bg_yellow,
1661 lldb_utility::ansi::k_escape_end);
1662 }
1663 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1664 {
1665 s.Printf ("%s%s%s",
1666 lldb_utility::ansi::k_escape_start,
1667 lldb_utility::ansi::k_bg_blue,
1668 lldb_utility::ansi::k_escape_end);
1669 }
1670 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1671 {
1672 s.Printf ("%s%s%s",
1673 lldb_utility::ansi::k_escape_start,
1674 lldb_utility::ansi::k_bg_purple,
1675 lldb_utility::ansi::k_escape_end);
1676 }
1677 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1678 {
1679 s.Printf ("%s%s%s",
1680 lldb_utility::ansi::k_escape_start,
1681 lldb_utility::ansi::k_bg_cyan,
1682 lldb_utility::ansi::k_escape_end);
1683 }
1684 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1685 {
1686 s.Printf ("%s%s%s",
1687 lldb_utility::ansi::k_escape_start,
1688 lldb_utility::ansi::k_bg_white,
1689 lldb_utility::ansi::k_escape_end);
1690 }
1691 else
1692 {
1693 var_success = false;
1694 }
1695 }
1696 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1697 {
1698 s.Printf ("%s%s%s",
1699 lldb_utility::ansi::k_escape_start,
1700 lldb_utility::ansi::k_ctrl_normal,
1701 lldb_utility::ansi::k_escape_end);
1702 }
1703 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1704 {
1705 s.Printf ("%s%s%s",
1706 lldb_utility::ansi::k_escape_start,
1707 lldb_utility::ansi::k_ctrl_bold,
1708 lldb_utility::ansi::k_escape_end);
1709 }
1710 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1711 {
1712 s.Printf ("%s%s%s",
1713 lldb_utility::ansi::k_escape_start,
1714 lldb_utility::ansi::k_ctrl_faint,
1715 lldb_utility::ansi::k_escape_end);
1716 }
1717 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1718 {
1719 s.Printf ("%s%s%s",
1720 lldb_utility::ansi::k_escape_start,
1721 lldb_utility::ansi::k_ctrl_italic,
1722 lldb_utility::ansi::k_escape_end);
1723 }
1724 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1725 {
1726 s.Printf ("%s%s%s",
1727 lldb_utility::ansi::k_escape_start,
1728 lldb_utility::ansi::k_ctrl_underline,
1729 lldb_utility::ansi::k_escape_end);
1730 }
1731 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1732 {
1733 s.Printf ("%s%s%s",
1734 lldb_utility::ansi::k_escape_start,
1735 lldb_utility::ansi::k_ctrl_slow_blink,
1736 lldb_utility::ansi::k_escape_end);
1737 }
1738 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1739 {
1740 s.Printf ("%s%s%s",
1741 lldb_utility::ansi::k_escape_start,
1742 lldb_utility::ansi::k_ctrl_fast_blink,
1743 lldb_utility::ansi::k_escape_end);
1744 }
1745 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1746 {
1747 s.Printf ("%s%s%s",
1748 lldb_utility::ansi::k_escape_start,
1749 lldb_utility::ansi::k_ctrl_negative,
1750 lldb_utility::ansi::k_escape_end);
1751 }
1752 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1753 {
1754 s.Printf ("%s%s%s",
1755 lldb_utility::ansi::k_escape_start,
1756 lldb_utility::ansi::k_ctrl_conceal,
1757 lldb_utility::ansi::k_escape_end);
1758
1759 }
1760 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1761 {
1762 s.Printf ("%s%s%s",
1763 lldb_utility::ansi::k_escape_start,
1764 lldb_utility::ansi::k_ctrl_crossed_out,
1765 lldb_utility::ansi::k_escape_end);
1766 }
1767 else
1768 {
1769 var_success = false;
1770 }
1771 }
Greg Clayton1b654882010-09-19 02:33:57 +00001772 break;
1773
1774 case 'p':
1775 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1776 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001777 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001778 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001779 Process *process = exe_ctx->GetProcessPtr();
1780 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001781 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001782 var_name_begin += ::strlen ("process.");
1783 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001784 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001785 s.Printf("%llu", process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001786 var_success = true;
1787 }
1788 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1789 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1790 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1791 {
1792 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1793 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001794 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001795 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1796 {
1797 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1798 var_success = format_file_spec;
1799 }
1800 else
1801 {
1802 format_file_spec = exe_module->GetFileSpec();
1803 var_success = format_file_spec;
1804 }
Greg Clayton1b654882010-09-19 02:33:57 +00001805 }
1806 }
1807 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001808 }
Greg Clayton1b654882010-09-19 02:33:57 +00001809 }
1810 break;
1811
1812 case 't':
1813 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1814 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001815 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001816 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001817 Thread *thread = exe_ctx->GetThreadPtr();
1818 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001819 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001820 var_name_begin += ::strlen ("thread.");
1821 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001822 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001823 s.Printf("0x%4.4llx", thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001824 var_success = true;
1825 }
1826 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1827 {
1828 s.Printf("%u", thread->GetIndexID());
1829 var_success = true;
1830 }
1831 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1832 {
1833 cstr = thread->GetName();
1834 var_success = cstr && cstr[0];
1835 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001836 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001837 }
1838 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1839 {
1840 cstr = thread->GetQueueName();
1841 var_success = cstr && cstr[0];
1842 if (var_success)
1843 s.PutCString(cstr);
1844 }
1845 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1846 {
1847 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1848 if (stop_info_sp)
1849 {
1850 cstr = stop_info_sp->GetDescription();
1851 if (cstr && cstr[0])
1852 {
1853 s.PutCString(cstr);
1854 var_success = true;
1855 }
Greg Clayton1b654882010-09-19 02:33:57 +00001856 }
1857 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001858 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1859 {
1860 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1861 if (stop_info_sp)
1862 {
1863 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1864 if (return_valobj_sp)
1865 {
Jim Inghamef651602011-12-22 19:12:40 +00001866 ValueObject::DumpValueObjectOptions dump_options;
1867 ValueObject::DumpValueObject (s, return_valobj_sp.get(), dump_options);
1868 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001869 }
1870 }
1871 }
Greg Clayton1b654882010-09-19 02:33:57 +00001872 }
1873 }
1874 }
1875 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1876 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001877 // TODO: hookup properties
1878// if (!target_properties_sp)
1879// {
1880// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1881// if (target)
1882// target_properties_sp = target->GetProperties();
1883// }
1884//
1885// if (target_properties_sp)
1886// {
1887// var_name_begin += ::strlen ("target.");
1888// const char *end_property = strchr(var_name_begin, '}');
1889// if (end_property)
1890// {
1891// ConstString property_name(var_name_begin, end_property - var_name_begin);
1892// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
1893// if (!property_value.empty())
1894// {
1895// s.PutCString (property_value.c_str());
1896// var_success = true;
1897// }
1898// }
1899// }
Greg Clayton0603aa92010-10-04 01:05:56 +00001900 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1901 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001902 {
Greg Clayton1b654882010-09-19 02:33:57 +00001903 var_name_begin += ::strlen ("target.");
1904 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1905 {
1906 ArchSpec arch (target->GetArchitecture ());
1907 if (arch.IsValid())
1908 {
Greg Clayton64195a22011-02-23 00:35:02 +00001909 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001910 var_success = true;
1911 }
1912 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001913 }
Greg Clayton1b654882010-09-19 02:33:57 +00001914 }
1915 break;
1916
1917
1918 case 'm':
1919 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1920 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001921 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001922 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001923 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001924 var_name_begin += ::strlen ("module.");
1925
1926 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1927 {
1928 if (module->GetFileSpec())
1929 {
1930 var_name_begin += ::strlen ("file.");
1931
1932 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1933 {
1934 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1935 var_success = format_file_spec;
1936 }
1937 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1938 {
1939 format_file_spec = module->GetFileSpec();
1940 var_success = format_file_spec;
1941 }
1942 }
1943 }
1944 }
1945 }
1946 break;
1947
1948
1949 case 'f':
1950 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1951 {
1952 if (sc && sc->comp_unit != NULL)
1953 {
1954 var_name_begin += ::strlen ("file.");
1955
1956 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1957 {
1958 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1959 var_success = format_file_spec;
1960 }
1961 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1962 {
1963 format_file_spec = *sc->comp_unit;
1964 var_success = format_file_spec;
1965 }
1966 }
1967 }
1968 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1969 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001970 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001971 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001972 StackFrame *frame = exe_ctx->GetFramePtr();
1973 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001974 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001975 var_name_begin += ::strlen ("frame.");
1976 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001977 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001978 s.Printf("%u", frame->GetFrameIndex());
1979 var_success = true;
1980 }
1981 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1982 {
1983 reg_kind = eRegisterKindGeneric;
1984 reg_num = LLDB_REGNUM_GENERIC_PC;
1985 var_success = true;
1986 }
1987 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1988 {
1989 reg_kind = eRegisterKindGeneric;
1990 reg_num = LLDB_REGNUM_GENERIC_SP;
1991 var_success = true;
1992 }
1993 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1994 {
1995 reg_kind = eRegisterKindGeneric;
1996 reg_num = LLDB_REGNUM_GENERIC_FP;
1997 var_success = true;
1998 }
1999 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
2000 {
2001 reg_kind = eRegisterKindGeneric;
2002 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2003 var_success = true;
2004 }
2005 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
2006 {
2007 reg_ctx = frame->GetRegisterContext().get();
2008 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002009 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002010 var_name_begin += ::strlen ("reg.");
2011 if (var_name_begin < var_name_end)
2012 {
2013 std::string reg_name (var_name_begin, var_name_end);
2014 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2015 if (reg_info)
2016 var_success = true;
2017 }
Greg Clayton1b654882010-09-19 02:33:57 +00002018 }
2019 }
2020 }
2021 }
2022 }
2023 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
2024 {
2025 if (sc && (sc->function != NULL || sc->symbol != NULL))
2026 {
2027 var_name_begin += ::strlen ("function.");
2028 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2029 {
2030 if (sc->function)
Greg Clayton81c22f62011-10-19 18:09:39 +00002031 s.Printf("function{0x%8.8llx}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002032 else
2033 s.Printf("symbol[%u]", sc->symbol->GetID());
2034
2035 var_success = true;
2036 }
2037 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2038 {
2039 if (sc->function)
2040 cstr = sc->function->GetName().AsCString (NULL);
2041 else if (sc->symbol)
2042 cstr = sc->symbol->GetName().AsCString (NULL);
2043 if (cstr)
2044 {
2045 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002046
2047 if (sc->block)
2048 {
2049 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2050 if (inline_block)
2051 {
2052 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2053 if (inline_info)
2054 {
2055 s.PutCString(" [inlined] ");
2056 inline_info->GetName().Dump(&s);
2057 }
2058 }
2059 }
Greg Clayton1b654882010-09-19 02:33:57 +00002060 var_success = true;
2061 }
2062 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002063 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2064 {
2065 // Print the function name with arguments in it
2066
2067 if (sc->function)
2068 {
2069 var_success = true;
2070 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2071 cstr = sc->function->GetName().AsCString (NULL);
2072 if (cstr)
2073 {
2074 const InlineFunctionInfo *inline_info = NULL;
2075 VariableListSP variable_list_sp;
2076 bool get_function_vars = true;
2077 if (sc->block)
2078 {
2079 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2080
2081 if (inline_block)
2082 {
2083 get_function_vars = false;
2084 inline_info = sc->block->GetInlinedFunctionInfo();
2085 if (inline_info)
2086 variable_list_sp = inline_block->GetBlockVariableList (true);
2087 }
2088 }
2089
2090 if (get_function_vars)
2091 {
2092 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2093 }
2094
2095 if (inline_info)
2096 {
2097 s.PutCString (cstr);
2098 s.PutCString (" [inlined] ");
2099 cstr = inline_info->GetName().GetCString();
2100 }
2101
2102 VariableList args;
2103 if (variable_list_sp)
2104 {
2105 const size_t num_variables = variable_list_sp->GetSize();
2106 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
2107 {
2108 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
2109 if (var_sp->GetScope() == eValueTypeVariableArgument)
2110 args.AddVariable (var_sp);
2111 }
2112
2113 }
2114 if (args.GetSize() > 0)
2115 {
2116 const char *open_paren = strchr (cstr, '(');
2117 const char *close_paren = NULL;
2118 if (open_paren)
2119 close_paren = strchr (open_paren, ')');
2120
2121 if (open_paren)
2122 s.Write(cstr, open_paren - cstr + 1);
2123 else
2124 {
2125 s.PutCString (cstr);
2126 s.PutChar ('(');
2127 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002128 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002129 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2130 {
2131 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2132 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2133 const char *var_name = var_value_sp->GetName().GetCString();
2134 const char *var_value = var_value_sp->GetValueAsCString();
2135 if (var_value_sp->GetError().Success())
2136 {
2137 if (arg_idx > 0)
2138 s.PutCString (", ");
2139 s.Printf ("%s=%s", var_name, var_value);
2140 }
2141 }
2142
2143 if (close_paren)
2144 s.PutCString (close_paren);
2145 else
2146 s.PutChar(')');
2147
2148 }
2149 else
2150 {
2151 s.PutCString(cstr);
2152 }
2153 }
2154 }
2155 else if (sc->symbol)
2156 {
2157 cstr = sc->symbol->GetName().AsCString (NULL);
2158 if (cstr)
2159 {
2160 s.PutCString(cstr);
2161 var_success = true;
2162 }
2163 }
2164 }
Greg Clayton1b654882010-09-19 02:33:57 +00002165 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2166 {
2167 var_success = addr != NULL;
2168 if (var_success)
2169 {
2170 format_addr = *addr;
2171 calculate_format_addr_function_offset = true;
2172 }
2173 }
2174 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2175 {
2176 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2177 if (var_success)
2178 {
2179 format_addr = sc->line_entry.range.GetBaseAddress();
2180 calculate_format_addr_function_offset = true;
2181 }
2182 }
2183 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2184 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002185 StackFrame *frame = exe_ctx->GetFramePtr();
2186 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002187 if (var_success)
2188 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002189 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002190 calculate_format_addr_function_offset = true;
2191 }
2192 }
2193 }
2194 }
2195 break;
2196
2197 case 'l':
2198 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2199 {
2200 if (sc && sc->line_entry.IsValid())
2201 {
2202 var_name_begin += ::strlen ("line.");
2203 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2204 {
2205 var_name_begin += ::strlen ("file.");
2206
2207 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2208 {
2209 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2210 var_success = format_file_spec;
2211 }
2212 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2213 {
2214 format_file_spec = sc->line_entry.file;
2215 var_success = format_file_spec;
2216 }
2217 }
2218 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2219 {
2220 var_success = true;
2221 s.Printf("%u", sc->line_entry.line);
2222 }
2223 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2224 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2225 {
2226 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2227 if (var_success)
2228 {
2229 format_addr = sc->line_entry.range.GetBaseAddress();
2230 if (var_name_begin[0] == 'e')
2231 format_addr.Slide (sc->line_entry.range.GetByteSize());
2232 }
2233 }
2234 }
2235 }
2236 break;
2237 }
2238
2239 if (var_success)
2240 {
2241 // If format addr is valid, then we need to print an address
2242 if (reg_num != LLDB_INVALID_REGNUM)
2243 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002244 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002245 // We have a register value to display...
2246 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2247 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002248 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002249 }
2250 else
2251 {
2252 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002253 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002254
2255 if (reg_ctx)
2256 {
2257 if (reg_kind != kNumRegisterKinds)
2258 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2259 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2260 var_success = reg_info != NULL;
2261 }
2262 }
2263 }
2264
2265 if (reg_info != NULL)
2266 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002267 RegisterValue reg_value;
2268 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2269 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002270 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002271 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002272 }
2273 }
2274
2275 if (format_file_spec)
2276 {
2277 s << format_file_spec;
2278 }
2279
2280 // If format addr is valid, then we need to print an address
2281 if (format_addr.IsValid())
2282 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002283 var_success = false;
2284
Greg Clayton1b654882010-09-19 02:33:57 +00002285 if (calculate_format_addr_function_offset)
2286 {
2287 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002288
Greg Clayton0603aa92010-10-04 01:05:56 +00002289 if (sc)
2290 {
2291 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002292 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002293 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002294 if (sc->block)
2295 {
2296 // Check to make sure we aren't in an inline
2297 // function. If we are, use the inline block
2298 // range that contains "format_addr" since
2299 // blocks can be discontiguous.
2300 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2301 AddressRange inline_range;
2302 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2303 func_addr = inline_range.GetBaseAddress();
2304 }
2305 }
Greg Claytone7612132012-03-07 21:03:09 +00002306 else if (sc->symbol && sc->symbol->ValueIsAddress())
2307 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002308 }
2309
2310 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002311 {
2312 if (func_addr.GetSection() == format_addr.GetSection())
2313 {
2314 addr_t func_file_addr = func_addr.GetFileAddress();
2315 addr_t addr_file_addr = format_addr.GetFileAddress();
2316 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002317 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002318 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002319 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002320 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002321 }
2322 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002323 {
2324 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2325 if (target)
2326 {
2327 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2328 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2329 if (addr_load_addr > func_load_addr)
2330 s.Printf(" + %llu", addr_load_addr - func_load_addr);
2331 else if (addr_load_addr < func_load_addr)
2332 s.Printf(" - %llu", func_load_addr - addr_load_addr);
2333 var_success = true;
2334 }
2335 }
Greg Clayton1b654882010-09-19 02:33:57 +00002336 }
2337 }
2338 else
2339 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002340 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002341 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002342 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2343 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002344 if (vaddr == LLDB_INVALID_ADDRESS)
2345 vaddr = format_addr.GetFileAddress ();
2346
2347 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002348 {
Greg Clayton514487e2011-02-15 21:59:32 +00002349 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002350 if (addr_width == 0)
2351 addr_width = 16;
2352 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002353 var_success = true;
2354 }
Greg Clayton1b654882010-09-19 02:33:57 +00002355 }
2356 }
2357 }
2358
2359 if (var_success == false)
2360 success = false;
2361 }
2362 p = var_name_end;
2363 }
2364 else
2365 break;
2366 }
2367 else
2368 {
2369 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2370 s.PutChar(*p);
2371 }
2372 }
2373 else if (*p == '\\')
2374 {
2375 ++p; // skip the slash
2376 switch (*p)
2377 {
2378 case 'a': s.PutChar ('\a'); break;
2379 case 'b': s.PutChar ('\b'); break;
2380 case 'f': s.PutChar ('\f'); break;
2381 case 'n': s.PutChar ('\n'); break;
2382 case 'r': s.PutChar ('\r'); break;
2383 case 't': s.PutChar ('\t'); break;
2384 case 'v': s.PutChar ('\v'); break;
2385 case '\'': s.PutChar ('\''); break;
2386 case '\\': s.PutChar ('\\'); break;
2387 case '0':
2388 // 1 to 3 octal chars
2389 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002390 // Make a string that can hold onto the initial zero char,
2391 // up to 3 octal digits, and a terminating NULL.
2392 char oct_str[5] = { 0, 0, 0, 0, 0 };
2393
2394 int i;
2395 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2396 oct_str[i] = p[i];
2397
2398 // We don't want to consume the last octal character since
2399 // the main for loop will do this for us, so we advance p by
2400 // one less than i (even if i is zero)
2401 p += i - 1;
2402 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2403 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002404 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002405 char octal_char = octal_value;
2406 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00002407 }
Greg Clayton1b654882010-09-19 02:33:57 +00002408 }
2409 break;
2410
2411 case 'x':
2412 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002413 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002414 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002415 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002416
Greg Clayton0603aa92010-10-04 01:05:56 +00002417 // Make a string that can hold onto two hex chars plus a
2418 // NULL terminator
2419 char hex_str[3] = { 0,0,0 };
2420 hex_str[0] = *p;
2421 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002422 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002423 ++p; // Skip the first of the two hex chars
2424 hex_str[1] = *p;
2425 }
2426
2427 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2428 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002429 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002430 }
2431 else
2432 {
2433 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002434 }
2435 break;
2436
2437 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002438 // Just desensitize any other character by just printing what
2439 // came after the '\'
2440 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002441 break;
2442
2443 }
2444
2445 }
2446 }
2447 if (end)
2448 *end = p;
2449 return success;
2450}
2451
Jim Ingham228063c2012-02-21 02:23:08 +00002452void
2453Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2454{
Jim Ingham4f02b222012-02-22 22:49:20 +00002455 // For simplicity's sake, I am not going to deal with how to close down any
2456 // open logging streams, I just redirect everything from here on out to the
2457 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002458 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2459}
2460
2461bool
2462Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2463{
2464 Log::Callbacks log_callbacks;
2465
2466 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002467 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002468 {
2469 log_stream_sp = m_log_callback_stream_sp;
2470 // For now when using the callback mode you always get thread & timestamp.
2471 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2472 }
2473 else if (log_file == NULL || *log_file == '\0')
2474 {
2475 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2476 }
2477 else
2478 {
2479 LogStreamMap::iterator pos = m_log_streams.find(log_file);
2480 if (pos == m_log_streams.end())
2481 {
2482 log_stream_sp.reset (new StreamFile (log_file));
2483 m_log_streams[log_file] = log_stream_sp;
2484 }
2485 else
2486 log_stream_sp = pos->second;
2487 }
2488 assert (log_stream_sp.get());
2489
2490 if (log_options == 0)
2491 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2492
2493 if (Log::GetLogChannelCallbacks (channel, log_callbacks))
2494 {
2495 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2496 return true;
2497 }
2498 else
2499 {
2500 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2501 if (log_channel_sp)
2502 {
2503 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2504 {
2505 return true;
2506 }
2507 else
2508 {
2509 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2510 return false;
2511 }
2512 }
2513 else
2514 {
2515 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2516 return false;
2517 }
2518 }
2519 return false;
2520}
2521