blob: 23fbbadbacefc9ba7d3665688d05562230e5b05d [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 Clayton7349bd92011-05-09 20:18:18 +000022#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000024#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000025#include "lldb/Core/StreamCallback.h"
Greg Clayton1b654882010-09-19 02:33:57 +000026#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000028#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000029#include "lldb/Core/ValueObjectVariable.h"
Greg Claytona3406612011-02-07 23:24:47 +000030#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000031#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000032#include "lldb/Interpreter/OptionValueSInt64.h"
33#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000034#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Target/TargetList.h"
36#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000037#include "lldb/Target/RegisterContext.h"
38#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000040#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
42using namespace lldb;
43using namespace lldb_private;
44
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045
Greg Clayton1b654882010-09-19 02:33:57 +000046static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000047static lldb::user_id_t g_unique_id = 1;
48
Greg Clayton1b654882010-09-19 02:33:57 +000049#pragma mark Static Functions
50
51static Mutex &
52GetDebuggerListMutex ()
53{
54 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
55 return g_mutex;
56}
57
58typedef std::vector<DebuggerSP> DebuggerList;
59
60static DebuggerList &
61GetDebuggerList()
62{
63 // hide the static debugger list inside a singleton accessor to avoid
64 // global init contructors
65 static DebuggerList g_list;
66 return g_list;
67}
Greg Claytone372b982011-11-21 21:44:34 +000068
69OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000070g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000071{
Greg Clayton67cc0632012-08-22 17:17:09 +000072 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
73 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
74 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000075 { 0, NULL, NULL }
76};
77
Greg Clayton67cc0632012-08-22 17:17:09 +000078OptionEnumValueElement
79g_language_enumerators[] =
80{
81 { eScriptLanguageNone, "none", "Disable scripting languages."},
82 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
83 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
84};
Greg Claytone372b982011-11-21 21:44:34 +000085
Greg Clayton67cc0632012-08-22 17:17:09 +000086#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
87#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
88
89#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
90 "{, ${frame.pc}}"\
91 MODULE_WITH_FUNC\
92 FILE_AND_LINE\
93 "{, stop reason = ${thread.stop-reason}}"\
94 "{\\nReturn value: ${thread.return-value}}"\
95 "\\n"
96
97#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
98 MODULE_WITH_FUNC\
99 FILE_AND_LINE\
100 "\\n"
101
102
103
Greg Clayton754a9362012-08-23 00:22:02 +0000104static PropertyDefinition
105g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000106{
107{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
108{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
109{ "notify-void", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
110{ "prompt", OptionValue::eTypeString , true, 0 , "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
111{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
112{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
113{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
114{ "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." },
115{ "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." },
116{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
117{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
118{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
119{ NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
120};
121
122enum
123{
124 ePropertyAutoConfirm = 0,
125 ePropertyFrameFormat,
126 ePropertyNotiftVoid,
127 ePropertyPrompt,
128 ePropertyScriptLanguage,
129 ePropertyStopDisassemblyCount,
130 ePropertyStopDisassemblyDisplay,
131 ePropertyStopLineCountAfter,
132 ePropertyStopLineCountBefore,
133 ePropertyTerminalWidth,
134 ePropertyThreadFormat,
135 ePropertyUseExternalEditor
136};
137
138//
139//const char *
140//Debugger::GetFrameFormat() const
141//{
142// return m_properties_sp->GetFrameFormat();
143//}
144//const char *
145//Debugger::GetThreadFormat() const
146//{
147// return m_properties_sp->GetThreadFormat();
148//}
149//
150bool
151Debugger::GetAutoConfirm () const
152{
153 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000154 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000155}
156
157const char *
158Debugger::GetFrameFormat() const
159{
160 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000161 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000162}
163
164bool
165Debugger::GetNotifyVoid () const
166{
167 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000168 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000169}
170
171const char *
172Debugger::GetPrompt() const
173{
174 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000175 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000176}
177
178void
179Debugger::SetPrompt(const char *p)
180{
181 const uint32_t idx = ePropertyPrompt;
182 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
183 const char *new_prompt = GetPrompt();
184 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
185 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
186}
187
188const char *
189Debugger::GetThreadFormat() const
190{
191 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000192 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000193}
194
195lldb::ScriptLanguage
196Debugger::GetScriptLanguage() const
197{
198 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000199 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000200}
201
202bool
203Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
204{
205 const uint32_t idx = ePropertyScriptLanguage;
206 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
207}
208
209uint32_t
210Debugger::GetTerminalWidth () const
211{
212 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000213 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000214}
215
216bool
217Debugger::SetTerminalWidth (uint32_t term_width)
218{
219 const uint32_t idx = ePropertyTerminalWidth;
220 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
221}
222
223bool
224Debugger::GetUseExternalEditor () const
225{
226 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000227 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000228}
229
230bool
231Debugger::SetUseExternalEditor (bool b)
232{
233 const uint32_t idx = ePropertyUseExternalEditor;
234 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
235}
236
237uint32_t
238Debugger::GetStopSourceLineCount (bool before) const
239{
240 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000241 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000242}
243
244Debugger::StopDisassemblyType
245Debugger::GetStopDisassemblyDisplay () const
246{
247 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000248 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000249}
250
251uint32_t
252Debugger::GetDisassemblyLineCount () const
253{
254 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000255 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000256}
Greg Claytone372b982011-11-21 21:44:34 +0000257
Greg Clayton1b654882010-09-19 02:33:57 +0000258#pragma mark Debugger
259
Greg Clayton67cc0632012-08-22 17:17:09 +0000260//const DebuggerPropertiesSP &
261//Debugger::GetSettings() const
262//{
263// return m_properties_sp;
264//}
265//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000266
Caroline Tice2f88aad2011-01-14 00:29:16 +0000267int
268Debugger::TestDebuggerRefCount ()
269{
270 return g_shared_debugger_refcount;
271}
272
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273void
274Debugger::Initialize ()
275{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000276 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000277 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278}
279
280void
281Debugger::Terminate ()
282{
Greg Clayton66111032010-06-23 01:19:29 +0000283 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 {
Greg Clayton66111032010-06-23 01:19:29 +0000285 g_shared_debugger_refcount--;
286 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 {
Greg Claytondbe54502010-11-19 03:46:01 +0000288 lldb_private::WillTerminate();
289 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000290
291 // Clear our master list of debugger objects
292 Mutex::Locker locker (GetDebuggerListMutex ());
293 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 }
296}
297
Caroline Tice20bd37f2011-03-10 22:14:10 +0000298void
299Debugger::SettingsInitialize ()
300{
Greg Clayton6920b522012-08-22 18:39:03 +0000301 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000302}
303
304void
305Debugger::SettingsTerminate ()
306{
Greg Clayton6920b522012-08-22 18:39:03 +0000307 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000308}
309
Greg Clayton66111032010-06-23 01:19:29 +0000310DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000311Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000312{
Jim Ingham228063c2012-02-21 02:23:08 +0000313 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000314 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000315 {
316 Mutex::Locker locker (GetDebuggerListMutex ());
317 GetDebuggerList().push_back(debugger_sp);
318 }
319 return debugger_sp;
320}
321
Caroline Ticee02657b2011-01-22 01:02:07 +0000322void
Greg Clayton4d122c42011-09-17 08:33:22 +0000323Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000324{
325 if (debugger_sp.get() == NULL)
326 return;
327
Jim Ingham8314c522011-09-15 21:36:42 +0000328 debugger_sp->Clear();
329
Greg Claytonc15f55e2012-03-30 20:53:46 +0000330 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000331 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000332 Mutex::Locker locker (GetDebuggerListMutex ());
333 DebuggerList &debugger_list = GetDebuggerList ();
334 DebuggerList::iterator pos, end = debugger_list.end();
335 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000336 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000337 if ((*pos).get() == debugger_sp.get())
338 {
339 debugger_list.erase (pos);
340 return;
341 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000342 }
343 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000344}
345
Greg Clayton4d122c42011-09-17 08:33:22 +0000346DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000347Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
348{
Greg Clayton4d122c42011-09-17 08:33:22 +0000349 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000350 if (g_shared_debugger_refcount > 0)
351 {
352 Mutex::Locker locker (GetDebuggerListMutex ());
353 DebuggerList &debugger_list = GetDebuggerList();
354 DebuggerList::iterator pos, end = debugger_list.end();
355
356 for (pos = debugger_list.begin(); pos != end; ++pos)
357 {
358 if ((*pos).get()->m_instance_name == instance_name)
359 {
360 debugger_sp = *pos;
361 break;
362 }
363 }
364 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000365 return debugger_sp;
366}
Greg Clayton66111032010-06-23 01:19:29 +0000367
368TargetSP
369Debugger::FindTargetWithProcessID (lldb::pid_t pid)
370{
Greg Clayton4d122c42011-09-17 08:33:22 +0000371 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000372 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000373 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000374 Mutex::Locker locker (GetDebuggerListMutex ());
375 DebuggerList &debugger_list = GetDebuggerList();
376 DebuggerList::iterator pos, end = debugger_list.end();
377 for (pos = debugger_list.begin(); pos != end; ++pos)
378 {
379 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
380 if (target_sp)
381 break;
382 }
Greg Clayton66111032010-06-23 01:19:29 +0000383 }
384 return target_sp;
385}
386
Greg Claytone4e45922011-11-16 05:37:56 +0000387TargetSP
388Debugger::FindTargetWithProcess (Process *process)
389{
390 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000391 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000392 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000393 Mutex::Locker locker (GetDebuggerListMutex ());
394 DebuggerList &debugger_list = GetDebuggerList();
395 DebuggerList::iterator pos, end = debugger_list.end();
396 for (pos = debugger_list.begin(); pos != end; ++pos)
397 {
398 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
399 if (target_sp)
400 break;
401 }
Greg Claytone4e45922011-11-16 05:37:56 +0000402 }
403 return target_sp;
404}
405
Jim Ingham228063c2012-02-21 02:23:08 +0000406Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000407 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000408 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000409 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 m_input_file (),
411 m_output_file (),
412 m_error_file (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000413 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000414 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 m_listener ("lldb.Debugger"),
Jim Inghame37d6052011-09-13 00:29:56 +0000416 m_source_manager(*this),
417 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000418 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000419 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000420 m_input_reader_data (),
421 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422{
Greg Clayton67cc0632012-08-22 17:17:09 +0000423 char instance_cstr[256];
424 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
425 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000426 if (log_callback)
427 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000428 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000429 // Always add our default platform to the platform list
430 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
431 assert (default_platform_sp.get());
432 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000433
Greg Clayton754a9362012-08-23 00:22:02 +0000434 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000435 m_collection_sp->AppendProperty (ConstString("target"),
436 ConstString("Settings specify to debugging targets."),
437 true,
438 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000439 if (m_command_interpreter_ap.get())
440 {
441 m_collection_sp->AppendProperty (ConstString("interpreter"),
442 ConstString("Settings specify to the debugger's command interpreter."),
443 true,
444 m_command_interpreter_ap->GetValueProperties());
445 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000446 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
447 term_width->SetMinimumValue(10);
448 term_width->SetMaximumValue(1024);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449}
450
451Debugger::~Debugger ()
452{
Jim Ingham8314c522011-09-15 21:36:42 +0000453 Clear();
454}
455
456void
457Debugger::Clear()
458{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000459 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000460 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000461 int num_targets = m_target_list.GetNumTargets();
462 for (int i = 0; i < num_targets; i++)
463 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000464 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
465 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000466 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000467 ProcessSP process_sp (target_sp->GetProcessSP());
468 if (process_sp)
469 {
470 if (process_sp->GetShouldDetach())
471 process_sp->Detach();
472 }
473 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000474 }
Greg Clayton66111032010-06-23 01:19:29 +0000475 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000476 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000477
478 // Close the input file _before_ we close the input read communications class
479 // as it does NOT own the input file, our m_input_file does.
480 GetInputFile().Close ();
481 // Now that we have closed m_input_file, we can now tell our input communication
482 // class to close down. Its read thread should quickly exit after we close
483 // the input file handle above.
484 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000485}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486
487bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000488Debugger::GetCloseInputOnEOF () const
489{
490 return m_input_comm.GetCloseOnEOF();
491}
492
493void
494Debugger::SetCloseInputOnEOF (bool b)
495{
496 m_input_comm.SetCloseOnEOF(b);
497}
498
499bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500Debugger::GetAsyncExecution ()
501{
Greg Clayton66111032010-06-23 01:19:29 +0000502 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503}
504
505void
506Debugger::SetAsyncExecution (bool async_execution)
507{
Greg Clayton66111032010-06-23 01:19:29 +0000508 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509}
510
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511
512void
513Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
514{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000515 File &in_file = GetInputFile();
516 in_file.SetStream (fh, tranfer_ownership);
517 if (in_file.IsValid() == false)
518 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519
520 // Disconnect from any old connection if we had one
521 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000522 // Pass false as the second argument to ConnectionFileDescriptor below because
523 // our "in_file" above will already take ownership if requested and we don't
524 // want to objects trying to own and close a file descriptor.
525 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
527
528 Error error;
529 if (m_input_comm.StartReadThread (&error) == false)
530 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000531 File &err_file = GetErrorFile();
532
533 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
534 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536}
537
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538void
539Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
540{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000541 File &out_file = GetOutputFile();
542 out_file.SetStream (fh, tranfer_ownership);
543 if (out_file.IsValid() == false)
544 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000545
546 GetCommandInterpreter().GetScriptInterpreter()->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547}
548
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549void
550Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
551{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000552 File &err_file = GetErrorFile();
553 err_file.SetStream (fh, tranfer_ownership);
554 if (err_file.IsValid() == false)
555 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556}
557
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000559Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560{
561 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000562 TargetSP target_sp(GetSelectedTarget());
563 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564
565 if (target_sp)
566 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000567 ProcessSP process_sp (target_sp->GetProcessSP());
568 exe_ctx.SetProcessSP (process_sp);
569 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000571 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
572 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000574 exe_ctx.SetThreadSP (thread_sp);
575 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
576 if (exe_ctx.GetFramePtr() == NULL)
577 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578 }
579 }
580 }
581 return exe_ctx;
582
583}
584
Caroline Ticeb44880c2011-02-10 01:15:13 +0000585InputReaderSP
586Debugger::GetCurrentInputReader ()
587{
588 InputReaderSP reader_sp;
589
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000590 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000591 {
592 // Clear any finished readers from the stack
593 while (CheckIfTopInputReaderIsDone()) ;
594
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000595 if (!m_input_reader_stack.IsEmpty())
596 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000597 }
598
599 return reader_sp;
600}
601
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602void
603Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
604{
Caroline Ticeefed6132010-11-19 20:47:54 +0000605 if (bytes_len > 0)
606 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
607 else
608 ((Debugger *)baton)->DispatchInputEndOfFile ();
609}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610
611
612void
613Debugger::DispatchInput (const char *bytes, size_t bytes_len)
614{
Caroline Ticeefed6132010-11-19 20:47:54 +0000615 if (bytes == NULL || bytes_len == 0)
616 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617
618 WriteToDefaultReader (bytes, bytes_len);
619}
620
621void
Caroline Ticeefed6132010-11-19 20:47:54 +0000622Debugger::DispatchInputInterrupt ()
623{
624 m_input_reader_data.clear();
625
Caroline Ticeb44880c2011-02-10 01:15:13 +0000626 InputReaderSP reader_sp (GetCurrentInputReader ());
627 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000628 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000629 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000630
Caroline Ticeb44880c2011-02-10 01:15:13 +0000631 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000632 while (CheckIfTopInputReaderIsDone ()) ;
633 }
634}
635
636void
637Debugger::DispatchInputEndOfFile ()
638{
639 m_input_reader_data.clear();
640
Caroline Ticeb44880c2011-02-10 01:15:13 +0000641 InputReaderSP reader_sp (GetCurrentInputReader ());
642 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000643 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000644 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000645
Caroline Ticeb44880c2011-02-10 01:15:13 +0000646 // 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 +0000647 while (CheckIfTopInputReaderIsDone ()) ;
648 }
649}
650
651void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000652Debugger::CleanUpInputReaders ()
653{
654 m_input_reader_data.clear();
655
Caroline Ticeb44880c2011-02-10 01:15:13 +0000656 // 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 +0000657 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000658 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000659 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000660 if (reader_sp)
661 {
662 reader_sp->Notify (eInputReaderEndOfFile);
663 reader_sp->SetIsDone (true);
664 }
665 }
666}
667
668void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000669Debugger::NotifyTopInputReader (InputReaderAction notification)
670{
671 InputReaderSP reader_sp (GetCurrentInputReader());
672 if (reader_sp)
673 {
674 reader_sp->Notify (notification);
675
676 // Flush out any input readers that are done.
677 while (CheckIfTopInputReaderIsDone ())
678 /* Do nothing. */;
679 }
680}
681
Caroline Tice9088b062011-05-09 23:06:58 +0000682bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000683Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000684{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000685 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000686
Caroline Ticed61c10b2011-06-16 16:27:19 +0000687 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000688}
689
690
Caroline Tice969ed3d2011-05-02 20:41:46 +0000691void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
693{
694 if (bytes && bytes_len)
695 m_input_reader_data.append (bytes, bytes_len);
696
697 if (m_input_reader_data.empty())
698 return;
699
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000700 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000703 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 if (!reader_sp)
705 break;
706
Greg Clayton471b31c2010-07-20 22:52:08 +0000707 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 m_input_reader_data.size());
709 if (bytes_handled)
710 {
711 m_input_reader_data.erase (0, bytes_handled);
712 }
713 else
714 {
715 // No bytes were handled, we might not have reached our
716 // granularity, just return and wait for more data
717 break;
718 }
719 }
720
Caroline Ticeb44880c2011-02-10 01:15:13 +0000721 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000722 while (CheckIfTopInputReaderIsDone ())
723 /* Do nothing. */;
724
725}
726
727void
728Debugger::PushInputReader (const InputReaderSP& reader_sp)
729{
730 if (!reader_sp)
731 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000732
733 // Deactivate the old top reader
734 InputReaderSP top_reader_sp (GetCurrentInputReader ());
735
736 if (top_reader_sp)
737 top_reader_sp->Notify (eInputReaderDeactivate);
738
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000739 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740 reader_sp->Notify (eInputReaderActivate);
741 ActivateInputReader (reader_sp);
742}
743
744bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000745Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746{
747 bool result = false;
748
749 // The reader on the stop of the stack is done, so let the next
750 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000751 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000753 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000754 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755
756 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
757 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000758 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 reader_sp->Notify (eInputReaderDeactivate);
760 reader_sp->Notify (eInputReaderDone);
761 result = true;
762
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000763 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000765 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766 if (reader_sp)
767 {
768 ActivateInputReader (reader_sp);
769 reader_sp->Notify (eInputReaderReactivate);
770 }
771 }
772 }
773 }
774 return result;
775}
776
777bool
778Debugger::CheckIfTopInputReaderIsDone ()
779{
780 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000781 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000783 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000784 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785
786 if (reader_sp && reader_sp->IsDone())
787 {
788 result = true;
789 PopInputReader (reader_sp);
790 }
791 }
792 return result;
793}
794
795void
796Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
797{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000798 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000800 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000802 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000803
804 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805
Greg Claytona3406612011-02-07 23:24:47 +0000806 switch (reader_sp->GetGranularity())
807 {
808 case eInputReaderGranularityByte:
809 case eInputReaderGranularityWord:
810 tty.SetCanonical (false);
811 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812
Greg Claytona3406612011-02-07 23:24:47 +0000813 case eInputReaderGranularityLine:
814 case eInputReaderGranularityAll:
815 tty.SetCanonical (true);
816 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817
Greg Claytona3406612011-02-07 23:24:47 +0000818 default:
819 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820 }
821 }
822}
Greg Clayton66111032010-06-23 01:19:29 +0000823
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000824StreamSP
825Debugger::GetAsyncOutputStream ()
826{
827 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
828 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
829}
830
831StreamSP
832Debugger::GetAsyncErrorStream ()
833{
834 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
835 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
836}
837
Enrico Granata061858c2012-02-15 02:34:21 +0000838uint32_t
839Debugger::GetNumDebuggers()
840{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000841 if (g_shared_debugger_refcount > 0)
842 {
843 Mutex::Locker locker (GetDebuggerListMutex ());
844 return GetDebuggerList().size();
845 }
846 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +0000847}
848
849lldb::DebuggerSP
850Debugger::GetDebuggerAtIndex (uint32_t index)
851{
852 DebuggerSP debugger_sp;
853
Greg Claytonc15f55e2012-03-30 20:53:46 +0000854 if (g_shared_debugger_refcount > 0)
855 {
856 Mutex::Locker locker (GetDebuggerListMutex ());
857 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +0000858
Greg Claytonc15f55e2012-03-30 20:53:46 +0000859 if (index < debugger_list.size())
860 debugger_sp = debugger_list[index];
861 }
862
Enrico Granata061858c2012-02-15 02:34:21 +0000863 return debugger_sp;
864}
865
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000866DebuggerSP
867Debugger::FindDebuggerWithID (lldb::user_id_t id)
868{
Greg Clayton4d122c42011-09-17 08:33:22 +0000869 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000870
Greg Claytonc15f55e2012-03-30 20:53:46 +0000871 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000872 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000873 Mutex::Locker locker (GetDebuggerListMutex ());
874 DebuggerList &debugger_list = GetDebuggerList();
875 DebuggerList::iterator pos, end = debugger_list.end();
876 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000877 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000878 if ((*pos).get()->GetID() == id)
879 {
880 debugger_sp = *pos;
881 break;
882 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000883 }
884 }
885 return debugger_sp;
886}
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000887
Greg Clayton1b654882010-09-19 02:33:57 +0000888static void
889TestPromptFormats (StackFrame *frame)
890{
891 if (frame == NULL)
892 return;
893
894 StreamString s;
895 const char *prompt_format =
896 "{addr = '${addr}'\n}"
897 "{process.id = '${process.id}'\n}"
898 "{process.name = '${process.name}'\n}"
899 "{process.file.basename = '${process.file.basename}'\n}"
900 "{process.file.fullpath = '${process.file.fullpath}'\n}"
901 "{thread.id = '${thread.id}'\n}"
902 "{thread.index = '${thread.index}'\n}"
903 "{thread.name = '${thread.name}'\n}"
904 "{thread.queue = '${thread.queue}'\n}"
905 "{thread.stop-reason = '${thread.stop-reason}'\n}"
906 "{target.arch = '${target.arch}'\n}"
907 "{module.file.basename = '${module.file.basename}'\n}"
908 "{module.file.fullpath = '${module.file.fullpath}'\n}"
909 "{file.basename = '${file.basename}'\n}"
910 "{file.fullpath = '${file.fullpath}'\n}"
911 "{frame.index = '${frame.index}'\n}"
912 "{frame.pc = '${frame.pc}'\n}"
913 "{frame.sp = '${frame.sp}'\n}"
914 "{frame.fp = '${frame.fp}'\n}"
915 "{frame.flags = '${frame.flags}'\n}"
916 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
917 "{frame.reg.rip = '${frame.reg.rip}'\n}"
918 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
919 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
920 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
921 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
922 "{frame.reg.carp = '${frame.reg.carp}'\n}"
923 "{function.id = '${function.id}'\n}"
924 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +0000925 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +0000926 "{function.addr-offset = '${function.addr-offset}'\n}"
927 "{function.line-offset = '${function.line-offset}'\n}"
928 "{function.pc-offset = '${function.pc-offset}'\n}"
929 "{line.file.basename = '${line.file.basename}'\n}"
930 "{line.file.fullpath = '${line.file.fullpath}'\n}"
931 "{line.number = '${line.number}'\n}"
932 "{line.start-addr = '${line.start-addr}'\n}"
933 "{line.end-addr = '${line.end-addr}'\n}"
934;
935
936 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
937 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +0000938 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +0000939 const char *end = NULL;
940 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
941 {
942 printf("%s\n", s.GetData());
943 }
944 else
945 {
946 printf ("error: at '%s'\n", end);
947 printf ("what we got: %s\n", s.GetData());
948 }
949}
950
Enrico Granata9fc19442011-07-06 02:13:41 +0000951static bool
Enrico Granatadc940732011-08-23 00:32:52 +0000952ScanFormatDescriptor (const char* var_name_begin,
953 const char* var_name_end,
954 const char** var_name_final,
955 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +0000956 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +0000957 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +0000958{
Enrico Granatae992a082011-07-22 17:03:19 +0000959 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000960 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +0000961 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +0000962 {
963 if (log)
964 log->Printf("no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +0000965 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +0000966 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000967 else
968 {
969 *var_name_final = *percent_position;
970 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
971 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +0000972 if (log)
973 log->Printf("parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000974 if ( !FormatManager::GetFormatFromCString(format_name,
975 true,
976 *custom_format) )
977 {
Enrico Granatae992a082011-07-22 17:03:19 +0000978 if (log)
979 log->Printf("%s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000980 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +0000981 if (*format_name == '@')
Enrico Granata86cc9822012-03-19 22:58:49 +0000982 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
Enrico Granata9fc19442011-07-06 02:13:41 +0000983 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +0000984 else if (*format_name == 'V')
Enrico Granata86cc9822012-03-19 22:58:49 +0000985 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatad55546b2011-07-22 00:16:08 +0000986 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +0000987 else if (*format_name == 'L')
Enrico Granata86cc9822012-03-19 22:58:49 +0000988 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +0000989 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +0000990 else if (*format_name == 'S')
Enrico Granata86cc9822012-03-19 22:58:49 +0000991 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Enrico Granata5dfd49c2011-08-04 02:34:29 +0000992 else if (*format_name == '#')
Enrico Granata86cc9822012-03-19 22:58:49 +0000993 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
Enrico Granatad64d0bc2011-08-19 21:13:46 +0000994 else if (*format_name == 'T')
Enrico Granata86cc9822012-03-19 22:58:49 +0000995 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
Enrico Granatae992a082011-07-22 17:03:19 +0000996 else if (log)
997 log->Printf("%s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000998 }
999 // a good custom format tells us to print the value using it
1000 else
Enrico Granatae992a082011-07-22 17:03:19 +00001001 {
1002 if (log)
1003 log->Printf("will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001004 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001005 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001006 delete format_name;
1007 }
Enrico Granatae992a082011-07-22 17:03:19 +00001008 if (log)
1009 log->Printf("final format description outcome: custom_format = %d, val_obj_display = %d",
1010 *custom_format,
1011 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001012 return true;
1013}
1014
1015static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001016ScanBracketedRange (const char* var_name_begin,
1017 const char* var_name_end,
1018 const char* var_name_final,
1019 const char** open_bracket_position,
1020 const char** separator_position,
1021 const char** close_bracket_position,
1022 const char** var_name_final_if_array_range,
1023 int64_t* index_lower,
1024 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001025{
Enrico Granatae992a082011-07-22 17:03:19 +00001026 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001027 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001028 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001029 {
1030 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1031 *close_bracket_position = ::strchr(*open_bracket_position,']');
1032 // as usual, we assume that [] will come before %
1033 //printf("trying to expand a []\n");
1034 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001035 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001036 {
Enrico Granatae992a082011-07-22 17:03:19 +00001037 if (log)
1038 log->Printf("[] detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001039 *index_lower = 0;
1040 }
1041 else if (*separator_position == NULL || *separator_position > var_name_end)
1042 {
1043 char *end = NULL;
1044 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1045 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001046 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001047 log->Printf("[%lld] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001048 }
Greg Clayton34132752011-07-06 04:07:21 +00001049 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001050 {
1051 char *end = NULL;
1052 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1053 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001054 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001055 log->Printf("[%lld-%lld] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001056 }
1057 else
Enrico Granatae992a082011-07-22 17:03:19 +00001058 {
1059 if (log)
1060 log->Printf("expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001061 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001062 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001063 if (*index_lower > *index_higher && *index_higher > 0)
1064 {
Enrico Granatae992a082011-07-22 17:03:19 +00001065 if (log)
1066 log->Printf("swapping indices");
Enrico Granata9fc19442011-07-06 02:13:41 +00001067 int temp = *index_lower;
1068 *index_lower = *index_higher;
1069 *index_higher = temp;
1070 }
1071 }
Enrico Granatae992a082011-07-22 17:03:19 +00001072 else if (log)
1073 log->Printf("no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001074 return true;
1075}
1076
Enrico Granata9fc19442011-07-06 02:13:41 +00001077static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001078ExpandIndexedExpression (ValueObject* valobj,
1079 uint32_t index,
1080 StackFrame* frame,
1081 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001082{
Enrico Granatae992a082011-07-22 17:03:19 +00001083 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001084 const char* ptr_deref_format = "[%d]";
1085 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
1086 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001087 if (log)
1088 log->Printf("name to deref: %s",ptr_deref_buffer.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001089 const char* first_unparsed;
1090 ValueObject::GetValueForExpressionPathOptions options;
1091 ValueObject::ExpressionPathEndResultType final_value_type;
1092 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001093 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatac482a192011-08-17 22:13:59 +00001094 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001095 &first_unparsed,
1096 &reason_to_stop,
1097 &final_value_type,
1098 options,
1099 &what_next);
1100 if (!item)
1101 {
Enrico Granatae992a082011-07-22 17:03:19 +00001102 if (log)
1103 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1104 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001105 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001106 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001107 else
1108 {
Enrico Granatae992a082011-07-22 17:03:19 +00001109 if (log)
1110 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1111 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001112 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001113 }
1114 return item;
1115}
1116
Greg Clayton1b654882010-09-19 02:33:57 +00001117bool
1118Debugger::FormatPrompt
1119(
1120 const char *format,
1121 const SymbolContext *sc,
1122 const ExecutionContext *exe_ctx,
1123 const Address *addr,
1124 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001125 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001126 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001127)
1128{
Enrico Granatac482a192011-08-17 22:13:59 +00001129 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001130 bool success = true;
1131 const char *p;
Enrico Granatae992a082011-07-22 17:03:19 +00001132 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001133 for (p = format; *p != '\0'; ++p)
1134 {
Enrico Granatac482a192011-08-17 22:13:59 +00001135 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001136 {
Enrico Granatac482a192011-08-17 22:13:59 +00001137 valobj = realvalobj;
1138 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001139 }
Greg Clayton1b654882010-09-19 02:33:57 +00001140 size_t non_special_chars = ::strcspn (p, "${}\\");
1141 if (non_special_chars > 0)
1142 {
1143 if (success)
1144 s.Write (p, non_special_chars);
1145 p += non_special_chars;
1146 }
1147
1148 if (*p == '\0')
1149 {
1150 break;
1151 }
1152 else if (*p == '{')
1153 {
1154 // Start a new scope that must have everything it needs if it is to
1155 // to make it into the final output stream "s". If you want to make
1156 // a format that only prints out the function or symbol name if there
1157 // is one in the symbol context you can use:
1158 // "{function =${function.name}}"
1159 // The first '{' starts a new scope that end with the matching '}' at
1160 // the end of the string. The contents "function =${function.name}"
1161 // will then be evaluated and only be output if there is a function
1162 // or symbol with a valid name.
1163 StreamString sub_strm;
1164
1165 ++p; // Skip the '{'
1166
Enrico Granatac482a192011-08-17 22:13:59 +00001167 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001168 {
1169 // The stream had all it needed
1170 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1171 }
1172 if (*p != '}')
1173 {
1174 success = false;
1175 break;
1176 }
1177 }
1178 else if (*p == '}')
1179 {
1180 // End of a enclosing scope
1181 break;
1182 }
1183 else if (*p == '$')
1184 {
1185 // We have a prompt variable to print
1186 ++p;
1187 if (*p == '{')
1188 {
1189 ++p;
1190 const char *var_name_begin = p;
1191 const char *var_name_end = ::strchr (p, '}');
1192
1193 if (var_name_end && var_name_begin < var_name_end)
1194 {
1195 // if we have already failed to parse, skip this variable
1196 if (success)
1197 {
1198 const char *cstr = NULL;
1199 Address format_addr;
1200 bool calculate_format_addr_function_offset = false;
1201 // Set reg_kind and reg_num to invalid values
1202 RegisterKind reg_kind = kNumRegisterKinds;
1203 uint32_t reg_num = LLDB_INVALID_REGNUM;
1204 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001205 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001206 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001207 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001208 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1209 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001210
Greg Clayton1b654882010-09-19 02:33:57 +00001211 // Each variable must set success to true below...
1212 bool var_success = false;
1213 switch (var_name_begin[0])
1214 {
Enrico Granata4becb372011-06-29 22:27:15 +00001215 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001216 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001217 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001218 {
Enrico Granatac482a192011-08-17 22:13:59 +00001219 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001220 break;
1221
Enrico Granatac3e320a2011-08-02 17:27:39 +00001222 if (log)
1223 log->Printf("initial string: %s",var_name_begin);
1224
Enrico Granata6f3533f2011-07-29 19:53:35 +00001225 // check for *var and *svar
1226 if (*var_name_begin == '*')
1227 {
1228 do_deref_pointer = true;
1229 var_name_begin++;
1230 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001231
1232 if (log)
1233 log->Printf("initial string: %s",var_name_begin);
1234
Enrico Granata6f3533f2011-07-29 19:53:35 +00001235 if (*var_name_begin == 's')
1236 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001237 if (!valobj->IsSynthetic())
1238 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001239 if (!valobj)
1240 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001241 var_name_begin++;
1242 }
1243
Enrico Granatac3e320a2011-08-02 17:27:39 +00001244 if (log)
1245 log->Printf("initial string: %s",var_name_begin);
1246
Enrico Granata6f3533f2011-07-29 19:53:35 +00001247 // should be a 'v' by now
1248 if (*var_name_begin != 'v')
1249 break;
1250
Enrico Granatac3e320a2011-08-02 17:27:39 +00001251 if (log)
1252 log->Printf("initial string: %s",var_name_begin);
1253
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001254 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001255 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001256 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001257 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001258 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001259 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001260 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001261 const char* var_name_final = NULL;
1262 const char* var_name_final_if_array_range = NULL;
1263 const char* close_bracket_position = NULL;
1264 int64_t index_lower = -1;
1265 int64_t index_higher = -1;
1266 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001267 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001268 bool was_plain_var = false;
1269 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001270 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001271
Enrico Granatac482a192011-08-17 22:13:59 +00001272 if (!valobj) break;
1273 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001274 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001275 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001276 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001277 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001278 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001279 }
1280 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1281 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001282 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001283 // this is a variable with some custom format applied to it
1284 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001285 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001286 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001287 ScanFormatDescriptor (var_name_begin,
1288 var_name_end,
1289 &var_name_final,
1290 &percent_position,
1291 &custom_format,
1292 &val_obj_display);
1293 }
1294 // this is ${var.something} or multiple .something nested
1295 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1296 {
Enrico Granataa777dc22012-05-08 21:49:57 +00001297 if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1298 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001299 const char* percent_position;
1300 ScanFormatDescriptor (var_name_begin,
1301 var_name_end,
1302 &var_name_final,
1303 &percent_position,
1304 &custom_format,
1305 &val_obj_display);
1306
1307 const char* open_bracket_position;
1308 const char* separator_position;
1309 ScanBracketedRange (var_name_begin,
1310 var_name_end,
1311 var_name_final,
1312 &open_bracket_position,
1313 &separator_position,
1314 &close_bracket_position,
1315 &var_name_final_if_array_range,
1316 &index_lower,
1317 &index_higher);
1318
1319 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001320
1321 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1322 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1323 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1324
Enrico Granatae992a082011-07-22 17:03:19 +00001325 if (log)
1326 log->Printf("symbol to expand: %s",expr_path.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001327
Enrico Granatac482a192011-08-17 22:13:59 +00001328 target = valobj->GetValueForExpressionPath(expr_path.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001329 &first_unparsed,
1330 &reason_to_stop,
1331 &final_value_type,
1332 options,
1333 &what_next).get();
1334
1335 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001336 {
Enrico Granatae992a082011-07-22 17:03:19 +00001337 if (log)
1338 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1339 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001340 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001341 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001342 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001343 else
1344 {
Enrico Granatae992a082011-07-22 17:03:19 +00001345 if (log)
1346 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1347 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001348 first_unparsed, reason_to_stop, final_value_type);
1349 }
Enrico Granata4becb372011-06-29 22:27:15 +00001350 }
Greg Clayton34132752011-07-06 04:07:21 +00001351 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001352 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001353
Enrico Granata86cc9822012-03-19 22:58:49 +00001354 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1355 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001356
Enrico Granata86cc9822012-03-19 22:58:49 +00001357 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001358
Enrico Granataa7187d02011-07-06 19:27:11 +00001359 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001360 {
Greg Clayton34132752011-07-06 04:07:21 +00001361 // I have not deref-ed yet, let's do it
1362 // this happens when we are not going through GetValueForVariableExpressionPath
1363 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001364 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001365 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001366 if (error.Fail())
1367 {
1368 if (log)
1369 log->Printf("ERROR: %s\n", error.AsCString("unknown")); \
1370 break;
1371 }
Greg Clayton34132752011-07-06 04:07:21 +00001372 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001373 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001374
Enrico Granataa777dc22012-05-08 21:49:57 +00001375 // <rdar://problem/11338654>
1376 // we do not want to use the summary for a bitfield of type T:n
1377 // if we were originally dealing with just a T - that would get
1378 // us into an endless recursion
1379 if (target->IsBitfield() && was_var_indexed)
1380 {
1381 // TODO: check for a (T:n)-specific summary - we should still obey that
1382 StreamString bitfield_name;
1383 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1384 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1385 if (!DataVisualization::GetSummaryForType(type_sp))
1386 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1387 }
1388
Enrico Granata85933ed2011-08-18 16:38:26 +00001389 // TODO use flags for these
Enrico Granataf4efecd2011-07-12 22:56:10 +00001390 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1391 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001392 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001393
Enrico Granata86cc9822012-03-19 22:58:49 +00001394 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 +00001395 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001396 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001397 if (log)
1398 log->Printf("I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001399
Enrico Granata86cc9822012-03-19 22:58:49 +00001400 if (target->HasSpecialPrintableRepresentation(val_obj_display,
1401 custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001402 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001403 // try to use the special cases
1404 var_success = target->DumpPrintableRepresentation(str_temp,
1405 val_obj_display,
1406 custom_format);
1407 if (log)
1408 log->Printf("special cases did%s match", var_success ? "" : "n't");
1409
1410 // should not happen
1411 if (!var_success)
1412 s << "<invalid usage of pointer value as object>";
1413 else
1414 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001415 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001416 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001417 }
1418 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001419 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001420 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001421 {
1422 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1423 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001424 else if (is_pointer) // if pointer, value is the address stored
1425 {
Greg Clayton23f59502012-07-17 03:23:13 +00001426 target->DumpPrintableRepresentation (s,
1427 val_obj_display,
1428 custom_format,
1429 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001430 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001431 else
1432 {
1433 s << "<invalid usage of pointer value as object>";
1434 }
1435 var_success = true;
1436 break;
1437 }
1438 }
1439
1440 // if directly trying to print ${var}, and this is an aggregate, display a nice
1441 // type @ location message
1442 if (is_aggregate && was_plain_var)
1443 {
1444 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1445 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001446 break;
1447 }
1448
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001449 // 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 +00001450 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001451 {
1452 s << "<invalid use of aggregate type>";
1453 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001454 break;
1455 }
Greg Clayton34132752011-07-06 04:07:21 +00001456
1457 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001458 {
1459 if (log)
1460 log->Printf("dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001461 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001462 }
Greg Clayton34132752011-07-06 04:07:21 +00001463 else
Enrico Granatae992a082011-07-22 17:03:19 +00001464 {
1465 if (log)
1466 log->Printf("checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001467 if (!is_array && !is_pointer)
1468 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001469 if (log)
1470 log->Printf("handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001471 const char* special_directions = NULL;
1472 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001473 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1474 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001475 ConstString additional_data;
1476 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1477 special_directions_writer.Printf("${%svar%s}",
1478 do_deref_pointer ? "*" : "",
1479 additional_data.GetCString());
1480 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001481 }
1482
1483 // let us display items index_lower thru index_higher of this array
1484 s.PutChar('[');
1485 var_success = true;
1486
1487 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001488 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001489
Greg Claytoncc4d0142012-02-17 07:49:44 +00001490 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001491
Greg Clayton34132752011-07-06 04:07:21 +00001492 for (;index_lower<=index_higher;index_lower++)
1493 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001494 ValueObject* item = ExpandIndexedExpression (target,
1495 index_lower,
1496 exe_ctx->GetFramePtr(),
1497 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001498
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001499 if (!item)
1500 {
Enrico Granatae992a082011-07-22 17:03:19 +00001501 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001502 log->Printf("ERROR in getting child item at index %lld", index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001503 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001504 else
1505 {
Enrico Granatae992a082011-07-22 17:03:19 +00001506 if (log)
1507 log->Printf("special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001508 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001509
Greg Clayton34132752011-07-06 04:07:21 +00001510 if (!special_directions)
1511 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1512 else
1513 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1514
Enrico Granata22c55d12011-08-12 02:00:06 +00001515 if (--max_num_children == 0)
1516 {
1517 s.PutCString(", ...");
1518 break;
1519 }
1520
Greg Clayton34132752011-07-06 04:07:21 +00001521 if (index_lower < index_higher)
1522 s.PutChar(',');
1523 }
1524 s.PutChar(']');
1525 }
Enrico Granata4becb372011-06-29 22:27:15 +00001526 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001527 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001528 case 'a':
1529 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1530 {
1531 if (addr && addr->IsValid())
1532 {
1533 var_success = true;
1534 format_addr = *addr;
1535 }
1536 }
Greg Clayton5a314712011-10-14 07:41:33 +00001537 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1538 {
1539 var_success = true;
1540 var_name_begin += strlen("ansi."); // Skip the "ansi."
1541 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1542 {
1543 var_name_begin += strlen("fg."); // Skip the "fg."
1544 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1545 {
1546 s.Printf ("%s%s%s",
1547 lldb_utility::ansi::k_escape_start,
1548 lldb_utility::ansi::k_fg_black,
1549 lldb_utility::ansi::k_escape_end);
1550 }
1551 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1552 {
1553 s.Printf ("%s%s%s",
1554 lldb_utility::ansi::k_escape_start,
1555 lldb_utility::ansi::k_fg_red,
1556 lldb_utility::ansi::k_escape_end);
1557 }
1558 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1559 {
1560 s.Printf ("%s%s%s",
1561 lldb_utility::ansi::k_escape_start,
1562 lldb_utility::ansi::k_fg_green,
1563 lldb_utility::ansi::k_escape_end);
1564 }
1565 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1566 {
1567 s.Printf ("%s%s%s",
1568 lldb_utility::ansi::k_escape_start,
1569 lldb_utility::ansi::k_fg_yellow,
1570 lldb_utility::ansi::k_escape_end);
1571 }
1572 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1573 {
1574 s.Printf ("%s%s%s",
1575 lldb_utility::ansi::k_escape_start,
1576 lldb_utility::ansi::k_fg_blue,
1577 lldb_utility::ansi::k_escape_end);
1578 }
1579 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1580 {
1581 s.Printf ("%s%s%s",
1582 lldb_utility::ansi::k_escape_start,
1583 lldb_utility::ansi::k_fg_purple,
1584 lldb_utility::ansi::k_escape_end);
1585 }
1586 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1587 {
1588 s.Printf ("%s%s%s",
1589 lldb_utility::ansi::k_escape_start,
1590 lldb_utility::ansi::k_fg_cyan,
1591 lldb_utility::ansi::k_escape_end);
1592 }
1593 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1594 {
1595 s.Printf ("%s%s%s",
1596 lldb_utility::ansi::k_escape_start,
1597 lldb_utility::ansi::k_fg_white,
1598 lldb_utility::ansi::k_escape_end);
1599 }
1600 else
1601 {
1602 var_success = false;
1603 }
1604 }
1605 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1606 {
1607 var_name_begin += strlen("bg."); // Skip the "bg."
1608 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1609 {
1610 s.Printf ("%s%s%s",
1611 lldb_utility::ansi::k_escape_start,
1612 lldb_utility::ansi::k_bg_black,
1613 lldb_utility::ansi::k_escape_end);
1614 }
1615 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1616 {
1617 s.Printf ("%s%s%s",
1618 lldb_utility::ansi::k_escape_start,
1619 lldb_utility::ansi::k_bg_red,
1620 lldb_utility::ansi::k_escape_end);
1621 }
1622 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1623 {
1624 s.Printf ("%s%s%s",
1625 lldb_utility::ansi::k_escape_start,
1626 lldb_utility::ansi::k_bg_green,
1627 lldb_utility::ansi::k_escape_end);
1628 }
1629 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1630 {
1631 s.Printf ("%s%s%s",
1632 lldb_utility::ansi::k_escape_start,
1633 lldb_utility::ansi::k_bg_yellow,
1634 lldb_utility::ansi::k_escape_end);
1635 }
1636 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1637 {
1638 s.Printf ("%s%s%s",
1639 lldb_utility::ansi::k_escape_start,
1640 lldb_utility::ansi::k_bg_blue,
1641 lldb_utility::ansi::k_escape_end);
1642 }
1643 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1644 {
1645 s.Printf ("%s%s%s",
1646 lldb_utility::ansi::k_escape_start,
1647 lldb_utility::ansi::k_bg_purple,
1648 lldb_utility::ansi::k_escape_end);
1649 }
1650 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1651 {
1652 s.Printf ("%s%s%s",
1653 lldb_utility::ansi::k_escape_start,
1654 lldb_utility::ansi::k_bg_cyan,
1655 lldb_utility::ansi::k_escape_end);
1656 }
1657 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1658 {
1659 s.Printf ("%s%s%s",
1660 lldb_utility::ansi::k_escape_start,
1661 lldb_utility::ansi::k_bg_white,
1662 lldb_utility::ansi::k_escape_end);
1663 }
1664 else
1665 {
1666 var_success = false;
1667 }
1668 }
1669 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1670 {
1671 s.Printf ("%s%s%s",
1672 lldb_utility::ansi::k_escape_start,
1673 lldb_utility::ansi::k_ctrl_normal,
1674 lldb_utility::ansi::k_escape_end);
1675 }
1676 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1677 {
1678 s.Printf ("%s%s%s",
1679 lldb_utility::ansi::k_escape_start,
1680 lldb_utility::ansi::k_ctrl_bold,
1681 lldb_utility::ansi::k_escape_end);
1682 }
1683 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1684 {
1685 s.Printf ("%s%s%s",
1686 lldb_utility::ansi::k_escape_start,
1687 lldb_utility::ansi::k_ctrl_faint,
1688 lldb_utility::ansi::k_escape_end);
1689 }
1690 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1691 {
1692 s.Printf ("%s%s%s",
1693 lldb_utility::ansi::k_escape_start,
1694 lldb_utility::ansi::k_ctrl_italic,
1695 lldb_utility::ansi::k_escape_end);
1696 }
1697 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1698 {
1699 s.Printf ("%s%s%s",
1700 lldb_utility::ansi::k_escape_start,
1701 lldb_utility::ansi::k_ctrl_underline,
1702 lldb_utility::ansi::k_escape_end);
1703 }
1704 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1705 {
1706 s.Printf ("%s%s%s",
1707 lldb_utility::ansi::k_escape_start,
1708 lldb_utility::ansi::k_ctrl_slow_blink,
1709 lldb_utility::ansi::k_escape_end);
1710 }
1711 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1712 {
1713 s.Printf ("%s%s%s",
1714 lldb_utility::ansi::k_escape_start,
1715 lldb_utility::ansi::k_ctrl_fast_blink,
1716 lldb_utility::ansi::k_escape_end);
1717 }
1718 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1719 {
1720 s.Printf ("%s%s%s",
1721 lldb_utility::ansi::k_escape_start,
1722 lldb_utility::ansi::k_ctrl_negative,
1723 lldb_utility::ansi::k_escape_end);
1724 }
1725 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1726 {
1727 s.Printf ("%s%s%s",
1728 lldb_utility::ansi::k_escape_start,
1729 lldb_utility::ansi::k_ctrl_conceal,
1730 lldb_utility::ansi::k_escape_end);
1731
1732 }
1733 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1734 {
1735 s.Printf ("%s%s%s",
1736 lldb_utility::ansi::k_escape_start,
1737 lldb_utility::ansi::k_ctrl_crossed_out,
1738 lldb_utility::ansi::k_escape_end);
1739 }
1740 else
1741 {
1742 var_success = false;
1743 }
1744 }
Greg Clayton1b654882010-09-19 02:33:57 +00001745 break;
1746
1747 case 'p':
1748 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1749 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001750 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001751 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001752 Process *process = exe_ctx->GetProcessPtr();
1753 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001754 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001755 var_name_begin += ::strlen ("process.");
1756 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001757 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001758 s.Printf("%llu", process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001759 var_success = true;
1760 }
1761 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1762 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1763 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1764 {
1765 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1766 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001767 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001768 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1769 {
1770 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1771 var_success = format_file_spec;
1772 }
1773 else
1774 {
1775 format_file_spec = exe_module->GetFileSpec();
1776 var_success = format_file_spec;
1777 }
Greg Clayton1b654882010-09-19 02:33:57 +00001778 }
1779 }
1780 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001781 }
Greg Clayton1b654882010-09-19 02:33:57 +00001782 }
1783 break;
1784
1785 case 't':
1786 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1787 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001788 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001789 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001790 Thread *thread = exe_ctx->GetThreadPtr();
1791 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001792 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001793 var_name_begin += ::strlen ("thread.");
1794 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001795 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001796 s.Printf("0x%4.4llx", thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001797 var_success = true;
1798 }
1799 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1800 {
1801 s.Printf("%u", thread->GetIndexID());
1802 var_success = true;
1803 }
1804 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1805 {
1806 cstr = thread->GetName();
1807 var_success = cstr && cstr[0];
1808 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001809 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001810 }
1811 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1812 {
1813 cstr = thread->GetQueueName();
1814 var_success = cstr && cstr[0];
1815 if (var_success)
1816 s.PutCString(cstr);
1817 }
1818 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1819 {
1820 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1821 if (stop_info_sp)
1822 {
1823 cstr = stop_info_sp->GetDescription();
1824 if (cstr && cstr[0])
1825 {
1826 s.PutCString(cstr);
1827 var_success = true;
1828 }
Greg Clayton1b654882010-09-19 02:33:57 +00001829 }
1830 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001831 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1832 {
1833 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1834 if (stop_info_sp)
1835 {
1836 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1837 if (return_valobj_sp)
1838 {
Jim Inghamef651602011-12-22 19:12:40 +00001839 ValueObject::DumpValueObjectOptions dump_options;
1840 ValueObject::DumpValueObject (s, return_valobj_sp.get(), dump_options);
1841 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001842 }
1843 }
1844 }
Greg Clayton1b654882010-09-19 02:33:57 +00001845 }
1846 }
1847 }
1848 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1849 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001850 // TODO: hookup properties
1851// if (!target_properties_sp)
1852// {
1853// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1854// if (target)
1855// target_properties_sp = target->GetProperties();
1856// }
1857//
1858// if (target_properties_sp)
1859// {
1860// var_name_begin += ::strlen ("target.");
1861// const char *end_property = strchr(var_name_begin, '}');
1862// if (end_property)
1863// {
1864// ConstString property_name(var_name_begin, end_property - var_name_begin);
1865// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
1866// if (!property_value.empty())
1867// {
1868// s.PutCString (property_value.c_str());
1869// var_success = true;
1870// }
1871// }
1872// }
Greg Clayton0603aa92010-10-04 01:05:56 +00001873 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1874 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001875 {
Greg Clayton1b654882010-09-19 02:33:57 +00001876 var_name_begin += ::strlen ("target.");
1877 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1878 {
1879 ArchSpec arch (target->GetArchitecture ());
1880 if (arch.IsValid())
1881 {
Greg Clayton64195a22011-02-23 00:35:02 +00001882 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001883 var_success = true;
1884 }
1885 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001886 }
Greg Clayton1b654882010-09-19 02:33:57 +00001887 }
1888 break;
1889
1890
1891 case 'm':
1892 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1893 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001894 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001895 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001896 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001897 var_name_begin += ::strlen ("module.");
1898
1899 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1900 {
1901 if (module->GetFileSpec())
1902 {
1903 var_name_begin += ::strlen ("file.");
1904
1905 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1906 {
1907 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1908 var_success = format_file_spec;
1909 }
1910 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1911 {
1912 format_file_spec = module->GetFileSpec();
1913 var_success = format_file_spec;
1914 }
1915 }
1916 }
1917 }
1918 }
1919 break;
1920
1921
1922 case 'f':
1923 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1924 {
1925 if (sc && sc->comp_unit != NULL)
1926 {
1927 var_name_begin += ::strlen ("file.");
1928
1929 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1930 {
1931 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1932 var_success = format_file_spec;
1933 }
1934 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1935 {
1936 format_file_spec = *sc->comp_unit;
1937 var_success = format_file_spec;
1938 }
1939 }
1940 }
1941 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1942 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001943 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001944 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001945 StackFrame *frame = exe_ctx->GetFramePtr();
1946 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001947 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001948 var_name_begin += ::strlen ("frame.");
1949 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001950 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001951 s.Printf("%u", frame->GetFrameIndex());
1952 var_success = true;
1953 }
1954 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1955 {
1956 reg_kind = eRegisterKindGeneric;
1957 reg_num = LLDB_REGNUM_GENERIC_PC;
1958 var_success = true;
1959 }
1960 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1961 {
1962 reg_kind = eRegisterKindGeneric;
1963 reg_num = LLDB_REGNUM_GENERIC_SP;
1964 var_success = true;
1965 }
1966 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1967 {
1968 reg_kind = eRegisterKindGeneric;
1969 reg_num = LLDB_REGNUM_GENERIC_FP;
1970 var_success = true;
1971 }
1972 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
1973 {
1974 reg_kind = eRegisterKindGeneric;
1975 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
1976 var_success = true;
1977 }
1978 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
1979 {
1980 reg_ctx = frame->GetRegisterContext().get();
1981 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001982 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001983 var_name_begin += ::strlen ("reg.");
1984 if (var_name_begin < var_name_end)
1985 {
1986 std::string reg_name (var_name_begin, var_name_end);
1987 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
1988 if (reg_info)
1989 var_success = true;
1990 }
Greg Clayton1b654882010-09-19 02:33:57 +00001991 }
1992 }
1993 }
1994 }
1995 }
1996 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
1997 {
1998 if (sc && (sc->function != NULL || sc->symbol != NULL))
1999 {
2000 var_name_begin += ::strlen ("function.");
2001 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2002 {
2003 if (sc->function)
Greg Clayton81c22f62011-10-19 18:09:39 +00002004 s.Printf("function{0x%8.8llx}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002005 else
2006 s.Printf("symbol[%u]", sc->symbol->GetID());
2007
2008 var_success = true;
2009 }
2010 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2011 {
2012 if (sc->function)
2013 cstr = sc->function->GetName().AsCString (NULL);
2014 else if (sc->symbol)
2015 cstr = sc->symbol->GetName().AsCString (NULL);
2016 if (cstr)
2017 {
2018 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002019
2020 if (sc->block)
2021 {
2022 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2023 if (inline_block)
2024 {
2025 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2026 if (inline_info)
2027 {
2028 s.PutCString(" [inlined] ");
2029 inline_info->GetName().Dump(&s);
2030 }
2031 }
2032 }
Greg Clayton1b654882010-09-19 02:33:57 +00002033 var_success = true;
2034 }
2035 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002036 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2037 {
2038 // Print the function name with arguments in it
2039
2040 if (sc->function)
2041 {
2042 var_success = true;
2043 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2044 cstr = sc->function->GetName().AsCString (NULL);
2045 if (cstr)
2046 {
2047 const InlineFunctionInfo *inline_info = NULL;
2048 VariableListSP variable_list_sp;
2049 bool get_function_vars = true;
2050 if (sc->block)
2051 {
2052 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2053
2054 if (inline_block)
2055 {
2056 get_function_vars = false;
2057 inline_info = sc->block->GetInlinedFunctionInfo();
2058 if (inline_info)
2059 variable_list_sp = inline_block->GetBlockVariableList (true);
2060 }
2061 }
2062
2063 if (get_function_vars)
2064 {
2065 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2066 }
2067
2068 if (inline_info)
2069 {
2070 s.PutCString (cstr);
2071 s.PutCString (" [inlined] ");
2072 cstr = inline_info->GetName().GetCString();
2073 }
2074
2075 VariableList args;
2076 if (variable_list_sp)
2077 {
2078 const size_t num_variables = variable_list_sp->GetSize();
2079 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
2080 {
2081 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
2082 if (var_sp->GetScope() == eValueTypeVariableArgument)
2083 args.AddVariable (var_sp);
2084 }
2085
2086 }
2087 if (args.GetSize() > 0)
2088 {
2089 const char *open_paren = strchr (cstr, '(');
2090 const char *close_paren = NULL;
2091 if (open_paren)
2092 close_paren = strchr (open_paren, ')');
2093
2094 if (open_paren)
2095 s.Write(cstr, open_paren - cstr + 1);
2096 else
2097 {
2098 s.PutCString (cstr);
2099 s.PutChar ('(');
2100 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002101 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002102 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2103 {
2104 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2105 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2106 const char *var_name = var_value_sp->GetName().GetCString();
2107 const char *var_value = var_value_sp->GetValueAsCString();
2108 if (var_value_sp->GetError().Success())
2109 {
2110 if (arg_idx > 0)
2111 s.PutCString (", ");
2112 s.Printf ("%s=%s", var_name, var_value);
2113 }
2114 }
2115
2116 if (close_paren)
2117 s.PutCString (close_paren);
2118 else
2119 s.PutChar(')');
2120
2121 }
2122 else
2123 {
2124 s.PutCString(cstr);
2125 }
2126 }
2127 }
2128 else if (sc->symbol)
2129 {
2130 cstr = sc->symbol->GetName().AsCString (NULL);
2131 if (cstr)
2132 {
2133 s.PutCString(cstr);
2134 var_success = true;
2135 }
2136 }
2137 }
Greg Clayton1b654882010-09-19 02:33:57 +00002138 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2139 {
2140 var_success = addr != NULL;
2141 if (var_success)
2142 {
2143 format_addr = *addr;
2144 calculate_format_addr_function_offset = true;
2145 }
2146 }
2147 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2148 {
2149 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2150 if (var_success)
2151 {
2152 format_addr = sc->line_entry.range.GetBaseAddress();
2153 calculate_format_addr_function_offset = true;
2154 }
2155 }
2156 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2157 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002158 StackFrame *frame = exe_ctx->GetFramePtr();
2159 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002160 if (var_success)
2161 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002162 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002163 calculate_format_addr_function_offset = true;
2164 }
2165 }
2166 }
2167 }
2168 break;
2169
2170 case 'l':
2171 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2172 {
2173 if (sc && sc->line_entry.IsValid())
2174 {
2175 var_name_begin += ::strlen ("line.");
2176 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2177 {
2178 var_name_begin += ::strlen ("file.");
2179
2180 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2181 {
2182 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2183 var_success = format_file_spec;
2184 }
2185 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2186 {
2187 format_file_spec = sc->line_entry.file;
2188 var_success = format_file_spec;
2189 }
2190 }
2191 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2192 {
2193 var_success = true;
2194 s.Printf("%u", sc->line_entry.line);
2195 }
2196 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2197 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2198 {
2199 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2200 if (var_success)
2201 {
2202 format_addr = sc->line_entry.range.GetBaseAddress();
2203 if (var_name_begin[0] == 'e')
2204 format_addr.Slide (sc->line_entry.range.GetByteSize());
2205 }
2206 }
2207 }
2208 }
2209 break;
2210 }
2211
2212 if (var_success)
2213 {
2214 // If format addr is valid, then we need to print an address
2215 if (reg_num != LLDB_INVALID_REGNUM)
2216 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002217 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002218 // We have a register value to display...
2219 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2220 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002221 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002222 }
2223 else
2224 {
2225 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002226 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002227
2228 if (reg_ctx)
2229 {
2230 if (reg_kind != kNumRegisterKinds)
2231 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2232 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2233 var_success = reg_info != NULL;
2234 }
2235 }
2236 }
2237
2238 if (reg_info != NULL)
2239 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002240 RegisterValue reg_value;
2241 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2242 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002243 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002244 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002245 }
2246 }
2247
2248 if (format_file_spec)
2249 {
2250 s << format_file_spec;
2251 }
2252
2253 // If format addr is valid, then we need to print an address
2254 if (format_addr.IsValid())
2255 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002256 var_success = false;
2257
Greg Clayton1b654882010-09-19 02:33:57 +00002258 if (calculate_format_addr_function_offset)
2259 {
2260 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002261
Greg Clayton0603aa92010-10-04 01:05:56 +00002262 if (sc)
2263 {
2264 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002265 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002266 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002267 if (sc->block)
2268 {
2269 // Check to make sure we aren't in an inline
2270 // function. If we are, use the inline block
2271 // range that contains "format_addr" since
2272 // blocks can be discontiguous.
2273 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2274 AddressRange inline_range;
2275 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2276 func_addr = inline_range.GetBaseAddress();
2277 }
2278 }
Greg Claytone7612132012-03-07 21:03:09 +00002279 else if (sc->symbol && sc->symbol->ValueIsAddress())
2280 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002281 }
2282
2283 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002284 {
2285 if (func_addr.GetSection() == format_addr.GetSection())
2286 {
2287 addr_t func_file_addr = func_addr.GetFileAddress();
2288 addr_t addr_file_addr = format_addr.GetFileAddress();
2289 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002290 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002291 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002292 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002293 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002294 }
2295 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002296 {
2297 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2298 if (target)
2299 {
2300 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2301 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2302 if (addr_load_addr > func_load_addr)
2303 s.Printf(" + %llu", addr_load_addr - func_load_addr);
2304 else if (addr_load_addr < func_load_addr)
2305 s.Printf(" - %llu", func_load_addr - addr_load_addr);
2306 var_success = true;
2307 }
2308 }
Greg Clayton1b654882010-09-19 02:33:57 +00002309 }
2310 }
2311 else
2312 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002313 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002314 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002315 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2316 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002317 if (vaddr == LLDB_INVALID_ADDRESS)
2318 vaddr = format_addr.GetFileAddress ();
2319
2320 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002321 {
Greg Clayton514487e2011-02-15 21:59:32 +00002322 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002323 if (addr_width == 0)
2324 addr_width = 16;
2325 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002326 var_success = true;
2327 }
Greg Clayton1b654882010-09-19 02:33:57 +00002328 }
2329 }
2330 }
2331
2332 if (var_success == false)
2333 success = false;
2334 }
2335 p = var_name_end;
2336 }
2337 else
2338 break;
2339 }
2340 else
2341 {
2342 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2343 s.PutChar(*p);
2344 }
2345 }
2346 else if (*p == '\\')
2347 {
2348 ++p; // skip the slash
2349 switch (*p)
2350 {
2351 case 'a': s.PutChar ('\a'); break;
2352 case 'b': s.PutChar ('\b'); break;
2353 case 'f': s.PutChar ('\f'); break;
2354 case 'n': s.PutChar ('\n'); break;
2355 case 'r': s.PutChar ('\r'); break;
2356 case 't': s.PutChar ('\t'); break;
2357 case 'v': s.PutChar ('\v'); break;
2358 case '\'': s.PutChar ('\''); break;
2359 case '\\': s.PutChar ('\\'); break;
2360 case '0':
2361 // 1 to 3 octal chars
2362 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002363 // Make a string that can hold onto the initial zero char,
2364 // up to 3 octal digits, and a terminating NULL.
2365 char oct_str[5] = { 0, 0, 0, 0, 0 };
2366
2367 int i;
2368 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2369 oct_str[i] = p[i];
2370
2371 // We don't want to consume the last octal character since
2372 // the main for loop will do this for us, so we advance p by
2373 // one less than i (even if i is zero)
2374 p += i - 1;
2375 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2376 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002377 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002378 char octal_char = octal_value;
2379 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00002380 }
Greg Clayton1b654882010-09-19 02:33:57 +00002381 }
2382 break;
2383
2384 case 'x':
2385 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002386 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002387 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002388 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002389
Greg Clayton0603aa92010-10-04 01:05:56 +00002390 // Make a string that can hold onto two hex chars plus a
2391 // NULL terminator
2392 char hex_str[3] = { 0,0,0 };
2393 hex_str[0] = *p;
2394 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002395 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002396 ++p; // Skip the first of the two hex chars
2397 hex_str[1] = *p;
2398 }
2399
2400 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2401 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002402 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002403 }
2404 else
2405 {
2406 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002407 }
2408 break;
2409
2410 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002411 // Just desensitize any other character by just printing what
2412 // came after the '\'
2413 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002414 break;
2415
2416 }
2417
2418 }
2419 }
2420 if (end)
2421 *end = p;
2422 return success;
2423}
2424
Jim Ingham228063c2012-02-21 02:23:08 +00002425void
2426Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2427{
Jim Ingham4f02b222012-02-22 22:49:20 +00002428 // For simplicity's sake, I am not going to deal with how to close down any
2429 // open logging streams, I just redirect everything from here on out to the
2430 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002431 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2432}
2433
2434bool
2435Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2436{
2437 Log::Callbacks log_callbacks;
2438
2439 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002440 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002441 {
2442 log_stream_sp = m_log_callback_stream_sp;
2443 // For now when using the callback mode you always get thread & timestamp.
2444 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2445 }
2446 else if (log_file == NULL || *log_file == '\0')
2447 {
2448 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2449 }
2450 else
2451 {
2452 LogStreamMap::iterator pos = m_log_streams.find(log_file);
2453 if (pos == m_log_streams.end())
2454 {
2455 log_stream_sp.reset (new StreamFile (log_file));
2456 m_log_streams[log_file] = log_stream_sp;
2457 }
2458 else
2459 log_stream_sp = pos->second;
2460 }
2461 assert (log_stream_sp.get());
2462
2463 if (log_options == 0)
2464 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2465
2466 if (Log::GetLogChannelCallbacks (channel, log_callbacks))
2467 {
2468 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2469 return true;
2470 }
2471 else
2472 {
2473 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2474 if (log_channel_sp)
2475 {
2476 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2477 {
2478 return true;
2479 }
2480 else
2481 {
2482 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2483 return false;
2484 }
2485 }
2486 else
2487 {
2488 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2489 return false;
2490 }
2491 }
2492 return false;
2493}
2494