blob: 673b077fc620431c8c91673b4778fae9c33611b4 [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 Claytone8cd0c92012-10-19 18:02:49 +000025#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000026#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000028#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000029#include "lldb/Core/StreamCallback.h"
Greg Clayton1b654882010-09-19 02:33:57 +000030#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000032#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000033#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000034#include "lldb/Host/DynamicLibrary.h"
Greg Claytona3406612011-02-07 23:24:47 +000035#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000036#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000037#include "lldb/Interpreter/OptionValueSInt64.h"
38#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000039#include "lldb/Symbol/ClangASTContext.h"
40#include "lldb/Symbol/CompileUnit.h"
41#include "lldb/Symbol/Function.h"
42#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000043#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "lldb/Target/TargetList.h"
45#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000046#include "lldb/Target/RegisterContext.h"
47#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000049#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
51using namespace lldb;
52using namespace lldb_private;
53
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
Greg Clayton1b654882010-09-19 02:33:57 +000055static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000056static lldb::user_id_t g_unique_id = 1;
57
Greg Clayton1b654882010-09-19 02:33:57 +000058#pragma mark Static Functions
59
60static Mutex &
61GetDebuggerListMutex ()
62{
63 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
64 return g_mutex;
65}
66
67typedef std::vector<DebuggerSP> DebuggerList;
68
69static DebuggerList &
70GetDebuggerList()
71{
72 // hide the static debugger list inside a singleton accessor to avoid
73 // global init contructors
74 static DebuggerList g_list;
75 return g_list;
76}
Greg Claytone372b982011-11-21 21:44:34 +000077
78OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000079g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000080{
Greg Clayton67cc0632012-08-22 17:17:09 +000081 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
82 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
83 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000084 { 0, NULL, NULL }
85};
86
Greg Clayton67cc0632012-08-22 17:17:09 +000087OptionEnumValueElement
88g_language_enumerators[] =
89{
90 { eScriptLanguageNone, "none", "Disable scripting languages."},
91 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
92 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000093 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000094};
Greg Claytone372b982011-11-21 21:44:34 +000095
Greg Clayton67cc0632012-08-22 17:17:09 +000096#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
97#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
98
99#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
100 "{, ${frame.pc}}"\
101 MODULE_WITH_FUNC\
102 FILE_AND_LINE\
103 "{, stop reason = ${thread.stop-reason}}"\
104 "{\\nReturn value: ${thread.return-value}}"\
105 "\\n"
106
107#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
108 MODULE_WITH_FUNC\
109 FILE_AND_LINE\
110 "\\n"
111
112
113
Greg Clayton754a9362012-08-23 00:22:02 +0000114static PropertyDefinition
115g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000116{
117{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
118{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
119{ "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 +0000120{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000121{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
122{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
123{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
124{ "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." },
125{ "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." },
126{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
127{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
128{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Greg Claytone8cd0c92012-10-19 18:02:49 +0000129
130 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000131};
132
133enum
134{
135 ePropertyAutoConfirm = 0,
136 ePropertyFrameFormat,
137 ePropertyNotiftVoid,
138 ePropertyPrompt,
139 ePropertyScriptLanguage,
140 ePropertyStopDisassemblyCount,
141 ePropertyStopDisassemblyDisplay,
142 ePropertyStopLineCountAfter,
143 ePropertyStopLineCountBefore,
144 ePropertyTerminalWidth,
145 ePropertyThreadFormat,
146 ePropertyUseExternalEditor
147};
148
149//
150//const char *
151//Debugger::GetFrameFormat() const
152//{
153// return m_properties_sp->GetFrameFormat();
154//}
155//const char *
156//Debugger::GetThreadFormat() const
157//{
158// return m_properties_sp->GetThreadFormat();
159//}
160//
Greg Clayton4c054102012-09-01 00:38:36 +0000161
162
163Error
164Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
165 VarSetOperationType op,
166 const char *property_path,
167 const char *value)
168{
169 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
170 if (error.Success())
171 {
172 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
173 {
174 const char *new_prompt = GetPrompt();
175 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
176 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
177 }
178 }
179 return error;
180}
181
Greg Clayton67cc0632012-08-22 17:17:09 +0000182bool
183Debugger::GetAutoConfirm () const
184{
185 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000186 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000187}
188
189const char *
190Debugger::GetFrameFormat() const
191{
192 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000193 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000194}
195
196bool
197Debugger::GetNotifyVoid () const
198{
199 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000200 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000201}
202
203const char *
204Debugger::GetPrompt() const
205{
206 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000207 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000208}
209
210void
211Debugger::SetPrompt(const char *p)
212{
213 const uint32_t idx = ePropertyPrompt;
214 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
215 const char *new_prompt = GetPrompt();
216 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
217 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
218}
219
220const char *
221Debugger::GetThreadFormat() const
222{
223 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000224 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000225}
226
227lldb::ScriptLanguage
228Debugger::GetScriptLanguage() const
229{
230 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000231 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000232}
233
234bool
235Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
236{
237 const uint32_t idx = ePropertyScriptLanguage;
238 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
239}
240
241uint32_t
242Debugger::GetTerminalWidth () const
243{
244 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000245 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000246}
247
248bool
249Debugger::SetTerminalWidth (uint32_t term_width)
250{
251 const uint32_t idx = ePropertyTerminalWidth;
252 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
253}
254
255bool
256Debugger::GetUseExternalEditor () const
257{
258 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000259 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000260}
261
262bool
263Debugger::SetUseExternalEditor (bool b)
264{
265 const uint32_t idx = ePropertyUseExternalEditor;
266 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
267}
268
269uint32_t
270Debugger::GetStopSourceLineCount (bool before) const
271{
272 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000273 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000274}
275
276Debugger::StopDisassemblyType
277Debugger::GetStopDisassemblyDisplay () const
278{
279 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000280 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000281}
282
283uint32_t
284Debugger::GetDisassemblyLineCount () const
285{
286 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000287 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000288}
Greg Claytone372b982011-11-21 21:44:34 +0000289
Greg Clayton1b654882010-09-19 02:33:57 +0000290#pragma mark Debugger
291
Greg Clayton67cc0632012-08-22 17:17:09 +0000292//const DebuggerPropertiesSP &
293//Debugger::GetSettings() const
294//{
295// return m_properties_sp;
296//}
297//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000298
Caroline Tice2f88aad2011-01-14 00:29:16 +0000299int
300Debugger::TestDebuggerRefCount ()
301{
302 return g_shared_debugger_refcount;
303}
304
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305void
306Debugger::Initialize ()
307{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000308 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000309 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310}
311
312void
313Debugger::Terminate ()
314{
Greg Clayton66111032010-06-23 01:19:29 +0000315 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 {
Greg Clayton66111032010-06-23 01:19:29 +0000317 g_shared_debugger_refcount--;
318 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319 {
Greg Claytondbe54502010-11-19 03:46:01 +0000320 lldb_private::WillTerminate();
321 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000322
323 // Clear our master list of debugger objects
324 Mutex::Locker locker (GetDebuggerListMutex ());
325 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 }
328}
329
Caroline Tice20bd37f2011-03-10 22:14:10 +0000330void
331Debugger::SettingsInitialize ()
332{
Greg Clayton6920b522012-08-22 18:39:03 +0000333 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000334}
335
336void
337Debugger::SettingsTerminate ()
338{
Greg Clayton6920b522012-08-22 18:39:03 +0000339 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000340}
341
Enrico Granata21dfcd92012-09-28 23:57:51 +0000342bool
343Debugger::LoadPlugin (const FileSpec& spec)
344{
345 lldb::DynamicLibrarySP dynlib_sp(new lldb_private::DynamicLibrary(spec));
346 lldb::DebuggerSP debugger_sp(shared_from_this());
347 lldb::SBDebugger debugger_sb(debugger_sp);
348 // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
349 LLDBCommandPluginInit init_func = dynlib_sp->GetSymbol<LLDBCommandPluginInit>("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
350 if (!init_func)
351 return false;
352 if (init_func(debugger_sb))
353 {
354 m_loaded_plugins.push_back(dynlib_sp);
355 return true;
356 }
357 return false;
358}
359
360static FileSpec::EnumerateDirectoryResult
361LoadPluginCallback
362(
363 void *baton,
364 FileSpec::FileType file_type,
365 const FileSpec &file_spec
366 )
367{
368 Error error;
369
370 static ConstString g_dylibext("dylib");
371
372 if (!baton)
373 return FileSpec::eEnumerateDirectoryResultQuit;
374
375 Debugger *debugger = (Debugger*)baton;
376
377 // If we have a regular file, a symbolic link or unknown file type, try
378 // and process the file. We must handle unknown as sometimes the directory
379 // enumeration might be enumerating a file system that doesn't have correct
380 // file type information.
381 if (file_type == FileSpec::eFileTypeRegular ||
382 file_type == FileSpec::eFileTypeSymbolicLink ||
383 file_type == FileSpec::eFileTypeUnknown )
384 {
385 FileSpec plugin_file_spec (file_spec);
386 plugin_file_spec.ResolvePath ();
387
388 if (plugin_file_spec.GetFileNameExtension() != g_dylibext)
389 return FileSpec::eEnumerateDirectoryResultNext;
390
391 debugger->LoadPlugin (plugin_file_spec);
392
393 return FileSpec::eEnumerateDirectoryResultNext;
394 }
395
396 else if (file_type == FileSpec::eFileTypeUnknown ||
397 file_type == FileSpec::eFileTypeDirectory ||
398 file_type == FileSpec::eFileTypeSymbolicLink )
399 {
400 // Try and recurse into anything that a directory or symbolic link.
401 // We must also do this for unknown as sometimes the directory enumeration
402 // might be enurating a file system that doesn't have correct file type
403 // information.
404 return FileSpec::eEnumerateDirectoryResultEnter;
405 }
406
407 return FileSpec::eEnumerateDirectoryResultNext;
408}
409
410void
411Debugger::InstanceInitialize ()
412{
413 FileSpec dir_spec;
414 const bool find_directories = true;
415 const bool find_files = true;
416 const bool find_other = true;
417 char dir_path[PATH_MAX];
418 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
419 {
420 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
421 {
422 FileSpec::EnumerateDirectory (dir_path,
423 find_directories,
424 find_files,
425 find_other,
426 LoadPluginCallback,
427 this);
428 }
429 }
430
431 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
432 {
433 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
434 {
435 FileSpec::EnumerateDirectory (dir_path,
436 find_directories,
437 find_files,
438 find_other,
439 LoadPluginCallback,
440 this);
441 }
442 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000443
444 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000445}
446
Greg Clayton66111032010-06-23 01:19:29 +0000447DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000448Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000449{
Jim Ingham228063c2012-02-21 02:23:08 +0000450 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000451 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000452 {
453 Mutex::Locker locker (GetDebuggerListMutex ());
454 GetDebuggerList().push_back(debugger_sp);
455 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000456 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000457 return debugger_sp;
458}
459
Caroline Ticee02657b2011-01-22 01:02:07 +0000460void
Greg Clayton4d122c42011-09-17 08:33:22 +0000461Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000462{
463 if (debugger_sp.get() == NULL)
464 return;
465
Jim Ingham8314c522011-09-15 21:36:42 +0000466 debugger_sp->Clear();
467
Greg Claytonc15f55e2012-03-30 20:53:46 +0000468 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000469 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000470 Mutex::Locker locker (GetDebuggerListMutex ());
471 DebuggerList &debugger_list = GetDebuggerList ();
472 DebuggerList::iterator pos, end = debugger_list.end();
473 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000474 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000475 if ((*pos).get() == debugger_sp.get())
476 {
477 debugger_list.erase (pos);
478 return;
479 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000480 }
481 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000482}
483
Greg Clayton4d122c42011-09-17 08:33:22 +0000484DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000485Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
486{
Greg Clayton4d122c42011-09-17 08:33:22 +0000487 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000488 if (g_shared_debugger_refcount > 0)
489 {
490 Mutex::Locker locker (GetDebuggerListMutex ());
491 DebuggerList &debugger_list = GetDebuggerList();
492 DebuggerList::iterator pos, end = debugger_list.end();
493
494 for (pos = debugger_list.begin(); pos != end; ++pos)
495 {
496 if ((*pos).get()->m_instance_name == instance_name)
497 {
498 debugger_sp = *pos;
499 break;
500 }
501 }
502 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000503 return debugger_sp;
504}
Greg Clayton66111032010-06-23 01:19:29 +0000505
506TargetSP
507Debugger::FindTargetWithProcessID (lldb::pid_t pid)
508{
Greg Clayton4d122c42011-09-17 08:33:22 +0000509 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000510 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000511 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000512 Mutex::Locker locker (GetDebuggerListMutex ());
513 DebuggerList &debugger_list = GetDebuggerList();
514 DebuggerList::iterator pos, end = debugger_list.end();
515 for (pos = debugger_list.begin(); pos != end; ++pos)
516 {
517 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
518 if (target_sp)
519 break;
520 }
Greg Clayton66111032010-06-23 01:19:29 +0000521 }
522 return target_sp;
523}
524
Greg Claytone4e45922011-11-16 05:37:56 +0000525TargetSP
526Debugger::FindTargetWithProcess (Process *process)
527{
528 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000529 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000530 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000531 Mutex::Locker locker (GetDebuggerListMutex ());
532 DebuggerList &debugger_list = GetDebuggerList();
533 DebuggerList::iterator pos, end = debugger_list.end();
534 for (pos = debugger_list.begin(); pos != end; ++pos)
535 {
536 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
537 if (target_sp)
538 break;
539 }
Greg Claytone4e45922011-11-16 05:37:56 +0000540 }
541 return target_sp;
542}
543
Jim Ingham228063c2012-02-21 02:23:08 +0000544Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000545 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000546 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000547 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548 m_input_file (),
549 m_output_file (),
550 m_error_file (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000551 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000552 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 m_listener ("lldb.Debugger"),
Jim Inghame37d6052011-09-13 00:29:56 +0000554 m_source_manager(*this),
555 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000556 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000557 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000558 m_input_reader_data (),
559 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560{
Greg Clayton67cc0632012-08-22 17:17:09 +0000561 char instance_cstr[256];
562 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
563 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000564 if (log_callback)
565 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000566 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000567 // Always add our default platform to the platform list
568 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
569 assert (default_platform_sp.get());
570 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000571
Greg Clayton754a9362012-08-23 00:22:02 +0000572 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000573 m_collection_sp->AppendProperty (ConstString("target"),
574 ConstString("Settings specify to debugging targets."),
575 true,
576 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000577 if (m_command_interpreter_ap.get())
578 {
579 m_collection_sp->AppendProperty (ConstString("interpreter"),
580 ConstString("Settings specify to the debugger's command interpreter."),
581 true,
582 m_command_interpreter_ap->GetValueProperties());
583 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000584 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
585 term_width->SetMinimumValue(10);
586 term_width->SetMaximumValue(1024);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
589Debugger::~Debugger ()
590{
Jim Ingham8314c522011-09-15 21:36:42 +0000591 Clear();
592}
593
594void
595Debugger::Clear()
596{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000597 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000598 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000599 int num_targets = m_target_list.GetNumTargets();
600 for (int i = 0; i < num_targets; i++)
601 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000602 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
603 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000604 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000605 ProcessSP process_sp (target_sp->GetProcessSP());
606 if (process_sp)
607 {
608 if (process_sp->GetShouldDetach())
609 process_sp->Detach();
610 }
611 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000612 }
Greg Clayton66111032010-06-23 01:19:29 +0000613 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000614 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000615
616 // Close the input file _before_ we close the input read communications class
617 // as it does NOT own the input file, our m_input_file does.
618 GetInputFile().Close ();
619 // Now that we have closed m_input_file, we can now tell our input communication
620 // class to close down. Its read thread should quickly exit after we close
621 // the input file handle above.
622 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000623}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624
625bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000626Debugger::GetCloseInputOnEOF () const
627{
628 return m_input_comm.GetCloseOnEOF();
629}
630
631void
632Debugger::SetCloseInputOnEOF (bool b)
633{
634 m_input_comm.SetCloseOnEOF(b);
635}
636
637bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638Debugger::GetAsyncExecution ()
639{
Greg Clayton66111032010-06-23 01:19:29 +0000640 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641}
642
643void
644Debugger::SetAsyncExecution (bool async_execution)
645{
Greg Clayton66111032010-06-23 01:19:29 +0000646 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647}
648
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649
650void
651Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
652{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000653 File &in_file = GetInputFile();
654 in_file.SetStream (fh, tranfer_ownership);
655 if (in_file.IsValid() == false)
656 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657
658 // Disconnect from any old connection if we had one
659 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000660 // Pass false as the second argument to ConnectionFileDescriptor below because
661 // our "in_file" above will already take ownership if requested and we don't
662 // want to objects trying to own and close a file descriptor.
663 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
665
666 Error error;
667 if (m_input_comm.StartReadThread (&error) == false)
668 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000669 File &err_file = GetErrorFile();
670
671 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
672 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674}
675
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676void
677Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
678{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000679 File &out_file = GetOutputFile();
680 out_file.SetStream (fh, tranfer_ownership);
681 if (out_file.IsValid() == false)
682 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000683
684 GetCommandInterpreter().GetScriptInterpreter()->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685}
686
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687void
688Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
689{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000690 File &err_file = GetErrorFile();
691 err_file.SetStream (fh, tranfer_ownership);
692 if (err_file.IsValid() == false)
693 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694}
695
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000697Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698{
699 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000700 TargetSP target_sp(GetSelectedTarget());
701 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702
703 if (target_sp)
704 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000705 ProcessSP process_sp (target_sp->GetProcessSP());
706 exe_ctx.SetProcessSP (process_sp);
707 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000709 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
710 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000712 exe_ctx.SetThreadSP (thread_sp);
713 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
714 if (exe_ctx.GetFramePtr() == NULL)
715 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 }
717 }
718 }
719 return exe_ctx;
720
721}
722
Caroline Ticeb44880c2011-02-10 01:15:13 +0000723InputReaderSP
724Debugger::GetCurrentInputReader ()
725{
726 InputReaderSP reader_sp;
727
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000728 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000729 {
730 // Clear any finished readers from the stack
731 while (CheckIfTopInputReaderIsDone()) ;
732
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000733 if (!m_input_reader_stack.IsEmpty())
734 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000735 }
736
737 return reader_sp;
738}
739
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740void
741Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
742{
Caroline Ticeefed6132010-11-19 20:47:54 +0000743 if (bytes_len > 0)
744 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
745 else
746 ((Debugger *)baton)->DispatchInputEndOfFile ();
747}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748
749
750void
751Debugger::DispatchInput (const char *bytes, size_t bytes_len)
752{
Caroline Ticeefed6132010-11-19 20:47:54 +0000753 if (bytes == NULL || bytes_len == 0)
754 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755
756 WriteToDefaultReader (bytes, bytes_len);
757}
758
759void
Caroline Ticeefed6132010-11-19 20:47:54 +0000760Debugger::DispatchInputInterrupt ()
761{
762 m_input_reader_data.clear();
763
Caroline Ticeb44880c2011-02-10 01:15:13 +0000764 InputReaderSP reader_sp (GetCurrentInputReader ());
765 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000766 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000767 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000768
Caroline Ticeb44880c2011-02-10 01:15:13 +0000769 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000770 while (CheckIfTopInputReaderIsDone ()) ;
771 }
772}
773
774void
775Debugger::DispatchInputEndOfFile ()
776{
777 m_input_reader_data.clear();
778
Caroline Ticeb44880c2011-02-10 01:15:13 +0000779 InputReaderSP reader_sp (GetCurrentInputReader ());
780 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000781 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000782 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000783
Caroline Ticeb44880c2011-02-10 01:15:13 +0000784 // 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 +0000785 while (CheckIfTopInputReaderIsDone ()) ;
786 }
787}
788
789void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000790Debugger::CleanUpInputReaders ()
791{
792 m_input_reader_data.clear();
793
Caroline Ticeb44880c2011-02-10 01:15:13 +0000794 // 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 +0000795 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000796 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000797 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000798 if (reader_sp)
799 {
800 reader_sp->Notify (eInputReaderEndOfFile);
801 reader_sp->SetIsDone (true);
802 }
803 }
804}
805
806void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000807Debugger::NotifyTopInputReader (InputReaderAction notification)
808{
809 InputReaderSP reader_sp (GetCurrentInputReader());
810 if (reader_sp)
811 {
812 reader_sp->Notify (notification);
813
814 // Flush out any input readers that are done.
815 while (CheckIfTopInputReaderIsDone ())
816 /* Do nothing. */;
817 }
818}
819
Caroline Tice9088b062011-05-09 23:06:58 +0000820bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000821Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000822{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000823 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000824
Caroline Ticed61c10b2011-06-16 16:27:19 +0000825 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000826}
827
828
Caroline Tice969ed3d2011-05-02 20:41:46 +0000829void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000830Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
831{
832 if (bytes && bytes_len)
833 m_input_reader_data.append (bytes, bytes_len);
834
835 if (m_input_reader_data.empty())
836 return;
837
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000838 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000841 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842 if (!reader_sp)
843 break;
844
Greg Clayton471b31c2010-07-20 22:52:08 +0000845 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846 m_input_reader_data.size());
847 if (bytes_handled)
848 {
849 m_input_reader_data.erase (0, bytes_handled);
850 }
851 else
852 {
853 // No bytes were handled, we might not have reached our
854 // granularity, just return and wait for more data
855 break;
856 }
857 }
858
Caroline Ticeb44880c2011-02-10 01:15:13 +0000859 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 while (CheckIfTopInputReaderIsDone ())
861 /* Do nothing. */;
862
863}
864
865void
866Debugger::PushInputReader (const InputReaderSP& reader_sp)
867{
868 if (!reader_sp)
869 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000870
871 // Deactivate the old top reader
872 InputReaderSP top_reader_sp (GetCurrentInputReader ());
873
874 if (top_reader_sp)
875 top_reader_sp->Notify (eInputReaderDeactivate);
876
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000877 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878 reader_sp->Notify (eInputReaderActivate);
879 ActivateInputReader (reader_sp);
880}
881
882bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000883Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884{
885 bool result = false;
886
887 // The reader on the stop of the stack is done, so let the next
888 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000889 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000891 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000892 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893
894 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
895 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000896 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897 reader_sp->Notify (eInputReaderDeactivate);
898 reader_sp->Notify (eInputReaderDone);
899 result = true;
900
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000901 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000903 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904 if (reader_sp)
905 {
906 ActivateInputReader (reader_sp);
907 reader_sp->Notify (eInputReaderReactivate);
908 }
909 }
910 }
911 }
912 return result;
913}
914
915bool
916Debugger::CheckIfTopInputReaderIsDone ()
917{
918 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000919 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000920 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000921 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000922 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923
924 if (reader_sp && reader_sp->IsDone())
925 {
926 result = true;
927 PopInputReader (reader_sp);
928 }
929 }
930 return result;
931}
932
933void
934Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
935{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000936 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000937
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000938 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000940 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000941
942 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943
Greg Claytona3406612011-02-07 23:24:47 +0000944 switch (reader_sp->GetGranularity())
945 {
946 case eInputReaderGranularityByte:
947 case eInputReaderGranularityWord:
948 tty.SetCanonical (false);
949 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950
Greg Claytona3406612011-02-07 23:24:47 +0000951 case eInputReaderGranularityLine:
952 case eInputReaderGranularityAll:
953 tty.SetCanonical (true);
954 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955
Greg Claytona3406612011-02-07 23:24:47 +0000956 default:
957 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958 }
959 }
960}
Greg Clayton66111032010-06-23 01:19:29 +0000961
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000962StreamSP
963Debugger::GetAsyncOutputStream ()
964{
965 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
966 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
967}
968
969StreamSP
970Debugger::GetAsyncErrorStream ()
971{
972 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
973 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
974}
975
Enrico Granata061858c2012-02-15 02:34:21 +0000976uint32_t
977Debugger::GetNumDebuggers()
978{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000979 if (g_shared_debugger_refcount > 0)
980 {
981 Mutex::Locker locker (GetDebuggerListMutex ());
982 return GetDebuggerList().size();
983 }
984 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +0000985}
986
987lldb::DebuggerSP
988Debugger::GetDebuggerAtIndex (uint32_t index)
989{
990 DebuggerSP debugger_sp;
991
Greg Claytonc15f55e2012-03-30 20:53:46 +0000992 if (g_shared_debugger_refcount > 0)
993 {
994 Mutex::Locker locker (GetDebuggerListMutex ());
995 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +0000996
Greg Claytonc15f55e2012-03-30 20:53:46 +0000997 if (index < debugger_list.size())
998 debugger_sp = debugger_list[index];
999 }
1000
Enrico Granata061858c2012-02-15 02:34:21 +00001001 return debugger_sp;
1002}
1003
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001004DebuggerSP
1005Debugger::FindDebuggerWithID (lldb::user_id_t id)
1006{
Greg Clayton4d122c42011-09-17 08:33:22 +00001007 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001008
Greg Claytonc15f55e2012-03-30 20:53:46 +00001009 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001010 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001011 Mutex::Locker locker (GetDebuggerListMutex ());
1012 DebuggerList &debugger_list = GetDebuggerList();
1013 DebuggerList::iterator pos, end = debugger_list.end();
1014 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001015 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001016 if ((*pos).get()->GetID() == id)
1017 {
1018 debugger_sp = *pos;
1019 break;
1020 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001021 }
1022 }
1023 return debugger_sp;
1024}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001025
Greg Clayton1b654882010-09-19 02:33:57 +00001026static void
1027TestPromptFormats (StackFrame *frame)
1028{
1029 if (frame == NULL)
1030 return;
1031
1032 StreamString s;
1033 const char *prompt_format =
1034 "{addr = '${addr}'\n}"
1035 "{process.id = '${process.id}'\n}"
1036 "{process.name = '${process.name}'\n}"
1037 "{process.file.basename = '${process.file.basename}'\n}"
1038 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1039 "{thread.id = '${thread.id}'\n}"
1040 "{thread.index = '${thread.index}'\n}"
1041 "{thread.name = '${thread.name}'\n}"
1042 "{thread.queue = '${thread.queue}'\n}"
1043 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1044 "{target.arch = '${target.arch}'\n}"
1045 "{module.file.basename = '${module.file.basename}'\n}"
1046 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1047 "{file.basename = '${file.basename}'\n}"
1048 "{file.fullpath = '${file.fullpath}'\n}"
1049 "{frame.index = '${frame.index}'\n}"
1050 "{frame.pc = '${frame.pc}'\n}"
1051 "{frame.sp = '${frame.sp}'\n}"
1052 "{frame.fp = '${frame.fp}'\n}"
1053 "{frame.flags = '${frame.flags}'\n}"
1054 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1055 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1056 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1057 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1058 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1059 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1060 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1061 "{function.id = '${function.id}'\n}"
1062 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001063 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001064 "{function.addr-offset = '${function.addr-offset}'\n}"
1065 "{function.line-offset = '${function.line-offset}'\n}"
1066 "{function.pc-offset = '${function.pc-offset}'\n}"
1067 "{line.file.basename = '${line.file.basename}'\n}"
1068 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1069 "{line.number = '${line.number}'\n}"
1070 "{line.start-addr = '${line.start-addr}'\n}"
1071 "{line.end-addr = '${line.end-addr}'\n}"
1072;
1073
1074 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1075 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001076 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +00001077 const char *end = NULL;
1078 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
1079 {
1080 printf("%s\n", s.GetData());
1081 }
1082 else
1083 {
1084 printf ("error: at '%s'\n", end);
1085 printf ("what we got: %s\n", s.GetData());
1086 }
1087}
1088
Enrico Granata9fc19442011-07-06 02:13:41 +00001089static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001090ScanFormatDescriptor (const char* var_name_begin,
1091 const char* var_name_end,
1092 const char** var_name_final,
1093 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001094 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001095 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001096{
Enrico Granatae992a082011-07-22 17:03:19 +00001097 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001098 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001099 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001100 {
1101 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001102 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001103 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001104 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001105 else
1106 {
1107 *var_name_final = *percent_position;
1108 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
1109 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001110 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001111 log->Printf("ScanFormatDescriptor] parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001112 if ( !FormatManager::GetFormatFromCString(format_name,
1113 true,
1114 *custom_format) )
1115 {
Enrico Granatae992a082011-07-22 17:03:19 +00001116 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001117 log->Printf("ScanFormatDescriptor] %s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001118 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +00001119 if (*format_name == '@')
Enrico Granata86cc9822012-03-19 22:58:49 +00001120 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
Enrico Granata9fc19442011-07-06 02:13:41 +00001121 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +00001122 else if (*format_name == 'V')
Enrico Granata86cc9822012-03-19 22:58:49 +00001123 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatad55546b2011-07-22 00:16:08 +00001124 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +00001125 else if (*format_name == 'L')
Enrico Granata86cc9822012-03-19 22:58:49 +00001126 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +00001127 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +00001128 else if (*format_name == 'S')
Enrico Granata86cc9822012-03-19 22:58:49 +00001129 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Enrico Granata5dfd49c2011-08-04 02:34:29 +00001130 else if (*format_name == '#')
Enrico Granata86cc9822012-03-19 22:58:49 +00001131 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001132 else if (*format_name == 'T')
Enrico Granata86cc9822012-03-19 22:58:49 +00001133 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
Enrico Granatae992a082011-07-22 17:03:19 +00001134 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001135 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001136 }
1137 // a good custom format tells us to print the value using it
1138 else
Enrico Granatae992a082011-07-22 17:03:19 +00001139 {
1140 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001141 log->Printf("ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001142 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001143 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001144 delete format_name;
1145 }
Enrico Granatae992a082011-07-22 17:03:19 +00001146 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001147 log->Printf("ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001148 *custom_format,
1149 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001150 return true;
1151}
1152
1153static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001154ScanBracketedRange (const char* var_name_begin,
1155 const char* var_name_end,
1156 const char* var_name_final,
1157 const char** open_bracket_position,
1158 const char** separator_position,
1159 const char** close_bracket_position,
1160 const char** var_name_final_if_array_range,
1161 int64_t* index_lower,
1162 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001163{
Enrico Granatae992a082011-07-22 17:03:19 +00001164 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001165 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001166 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001167 {
1168 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1169 *close_bracket_position = ::strchr(*open_bracket_position,']');
1170 // as usual, we assume that [] will come before %
1171 //printf("trying to expand a []\n");
1172 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001173 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001174 {
Enrico Granatae992a082011-07-22 17:03:19 +00001175 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001176 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001177 *index_lower = 0;
1178 }
1179 else if (*separator_position == NULL || *separator_position > var_name_end)
1180 {
1181 char *end = NULL;
1182 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1183 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001184 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001185 log->Printf("[ScanBracketedRange] [%lld] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001186 }
Greg Clayton34132752011-07-06 04:07:21 +00001187 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001188 {
1189 char *end = NULL;
1190 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1191 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001192 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001193 log->Printf("[ScanBracketedRange] [%lld-%lld] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001194 }
1195 else
Enrico Granatae992a082011-07-22 17:03:19 +00001196 {
1197 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001198 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001199 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001200 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001201 if (*index_lower > *index_higher && *index_higher > 0)
1202 {
Enrico Granatae992a082011-07-22 17:03:19 +00001203 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001204 log->Printf("[ScanBracketedRange] swapping indices");
Enrico Granata9fc19442011-07-06 02:13:41 +00001205 int temp = *index_lower;
1206 *index_lower = *index_higher;
1207 *index_higher = temp;
1208 }
1209 }
Enrico Granatae992a082011-07-22 17:03:19 +00001210 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001211 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001212 return true;
1213}
1214
Enrico Granata9fc19442011-07-06 02:13:41 +00001215static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001216ExpandIndexedExpression (ValueObject* valobj,
1217 uint32_t index,
1218 StackFrame* frame,
1219 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001220{
Enrico Granatae992a082011-07-22 17:03:19 +00001221 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001222 const char* ptr_deref_format = "[%d]";
1223 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
1224 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001225 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001226 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001227 const char* first_unparsed;
1228 ValueObject::GetValueForExpressionPathOptions options;
1229 ValueObject::ExpressionPathEndResultType final_value_type;
1230 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001231 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatac482a192011-08-17 22:13:59 +00001232 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001233 &first_unparsed,
1234 &reason_to_stop,
1235 &final_value_type,
1236 options,
1237 &what_next);
1238 if (!item)
1239 {
Enrico Granatae992a082011-07-22 17:03:19 +00001240 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001241 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001242 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001243 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001244 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001245 else
1246 {
Enrico Granatae992a082011-07-22 17:03:19 +00001247 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001248 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001249 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001250 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001251 }
1252 return item;
1253}
1254
Greg Clayton1b654882010-09-19 02:33:57 +00001255bool
1256Debugger::FormatPrompt
1257(
1258 const char *format,
1259 const SymbolContext *sc,
1260 const ExecutionContext *exe_ctx,
1261 const Address *addr,
1262 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001263 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001264 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001265)
1266{
Enrico Granatac482a192011-08-17 22:13:59 +00001267 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001268 bool success = true;
1269 const char *p;
Enrico Granatae992a082011-07-22 17:03:19 +00001270 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001271 for (p = format; *p != '\0'; ++p)
1272 {
Enrico Granatac482a192011-08-17 22:13:59 +00001273 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001274 {
Enrico Granatac482a192011-08-17 22:13:59 +00001275 valobj = realvalobj;
1276 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001277 }
Greg Clayton1b654882010-09-19 02:33:57 +00001278 size_t non_special_chars = ::strcspn (p, "${}\\");
1279 if (non_special_chars > 0)
1280 {
1281 if (success)
1282 s.Write (p, non_special_chars);
1283 p += non_special_chars;
1284 }
1285
1286 if (*p == '\0')
1287 {
1288 break;
1289 }
1290 else if (*p == '{')
1291 {
1292 // Start a new scope that must have everything it needs if it is to
1293 // to make it into the final output stream "s". If you want to make
1294 // a format that only prints out the function or symbol name if there
1295 // is one in the symbol context you can use:
1296 // "{function =${function.name}}"
1297 // The first '{' starts a new scope that end with the matching '}' at
1298 // the end of the string. The contents "function =${function.name}"
1299 // will then be evaluated and only be output if there is a function
1300 // or symbol with a valid name.
1301 StreamString sub_strm;
1302
1303 ++p; // Skip the '{'
1304
Enrico Granatac482a192011-08-17 22:13:59 +00001305 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001306 {
1307 // The stream had all it needed
1308 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1309 }
1310 if (*p != '}')
1311 {
1312 success = false;
1313 break;
1314 }
1315 }
1316 else if (*p == '}')
1317 {
1318 // End of a enclosing scope
1319 break;
1320 }
1321 else if (*p == '$')
1322 {
1323 // We have a prompt variable to print
1324 ++p;
1325 if (*p == '{')
1326 {
1327 ++p;
1328 const char *var_name_begin = p;
1329 const char *var_name_end = ::strchr (p, '}');
1330
1331 if (var_name_end && var_name_begin < var_name_end)
1332 {
1333 // if we have already failed to parse, skip this variable
1334 if (success)
1335 {
1336 const char *cstr = NULL;
1337 Address format_addr;
1338 bool calculate_format_addr_function_offset = false;
1339 // Set reg_kind and reg_num to invalid values
1340 RegisterKind reg_kind = kNumRegisterKinds;
1341 uint32_t reg_num = LLDB_INVALID_REGNUM;
1342 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001343 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001344 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001345 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001346 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1347 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001348
Greg Clayton1b654882010-09-19 02:33:57 +00001349 // Each variable must set success to true below...
1350 bool var_success = false;
1351 switch (var_name_begin[0])
1352 {
Enrico Granata4becb372011-06-29 22:27:15 +00001353 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001354 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001355 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001356 {
Enrico Granatac482a192011-08-17 22:13:59 +00001357 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001358 break;
1359
Enrico Granatac3e320a2011-08-02 17:27:39 +00001360 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001361 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001362
Enrico Granata6f3533f2011-07-29 19:53:35 +00001363 // check for *var and *svar
1364 if (*var_name_begin == '*')
1365 {
1366 do_deref_pointer = true;
1367 var_name_begin++;
1368 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001369
1370 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001371 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001372
Enrico Granata6f3533f2011-07-29 19:53:35 +00001373 if (*var_name_begin == 's')
1374 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001375 if (!valobj->IsSynthetic())
1376 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001377 if (!valobj)
1378 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001379 var_name_begin++;
1380 }
1381
Enrico Granatac3e320a2011-08-02 17:27:39 +00001382 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001383 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001384
Enrico Granata6f3533f2011-07-29 19:53:35 +00001385 // should be a 'v' by now
1386 if (*var_name_begin != 'v')
1387 break;
1388
Enrico Granatac3e320a2011-08-02 17:27:39 +00001389 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001390 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001391
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001392 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001393 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001394 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001395 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001396 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001397 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001398 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001399 const char* var_name_final = NULL;
1400 const char* var_name_final_if_array_range = NULL;
1401 const char* close_bracket_position = NULL;
1402 int64_t index_lower = -1;
1403 int64_t index_higher = -1;
1404 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001405 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001406 bool was_plain_var = false;
1407 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001408 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001409
Enrico Granatac482a192011-08-17 22:13:59 +00001410 if (!valobj) break;
1411 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001412 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001413 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001414 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001415 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001416 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001417 }
1418 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1419 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001420 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001421 // this is a variable with some custom format applied to it
1422 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001423 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001424 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001425 ScanFormatDescriptor (var_name_begin,
1426 var_name_end,
1427 &var_name_final,
1428 &percent_position,
1429 &custom_format,
1430 &val_obj_display);
1431 }
1432 // this is ${var.something} or multiple .something nested
1433 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1434 {
Enrico Granataa777dc22012-05-08 21:49:57 +00001435 if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1436 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001437 const char* percent_position;
1438 ScanFormatDescriptor (var_name_begin,
1439 var_name_end,
1440 &var_name_final,
1441 &percent_position,
1442 &custom_format,
1443 &val_obj_display);
1444
1445 const char* open_bracket_position;
1446 const char* separator_position;
1447 ScanBracketedRange (var_name_begin,
1448 var_name_end,
1449 var_name_final,
1450 &open_bracket_position,
1451 &separator_position,
1452 &close_bracket_position,
1453 &var_name_final_if_array_range,
1454 &index_lower,
1455 &index_higher);
1456
1457 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001458
1459 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1460 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1461 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1462
Enrico Granatae992a082011-07-22 17:03:19 +00001463 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001464 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001465
Enrico Granatac482a192011-08-17 22:13:59 +00001466 target = valobj->GetValueForExpressionPath(expr_path.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001467 &first_unparsed,
1468 &reason_to_stop,
1469 &final_value_type,
1470 options,
1471 &what_next).get();
1472
1473 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001474 {
Enrico Granatae992a082011-07-22 17:03:19 +00001475 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001476 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001477 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001478 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001479 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001480 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001481 else
1482 {
Enrico Granatae992a082011-07-22 17:03:19 +00001483 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001484 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001485 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001486 first_unparsed, reason_to_stop, final_value_type);
1487 }
Enrico Granata4becb372011-06-29 22:27:15 +00001488 }
Greg Clayton34132752011-07-06 04:07:21 +00001489 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001490 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001491
Enrico Granata86cc9822012-03-19 22:58:49 +00001492 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1493 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001494
Enrico Granata86cc9822012-03-19 22:58:49 +00001495 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001496
Enrico Granataa7187d02011-07-06 19:27:11 +00001497 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001498 {
Greg Clayton34132752011-07-06 04:07:21 +00001499 // I have not deref-ed yet, let's do it
1500 // this happens when we are not going through GetValueForVariableExpressionPath
1501 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001502 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001503 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001504 if (error.Fail())
1505 {
1506 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001507 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001508 break;
1509 }
Greg Clayton34132752011-07-06 04:07:21 +00001510 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001511 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001512
Enrico Granataa777dc22012-05-08 21:49:57 +00001513 // <rdar://problem/11338654>
1514 // we do not want to use the summary for a bitfield of type T:n
1515 // if we were originally dealing with just a T - that would get
1516 // us into an endless recursion
1517 if (target->IsBitfield() && was_var_indexed)
1518 {
1519 // TODO: check for a (T:n)-specific summary - we should still obey that
1520 StreamString bitfield_name;
1521 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1522 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1523 if (!DataVisualization::GetSummaryForType(type_sp))
1524 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1525 }
1526
Enrico Granata85933ed2011-08-18 16:38:26 +00001527 // TODO use flags for these
Enrico Granataf4efecd2011-07-12 22:56:10 +00001528 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1529 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001530 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001531
Enrico Granata86cc9822012-03-19 22:58:49 +00001532 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 +00001533 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001534 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001535 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001536 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001537
Enrico Granata86cc9822012-03-19 22:58:49 +00001538 if (target->HasSpecialPrintableRepresentation(val_obj_display,
1539 custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001540 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001541 // try to use the special cases
1542 var_success = target->DumpPrintableRepresentation(str_temp,
1543 val_obj_display,
1544 custom_format);
1545 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001546 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001547
1548 // should not happen
1549 if (!var_success)
1550 s << "<invalid usage of pointer value as object>";
1551 else
1552 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001553 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001554 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001555 }
1556 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001557 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001558 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001559 {
1560 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1561 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001562 else if (is_pointer) // if pointer, value is the address stored
1563 {
Greg Clayton23f59502012-07-17 03:23:13 +00001564 target->DumpPrintableRepresentation (s,
1565 val_obj_display,
1566 custom_format,
1567 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001568 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001569 else
1570 {
1571 s << "<invalid usage of pointer value as object>";
1572 }
1573 var_success = true;
1574 break;
1575 }
1576 }
1577
1578 // if directly trying to print ${var}, and this is an aggregate, display a nice
1579 // type @ location message
1580 if (is_aggregate && was_plain_var)
1581 {
1582 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1583 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001584 break;
1585 }
1586
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001587 // 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 +00001588 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001589 {
1590 s << "<invalid use of aggregate type>";
1591 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001592 break;
1593 }
Greg Clayton34132752011-07-06 04:07:21 +00001594
1595 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001596 {
1597 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001598 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001599 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001600 }
Greg Clayton34132752011-07-06 04:07:21 +00001601 else
Enrico Granatae992a082011-07-22 17:03:19 +00001602 {
1603 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001604 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001605 if (!is_array && !is_pointer)
1606 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001607 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001608 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001609 const char* special_directions = NULL;
1610 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001611 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1612 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001613 ConstString additional_data;
1614 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1615 special_directions_writer.Printf("${%svar%s}",
1616 do_deref_pointer ? "*" : "",
1617 additional_data.GetCString());
1618 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001619 }
1620
1621 // let us display items index_lower thru index_higher of this array
1622 s.PutChar('[');
1623 var_success = true;
1624
1625 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001626 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001627
Greg Claytoncc4d0142012-02-17 07:49:44 +00001628 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001629
Greg Clayton34132752011-07-06 04:07:21 +00001630 for (;index_lower<=index_higher;index_lower++)
1631 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001632 ValueObject* item = ExpandIndexedExpression (target,
1633 index_lower,
1634 exe_ctx->GetFramePtr(),
1635 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001636
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001637 if (!item)
1638 {
Enrico Granatae992a082011-07-22 17:03:19 +00001639 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001640 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %lld", index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001641 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001642 else
1643 {
Enrico Granatae992a082011-07-22 17:03:19 +00001644 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001645 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001646 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001647
Greg Clayton34132752011-07-06 04:07:21 +00001648 if (!special_directions)
1649 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1650 else
1651 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1652
Enrico Granata22c55d12011-08-12 02:00:06 +00001653 if (--max_num_children == 0)
1654 {
1655 s.PutCString(", ...");
1656 break;
1657 }
1658
Greg Clayton34132752011-07-06 04:07:21 +00001659 if (index_lower < index_higher)
1660 s.PutChar(',');
1661 }
1662 s.PutChar(']');
1663 }
Enrico Granata4becb372011-06-29 22:27:15 +00001664 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001665 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001666 case 'a':
1667 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1668 {
1669 if (addr && addr->IsValid())
1670 {
1671 var_success = true;
1672 format_addr = *addr;
1673 }
1674 }
Greg Clayton5a314712011-10-14 07:41:33 +00001675 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1676 {
1677 var_success = true;
1678 var_name_begin += strlen("ansi."); // Skip the "ansi."
1679 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1680 {
1681 var_name_begin += strlen("fg."); // Skip the "fg."
1682 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1683 {
1684 s.Printf ("%s%s%s",
1685 lldb_utility::ansi::k_escape_start,
1686 lldb_utility::ansi::k_fg_black,
1687 lldb_utility::ansi::k_escape_end);
1688 }
1689 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1690 {
1691 s.Printf ("%s%s%s",
1692 lldb_utility::ansi::k_escape_start,
1693 lldb_utility::ansi::k_fg_red,
1694 lldb_utility::ansi::k_escape_end);
1695 }
1696 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1697 {
1698 s.Printf ("%s%s%s",
1699 lldb_utility::ansi::k_escape_start,
1700 lldb_utility::ansi::k_fg_green,
1701 lldb_utility::ansi::k_escape_end);
1702 }
1703 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1704 {
1705 s.Printf ("%s%s%s",
1706 lldb_utility::ansi::k_escape_start,
1707 lldb_utility::ansi::k_fg_yellow,
1708 lldb_utility::ansi::k_escape_end);
1709 }
1710 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1711 {
1712 s.Printf ("%s%s%s",
1713 lldb_utility::ansi::k_escape_start,
1714 lldb_utility::ansi::k_fg_blue,
1715 lldb_utility::ansi::k_escape_end);
1716 }
1717 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1718 {
1719 s.Printf ("%s%s%s",
1720 lldb_utility::ansi::k_escape_start,
1721 lldb_utility::ansi::k_fg_purple,
1722 lldb_utility::ansi::k_escape_end);
1723 }
1724 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1725 {
1726 s.Printf ("%s%s%s",
1727 lldb_utility::ansi::k_escape_start,
1728 lldb_utility::ansi::k_fg_cyan,
1729 lldb_utility::ansi::k_escape_end);
1730 }
1731 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1732 {
1733 s.Printf ("%s%s%s",
1734 lldb_utility::ansi::k_escape_start,
1735 lldb_utility::ansi::k_fg_white,
1736 lldb_utility::ansi::k_escape_end);
1737 }
1738 else
1739 {
1740 var_success = false;
1741 }
1742 }
1743 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1744 {
1745 var_name_begin += strlen("bg."); // Skip the "bg."
1746 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1747 {
1748 s.Printf ("%s%s%s",
1749 lldb_utility::ansi::k_escape_start,
1750 lldb_utility::ansi::k_bg_black,
1751 lldb_utility::ansi::k_escape_end);
1752 }
1753 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1754 {
1755 s.Printf ("%s%s%s",
1756 lldb_utility::ansi::k_escape_start,
1757 lldb_utility::ansi::k_bg_red,
1758 lldb_utility::ansi::k_escape_end);
1759 }
1760 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1761 {
1762 s.Printf ("%s%s%s",
1763 lldb_utility::ansi::k_escape_start,
1764 lldb_utility::ansi::k_bg_green,
1765 lldb_utility::ansi::k_escape_end);
1766 }
1767 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1768 {
1769 s.Printf ("%s%s%s",
1770 lldb_utility::ansi::k_escape_start,
1771 lldb_utility::ansi::k_bg_yellow,
1772 lldb_utility::ansi::k_escape_end);
1773 }
1774 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1775 {
1776 s.Printf ("%s%s%s",
1777 lldb_utility::ansi::k_escape_start,
1778 lldb_utility::ansi::k_bg_blue,
1779 lldb_utility::ansi::k_escape_end);
1780 }
1781 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1782 {
1783 s.Printf ("%s%s%s",
1784 lldb_utility::ansi::k_escape_start,
1785 lldb_utility::ansi::k_bg_purple,
1786 lldb_utility::ansi::k_escape_end);
1787 }
1788 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1789 {
1790 s.Printf ("%s%s%s",
1791 lldb_utility::ansi::k_escape_start,
1792 lldb_utility::ansi::k_bg_cyan,
1793 lldb_utility::ansi::k_escape_end);
1794 }
1795 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1796 {
1797 s.Printf ("%s%s%s",
1798 lldb_utility::ansi::k_escape_start,
1799 lldb_utility::ansi::k_bg_white,
1800 lldb_utility::ansi::k_escape_end);
1801 }
1802 else
1803 {
1804 var_success = false;
1805 }
1806 }
1807 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1808 {
1809 s.Printf ("%s%s%s",
1810 lldb_utility::ansi::k_escape_start,
1811 lldb_utility::ansi::k_ctrl_normal,
1812 lldb_utility::ansi::k_escape_end);
1813 }
1814 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1815 {
1816 s.Printf ("%s%s%s",
1817 lldb_utility::ansi::k_escape_start,
1818 lldb_utility::ansi::k_ctrl_bold,
1819 lldb_utility::ansi::k_escape_end);
1820 }
1821 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1822 {
1823 s.Printf ("%s%s%s",
1824 lldb_utility::ansi::k_escape_start,
1825 lldb_utility::ansi::k_ctrl_faint,
1826 lldb_utility::ansi::k_escape_end);
1827 }
1828 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1829 {
1830 s.Printf ("%s%s%s",
1831 lldb_utility::ansi::k_escape_start,
1832 lldb_utility::ansi::k_ctrl_italic,
1833 lldb_utility::ansi::k_escape_end);
1834 }
1835 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1836 {
1837 s.Printf ("%s%s%s",
1838 lldb_utility::ansi::k_escape_start,
1839 lldb_utility::ansi::k_ctrl_underline,
1840 lldb_utility::ansi::k_escape_end);
1841 }
1842 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1843 {
1844 s.Printf ("%s%s%s",
1845 lldb_utility::ansi::k_escape_start,
1846 lldb_utility::ansi::k_ctrl_slow_blink,
1847 lldb_utility::ansi::k_escape_end);
1848 }
1849 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1850 {
1851 s.Printf ("%s%s%s",
1852 lldb_utility::ansi::k_escape_start,
1853 lldb_utility::ansi::k_ctrl_fast_blink,
1854 lldb_utility::ansi::k_escape_end);
1855 }
1856 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1857 {
1858 s.Printf ("%s%s%s",
1859 lldb_utility::ansi::k_escape_start,
1860 lldb_utility::ansi::k_ctrl_negative,
1861 lldb_utility::ansi::k_escape_end);
1862 }
1863 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1864 {
1865 s.Printf ("%s%s%s",
1866 lldb_utility::ansi::k_escape_start,
1867 lldb_utility::ansi::k_ctrl_conceal,
1868 lldb_utility::ansi::k_escape_end);
1869
1870 }
1871 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1872 {
1873 s.Printf ("%s%s%s",
1874 lldb_utility::ansi::k_escape_start,
1875 lldb_utility::ansi::k_ctrl_crossed_out,
1876 lldb_utility::ansi::k_escape_end);
1877 }
1878 else
1879 {
1880 var_success = false;
1881 }
1882 }
Greg Clayton1b654882010-09-19 02:33:57 +00001883 break;
1884
1885 case 'p':
1886 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1887 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001888 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001889 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001890 Process *process = exe_ctx->GetProcessPtr();
1891 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001892 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001893 var_name_begin += ::strlen ("process.");
1894 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001895 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001896 s.Printf("%llu", process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001897 var_success = true;
1898 }
1899 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1900 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1901 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1902 {
1903 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1904 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001905 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001906 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1907 {
1908 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1909 var_success = format_file_spec;
1910 }
1911 else
1912 {
1913 format_file_spec = exe_module->GetFileSpec();
1914 var_success = format_file_spec;
1915 }
Greg Clayton1b654882010-09-19 02:33:57 +00001916 }
1917 }
1918 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001919 }
Greg Clayton1b654882010-09-19 02:33:57 +00001920 }
1921 break;
1922
1923 case 't':
1924 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1925 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001926 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001927 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001928 Thread *thread = exe_ctx->GetThreadPtr();
1929 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001930 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001931 var_name_begin += ::strlen ("thread.");
1932 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001933 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001934 s.Printf("0x%4.4llx", thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001935 var_success = true;
1936 }
1937 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1938 {
1939 s.Printf("%u", thread->GetIndexID());
1940 var_success = true;
1941 }
1942 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1943 {
1944 cstr = thread->GetName();
1945 var_success = cstr && cstr[0];
1946 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001947 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001948 }
1949 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1950 {
1951 cstr = thread->GetQueueName();
1952 var_success = cstr && cstr[0];
1953 if (var_success)
1954 s.PutCString(cstr);
1955 }
1956 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1957 {
1958 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001959 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001960 {
1961 cstr = stop_info_sp->GetDescription();
1962 if (cstr && cstr[0])
1963 {
1964 s.PutCString(cstr);
1965 var_success = true;
1966 }
Greg Clayton1b654882010-09-19 02:33:57 +00001967 }
1968 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001969 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1970 {
1971 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001972 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001973 {
1974 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1975 if (return_valobj_sp)
1976 {
Jim Inghamef651602011-12-22 19:12:40 +00001977 ValueObject::DumpValueObjectOptions dump_options;
1978 ValueObject::DumpValueObject (s, return_valobj_sp.get(), dump_options);
1979 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001980 }
1981 }
1982 }
Greg Clayton1b654882010-09-19 02:33:57 +00001983 }
1984 }
1985 }
1986 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1987 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001988 // TODO: hookup properties
1989// if (!target_properties_sp)
1990// {
1991// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1992// if (target)
1993// target_properties_sp = target->GetProperties();
1994// }
1995//
1996// if (target_properties_sp)
1997// {
1998// var_name_begin += ::strlen ("target.");
1999// const char *end_property = strchr(var_name_begin, '}');
2000// if (end_property)
2001// {
2002// ConstString property_name(var_name_begin, end_property - var_name_begin);
2003// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2004// if (!property_value.empty())
2005// {
2006// s.PutCString (property_value.c_str());
2007// var_success = true;
2008// }
2009// }
2010// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002011 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2012 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002013 {
Greg Clayton1b654882010-09-19 02:33:57 +00002014 var_name_begin += ::strlen ("target.");
2015 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
2016 {
2017 ArchSpec arch (target->GetArchitecture ());
2018 if (arch.IsValid())
2019 {
Greg Clayton64195a22011-02-23 00:35:02 +00002020 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002021 var_success = true;
2022 }
2023 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002024 }
Greg Clayton1b654882010-09-19 02:33:57 +00002025 }
2026 break;
2027
2028
2029 case 'm':
2030 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
2031 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002032 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002033 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002034 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002035 var_name_begin += ::strlen ("module.");
2036
2037 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2038 {
2039 if (module->GetFileSpec())
2040 {
2041 var_name_begin += ::strlen ("file.");
2042
2043 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2044 {
2045 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2046 var_success = format_file_spec;
2047 }
2048 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2049 {
2050 format_file_spec = module->GetFileSpec();
2051 var_success = format_file_spec;
2052 }
2053 }
2054 }
2055 }
2056 }
2057 break;
2058
2059
2060 case 'f':
2061 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2062 {
2063 if (sc && sc->comp_unit != NULL)
2064 {
2065 var_name_begin += ::strlen ("file.");
2066
2067 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2068 {
2069 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2070 var_success = format_file_spec;
2071 }
2072 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2073 {
2074 format_file_spec = *sc->comp_unit;
2075 var_success = format_file_spec;
2076 }
2077 }
2078 }
2079 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
2080 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002081 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002082 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002083 StackFrame *frame = exe_ctx->GetFramePtr();
2084 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002085 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002086 var_name_begin += ::strlen ("frame.");
2087 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00002088 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002089 s.Printf("%u", frame->GetFrameIndex());
2090 var_success = true;
2091 }
2092 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
2093 {
2094 reg_kind = eRegisterKindGeneric;
2095 reg_num = LLDB_REGNUM_GENERIC_PC;
2096 var_success = true;
2097 }
2098 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
2099 {
2100 reg_kind = eRegisterKindGeneric;
2101 reg_num = LLDB_REGNUM_GENERIC_SP;
2102 var_success = true;
2103 }
2104 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
2105 {
2106 reg_kind = eRegisterKindGeneric;
2107 reg_num = LLDB_REGNUM_GENERIC_FP;
2108 var_success = true;
2109 }
2110 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
2111 {
2112 reg_kind = eRegisterKindGeneric;
2113 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2114 var_success = true;
2115 }
2116 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
2117 {
2118 reg_ctx = frame->GetRegisterContext().get();
2119 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002120 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002121 var_name_begin += ::strlen ("reg.");
2122 if (var_name_begin < var_name_end)
2123 {
2124 std::string reg_name (var_name_begin, var_name_end);
2125 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2126 if (reg_info)
2127 var_success = true;
2128 }
Greg Clayton1b654882010-09-19 02:33:57 +00002129 }
2130 }
2131 }
2132 }
2133 }
2134 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
2135 {
2136 if (sc && (sc->function != NULL || sc->symbol != NULL))
2137 {
2138 var_name_begin += ::strlen ("function.");
2139 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2140 {
2141 if (sc->function)
Greg Clayton81c22f62011-10-19 18:09:39 +00002142 s.Printf("function{0x%8.8llx}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002143 else
2144 s.Printf("symbol[%u]", sc->symbol->GetID());
2145
2146 var_success = true;
2147 }
2148 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2149 {
2150 if (sc->function)
2151 cstr = sc->function->GetName().AsCString (NULL);
2152 else if (sc->symbol)
2153 cstr = sc->symbol->GetName().AsCString (NULL);
2154 if (cstr)
2155 {
2156 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002157
2158 if (sc->block)
2159 {
2160 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2161 if (inline_block)
2162 {
2163 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2164 if (inline_info)
2165 {
2166 s.PutCString(" [inlined] ");
2167 inline_info->GetName().Dump(&s);
2168 }
2169 }
2170 }
Greg Clayton1b654882010-09-19 02:33:57 +00002171 var_success = true;
2172 }
2173 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002174 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2175 {
2176 // Print the function name with arguments in it
2177
2178 if (sc->function)
2179 {
2180 var_success = true;
2181 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2182 cstr = sc->function->GetName().AsCString (NULL);
2183 if (cstr)
2184 {
2185 const InlineFunctionInfo *inline_info = NULL;
2186 VariableListSP variable_list_sp;
2187 bool get_function_vars = true;
2188 if (sc->block)
2189 {
2190 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2191
2192 if (inline_block)
2193 {
2194 get_function_vars = false;
2195 inline_info = sc->block->GetInlinedFunctionInfo();
2196 if (inline_info)
2197 variable_list_sp = inline_block->GetBlockVariableList (true);
2198 }
2199 }
2200
2201 if (get_function_vars)
2202 {
2203 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2204 }
2205
2206 if (inline_info)
2207 {
2208 s.PutCString (cstr);
2209 s.PutCString (" [inlined] ");
2210 cstr = inline_info->GetName().GetCString();
2211 }
2212
2213 VariableList args;
2214 if (variable_list_sp)
2215 {
2216 const size_t num_variables = variable_list_sp->GetSize();
2217 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
2218 {
2219 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
2220 if (var_sp->GetScope() == eValueTypeVariableArgument)
2221 args.AddVariable (var_sp);
2222 }
2223
2224 }
2225 if (args.GetSize() > 0)
2226 {
2227 const char *open_paren = strchr (cstr, '(');
2228 const char *close_paren = NULL;
2229 if (open_paren)
2230 close_paren = strchr (open_paren, ')');
2231
2232 if (open_paren)
2233 s.Write(cstr, open_paren - cstr + 1);
2234 else
2235 {
2236 s.PutCString (cstr);
2237 s.PutChar ('(');
2238 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002239 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002240 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2241 {
2242 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2243 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2244 const char *var_name = var_value_sp->GetName().GetCString();
2245 const char *var_value = var_value_sp->GetValueAsCString();
2246 if (var_value_sp->GetError().Success())
2247 {
2248 if (arg_idx > 0)
2249 s.PutCString (", ");
2250 s.Printf ("%s=%s", var_name, var_value);
2251 }
2252 }
2253
2254 if (close_paren)
2255 s.PutCString (close_paren);
2256 else
2257 s.PutChar(')');
2258
2259 }
2260 else
2261 {
2262 s.PutCString(cstr);
2263 }
2264 }
2265 }
2266 else if (sc->symbol)
2267 {
2268 cstr = sc->symbol->GetName().AsCString (NULL);
2269 if (cstr)
2270 {
2271 s.PutCString(cstr);
2272 var_success = true;
2273 }
2274 }
2275 }
Greg Clayton1b654882010-09-19 02:33:57 +00002276 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2277 {
2278 var_success = addr != NULL;
2279 if (var_success)
2280 {
2281 format_addr = *addr;
2282 calculate_format_addr_function_offset = true;
2283 }
2284 }
2285 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2286 {
2287 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2288 if (var_success)
2289 {
2290 format_addr = sc->line_entry.range.GetBaseAddress();
2291 calculate_format_addr_function_offset = true;
2292 }
2293 }
2294 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2295 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002296 StackFrame *frame = exe_ctx->GetFramePtr();
2297 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002298 if (var_success)
2299 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002300 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002301 calculate_format_addr_function_offset = true;
2302 }
2303 }
2304 }
2305 }
2306 break;
2307
2308 case 'l':
2309 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2310 {
2311 if (sc && sc->line_entry.IsValid())
2312 {
2313 var_name_begin += ::strlen ("line.");
2314 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2315 {
2316 var_name_begin += ::strlen ("file.");
2317
2318 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2319 {
2320 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2321 var_success = format_file_spec;
2322 }
2323 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2324 {
2325 format_file_spec = sc->line_entry.file;
2326 var_success = format_file_spec;
2327 }
2328 }
2329 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2330 {
2331 var_success = true;
2332 s.Printf("%u", sc->line_entry.line);
2333 }
2334 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2335 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2336 {
2337 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2338 if (var_success)
2339 {
2340 format_addr = sc->line_entry.range.GetBaseAddress();
2341 if (var_name_begin[0] == 'e')
2342 format_addr.Slide (sc->line_entry.range.GetByteSize());
2343 }
2344 }
2345 }
2346 }
2347 break;
2348 }
2349
2350 if (var_success)
2351 {
2352 // If format addr is valid, then we need to print an address
2353 if (reg_num != LLDB_INVALID_REGNUM)
2354 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002355 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002356 // We have a register value to display...
2357 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2358 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002359 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002360 }
2361 else
2362 {
2363 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002364 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002365
2366 if (reg_ctx)
2367 {
2368 if (reg_kind != kNumRegisterKinds)
2369 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2370 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2371 var_success = reg_info != NULL;
2372 }
2373 }
2374 }
2375
2376 if (reg_info != NULL)
2377 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002378 RegisterValue reg_value;
2379 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2380 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002381 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002382 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002383 }
2384 }
2385
2386 if (format_file_spec)
2387 {
2388 s << format_file_spec;
2389 }
2390
2391 // If format addr is valid, then we need to print an address
2392 if (format_addr.IsValid())
2393 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002394 var_success = false;
2395
Greg Clayton1b654882010-09-19 02:33:57 +00002396 if (calculate_format_addr_function_offset)
2397 {
2398 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002399
Greg Clayton0603aa92010-10-04 01:05:56 +00002400 if (sc)
2401 {
2402 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002403 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002404 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002405 if (sc->block)
2406 {
2407 // Check to make sure we aren't in an inline
2408 // function. If we are, use the inline block
2409 // range that contains "format_addr" since
2410 // blocks can be discontiguous.
2411 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2412 AddressRange inline_range;
2413 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2414 func_addr = inline_range.GetBaseAddress();
2415 }
2416 }
Greg Claytone7612132012-03-07 21:03:09 +00002417 else if (sc->symbol && sc->symbol->ValueIsAddress())
2418 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002419 }
2420
2421 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002422 {
2423 if (func_addr.GetSection() == format_addr.GetSection())
2424 {
2425 addr_t func_file_addr = func_addr.GetFileAddress();
2426 addr_t addr_file_addr = format_addr.GetFileAddress();
2427 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002428 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002429 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002430 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002431 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002432 }
2433 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002434 {
2435 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2436 if (target)
2437 {
2438 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2439 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2440 if (addr_load_addr > func_load_addr)
2441 s.Printf(" + %llu", addr_load_addr - func_load_addr);
2442 else if (addr_load_addr < func_load_addr)
2443 s.Printf(" - %llu", func_load_addr - addr_load_addr);
2444 var_success = true;
2445 }
2446 }
Greg Clayton1b654882010-09-19 02:33:57 +00002447 }
2448 }
2449 else
2450 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002451 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002452 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002453 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2454 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002455 if (vaddr == LLDB_INVALID_ADDRESS)
2456 vaddr = format_addr.GetFileAddress ();
2457
2458 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002459 {
Greg Clayton514487e2011-02-15 21:59:32 +00002460 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002461 if (addr_width == 0)
2462 addr_width = 16;
2463 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002464 var_success = true;
2465 }
Greg Clayton1b654882010-09-19 02:33:57 +00002466 }
2467 }
2468 }
2469
2470 if (var_success == false)
2471 success = false;
2472 }
2473 p = var_name_end;
2474 }
2475 else
2476 break;
2477 }
2478 else
2479 {
2480 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2481 s.PutChar(*p);
2482 }
2483 }
2484 else if (*p == '\\')
2485 {
2486 ++p; // skip the slash
2487 switch (*p)
2488 {
2489 case 'a': s.PutChar ('\a'); break;
2490 case 'b': s.PutChar ('\b'); break;
2491 case 'f': s.PutChar ('\f'); break;
2492 case 'n': s.PutChar ('\n'); break;
2493 case 'r': s.PutChar ('\r'); break;
2494 case 't': s.PutChar ('\t'); break;
2495 case 'v': s.PutChar ('\v'); break;
2496 case '\'': s.PutChar ('\''); break;
2497 case '\\': s.PutChar ('\\'); break;
2498 case '0':
2499 // 1 to 3 octal chars
2500 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002501 // Make a string that can hold onto the initial zero char,
2502 // up to 3 octal digits, and a terminating NULL.
2503 char oct_str[5] = { 0, 0, 0, 0, 0 };
2504
2505 int i;
2506 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2507 oct_str[i] = p[i];
2508
2509 // We don't want to consume the last octal character since
2510 // the main for loop will do this for us, so we advance p by
2511 // one less than i (even if i is zero)
2512 p += i - 1;
2513 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2514 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002515 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002516 char octal_char = octal_value;
2517 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00002518 }
Greg Clayton1b654882010-09-19 02:33:57 +00002519 }
2520 break;
2521
2522 case 'x':
2523 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002524 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002525 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002526 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002527
Greg Clayton0603aa92010-10-04 01:05:56 +00002528 // Make a string that can hold onto two hex chars plus a
2529 // NULL terminator
2530 char hex_str[3] = { 0,0,0 };
2531 hex_str[0] = *p;
2532 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002533 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002534 ++p; // Skip the first of the two hex chars
2535 hex_str[1] = *p;
2536 }
2537
2538 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2539 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002540 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002541 }
2542 else
2543 {
2544 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002545 }
2546 break;
2547
2548 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002549 // Just desensitize any other character by just printing what
2550 // came after the '\'
2551 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002552 break;
2553
2554 }
2555
2556 }
2557 }
2558 if (end)
2559 *end = p;
2560 return success;
2561}
2562
Jim Ingham228063c2012-02-21 02:23:08 +00002563void
2564Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2565{
Jim Ingham4f02b222012-02-22 22:49:20 +00002566 // For simplicity's sake, I am not going to deal with how to close down any
2567 // open logging streams, I just redirect everything from here on out to the
2568 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002569 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2570}
2571
2572bool
2573Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2574{
2575 Log::Callbacks log_callbacks;
2576
2577 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002578 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002579 {
2580 log_stream_sp = m_log_callback_stream_sp;
2581 // For now when using the callback mode you always get thread & timestamp.
2582 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2583 }
2584 else if (log_file == NULL || *log_file == '\0')
2585 {
2586 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2587 }
2588 else
2589 {
2590 LogStreamMap::iterator pos = m_log_streams.find(log_file);
2591 if (pos == m_log_streams.end())
2592 {
2593 log_stream_sp.reset (new StreamFile (log_file));
2594 m_log_streams[log_file] = log_stream_sp;
2595 }
2596 else
2597 log_stream_sp = pos->second;
2598 }
2599 assert (log_stream_sp.get());
2600
2601 if (log_options == 0)
2602 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2603
2604 if (Log::GetLogChannelCallbacks (channel, log_callbacks))
2605 {
2606 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2607 return true;
2608 }
2609 else
2610 {
2611 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2612 if (log_channel_sp)
2613 {
2614 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2615 {
2616 return true;
2617 }
2618 else
2619 {
2620 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2621 return false;
2622 }
2623 }
2624 else
2625 {
2626 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2627 return false;
2628 }
2629 }
2630 return false;
2631}
2632