blob: dd8c613f2fb2c8b8d9ed14439404f7a4100520de [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Enrico Granata21dfcd92012-09-28 23:57:51 +000012#include "lldb/API/SBDebugger.h"
13
Greg Clayton4a33d312011-06-23 17:59:56 +000014#include "lldb/Core/Debugger.h"
15
16#include <map>
17
Enrico Granata4becb372011-06-29 22:27:15 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Type.h"
20
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/lldb-private.h"
22#include "lldb/Core/ConnectionFileDescriptor.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 Granata5548cb52013-01-28 23:47:25 +000034#include "lldb/DataFormatters/DataVisualization.h"
35#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000036#include "lldb/Host/DynamicLibrary.h"
Greg Claytona3406612011-02-07 23:24:47 +000037#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000038#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000039#include "lldb/Interpreter/OptionValueSInt64.h"
40#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000041#include "lldb/Symbol/ClangASTContext.h"
42#include "lldb/Symbol/CompileUnit.h"
43#include "lldb/Symbol/Function.h"
44#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000045#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Target/TargetList.h"
47#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000048#include "lldb/Target/RegisterContext.h"
49#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000051#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
53using namespace lldb;
54using namespace lldb_private;
55
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Greg Clayton1b654882010-09-19 02:33:57 +000057static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000058static lldb::user_id_t g_unique_id = 1;
59
Greg Clayton1b654882010-09-19 02:33:57 +000060#pragma mark Static Functions
61
62static Mutex &
63GetDebuggerListMutex ()
64{
65 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
66 return g_mutex;
67}
68
69typedef std::vector<DebuggerSP> DebuggerList;
70
71static DebuggerList &
72GetDebuggerList()
73{
74 // hide the static debugger list inside a singleton accessor to avoid
75 // global init contructors
76 static DebuggerList g_list;
77 return g_list;
78}
Greg Claytone372b982011-11-21 21:44:34 +000079
80OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000081g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000082{
Greg Clayton67cc0632012-08-22 17:17:09 +000083 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
84 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
85 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000086 { 0, NULL, NULL }
87};
88
Greg Clayton67cc0632012-08-22 17:17:09 +000089OptionEnumValueElement
90g_language_enumerators[] =
91{
92 { eScriptLanguageNone, "none", "Disable scripting languages."},
93 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
94 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000095 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000096};
Greg Claytone372b982011-11-21 21:44:34 +000097
Greg Clayton67cc0632012-08-22 17:17:09 +000098#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
99#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
100
101#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
102 "{, ${frame.pc}}"\
103 MODULE_WITH_FUNC\
104 FILE_AND_LINE\
105 "{, stop reason = ${thread.stop-reason}}"\
106 "{\\nReturn value: ${thread.return-value}}"\
107 "\\n"
108
109#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
110 MODULE_WITH_FUNC\
111 FILE_AND_LINE\
112 "\\n"
113
114
115
Greg Clayton754a9362012-08-23 00:22:02 +0000116static PropertyDefinition
117g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000118{
119{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
120{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
121{ "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 +0000122{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000123{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
124{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
125{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
126{ "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." },
127{ "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." },
128{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
129{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
130{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Greg Claytone8cd0c92012-10-19 18:02:49 +0000131
132 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000133};
134
135enum
136{
137 ePropertyAutoConfirm = 0,
138 ePropertyFrameFormat,
139 ePropertyNotiftVoid,
140 ePropertyPrompt,
141 ePropertyScriptLanguage,
142 ePropertyStopDisassemblyCount,
143 ePropertyStopDisassemblyDisplay,
144 ePropertyStopLineCountAfter,
145 ePropertyStopLineCountBefore,
146 ePropertyTerminalWidth,
147 ePropertyThreadFormat,
148 ePropertyUseExternalEditor
149};
150
151//
152//const char *
153//Debugger::GetFrameFormat() const
154//{
155// return m_properties_sp->GetFrameFormat();
156//}
157//const char *
158//Debugger::GetThreadFormat() const
159//{
160// return m_properties_sp->GetThreadFormat();
161//}
162//
Greg Clayton4c054102012-09-01 00:38:36 +0000163
164
165Error
166Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
167 VarSetOperationType op,
168 const char *property_path,
169 const char *value)
170{
171 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
172 if (error.Success())
173 {
174 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
175 {
176 const char *new_prompt = GetPrompt();
177 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
178 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
179 }
180 }
181 return error;
182}
183
Greg Clayton67cc0632012-08-22 17:17:09 +0000184bool
185Debugger::GetAutoConfirm () const
186{
187 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000188 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000189}
190
191const char *
192Debugger::GetFrameFormat() const
193{
194 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000195 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000196}
197
198bool
199Debugger::GetNotifyVoid () const
200{
201 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000202 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000203}
204
205const char *
206Debugger::GetPrompt() const
207{
208 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000209 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000210}
211
212void
213Debugger::SetPrompt(const char *p)
214{
215 const uint32_t idx = ePropertyPrompt;
216 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
217 const char *new_prompt = GetPrompt();
218 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
219 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
220}
221
222const char *
223Debugger::GetThreadFormat() const
224{
225 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000226 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000227}
228
229lldb::ScriptLanguage
230Debugger::GetScriptLanguage() const
231{
232 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000233 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000234}
235
236bool
237Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
238{
239 const uint32_t idx = ePropertyScriptLanguage;
240 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
241}
242
243uint32_t
244Debugger::GetTerminalWidth () const
245{
246 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000247 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000248}
249
250bool
251Debugger::SetTerminalWidth (uint32_t term_width)
252{
253 const uint32_t idx = ePropertyTerminalWidth;
254 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
255}
256
257bool
258Debugger::GetUseExternalEditor () const
259{
260 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000261 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000262}
263
264bool
265Debugger::SetUseExternalEditor (bool b)
266{
267 const uint32_t idx = ePropertyUseExternalEditor;
268 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
269}
270
271uint32_t
272Debugger::GetStopSourceLineCount (bool before) const
273{
274 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000275 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000276}
277
278Debugger::StopDisassemblyType
279Debugger::GetStopDisassemblyDisplay () const
280{
281 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000282 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000283}
284
285uint32_t
286Debugger::GetDisassemblyLineCount () const
287{
288 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000289 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000290}
Greg Claytone372b982011-11-21 21:44:34 +0000291
Greg Clayton1b654882010-09-19 02:33:57 +0000292#pragma mark Debugger
293
Greg Clayton67cc0632012-08-22 17:17:09 +0000294//const DebuggerPropertiesSP &
295//Debugger::GetSettings() const
296//{
297// return m_properties_sp;
298//}
299//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000300
Caroline Tice2f88aad2011-01-14 00:29:16 +0000301int
302Debugger::TestDebuggerRefCount ()
303{
304 return g_shared_debugger_refcount;
305}
306
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307void
308Debugger::Initialize ()
309{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000310 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000311 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312}
313
314void
315Debugger::Terminate ()
316{
Greg Clayton66111032010-06-23 01:19:29 +0000317 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 {
Greg Clayton66111032010-06-23 01:19:29 +0000319 g_shared_debugger_refcount--;
320 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 {
Greg Claytondbe54502010-11-19 03:46:01 +0000322 lldb_private::WillTerminate();
323 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000324
325 // Clear our master list of debugger objects
326 Mutex::Locker locker (GetDebuggerListMutex ());
327 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329 }
330}
331
Caroline Tice20bd37f2011-03-10 22:14:10 +0000332void
333Debugger::SettingsInitialize ()
334{
Greg Clayton6920b522012-08-22 18:39:03 +0000335 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000336}
337
338void
339Debugger::SettingsTerminate ()
340{
Greg Clayton6920b522012-08-22 18:39:03 +0000341 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000342}
343
Enrico Granata21dfcd92012-09-28 23:57:51 +0000344bool
345Debugger::LoadPlugin (const FileSpec& spec)
346{
347 lldb::DynamicLibrarySP dynlib_sp(new lldb_private::DynamicLibrary(spec));
348 lldb::DebuggerSP debugger_sp(shared_from_this());
349 lldb::SBDebugger debugger_sb(debugger_sp);
350 // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
351 LLDBCommandPluginInit init_func = dynlib_sp->GetSymbol<LLDBCommandPluginInit>("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
352 if (!init_func)
353 return false;
354 if (init_func(debugger_sb))
355 {
356 m_loaded_plugins.push_back(dynlib_sp);
357 return true;
358 }
359 return false;
360}
361
362static FileSpec::EnumerateDirectoryResult
363LoadPluginCallback
364(
365 void *baton,
366 FileSpec::FileType file_type,
367 const FileSpec &file_spec
368 )
369{
370 Error error;
371
372 static ConstString g_dylibext("dylib");
373
374 if (!baton)
375 return FileSpec::eEnumerateDirectoryResultQuit;
376
377 Debugger *debugger = (Debugger*)baton;
378
379 // If we have a regular file, a symbolic link or unknown file type, try
380 // and process the file. We must handle unknown as sometimes the directory
381 // enumeration might be enumerating a file system that doesn't have correct
382 // file type information.
383 if (file_type == FileSpec::eFileTypeRegular ||
384 file_type == FileSpec::eFileTypeSymbolicLink ||
385 file_type == FileSpec::eFileTypeUnknown )
386 {
387 FileSpec plugin_file_spec (file_spec);
388 plugin_file_spec.ResolvePath ();
389
390 if (plugin_file_spec.GetFileNameExtension() != g_dylibext)
391 return FileSpec::eEnumerateDirectoryResultNext;
392
393 debugger->LoadPlugin (plugin_file_spec);
394
395 return FileSpec::eEnumerateDirectoryResultNext;
396 }
397
398 else if (file_type == FileSpec::eFileTypeUnknown ||
399 file_type == FileSpec::eFileTypeDirectory ||
400 file_type == FileSpec::eFileTypeSymbolicLink )
401 {
402 // Try and recurse into anything that a directory or symbolic link.
403 // We must also do this for unknown as sometimes the directory enumeration
404 // might be enurating a file system that doesn't have correct file type
405 // information.
406 return FileSpec::eEnumerateDirectoryResultEnter;
407 }
408
409 return FileSpec::eEnumerateDirectoryResultNext;
410}
411
412void
413Debugger::InstanceInitialize ()
414{
415 FileSpec dir_spec;
416 const bool find_directories = true;
417 const bool find_files = true;
418 const bool find_other = true;
419 char dir_path[PATH_MAX];
420 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
421 {
422 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
423 {
424 FileSpec::EnumerateDirectory (dir_path,
425 find_directories,
426 find_files,
427 find_other,
428 LoadPluginCallback,
429 this);
430 }
431 }
432
433 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
434 {
435 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
436 {
437 FileSpec::EnumerateDirectory (dir_path,
438 find_directories,
439 find_files,
440 find_other,
441 LoadPluginCallback,
442 this);
443 }
444 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000445
446 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000447}
448
Greg Clayton66111032010-06-23 01:19:29 +0000449DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000450Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000451{
Jim Ingham228063c2012-02-21 02:23:08 +0000452 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000453 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000454 {
455 Mutex::Locker locker (GetDebuggerListMutex ());
456 GetDebuggerList().push_back(debugger_sp);
457 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000458 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000459 return debugger_sp;
460}
461
Caroline Ticee02657b2011-01-22 01:02:07 +0000462void
Greg Clayton4d122c42011-09-17 08:33:22 +0000463Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000464{
465 if (debugger_sp.get() == NULL)
466 return;
467
Jim Ingham8314c522011-09-15 21:36:42 +0000468 debugger_sp->Clear();
469
Greg Claytonc15f55e2012-03-30 20:53:46 +0000470 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000471 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000472 Mutex::Locker locker (GetDebuggerListMutex ());
473 DebuggerList &debugger_list = GetDebuggerList ();
474 DebuggerList::iterator pos, end = debugger_list.end();
475 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000476 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000477 if ((*pos).get() == debugger_sp.get())
478 {
479 debugger_list.erase (pos);
480 return;
481 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000482 }
483 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000484}
485
Greg Clayton4d122c42011-09-17 08:33:22 +0000486DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000487Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
488{
Greg Clayton4d122c42011-09-17 08:33:22 +0000489 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000490 if (g_shared_debugger_refcount > 0)
491 {
492 Mutex::Locker locker (GetDebuggerListMutex ());
493 DebuggerList &debugger_list = GetDebuggerList();
494 DebuggerList::iterator pos, end = debugger_list.end();
495
496 for (pos = debugger_list.begin(); pos != end; ++pos)
497 {
498 if ((*pos).get()->m_instance_name == instance_name)
499 {
500 debugger_sp = *pos;
501 break;
502 }
503 }
504 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000505 return debugger_sp;
506}
Greg Clayton66111032010-06-23 01:19:29 +0000507
508TargetSP
509Debugger::FindTargetWithProcessID (lldb::pid_t pid)
510{
Greg Clayton4d122c42011-09-17 08:33:22 +0000511 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000512 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000513 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000514 Mutex::Locker locker (GetDebuggerListMutex ());
515 DebuggerList &debugger_list = GetDebuggerList();
516 DebuggerList::iterator pos, end = debugger_list.end();
517 for (pos = debugger_list.begin(); pos != end; ++pos)
518 {
519 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
520 if (target_sp)
521 break;
522 }
Greg Clayton66111032010-06-23 01:19:29 +0000523 }
524 return target_sp;
525}
526
Greg Claytone4e45922011-11-16 05:37:56 +0000527TargetSP
528Debugger::FindTargetWithProcess (Process *process)
529{
530 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000531 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000532 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000533 Mutex::Locker locker (GetDebuggerListMutex ());
534 DebuggerList &debugger_list = GetDebuggerList();
535 DebuggerList::iterator pos, end = debugger_list.end();
536 for (pos = debugger_list.begin(); pos != end; ++pos)
537 {
538 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
539 if (target_sp)
540 break;
541 }
Greg Claytone4e45922011-11-16 05:37:56 +0000542 }
543 return target_sp;
544}
545
Jim Ingham228063c2012-02-21 02:23:08 +0000546Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000547 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000548 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000549 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550 m_input_file (),
551 m_output_file (),
552 m_error_file (),
Jim Inghamc5917d92012-11-30 20:23:19 +0000553 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000554 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000555 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 m_listener ("lldb.Debugger"),
Jim Inghame37d6052011-09-13 00:29:56 +0000557 m_source_manager(*this),
558 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000559 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000560 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000561 m_input_reader_data (),
562 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563{
Greg Clayton67cc0632012-08-22 17:17:09 +0000564 char instance_cstr[256];
565 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
566 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000567 if (log_callback)
568 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000569 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000570 // Always add our default platform to the platform list
571 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
572 assert (default_platform_sp.get());
573 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000574
Greg Clayton754a9362012-08-23 00:22:02 +0000575 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000576 m_collection_sp->AppendProperty (ConstString("target"),
577 ConstString("Settings specify to debugging targets."),
578 true,
579 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000580 if (m_command_interpreter_ap.get())
581 {
582 m_collection_sp->AppendProperty (ConstString("interpreter"),
583 ConstString("Settings specify to the debugger's command interpreter."),
584 true,
585 m_command_interpreter_ap->GetValueProperties());
586 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000587 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
588 term_width->SetMinimumValue(10);
589 term_width->SetMaximumValue(1024);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590}
591
592Debugger::~Debugger ()
593{
Jim Ingham8314c522011-09-15 21:36:42 +0000594 Clear();
595}
596
597void
598Debugger::Clear()
599{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000600 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000601 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000602 int num_targets = m_target_list.GetNumTargets();
603 for (int i = 0; i < num_targets; i++)
604 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000605 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
606 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000607 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000608 ProcessSP process_sp (target_sp->GetProcessSP());
609 if (process_sp)
610 {
611 if (process_sp->GetShouldDetach())
612 process_sp->Detach();
613 }
614 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000615 }
Greg Clayton66111032010-06-23 01:19:29 +0000616 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000617 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000618
619 // Close the input file _before_ we close the input read communications class
620 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000621 m_terminal_state.Clear();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000622 GetInputFile().Close ();
623 // Now that we have closed m_input_file, we can now tell our input communication
624 // class to close down. Its read thread should quickly exit after we close
625 // the input file handle above.
626 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000627}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628
629bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000630Debugger::GetCloseInputOnEOF () const
631{
632 return m_input_comm.GetCloseOnEOF();
633}
634
635void
636Debugger::SetCloseInputOnEOF (bool b)
637{
638 m_input_comm.SetCloseOnEOF(b);
639}
640
641bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642Debugger::GetAsyncExecution ()
643{
Greg Clayton66111032010-06-23 01:19:29 +0000644 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645}
646
647void
648Debugger::SetAsyncExecution (bool async_execution)
649{
Greg Clayton66111032010-06-23 01:19:29 +0000650 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651}
652
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653
654void
655Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
656{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000657 File &in_file = GetInputFile();
658 in_file.SetStream (fh, tranfer_ownership);
659 if (in_file.IsValid() == false)
660 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661
662 // Disconnect from any old connection if we had one
663 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000664 // Pass false as the second argument to ConnectionFileDescriptor below because
665 // our "in_file" above will already take ownership if requested and we don't
666 // want to objects trying to own and close a file descriptor.
667 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
Jim Inghamc5917d92012-11-30 20:23:19 +0000669
670 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
671 SaveInputTerminalState ();
672
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 Error error;
674 if (m_input_comm.StartReadThread (&error) == false)
675 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000676 File &err_file = GetErrorFile();
677
678 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
679 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681}
682
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683void
684Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
685{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000686 File &out_file = GetOutputFile();
687 out_file.SetStream (fh, tranfer_ownership);
688 if (out_file.IsValid() == false)
689 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000690
Enrico Granatab5887262012-10-29 21:18:03 +0000691 // do not create the ScriptInterpreter just for setting the output file handle
692 // as the constructor will know how to do the right thing on its own
693 const bool can_create = false;
694 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
695 if (script_interpreter)
696 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697}
698
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699void
700Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
701{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000702 File &err_file = GetErrorFile();
703 err_file.SetStream (fh, tranfer_ownership);
704 if (err_file.IsValid() == false)
705 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706}
707
Jim Inghamc5917d92012-11-30 20:23:19 +0000708void
709Debugger::SaveInputTerminalState ()
710{
711 File &in_file = GetInputFile();
712 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
713 m_terminal_state.Save(in_file.GetDescriptor(), true);
714}
715
716void
717Debugger::RestoreInputTerminalState ()
718{
719 m_terminal_state.Restore();
720}
721
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000722ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000723Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724{
725 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000726 TargetSP target_sp(GetSelectedTarget());
727 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728
729 if (target_sp)
730 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000731 ProcessSP process_sp (target_sp->GetProcessSP());
732 exe_ctx.SetProcessSP (process_sp);
733 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000735 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
736 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000738 exe_ctx.SetThreadSP (thread_sp);
739 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
740 if (exe_ctx.GetFramePtr() == NULL)
741 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742 }
743 }
744 }
745 return exe_ctx;
746
747}
748
Caroline Ticeb44880c2011-02-10 01:15:13 +0000749InputReaderSP
750Debugger::GetCurrentInputReader ()
751{
752 InputReaderSP reader_sp;
753
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000754 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000755 {
756 // Clear any finished readers from the stack
757 while (CheckIfTopInputReaderIsDone()) ;
758
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000759 if (!m_input_reader_stack.IsEmpty())
760 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000761 }
762
763 return reader_sp;
764}
765
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766void
767Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
768{
Caroline Ticeefed6132010-11-19 20:47:54 +0000769 if (bytes_len > 0)
770 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
771 else
772 ((Debugger *)baton)->DispatchInputEndOfFile ();
773}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774
775
776void
777Debugger::DispatchInput (const char *bytes, size_t bytes_len)
778{
Caroline Ticeefed6132010-11-19 20:47:54 +0000779 if (bytes == NULL || bytes_len == 0)
780 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781
782 WriteToDefaultReader (bytes, bytes_len);
783}
784
785void
Caroline Ticeefed6132010-11-19 20:47:54 +0000786Debugger::DispatchInputInterrupt ()
787{
788 m_input_reader_data.clear();
789
Caroline Ticeb44880c2011-02-10 01:15:13 +0000790 InputReaderSP reader_sp (GetCurrentInputReader ());
791 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000792 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000793 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000794
Caroline Ticeb44880c2011-02-10 01:15:13 +0000795 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000796 while (CheckIfTopInputReaderIsDone ()) ;
797 }
798}
799
800void
801Debugger::DispatchInputEndOfFile ()
802{
803 m_input_reader_data.clear();
804
Caroline Ticeb44880c2011-02-10 01:15:13 +0000805 InputReaderSP reader_sp (GetCurrentInputReader ());
806 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000807 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000808 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000809
Caroline Ticeb44880c2011-02-10 01:15:13 +0000810 // 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 +0000811 while (CheckIfTopInputReaderIsDone ()) ;
812 }
813}
814
815void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000816Debugger::CleanUpInputReaders ()
817{
818 m_input_reader_data.clear();
819
Caroline Ticeb44880c2011-02-10 01:15:13 +0000820 // 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 +0000821 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000822 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000823 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000824 if (reader_sp)
825 {
826 reader_sp->Notify (eInputReaderEndOfFile);
827 reader_sp->SetIsDone (true);
828 }
829 }
830}
831
832void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000833Debugger::NotifyTopInputReader (InputReaderAction notification)
834{
835 InputReaderSP reader_sp (GetCurrentInputReader());
836 if (reader_sp)
837 {
838 reader_sp->Notify (notification);
839
840 // Flush out any input readers that are done.
841 while (CheckIfTopInputReaderIsDone ())
842 /* Do nothing. */;
843 }
844}
845
Caroline Tice9088b062011-05-09 23:06:58 +0000846bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000847Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000848{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000849 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000850
Caroline Ticed61c10b2011-06-16 16:27:19 +0000851 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000852}
853
854
Caroline Tice969ed3d2011-05-02 20:41:46 +0000855void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000856Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
857{
858 if (bytes && bytes_len)
859 m_input_reader_data.append (bytes, bytes_len);
860
861 if (m_input_reader_data.empty())
862 return;
863
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000864 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000867 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 if (!reader_sp)
869 break;
870
Greg Clayton471b31c2010-07-20 22:52:08 +0000871 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872 m_input_reader_data.size());
873 if (bytes_handled)
874 {
875 m_input_reader_data.erase (0, bytes_handled);
876 }
877 else
878 {
879 // No bytes were handled, we might not have reached our
880 // granularity, just return and wait for more data
881 break;
882 }
883 }
884
Caroline Ticeb44880c2011-02-10 01:15:13 +0000885 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886 while (CheckIfTopInputReaderIsDone ())
887 /* Do nothing. */;
888
889}
890
891void
892Debugger::PushInputReader (const InputReaderSP& reader_sp)
893{
894 if (!reader_sp)
895 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000896
897 // Deactivate the old top reader
898 InputReaderSP top_reader_sp (GetCurrentInputReader ());
899
900 if (top_reader_sp)
901 top_reader_sp->Notify (eInputReaderDeactivate);
902
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000903 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904 reader_sp->Notify (eInputReaderActivate);
905 ActivateInputReader (reader_sp);
906}
907
908bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000909Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910{
911 bool result = false;
912
913 // The reader on the stop of the stack is done, so let the next
914 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000915 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000917 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000918 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919
920 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
921 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000922 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923 reader_sp->Notify (eInputReaderDeactivate);
924 reader_sp->Notify (eInputReaderDone);
925 result = true;
926
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000927 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000929 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930 if (reader_sp)
931 {
932 ActivateInputReader (reader_sp);
933 reader_sp->Notify (eInputReaderReactivate);
934 }
935 }
936 }
937 }
938 return result;
939}
940
941bool
942Debugger::CheckIfTopInputReaderIsDone ()
943{
944 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000945 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000947 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000948 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949
950 if (reader_sp && reader_sp->IsDone())
951 {
952 result = true;
953 PopInputReader (reader_sp);
954 }
955 }
956 return result;
957}
958
959void
960Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
961{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000962 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000964 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000966 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000967
968 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000969
Greg Claytona3406612011-02-07 23:24:47 +0000970 switch (reader_sp->GetGranularity())
971 {
972 case eInputReaderGranularityByte:
973 case eInputReaderGranularityWord:
974 tty.SetCanonical (false);
975 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976
Greg Claytona3406612011-02-07 23:24:47 +0000977 case eInputReaderGranularityLine:
978 case eInputReaderGranularityAll:
979 tty.SetCanonical (true);
980 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981
Greg Claytona3406612011-02-07 23:24:47 +0000982 default:
983 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984 }
985 }
986}
Greg Clayton66111032010-06-23 01:19:29 +0000987
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000988StreamSP
989Debugger::GetAsyncOutputStream ()
990{
991 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
992 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
993}
994
995StreamSP
996Debugger::GetAsyncErrorStream ()
997{
998 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
999 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1000}
1001
Greg Claytonc7bece562013-01-25 18:06:21 +00001002size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001003Debugger::GetNumDebuggers()
1004{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001005 if (g_shared_debugger_refcount > 0)
1006 {
1007 Mutex::Locker locker (GetDebuggerListMutex ());
1008 return GetDebuggerList().size();
1009 }
1010 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001011}
1012
1013lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001014Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001015{
1016 DebuggerSP debugger_sp;
1017
Greg Claytonc15f55e2012-03-30 20:53:46 +00001018 if (g_shared_debugger_refcount > 0)
1019 {
1020 Mutex::Locker locker (GetDebuggerListMutex ());
1021 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001022
Greg Claytonc15f55e2012-03-30 20:53:46 +00001023 if (index < debugger_list.size())
1024 debugger_sp = debugger_list[index];
1025 }
1026
Enrico Granata061858c2012-02-15 02:34:21 +00001027 return debugger_sp;
1028}
1029
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001030DebuggerSP
1031Debugger::FindDebuggerWithID (lldb::user_id_t id)
1032{
Greg Clayton4d122c42011-09-17 08:33:22 +00001033 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001034
Greg Claytonc15f55e2012-03-30 20:53:46 +00001035 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001036 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001037 Mutex::Locker locker (GetDebuggerListMutex ());
1038 DebuggerList &debugger_list = GetDebuggerList();
1039 DebuggerList::iterator pos, end = debugger_list.end();
1040 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001041 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001042 if ((*pos).get()->GetID() == id)
1043 {
1044 debugger_sp = *pos;
1045 break;
1046 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001047 }
1048 }
1049 return debugger_sp;
1050}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001051
Greg Clayton1b654882010-09-19 02:33:57 +00001052static void
1053TestPromptFormats (StackFrame *frame)
1054{
1055 if (frame == NULL)
1056 return;
1057
1058 StreamString s;
1059 const char *prompt_format =
1060 "{addr = '${addr}'\n}"
1061 "{process.id = '${process.id}'\n}"
1062 "{process.name = '${process.name}'\n}"
1063 "{process.file.basename = '${process.file.basename}'\n}"
1064 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1065 "{thread.id = '${thread.id}'\n}"
1066 "{thread.index = '${thread.index}'\n}"
1067 "{thread.name = '${thread.name}'\n}"
1068 "{thread.queue = '${thread.queue}'\n}"
1069 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1070 "{target.arch = '${target.arch}'\n}"
1071 "{module.file.basename = '${module.file.basename}'\n}"
1072 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1073 "{file.basename = '${file.basename}'\n}"
1074 "{file.fullpath = '${file.fullpath}'\n}"
1075 "{frame.index = '${frame.index}'\n}"
1076 "{frame.pc = '${frame.pc}'\n}"
1077 "{frame.sp = '${frame.sp}'\n}"
1078 "{frame.fp = '${frame.fp}'\n}"
1079 "{frame.flags = '${frame.flags}'\n}"
1080 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1081 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1082 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1083 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1084 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1085 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1086 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1087 "{function.id = '${function.id}'\n}"
1088 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001089 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001090 "{function.addr-offset = '${function.addr-offset}'\n}"
1091 "{function.line-offset = '${function.line-offset}'\n}"
1092 "{function.pc-offset = '${function.pc-offset}'\n}"
1093 "{line.file.basename = '${line.file.basename}'\n}"
1094 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1095 "{line.number = '${line.number}'\n}"
1096 "{line.start-addr = '${line.start-addr}'\n}"
1097 "{line.end-addr = '${line.end-addr}'\n}"
1098;
1099
1100 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1101 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001102 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +00001103 const char *end = NULL;
1104 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
1105 {
1106 printf("%s\n", s.GetData());
1107 }
1108 else
1109 {
1110 printf ("error: at '%s'\n", end);
1111 printf ("what we got: %s\n", s.GetData());
1112 }
1113}
1114
Enrico Granata9fc19442011-07-06 02:13:41 +00001115static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001116ScanFormatDescriptor (const char* var_name_begin,
1117 const char* var_name_end,
1118 const char** var_name_final,
1119 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001120 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001121 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001122{
Enrico Granatae992a082011-07-22 17:03:19 +00001123 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001124 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001125 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001126 {
1127 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001128 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001129 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001130 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001131 else
1132 {
1133 *var_name_final = *percent_position;
1134 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
1135 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001136 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001137 log->Printf("ScanFormatDescriptor] parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001138 if ( !FormatManager::GetFormatFromCString(format_name,
1139 true,
1140 *custom_format) )
1141 {
Enrico Granatae992a082011-07-22 17:03:19 +00001142 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001143 log->Printf("ScanFormatDescriptor] %s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001144 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +00001145 if (*format_name == '@')
Enrico Granata86cc9822012-03-19 22:58:49 +00001146 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
Enrico Granata9fc19442011-07-06 02:13:41 +00001147 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +00001148 else if (*format_name == 'V')
Enrico Granata86cc9822012-03-19 22:58:49 +00001149 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatad55546b2011-07-22 00:16:08 +00001150 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +00001151 else if (*format_name == 'L')
Enrico Granata86cc9822012-03-19 22:58:49 +00001152 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +00001153 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +00001154 else if (*format_name == 'S')
Enrico Granata86cc9822012-03-19 22:58:49 +00001155 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Enrico Granata5dfd49c2011-08-04 02:34:29 +00001156 else if (*format_name == '#')
Enrico Granata86cc9822012-03-19 22:58:49 +00001157 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001158 else if (*format_name == 'T')
Enrico Granata86cc9822012-03-19 22:58:49 +00001159 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
Enrico Granatae992a082011-07-22 17:03:19 +00001160 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001161 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +00001162 }
1163 // a good custom format tells us to print the value using it
1164 else
Enrico Granatae992a082011-07-22 17:03:19 +00001165 {
1166 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001167 log->Printf("ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001168 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001169 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001170 delete format_name;
1171 }
Enrico Granatae992a082011-07-22 17:03:19 +00001172 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001173 log->Printf("ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001174 *custom_format,
1175 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001176 return true;
1177}
1178
1179static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001180ScanBracketedRange (const char* var_name_begin,
1181 const char* var_name_end,
1182 const char* var_name_final,
1183 const char** open_bracket_position,
1184 const char** separator_position,
1185 const char** close_bracket_position,
1186 const char** var_name_final_if_array_range,
1187 int64_t* index_lower,
1188 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001189{
Enrico Granatae992a082011-07-22 17:03:19 +00001190 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001191 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001192 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001193 {
1194 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1195 *close_bracket_position = ::strchr(*open_bracket_position,']');
1196 // as usual, we assume that [] will come before %
1197 //printf("trying to expand a []\n");
1198 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001199 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001200 {
Enrico Granatae992a082011-07-22 17:03:19 +00001201 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001202 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001203 *index_lower = 0;
1204 }
1205 else if (*separator_position == NULL || *separator_position > var_name_end)
1206 {
1207 char *end = NULL;
1208 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1209 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001210 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001211 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001212 }
Greg Clayton34132752011-07-06 04:07:21 +00001213 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001214 {
1215 char *end = NULL;
1216 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1217 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001218 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001219 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001220 }
1221 else
Enrico Granatae992a082011-07-22 17:03:19 +00001222 {
1223 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001224 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001225 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001226 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001227 if (*index_lower > *index_higher && *index_higher > 0)
1228 {
Enrico Granatae992a082011-07-22 17:03:19 +00001229 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001230 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001231 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001232 *index_lower = *index_higher;
1233 *index_higher = temp;
1234 }
1235 }
Enrico Granatae992a082011-07-22 17:03:19 +00001236 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001237 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001238 return true;
1239}
1240
Enrico Granata9fc19442011-07-06 02:13:41 +00001241static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001242ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001243 size_t index,
Enrico Granatadc940732011-08-23 00:32:52 +00001244 StackFrame* frame,
1245 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001246{
Enrico Granatae992a082011-07-22 17:03:19 +00001247 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001248 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001249 std::string ptr_deref_buffer(10,0);
1250 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001251 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001252 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001253 const char* first_unparsed;
1254 ValueObject::GetValueForExpressionPathOptions options;
1255 ValueObject::ExpressionPathEndResultType final_value_type;
1256 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001257 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001258 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001259 &first_unparsed,
1260 &reason_to_stop,
1261 &final_value_type,
1262 options,
1263 &what_next);
1264 if (!item)
1265 {
Enrico Granatae992a082011-07-22 17:03:19 +00001266 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001267 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001268 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001269 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001270 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001271 else
1272 {
Enrico Granatae992a082011-07-22 17:03:19 +00001273 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001274 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001275 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001276 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001277 }
1278 return item;
1279}
1280
Greg Clayton1b654882010-09-19 02:33:57 +00001281bool
1282Debugger::FormatPrompt
1283(
1284 const char *format,
1285 const SymbolContext *sc,
1286 const ExecutionContext *exe_ctx,
1287 const Address *addr,
1288 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001289 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001290 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001291)
1292{
Enrico Granatac482a192011-08-17 22:13:59 +00001293 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001294 bool success = true;
1295 const char *p;
Enrico Granatae992a082011-07-22 17:03:19 +00001296 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001297 for (p = format; *p != '\0'; ++p)
1298 {
Enrico Granatac482a192011-08-17 22:13:59 +00001299 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001300 {
Enrico Granatac482a192011-08-17 22:13:59 +00001301 valobj = realvalobj;
1302 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001303 }
Greg Clayton1b654882010-09-19 02:33:57 +00001304 size_t non_special_chars = ::strcspn (p, "${}\\");
1305 if (non_special_chars > 0)
1306 {
1307 if (success)
1308 s.Write (p, non_special_chars);
1309 p += non_special_chars;
1310 }
1311
1312 if (*p == '\0')
1313 {
1314 break;
1315 }
1316 else if (*p == '{')
1317 {
1318 // Start a new scope that must have everything it needs if it is to
1319 // to make it into the final output stream "s". If you want to make
1320 // a format that only prints out the function or symbol name if there
1321 // is one in the symbol context you can use:
1322 // "{function =${function.name}}"
1323 // The first '{' starts a new scope that end with the matching '}' at
1324 // the end of the string. The contents "function =${function.name}"
1325 // will then be evaluated and only be output if there is a function
1326 // or symbol with a valid name.
1327 StreamString sub_strm;
1328
1329 ++p; // Skip the '{'
1330
Enrico Granatac482a192011-08-17 22:13:59 +00001331 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001332 {
1333 // The stream had all it needed
1334 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1335 }
1336 if (*p != '}')
1337 {
1338 success = false;
1339 break;
1340 }
1341 }
1342 else if (*p == '}')
1343 {
1344 // End of a enclosing scope
1345 break;
1346 }
1347 else if (*p == '$')
1348 {
1349 // We have a prompt variable to print
1350 ++p;
1351 if (*p == '{')
1352 {
1353 ++p;
1354 const char *var_name_begin = p;
1355 const char *var_name_end = ::strchr (p, '}');
1356
1357 if (var_name_end && var_name_begin < var_name_end)
1358 {
1359 // if we have already failed to parse, skip this variable
1360 if (success)
1361 {
1362 const char *cstr = NULL;
1363 Address format_addr;
1364 bool calculate_format_addr_function_offset = false;
1365 // Set reg_kind and reg_num to invalid values
1366 RegisterKind reg_kind = kNumRegisterKinds;
1367 uint32_t reg_num = LLDB_INVALID_REGNUM;
1368 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001369 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001370 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001371 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001372 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1373 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001374
Greg Clayton1b654882010-09-19 02:33:57 +00001375 // Each variable must set success to true below...
1376 bool var_success = false;
1377 switch (var_name_begin[0])
1378 {
Enrico Granata4becb372011-06-29 22:27:15 +00001379 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001380 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001381 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001382 {
Enrico Granatac482a192011-08-17 22:13:59 +00001383 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001384 break;
1385
Enrico Granatac3e320a2011-08-02 17:27:39 +00001386 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001387 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001388
Enrico Granata6f3533f2011-07-29 19:53:35 +00001389 // check for *var and *svar
1390 if (*var_name_begin == '*')
1391 {
1392 do_deref_pointer = true;
1393 var_name_begin++;
1394 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001395
1396 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001397 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001398
Enrico Granata6f3533f2011-07-29 19:53:35 +00001399 if (*var_name_begin == 's')
1400 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001401 if (!valobj->IsSynthetic())
1402 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001403 if (!valobj)
1404 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001405 var_name_begin++;
1406 }
1407
Enrico Granatac3e320a2011-08-02 17:27:39 +00001408 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001409 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001410
Enrico Granata6f3533f2011-07-29 19:53:35 +00001411 // should be a 'v' by now
1412 if (*var_name_begin != 'v')
1413 break;
1414
Enrico Granatac3e320a2011-08-02 17:27:39 +00001415 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001416 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001417
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001418 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001419 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001420 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001421 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001422 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001423 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001424 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001425 const char* var_name_final = NULL;
1426 const char* var_name_final_if_array_range = NULL;
1427 const char* close_bracket_position = NULL;
1428 int64_t index_lower = -1;
1429 int64_t index_higher = -1;
1430 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001431 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001432 bool was_plain_var = false;
1433 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001434 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001435
Enrico Granatac482a192011-08-17 22:13:59 +00001436 if (!valobj) break;
1437 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001438 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001439 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001440 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001441 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001442 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001443 }
1444 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1445 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001446 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001447 // this is a variable with some custom format applied to it
1448 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001449 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001450 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001451 ScanFormatDescriptor (var_name_begin,
1452 var_name_end,
1453 &var_name_final,
1454 &percent_position,
1455 &custom_format,
1456 &val_obj_display);
1457 }
1458 // this is ${var.something} or multiple .something nested
1459 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1460 {
Enrico Granataa777dc22012-05-08 21:49:57 +00001461 if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1462 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001463 const char* percent_position;
1464 ScanFormatDescriptor (var_name_begin,
1465 var_name_end,
1466 &var_name_final,
1467 &percent_position,
1468 &custom_format,
1469 &val_obj_display);
1470
1471 const char* open_bracket_position;
1472 const char* separator_position;
1473 ScanBracketedRange (var_name_begin,
1474 var_name_end,
1475 var_name_final,
1476 &open_bracket_position,
1477 &separator_position,
1478 &close_bracket_position,
1479 &var_name_final_if_array_range,
1480 &index_lower,
1481 &index_higher);
1482
1483 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001484
Enrico Granata599171a2013-02-01 23:59:44 +00001485 std::string expr_path(var_name_final-var_name_begin-1,0);
1486 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1487
1488 if (log)
1489 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1490
1491 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001492 &first_unparsed,
1493 &reason_to_stop,
1494 &final_value_type,
1495 options,
1496 &what_next).get();
1497
1498 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001499 {
Enrico Granatae992a082011-07-22 17:03:19 +00001500 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001501 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001502 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001503 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001504 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001505 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001506 else
1507 {
Enrico Granatae992a082011-07-22 17:03:19 +00001508 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001509 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001510 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001511 first_unparsed, reason_to_stop, final_value_type);
1512 }
Enrico Granata4becb372011-06-29 22:27:15 +00001513 }
Greg Clayton34132752011-07-06 04:07:21 +00001514 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001515 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001516
Enrico Granata86cc9822012-03-19 22:58:49 +00001517 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1518 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001519
Enrico Granata86cc9822012-03-19 22:58:49 +00001520 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001521
Enrico Granataa7187d02011-07-06 19:27:11 +00001522 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001523 {
Greg Clayton34132752011-07-06 04:07:21 +00001524 // I have not deref-ed yet, let's do it
1525 // this happens when we are not going through GetValueForVariableExpressionPath
1526 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001527 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001528 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001529 if (error.Fail())
1530 {
1531 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001532 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001533 break;
1534 }
Greg Clayton34132752011-07-06 04:07:21 +00001535 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001536 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001537
Enrico Granataa777dc22012-05-08 21:49:57 +00001538 // <rdar://problem/11338654>
1539 // we do not want to use the summary for a bitfield of type T:n
1540 // if we were originally dealing with just a T - that would get
1541 // us into an endless recursion
1542 if (target->IsBitfield() && was_var_indexed)
1543 {
1544 // TODO: check for a (T:n)-specific summary - we should still obey that
1545 StreamString bitfield_name;
1546 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1547 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1548 if (!DataVisualization::GetSummaryForType(type_sp))
1549 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1550 }
1551
Enrico Granata85933ed2011-08-18 16:38:26 +00001552 // TODO use flags for these
Greg Clayton4ef877f2012-12-06 02:33:54 +00001553 bool is_array = ClangASTContext::IsArrayType(target->GetClangType(), NULL, NULL, NULL);
Enrico Granataf4efecd2011-07-12 22:56:10 +00001554 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001555 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001556
Enrico Granata86cc9822012-03-19 22:58:49 +00001557 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 +00001558 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001559 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001560 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001561 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001562
Enrico Granata86cc9822012-03-19 22:58:49 +00001563 if (target->HasSpecialPrintableRepresentation(val_obj_display,
1564 custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001565 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001566 // try to use the special cases
1567 var_success = target->DumpPrintableRepresentation(str_temp,
1568 val_obj_display,
1569 custom_format);
1570 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001571 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001572
1573 // should not happen
1574 if (!var_success)
1575 s << "<invalid usage of pointer value as object>";
1576 else
1577 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001578 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001579 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001580 }
1581 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001582 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001583 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001584 {
1585 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1586 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001587 else if (is_pointer) // if pointer, value is the address stored
1588 {
Greg Clayton23f59502012-07-17 03:23:13 +00001589 target->DumpPrintableRepresentation (s,
1590 val_obj_display,
1591 custom_format,
1592 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001593 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001594 else
1595 {
1596 s << "<invalid usage of pointer value as object>";
1597 }
1598 var_success = true;
1599 break;
1600 }
1601 }
1602
1603 // if directly trying to print ${var}, and this is an aggregate, display a nice
1604 // type @ location message
1605 if (is_aggregate && was_plain_var)
1606 {
1607 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1608 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001609 break;
1610 }
1611
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001612 // 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 +00001613 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001614 {
1615 s << "<invalid use of aggregate type>";
1616 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001617 break;
1618 }
Greg Clayton34132752011-07-06 04:07:21 +00001619
1620 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001621 {
1622 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001623 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001624 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001625 }
Greg Clayton34132752011-07-06 04:07:21 +00001626 else
Enrico Granatae992a082011-07-22 17:03:19 +00001627 {
1628 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001629 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001630 if (!is_array && !is_pointer)
1631 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001632 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001633 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001634 const char* special_directions = NULL;
1635 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001636 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1637 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001638 ConstString additional_data;
1639 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1640 special_directions_writer.Printf("${%svar%s}",
1641 do_deref_pointer ? "*" : "",
1642 additional_data.GetCString());
1643 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001644 }
1645
1646 // let us display items index_lower thru index_higher of this array
1647 s.PutChar('[');
1648 var_success = true;
1649
1650 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001651 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001652
Greg Claytoncc4d0142012-02-17 07:49:44 +00001653 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001654
Greg Clayton34132752011-07-06 04:07:21 +00001655 for (;index_lower<=index_higher;index_lower++)
1656 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001657 ValueObject* item = ExpandIndexedExpression (target,
1658 index_lower,
1659 exe_ctx->GetFramePtr(),
1660 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001661
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001662 if (!item)
1663 {
Enrico Granatae992a082011-07-22 17:03:19 +00001664 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001665 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001666 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001667 else
1668 {
Enrico Granatae992a082011-07-22 17:03:19 +00001669 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001670 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001671 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001672
Greg Clayton34132752011-07-06 04:07:21 +00001673 if (!special_directions)
1674 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1675 else
1676 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1677
Enrico Granata22c55d12011-08-12 02:00:06 +00001678 if (--max_num_children == 0)
1679 {
1680 s.PutCString(", ...");
1681 break;
1682 }
1683
Greg Clayton34132752011-07-06 04:07:21 +00001684 if (index_lower < index_higher)
1685 s.PutChar(',');
1686 }
1687 s.PutChar(']');
1688 }
Enrico Granata4becb372011-06-29 22:27:15 +00001689 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001690 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001691 case 'a':
1692 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1693 {
1694 if (addr && addr->IsValid())
1695 {
1696 var_success = true;
1697 format_addr = *addr;
1698 }
1699 }
Greg Clayton5a314712011-10-14 07:41:33 +00001700 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1701 {
1702 var_success = true;
1703 var_name_begin += strlen("ansi."); // Skip the "ansi."
1704 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1705 {
1706 var_name_begin += strlen("fg."); // Skip the "fg."
1707 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1708 {
1709 s.Printf ("%s%s%s",
1710 lldb_utility::ansi::k_escape_start,
1711 lldb_utility::ansi::k_fg_black,
1712 lldb_utility::ansi::k_escape_end);
1713 }
1714 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1715 {
1716 s.Printf ("%s%s%s",
1717 lldb_utility::ansi::k_escape_start,
1718 lldb_utility::ansi::k_fg_red,
1719 lldb_utility::ansi::k_escape_end);
1720 }
1721 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1722 {
1723 s.Printf ("%s%s%s",
1724 lldb_utility::ansi::k_escape_start,
1725 lldb_utility::ansi::k_fg_green,
1726 lldb_utility::ansi::k_escape_end);
1727 }
1728 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1729 {
1730 s.Printf ("%s%s%s",
1731 lldb_utility::ansi::k_escape_start,
1732 lldb_utility::ansi::k_fg_yellow,
1733 lldb_utility::ansi::k_escape_end);
1734 }
1735 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1736 {
1737 s.Printf ("%s%s%s",
1738 lldb_utility::ansi::k_escape_start,
1739 lldb_utility::ansi::k_fg_blue,
1740 lldb_utility::ansi::k_escape_end);
1741 }
1742 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1743 {
1744 s.Printf ("%s%s%s",
1745 lldb_utility::ansi::k_escape_start,
1746 lldb_utility::ansi::k_fg_purple,
1747 lldb_utility::ansi::k_escape_end);
1748 }
1749 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1750 {
1751 s.Printf ("%s%s%s",
1752 lldb_utility::ansi::k_escape_start,
1753 lldb_utility::ansi::k_fg_cyan,
1754 lldb_utility::ansi::k_escape_end);
1755 }
1756 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1757 {
1758 s.Printf ("%s%s%s",
1759 lldb_utility::ansi::k_escape_start,
1760 lldb_utility::ansi::k_fg_white,
1761 lldb_utility::ansi::k_escape_end);
1762 }
1763 else
1764 {
1765 var_success = false;
1766 }
1767 }
1768 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1769 {
1770 var_name_begin += strlen("bg."); // Skip the "bg."
1771 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1772 {
1773 s.Printf ("%s%s%s",
1774 lldb_utility::ansi::k_escape_start,
1775 lldb_utility::ansi::k_bg_black,
1776 lldb_utility::ansi::k_escape_end);
1777 }
1778 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1779 {
1780 s.Printf ("%s%s%s",
1781 lldb_utility::ansi::k_escape_start,
1782 lldb_utility::ansi::k_bg_red,
1783 lldb_utility::ansi::k_escape_end);
1784 }
1785 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1786 {
1787 s.Printf ("%s%s%s",
1788 lldb_utility::ansi::k_escape_start,
1789 lldb_utility::ansi::k_bg_green,
1790 lldb_utility::ansi::k_escape_end);
1791 }
1792 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1793 {
1794 s.Printf ("%s%s%s",
1795 lldb_utility::ansi::k_escape_start,
1796 lldb_utility::ansi::k_bg_yellow,
1797 lldb_utility::ansi::k_escape_end);
1798 }
1799 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1800 {
1801 s.Printf ("%s%s%s",
1802 lldb_utility::ansi::k_escape_start,
1803 lldb_utility::ansi::k_bg_blue,
1804 lldb_utility::ansi::k_escape_end);
1805 }
1806 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1807 {
1808 s.Printf ("%s%s%s",
1809 lldb_utility::ansi::k_escape_start,
1810 lldb_utility::ansi::k_bg_purple,
1811 lldb_utility::ansi::k_escape_end);
1812 }
1813 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1814 {
1815 s.Printf ("%s%s%s",
1816 lldb_utility::ansi::k_escape_start,
1817 lldb_utility::ansi::k_bg_cyan,
1818 lldb_utility::ansi::k_escape_end);
1819 }
1820 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1821 {
1822 s.Printf ("%s%s%s",
1823 lldb_utility::ansi::k_escape_start,
1824 lldb_utility::ansi::k_bg_white,
1825 lldb_utility::ansi::k_escape_end);
1826 }
1827 else
1828 {
1829 var_success = false;
1830 }
1831 }
1832 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1833 {
1834 s.Printf ("%s%s%s",
1835 lldb_utility::ansi::k_escape_start,
1836 lldb_utility::ansi::k_ctrl_normal,
1837 lldb_utility::ansi::k_escape_end);
1838 }
1839 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1840 {
1841 s.Printf ("%s%s%s",
1842 lldb_utility::ansi::k_escape_start,
1843 lldb_utility::ansi::k_ctrl_bold,
1844 lldb_utility::ansi::k_escape_end);
1845 }
1846 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1847 {
1848 s.Printf ("%s%s%s",
1849 lldb_utility::ansi::k_escape_start,
1850 lldb_utility::ansi::k_ctrl_faint,
1851 lldb_utility::ansi::k_escape_end);
1852 }
1853 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1854 {
1855 s.Printf ("%s%s%s",
1856 lldb_utility::ansi::k_escape_start,
1857 lldb_utility::ansi::k_ctrl_italic,
1858 lldb_utility::ansi::k_escape_end);
1859 }
1860 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1861 {
1862 s.Printf ("%s%s%s",
1863 lldb_utility::ansi::k_escape_start,
1864 lldb_utility::ansi::k_ctrl_underline,
1865 lldb_utility::ansi::k_escape_end);
1866 }
1867 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1868 {
1869 s.Printf ("%s%s%s",
1870 lldb_utility::ansi::k_escape_start,
1871 lldb_utility::ansi::k_ctrl_slow_blink,
1872 lldb_utility::ansi::k_escape_end);
1873 }
1874 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1875 {
1876 s.Printf ("%s%s%s",
1877 lldb_utility::ansi::k_escape_start,
1878 lldb_utility::ansi::k_ctrl_fast_blink,
1879 lldb_utility::ansi::k_escape_end);
1880 }
1881 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1882 {
1883 s.Printf ("%s%s%s",
1884 lldb_utility::ansi::k_escape_start,
1885 lldb_utility::ansi::k_ctrl_negative,
1886 lldb_utility::ansi::k_escape_end);
1887 }
1888 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1889 {
1890 s.Printf ("%s%s%s",
1891 lldb_utility::ansi::k_escape_start,
1892 lldb_utility::ansi::k_ctrl_conceal,
1893 lldb_utility::ansi::k_escape_end);
1894
1895 }
1896 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1897 {
1898 s.Printf ("%s%s%s",
1899 lldb_utility::ansi::k_escape_start,
1900 lldb_utility::ansi::k_ctrl_crossed_out,
1901 lldb_utility::ansi::k_escape_end);
1902 }
1903 else
1904 {
1905 var_success = false;
1906 }
1907 }
Greg Clayton1b654882010-09-19 02:33:57 +00001908 break;
1909
1910 case 'p':
1911 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1912 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001913 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001914 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001915 Process *process = exe_ctx->GetProcessPtr();
1916 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001917 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001918 var_name_begin += ::strlen ("process.");
1919 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001920 {
Daniel Malead01b2952012-11-29 21:49:15 +00001921 s.Printf("%" PRIu64, process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001922 var_success = true;
1923 }
1924 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1925 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1926 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1927 {
1928 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1929 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001930 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001931 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1932 {
1933 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1934 var_success = format_file_spec;
1935 }
1936 else
1937 {
1938 format_file_spec = exe_module->GetFileSpec();
1939 var_success = format_file_spec;
1940 }
Greg Clayton1b654882010-09-19 02:33:57 +00001941 }
1942 }
1943 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001944 }
Greg Clayton1b654882010-09-19 02:33:57 +00001945 }
1946 break;
1947
1948 case 't':
1949 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1950 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001951 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001952 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001953 Thread *thread = exe_ctx->GetThreadPtr();
1954 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001955 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001956 var_name_begin += ::strlen ("thread.");
1957 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001958 {
Daniel Malead01b2952012-11-29 21:49:15 +00001959 s.Printf("0x%4.4" PRIx64, thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001960 var_success = true;
1961 }
1962 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1963 {
1964 s.Printf("%u", thread->GetIndexID());
1965 var_success = true;
1966 }
1967 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1968 {
1969 cstr = thread->GetName();
1970 var_success = cstr && cstr[0];
1971 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001972 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001973 }
1974 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1975 {
1976 cstr = thread->GetQueueName();
1977 var_success = cstr && cstr[0];
1978 if (var_success)
1979 s.PutCString(cstr);
1980 }
1981 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1982 {
1983 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001984 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001985 {
1986 cstr = stop_info_sp->GetDescription();
1987 if (cstr && cstr[0])
1988 {
1989 s.PutCString(cstr);
1990 var_success = true;
1991 }
Greg Clayton1b654882010-09-19 02:33:57 +00001992 }
1993 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001994 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1995 {
1996 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001997 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001998 {
1999 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2000 if (return_valobj_sp)
2001 {
Jim Inghamef651602011-12-22 19:12:40 +00002002 ValueObject::DumpValueObjectOptions dump_options;
2003 ValueObject::DumpValueObject (s, return_valobj_sp.get(), dump_options);
2004 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00002005 }
2006 }
2007 }
Greg Clayton1b654882010-09-19 02:33:57 +00002008 }
2009 }
2010 }
2011 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
2012 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002013 // TODO: hookup properties
2014// if (!target_properties_sp)
2015// {
2016// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2017// if (target)
2018// target_properties_sp = target->GetProperties();
2019// }
2020//
2021// if (target_properties_sp)
2022// {
2023// var_name_begin += ::strlen ("target.");
2024// const char *end_property = strchr(var_name_begin, '}');
2025// if (end_property)
2026// {
2027// ConstString property_name(var_name_begin, end_property - var_name_begin);
2028// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2029// if (!property_value.empty())
2030// {
2031// s.PutCString (property_value.c_str());
2032// var_success = true;
2033// }
2034// }
2035// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002036 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2037 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002038 {
Greg Clayton1b654882010-09-19 02:33:57 +00002039 var_name_begin += ::strlen ("target.");
2040 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
2041 {
2042 ArchSpec arch (target->GetArchitecture ());
2043 if (arch.IsValid())
2044 {
Greg Clayton64195a22011-02-23 00:35:02 +00002045 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002046 var_success = true;
2047 }
2048 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002049 }
Greg Clayton1b654882010-09-19 02:33:57 +00002050 }
2051 break;
2052
2053
2054 case 'm':
2055 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
2056 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002057 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002058 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002059 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002060 var_name_begin += ::strlen ("module.");
2061
2062 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2063 {
2064 if (module->GetFileSpec())
2065 {
2066 var_name_begin += ::strlen ("file.");
2067
2068 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2069 {
2070 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2071 var_success = format_file_spec;
2072 }
2073 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2074 {
2075 format_file_spec = module->GetFileSpec();
2076 var_success = format_file_spec;
2077 }
2078 }
2079 }
2080 }
2081 }
2082 break;
2083
2084
2085 case 'f':
2086 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2087 {
2088 if (sc && sc->comp_unit != NULL)
2089 {
2090 var_name_begin += ::strlen ("file.");
2091
2092 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2093 {
2094 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2095 var_success = format_file_spec;
2096 }
2097 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2098 {
2099 format_file_spec = *sc->comp_unit;
2100 var_success = format_file_spec;
2101 }
2102 }
2103 }
2104 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
2105 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002106 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002107 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002108 StackFrame *frame = exe_ctx->GetFramePtr();
2109 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002110 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002111 var_name_begin += ::strlen ("frame.");
2112 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00002113 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002114 s.Printf("%u", frame->GetFrameIndex());
2115 var_success = true;
2116 }
2117 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
2118 {
2119 reg_kind = eRegisterKindGeneric;
2120 reg_num = LLDB_REGNUM_GENERIC_PC;
2121 var_success = true;
2122 }
2123 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
2124 {
2125 reg_kind = eRegisterKindGeneric;
2126 reg_num = LLDB_REGNUM_GENERIC_SP;
2127 var_success = true;
2128 }
2129 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
2130 {
2131 reg_kind = eRegisterKindGeneric;
2132 reg_num = LLDB_REGNUM_GENERIC_FP;
2133 var_success = true;
2134 }
2135 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
2136 {
2137 reg_kind = eRegisterKindGeneric;
2138 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2139 var_success = true;
2140 }
2141 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
2142 {
2143 reg_ctx = frame->GetRegisterContext().get();
2144 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002145 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002146 var_name_begin += ::strlen ("reg.");
2147 if (var_name_begin < var_name_end)
2148 {
2149 std::string reg_name (var_name_begin, var_name_end);
2150 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2151 if (reg_info)
2152 var_success = true;
2153 }
Greg Clayton1b654882010-09-19 02:33:57 +00002154 }
2155 }
2156 }
2157 }
2158 }
2159 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
2160 {
2161 if (sc && (sc->function != NULL || sc->symbol != NULL))
2162 {
2163 var_name_begin += ::strlen ("function.");
2164 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2165 {
2166 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002167 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002168 else
2169 s.Printf("symbol[%u]", sc->symbol->GetID());
2170
2171 var_success = true;
2172 }
2173 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2174 {
2175 if (sc->function)
2176 cstr = sc->function->GetName().AsCString (NULL);
2177 else if (sc->symbol)
2178 cstr = sc->symbol->GetName().AsCString (NULL);
2179 if (cstr)
2180 {
2181 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002182
2183 if (sc->block)
2184 {
2185 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2186 if (inline_block)
2187 {
2188 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2189 if (inline_info)
2190 {
2191 s.PutCString(" [inlined] ");
2192 inline_info->GetName().Dump(&s);
2193 }
2194 }
2195 }
Greg Clayton1b654882010-09-19 02:33:57 +00002196 var_success = true;
2197 }
2198 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002199 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2200 {
2201 // Print the function name with arguments in it
2202
2203 if (sc->function)
2204 {
2205 var_success = true;
2206 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2207 cstr = sc->function->GetName().AsCString (NULL);
2208 if (cstr)
2209 {
2210 const InlineFunctionInfo *inline_info = NULL;
2211 VariableListSP variable_list_sp;
2212 bool get_function_vars = true;
2213 if (sc->block)
2214 {
2215 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2216
2217 if (inline_block)
2218 {
2219 get_function_vars = false;
2220 inline_info = sc->block->GetInlinedFunctionInfo();
2221 if (inline_info)
2222 variable_list_sp = inline_block->GetBlockVariableList (true);
2223 }
2224 }
2225
2226 if (get_function_vars)
2227 {
2228 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2229 }
2230
2231 if (inline_info)
2232 {
2233 s.PutCString (cstr);
2234 s.PutCString (" [inlined] ");
2235 cstr = inline_info->GetName().GetCString();
2236 }
2237
2238 VariableList args;
2239 if (variable_list_sp)
2240 {
2241 const size_t num_variables = variable_list_sp->GetSize();
2242 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
2243 {
2244 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
2245 if (var_sp->GetScope() == eValueTypeVariableArgument)
2246 args.AddVariable (var_sp);
2247 }
2248
2249 }
2250 if (args.GetSize() > 0)
2251 {
2252 const char *open_paren = strchr (cstr, '(');
2253 const char *close_paren = NULL;
2254 if (open_paren)
2255 close_paren = strchr (open_paren, ')');
2256
2257 if (open_paren)
2258 s.Write(cstr, open_paren - cstr + 1);
2259 else
2260 {
2261 s.PutCString (cstr);
2262 s.PutChar ('(');
2263 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002264 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002265 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2266 {
2267 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2268 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2269 const char *var_name = var_value_sp->GetName().GetCString();
2270 const char *var_value = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002271 if (arg_idx > 0)
2272 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002273 if (var_value_sp->GetError().Success())
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002274 s.Printf ("%s=%s", var_name, var_value);
Greg Clayton3b188b12012-12-10 22:26:34 +00002275 else
2276 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002277 }
2278
2279 if (close_paren)
2280 s.PutCString (close_paren);
2281 else
2282 s.PutChar(')');
2283
2284 }
2285 else
2286 {
2287 s.PutCString(cstr);
2288 }
2289 }
2290 }
2291 else if (sc->symbol)
2292 {
2293 cstr = sc->symbol->GetName().AsCString (NULL);
2294 if (cstr)
2295 {
2296 s.PutCString(cstr);
2297 var_success = true;
2298 }
2299 }
2300 }
Greg Clayton1b654882010-09-19 02:33:57 +00002301 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2302 {
2303 var_success = addr != NULL;
2304 if (var_success)
2305 {
2306 format_addr = *addr;
2307 calculate_format_addr_function_offset = true;
2308 }
2309 }
2310 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2311 {
2312 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2313 if (var_success)
2314 {
2315 format_addr = sc->line_entry.range.GetBaseAddress();
2316 calculate_format_addr_function_offset = true;
2317 }
2318 }
2319 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2320 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002321 StackFrame *frame = exe_ctx->GetFramePtr();
2322 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002323 if (var_success)
2324 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002325 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002326 calculate_format_addr_function_offset = true;
2327 }
2328 }
2329 }
2330 }
2331 break;
2332
2333 case 'l':
2334 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2335 {
2336 if (sc && sc->line_entry.IsValid())
2337 {
2338 var_name_begin += ::strlen ("line.");
2339 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2340 {
2341 var_name_begin += ::strlen ("file.");
2342
2343 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2344 {
2345 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2346 var_success = format_file_spec;
2347 }
2348 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2349 {
2350 format_file_spec = sc->line_entry.file;
2351 var_success = format_file_spec;
2352 }
2353 }
2354 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2355 {
2356 var_success = true;
2357 s.Printf("%u", sc->line_entry.line);
2358 }
2359 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2360 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2361 {
2362 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2363 if (var_success)
2364 {
2365 format_addr = sc->line_entry.range.GetBaseAddress();
2366 if (var_name_begin[0] == 'e')
2367 format_addr.Slide (sc->line_entry.range.GetByteSize());
2368 }
2369 }
2370 }
2371 }
2372 break;
2373 }
2374
2375 if (var_success)
2376 {
2377 // If format addr is valid, then we need to print an address
2378 if (reg_num != LLDB_INVALID_REGNUM)
2379 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002380 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002381 // We have a register value to display...
2382 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2383 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002384 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002385 }
2386 else
2387 {
2388 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002389 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002390
2391 if (reg_ctx)
2392 {
2393 if (reg_kind != kNumRegisterKinds)
2394 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2395 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2396 var_success = reg_info != NULL;
2397 }
2398 }
2399 }
2400
2401 if (reg_info != NULL)
2402 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002403 RegisterValue reg_value;
2404 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2405 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002406 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002407 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002408 }
2409 }
2410
2411 if (format_file_spec)
2412 {
2413 s << format_file_spec;
2414 }
2415
2416 // If format addr is valid, then we need to print an address
2417 if (format_addr.IsValid())
2418 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002419 var_success = false;
2420
Greg Clayton1b654882010-09-19 02:33:57 +00002421 if (calculate_format_addr_function_offset)
2422 {
2423 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002424
Greg Clayton0603aa92010-10-04 01:05:56 +00002425 if (sc)
2426 {
2427 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002428 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002429 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002430 if (sc->block)
2431 {
2432 // Check to make sure we aren't in an inline
2433 // function. If we are, use the inline block
2434 // range that contains "format_addr" since
2435 // blocks can be discontiguous.
2436 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2437 AddressRange inline_range;
2438 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2439 func_addr = inline_range.GetBaseAddress();
2440 }
2441 }
Greg Claytone7612132012-03-07 21:03:09 +00002442 else if (sc->symbol && sc->symbol->ValueIsAddress())
2443 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002444 }
2445
2446 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002447 {
2448 if (func_addr.GetSection() == format_addr.GetSection())
2449 {
2450 addr_t func_file_addr = func_addr.GetFileAddress();
2451 addr_t addr_file_addr = format_addr.GetFileAddress();
2452 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002453 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002454 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002455 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002456 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002457 }
2458 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002459 {
2460 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2461 if (target)
2462 {
2463 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2464 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2465 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002466 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002467 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002468 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002469 var_success = true;
2470 }
2471 }
Greg Clayton1b654882010-09-19 02:33:57 +00002472 }
2473 }
2474 else
2475 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002476 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002477 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002478 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2479 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002480 if (vaddr == LLDB_INVALID_ADDRESS)
2481 vaddr = format_addr.GetFileAddress ();
2482
2483 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002484 {
Greg Clayton514487e2011-02-15 21:59:32 +00002485 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002486 if (addr_width == 0)
2487 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002488 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002489 var_success = true;
2490 }
Greg Clayton1b654882010-09-19 02:33:57 +00002491 }
2492 }
2493 }
2494
2495 if (var_success == false)
2496 success = false;
2497 }
2498 p = var_name_end;
2499 }
2500 else
2501 break;
2502 }
2503 else
2504 {
2505 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2506 s.PutChar(*p);
2507 }
2508 }
2509 else if (*p == '\\')
2510 {
2511 ++p; // skip the slash
2512 switch (*p)
2513 {
2514 case 'a': s.PutChar ('\a'); break;
2515 case 'b': s.PutChar ('\b'); break;
2516 case 'f': s.PutChar ('\f'); break;
2517 case 'n': s.PutChar ('\n'); break;
2518 case 'r': s.PutChar ('\r'); break;
2519 case 't': s.PutChar ('\t'); break;
2520 case 'v': s.PutChar ('\v'); break;
2521 case '\'': s.PutChar ('\''); break;
2522 case '\\': s.PutChar ('\\'); break;
2523 case '0':
2524 // 1 to 3 octal chars
2525 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002526 // Make a string that can hold onto the initial zero char,
2527 // up to 3 octal digits, and a terminating NULL.
2528 char oct_str[5] = { 0, 0, 0, 0, 0 };
2529
2530 int i;
2531 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2532 oct_str[i] = p[i];
2533
2534 // We don't want to consume the last octal character since
2535 // the main for loop will do this for us, so we advance p by
2536 // one less than i (even if i is zero)
2537 p += i - 1;
2538 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2539 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002540 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002541 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002542 }
Greg Clayton1b654882010-09-19 02:33:57 +00002543 }
2544 break;
2545
2546 case 'x':
2547 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002548 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002549 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002550 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002551
Greg Clayton0603aa92010-10-04 01:05:56 +00002552 // Make a string that can hold onto two hex chars plus a
2553 // NULL terminator
2554 char hex_str[3] = { 0,0,0 };
2555 hex_str[0] = *p;
2556 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002557 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002558 ++p; // Skip the first of the two hex chars
2559 hex_str[1] = *p;
2560 }
2561
2562 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2563 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002564 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002565 }
2566 else
2567 {
2568 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002569 }
2570 break;
2571
2572 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002573 // Just desensitize any other character by just printing what
2574 // came after the '\'
2575 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002576 break;
2577
2578 }
2579
2580 }
2581 }
2582 if (end)
2583 *end = p;
2584 return success;
2585}
2586
Jim Ingham228063c2012-02-21 02:23:08 +00002587void
2588Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2589{
Jim Ingham4f02b222012-02-22 22:49:20 +00002590 // For simplicity's sake, I am not going to deal with how to close down any
2591 // open logging streams, I just redirect everything from here on out to the
2592 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002593 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2594}
2595
2596bool
2597Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2598{
2599 Log::Callbacks log_callbacks;
2600
2601 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002602 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002603 {
2604 log_stream_sp = m_log_callback_stream_sp;
2605 // For now when using the callback mode you always get thread & timestamp.
2606 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2607 }
2608 else if (log_file == NULL || *log_file == '\0')
2609 {
2610 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2611 }
2612 else
2613 {
2614 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002615 if (pos != m_log_streams.end())
2616 log_stream_sp = pos->second.lock();
2617 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002618 {
2619 log_stream_sp.reset (new StreamFile (log_file));
2620 m_log_streams[log_file] = log_stream_sp;
2621 }
Jim Ingham228063c2012-02-21 02:23:08 +00002622 }
2623 assert (log_stream_sp.get());
2624
2625 if (log_options == 0)
2626 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2627
2628 if (Log::GetLogChannelCallbacks (channel, log_callbacks))
2629 {
2630 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2631 return true;
2632 }
2633 else
2634 {
2635 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2636 if (log_channel_sp)
2637 {
2638 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2639 {
2640 return true;
2641 }
2642 else
2643 {
2644 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2645 return false;
2646 }
2647 }
2648 else
2649 {
2650 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2651 return false;
2652 }
2653 }
2654 return false;
2655}
2656