blob: 86b0cf53e3c41f691459e5a04281ec30d52316ae [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
Enrico Granata21dfcd92012-09-28 23:57:51 +000010#include "lldb/API/SBDebugger.h"
11
Greg Clayton4a33d312011-06-23 17:59:56 +000012#include "lldb/Core/Debugger.h"
13
14#include <map>
15
Enrico Granata4becb372011-06-29 22:27:15 +000016#include "clang/AST/DeclCXX.h"
17#include "clang/AST/Type.h"
18
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/lldb-private.h"
20#include "lldb/Core/ConnectionFileDescriptor.h"
Enrico Granataa777dc22012-05-08 21:49:57 +000021#include "lldb/Core/DataVisualization.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000022#include "lldb/Core/FormatManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/InputReader.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Core/Module.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000025#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000027#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000028#include "lldb/Core/StreamCallback.h"
Greg Clayton1b654882010-09-19 02:33:57 +000029#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000031#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000032#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000033#include "lldb/Host/DynamicLibrary.h"
Greg Claytona3406612011-02-07 23:24:47 +000034#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000035#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000036#include "lldb/Interpreter/OptionValueSInt64.h"
37#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000038#include "lldb/Symbol/ClangASTContext.h"
39#include "lldb/Symbol/CompileUnit.h"
40#include "lldb/Symbol/Function.h"
41#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000042#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Target/TargetList.h"
44#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000045#include "lldb/Target/RegisterContext.h"
46#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000048#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
50using namespace lldb;
51using namespace lldb_private;
52
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
Greg Clayton1b654882010-09-19 02:33:57 +000054static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000055static lldb::user_id_t g_unique_id = 1;
56
Greg Clayton1b654882010-09-19 02:33:57 +000057#pragma mark Static Functions
58
59static Mutex &
60GetDebuggerListMutex ()
61{
62 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
63 return g_mutex;
64}
65
66typedef std::vector<DebuggerSP> DebuggerList;
67
68static DebuggerList &
69GetDebuggerList()
70{
71 // hide the static debugger list inside a singleton accessor to avoid
72 // global init contructors
73 static DebuggerList g_list;
74 return g_list;
75}
Greg Claytone372b982011-11-21 21:44:34 +000076
77OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000078g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000079{
Greg Clayton67cc0632012-08-22 17:17:09 +000080 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
81 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
82 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000083 { 0, NULL, NULL }
84};
85
Greg Clayton67cc0632012-08-22 17:17:09 +000086OptionEnumValueElement
87g_language_enumerators[] =
88{
89 { eScriptLanguageNone, "none", "Disable scripting languages."},
90 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
91 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000092 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000093};
Greg Claytone372b982011-11-21 21:44:34 +000094
Greg Clayton67cc0632012-08-22 17:17:09 +000095#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
96#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
97
98#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
99 "{, ${frame.pc}}"\
100 MODULE_WITH_FUNC\
101 FILE_AND_LINE\
102 "{, stop reason = ${thread.stop-reason}}"\
103 "{\\nReturn value: ${thread.return-value}}"\
104 "\\n"
105
106#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
107 MODULE_WITH_FUNC\
108 FILE_AND_LINE\
109 "\\n"
110
111
112
Greg Clayton754a9362012-08-23 00:22:02 +0000113static PropertyDefinition
114g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000115{
116{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
117{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
118{ "notify-void", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
Greg Clayton4c054102012-09-01 00:38:36 +0000119{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000120{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
121{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
122{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
123{ "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." },
124{ "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." },
125{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
126{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
127{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
128{ NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
129};
130
131enum
132{
133 ePropertyAutoConfirm = 0,
134 ePropertyFrameFormat,
135 ePropertyNotiftVoid,
136 ePropertyPrompt,
137 ePropertyScriptLanguage,
138 ePropertyStopDisassemblyCount,
139 ePropertyStopDisassemblyDisplay,
140 ePropertyStopLineCountAfter,
141 ePropertyStopLineCountBefore,
142 ePropertyTerminalWidth,
143 ePropertyThreadFormat,
144 ePropertyUseExternalEditor
145};
146
147//
148//const char *
149//Debugger::GetFrameFormat() const
150//{
151// return m_properties_sp->GetFrameFormat();
152//}
153//const char *
154//Debugger::GetThreadFormat() const
155//{
156// return m_properties_sp->GetThreadFormat();
157//}
158//
Greg Clayton4c054102012-09-01 00:38:36 +0000159
160
161Error
162Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
163 VarSetOperationType op,
164 const char *property_path,
165 const char *value)
166{
167 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
168 if (error.Success())
169 {
170 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
171 {
172 const char *new_prompt = GetPrompt();
173 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
174 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
175 }
176 }
177 return error;
178}
179
Greg Clayton67cc0632012-08-22 17:17:09 +0000180bool
181Debugger::GetAutoConfirm () const
182{
183 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000184 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000185}
186
187const char *
188Debugger::GetFrameFormat() const
189{
190 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000191 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000192}
193
194bool
195Debugger::GetNotifyVoid () const
196{
197 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000198 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000199}
200
201const char *
202Debugger::GetPrompt() const
203{
204 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000205 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000206}
207
208void
209Debugger::SetPrompt(const char *p)
210{
211 const uint32_t idx = ePropertyPrompt;
212 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
213 const char *new_prompt = GetPrompt();
214 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
215 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
216}
217
218const char *
219Debugger::GetThreadFormat() const
220{
221 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000222 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000223}
224
225lldb::ScriptLanguage
226Debugger::GetScriptLanguage() const
227{
228 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000229 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000230}
231
232bool
233Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
234{
235 const uint32_t idx = ePropertyScriptLanguage;
236 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
237}
238
239uint32_t
240Debugger::GetTerminalWidth () const
241{
242 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000243 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000244}
245
246bool
247Debugger::SetTerminalWidth (uint32_t term_width)
248{
249 const uint32_t idx = ePropertyTerminalWidth;
250 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
251}
252
253bool
254Debugger::GetUseExternalEditor () const
255{
256 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000257 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000258}
259
260bool
261Debugger::SetUseExternalEditor (bool b)
262{
263 const uint32_t idx = ePropertyUseExternalEditor;
264 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
265}
266
267uint32_t
268Debugger::GetStopSourceLineCount (bool before) const
269{
270 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000271 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000272}
273
274Debugger::StopDisassemblyType
275Debugger::GetStopDisassemblyDisplay () const
276{
277 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000278 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000279}
280
281uint32_t
282Debugger::GetDisassemblyLineCount () const
283{
284 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000285 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000286}
Greg Claytone372b982011-11-21 21:44:34 +0000287
Greg Clayton1b654882010-09-19 02:33:57 +0000288#pragma mark Debugger
289
Greg Clayton67cc0632012-08-22 17:17:09 +0000290//const DebuggerPropertiesSP &
291//Debugger::GetSettings() const
292//{
293// return m_properties_sp;
294//}
295//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000296
Caroline Tice2f88aad2011-01-14 00:29:16 +0000297int
298Debugger::TestDebuggerRefCount ()
299{
300 return g_shared_debugger_refcount;
301}
302
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303void
304Debugger::Initialize ()
305{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000306 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000307 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308}
309
310void
311Debugger::Terminate ()
312{
Greg Clayton66111032010-06-23 01:19:29 +0000313 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314 {
Greg Clayton66111032010-06-23 01:19:29 +0000315 g_shared_debugger_refcount--;
316 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 {
Greg Claytondbe54502010-11-19 03:46:01 +0000318 lldb_private::WillTerminate();
319 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000320
321 // Clear our master list of debugger objects
322 Mutex::Locker locker (GetDebuggerListMutex ());
323 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 }
326}
327
Caroline Tice20bd37f2011-03-10 22:14:10 +0000328void
329Debugger::SettingsInitialize ()
330{
Greg Clayton6920b522012-08-22 18:39:03 +0000331 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000332}
333
334void
335Debugger::SettingsTerminate ()
336{
Greg Clayton6920b522012-08-22 18:39:03 +0000337 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000338}
339
Enrico Granata21dfcd92012-09-28 23:57:51 +0000340bool
341Debugger::LoadPlugin (const FileSpec& spec)
342{
343 lldb::DynamicLibrarySP dynlib_sp(new lldb_private::DynamicLibrary(spec));
344 lldb::DebuggerSP debugger_sp(shared_from_this());
345 lldb::SBDebugger debugger_sb(debugger_sp);
346 // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
347 LLDBCommandPluginInit init_func = dynlib_sp->GetSymbol<LLDBCommandPluginInit>("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
348 if (!init_func)
349 return false;
350 if (init_func(debugger_sb))
351 {
352 m_loaded_plugins.push_back(dynlib_sp);
353 return true;
354 }
355 return false;
356}
357
358static FileSpec::EnumerateDirectoryResult
359LoadPluginCallback
360(
361 void *baton,
362 FileSpec::FileType file_type,
363 const FileSpec &file_spec
364 )
365{
366 Error error;
367
368 static ConstString g_dylibext("dylib");
369
370 if (!baton)
371 return FileSpec::eEnumerateDirectoryResultQuit;
372
373 Debugger *debugger = (Debugger*)baton;
374
375 // If we have a regular file, a symbolic link or unknown file type, try
376 // and process the file. We must handle unknown as sometimes the directory
377 // enumeration might be enumerating a file system that doesn't have correct
378 // file type information.
379 if (file_type == FileSpec::eFileTypeRegular ||
380 file_type == FileSpec::eFileTypeSymbolicLink ||
381 file_type == FileSpec::eFileTypeUnknown )
382 {
383 FileSpec plugin_file_spec (file_spec);
384 plugin_file_spec.ResolvePath ();
385
386 if (plugin_file_spec.GetFileNameExtension() != g_dylibext)
387 return FileSpec::eEnumerateDirectoryResultNext;
388
389 debugger->LoadPlugin (plugin_file_spec);
390
391 return FileSpec::eEnumerateDirectoryResultNext;
392 }
393
394 else if (file_type == FileSpec::eFileTypeUnknown ||
395 file_type == FileSpec::eFileTypeDirectory ||
396 file_type == FileSpec::eFileTypeSymbolicLink )
397 {
398 // Try and recurse into anything that a directory or symbolic link.
399 // We must also do this for unknown as sometimes the directory enumeration
400 // might be enurating a file system that doesn't have correct file type
401 // information.
402 return FileSpec::eEnumerateDirectoryResultEnter;
403 }
404
405 return FileSpec::eEnumerateDirectoryResultNext;
406}
407
408void
409Debugger::InstanceInitialize ()
410{
411 FileSpec dir_spec;
412 const bool find_directories = true;
413 const bool find_files = true;
414 const bool find_other = true;
415 char dir_path[PATH_MAX];
416 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
417 {
418 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
419 {
420 FileSpec::EnumerateDirectory (dir_path,
421 find_directories,
422 find_files,
423 find_other,
424 LoadPluginCallback,
425 this);
426 }
427 }
428
429 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
430 {
431 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
432 {
433 FileSpec::EnumerateDirectory (dir_path,
434 find_directories,
435 find_files,
436 find_other,
437 LoadPluginCallback,
438 this);
439 }
440 }
441}
442
Greg Clayton66111032010-06-23 01:19:29 +0000443DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000444Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000445{
Jim Ingham228063c2012-02-21 02:23:08 +0000446 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000447 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000448 {
449 Mutex::Locker locker (GetDebuggerListMutex ());
450 GetDebuggerList().push_back(debugger_sp);
451 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000452 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000453 return debugger_sp;
454}
455
Caroline Ticee02657b2011-01-22 01:02:07 +0000456void
Greg Clayton4d122c42011-09-17 08:33:22 +0000457Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000458{
459 if (debugger_sp.get() == NULL)
460 return;
461
Jim Ingham8314c522011-09-15 21:36:42 +0000462 debugger_sp->Clear();
463
Greg Claytonc15f55e2012-03-30 20:53:46 +0000464 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000465 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000466 Mutex::Locker locker (GetDebuggerListMutex ());
467 DebuggerList &debugger_list = GetDebuggerList ();
468 DebuggerList::iterator pos, end = debugger_list.end();
469 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000470 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000471 if ((*pos).get() == debugger_sp.get())
472 {
473 debugger_list.erase (pos);
474 return;
475 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000476 }
477 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000478}
479
Greg Clayton4d122c42011-09-17 08:33:22 +0000480DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000481Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
482{
Greg Clayton4d122c42011-09-17 08:33:22 +0000483 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000484 if (g_shared_debugger_refcount > 0)
485 {
486 Mutex::Locker locker (GetDebuggerListMutex ());
487 DebuggerList &debugger_list = GetDebuggerList();
488 DebuggerList::iterator pos, end = debugger_list.end();
489
490 for (pos = debugger_list.begin(); pos != end; ++pos)
491 {
492 if ((*pos).get()->m_instance_name == instance_name)
493 {
494 debugger_sp = *pos;
495 break;
496 }
497 }
498 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000499 return debugger_sp;
500}
Greg Clayton66111032010-06-23 01:19:29 +0000501
502TargetSP
503Debugger::FindTargetWithProcessID (lldb::pid_t pid)
504{
Greg Clayton4d122c42011-09-17 08:33:22 +0000505 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000506 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000507 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000508 Mutex::Locker locker (GetDebuggerListMutex ());
509 DebuggerList &debugger_list = GetDebuggerList();
510 DebuggerList::iterator pos, end = debugger_list.end();
511 for (pos = debugger_list.begin(); pos != end; ++pos)
512 {
513 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
514 if (target_sp)
515 break;
516 }
Greg Clayton66111032010-06-23 01:19:29 +0000517 }
518 return target_sp;
519}
520
Greg Claytone4e45922011-11-16 05:37:56 +0000521TargetSP
522Debugger::FindTargetWithProcess (Process *process)
523{
524 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000525 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000526 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000527 Mutex::Locker locker (GetDebuggerListMutex ());
528 DebuggerList &debugger_list = GetDebuggerList();
529 DebuggerList::iterator pos, end = debugger_list.end();
530 for (pos = debugger_list.begin(); pos != end; ++pos)
531 {
532 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
533 if (target_sp)
534 break;
535 }
Greg Claytone4e45922011-11-16 05:37:56 +0000536 }
537 return target_sp;
538}
539
Jim Ingham228063c2012-02-21 02:23:08 +0000540Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000541 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000542 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000543 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544 m_input_file (),
545 m_output_file (),
546 m_error_file (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000547 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000548 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 m_listener ("lldb.Debugger"),
Jim Inghame37d6052011-09-13 00:29:56 +0000550 m_source_manager(*this),
551 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000552 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000553 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000554 m_input_reader_data (),
555 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556{
Greg Clayton67cc0632012-08-22 17:17:09 +0000557 char instance_cstr[256];
558 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
559 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000560 if (log_callback)
561 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000562 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000563 // Always add our default platform to the platform list
564 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
565 assert (default_platform_sp.get());
566 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000567
Greg Clayton754a9362012-08-23 00:22:02 +0000568 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000569 m_collection_sp->AppendProperty (ConstString("target"),
570 ConstString("Settings specify to debugging targets."),
571 true,
572 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000573 if (m_command_interpreter_ap.get())
574 {
575 m_collection_sp->AppendProperty (ConstString("interpreter"),
576 ConstString("Settings specify to the debugger's command interpreter."),
577 true,
578 m_command_interpreter_ap->GetValueProperties());
579 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000580 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
581 term_width->SetMinimumValue(10);
582 term_width->SetMaximumValue(1024);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583}
584
585Debugger::~Debugger ()
586{
Jim Ingham8314c522011-09-15 21:36:42 +0000587 Clear();
588}
589
590void
591Debugger::Clear()
592{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000593 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000594 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000595 int num_targets = m_target_list.GetNumTargets();
596 for (int i = 0; i < num_targets; i++)
597 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000598 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
599 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000600 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000601 ProcessSP process_sp (target_sp->GetProcessSP());
602 if (process_sp)
603 {
604 if (process_sp->GetShouldDetach())
605 process_sp->Detach();
606 }
607 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000608 }
Greg Clayton66111032010-06-23 01:19:29 +0000609 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000610 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000611
612 // Close the input file _before_ we close the input read communications class
613 // as it does NOT own the input file, our m_input_file does.
614 GetInputFile().Close ();
615 // Now that we have closed m_input_file, we can now tell our input communication
616 // class to close down. Its read thread should quickly exit after we close
617 // the input file handle above.
618 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000619}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620
621bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000622Debugger::GetCloseInputOnEOF () const
623{
624 return m_input_comm.GetCloseOnEOF();
625}
626
627void
628Debugger::SetCloseInputOnEOF (bool b)
629{
630 m_input_comm.SetCloseOnEOF(b);
631}
632
633bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634Debugger::GetAsyncExecution ()
635{
Greg Clayton66111032010-06-23 01:19:29 +0000636 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637}
638
639void
640Debugger::SetAsyncExecution (bool async_execution)
641{
Greg Clayton66111032010-06-23 01:19:29 +0000642 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643}
644
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645
646void
647Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
648{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000649 File &in_file = GetInputFile();
650 in_file.SetStream (fh, tranfer_ownership);
651 if (in_file.IsValid() == false)
652 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653
654 // Disconnect from any old connection if we had one
655 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000656 // Pass false as the second argument to ConnectionFileDescriptor below because
657 // our "in_file" above will already take ownership if requested and we don't
658 // want to objects trying to own and close a file descriptor.
659 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
661
662 Error error;
663 if (m_input_comm.StartReadThread (&error) == false)
664 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000665 File &err_file = GetErrorFile();
666
667 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
668 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670}
671
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672void
673Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
674{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000675 File &out_file = GetOutputFile();
676 out_file.SetStream (fh, tranfer_ownership);
677 if (out_file.IsValid() == false)
678 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000679
680 GetCommandInterpreter().GetScriptInterpreter()->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681}
682
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683void
684Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
685{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000686 File &err_file = GetErrorFile();
687 err_file.SetStream (fh, tranfer_ownership);
688 if (err_file.IsValid() == false)
689 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690}
691
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000693Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694{
695 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000696 TargetSP target_sp(GetSelectedTarget());
697 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698
699 if (target_sp)
700 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000701 ProcessSP process_sp (target_sp->GetProcessSP());
702 exe_ctx.SetProcessSP (process_sp);
703 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000705 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
706 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000708 exe_ctx.SetThreadSP (thread_sp);
709 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
710 if (exe_ctx.GetFramePtr() == NULL)
711 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 }
713 }
714 }
715 return exe_ctx;
716
717}
718
Caroline Ticeb44880c2011-02-10 01:15:13 +0000719InputReaderSP
720Debugger::GetCurrentInputReader ()
721{
722 InputReaderSP reader_sp;
723
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000724 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000725 {
726 // Clear any finished readers from the stack
727 while (CheckIfTopInputReaderIsDone()) ;
728
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000729 if (!m_input_reader_stack.IsEmpty())
730 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000731 }
732
733 return reader_sp;
734}
735
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736void
737Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
738{
Caroline Ticeefed6132010-11-19 20:47:54 +0000739 if (bytes_len > 0)
740 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
741 else
742 ((Debugger *)baton)->DispatchInputEndOfFile ();
743}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744
745
746void
747Debugger::DispatchInput (const char *bytes, size_t bytes_len)
748{
Caroline Ticeefed6132010-11-19 20:47:54 +0000749 if (bytes == NULL || bytes_len == 0)
750 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751
752 WriteToDefaultReader (bytes, bytes_len);
753}
754
755void
Caroline Ticeefed6132010-11-19 20:47:54 +0000756Debugger::DispatchInputInterrupt ()
757{
758 m_input_reader_data.clear();
759
Caroline Ticeb44880c2011-02-10 01:15:13 +0000760 InputReaderSP reader_sp (GetCurrentInputReader ());
761 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000762 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000763 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000764
Caroline Ticeb44880c2011-02-10 01:15:13 +0000765 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000766 while (CheckIfTopInputReaderIsDone ()) ;
767 }
768}
769
770void
771Debugger::DispatchInputEndOfFile ()
772{
773 m_input_reader_data.clear();
774
Caroline Ticeb44880c2011-02-10 01:15:13 +0000775 InputReaderSP reader_sp (GetCurrentInputReader ());
776 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000777 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000778 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000779
Caroline Ticeb44880c2011-02-10 01:15:13 +0000780 // 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 +0000781 while (CheckIfTopInputReaderIsDone ()) ;
782 }
783}
784
785void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000786Debugger::CleanUpInputReaders ()
787{
788 m_input_reader_data.clear();
789
Caroline Ticeb44880c2011-02-10 01:15:13 +0000790 // 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 +0000791 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000792 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000793 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000794 if (reader_sp)
795 {
796 reader_sp->Notify (eInputReaderEndOfFile);
797 reader_sp->SetIsDone (true);
798 }
799 }
800}
801
802void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000803Debugger::NotifyTopInputReader (InputReaderAction notification)
804{
805 InputReaderSP reader_sp (GetCurrentInputReader());
806 if (reader_sp)
807 {
808 reader_sp->Notify (notification);
809
810 // Flush out any input readers that are done.
811 while (CheckIfTopInputReaderIsDone ())
812 /* Do nothing. */;
813 }
814}
815
Caroline Tice9088b062011-05-09 23:06:58 +0000816bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000817Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000818{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000819 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000820
Caroline Ticed61c10b2011-06-16 16:27:19 +0000821 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000822}
823
824
Caroline Tice969ed3d2011-05-02 20:41:46 +0000825void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
827{
828 if (bytes && bytes_len)
829 m_input_reader_data.append (bytes, bytes_len);
830
831 if (m_input_reader_data.empty())
832 return;
833
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000834 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000837 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 if (!reader_sp)
839 break;
840
Greg Clayton471b31c2010-07-20 22:52:08 +0000841 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842 m_input_reader_data.size());
843 if (bytes_handled)
844 {
845 m_input_reader_data.erase (0, bytes_handled);
846 }
847 else
848 {
849 // No bytes were handled, we might not have reached our
850 // granularity, just return and wait for more data
851 break;
852 }
853 }
854
Caroline Ticeb44880c2011-02-10 01:15:13 +0000855 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856 while (CheckIfTopInputReaderIsDone ())
857 /* Do nothing. */;
858
859}
860
861void
862Debugger::PushInputReader (const InputReaderSP& reader_sp)
863{
864 if (!reader_sp)
865 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000866
867 // Deactivate the old top reader
868 InputReaderSP top_reader_sp (GetCurrentInputReader ());
869
870 if (top_reader_sp)
871 top_reader_sp->Notify (eInputReaderDeactivate);
872
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000873 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 reader_sp->Notify (eInputReaderActivate);
875 ActivateInputReader (reader_sp);
876}
877
878bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000879Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880{
881 bool result = false;
882
883 // The reader on the stop of the stack is done, so let the next
884 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000885 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000887 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000888 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889
890 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
891 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000892 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893 reader_sp->Notify (eInputReaderDeactivate);
894 reader_sp->Notify (eInputReaderDone);
895 result = true;
896
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000897 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000899 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900 if (reader_sp)
901 {
902 ActivateInputReader (reader_sp);
903 reader_sp->Notify (eInputReaderReactivate);
904 }
905 }
906 }
907 }
908 return result;
909}
910
911bool
912Debugger::CheckIfTopInputReaderIsDone ()
913{
914 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000915 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000917 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000918 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919
920 if (reader_sp && reader_sp->IsDone())
921 {
922 result = true;
923 PopInputReader (reader_sp);
924 }
925 }
926 return result;
927}
928
929void
930Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
931{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000932 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000934 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000935 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000936 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000937
938 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939
Greg Claytona3406612011-02-07 23:24:47 +0000940 switch (reader_sp->GetGranularity())
941 {
942 case eInputReaderGranularityByte:
943 case eInputReaderGranularityWord:
944 tty.SetCanonical (false);
945 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946
Greg Claytona3406612011-02-07 23:24:47 +0000947 case eInputReaderGranularityLine:
948 case eInputReaderGranularityAll:
949 tty.SetCanonical (true);
950 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951
Greg Claytona3406612011-02-07 23:24:47 +0000952 default:
953 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954 }
955 }
956}
Greg Clayton66111032010-06-23 01:19:29 +0000957
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000958StreamSP
959Debugger::GetAsyncOutputStream ()
960{
961 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
962 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
963}
964
965StreamSP
966Debugger::GetAsyncErrorStream ()
967{
968 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
969 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
970}
971
Enrico Granata061858c2012-02-15 02:34:21 +0000972uint32_t
973Debugger::GetNumDebuggers()
974{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000975 if (g_shared_debugger_refcount > 0)
976 {
977 Mutex::Locker locker (GetDebuggerListMutex ());
978 return GetDebuggerList().size();
979 }
980 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +0000981}
982
983lldb::DebuggerSP
984Debugger::GetDebuggerAtIndex (uint32_t index)
985{
986 DebuggerSP debugger_sp;
987
Greg Claytonc15f55e2012-03-30 20:53:46 +0000988 if (g_shared_debugger_refcount > 0)
989 {
990 Mutex::Locker locker (GetDebuggerListMutex ());
991 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +0000992
Greg Claytonc15f55e2012-03-30 20:53:46 +0000993 if (index < debugger_list.size())
994 debugger_sp = debugger_list[index];
995 }
996
Enrico Granata061858c2012-02-15 02:34:21 +0000997 return debugger_sp;
998}
999
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001000DebuggerSP
1001Debugger::FindDebuggerWithID (lldb::user_id_t id)
1002{
Greg Clayton4d122c42011-09-17 08:33:22 +00001003 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001004
Greg Claytonc15f55e2012-03-30 20:53:46 +00001005 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001006 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001007 Mutex::Locker locker (GetDebuggerListMutex ());
1008 DebuggerList &debugger_list = GetDebuggerList();
1009 DebuggerList::iterator pos, end = debugger_list.end();
1010 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001011 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001012 if ((*pos).get()->GetID() == id)
1013 {
1014 debugger_sp = *pos;
1015 break;
1016 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001017 }
1018 }
1019 return debugger_sp;
1020}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001021
Greg Clayton1b654882010-09-19 02:33:57 +00001022static void
1023TestPromptFormats (StackFrame *frame)
1024{
1025 if (frame == NULL)
1026 return;
1027
1028 StreamString s;
1029 const char *prompt_format =
1030 "{addr = '${addr}'\n}"
1031 "{process.id = '${process.id}'\n}"
1032 "{process.name = '${process.name}'\n}"
1033 "{process.file.basename = '${process.file.basename}'\n}"
1034 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1035 "{thread.id = '${thread.id}'\n}"
1036 "{thread.index = '${thread.index}'\n}"
1037 "{thread.name = '${thread.name}'\n}"
1038 "{thread.queue = '${thread.queue}'\n}"
1039 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1040 "{target.arch = '${target.arch}'\n}"
1041 "{module.file.basename = '${module.file.basename}'\n}"
1042 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1043 "{file.basename = '${file.basename}'\n}"
1044 "{file.fullpath = '${file.fullpath}'\n}"
1045 "{frame.index = '${frame.index}'\n}"
1046 "{frame.pc = '${frame.pc}'\n}"
1047 "{frame.sp = '${frame.sp}'\n}"
1048 "{frame.fp = '${frame.fp}'\n}"
1049 "{frame.flags = '${frame.flags}'\n}"
1050 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1051 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1052 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1053 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1054 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1055 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1056 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1057 "{function.id = '${function.id}'\n}"
1058 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001059 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001060 "{function.addr-offset = '${function.addr-offset}'\n}"
1061 "{function.line-offset = '${function.line-offset}'\n}"
1062 "{function.pc-offset = '${function.pc-offset}'\n}"
1063 "{line.file.basename = '${line.file.basename}'\n}"
1064 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1065 "{line.number = '${line.number}'\n}"
1066 "{line.start-addr = '${line.start-addr}'\n}"
1067 "{line.end-addr = '${line.end-addr}'\n}"
1068;
1069
1070 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1071 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001072 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +00001073 const char *end = NULL;
1074 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
1075 {
1076 printf("%s\n", s.GetData());
1077 }
1078 else
1079 {
1080 printf ("error: at '%s'\n", end);
1081 printf ("what we got: %s\n", s.GetData());
1082 }
1083}
1084
Enrico Granata9fc19442011-07-06 02:13:41 +00001085static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001086ScanFormatDescriptor (const char* var_name_begin,
1087 const char* var_name_end,
1088 const char** var_name_final,
1089 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001090 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001091 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001092{
Enrico Granatae992a082011-07-22 17:03:19 +00001093 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001094 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001095 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001096 {
1097 if (log)
1098 log->Printf("no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001099 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001100 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001101 else
1102 {
1103 *var_name_final = *percent_position;
1104 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
1105 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001106 if (log)
1107 log->Printf("parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001108 if ( !FormatManager::GetFormatFromCString(format_name,
1109 true,
1110 *custom_format) )
1111 {
Enrico Granatae992a082011-07-22 17:03:19 +00001112 if (log)
1113 log->Printf("%s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001114 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +00001115 if (*format_name == '@')
Enrico Granata86cc9822012-03-19 22:58:49 +00001116 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
Enrico Granata9fc19442011-07-06 02:13:41 +00001117 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +00001118 else if (*format_name == 'V')
Enrico Granata86cc9822012-03-19 22:58:49 +00001119 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatad55546b2011-07-22 00:16:08 +00001120 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +00001121 else if (*format_name == 'L')
Enrico Granata86cc9822012-03-19 22:58:49 +00001122 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +00001123 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +00001124 else if (*format_name == 'S')
Enrico Granata86cc9822012-03-19 22:58:49 +00001125 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Enrico Granata5dfd49c2011-08-04 02:34:29 +00001126 else if (*format_name == '#')
Enrico Granata86cc9822012-03-19 22:58:49 +00001127 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001128 else if (*format_name == 'T')
Enrico Granata86cc9822012-03-19 22:58:49 +00001129 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
Enrico Granatae992a082011-07-22 17:03:19 +00001130 else if (log)
1131 log->Printf("%s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001132 }
1133 // a good custom format tells us to print the value using it
1134 else
Enrico Granatae992a082011-07-22 17:03:19 +00001135 {
1136 if (log)
1137 log->Printf("will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001138 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001139 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001140 delete format_name;
1141 }
Enrico Granatae992a082011-07-22 17:03:19 +00001142 if (log)
1143 log->Printf("final format description outcome: custom_format = %d, val_obj_display = %d",
1144 *custom_format,
1145 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001146 return true;
1147}
1148
1149static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001150ScanBracketedRange (const char* var_name_begin,
1151 const char* var_name_end,
1152 const char* var_name_final,
1153 const char** open_bracket_position,
1154 const char** separator_position,
1155 const char** close_bracket_position,
1156 const char** var_name_final_if_array_range,
1157 int64_t* index_lower,
1158 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001159{
Enrico Granatae992a082011-07-22 17:03:19 +00001160 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001161 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001162 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001163 {
1164 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1165 *close_bracket_position = ::strchr(*open_bracket_position,']');
1166 // as usual, we assume that [] will come before %
1167 //printf("trying to expand a []\n");
1168 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001169 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001170 {
Enrico Granatae992a082011-07-22 17:03:19 +00001171 if (log)
1172 log->Printf("[] detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001173 *index_lower = 0;
1174 }
1175 else if (*separator_position == NULL || *separator_position > var_name_end)
1176 {
1177 char *end = NULL;
1178 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1179 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001180 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001181 log->Printf("[%lld] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001182 }
Greg Clayton34132752011-07-06 04:07:21 +00001183 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001184 {
1185 char *end = NULL;
1186 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1187 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001188 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001189 log->Printf("[%lld-%lld] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001190 }
1191 else
Enrico Granatae992a082011-07-22 17:03:19 +00001192 {
1193 if (log)
1194 log->Printf("expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001195 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001196 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001197 if (*index_lower > *index_higher && *index_higher > 0)
1198 {
Enrico Granatae992a082011-07-22 17:03:19 +00001199 if (log)
1200 log->Printf("swapping indices");
Enrico Granata9fc19442011-07-06 02:13:41 +00001201 int temp = *index_lower;
1202 *index_lower = *index_higher;
1203 *index_higher = temp;
1204 }
1205 }
Enrico Granatae992a082011-07-22 17:03:19 +00001206 else if (log)
1207 log->Printf("no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001208 return true;
1209}
1210
Enrico Granata9fc19442011-07-06 02:13:41 +00001211static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001212ExpandIndexedExpression (ValueObject* valobj,
1213 uint32_t index,
1214 StackFrame* frame,
1215 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001216{
Enrico Granatae992a082011-07-22 17:03:19 +00001217 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001218 const char* ptr_deref_format = "[%d]";
1219 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
1220 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001221 if (log)
1222 log->Printf("name to deref: %s",ptr_deref_buffer.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001223 const char* first_unparsed;
1224 ValueObject::GetValueForExpressionPathOptions options;
1225 ValueObject::ExpressionPathEndResultType final_value_type;
1226 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001227 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatac482a192011-08-17 22:13:59 +00001228 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001229 &first_unparsed,
1230 &reason_to_stop,
1231 &final_value_type,
1232 options,
1233 &what_next);
1234 if (!item)
1235 {
Enrico Granatae992a082011-07-22 17:03:19 +00001236 if (log)
1237 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1238 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001239 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001240 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001241 else
1242 {
Enrico Granatae992a082011-07-22 17:03:19 +00001243 if (log)
1244 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1245 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001246 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001247 }
1248 return item;
1249}
1250
Greg Clayton1b654882010-09-19 02:33:57 +00001251bool
1252Debugger::FormatPrompt
1253(
1254 const char *format,
1255 const SymbolContext *sc,
1256 const ExecutionContext *exe_ctx,
1257 const Address *addr,
1258 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001259 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001260 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001261)
1262{
Enrico Granatac482a192011-08-17 22:13:59 +00001263 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001264 bool success = true;
1265 const char *p;
Enrico Granatae992a082011-07-22 17:03:19 +00001266 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001267 for (p = format; *p != '\0'; ++p)
1268 {
Enrico Granatac482a192011-08-17 22:13:59 +00001269 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001270 {
Enrico Granatac482a192011-08-17 22:13:59 +00001271 valobj = realvalobj;
1272 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001273 }
Greg Clayton1b654882010-09-19 02:33:57 +00001274 size_t non_special_chars = ::strcspn (p, "${}\\");
1275 if (non_special_chars > 0)
1276 {
1277 if (success)
1278 s.Write (p, non_special_chars);
1279 p += non_special_chars;
1280 }
1281
1282 if (*p == '\0')
1283 {
1284 break;
1285 }
1286 else if (*p == '{')
1287 {
1288 // Start a new scope that must have everything it needs if it is to
1289 // to make it into the final output stream "s". If you want to make
1290 // a format that only prints out the function or symbol name if there
1291 // is one in the symbol context you can use:
1292 // "{function =${function.name}}"
1293 // The first '{' starts a new scope that end with the matching '}' at
1294 // the end of the string. The contents "function =${function.name}"
1295 // will then be evaluated and only be output if there is a function
1296 // or symbol with a valid name.
1297 StreamString sub_strm;
1298
1299 ++p; // Skip the '{'
1300
Enrico Granatac482a192011-08-17 22:13:59 +00001301 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001302 {
1303 // The stream had all it needed
1304 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1305 }
1306 if (*p != '}')
1307 {
1308 success = false;
1309 break;
1310 }
1311 }
1312 else if (*p == '}')
1313 {
1314 // End of a enclosing scope
1315 break;
1316 }
1317 else if (*p == '$')
1318 {
1319 // We have a prompt variable to print
1320 ++p;
1321 if (*p == '{')
1322 {
1323 ++p;
1324 const char *var_name_begin = p;
1325 const char *var_name_end = ::strchr (p, '}');
1326
1327 if (var_name_end && var_name_begin < var_name_end)
1328 {
1329 // if we have already failed to parse, skip this variable
1330 if (success)
1331 {
1332 const char *cstr = NULL;
1333 Address format_addr;
1334 bool calculate_format_addr_function_offset = false;
1335 // Set reg_kind and reg_num to invalid values
1336 RegisterKind reg_kind = kNumRegisterKinds;
1337 uint32_t reg_num = LLDB_INVALID_REGNUM;
1338 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001339 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001340 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001341 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001342 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1343 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001344
Greg Clayton1b654882010-09-19 02:33:57 +00001345 // Each variable must set success to true below...
1346 bool var_success = false;
1347 switch (var_name_begin[0])
1348 {
Enrico Granata4becb372011-06-29 22:27:15 +00001349 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001350 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001351 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001352 {
Enrico Granatac482a192011-08-17 22:13:59 +00001353 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001354 break;
1355
Enrico Granatac3e320a2011-08-02 17:27:39 +00001356 if (log)
1357 log->Printf("initial string: %s",var_name_begin);
1358
Enrico Granata6f3533f2011-07-29 19:53:35 +00001359 // check for *var and *svar
1360 if (*var_name_begin == '*')
1361 {
1362 do_deref_pointer = true;
1363 var_name_begin++;
1364 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001365
1366 if (log)
1367 log->Printf("initial string: %s",var_name_begin);
1368
Enrico Granata6f3533f2011-07-29 19:53:35 +00001369 if (*var_name_begin == 's')
1370 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001371 if (!valobj->IsSynthetic())
1372 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001373 if (!valobj)
1374 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001375 var_name_begin++;
1376 }
1377
Enrico Granatac3e320a2011-08-02 17:27:39 +00001378 if (log)
1379 log->Printf("initial string: %s",var_name_begin);
1380
Enrico Granata6f3533f2011-07-29 19:53:35 +00001381 // should be a 'v' by now
1382 if (*var_name_begin != 'v')
1383 break;
1384
Enrico Granatac3e320a2011-08-02 17:27:39 +00001385 if (log)
1386 log->Printf("initial string: %s",var_name_begin);
1387
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001388 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001389 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001390 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001391 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001392 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001393 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001394 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001395 const char* var_name_final = NULL;
1396 const char* var_name_final_if_array_range = NULL;
1397 const char* close_bracket_position = NULL;
1398 int64_t index_lower = -1;
1399 int64_t index_higher = -1;
1400 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001401 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001402 bool was_plain_var = false;
1403 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001404 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001405
Enrico Granatac482a192011-08-17 22:13:59 +00001406 if (!valobj) break;
1407 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001408 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001409 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001410 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001411 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001412 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001413 }
1414 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1415 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001416 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001417 // this is a variable with some custom format applied to it
1418 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001419 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001420 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001421 ScanFormatDescriptor (var_name_begin,
1422 var_name_end,
1423 &var_name_final,
1424 &percent_position,
1425 &custom_format,
1426 &val_obj_display);
1427 }
1428 // this is ${var.something} or multiple .something nested
1429 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1430 {
Enrico Granataa777dc22012-05-08 21:49:57 +00001431 if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1432 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001433 const char* percent_position;
1434 ScanFormatDescriptor (var_name_begin,
1435 var_name_end,
1436 &var_name_final,
1437 &percent_position,
1438 &custom_format,
1439 &val_obj_display);
1440
1441 const char* open_bracket_position;
1442 const char* separator_position;
1443 ScanBracketedRange (var_name_begin,
1444 var_name_end,
1445 var_name_final,
1446 &open_bracket_position,
1447 &separator_position,
1448 &close_bracket_position,
1449 &var_name_final_if_array_range,
1450 &index_lower,
1451 &index_higher);
1452
1453 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001454
1455 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1456 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1457 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1458
Enrico Granatae992a082011-07-22 17:03:19 +00001459 if (log)
1460 log->Printf("symbol to expand: %s",expr_path.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001461
Enrico Granatac482a192011-08-17 22:13:59 +00001462 target = valobj->GetValueForExpressionPath(expr_path.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001463 &first_unparsed,
1464 &reason_to_stop,
1465 &final_value_type,
1466 options,
1467 &what_next).get();
1468
1469 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001470 {
Enrico Granatae992a082011-07-22 17:03:19 +00001471 if (log)
1472 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1473 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001474 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001475 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001476 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001477 else
1478 {
Enrico Granatae992a082011-07-22 17:03:19 +00001479 if (log)
1480 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1481 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001482 first_unparsed, reason_to_stop, final_value_type);
1483 }
Enrico Granata4becb372011-06-29 22:27:15 +00001484 }
Greg Clayton34132752011-07-06 04:07:21 +00001485 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001486 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001487
Enrico Granata86cc9822012-03-19 22:58:49 +00001488 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1489 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001490
Enrico Granata86cc9822012-03-19 22:58:49 +00001491 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001492
Enrico Granataa7187d02011-07-06 19:27:11 +00001493 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001494 {
Greg Clayton34132752011-07-06 04:07:21 +00001495 // I have not deref-ed yet, let's do it
1496 // this happens when we are not going through GetValueForVariableExpressionPath
1497 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001498 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001499 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001500 if (error.Fail())
1501 {
1502 if (log)
1503 log->Printf("ERROR: %s\n", error.AsCString("unknown")); \
1504 break;
1505 }
Greg Clayton34132752011-07-06 04:07:21 +00001506 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001507 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001508
Enrico Granataa777dc22012-05-08 21:49:57 +00001509 // <rdar://problem/11338654>
1510 // we do not want to use the summary for a bitfield of type T:n
1511 // if we were originally dealing with just a T - that would get
1512 // us into an endless recursion
1513 if (target->IsBitfield() && was_var_indexed)
1514 {
1515 // TODO: check for a (T:n)-specific summary - we should still obey that
1516 StreamString bitfield_name;
1517 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1518 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1519 if (!DataVisualization::GetSummaryForType(type_sp))
1520 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1521 }
1522
Enrico Granata85933ed2011-08-18 16:38:26 +00001523 // TODO use flags for these
Enrico Granataf4efecd2011-07-12 22:56:10 +00001524 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1525 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001526 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001527
Enrico Granata86cc9822012-03-19 22:58:49 +00001528 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 +00001529 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001530 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001531 if (log)
1532 log->Printf("I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001533
Enrico Granata86cc9822012-03-19 22:58:49 +00001534 if (target->HasSpecialPrintableRepresentation(val_obj_display,
1535 custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001536 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001537 // try to use the special cases
1538 var_success = target->DumpPrintableRepresentation(str_temp,
1539 val_obj_display,
1540 custom_format);
1541 if (log)
1542 log->Printf("special cases did%s match", var_success ? "" : "n't");
1543
1544 // should not happen
1545 if (!var_success)
1546 s << "<invalid usage of pointer value as object>";
1547 else
1548 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001549 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001550 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001551 }
1552 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001553 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001554 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001555 {
1556 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1557 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001558 else if (is_pointer) // if pointer, value is the address stored
1559 {
Greg Clayton23f59502012-07-17 03:23:13 +00001560 target->DumpPrintableRepresentation (s,
1561 val_obj_display,
1562 custom_format,
1563 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001564 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001565 else
1566 {
1567 s << "<invalid usage of pointer value as object>";
1568 }
1569 var_success = true;
1570 break;
1571 }
1572 }
1573
1574 // if directly trying to print ${var}, and this is an aggregate, display a nice
1575 // type @ location message
1576 if (is_aggregate && was_plain_var)
1577 {
1578 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1579 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001580 break;
1581 }
1582
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001583 // 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 +00001584 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001585 {
1586 s << "<invalid use of aggregate type>";
1587 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001588 break;
1589 }
Greg Clayton34132752011-07-06 04:07:21 +00001590
1591 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001592 {
1593 if (log)
1594 log->Printf("dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001595 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001596 }
Greg Clayton34132752011-07-06 04:07:21 +00001597 else
Enrico Granatae992a082011-07-22 17:03:19 +00001598 {
1599 if (log)
1600 log->Printf("checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001601 if (!is_array && !is_pointer)
1602 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001603 if (log)
1604 log->Printf("handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001605 const char* special_directions = NULL;
1606 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001607 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1608 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001609 ConstString additional_data;
1610 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1611 special_directions_writer.Printf("${%svar%s}",
1612 do_deref_pointer ? "*" : "",
1613 additional_data.GetCString());
1614 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001615 }
1616
1617 // let us display items index_lower thru index_higher of this array
1618 s.PutChar('[');
1619 var_success = true;
1620
1621 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001622 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001623
Greg Claytoncc4d0142012-02-17 07:49:44 +00001624 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001625
Greg Clayton34132752011-07-06 04:07:21 +00001626 for (;index_lower<=index_higher;index_lower++)
1627 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001628 ValueObject* item = ExpandIndexedExpression (target,
1629 index_lower,
1630 exe_ctx->GetFramePtr(),
1631 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001632
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001633 if (!item)
1634 {
Enrico Granatae992a082011-07-22 17:03:19 +00001635 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001636 log->Printf("ERROR in getting child item at index %lld", index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001637 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001638 else
1639 {
Enrico Granatae992a082011-07-22 17:03:19 +00001640 if (log)
1641 log->Printf("special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001642 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001643
Greg Clayton34132752011-07-06 04:07:21 +00001644 if (!special_directions)
1645 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1646 else
1647 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1648
Enrico Granata22c55d12011-08-12 02:00:06 +00001649 if (--max_num_children == 0)
1650 {
1651 s.PutCString(", ...");
1652 break;
1653 }
1654
Greg Clayton34132752011-07-06 04:07:21 +00001655 if (index_lower < index_higher)
1656 s.PutChar(',');
1657 }
1658 s.PutChar(']');
1659 }
Enrico Granata4becb372011-06-29 22:27:15 +00001660 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001661 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001662 case 'a':
1663 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1664 {
1665 if (addr && addr->IsValid())
1666 {
1667 var_success = true;
1668 format_addr = *addr;
1669 }
1670 }
Greg Clayton5a314712011-10-14 07:41:33 +00001671 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1672 {
1673 var_success = true;
1674 var_name_begin += strlen("ansi."); // Skip the "ansi."
1675 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1676 {
1677 var_name_begin += strlen("fg."); // Skip the "fg."
1678 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1679 {
1680 s.Printf ("%s%s%s",
1681 lldb_utility::ansi::k_escape_start,
1682 lldb_utility::ansi::k_fg_black,
1683 lldb_utility::ansi::k_escape_end);
1684 }
1685 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1686 {
1687 s.Printf ("%s%s%s",
1688 lldb_utility::ansi::k_escape_start,
1689 lldb_utility::ansi::k_fg_red,
1690 lldb_utility::ansi::k_escape_end);
1691 }
1692 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1693 {
1694 s.Printf ("%s%s%s",
1695 lldb_utility::ansi::k_escape_start,
1696 lldb_utility::ansi::k_fg_green,
1697 lldb_utility::ansi::k_escape_end);
1698 }
1699 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1700 {
1701 s.Printf ("%s%s%s",
1702 lldb_utility::ansi::k_escape_start,
1703 lldb_utility::ansi::k_fg_yellow,
1704 lldb_utility::ansi::k_escape_end);
1705 }
1706 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1707 {
1708 s.Printf ("%s%s%s",
1709 lldb_utility::ansi::k_escape_start,
1710 lldb_utility::ansi::k_fg_blue,
1711 lldb_utility::ansi::k_escape_end);
1712 }
1713 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1714 {
1715 s.Printf ("%s%s%s",
1716 lldb_utility::ansi::k_escape_start,
1717 lldb_utility::ansi::k_fg_purple,
1718 lldb_utility::ansi::k_escape_end);
1719 }
1720 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1721 {
1722 s.Printf ("%s%s%s",
1723 lldb_utility::ansi::k_escape_start,
1724 lldb_utility::ansi::k_fg_cyan,
1725 lldb_utility::ansi::k_escape_end);
1726 }
1727 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1728 {
1729 s.Printf ("%s%s%s",
1730 lldb_utility::ansi::k_escape_start,
1731 lldb_utility::ansi::k_fg_white,
1732 lldb_utility::ansi::k_escape_end);
1733 }
1734 else
1735 {
1736 var_success = false;
1737 }
1738 }
1739 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1740 {
1741 var_name_begin += strlen("bg."); // Skip the "bg."
1742 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1743 {
1744 s.Printf ("%s%s%s",
1745 lldb_utility::ansi::k_escape_start,
1746 lldb_utility::ansi::k_bg_black,
1747 lldb_utility::ansi::k_escape_end);
1748 }
1749 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1750 {
1751 s.Printf ("%s%s%s",
1752 lldb_utility::ansi::k_escape_start,
1753 lldb_utility::ansi::k_bg_red,
1754 lldb_utility::ansi::k_escape_end);
1755 }
1756 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1757 {
1758 s.Printf ("%s%s%s",
1759 lldb_utility::ansi::k_escape_start,
1760 lldb_utility::ansi::k_bg_green,
1761 lldb_utility::ansi::k_escape_end);
1762 }
1763 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1764 {
1765 s.Printf ("%s%s%s",
1766 lldb_utility::ansi::k_escape_start,
1767 lldb_utility::ansi::k_bg_yellow,
1768 lldb_utility::ansi::k_escape_end);
1769 }
1770 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1771 {
1772 s.Printf ("%s%s%s",
1773 lldb_utility::ansi::k_escape_start,
1774 lldb_utility::ansi::k_bg_blue,
1775 lldb_utility::ansi::k_escape_end);
1776 }
1777 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1778 {
1779 s.Printf ("%s%s%s",
1780 lldb_utility::ansi::k_escape_start,
1781 lldb_utility::ansi::k_bg_purple,
1782 lldb_utility::ansi::k_escape_end);
1783 }
1784 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1785 {
1786 s.Printf ("%s%s%s",
1787 lldb_utility::ansi::k_escape_start,
1788 lldb_utility::ansi::k_bg_cyan,
1789 lldb_utility::ansi::k_escape_end);
1790 }
1791 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1792 {
1793 s.Printf ("%s%s%s",
1794 lldb_utility::ansi::k_escape_start,
1795 lldb_utility::ansi::k_bg_white,
1796 lldb_utility::ansi::k_escape_end);
1797 }
1798 else
1799 {
1800 var_success = false;
1801 }
1802 }
1803 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1804 {
1805 s.Printf ("%s%s%s",
1806 lldb_utility::ansi::k_escape_start,
1807 lldb_utility::ansi::k_ctrl_normal,
1808 lldb_utility::ansi::k_escape_end);
1809 }
1810 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1811 {
1812 s.Printf ("%s%s%s",
1813 lldb_utility::ansi::k_escape_start,
1814 lldb_utility::ansi::k_ctrl_bold,
1815 lldb_utility::ansi::k_escape_end);
1816 }
1817 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1818 {
1819 s.Printf ("%s%s%s",
1820 lldb_utility::ansi::k_escape_start,
1821 lldb_utility::ansi::k_ctrl_faint,
1822 lldb_utility::ansi::k_escape_end);
1823 }
1824 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1825 {
1826 s.Printf ("%s%s%s",
1827 lldb_utility::ansi::k_escape_start,
1828 lldb_utility::ansi::k_ctrl_italic,
1829 lldb_utility::ansi::k_escape_end);
1830 }
1831 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1832 {
1833 s.Printf ("%s%s%s",
1834 lldb_utility::ansi::k_escape_start,
1835 lldb_utility::ansi::k_ctrl_underline,
1836 lldb_utility::ansi::k_escape_end);
1837 }
1838 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1839 {
1840 s.Printf ("%s%s%s",
1841 lldb_utility::ansi::k_escape_start,
1842 lldb_utility::ansi::k_ctrl_slow_blink,
1843 lldb_utility::ansi::k_escape_end);
1844 }
1845 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1846 {
1847 s.Printf ("%s%s%s",
1848 lldb_utility::ansi::k_escape_start,
1849 lldb_utility::ansi::k_ctrl_fast_blink,
1850 lldb_utility::ansi::k_escape_end);
1851 }
1852 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1853 {
1854 s.Printf ("%s%s%s",
1855 lldb_utility::ansi::k_escape_start,
1856 lldb_utility::ansi::k_ctrl_negative,
1857 lldb_utility::ansi::k_escape_end);
1858 }
1859 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1860 {
1861 s.Printf ("%s%s%s",
1862 lldb_utility::ansi::k_escape_start,
1863 lldb_utility::ansi::k_ctrl_conceal,
1864 lldb_utility::ansi::k_escape_end);
1865
1866 }
1867 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1868 {
1869 s.Printf ("%s%s%s",
1870 lldb_utility::ansi::k_escape_start,
1871 lldb_utility::ansi::k_ctrl_crossed_out,
1872 lldb_utility::ansi::k_escape_end);
1873 }
1874 else
1875 {
1876 var_success = false;
1877 }
1878 }
Greg Clayton1b654882010-09-19 02:33:57 +00001879 break;
1880
1881 case 'p':
1882 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1883 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001884 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001885 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001886 Process *process = exe_ctx->GetProcessPtr();
1887 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001888 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001889 var_name_begin += ::strlen ("process.");
1890 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001891 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001892 s.Printf("%llu", process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001893 var_success = true;
1894 }
1895 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1896 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1897 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1898 {
1899 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1900 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001901 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001902 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1903 {
1904 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1905 var_success = format_file_spec;
1906 }
1907 else
1908 {
1909 format_file_spec = exe_module->GetFileSpec();
1910 var_success = format_file_spec;
1911 }
Greg Clayton1b654882010-09-19 02:33:57 +00001912 }
1913 }
1914 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001915 }
Greg Clayton1b654882010-09-19 02:33:57 +00001916 }
1917 break;
1918
1919 case 't':
1920 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1921 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001922 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001923 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001924 Thread *thread = exe_ctx->GetThreadPtr();
1925 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001926 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001927 var_name_begin += ::strlen ("thread.");
1928 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001929 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001930 s.Printf("0x%4.4llx", thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001931 var_success = true;
1932 }
1933 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1934 {
1935 s.Printf("%u", thread->GetIndexID());
1936 var_success = true;
1937 }
1938 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1939 {
1940 cstr = thread->GetName();
1941 var_success = cstr && cstr[0];
1942 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001943 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001944 }
1945 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1946 {
1947 cstr = thread->GetQueueName();
1948 var_success = cstr && cstr[0];
1949 if (var_success)
1950 s.PutCString(cstr);
1951 }
1952 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1953 {
1954 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1955 if (stop_info_sp)
1956 {
1957 cstr = stop_info_sp->GetDescription();
1958 if (cstr && cstr[0])
1959 {
1960 s.PutCString(cstr);
1961 var_success = true;
1962 }
Greg Clayton1b654882010-09-19 02:33:57 +00001963 }
1964 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001965 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1966 {
1967 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1968 if (stop_info_sp)
1969 {
1970 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1971 if (return_valobj_sp)
1972 {
Jim Inghamef651602011-12-22 19:12:40 +00001973 ValueObject::DumpValueObjectOptions dump_options;
1974 ValueObject::DumpValueObject (s, return_valobj_sp.get(), dump_options);
1975 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001976 }
1977 }
1978 }
Greg Clayton1b654882010-09-19 02:33:57 +00001979 }
1980 }
1981 }
1982 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1983 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001984 // TODO: hookup properties
1985// if (!target_properties_sp)
1986// {
1987// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1988// if (target)
1989// target_properties_sp = target->GetProperties();
1990// }
1991//
1992// if (target_properties_sp)
1993// {
1994// var_name_begin += ::strlen ("target.");
1995// const char *end_property = strchr(var_name_begin, '}');
1996// if (end_property)
1997// {
1998// ConstString property_name(var_name_begin, end_property - var_name_begin);
1999// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2000// if (!property_value.empty())
2001// {
2002// s.PutCString (property_value.c_str());
2003// var_success = true;
2004// }
2005// }
2006// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002007 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2008 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002009 {
Greg Clayton1b654882010-09-19 02:33:57 +00002010 var_name_begin += ::strlen ("target.");
2011 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
2012 {
2013 ArchSpec arch (target->GetArchitecture ());
2014 if (arch.IsValid())
2015 {
Greg Clayton64195a22011-02-23 00:35:02 +00002016 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002017 var_success = true;
2018 }
2019 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002020 }
Greg Clayton1b654882010-09-19 02:33:57 +00002021 }
2022 break;
2023
2024
2025 case 'm':
2026 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
2027 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002028 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002029 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002030 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002031 var_name_begin += ::strlen ("module.");
2032
2033 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2034 {
2035 if (module->GetFileSpec())
2036 {
2037 var_name_begin += ::strlen ("file.");
2038
2039 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2040 {
2041 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2042 var_success = format_file_spec;
2043 }
2044 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2045 {
2046 format_file_spec = module->GetFileSpec();
2047 var_success = format_file_spec;
2048 }
2049 }
2050 }
2051 }
2052 }
2053 break;
2054
2055
2056 case 'f':
2057 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2058 {
2059 if (sc && sc->comp_unit != NULL)
2060 {
2061 var_name_begin += ::strlen ("file.");
2062
2063 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2064 {
2065 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2066 var_success = format_file_spec;
2067 }
2068 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2069 {
2070 format_file_spec = *sc->comp_unit;
2071 var_success = format_file_spec;
2072 }
2073 }
2074 }
2075 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
2076 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002077 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002078 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002079 StackFrame *frame = exe_ctx->GetFramePtr();
2080 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002081 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002082 var_name_begin += ::strlen ("frame.");
2083 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00002084 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002085 s.Printf("%u", frame->GetFrameIndex());
2086 var_success = true;
2087 }
2088 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
2089 {
2090 reg_kind = eRegisterKindGeneric;
2091 reg_num = LLDB_REGNUM_GENERIC_PC;
2092 var_success = true;
2093 }
2094 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
2095 {
2096 reg_kind = eRegisterKindGeneric;
2097 reg_num = LLDB_REGNUM_GENERIC_SP;
2098 var_success = true;
2099 }
2100 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
2101 {
2102 reg_kind = eRegisterKindGeneric;
2103 reg_num = LLDB_REGNUM_GENERIC_FP;
2104 var_success = true;
2105 }
2106 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
2107 {
2108 reg_kind = eRegisterKindGeneric;
2109 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2110 var_success = true;
2111 }
2112 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
2113 {
2114 reg_ctx = frame->GetRegisterContext().get();
2115 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002116 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002117 var_name_begin += ::strlen ("reg.");
2118 if (var_name_begin < var_name_end)
2119 {
2120 std::string reg_name (var_name_begin, var_name_end);
2121 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2122 if (reg_info)
2123 var_success = true;
2124 }
Greg Clayton1b654882010-09-19 02:33:57 +00002125 }
2126 }
2127 }
2128 }
2129 }
2130 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
2131 {
2132 if (sc && (sc->function != NULL || sc->symbol != NULL))
2133 {
2134 var_name_begin += ::strlen ("function.");
2135 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2136 {
2137 if (sc->function)
Greg Clayton81c22f62011-10-19 18:09:39 +00002138 s.Printf("function{0x%8.8llx}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002139 else
2140 s.Printf("symbol[%u]", sc->symbol->GetID());
2141
2142 var_success = true;
2143 }
2144 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2145 {
2146 if (sc->function)
2147 cstr = sc->function->GetName().AsCString (NULL);
2148 else if (sc->symbol)
2149 cstr = sc->symbol->GetName().AsCString (NULL);
2150 if (cstr)
2151 {
2152 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002153
2154 if (sc->block)
2155 {
2156 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2157 if (inline_block)
2158 {
2159 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2160 if (inline_info)
2161 {
2162 s.PutCString(" [inlined] ");
2163 inline_info->GetName().Dump(&s);
2164 }
2165 }
2166 }
Greg Clayton1b654882010-09-19 02:33:57 +00002167 var_success = true;
2168 }
2169 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002170 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2171 {
2172 // Print the function name with arguments in it
2173
2174 if (sc->function)
2175 {
2176 var_success = true;
2177 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2178 cstr = sc->function->GetName().AsCString (NULL);
2179 if (cstr)
2180 {
2181 const InlineFunctionInfo *inline_info = NULL;
2182 VariableListSP variable_list_sp;
2183 bool get_function_vars = true;
2184 if (sc->block)
2185 {
2186 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2187
2188 if (inline_block)
2189 {
2190 get_function_vars = false;
2191 inline_info = sc->block->GetInlinedFunctionInfo();
2192 if (inline_info)
2193 variable_list_sp = inline_block->GetBlockVariableList (true);
2194 }
2195 }
2196
2197 if (get_function_vars)
2198 {
2199 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2200 }
2201
2202 if (inline_info)
2203 {
2204 s.PutCString (cstr);
2205 s.PutCString (" [inlined] ");
2206 cstr = inline_info->GetName().GetCString();
2207 }
2208
2209 VariableList args;
2210 if (variable_list_sp)
2211 {
2212 const size_t num_variables = variable_list_sp->GetSize();
2213 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
2214 {
2215 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
2216 if (var_sp->GetScope() == eValueTypeVariableArgument)
2217 args.AddVariable (var_sp);
2218 }
2219
2220 }
2221 if (args.GetSize() > 0)
2222 {
2223 const char *open_paren = strchr (cstr, '(');
2224 const char *close_paren = NULL;
2225 if (open_paren)
2226 close_paren = strchr (open_paren, ')');
2227
2228 if (open_paren)
2229 s.Write(cstr, open_paren - cstr + 1);
2230 else
2231 {
2232 s.PutCString (cstr);
2233 s.PutChar ('(');
2234 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002235 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002236 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2237 {
2238 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2239 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2240 const char *var_name = var_value_sp->GetName().GetCString();
2241 const char *var_value = var_value_sp->GetValueAsCString();
2242 if (var_value_sp->GetError().Success())
2243 {
2244 if (arg_idx > 0)
2245 s.PutCString (", ");
2246 s.Printf ("%s=%s", var_name, var_value);
2247 }
2248 }
2249
2250 if (close_paren)
2251 s.PutCString (close_paren);
2252 else
2253 s.PutChar(')');
2254
2255 }
2256 else
2257 {
2258 s.PutCString(cstr);
2259 }
2260 }
2261 }
2262 else if (sc->symbol)
2263 {
2264 cstr = sc->symbol->GetName().AsCString (NULL);
2265 if (cstr)
2266 {
2267 s.PutCString(cstr);
2268 var_success = true;
2269 }
2270 }
2271 }
Greg Clayton1b654882010-09-19 02:33:57 +00002272 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2273 {
2274 var_success = addr != NULL;
2275 if (var_success)
2276 {
2277 format_addr = *addr;
2278 calculate_format_addr_function_offset = true;
2279 }
2280 }
2281 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2282 {
2283 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2284 if (var_success)
2285 {
2286 format_addr = sc->line_entry.range.GetBaseAddress();
2287 calculate_format_addr_function_offset = true;
2288 }
2289 }
2290 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2291 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002292 StackFrame *frame = exe_ctx->GetFramePtr();
2293 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002294 if (var_success)
2295 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002296 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002297 calculate_format_addr_function_offset = true;
2298 }
2299 }
2300 }
2301 }
2302 break;
2303
2304 case 'l':
2305 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2306 {
2307 if (sc && sc->line_entry.IsValid())
2308 {
2309 var_name_begin += ::strlen ("line.");
2310 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2311 {
2312 var_name_begin += ::strlen ("file.");
2313
2314 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2315 {
2316 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2317 var_success = format_file_spec;
2318 }
2319 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2320 {
2321 format_file_spec = sc->line_entry.file;
2322 var_success = format_file_spec;
2323 }
2324 }
2325 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2326 {
2327 var_success = true;
2328 s.Printf("%u", sc->line_entry.line);
2329 }
2330 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2331 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2332 {
2333 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2334 if (var_success)
2335 {
2336 format_addr = sc->line_entry.range.GetBaseAddress();
2337 if (var_name_begin[0] == 'e')
2338 format_addr.Slide (sc->line_entry.range.GetByteSize());
2339 }
2340 }
2341 }
2342 }
2343 break;
2344 }
2345
2346 if (var_success)
2347 {
2348 // If format addr is valid, then we need to print an address
2349 if (reg_num != LLDB_INVALID_REGNUM)
2350 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002351 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002352 // We have a register value to display...
2353 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2354 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002355 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002356 }
2357 else
2358 {
2359 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002360 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002361
2362 if (reg_ctx)
2363 {
2364 if (reg_kind != kNumRegisterKinds)
2365 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2366 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2367 var_success = reg_info != NULL;
2368 }
2369 }
2370 }
2371
2372 if (reg_info != NULL)
2373 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002374 RegisterValue reg_value;
2375 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2376 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002377 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002378 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002379 }
2380 }
2381
2382 if (format_file_spec)
2383 {
2384 s << format_file_spec;
2385 }
2386
2387 // If format addr is valid, then we need to print an address
2388 if (format_addr.IsValid())
2389 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002390 var_success = false;
2391
Greg Clayton1b654882010-09-19 02:33:57 +00002392 if (calculate_format_addr_function_offset)
2393 {
2394 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002395
Greg Clayton0603aa92010-10-04 01:05:56 +00002396 if (sc)
2397 {
2398 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002399 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002400 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002401 if (sc->block)
2402 {
2403 // Check to make sure we aren't in an inline
2404 // function. If we are, use the inline block
2405 // range that contains "format_addr" since
2406 // blocks can be discontiguous.
2407 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2408 AddressRange inline_range;
2409 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2410 func_addr = inline_range.GetBaseAddress();
2411 }
2412 }
Greg Claytone7612132012-03-07 21:03:09 +00002413 else if (sc->symbol && sc->symbol->ValueIsAddress())
2414 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002415 }
2416
2417 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002418 {
2419 if (func_addr.GetSection() == format_addr.GetSection())
2420 {
2421 addr_t func_file_addr = func_addr.GetFileAddress();
2422 addr_t addr_file_addr = format_addr.GetFileAddress();
2423 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002424 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002425 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002426 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002427 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002428 }
2429 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002430 {
2431 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2432 if (target)
2433 {
2434 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2435 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2436 if (addr_load_addr > func_load_addr)
2437 s.Printf(" + %llu", addr_load_addr - func_load_addr);
2438 else if (addr_load_addr < func_load_addr)
2439 s.Printf(" - %llu", func_load_addr - addr_load_addr);
2440 var_success = true;
2441 }
2442 }
Greg Clayton1b654882010-09-19 02:33:57 +00002443 }
2444 }
2445 else
2446 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002447 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002448 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002449 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2450 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002451 if (vaddr == LLDB_INVALID_ADDRESS)
2452 vaddr = format_addr.GetFileAddress ();
2453
2454 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002455 {
Greg Clayton514487e2011-02-15 21:59:32 +00002456 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002457 if (addr_width == 0)
2458 addr_width = 16;
2459 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002460 var_success = true;
2461 }
Greg Clayton1b654882010-09-19 02:33:57 +00002462 }
2463 }
2464 }
2465
2466 if (var_success == false)
2467 success = false;
2468 }
2469 p = var_name_end;
2470 }
2471 else
2472 break;
2473 }
2474 else
2475 {
2476 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2477 s.PutChar(*p);
2478 }
2479 }
2480 else if (*p == '\\')
2481 {
2482 ++p; // skip the slash
2483 switch (*p)
2484 {
2485 case 'a': s.PutChar ('\a'); break;
2486 case 'b': s.PutChar ('\b'); break;
2487 case 'f': s.PutChar ('\f'); break;
2488 case 'n': s.PutChar ('\n'); break;
2489 case 'r': s.PutChar ('\r'); break;
2490 case 't': s.PutChar ('\t'); break;
2491 case 'v': s.PutChar ('\v'); break;
2492 case '\'': s.PutChar ('\''); break;
2493 case '\\': s.PutChar ('\\'); break;
2494 case '0':
2495 // 1 to 3 octal chars
2496 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002497 // Make a string that can hold onto the initial zero char,
2498 // up to 3 octal digits, and a terminating NULL.
2499 char oct_str[5] = { 0, 0, 0, 0, 0 };
2500
2501 int i;
2502 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2503 oct_str[i] = p[i];
2504
2505 // We don't want to consume the last octal character since
2506 // the main for loop will do this for us, so we advance p by
2507 // one less than i (even if i is zero)
2508 p += i - 1;
2509 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2510 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002511 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002512 char octal_char = octal_value;
2513 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00002514 }
Greg Clayton1b654882010-09-19 02:33:57 +00002515 }
2516 break;
2517
2518 case 'x':
2519 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002520 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002521 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002522 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002523
Greg Clayton0603aa92010-10-04 01:05:56 +00002524 // Make a string that can hold onto two hex chars plus a
2525 // NULL terminator
2526 char hex_str[3] = { 0,0,0 };
2527 hex_str[0] = *p;
2528 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002529 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002530 ++p; // Skip the first of the two hex chars
2531 hex_str[1] = *p;
2532 }
2533
2534 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2535 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002536 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002537 }
2538 else
2539 {
2540 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002541 }
2542 break;
2543
2544 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002545 // Just desensitize any other character by just printing what
2546 // came after the '\'
2547 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002548 break;
2549
2550 }
2551
2552 }
2553 }
2554 if (end)
2555 *end = p;
2556 return success;
2557}
2558
Jim Ingham228063c2012-02-21 02:23:08 +00002559void
2560Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2561{
Jim Ingham4f02b222012-02-22 22:49:20 +00002562 // For simplicity's sake, I am not going to deal with how to close down any
2563 // open logging streams, I just redirect everything from here on out to the
2564 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002565 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2566}
2567
2568bool
2569Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2570{
2571 Log::Callbacks log_callbacks;
2572
2573 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002574 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002575 {
2576 log_stream_sp = m_log_callback_stream_sp;
2577 // For now when using the callback mode you always get thread & timestamp.
2578 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2579 }
2580 else if (log_file == NULL || *log_file == '\0')
2581 {
2582 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2583 }
2584 else
2585 {
2586 LogStreamMap::iterator pos = m_log_streams.find(log_file);
2587 if (pos == m_log_streams.end())
2588 {
2589 log_stream_sp.reset (new StreamFile (log_file));
2590 m_log_streams[log_file] = log_stream_sp;
2591 }
2592 else
2593 log_stream_sp = pos->second;
2594 }
2595 assert (log_stream_sp.get());
2596
2597 if (log_options == 0)
2598 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2599
2600 if (Log::GetLogChannelCallbacks (channel, log_callbacks))
2601 {
2602 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2603 return true;
2604 }
2605 else
2606 {
2607 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2608 if (log_channel_sp)
2609 {
2610 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2611 {
2612 return true;
2613 }
2614 else
2615 {
2616 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2617 return false;
2618 }
2619 }
2620 else
2621 {
2622 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2623 return false;
2624 }
2625 }
2626 return false;
2627}
2628