blob: aadbf4d1da31caf62cefae182719e3eb23c340a3 [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
Enrico Granatab5887262012-10-29 21:18:03 +0000684 // do not create the ScriptInterpreter just for setting the output file handle
685 // as the constructor will know how to do the right thing on its own
686 const bool can_create = false;
687 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
688 if (script_interpreter)
689 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690}
691
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692void
693Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
694{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000695 File &err_file = GetErrorFile();
696 err_file.SetStream (fh, tranfer_ownership);
697 if (err_file.IsValid() == false)
698 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699}
700
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000702Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703{
704 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000705 TargetSP target_sp(GetSelectedTarget());
706 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707
708 if (target_sp)
709 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000710 ProcessSP process_sp (target_sp->GetProcessSP());
711 exe_ctx.SetProcessSP (process_sp);
712 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000714 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
715 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000717 exe_ctx.SetThreadSP (thread_sp);
718 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
719 if (exe_ctx.GetFramePtr() == NULL)
720 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721 }
722 }
723 }
724 return exe_ctx;
725
726}
727
Caroline Ticeb44880c2011-02-10 01:15:13 +0000728InputReaderSP
729Debugger::GetCurrentInputReader ()
730{
731 InputReaderSP reader_sp;
732
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000733 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000734 {
735 // Clear any finished readers from the stack
736 while (CheckIfTopInputReaderIsDone()) ;
737
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000738 if (!m_input_reader_stack.IsEmpty())
739 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000740 }
741
742 return reader_sp;
743}
744
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745void
746Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
747{
Caroline Ticeefed6132010-11-19 20:47:54 +0000748 if (bytes_len > 0)
749 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
750 else
751 ((Debugger *)baton)->DispatchInputEndOfFile ();
752}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753
754
755void
756Debugger::DispatchInput (const char *bytes, size_t bytes_len)
757{
Caroline Ticeefed6132010-11-19 20:47:54 +0000758 if (bytes == NULL || bytes_len == 0)
759 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760
761 WriteToDefaultReader (bytes, bytes_len);
762}
763
764void
Caroline Ticeefed6132010-11-19 20:47:54 +0000765Debugger::DispatchInputInterrupt ()
766{
767 m_input_reader_data.clear();
768
Caroline Ticeb44880c2011-02-10 01:15:13 +0000769 InputReaderSP reader_sp (GetCurrentInputReader ());
770 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000771 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000772 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000773
Caroline Ticeb44880c2011-02-10 01:15:13 +0000774 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000775 while (CheckIfTopInputReaderIsDone ()) ;
776 }
777}
778
779void
780Debugger::DispatchInputEndOfFile ()
781{
782 m_input_reader_data.clear();
783
Caroline Ticeb44880c2011-02-10 01:15:13 +0000784 InputReaderSP reader_sp (GetCurrentInputReader ());
785 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000786 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000787 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000788
Caroline Ticeb44880c2011-02-10 01:15:13 +0000789 // 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 +0000790 while (CheckIfTopInputReaderIsDone ()) ;
791 }
792}
793
794void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000795Debugger::CleanUpInputReaders ()
796{
797 m_input_reader_data.clear();
798
Caroline Ticeb44880c2011-02-10 01:15:13 +0000799 // 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 +0000800 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000801 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000802 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000803 if (reader_sp)
804 {
805 reader_sp->Notify (eInputReaderEndOfFile);
806 reader_sp->SetIsDone (true);
807 }
808 }
809}
810
811void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000812Debugger::NotifyTopInputReader (InputReaderAction notification)
813{
814 InputReaderSP reader_sp (GetCurrentInputReader());
815 if (reader_sp)
816 {
817 reader_sp->Notify (notification);
818
819 // Flush out any input readers that are done.
820 while (CheckIfTopInputReaderIsDone ())
821 /* Do nothing. */;
822 }
823}
824
Caroline Tice9088b062011-05-09 23:06:58 +0000825bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000826Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000827{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000828 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000829
Caroline Ticed61c10b2011-06-16 16:27:19 +0000830 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000831}
832
833
Caroline Tice969ed3d2011-05-02 20:41:46 +0000834void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
836{
837 if (bytes && bytes_len)
838 m_input_reader_data.append (bytes, bytes_len);
839
840 if (m_input_reader_data.empty())
841 return;
842
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000843 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000845 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000846 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 if (!reader_sp)
848 break;
849
Greg Clayton471b31c2010-07-20 22:52:08 +0000850 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851 m_input_reader_data.size());
852 if (bytes_handled)
853 {
854 m_input_reader_data.erase (0, bytes_handled);
855 }
856 else
857 {
858 // No bytes were handled, we might not have reached our
859 // granularity, just return and wait for more data
860 break;
861 }
862 }
863
Caroline Ticeb44880c2011-02-10 01:15:13 +0000864 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 while (CheckIfTopInputReaderIsDone ())
866 /* Do nothing. */;
867
868}
869
870void
871Debugger::PushInputReader (const InputReaderSP& reader_sp)
872{
873 if (!reader_sp)
874 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000875
876 // Deactivate the old top reader
877 InputReaderSP top_reader_sp (GetCurrentInputReader ());
878
879 if (top_reader_sp)
880 top_reader_sp->Notify (eInputReaderDeactivate);
881
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000882 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883 reader_sp->Notify (eInputReaderActivate);
884 ActivateInputReader (reader_sp);
885}
886
887bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000888Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889{
890 bool result = false;
891
892 // The reader on the stop of the stack is done, so let the next
893 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000894 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000896 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000897 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898
899 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
900 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000901 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 reader_sp->Notify (eInputReaderDeactivate);
903 reader_sp->Notify (eInputReaderDone);
904 result = true;
905
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000906 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000908 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909 if (reader_sp)
910 {
911 ActivateInputReader (reader_sp);
912 reader_sp->Notify (eInputReaderReactivate);
913 }
914 }
915 }
916 }
917 return result;
918}
919
920bool
921Debugger::CheckIfTopInputReaderIsDone ()
922{
923 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000924 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000926 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000927 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928
929 if (reader_sp && reader_sp->IsDone())
930 {
931 result = true;
932 PopInputReader (reader_sp);
933 }
934 }
935 return result;
936}
937
938void
939Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
940{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000941 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000943 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000945 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000946
947 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948
Greg Claytona3406612011-02-07 23:24:47 +0000949 switch (reader_sp->GetGranularity())
950 {
951 case eInputReaderGranularityByte:
952 case eInputReaderGranularityWord:
953 tty.SetCanonical (false);
954 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955
Greg Claytona3406612011-02-07 23:24:47 +0000956 case eInputReaderGranularityLine:
957 case eInputReaderGranularityAll:
958 tty.SetCanonical (true);
959 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960
Greg Claytona3406612011-02-07 23:24:47 +0000961 default:
962 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 }
964 }
965}
Greg Clayton66111032010-06-23 01:19:29 +0000966
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000967StreamSP
968Debugger::GetAsyncOutputStream ()
969{
970 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
971 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
972}
973
974StreamSP
975Debugger::GetAsyncErrorStream ()
976{
977 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
978 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
979}
980
Enrico Granata061858c2012-02-15 02:34:21 +0000981uint32_t
982Debugger::GetNumDebuggers()
983{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000984 if (g_shared_debugger_refcount > 0)
985 {
986 Mutex::Locker locker (GetDebuggerListMutex ());
987 return GetDebuggerList().size();
988 }
989 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +0000990}
991
992lldb::DebuggerSP
993Debugger::GetDebuggerAtIndex (uint32_t index)
994{
995 DebuggerSP debugger_sp;
996
Greg Claytonc15f55e2012-03-30 20:53:46 +0000997 if (g_shared_debugger_refcount > 0)
998 {
999 Mutex::Locker locker (GetDebuggerListMutex ());
1000 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001001
Greg Claytonc15f55e2012-03-30 20:53:46 +00001002 if (index < debugger_list.size())
1003 debugger_sp = debugger_list[index];
1004 }
1005
Enrico Granata061858c2012-02-15 02:34:21 +00001006 return debugger_sp;
1007}
1008
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001009DebuggerSP
1010Debugger::FindDebuggerWithID (lldb::user_id_t id)
1011{
Greg Clayton4d122c42011-09-17 08:33:22 +00001012 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001013
Greg Claytonc15f55e2012-03-30 20:53:46 +00001014 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001015 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001016 Mutex::Locker locker (GetDebuggerListMutex ());
1017 DebuggerList &debugger_list = GetDebuggerList();
1018 DebuggerList::iterator pos, end = debugger_list.end();
1019 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001020 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001021 if ((*pos).get()->GetID() == id)
1022 {
1023 debugger_sp = *pos;
1024 break;
1025 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001026 }
1027 }
1028 return debugger_sp;
1029}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001030
Greg Clayton1b654882010-09-19 02:33:57 +00001031static void
1032TestPromptFormats (StackFrame *frame)
1033{
1034 if (frame == NULL)
1035 return;
1036
1037 StreamString s;
1038 const char *prompt_format =
1039 "{addr = '${addr}'\n}"
1040 "{process.id = '${process.id}'\n}"
1041 "{process.name = '${process.name}'\n}"
1042 "{process.file.basename = '${process.file.basename}'\n}"
1043 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1044 "{thread.id = '${thread.id}'\n}"
1045 "{thread.index = '${thread.index}'\n}"
1046 "{thread.name = '${thread.name}'\n}"
1047 "{thread.queue = '${thread.queue}'\n}"
1048 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1049 "{target.arch = '${target.arch}'\n}"
1050 "{module.file.basename = '${module.file.basename}'\n}"
1051 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1052 "{file.basename = '${file.basename}'\n}"
1053 "{file.fullpath = '${file.fullpath}'\n}"
1054 "{frame.index = '${frame.index}'\n}"
1055 "{frame.pc = '${frame.pc}'\n}"
1056 "{frame.sp = '${frame.sp}'\n}"
1057 "{frame.fp = '${frame.fp}'\n}"
1058 "{frame.flags = '${frame.flags}'\n}"
1059 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1060 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1061 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1062 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1063 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1064 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1065 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1066 "{function.id = '${function.id}'\n}"
1067 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001068 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001069 "{function.addr-offset = '${function.addr-offset}'\n}"
1070 "{function.line-offset = '${function.line-offset}'\n}"
1071 "{function.pc-offset = '${function.pc-offset}'\n}"
1072 "{line.file.basename = '${line.file.basename}'\n}"
1073 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1074 "{line.number = '${line.number}'\n}"
1075 "{line.start-addr = '${line.start-addr}'\n}"
1076 "{line.end-addr = '${line.end-addr}'\n}"
1077;
1078
1079 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1080 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001081 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +00001082 const char *end = NULL;
1083 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
1084 {
1085 printf("%s\n", s.GetData());
1086 }
1087 else
1088 {
1089 printf ("error: at '%s'\n", end);
1090 printf ("what we got: %s\n", s.GetData());
1091 }
1092}
1093
Enrico Granata9fc19442011-07-06 02:13:41 +00001094static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001095ScanFormatDescriptor (const char* var_name_begin,
1096 const char* var_name_end,
1097 const char** var_name_final,
1098 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001099 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001100 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001101{
Enrico Granatae992a082011-07-22 17:03:19 +00001102 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001103 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001104 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001105 {
1106 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001107 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001108 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001109 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001110 else
1111 {
1112 *var_name_final = *percent_position;
1113 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
1114 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001115 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001116 log->Printf("ScanFormatDescriptor] parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001117 if ( !FormatManager::GetFormatFromCString(format_name,
1118 true,
1119 *custom_format) )
1120 {
Enrico Granatae992a082011-07-22 17:03:19 +00001121 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001122 log->Printf("ScanFormatDescriptor] %s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001123 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +00001124 if (*format_name == '@')
Enrico Granata86cc9822012-03-19 22:58:49 +00001125 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
Enrico Granata9fc19442011-07-06 02:13:41 +00001126 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +00001127 else if (*format_name == 'V')
Enrico Granata86cc9822012-03-19 22:58:49 +00001128 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatad55546b2011-07-22 00:16:08 +00001129 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +00001130 else if (*format_name == 'L')
Enrico Granata86cc9822012-03-19 22:58:49 +00001131 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +00001132 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +00001133 else if (*format_name == 'S')
Enrico Granata86cc9822012-03-19 22:58:49 +00001134 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Enrico Granata5dfd49c2011-08-04 02:34:29 +00001135 else if (*format_name == '#')
Enrico Granata86cc9822012-03-19 22:58:49 +00001136 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001137 else if (*format_name == 'T')
Enrico Granata86cc9822012-03-19 22:58:49 +00001138 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
Enrico Granatae992a082011-07-22 17:03:19 +00001139 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001140 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001141 }
1142 // a good custom format tells us to print the value using it
1143 else
Enrico Granatae992a082011-07-22 17:03:19 +00001144 {
1145 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001146 log->Printf("ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001147 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001148 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001149 delete format_name;
1150 }
Enrico Granatae992a082011-07-22 17:03:19 +00001151 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001152 log->Printf("ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001153 *custom_format,
1154 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001155 return true;
1156}
1157
1158static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001159ScanBracketedRange (const char* var_name_begin,
1160 const char* var_name_end,
1161 const char* var_name_final,
1162 const char** open_bracket_position,
1163 const char** separator_position,
1164 const char** close_bracket_position,
1165 const char** var_name_final_if_array_range,
1166 int64_t* index_lower,
1167 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001168{
Enrico Granatae992a082011-07-22 17:03:19 +00001169 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001170 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001171 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001172 {
1173 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1174 *close_bracket_position = ::strchr(*open_bracket_position,']');
1175 // as usual, we assume that [] will come before %
1176 //printf("trying to expand a []\n");
1177 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001178 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001179 {
Enrico Granatae992a082011-07-22 17:03:19 +00001180 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001181 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001182 *index_lower = 0;
1183 }
1184 else if (*separator_position == NULL || *separator_position > var_name_end)
1185 {
1186 char *end = NULL;
1187 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1188 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001189 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001190 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001191 }
Greg Clayton34132752011-07-06 04:07:21 +00001192 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001193 {
1194 char *end = NULL;
1195 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1196 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001197 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001198 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001199 }
1200 else
Enrico Granatae992a082011-07-22 17:03:19 +00001201 {
1202 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001203 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001204 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001205 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001206 if (*index_lower > *index_higher && *index_higher > 0)
1207 {
Enrico Granatae992a082011-07-22 17:03:19 +00001208 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001209 log->Printf("[ScanBracketedRange] swapping indices");
Enrico Granata9fc19442011-07-06 02:13:41 +00001210 int temp = *index_lower;
1211 *index_lower = *index_higher;
1212 *index_higher = temp;
1213 }
1214 }
Enrico Granatae992a082011-07-22 17:03:19 +00001215 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001216 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001217 return true;
1218}
1219
Enrico Granata9fc19442011-07-06 02:13:41 +00001220static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001221ExpandIndexedExpression (ValueObject* valobj,
1222 uint32_t index,
1223 StackFrame* frame,
1224 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001225{
Enrico Granatae992a082011-07-22 17:03:19 +00001226 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001227 const char* ptr_deref_format = "[%d]";
1228 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
1229 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001230 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001231 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001232 const char* first_unparsed;
1233 ValueObject::GetValueForExpressionPathOptions options;
1234 ValueObject::ExpressionPathEndResultType final_value_type;
1235 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001236 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatac482a192011-08-17 22:13:59 +00001237 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001238 &first_unparsed,
1239 &reason_to_stop,
1240 &final_value_type,
1241 options,
1242 &what_next);
1243 if (!item)
1244 {
Enrico Granatae992a082011-07-22 17:03:19 +00001245 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001246 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001247 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001248 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001249 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001250 else
1251 {
Enrico Granatae992a082011-07-22 17:03:19 +00001252 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001253 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001254 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001255 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001256 }
1257 return item;
1258}
1259
Greg Clayton1b654882010-09-19 02:33:57 +00001260bool
1261Debugger::FormatPrompt
1262(
1263 const char *format,
1264 const SymbolContext *sc,
1265 const ExecutionContext *exe_ctx,
1266 const Address *addr,
1267 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001268 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001269 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001270)
1271{
Enrico Granatac482a192011-08-17 22:13:59 +00001272 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001273 bool success = true;
1274 const char *p;
Enrico Granatae992a082011-07-22 17:03:19 +00001275 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001276 for (p = format; *p != '\0'; ++p)
1277 {
Enrico Granatac482a192011-08-17 22:13:59 +00001278 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001279 {
Enrico Granatac482a192011-08-17 22:13:59 +00001280 valobj = realvalobj;
1281 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001282 }
Greg Clayton1b654882010-09-19 02:33:57 +00001283 size_t non_special_chars = ::strcspn (p, "${}\\");
1284 if (non_special_chars > 0)
1285 {
1286 if (success)
1287 s.Write (p, non_special_chars);
1288 p += non_special_chars;
1289 }
1290
1291 if (*p == '\0')
1292 {
1293 break;
1294 }
1295 else if (*p == '{')
1296 {
1297 // Start a new scope that must have everything it needs if it is to
1298 // to make it into the final output stream "s". If you want to make
1299 // a format that only prints out the function or symbol name if there
1300 // is one in the symbol context you can use:
1301 // "{function =${function.name}}"
1302 // The first '{' starts a new scope that end with the matching '}' at
1303 // the end of the string. The contents "function =${function.name}"
1304 // will then be evaluated and only be output if there is a function
1305 // or symbol with a valid name.
1306 StreamString sub_strm;
1307
1308 ++p; // Skip the '{'
1309
Enrico Granatac482a192011-08-17 22:13:59 +00001310 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001311 {
1312 // The stream had all it needed
1313 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1314 }
1315 if (*p != '}')
1316 {
1317 success = false;
1318 break;
1319 }
1320 }
1321 else if (*p == '}')
1322 {
1323 // End of a enclosing scope
1324 break;
1325 }
1326 else if (*p == '$')
1327 {
1328 // We have a prompt variable to print
1329 ++p;
1330 if (*p == '{')
1331 {
1332 ++p;
1333 const char *var_name_begin = p;
1334 const char *var_name_end = ::strchr (p, '}');
1335
1336 if (var_name_end && var_name_begin < var_name_end)
1337 {
1338 // if we have already failed to parse, skip this variable
1339 if (success)
1340 {
1341 const char *cstr = NULL;
1342 Address format_addr;
1343 bool calculate_format_addr_function_offset = false;
1344 // Set reg_kind and reg_num to invalid values
1345 RegisterKind reg_kind = kNumRegisterKinds;
1346 uint32_t reg_num = LLDB_INVALID_REGNUM;
1347 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001348 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001349 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001350 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001351 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1352 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001353
Greg Clayton1b654882010-09-19 02:33:57 +00001354 // Each variable must set success to true below...
1355 bool var_success = false;
1356 switch (var_name_begin[0])
1357 {
Enrico Granata4becb372011-06-29 22:27:15 +00001358 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001359 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001360 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001361 {
Enrico Granatac482a192011-08-17 22:13:59 +00001362 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001363 break;
1364
Enrico Granatac3e320a2011-08-02 17:27:39 +00001365 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001366 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001367
Enrico Granata6f3533f2011-07-29 19:53:35 +00001368 // check for *var and *svar
1369 if (*var_name_begin == '*')
1370 {
1371 do_deref_pointer = true;
1372 var_name_begin++;
1373 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001374
1375 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001376 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001377
Enrico Granata6f3533f2011-07-29 19:53:35 +00001378 if (*var_name_begin == 's')
1379 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001380 if (!valobj->IsSynthetic())
1381 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001382 if (!valobj)
1383 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001384 var_name_begin++;
1385 }
1386
Enrico Granatac3e320a2011-08-02 17:27:39 +00001387 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001388 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001389
Enrico Granata6f3533f2011-07-29 19:53:35 +00001390 // should be a 'v' by now
1391 if (*var_name_begin != 'v')
1392 break;
1393
Enrico Granatac3e320a2011-08-02 17:27:39 +00001394 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001395 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001396
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001397 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001398 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001399 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001400 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001401 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001402 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001403 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001404 const char* var_name_final = NULL;
1405 const char* var_name_final_if_array_range = NULL;
1406 const char* close_bracket_position = NULL;
1407 int64_t index_lower = -1;
1408 int64_t index_higher = -1;
1409 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001410 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001411 bool was_plain_var = false;
1412 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001413 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001414
Enrico Granatac482a192011-08-17 22:13:59 +00001415 if (!valobj) break;
1416 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001417 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001418 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001419 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001420 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001421 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001422 }
1423 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1424 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001425 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001426 // this is a variable with some custom format applied to it
1427 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001428 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001429 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001430 ScanFormatDescriptor (var_name_begin,
1431 var_name_end,
1432 &var_name_final,
1433 &percent_position,
1434 &custom_format,
1435 &val_obj_display);
1436 }
1437 // this is ${var.something} or multiple .something nested
1438 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1439 {
Enrico Granataa777dc22012-05-08 21:49:57 +00001440 if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1441 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001442 const char* percent_position;
1443 ScanFormatDescriptor (var_name_begin,
1444 var_name_end,
1445 &var_name_final,
1446 &percent_position,
1447 &custom_format,
1448 &val_obj_display);
1449
1450 const char* open_bracket_position;
1451 const char* separator_position;
1452 ScanBracketedRange (var_name_begin,
1453 var_name_end,
1454 var_name_final,
1455 &open_bracket_position,
1456 &separator_position,
1457 &close_bracket_position,
1458 &var_name_final_if_array_range,
1459 &index_lower,
1460 &index_higher);
1461
1462 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001463
1464 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1465 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1466 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1467
Enrico Granatae992a082011-07-22 17:03:19 +00001468 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001469 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001470
Enrico Granatac482a192011-08-17 22:13:59 +00001471 target = valobj->GetValueForExpressionPath(expr_path.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001472 &first_unparsed,
1473 &reason_to_stop,
1474 &final_value_type,
1475 options,
1476 &what_next).get();
1477
1478 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001479 {
Enrico Granatae992a082011-07-22 17:03:19 +00001480 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001481 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001482 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001483 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001484 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001485 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001486 else
1487 {
Enrico Granatae992a082011-07-22 17:03:19 +00001488 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001489 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001490 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001491 first_unparsed, reason_to_stop, final_value_type);
1492 }
Enrico Granata4becb372011-06-29 22:27:15 +00001493 }
Greg Clayton34132752011-07-06 04:07:21 +00001494 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001495 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001496
Enrico Granata86cc9822012-03-19 22:58:49 +00001497 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1498 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001499
Enrico Granata86cc9822012-03-19 22:58:49 +00001500 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001501
Enrico Granataa7187d02011-07-06 19:27:11 +00001502 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001503 {
Greg Clayton34132752011-07-06 04:07:21 +00001504 // I have not deref-ed yet, let's do it
1505 // this happens when we are not going through GetValueForVariableExpressionPath
1506 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001507 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001508 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001509 if (error.Fail())
1510 {
1511 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001512 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001513 break;
1514 }
Greg Clayton34132752011-07-06 04:07:21 +00001515 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001516 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001517
Enrico Granataa777dc22012-05-08 21:49:57 +00001518 // <rdar://problem/11338654>
1519 // we do not want to use the summary for a bitfield of type T:n
1520 // if we were originally dealing with just a T - that would get
1521 // us into an endless recursion
1522 if (target->IsBitfield() && was_var_indexed)
1523 {
1524 // TODO: check for a (T:n)-specific summary - we should still obey that
1525 StreamString bitfield_name;
1526 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1527 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1528 if (!DataVisualization::GetSummaryForType(type_sp))
1529 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1530 }
1531
Enrico Granata85933ed2011-08-18 16:38:26 +00001532 // TODO use flags for these
Enrico Granataf4efecd2011-07-12 22:56:10 +00001533 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1534 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001535 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001536
Enrico Granata86cc9822012-03-19 22:58:49 +00001537 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 +00001538 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001539 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001540 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001541 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001542
Enrico Granata86cc9822012-03-19 22:58:49 +00001543 if (target->HasSpecialPrintableRepresentation(val_obj_display,
1544 custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001545 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001546 // try to use the special cases
1547 var_success = target->DumpPrintableRepresentation(str_temp,
1548 val_obj_display,
1549 custom_format);
1550 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001551 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001552
1553 // should not happen
1554 if (!var_success)
1555 s << "<invalid usage of pointer value as object>";
1556 else
1557 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001558 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001559 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001560 }
1561 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001562 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001563 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001564 {
1565 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1566 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001567 else if (is_pointer) // if pointer, value is the address stored
1568 {
Greg Clayton23f59502012-07-17 03:23:13 +00001569 target->DumpPrintableRepresentation (s,
1570 val_obj_display,
1571 custom_format,
1572 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001573 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001574 else
1575 {
1576 s << "<invalid usage of pointer value as object>";
1577 }
1578 var_success = true;
1579 break;
1580 }
1581 }
1582
1583 // if directly trying to print ${var}, and this is an aggregate, display a nice
1584 // type @ location message
1585 if (is_aggregate && was_plain_var)
1586 {
1587 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1588 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001589 break;
1590 }
1591
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001592 // 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 +00001593 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001594 {
1595 s << "<invalid use of aggregate type>";
1596 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001597 break;
1598 }
Greg Clayton34132752011-07-06 04:07:21 +00001599
1600 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001601 {
1602 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001603 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001604 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001605 }
Greg Clayton34132752011-07-06 04:07:21 +00001606 else
Enrico Granatae992a082011-07-22 17:03:19 +00001607 {
1608 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001609 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001610 if (!is_array && !is_pointer)
1611 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001612 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001613 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001614 const char* special_directions = NULL;
1615 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001616 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1617 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001618 ConstString additional_data;
1619 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1620 special_directions_writer.Printf("${%svar%s}",
1621 do_deref_pointer ? "*" : "",
1622 additional_data.GetCString());
1623 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001624 }
1625
1626 // let us display items index_lower thru index_higher of this array
1627 s.PutChar('[');
1628 var_success = true;
1629
1630 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001631 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001632
Greg Claytoncc4d0142012-02-17 07:49:44 +00001633 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001634
Greg Clayton34132752011-07-06 04:07:21 +00001635 for (;index_lower<=index_higher;index_lower++)
1636 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001637 ValueObject* item = ExpandIndexedExpression (target,
1638 index_lower,
1639 exe_ctx->GetFramePtr(),
1640 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001641
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001642 if (!item)
1643 {
Enrico Granatae992a082011-07-22 17:03:19 +00001644 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001645 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001646 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001647 else
1648 {
Enrico Granatae992a082011-07-22 17:03:19 +00001649 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001650 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001651 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001652
Greg Clayton34132752011-07-06 04:07:21 +00001653 if (!special_directions)
1654 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1655 else
1656 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1657
Enrico Granata22c55d12011-08-12 02:00:06 +00001658 if (--max_num_children == 0)
1659 {
1660 s.PutCString(", ...");
1661 break;
1662 }
1663
Greg Clayton34132752011-07-06 04:07:21 +00001664 if (index_lower < index_higher)
1665 s.PutChar(',');
1666 }
1667 s.PutChar(']');
1668 }
Enrico Granata4becb372011-06-29 22:27:15 +00001669 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001670 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001671 case 'a':
1672 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1673 {
1674 if (addr && addr->IsValid())
1675 {
1676 var_success = true;
1677 format_addr = *addr;
1678 }
1679 }
Greg Clayton5a314712011-10-14 07:41:33 +00001680 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1681 {
1682 var_success = true;
1683 var_name_begin += strlen("ansi."); // Skip the "ansi."
1684 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1685 {
1686 var_name_begin += strlen("fg."); // Skip the "fg."
1687 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1688 {
1689 s.Printf ("%s%s%s",
1690 lldb_utility::ansi::k_escape_start,
1691 lldb_utility::ansi::k_fg_black,
1692 lldb_utility::ansi::k_escape_end);
1693 }
1694 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1695 {
1696 s.Printf ("%s%s%s",
1697 lldb_utility::ansi::k_escape_start,
1698 lldb_utility::ansi::k_fg_red,
1699 lldb_utility::ansi::k_escape_end);
1700 }
1701 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1702 {
1703 s.Printf ("%s%s%s",
1704 lldb_utility::ansi::k_escape_start,
1705 lldb_utility::ansi::k_fg_green,
1706 lldb_utility::ansi::k_escape_end);
1707 }
1708 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1709 {
1710 s.Printf ("%s%s%s",
1711 lldb_utility::ansi::k_escape_start,
1712 lldb_utility::ansi::k_fg_yellow,
1713 lldb_utility::ansi::k_escape_end);
1714 }
1715 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1716 {
1717 s.Printf ("%s%s%s",
1718 lldb_utility::ansi::k_escape_start,
1719 lldb_utility::ansi::k_fg_blue,
1720 lldb_utility::ansi::k_escape_end);
1721 }
1722 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1723 {
1724 s.Printf ("%s%s%s",
1725 lldb_utility::ansi::k_escape_start,
1726 lldb_utility::ansi::k_fg_purple,
1727 lldb_utility::ansi::k_escape_end);
1728 }
1729 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1730 {
1731 s.Printf ("%s%s%s",
1732 lldb_utility::ansi::k_escape_start,
1733 lldb_utility::ansi::k_fg_cyan,
1734 lldb_utility::ansi::k_escape_end);
1735 }
1736 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1737 {
1738 s.Printf ("%s%s%s",
1739 lldb_utility::ansi::k_escape_start,
1740 lldb_utility::ansi::k_fg_white,
1741 lldb_utility::ansi::k_escape_end);
1742 }
1743 else
1744 {
1745 var_success = false;
1746 }
1747 }
1748 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1749 {
1750 var_name_begin += strlen("bg."); // Skip the "bg."
1751 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1752 {
1753 s.Printf ("%s%s%s",
1754 lldb_utility::ansi::k_escape_start,
1755 lldb_utility::ansi::k_bg_black,
1756 lldb_utility::ansi::k_escape_end);
1757 }
1758 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1759 {
1760 s.Printf ("%s%s%s",
1761 lldb_utility::ansi::k_escape_start,
1762 lldb_utility::ansi::k_bg_red,
1763 lldb_utility::ansi::k_escape_end);
1764 }
1765 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1766 {
1767 s.Printf ("%s%s%s",
1768 lldb_utility::ansi::k_escape_start,
1769 lldb_utility::ansi::k_bg_green,
1770 lldb_utility::ansi::k_escape_end);
1771 }
1772 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1773 {
1774 s.Printf ("%s%s%s",
1775 lldb_utility::ansi::k_escape_start,
1776 lldb_utility::ansi::k_bg_yellow,
1777 lldb_utility::ansi::k_escape_end);
1778 }
1779 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1780 {
1781 s.Printf ("%s%s%s",
1782 lldb_utility::ansi::k_escape_start,
1783 lldb_utility::ansi::k_bg_blue,
1784 lldb_utility::ansi::k_escape_end);
1785 }
1786 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1787 {
1788 s.Printf ("%s%s%s",
1789 lldb_utility::ansi::k_escape_start,
1790 lldb_utility::ansi::k_bg_purple,
1791 lldb_utility::ansi::k_escape_end);
1792 }
1793 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1794 {
1795 s.Printf ("%s%s%s",
1796 lldb_utility::ansi::k_escape_start,
1797 lldb_utility::ansi::k_bg_cyan,
1798 lldb_utility::ansi::k_escape_end);
1799 }
1800 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1801 {
1802 s.Printf ("%s%s%s",
1803 lldb_utility::ansi::k_escape_start,
1804 lldb_utility::ansi::k_bg_white,
1805 lldb_utility::ansi::k_escape_end);
1806 }
1807 else
1808 {
1809 var_success = false;
1810 }
1811 }
1812 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1813 {
1814 s.Printf ("%s%s%s",
1815 lldb_utility::ansi::k_escape_start,
1816 lldb_utility::ansi::k_ctrl_normal,
1817 lldb_utility::ansi::k_escape_end);
1818 }
1819 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1820 {
1821 s.Printf ("%s%s%s",
1822 lldb_utility::ansi::k_escape_start,
1823 lldb_utility::ansi::k_ctrl_bold,
1824 lldb_utility::ansi::k_escape_end);
1825 }
1826 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1827 {
1828 s.Printf ("%s%s%s",
1829 lldb_utility::ansi::k_escape_start,
1830 lldb_utility::ansi::k_ctrl_faint,
1831 lldb_utility::ansi::k_escape_end);
1832 }
1833 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1834 {
1835 s.Printf ("%s%s%s",
1836 lldb_utility::ansi::k_escape_start,
1837 lldb_utility::ansi::k_ctrl_italic,
1838 lldb_utility::ansi::k_escape_end);
1839 }
1840 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1841 {
1842 s.Printf ("%s%s%s",
1843 lldb_utility::ansi::k_escape_start,
1844 lldb_utility::ansi::k_ctrl_underline,
1845 lldb_utility::ansi::k_escape_end);
1846 }
1847 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1848 {
1849 s.Printf ("%s%s%s",
1850 lldb_utility::ansi::k_escape_start,
1851 lldb_utility::ansi::k_ctrl_slow_blink,
1852 lldb_utility::ansi::k_escape_end);
1853 }
1854 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1855 {
1856 s.Printf ("%s%s%s",
1857 lldb_utility::ansi::k_escape_start,
1858 lldb_utility::ansi::k_ctrl_fast_blink,
1859 lldb_utility::ansi::k_escape_end);
1860 }
1861 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1862 {
1863 s.Printf ("%s%s%s",
1864 lldb_utility::ansi::k_escape_start,
1865 lldb_utility::ansi::k_ctrl_negative,
1866 lldb_utility::ansi::k_escape_end);
1867 }
1868 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1869 {
1870 s.Printf ("%s%s%s",
1871 lldb_utility::ansi::k_escape_start,
1872 lldb_utility::ansi::k_ctrl_conceal,
1873 lldb_utility::ansi::k_escape_end);
1874
1875 }
1876 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1877 {
1878 s.Printf ("%s%s%s",
1879 lldb_utility::ansi::k_escape_start,
1880 lldb_utility::ansi::k_ctrl_crossed_out,
1881 lldb_utility::ansi::k_escape_end);
1882 }
1883 else
1884 {
1885 var_success = false;
1886 }
1887 }
Greg Clayton1b654882010-09-19 02:33:57 +00001888 break;
1889
1890 case 'p':
1891 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1892 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001893 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001894 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001895 Process *process = exe_ctx->GetProcessPtr();
1896 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001897 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001898 var_name_begin += ::strlen ("process.");
1899 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001900 {
Daniel Malead01b2952012-11-29 21:49:15 +00001901 s.Printf("%" PRIu64, process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001902 var_success = true;
1903 }
1904 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1905 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1906 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1907 {
1908 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1909 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001910 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001911 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1912 {
1913 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1914 var_success = format_file_spec;
1915 }
1916 else
1917 {
1918 format_file_spec = exe_module->GetFileSpec();
1919 var_success = format_file_spec;
1920 }
Greg Clayton1b654882010-09-19 02:33:57 +00001921 }
1922 }
1923 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001924 }
Greg Clayton1b654882010-09-19 02:33:57 +00001925 }
1926 break;
1927
1928 case 't':
1929 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1930 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001931 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001932 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001933 Thread *thread = exe_ctx->GetThreadPtr();
1934 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001935 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001936 var_name_begin += ::strlen ("thread.");
1937 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001938 {
Daniel Malead01b2952012-11-29 21:49:15 +00001939 s.Printf("0x%4.4" PRIx64, thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001940 var_success = true;
1941 }
1942 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1943 {
1944 s.Printf("%u", thread->GetIndexID());
1945 var_success = true;
1946 }
1947 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1948 {
1949 cstr = thread->GetName();
1950 var_success = cstr && cstr[0];
1951 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001952 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001953 }
1954 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1955 {
1956 cstr = thread->GetQueueName();
1957 var_success = cstr && cstr[0];
1958 if (var_success)
1959 s.PutCString(cstr);
1960 }
1961 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1962 {
1963 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001964 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001965 {
1966 cstr = stop_info_sp->GetDescription();
1967 if (cstr && cstr[0])
1968 {
1969 s.PutCString(cstr);
1970 var_success = true;
1971 }
Greg Clayton1b654882010-09-19 02:33:57 +00001972 }
1973 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001974 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1975 {
1976 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001977 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001978 {
1979 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1980 if (return_valobj_sp)
1981 {
Jim Inghamef651602011-12-22 19:12:40 +00001982 ValueObject::DumpValueObjectOptions dump_options;
1983 ValueObject::DumpValueObject (s, return_valobj_sp.get(), dump_options);
1984 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001985 }
1986 }
1987 }
Greg Clayton1b654882010-09-19 02:33:57 +00001988 }
1989 }
1990 }
1991 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1992 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001993 // TODO: hookup properties
1994// if (!target_properties_sp)
1995// {
1996// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1997// if (target)
1998// target_properties_sp = target->GetProperties();
1999// }
2000//
2001// if (target_properties_sp)
2002// {
2003// var_name_begin += ::strlen ("target.");
2004// const char *end_property = strchr(var_name_begin, '}');
2005// if (end_property)
2006// {
2007// ConstString property_name(var_name_begin, end_property - var_name_begin);
2008// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2009// if (!property_value.empty())
2010// {
2011// s.PutCString (property_value.c_str());
2012// var_success = true;
2013// }
2014// }
2015// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002016 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2017 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002018 {
Greg Clayton1b654882010-09-19 02:33:57 +00002019 var_name_begin += ::strlen ("target.");
2020 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
2021 {
2022 ArchSpec arch (target->GetArchitecture ());
2023 if (arch.IsValid())
2024 {
Greg Clayton64195a22011-02-23 00:35:02 +00002025 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002026 var_success = true;
2027 }
2028 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002029 }
Greg Clayton1b654882010-09-19 02:33:57 +00002030 }
2031 break;
2032
2033
2034 case 'm':
2035 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
2036 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002037 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002038 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002039 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002040 var_name_begin += ::strlen ("module.");
2041
2042 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2043 {
2044 if (module->GetFileSpec())
2045 {
2046 var_name_begin += ::strlen ("file.");
2047
2048 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2049 {
2050 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2051 var_success = format_file_spec;
2052 }
2053 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2054 {
2055 format_file_spec = module->GetFileSpec();
2056 var_success = format_file_spec;
2057 }
2058 }
2059 }
2060 }
2061 }
2062 break;
2063
2064
2065 case 'f':
2066 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2067 {
2068 if (sc && sc->comp_unit != NULL)
2069 {
2070 var_name_begin += ::strlen ("file.");
2071
2072 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2073 {
2074 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2075 var_success = format_file_spec;
2076 }
2077 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2078 {
2079 format_file_spec = *sc->comp_unit;
2080 var_success = format_file_spec;
2081 }
2082 }
2083 }
2084 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
2085 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002086 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002087 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002088 StackFrame *frame = exe_ctx->GetFramePtr();
2089 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002090 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002091 var_name_begin += ::strlen ("frame.");
2092 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00002093 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002094 s.Printf("%u", frame->GetFrameIndex());
2095 var_success = true;
2096 }
2097 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
2098 {
2099 reg_kind = eRegisterKindGeneric;
2100 reg_num = LLDB_REGNUM_GENERIC_PC;
2101 var_success = true;
2102 }
2103 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
2104 {
2105 reg_kind = eRegisterKindGeneric;
2106 reg_num = LLDB_REGNUM_GENERIC_SP;
2107 var_success = true;
2108 }
2109 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
2110 {
2111 reg_kind = eRegisterKindGeneric;
2112 reg_num = LLDB_REGNUM_GENERIC_FP;
2113 var_success = true;
2114 }
2115 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
2116 {
2117 reg_kind = eRegisterKindGeneric;
2118 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2119 var_success = true;
2120 }
2121 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
2122 {
2123 reg_ctx = frame->GetRegisterContext().get();
2124 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002125 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002126 var_name_begin += ::strlen ("reg.");
2127 if (var_name_begin < var_name_end)
2128 {
2129 std::string reg_name (var_name_begin, var_name_end);
2130 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2131 if (reg_info)
2132 var_success = true;
2133 }
Greg Clayton1b654882010-09-19 02:33:57 +00002134 }
2135 }
2136 }
2137 }
2138 }
2139 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
2140 {
2141 if (sc && (sc->function != NULL || sc->symbol != NULL))
2142 {
2143 var_name_begin += ::strlen ("function.");
2144 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2145 {
2146 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002147 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002148 else
2149 s.Printf("symbol[%u]", sc->symbol->GetID());
2150
2151 var_success = true;
2152 }
2153 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2154 {
2155 if (sc->function)
2156 cstr = sc->function->GetName().AsCString (NULL);
2157 else if (sc->symbol)
2158 cstr = sc->symbol->GetName().AsCString (NULL);
2159 if (cstr)
2160 {
2161 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002162
2163 if (sc->block)
2164 {
2165 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2166 if (inline_block)
2167 {
2168 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2169 if (inline_info)
2170 {
2171 s.PutCString(" [inlined] ");
2172 inline_info->GetName().Dump(&s);
2173 }
2174 }
2175 }
Greg Clayton1b654882010-09-19 02:33:57 +00002176 var_success = true;
2177 }
2178 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002179 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2180 {
2181 // Print the function name with arguments in it
2182
2183 if (sc->function)
2184 {
2185 var_success = true;
2186 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2187 cstr = sc->function->GetName().AsCString (NULL);
2188 if (cstr)
2189 {
2190 const InlineFunctionInfo *inline_info = NULL;
2191 VariableListSP variable_list_sp;
2192 bool get_function_vars = true;
2193 if (sc->block)
2194 {
2195 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2196
2197 if (inline_block)
2198 {
2199 get_function_vars = false;
2200 inline_info = sc->block->GetInlinedFunctionInfo();
2201 if (inline_info)
2202 variable_list_sp = inline_block->GetBlockVariableList (true);
2203 }
2204 }
2205
2206 if (get_function_vars)
2207 {
2208 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2209 }
2210
2211 if (inline_info)
2212 {
2213 s.PutCString (cstr);
2214 s.PutCString (" [inlined] ");
2215 cstr = inline_info->GetName().GetCString();
2216 }
2217
2218 VariableList args;
2219 if (variable_list_sp)
2220 {
2221 const size_t num_variables = variable_list_sp->GetSize();
2222 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
2223 {
2224 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
2225 if (var_sp->GetScope() == eValueTypeVariableArgument)
2226 args.AddVariable (var_sp);
2227 }
2228
2229 }
2230 if (args.GetSize() > 0)
2231 {
2232 const char *open_paren = strchr (cstr, '(');
2233 const char *close_paren = NULL;
2234 if (open_paren)
2235 close_paren = strchr (open_paren, ')');
2236
2237 if (open_paren)
2238 s.Write(cstr, open_paren - cstr + 1);
2239 else
2240 {
2241 s.PutCString (cstr);
2242 s.PutChar ('(');
2243 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002244 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002245 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2246 {
2247 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2248 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2249 const char *var_name = var_value_sp->GetName().GetCString();
2250 const char *var_value = var_value_sp->GetValueAsCString();
2251 if (var_value_sp->GetError().Success())
2252 {
2253 if (arg_idx > 0)
2254 s.PutCString (", ");
2255 s.Printf ("%s=%s", var_name, var_value);
2256 }
2257 }
2258
2259 if (close_paren)
2260 s.PutCString (close_paren);
2261 else
2262 s.PutChar(')');
2263
2264 }
2265 else
2266 {
2267 s.PutCString(cstr);
2268 }
2269 }
2270 }
2271 else if (sc->symbol)
2272 {
2273 cstr = sc->symbol->GetName().AsCString (NULL);
2274 if (cstr)
2275 {
2276 s.PutCString(cstr);
2277 var_success = true;
2278 }
2279 }
2280 }
Greg Clayton1b654882010-09-19 02:33:57 +00002281 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2282 {
2283 var_success = addr != NULL;
2284 if (var_success)
2285 {
2286 format_addr = *addr;
2287 calculate_format_addr_function_offset = true;
2288 }
2289 }
2290 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2291 {
2292 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2293 if (var_success)
2294 {
2295 format_addr = sc->line_entry.range.GetBaseAddress();
2296 calculate_format_addr_function_offset = true;
2297 }
2298 }
2299 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2300 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002301 StackFrame *frame = exe_ctx->GetFramePtr();
2302 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002303 if (var_success)
2304 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002305 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002306 calculate_format_addr_function_offset = true;
2307 }
2308 }
2309 }
2310 }
2311 break;
2312
2313 case 'l':
2314 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2315 {
2316 if (sc && sc->line_entry.IsValid())
2317 {
2318 var_name_begin += ::strlen ("line.");
2319 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2320 {
2321 var_name_begin += ::strlen ("file.");
2322
2323 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2324 {
2325 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2326 var_success = format_file_spec;
2327 }
2328 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2329 {
2330 format_file_spec = sc->line_entry.file;
2331 var_success = format_file_spec;
2332 }
2333 }
2334 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2335 {
2336 var_success = true;
2337 s.Printf("%u", sc->line_entry.line);
2338 }
2339 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2340 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2341 {
2342 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2343 if (var_success)
2344 {
2345 format_addr = sc->line_entry.range.GetBaseAddress();
2346 if (var_name_begin[0] == 'e')
2347 format_addr.Slide (sc->line_entry.range.GetByteSize());
2348 }
2349 }
2350 }
2351 }
2352 break;
2353 }
2354
2355 if (var_success)
2356 {
2357 // If format addr is valid, then we need to print an address
2358 if (reg_num != LLDB_INVALID_REGNUM)
2359 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002360 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002361 // We have a register value to display...
2362 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2363 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002364 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002365 }
2366 else
2367 {
2368 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002369 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002370
2371 if (reg_ctx)
2372 {
2373 if (reg_kind != kNumRegisterKinds)
2374 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2375 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2376 var_success = reg_info != NULL;
2377 }
2378 }
2379 }
2380
2381 if (reg_info != NULL)
2382 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002383 RegisterValue reg_value;
2384 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2385 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002386 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002387 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002388 }
2389 }
2390
2391 if (format_file_spec)
2392 {
2393 s << format_file_spec;
2394 }
2395
2396 // If format addr is valid, then we need to print an address
2397 if (format_addr.IsValid())
2398 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002399 var_success = false;
2400
Greg Clayton1b654882010-09-19 02:33:57 +00002401 if (calculate_format_addr_function_offset)
2402 {
2403 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002404
Greg Clayton0603aa92010-10-04 01:05:56 +00002405 if (sc)
2406 {
2407 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002408 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002409 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002410 if (sc->block)
2411 {
2412 // Check to make sure we aren't in an inline
2413 // function. If we are, use the inline block
2414 // range that contains "format_addr" since
2415 // blocks can be discontiguous.
2416 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2417 AddressRange inline_range;
2418 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2419 func_addr = inline_range.GetBaseAddress();
2420 }
2421 }
Greg Claytone7612132012-03-07 21:03:09 +00002422 else if (sc->symbol && sc->symbol->ValueIsAddress())
2423 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002424 }
2425
2426 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002427 {
2428 if (func_addr.GetSection() == format_addr.GetSection())
2429 {
2430 addr_t func_file_addr = func_addr.GetFileAddress();
2431 addr_t addr_file_addr = format_addr.GetFileAddress();
2432 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002433 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002434 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002435 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002436 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002437 }
2438 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002439 {
2440 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2441 if (target)
2442 {
2443 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2444 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2445 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002446 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002447 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002448 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002449 var_success = true;
2450 }
2451 }
Greg Clayton1b654882010-09-19 02:33:57 +00002452 }
2453 }
2454 else
2455 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002456 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002457 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002458 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2459 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002460 if (vaddr == LLDB_INVALID_ADDRESS)
2461 vaddr = format_addr.GetFileAddress ();
2462
2463 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002464 {
Greg Clayton514487e2011-02-15 21:59:32 +00002465 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002466 if (addr_width == 0)
2467 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002468 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002469 var_success = true;
2470 }
Greg Clayton1b654882010-09-19 02:33:57 +00002471 }
2472 }
2473 }
2474
2475 if (var_success == false)
2476 success = false;
2477 }
2478 p = var_name_end;
2479 }
2480 else
2481 break;
2482 }
2483 else
2484 {
2485 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2486 s.PutChar(*p);
2487 }
2488 }
2489 else if (*p == '\\')
2490 {
2491 ++p; // skip the slash
2492 switch (*p)
2493 {
2494 case 'a': s.PutChar ('\a'); break;
2495 case 'b': s.PutChar ('\b'); break;
2496 case 'f': s.PutChar ('\f'); break;
2497 case 'n': s.PutChar ('\n'); break;
2498 case 'r': s.PutChar ('\r'); break;
2499 case 't': s.PutChar ('\t'); break;
2500 case 'v': s.PutChar ('\v'); break;
2501 case '\'': s.PutChar ('\''); break;
2502 case '\\': s.PutChar ('\\'); break;
2503 case '0':
2504 // 1 to 3 octal chars
2505 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002506 // Make a string that can hold onto the initial zero char,
2507 // up to 3 octal digits, and a terminating NULL.
2508 char oct_str[5] = { 0, 0, 0, 0, 0 };
2509
2510 int i;
2511 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2512 oct_str[i] = p[i];
2513
2514 // We don't want to consume the last octal character since
2515 // the main for loop will do this for us, so we advance p by
2516 // one less than i (even if i is zero)
2517 p += i - 1;
2518 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2519 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002520 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002521 char octal_char = octal_value;
2522 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00002523 }
Greg Clayton1b654882010-09-19 02:33:57 +00002524 }
2525 break;
2526
2527 case 'x':
2528 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002529 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002530 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002531 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002532
Greg Clayton0603aa92010-10-04 01:05:56 +00002533 // Make a string that can hold onto two hex chars plus a
2534 // NULL terminator
2535 char hex_str[3] = { 0,0,0 };
2536 hex_str[0] = *p;
2537 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002538 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002539 ++p; // Skip the first of the two hex chars
2540 hex_str[1] = *p;
2541 }
2542
2543 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2544 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002545 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002546 }
2547 else
2548 {
2549 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002550 }
2551 break;
2552
2553 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002554 // Just desensitize any other character by just printing what
2555 // came after the '\'
2556 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002557 break;
2558
2559 }
2560
2561 }
2562 }
2563 if (end)
2564 *end = p;
2565 return success;
2566}
2567
Jim Ingham228063c2012-02-21 02:23:08 +00002568void
2569Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2570{
Jim Ingham4f02b222012-02-22 22:49:20 +00002571 // For simplicity's sake, I am not going to deal with how to close down any
2572 // open logging streams, I just redirect everything from here on out to the
2573 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002574 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2575}
2576
2577bool
2578Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2579{
2580 Log::Callbacks log_callbacks;
2581
2582 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002583 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002584 {
2585 log_stream_sp = m_log_callback_stream_sp;
2586 // For now when using the callback mode you always get thread & timestamp.
2587 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2588 }
2589 else if (log_file == NULL || *log_file == '\0')
2590 {
2591 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2592 }
2593 else
2594 {
2595 LogStreamMap::iterator pos = m_log_streams.find(log_file);
2596 if (pos == m_log_streams.end())
2597 {
2598 log_stream_sp.reset (new StreamFile (log_file));
2599 m_log_streams[log_file] = log_stream_sp;
2600 }
2601 else
2602 log_stream_sp = pos->second;
2603 }
2604 assert (log_stream_sp.get());
2605
2606 if (log_options == 0)
2607 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2608
2609 if (Log::GetLogChannelCallbacks (channel, log_callbacks))
2610 {
2611 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2612 return true;
2613 }
2614 else
2615 {
2616 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2617 if (log_channel_sp)
2618 {
2619 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2620 {
2621 return true;
2622 }
2623 else
2624 {
2625 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2626 return false;
2627 }
2628 }
2629 else
2630 {
2631 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2632 return false;
2633 }
2634 }
2635 return false;
2636}
2637