blob: fa4afb18e84379400421a031b7f6eb371dc7c790 [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"
Enrico Granata84a53df2013-05-20 22:29:23 +000050#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000052#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54using namespace lldb;
55using namespace lldb_private;
56
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
Greg Clayton1b654882010-09-19 02:33:57 +000058static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000059static lldb::user_id_t g_unique_id = 1;
60
Greg Clayton1b654882010-09-19 02:33:57 +000061#pragma mark Static Functions
62
63static Mutex &
64GetDebuggerListMutex ()
65{
66 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
67 return g_mutex;
68}
69
70typedef std::vector<DebuggerSP> DebuggerList;
71
72static DebuggerList &
73GetDebuggerList()
74{
75 // hide the static debugger list inside a singleton accessor to avoid
76 // global init contructors
77 static DebuggerList g_list;
78 return g_list;
79}
Greg Claytone372b982011-11-21 21:44:34 +000080
81OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000082g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000083{
Greg Clayton67cc0632012-08-22 17:17:09 +000084 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
85 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
86 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000087 { 0, NULL, NULL }
88};
89
Greg Clayton67cc0632012-08-22 17:17:09 +000090OptionEnumValueElement
91g_language_enumerators[] =
92{
93 { eScriptLanguageNone, "none", "Disable scripting languages."},
94 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
95 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000096 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000097};
Greg Claytone372b982011-11-21 21:44:34 +000098
Greg Clayton67cc0632012-08-22 17:17:09 +000099#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
100#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
101
102#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
103 "{, ${frame.pc}}"\
104 MODULE_WITH_FUNC\
105 FILE_AND_LINE\
Greg Clayton85d0c572013-04-04 20:40:35 +0000106 "{, name = '${thread.name}}"\
107 "{, queue = '${thread.queue}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000108 "{, stop reason = ${thread.stop-reason}}"\
109 "{\\nReturn value: ${thread.return-value}}"\
110 "\\n"
111
112#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
113 MODULE_WITH_FUNC\
114 FILE_AND_LINE\
115 "\\n"
116
117
118
Greg Clayton754a9362012-08-23 00:22:02 +0000119static PropertyDefinition
120g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000121{
122{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
123{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
124{ "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 +0000125{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000126{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
127{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
128{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
129{ "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." },
130{ "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." },
131{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
132{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
133{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Greg Claytone8cd0c92012-10-19 18:02:49 +0000134
135 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000136};
137
138enum
139{
140 ePropertyAutoConfirm = 0,
141 ePropertyFrameFormat,
142 ePropertyNotiftVoid,
143 ePropertyPrompt,
144 ePropertyScriptLanguage,
145 ePropertyStopDisassemblyCount,
146 ePropertyStopDisassemblyDisplay,
147 ePropertyStopLineCountAfter,
148 ePropertyStopLineCountBefore,
149 ePropertyTerminalWidth,
150 ePropertyThreadFormat,
151 ePropertyUseExternalEditor
152};
153
154//
155//const char *
156//Debugger::GetFrameFormat() const
157//{
158// return m_properties_sp->GetFrameFormat();
159//}
160//const char *
161//Debugger::GetThreadFormat() const
162//{
163// return m_properties_sp->GetThreadFormat();
164//}
165//
Greg Clayton4c054102012-09-01 00:38:36 +0000166
167
168Error
169Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
170 VarSetOperationType op,
171 const char *property_path,
172 const char *value)
173{
Enrico Granata84a53df2013-05-20 22:29:23 +0000174 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
175 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000176 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000177 if (is_load_script && exe_ctx->GetTargetSP())
178 {
179 target_sp = exe_ctx->GetTargetSP();
180 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
181 }
Greg Clayton4c054102012-09-01 00:38:36 +0000182 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
183 if (error.Success())
184 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000185 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000186 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
187 {
188 const char *new_prompt = GetPrompt();
189 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
190 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
191 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000192 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000193 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000194 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000195 {
196 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000197 StreamString feedback_stream;
198 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000199 {
200 for (auto error : errors)
201 {
Enrico Granata97303392013-05-21 00:00:30 +0000202 GetErrorStream().Printf("%s\n",error.AsCString());
Enrico Granata84a53df2013-05-20 22:29:23 +0000203 }
Enrico Granata97303392013-05-21 00:00:30 +0000204 if (feedback_stream.GetSize())
205 GetErrorStream().Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000206 }
207 }
208 }
Greg Clayton4c054102012-09-01 00:38:36 +0000209 }
210 return error;
211}
212
Greg Clayton67cc0632012-08-22 17:17:09 +0000213bool
214Debugger::GetAutoConfirm () const
215{
216 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000217 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000218}
219
220const char *
221Debugger::GetFrameFormat() const
222{
223 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000224 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000225}
226
227bool
228Debugger::GetNotifyVoid () const
229{
230 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000231 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000232}
233
234const char *
235Debugger::GetPrompt() const
236{
237 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000238 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000239}
240
241void
242Debugger::SetPrompt(const char *p)
243{
244 const uint32_t idx = ePropertyPrompt;
245 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
246 const char *new_prompt = GetPrompt();
247 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
248 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
249}
250
251const char *
252Debugger::GetThreadFormat() const
253{
254 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000255 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000256}
257
258lldb::ScriptLanguage
259Debugger::GetScriptLanguage() const
260{
261 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000262 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000263}
264
265bool
266Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
267{
268 const uint32_t idx = ePropertyScriptLanguage;
269 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
270}
271
272uint32_t
273Debugger::GetTerminalWidth () const
274{
275 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000276 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000277}
278
279bool
280Debugger::SetTerminalWidth (uint32_t term_width)
281{
282 const uint32_t idx = ePropertyTerminalWidth;
283 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
284}
285
286bool
287Debugger::GetUseExternalEditor () const
288{
289 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000290 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000291}
292
293bool
294Debugger::SetUseExternalEditor (bool b)
295{
296 const uint32_t idx = ePropertyUseExternalEditor;
297 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
298}
299
300uint32_t
301Debugger::GetStopSourceLineCount (bool before) const
302{
303 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000304 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000305}
306
307Debugger::StopDisassemblyType
308Debugger::GetStopDisassemblyDisplay () const
309{
310 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000311 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000312}
313
314uint32_t
315Debugger::GetDisassemblyLineCount () const
316{
317 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000318 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000319}
Greg Claytone372b982011-11-21 21:44:34 +0000320
Greg Clayton1b654882010-09-19 02:33:57 +0000321#pragma mark Debugger
322
Greg Clayton67cc0632012-08-22 17:17:09 +0000323//const DebuggerPropertiesSP &
324//Debugger::GetSettings() const
325//{
326// return m_properties_sp;
327//}
328//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000329
Caroline Tice2f88aad2011-01-14 00:29:16 +0000330int
331Debugger::TestDebuggerRefCount ()
332{
333 return g_shared_debugger_refcount;
334}
335
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336void
337Debugger::Initialize ()
338{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000339 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000340 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341}
342
343void
344Debugger::Terminate ()
345{
Greg Clayton66111032010-06-23 01:19:29 +0000346 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347 {
Greg Clayton66111032010-06-23 01:19:29 +0000348 g_shared_debugger_refcount--;
349 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 {
Greg Claytondbe54502010-11-19 03:46:01 +0000351 lldb_private::WillTerminate();
352 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000353
354 // Clear our master list of debugger objects
355 Mutex::Locker locker (GetDebuggerListMutex ());
356 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 }
359}
360
Caroline Tice20bd37f2011-03-10 22:14:10 +0000361void
362Debugger::SettingsInitialize ()
363{
Greg Clayton6920b522012-08-22 18:39:03 +0000364 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000365}
366
367void
368Debugger::SettingsTerminate ()
369{
Greg Clayton6920b522012-08-22 18:39:03 +0000370 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000371}
372
Enrico Granata21dfcd92012-09-28 23:57:51 +0000373bool
Enrico Granatae743c782013-04-24 21:29:08 +0000374Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000375{
376 lldb::DynamicLibrarySP dynlib_sp(new lldb_private::DynamicLibrary(spec));
Enrico Granatae743c782013-04-24 21:29:08 +0000377 if (!dynlib_sp || dynlib_sp->IsValid() == false)
378 {
379 if (spec.Exists())
380 error.SetErrorString("this file does not represent a loadable dylib");
381 else
382 error.SetErrorString("no such file");
383 return false;
384 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000385 lldb::DebuggerSP debugger_sp(shared_from_this());
386 lldb::SBDebugger debugger_sb(debugger_sp);
387 // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
388 LLDBCommandPluginInit init_func = dynlib_sp->GetSymbol<LLDBCommandPluginInit>("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
389 if (!init_func)
Enrico Granatae743c782013-04-24 21:29:08 +0000390 {
391 error.SetErrorString("cannot find the initialization function lldb::PluginInitialize(lldb::SBDebugger)");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000392 return false;
Enrico Granatae743c782013-04-24 21:29:08 +0000393 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000394 if (init_func(debugger_sb))
395 {
396 m_loaded_plugins.push_back(dynlib_sp);
397 return true;
398 }
Enrico Granatae743c782013-04-24 21:29:08 +0000399 error.SetErrorString("dylib refused to be loaded");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000400 return false;
401}
402
403static FileSpec::EnumerateDirectoryResult
404LoadPluginCallback
405(
406 void *baton,
407 FileSpec::FileType file_type,
408 const FileSpec &file_spec
409 )
410{
411 Error error;
412
413 static ConstString g_dylibext("dylib");
414
415 if (!baton)
416 return FileSpec::eEnumerateDirectoryResultQuit;
417
418 Debugger *debugger = (Debugger*)baton;
419
420 // If we have a regular file, a symbolic link or unknown file type, try
421 // and process the file. We must handle unknown as sometimes the directory
422 // enumeration might be enumerating a file system that doesn't have correct
423 // file type information.
424 if (file_type == FileSpec::eFileTypeRegular ||
425 file_type == FileSpec::eFileTypeSymbolicLink ||
426 file_type == FileSpec::eFileTypeUnknown )
427 {
428 FileSpec plugin_file_spec (file_spec);
429 plugin_file_spec.ResolvePath ();
430
431 if (plugin_file_spec.GetFileNameExtension() != g_dylibext)
432 return FileSpec::eEnumerateDirectoryResultNext;
433
Enrico Granatae743c782013-04-24 21:29:08 +0000434 Error plugin_load_error;
435 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000436
437 return FileSpec::eEnumerateDirectoryResultNext;
438 }
439
440 else if (file_type == FileSpec::eFileTypeUnknown ||
441 file_type == FileSpec::eFileTypeDirectory ||
442 file_type == FileSpec::eFileTypeSymbolicLink )
443 {
444 // Try and recurse into anything that a directory or symbolic link.
445 // We must also do this for unknown as sometimes the directory enumeration
446 // might be enurating a file system that doesn't have correct file type
447 // information.
448 return FileSpec::eEnumerateDirectoryResultEnter;
449 }
450
451 return FileSpec::eEnumerateDirectoryResultNext;
452}
453
454void
455Debugger::InstanceInitialize ()
456{
457 FileSpec dir_spec;
458 const bool find_directories = true;
459 const bool find_files = true;
460 const bool find_other = true;
461 char dir_path[PATH_MAX];
462 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
463 {
464 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
465 {
466 FileSpec::EnumerateDirectory (dir_path,
467 find_directories,
468 find_files,
469 find_other,
470 LoadPluginCallback,
471 this);
472 }
473 }
474
475 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
476 {
477 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
478 {
479 FileSpec::EnumerateDirectory (dir_path,
480 find_directories,
481 find_files,
482 find_other,
483 LoadPluginCallback,
484 this);
485 }
486 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000487
488 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000489}
490
Greg Clayton66111032010-06-23 01:19:29 +0000491DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000492Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000493{
Jim Ingham228063c2012-02-21 02:23:08 +0000494 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000495 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000496 {
497 Mutex::Locker locker (GetDebuggerListMutex ());
498 GetDebuggerList().push_back(debugger_sp);
499 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000500 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000501 return debugger_sp;
502}
503
Caroline Ticee02657b2011-01-22 01:02:07 +0000504void
Greg Clayton4d122c42011-09-17 08:33:22 +0000505Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000506{
507 if (debugger_sp.get() == NULL)
508 return;
509
Jim Ingham8314c522011-09-15 21:36:42 +0000510 debugger_sp->Clear();
511
Greg Claytonc15f55e2012-03-30 20:53:46 +0000512 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +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)
Caroline Ticee02657b2011-01-22 01:02:07 +0000518 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000519 if ((*pos).get() == debugger_sp.get())
520 {
521 debugger_list.erase (pos);
522 return;
523 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000524 }
525 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000526}
527
Greg Clayton4d122c42011-09-17 08:33:22 +0000528DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000529Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
530{
Greg Clayton4d122c42011-09-17 08:33:22 +0000531 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000532 if (g_shared_debugger_refcount > 0)
533 {
534 Mutex::Locker locker (GetDebuggerListMutex ());
535 DebuggerList &debugger_list = GetDebuggerList();
536 DebuggerList::iterator pos, end = debugger_list.end();
537
538 for (pos = debugger_list.begin(); pos != end; ++pos)
539 {
540 if ((*pos).get()->m_instance_name == instance_name)
541 {
542 debugger_sp = *pos;
543 break;
544 }
545 }
546 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000547 return debugger_sp;
548}
Greg Clayton66111032010-06-23 01:19:29 +0000549
550TargetSP
551Debugger::FindTargetWithProcessID (lldb::pid_t pid)
552{
Greg Clayton4d122c42011-09-17 08:33:22 +0000553 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000554 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000555 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000556 Mutex::Locker locker (GetDebuggerListMutex ());
557 DebuggerList &debugger_list = GetDebuggerList();
558 DebuggerList::iterator pos, end = debugger_list.end();
559 for (pos = debugger_list.begin(); pos != end; ++pos)
560 {
561 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
562 if (target_sp)
563 break;
564 }
Greg Clayton66111032010-06-23 01:19:29 +0000565 }
566 return target_sp;
567}
568
Greg Claytone4e45922011-11-16 05:37:56 +0000569TargetSP
570Debugger::FindTargetWithProcess (Process *process)
571{
572 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000573 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000574 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000575 Mutex::Locker locker (GetDebuggerListMutex ());
576 DebuggerList &debugger_list = GetDebuggerList();
577 DebuggerList::iterator pos, end = debugger_list.end();
578 for (pos = debugger_list.begin(); pos != end; ++pos)
579 {
580 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
581 if (target_sp)
582 break;
583 }
Greg Claytone4e45922011-11-16 05:37:56 +0000584 }
585 return target_sp;
586}
587
Jim Ingham228063c2012-02-21 02:23:08 +0000588Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000589 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000590 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000591 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 m_input_file (),
593 m_output_file (),
594 m_error_file (),
Jim Inghamc5917d92012-11-30 20:23:19 +0000595 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000596 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000597 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 m_listener ("lldb.Debugger"),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000599 m_source_manager_ap(),
Jim Inghame37d6052011-09-13 00:29:56 +0000600 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000601 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000602 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000603 m_input_reader_data (),
604 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605{
Greg Clayton67cc0632012-08-22 17:17:09 +0000606 char instance_cstr[256];
607 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
608 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000609 if (log_callback)
610 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000611 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000612 // Always add our default platform to the platform list
613 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
614 assert (default_platform_sp.get());
615 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000616
Greg Clayton754a9362012-08-23 00:22:02 +0000617 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000618 m_collection_sp->AppendProperty (ConstString("target"),
619 ConstString("Settings specify to debugging targets."),
620 true,
621 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000622 if (m_command_interpreter_ap.get())
623 {
624 m_collection_sp->AppendProperty (ConstString("interpreter"),
625 ConstString("Settings specify to the debugger's command interpreter."),
626 true,
627 m_command_interpreter_ap->GetValueProperties());
628 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000629 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
630 term_width->SetMinimumValue(10);
631 term_width->SetMaximumValue(1024);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632}
633
634Debugger::~Debugger ()
635{
Jim Ingham8314c522011-09-15 21:36:42 +0000636 Clear();
637}
638
639void
640Debugger::Clear()
641{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000642 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000643 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000644 int num_targets = m_target_list.GetNumTargets();
645 for (int i = 0; i < num_targets; i++)
646 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000647 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
648 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000649 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000650 ProcessSP process_sp (target_sp->GetProcessSP());
651 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000652 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000653 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000654 }
Greg Clayton66111032010-06-23 01:19:29 +0000655 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000656 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000657
658 // Close the input file _before_ we close the input read communications class
659 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000660 m_terminal_state.Clear();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000661 GetInputFile().Close ();
662 // Now that we have closed m_input_file, we can now tell our input communication
663 // class to close down. Its read thread should quickly exit after we close
664 // the input file handle above.
665 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000666}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667
668bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000669Debugger::GetCloseInputOnEOF () const
670{
671 return m_input_comm.GetCloseOnEOF();
672}
673
674void
675Debugger::SetCloseInputOnEOF (bool b)
676{
677 m_input_comm.SetCloseOnEOF(b);
678}
679
680bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681Debugger::GetAsyncExecution ()
682{
Greg Clayton66111032010-06-23 01:19:29 +0000683 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684}
685
686void
687Debugger::SetAsyncExecution (bool async_execution)
688{
Greg Clayton66111032010-06-23 01:19:29 +0000689 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690}
691
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692
693void
694Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
695{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000696 File &in_file = GetInputFile();
697 in_file.SetStream (fh, tranfer_ownership);
698 if (in_file.IsValid() == false)
699 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700
701 // Disconnect from any old connection if we had one
702 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000703 // Pass false as the second argument to ConnectionFileDescriptor below because
704 // our "in_file" above will already take ownership if requested and we don't
705 // want to objects trying to own and close a file descriptor.
706 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
Jim Inghamc5917d92012-11-30 20:23:19 +0000708
709 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
710 SaveInputTerminalState ();
711
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 Error error;
713 if (m_input_comm.StartReadThread (&error) == false)
714 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000715 File &err_file = GetErrorFile();
716
717 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
718 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720}
721
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000722void
723Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
724{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000725 File &out_file = GetOutputFile();
726 out_file.SetStream (fh, tranfer_ownership);
727 if (out_file.IsValid() == false)
728 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000729
Enrico Granatab5887262012-10-29 21:18:03 +0000730 // do not create the ScriptInterpreter just for setting the output file handle
731 // as the constructor will know how to do the right thing on its own
732 const bool can_create = false;
733 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
734 if (script_interpreter)
735 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736}
737
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738void
739Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
740{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000741 File &err_file = GetErrorFile();
742 err_file.SetStream (fh, tranfer_ownership);
743 if (err_file.IsValid() == false)
744 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745}
746
Jim Inghamc5917d92012-11-30 20:23:19 +0000747void
748Debugger::SaveInputTerminalState ()
749{
750 File &in_file = GetInputFile();
751 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
752 m_terminal_state.Save(in_file.GetDescriptor(), true);
753}
754
755void
756Debugger::RestoreInputTerminalState ()
757{
758 m_terminal_state.Restore();
759}
760
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000762Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763{
764 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000765 TargetSP target_sp(GetSelectedTarget());
766 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767
768 if (target_sp)
769 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000770 ProcessSP process_sp (target_sp->GetProcessSP());
771 exe_ctx.SetProcessSP (process_sp);
772 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000774 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
775 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000777 exe_ctx.SetThreadSP (thread_sp);
778 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
779 if (exe_ctx.GetFramePtr() == NULL)
780 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781 }
782 }
783 }
784 return exe_ctx;
785
786}
787
Caroline Ticeb44880c2011-02-10 01:15:13 +0000788InputReaderSP
789Debugger::GetCurrentInputReader ()
790{
791 InputReaderSP reader_sp;
792
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000793 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000794 {
795 // Clear any finished readers from the stack
796 while (CheckIfTopInputReaderIsDone()) ;
797
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000798 if (!m_input_reader_stack.IsEmpty())
799 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000800 }
801
802 return reader_sp;
803}
804
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805void
806Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
807{
Caroline Ticeefed6132010-11-19 20:47:54 +0000808 if (bytes_len > 0)
809 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
810 else
811 ((Debugger *)baton)->DispatchInputEndOfFile ();
812}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813
814
815void
816Debugger::DispatchInput (const char *bytes, size_t bytes_len)
817{
Caroline Ticeefed6132010-11-19 20:47:54 +0000818 if (bytes == NULL || bytes_len == 0)
819 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820
821 WriteToDefaultReader (bytes, bytes_len);
822}
823
824void
Caroline Ticeefed6132010-11-19 20:47:54 +0000825Debugger::DispatchInputInterrupt ()
826{
827 m_input_reader_data.clear();
828
Caroline Ticeb44880c2011-02-10 01:15:13 +0000829 InputReaderSP reader_sp (GetCurrentInputReader ());
830 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000831 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000832 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000833
Caroline Ticeb44880c2011-02-10 01:15:13 +0000834 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000835 while (CheckIfTopInputReaderIsDone ()) ;
836 }
837}
838
839void
840Debugger::DispatchInputEndOfFile ()
841{
842 m_input_reader_data.clear();
843
Caroline Ticeb44880c2011-02-10 01:15:13 +0000844 InputReaderSP reader_sp (GetCurrentInputReader ());
845 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000846 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000847 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000848
Caroline Ticeb44880c2011-02-10 01:15:13 +0000849 // 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 +0000850 while (CheckIfTopInputReaderIsDone ()) ;
851 }
852}
853
854void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000855Debugger::CleanUpInputReaders ()
856{
857 m_input_reader_data.clear();
858
Caroline Ticeb44880c2011-02-10 01:15:13 +0000859 // 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 +0000860 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000861 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000862 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000863 if (reader_sp)
864 {
865 reader_sp->Notify (eInputReaderEndOfFile);
866 reader_sp->SetIsDone (true);
867 }
868 }
869}
870
871void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000872Debugger::NotifyTopInputReader (InputReaderAction notification)
873{
874 InputReaderSP reader_sp (GetCurrentInputReader());
875 if (reader_sp)
876 {
877 reader_sp->Notify (notification);
878
879 // Flush out any input readers that are done.
880 while (CheckIfTopInputReaderIsDone ())
881 /* Do nothing. */;
882 }
883}
884
Caroline Tice9088b062011-05-09 23:06:58 +0000885bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000886Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000887{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000888 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000889
Caroline Ticed61c10b2011-06-16 16:27:19 +0000890 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000891}
892
893
Caroline Tice969ed3d2011-05-02 20:41:46 +0000894void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
896{
897 if (bytes && bytes_len)
898 m_input_reader_data.append (bytes, bytes_len);
899
900 if (m_input_reader_data.empty())
901 return;
902
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000903 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000905 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000906 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907 if (!reader_sp)
908 break;
909
Greg Clayton471b31c2010-07-20 22:52:08 +0000910 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911 m_input_reader_data.size());
912 if (bytes_handled)
913 {
914 m_input_reader_data.erase (0, bytes_handled);
915 }
916 else
917 {
918 // No bytes were handled, we might not have reached our
919 // granularity, just return and wait for more data
920 break;
921 }
922 }
923
Caroline Ticeb44880c2011-02-10 01:15:13 +0000924 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925 while (CheckIfTopInputReaderIsDone ())
926 /* Do nothing. */;
927
928}
929
930void
931Debugger::PushInputReader (const InputReaderSP& reader_sp)
932{
933 if (!reader_sp)
934 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000935
936 // Deactivate the old top reader
937 InputReaderSP top_reader_sp (GetCurrentInputReader ());
938
939 if (top_reader_sp)
940 top_reader_sp->Notify (eInputReaderDeactivate);
941
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000942 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943 reader_sp->Notify (eInputReaderActivate);
944 ActivateInputReader (reader_sp);
945}
946
947bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000948Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949{
950 bool result = false;
951
952 // The reader on the stop of the stack is done, so let the next
953 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000954 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000956 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000957 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958
959 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
960 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000961 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962 reader_sp->Notify (eInputReaderDeactivate);
963 reader_sp->Notify (eInputReaderDone);
964 result = true;
965
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000966 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000968 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000969 if (reader_sp)
970 {
971 ActivateInputReader (reader_sp);
972 reader_sp->Notify (eInputReaderReactivate);
973 }
974 }
975 }
976 }
977 return result;
978}
979
980bool
981Debugger::CheckIfTopInputReaderIsDone ()
982{
983 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000984 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000986 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000987 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988
989 if (reader_sp && reader_sp->IsDone())
990 {
991 result = true;
992 PopInputReader (reader_sp);
993 }
994 }
995 return result;
996}
997
998void
999Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
1000{
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001001 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001003 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001005 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +00001006
1007 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008
Greg Claytona3406612011-02-07 23:24:47 +00001009 switch (reader_sp->GetGranularity())
1010 {
1011 case eInputReaderGranularityByte:
1012 case eInputReaderGranularityWord:
1013 tty.SetCanonical (false);
1014 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015
Greg Claytona3406612011-02-07 23:24:47 +00001016 case eInputReaderGranularityLine:
1017 case eInputReaderGranularityAll:
1018 tty.SetCanonical (true);
1019 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020
Greg Claytona3406612011-02-07 23:24:47 +00001021 default:
1022 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023 }
1024 }
1025}
Greg Clayton66111032010-06-23 01:19:29 +00001026
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001027StreamSP
1028Debugger::GetAsyncOutputStream ()
1029{
1030 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1031 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1032}
1033
1034StreamSP
1035Debugger::GetAsyncErrorStream ()
1036{
1037 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1038 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1039}
1040
Greg Claytonc7bece562013-01-25 18:06:21 +00001041size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001042Debugger::GetNumDebuggers()
1043{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001044 if (g_shared_debugger_refcount > 0)
1045 {
1046 Mutex::Locker locker (GetDebuggerListMutex ());
1047 return GetDebuggerList().size();
1048 }
1049 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001050}
1051
1052lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001053Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001054{
1055 DebuggerSP debugger_sp;
1056
Greg Claytonc15f55e2012-03-30 20:53:46 +00001057 if (g_shared_debugger_refcount > 0)
1058 {
1059 Mutex::Locker locker (GetDebuggerListMutex ());
1060 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001061
Greg Claytonc15f55e2012-03-30 20:53:46 +00001062 if (index < debugger_list.size())
1063 debugger_sp = debugger_list[index];
1064 }
1065
Enrico Granata061858c2012-02-15 02:34:21 +00001066 return debugger_sp;
1067}
1068
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001069DebuggerSP
1070Debugger::FindDebuggerWithID (lldb::user_id_t id)
1071{
Greg Clayton4d122c42011-09-17 08:33:22 +00001072 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001073
Greg Claytonc15f55e2012-03-30 20:53:46 +00001074 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001075 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001076 Mutex::Locker locker (GetDebuggerListMutex ());
1077 DebuggerList &debugger_list = GetDebuggerList();
1078 DebuggerList::iterator pos, end = debugger_list.end();
1079 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001080 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001081 if ((*pos).get()->GetID() == id)
1082 {
1083 debugger_sp = *pos;
1084 break;
1085 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001086 }
1087 }
1088 return debugger_sp;
1089}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001090
Greg Clayton1b654882010-09-19 02:33:57 +00001091static void
1092TestPromptFormats (StackFrame *frame)
1093{
1094 if (frame == NULL)
1095 return;
1096
1097 StreamString s;
1098 const char *prompt_format =
1099 "{addr = '${addr}'\n}"
1100 "{process.id = '${process.id}'\n}"
1101 "{process.name = '${process.name}'\n}"
1102 "{process.file.basename = '${process.file.basename}'\n}"
1103 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1104 "{thread.id = '${thread.id}'\n}"
1105 "{thread.index = '${thread.index}'\n}"
1106 "{thread.name = '${thread.name}'\n}"
1107 "{thread.queue = '${thread.queue}'\n}"
1108 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1109 "{target.arch = '${target.arch}'\n}"
1110 "{module.file.basename = '${module.file.basename}'\n}"
1111 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1112 "{file.basename = '${file.basename}'\n}"
1113 "{file.fullpath = '${file.fullpath}'\n}"
1114 "{frame.index = '${frame.index}'\n}"
1115 "{frame.pc = '${frame.pc}'\n}"
1116 "{frame.sp = '${frame.sp}'\n}"
1117 "{frame.fp = '${frame.fp}'\n}"
1118 "{frame.flags = '${frame.flags}'\n}"
1119 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1120 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1121 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1122 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1123 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1124 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1125 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1126 "{function.id = '${function.id}'\n}"
1127 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001128 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001129 "{function.addr-offset = '${function.addr-offset}'\n}"
1130 "{function.line-offset = '${function.line-offset}'\n}"
1131 "{function.pc-offset = '${function.pc-offset}'\n}"
1132 "{line.file.basename = '${line.file.basename}'\n}"
1133 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1134 "{line.number = '${line.number}'\n}"
1135 "{line.start-addr = '${line.start-addr}'\n}"
1136 "{line.end-addr = '${line.end-addr}'\n}"
1137;
1138
1139 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1140 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001141 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +00001142 const char *end = NULL;
1143 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
1144 {
1145 printf("%s\n", s.GetData());
1146 }
1147 else
1148 {
1149 printf ("error: at '%s'\n", end);
1150 printf ("what we got: %s\n", s.GetData());
1151 }
1152}
1153
Enrico Granata9fc19442011-07-06 02:13:41 +00001154static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001155ScanFormatDescriptor (const char* var_name_begin,
1156 const char* var_name_end,
1157 const char** var_name_final,
1158 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001159 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001160 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001161{
Greg Clayton5160ce52013-03-27 23:08:40 +00001162 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001163 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001164 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001165 {
1166 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001167 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001168 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001169 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001170 else
1171 {
1172 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001173 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001174 if (log)
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001175 log->Printf("ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
1176 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001177 true,
1178 *custom_format) )
1179 {
Enrico Granatae992a082011-07-22 17:03:19 +00001180 if (log)
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001181 log->Printf("ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
1182
1183 switch (format_name.front())
1184 {
1185 case '@': // if this is an @ sign, print ObjC description
1186 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1187 break;
1188 case 'V': // if this is a V, print the value using the default format
1189 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1190 break;
1191 case 'L': // if this is an L, print the location of the value
1192 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1193 break;
1194 case 'S': // if this is an S, print the summary after all
1195 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1196 break;
1197 case '#': // if this is a '#', print the number of children
1198 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1199 break;
1200 case 'T': // if this is a 'T', print the type
1201 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1202 break;
1203 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001204 if (log)
1205 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001206 break;
1207 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001208 }
1209 // a good custom format tells us to print the value using it
1210 else
Enrico Granatae992a082011-07-22 17:03:19 +00001211 {
1212 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001213 log->Printf("ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001214 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001215 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001216 }
Enrico Granatae992a082011-07-22 17:03:19 +00001217 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001218 log->Printf("ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001219 *custom_format,
1220 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001221 return true;
1222}
1223
1224static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001225ScanBracketedRange (const char* var_name_begin,
1226 const char* var_name_end,
1227 const char* var_name_final,
1228 const char** open_bracket_position,
1229 const char** separator_position,
1230 const char** close_bracket_position,
1231 const char** var_name_final_if_array_range,
1232 int64_t* index_lower,
1233 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001234{
Greg Clayton5160ce52013-03-27 23:08:40 +00001235 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001236 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001237 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001238 {
1239 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1240 *close_bracket_position = ::strchr(*open_bracket_position,']');
1241 // as usual, we assume that [] will come before %
1242 //printf("trying to expand a []\n");
1243 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001244 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001245 {
Enrico Granatae992a082011-07-22 17:03:19 +00001246 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001247 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001248 *index_lower = 0;
1249 }
1250 else if (*separator_position == NULL || *separator_position > var_name_end)
1251 {
1252 char *end = NULL;
1253 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1254 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001255 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001256 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001257 }
Greg Clayton34132752011-07-06 04:07:21 +00001258 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001259 {
1260 char *end = NULL;
1261 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1262 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001263 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001264 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001265 }
1266 else
Enrico Granatae992a082011-07-22 17:03:19 +00001267 {
1268 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001269 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001270 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001271 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001272 if (*index_lower > *index_higher && *index_higher > 0)
1273 {
Enrico Granatae992a082011-07-22 17:03:19 +00001274 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001275 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001276 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001277 *index_lower = *index_higher;
1278 *index_higher = temp;
1279 }
1280 }
Enrico Granatae992a082011-07-22 17:03:19 +00001281 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001282 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001283 return true;
1284}
1285
Enrico Granata9fc19442011-07-06 02:13:41 +00001286static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001287ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001288 size_t index,
Enrico Granatadc940732011-08-23 00:32:52 +00001289 StackFrame* frame,
1290 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001291{
Greg Clayton5160ce52013-03-27 23:08:40 +00001292 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001293 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001294 std::string ptr_deref_buffer(10,0);
1295 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001296 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001297 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001298 const char* first_unparsed;
1299 ValueObject::GetValueForExpressionPathOptions options;
1300 ValueObject::ExpressionPathEndResultType final_value_type;
1301 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001302 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001303 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001304 &first_unparsed,
1305 &reason_to_stop,
1306 &final_value_type,
1307 options,
1308 &what_next);
1309 if (!item)
1310 {
Enrico Granatae992a082011-07-22 17:03:19 +00001311 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001312 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001313 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001314 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001315 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001316 else
1317 {
Enrico Granatae992a082011-07-22 17:03:19 +00001318 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001319 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001320 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001321 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001322 }
1323 return item;
1324}
1325
Greg Clayton1b654882010-09-19 02:33:57 +00001326bool
1327Debugger::FormatPrompt
1328(
1329 const char *format,
1330 const SymbolContext *sc,
1331 const ExecutionContext *exe_ctx,
1332 const Address *addr,
1333 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001334 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001335 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001336)
1337{
Enrico Granatac482a192011-08-17 22:13:59 +00001338 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001339 bool success = true;
1340 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001341 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001342 for (p = format; *p != '\0'; ++p)
1343 {
Enrico Granatac482a192011-08-17 22:13:59 +00001344 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001345 {
Enrico Granatac482a192011-08-17 22:13:59 +00001346 valobj = realvalobj;
1347 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001348 }
Greg Clayton1b654882010-09-19 02:33:57 +00001349 size_t non_special_chars = ::strcspn (p, "${}\\");
1350 if (non_special_chars > 0)
1351 {
1352 if (success)
1353 s.Write (p, non_special_chars);
1354 p += non_special_chars;
1355 }
1356
1357 if (*p == '\0')
1358 {
1359 break;
1360 }
1361 else if (*p == '{')
1362 {
1363 // Start a new scope that must have everything it needs if it is to
1364 // to make it into the final output stream "s". If you want to make
1365 // a format that only prints out the function or symbol name if there
1366 // is one in the symbol context you can use:
1367 // "{function =${function.name}}"
1368 // The first '{' starts a new scope that end with the matching '}' at
1369 // the end of the string. The contents "function =${function.name}"
1370 // will then be evaluated and only be output if there is a function
1371 // or symbol with a valid name.
1372 StreamString sub_strm;
1373
1374 ++p; // Skip the '{'
1375
Enrico Granatac482a192011-08-17 22:13:59 +00001376 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001377 {
1378 // The stream had all it needed
1379 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1380 }
1381 if (*p != '}')
1382 {
1383 success = false;
1384 break;
1385 }
1386 }
1387 else if (*p == '}')
1388 {
1389 // End of a enclosing scope
1390 break;
1391 }
1392 else if (*p == '$')
1393 {
1394 // We have a prompt variable to print
1395 ++p;
1396 if (*p == '{')
1397 {
1398 ++p;
1399 const char *var_name_begin = p;
1400 const char *var_name_end = ::strchr (p, '}');
1401
1402 if (var_name_end && var_name_begin < var_name_end)
1403 {
1404 // if we have already failed to parse, skip this variable
1405 if (success)
1406 {
1407 const char *cstr = NULL;
1408 Address format_addr;
1409 bool calculate_format_addr_function_offset = false;
1410 // Set reg_kind and reg_num to invalid values
1411 RegisterKind reg_kind = kNumRegisterKinds;
1412 uint32_t reg_num = LLDB_INVALID_REGNUM;
1413 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001414 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001415 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001416 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001417 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1418 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001419
Greg Clayton1b654882010-09-19 02:33:57 +00001420 // Each variable must set success to true below...
1421 bool var_success = false;
1422 switch (var_name_begin[0])
1423 {
Enrico Granata4becb372011-06-29 22:27:15 +00001424 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001425 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001426 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001427 {
Enrico Granatac482a192011-08-17 22:13:59 +00001428 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001429 break;
1430
Enrico Granatac3e320a2011-08-02 17:27:39 +00001431 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001432 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001433
Enrico Granata6f3533f2011-07-29 19:53:35 +00001434 // check for *var and *svar
1435 if (*var_name_begin == '*')
1436 {
1437 do_deref_pointer = true;
1438 var_name_begin++;
1439 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001440
1441 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001442 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001443
Enrico Granata6f3533f2011-07-29 19:53:35 +00001444 if (*var_name_begin == 's')
1445 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001446 if (!valobj->IsSynthetic())
1447 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001448 if (!valobj)
1449 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001450 var_name_begin++;
1451 }
1452
Enrico Granatac3e320a2011-08-02 17:27:39 +00001453 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001454 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001455
Enrico Granata6f3533f2011-07-29 19:53:35 +00001456 // should be a 'v' by now
1457 if (*var_name_begin != 'v')
1458 break;
1459
Enrico Granatac3e320a2011-08-02 17:27:39 +00001460 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001461 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001462
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001463 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001464 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001465 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001466 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001467 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001468 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001469 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001470 const char* var_name_final = NULL;
1471 const char* var_name_final_if_array_range = NULL;
1472 const char* close_bracket_position = NULL;
1473 int64_t index_lower = -1;
1474 int64_t index_higher = -1;
1475 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001476 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001477 bool was_plain_var = false;
1478 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001479 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001480
Enrico Granatac482a192011-08-17 22:13:59 +00001481 if (!valobj) break;
1482 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001483 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001484 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001485 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001486 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001487 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001488 }
1489 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1490 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001491 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001492 // this is a variable with some custom format applied to it
1493 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001494 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001495 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001496 ScanFormatDescriptor (var_name_begin,
1497 var_name_end,
1498 &var_name_final,
1499 &percent_position,
1500 &custom_format,
1501 &val_obj_display);
1502 }
1503 // this is ${var.something} or multiple .something nested
1504 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1505 {
Enrico Granataa777dc22012-05-08 21:49:57 +00001506 if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1507 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001508 const char* percent_position;
1509 ScanFormatDescriptor (var_name_begin,
1510 var_name_end,
1511 &var_name_final,
1512 &percent_position,
1513 &custom_format,
1514 &val_obj_display);
1515
1516 const char* open_bracket_position;
1517 const char* separator_position;
1518 ScanBracketedRange (var_name_begin,
1519 var_name_end,
1520 var_name_final,
1521 &open_bracket_position,
1522 &separator_position,
1523 &close_bracket_position,
1524 &var_name_final_if_array_range,
1525 &index_lower,
1526 &index_higher);
1527
1528 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001529
Enrico Granata599171a2013-02-01 23:59:44 +00001530 std::string expr_path(var_name_final-var_name_begin-1,0);
1531 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1532
1533 if (log)
1534 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1535
1536 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001537 &first_unparsed,
1538 &reason_to_stop,
1539 &final_value_type,
1540 options,
1541 &what_next).get();
1542
1543 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001544 {
Enrico Granatae992a082011-07-22 17:03:19 +00001545 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001546 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001547 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001548 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001549 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001550 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001551 else
1552 {
Enrico Granatae992a082011-07-22 17:03:19 +00001553 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001554 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001555 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001556 first_unparsed, reason_to_stop, final_value_type);
1557 }
Enrico Granata4becb372011-06-29 22:27:15 +00001558 }
Greg Clayton34132752011-07-06 04:07:21 +00001559 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001560 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001561
Enrico Granata86cc9822012-03-19 22:58:49 +00001562 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1563 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001564
Enrico Granata86cc9822012-03-19 22:58:49 +00001565 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001566
Enrico Granataa7187d02011-07-06 19:27:11 +00001567 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001568 {
Greg Clayton34132752011-07-06 04:07:21 +00001569 // I have not deref-ed yet, let's do it
1570 // this happens when we are not going through GetValueForVariableExpressionPath
1571 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001572 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001573 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001574 if (error.Fail())
1575 {
1576 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001577 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001578 break;
1579 }
Greg Clayton34132752011-07-06 04:07:21 +00001580 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001581 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001582
Enrico Granataa777dc22012-05-08 21:49:57 +00001583 // <rdar://problem/11338654>
1584 // we do not want to use the summary for a bitfield of type T:n
1585 // if we were originally dealing with just a T - that would get
1586 // us into an endless recursion
1587 if (target->IsBitfield() && was_var_indexed)
1588 {
1589 // TODO: check for a (T:n)-specific summary - we should still obey that
1590 StreamString bitfield_name;
1591 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1592 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1593 if (!DataVisualization::GetSummaryForType(type_sp))
1594 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1595 }
1596
Enrico Granata85933ed2011-08-18 16:38:26 +00001597 // TODO use flags for these
Greg Clayton4ef877f2012-12-06 02:33:54 +00001598 bool is_array = ClangASTContext::IsArrayType(target->GetClangType(), NULL, NULL, NULL);
Enrico Granataf4efecd2011-07-12 22:56:10 +00001599 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001600 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001601
Enrico Granata86cc9822012-03-19 22:58:49 +00001602 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 +00001603 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001604 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001605 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001606 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001607
Greg Clayton5088c482013-03-25 21:06:13 +00001608 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001609 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001610 // try to use the special cases
1611 var_success = target->DumpPrintableRepresentation(str_temp,
1612 val_obj_display,
1613 custom_format);
1614 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001615 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001616
1617 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001618 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001619 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001620 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001621 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001622 }
1623 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001624 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001625 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001626 {
1627 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1628 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001629 else if (is_pointer) // if pointer, value is the address stored
1630 {
Greg Clayton23f59502012-07-17 03:23:13 +00001631 target->DumpPrintableRepresentation (s,
1632 val_obj_display,
1633 custom_format,
1634 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001635 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001636 var_success = true;
1637 break;
1638 }
1639 }
1640
1641 // if directly trying to print ${var}, and this is an aggregate, display a nice
1642 // type @ location message
1643 if (is_aggregate && was_plain_var)
1644 {
1645 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1646 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001647 break;
1648 }
1649
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001650 // 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 +00001651 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001652 {
1653 s << "<invalid use of aggregate type>";
1654 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001655 break;
1656 }
Greg Clayton34132752011-07-06 04:07:21 +00001657
1658 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001659 {
1660 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001661 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001662 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001663 }
Greg Clayton34132752011-07-06 04:07:21 +00001664 else
Enrico Granatae992a082011-07-22 17:03:19 +00001665 {
1666 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001667 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001668 if (!is_array && !is_pointer)
1669 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001670 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001671 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001672 const char* special_directions = NULL;
1673 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001674 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1675 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001676 ConstString additional_data;
1677 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1678 special_directions_writer.Printf("${%svar%s}",
1679 do_deref_pointer ? "*" : "",
1680 additional_data.GetCString());
1681 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001682 }
1683
1684 // let us display items index_lower thru index_higher of this array
1685 s.PutChar('[');
1686 var_success = true;
1687
1688 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001689 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001690
Greg Claytoncc4d0142012-02-17 07:49:44 +00001691 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001692
Greg Clayton34132752011-07-06 04:07:21 +00001693 for (;index_lower<=index_higher;index_lower++)
1694 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001695 ValueObject* item = ExpandIndexedExpression (target,
1696 index_lower,
1697 exe_ctx->GetFramePtr(),
1698 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001699
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001700 if (!item)
1701 {
Enrico Granatae992a082011-07-22 17:03:19 +00001702 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001703 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001704 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001705 else
1706 {
Enrico Granatae992a082011-07-22 17:03:19 +00001707 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001708 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001709 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001710
Greg Clayton34132752011-07-06 04:07:21 +00001711 if (!special_directions)
1712 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1713 else
1714 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1715
Enrico Granata22c55d12011-08-12 02:00:06 +00001716 if (--max_num_children == 0)
1717 {
1718 s.PutCString(", ...");
1719 break;
1720 }
1721
Greg Clayton34132752011-07-06 04:07:21 +00001722 if (index_lower < index_higher)
1723 s.PutChar(',');
1724 }
1725 s.PutChar(']');
1726 }
Enrico Granata4becb372011-06-29 22:27:15 +00001727 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001728 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001729 case 'a':
1730 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1731 {
1732 if (addr && addr->IsValid())
1733 {
1734 var_success = true;
1735 format_addr = *addr;
1736 }
1737 }
Greg Clayton5a314712011-10-14 07:41:33 +00001738 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1739 {
1740 var_success = true;
1741 var_name_begin += strlen("ansi."); // Skip the "ansi."
1742 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1743 {
1744 var_name_begin += strlen("fg."); // Skip the "fg."
1745 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1746 {
1747 s.Printf ("%s%s%s",
1748 lldb_utility::ansi::k_escape_start,
1749 lldb_utility::ansi::k_fg_black,
1750 lldb_utility::ansi::k_escape_end);
1751 }
1752 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1753 {
1754 s.Printf ("%s%s%s",
1755 lldb_utility::ansi::k_escape_start,
1756 lldb_utility::ansi::k_fg_red,
1757 lldb_utility::ansi::k_escape_end);
1758 }
1759 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1760 {
1761 s.Printf ("%s%s%s",
1762 lldb_utility::ansi::k_escape_start,
1763 lldb_utility::ansi::k_fg_green,
1764 lldb_utility::ansi::k_escape_end);
1765 }
1766 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1767 {
1768 s.Printf ("%s%s%s",
1769 lldb_utility::ansi::k_escape_start,
1770 lldb_utility::ansi::k_fg_yellow,
1771 lldb_utility::ansi::k_escape_end);
1772 }
1773 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1774 {
1775 s.Printf ("%s%s%s",
1776 lldb_utility::ansi::k_escape_start,
1777 lldb_utility::ansi::k_fg_blue,
1778 lldb_utility::ansi::k_escape_end);
1779 }
1780 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1781 {
1782 s.Printf ("%s%s%s",
1783 lldb_utility::ansi::k_escape_start,
1784 lldb_utility::ansi::k_fg_purple,
1785 lldb_utility::ansi::k_escape_end);
1786 }
1787 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1788 {
1789 s.Printf ("%s%s%s",
1790 lldb_utility::ansi::k_escape_start,
1791 lldb_utility::ansi::k_fg_cyan,
1792 lldb_utility::ansi::k_escape_end);
1793 }
1794 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1795 {
1796 s.Printf ("%s%s%s",
1797 lldb_utility::ansi::k_escape_start,
1798 lldb_utility::ansi::k_fg_white,
1799 lldb_utility::ansi::k_escape_end);
1800 }
1801 else
1802 {
1803 var_success = false;
1804 }
1805 }
1806 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1807 {
1808 var_name_begin += strlen("bg."); // Skip the "bg."
1809 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1810 {
1811 s.Printf ("%s%s%s",
1812 lldb_utility::ansi::k_escape_start,
1813 lldb_utility::ansi::k_bg_black,
1814 lldb_utility::ansi::k_escape_end);
1815 }
1816 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1817 {
1818 s.Printf ("%s%s%s",
1819 lldb_utility::ansi::k_escape_start,
1820 lldb_utility::ansi::k_bg_red,
1821 lldb_utility::ansi::k_escape_end);
1822 }
1823 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1824 {
1825 s.Printf ("%s%s%s",
1826 lldb_utility::ansi::k_escape_start,
1827 lldb_utility::ansi::k_bg_green,
1828 lldb_utility::ansi::k_escape_end);
1829 }
1830 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1831 {
1832 s.Printf ("%s%s%s",
1833 lldb_utility::ansi::k_escape_start,
1834 lldb_utility::ansi::k_bg_yellow,
1835 lldb_utility::ansi::k_escape_end);
1836 }
1837 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1838 {
1839 s.Printf ("%s%s%s",
1840 lldb_utility::ansi::k_escape_start,
1841 lldb_utility::ansi::k_bg_blue,
1842 lldb_utility::ansi::k_escape_end);
1843 }
1844 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1845 {
1846 s.Printf ("%s%s%s",
1847 lldb_utility::ansi::k_escape_start,
1848 lldb_utility::ansi::k_bg_purple,
1849 lldb_utility::ansi::k_escape_end);
1850 }
1851 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1852 {
1853 s.Printf ("%s%s%s",
1854 lldb_utility::ansi::k_escape_start,
1855 lldb_utility::ansi::k_bg_cyan,
1856 lldb_utility::ansi::k_escape_end);
1857 }
1858 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1859 {
1860 s.Printf ("%s%s%s",
1861 lldb_utility::ansi::k_escape_start,
1862 lldb_utility::ansi::k_bg_white,
1863 lldb_utility::ansi::k_escape_end);
1864 }
1865 else
1866 {
1867 var_success = false;
1868 }
1869 }
1870 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1871 {
1872 s.Printf ("%s%s%s",
1873 lldb_utility::ansi::k_escape_start,
1874 lldb_utility::ansi::k_ctrl_normal,
1875 lldb_utility::ansi::k_escape_end);
1876 }
1877 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1878 {
1879 s.Printf ("%s%s%s",
1880 lldb_utility::ansi::k_escape_start,
1881 lldb_utility::ansi::k_ctrl_bold,
1882 lldb_utility::ansi::k_escape_end);
1883 }
1884 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1885 {
1886 s.Printf ("%s%s%s",
1887 lldb_utility::ansi::k_escape_start,
1888 lldb_utility::ansi::k_ctrl_faint,
1889 lldb_utility::ansi::k_escape_end);
1890 }
1891 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1892 {
1893 s.Printf ("%s%s%s",
1894 lldb_utility::ansi::k_escape_start,
1895 lldb_utility::ansi::k_ctrl_italic,
1896 lldb_utility::ansi::k_escape_end);
1897 }
1898 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1899 {
1900 s.Printf ("%s%s%s",
1901 lldb_utility::ansi::k_escape_start,
1902 lldb_utility::ansi::k_ctrl_underline,
1903 lldb_utility::ansi::k_escape_end);
1904 }
1905 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1906 {
1907 s.Printf ("%s%s%s",
1908 lldb_utility::ansi::k_escape_start,
1909 lldb_utility::ansi::k_ctrl_slow_blink,
1910 lldb_utility::ansi::k_escape_end);
1911 }
1912 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1913 {
1914 s.Printf ("%s%s%s",
1915 lldb_utility::ansi::k_escape_start,
1916 lldb_utility::ansi::k_ctrl_fast_blink,
1917 lldb_utility::ansi::k_escape_end);
1918 }
1919 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1920 {
1921 s.Printf ("%s%s%s",
1922 lldb_utility::ansi::k_escape_start,
1923 lldb_utility::ansi::k_ctrl_negative,
1924 lldb_utility::ansi::k_escape_end);
1925 }
1926 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1927 {
1928 s.Printf ("%s%s%s",
1929 lldb_utility::ansi::k_escape_start,
1930 lldb_utility::ansi::k_ctrl_conceal,
1931 lldb_utility::ansi::k_escape_end);
1932
1933 }
1934 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1935 {
1936 s.Printf ("%s%s%s",
1937 lldb_utility::ansi::k_escape_start,
1938 lldb_utility::ansi::k_ctrl_crossed_out,
1939 lldb_utility::ansi::k_escape_end);
1940 }
1941 else
1942 {
1943 var_success = false;
1944 }
1945 }
Greg Clayton1b654882010-09-19 02:33:57 +00001946 break;
1947
1948 case 'p':
1949 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 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 Process *process = exe_ctx->GetProcessPtr();
1954 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001955 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001956 var_name_begin += ::strlen ("process.");
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("%" PRIu64, process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001960 var_success = true;
1961 }
1962 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1963 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1964 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1965 {
1966 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1967 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001968 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001969 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1970 {
1971 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1972 var_success = format_file_spec;
1973 }
1974 else
1975 {
1976 format_file_spec = exe_module->GetFileSpec();
1977 var_success = format_file_spec;
1978 }
Greg Clayton1b654882010-09-19 02:33:57 +00001979 }
1980 }
1981 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001982 }
Greg Clayton1b654882010-09-19 02:33:57 +00001983 }
1984 break;
1985
1986 case 't':
1987 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1988 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001989 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001990 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001991 Thread *thread = exe_ctx->GetThreadPtr();
1992 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001993 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001994 var_name_begin += ::strlen ("thread.");
1995 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001996 {
Daniel Malead01b2952012-11-29 21:49:15 +00001997 s.Printf("0x%4.4" PRIx64, thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001998 var_success = true;
1999 }
Greg Clayton160c9d82013-05-01 21:54:04 +00002000 else if (::strncmp (var_name_begin, "protocol_id}", strlen("protocol_id}")) == 0)
2001 {
2002 s.Printf("0x%4.4" PRIx64, thread->GetProtocolID());
2003 var_success = true;
2004 }
Greg Claytonc14ee322011-09-22 04:58:26 +00002005 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
2006 {
2007 s.Printf("%u", thread->GetIndexID());
2008 var_success = true;
2009 }
2010 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2011 {
2012 cstr = thread->GetName();
2013 var_success = cstr && cstr[0];
2014 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002015 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00002016 }
2017 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
2018 {
2019 cstr = thread->GetQueueName();
2020 var_success = cstr && cstr[0];
2021 if (var_success)
2022 s.PutCString(cstr);
2023 }
2024 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
2025 {
2026 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002027 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00002028 {
2029 cstr = stop_info_sp->GetDescription();
2030 if (cstr && cstr[0])
2031 {
2032 s.PutCString(cstr);
2033 var_success = true;
2034 }
Greg Clayton1b654882010-09-19 02:33:57 +00002035 }
2036 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00002037 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
2038 {
2039 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002040 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00002041 {
2042 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2043 if (return_valobj_sp)
2044 {
Enrico Granata9fb5ab52013-03-26 18:04:53 +00002045 ValueObject::DumpValueObject (s, return_valobj_sp.get());
Jim Inghamef651602011-12-22 19:12:40 +00002046 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00002047 }
2048 }
2049 }
Greg Clayton1b654882010-09-19 02:33:57 +00002050 }
2051 }
2052 }
2053 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
2054 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002055 // TODO: hookup properties
2056// if (!target_properties_sp)
2057// {
2058// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2059// if (target)
2060// target_properties_sp = target->GetProperties();
2061// }
2062//
2063// if (target_properties_sp)
2064// {
2065// var_name_begin += ::strlen ("target.");
2066// const char *end_property = strchr(var_name_begin, '}');
2067// if (end_property)
2068// {
2069// ConstString property_name(var_name_begin, end_property - var_name_begin);
2070// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2071// if (!property_value.empty())
2072// {
2073// s.PutCString (property_value.c_str());
2074// var_success = true;
2075// }
2076// }
2077// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002078 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2079 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002080 {
Greg Clayton1b654882010-09-19 02:33:57 +00002081 var_name_begin += ::strlen ("target.");
2082 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
2083 {
2084 ArchSpec arch (target->GetArchitecture ());
2085 if (arch.IsValid())
2086 {
Greg Clayton64195a22011-02-23 00:35:02 +00002087 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002088 var_success = true;
2089 }
2090 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002091 }
Greg Clayton1b654882010-09-19 02:33:57 +00002092 }
2093 break;
2094
2095
2096 case 'm':
2097 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
2098 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002099 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002100 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002101 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002102 var_name_begin += ::strlen ("module.");
2103
2104 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2105 {
2106 if (module->GetFileSpec())
2107 {
2108 var_name_begin += ::strlen ("file.");
2109
2110 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2111 {
2112 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2113 var_success = format_file_spec;
2114 }
2115 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2116 {
2117 format_file_spec = module->GetFileSpec();
2118 var_success = format_file_spec;
2119 }
2120 }
2121 }
2122 }
2123 }
2124 break;
2125
2126
2127 case 'f':
2128 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2129 {
2130 if (sc && sc->comp_unit != NULL)
2131 {
2132 var_name_begin += ::strlen ("file.");
2133
2134 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2135 {
2136 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2137 var_success = format_file_spec;
2138 }
2139 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2140 {
2141 format_file_spec = *sc->comp_unit;
2142 var_success = format_file_spec;
2143 }
2144 }
2145 }
2146 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
2147 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002148 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002149 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002150 StackFrame *frame = exe_ctx->GetFramePtr();
2151 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002152 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002153 var_name_begin += ::strlen ("frame.");
2154 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00002155 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002156 s.Printf("%u", frame->GetFrameIndex());
2157 var_success = true;
2158 }
2159 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
2160 {
2161 reg_kind = eRegisterKindGeneric;
2162 reg_num = LLDB_REGNUM_GENERIC_PC;
2163 var_success = true;
2164 }
2165 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
2166 {
2167 reg_kind = eRegisterKindGeneric;
2168 reg_num = LLDB_REGNUM_GENERIC_SP;
2169 var_success = true;
2170 }
2171 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
2172 {
2173 reg_kind = eRegisterKindGeneric;
2174 reg_num = LLDB_REGNUM_GENERIC_FP;
2175 var_success = true;
2176 }
2177 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
2178 {
2179 reg_kind = eRegisterKindGeneric;
2180 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2181 var_success = true;
2182 }
2183 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
2184 {
2185 reg_ctx = frame->GetRegisterContext().get();
2186 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002187 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002188 var_name_begin += ::strlen ("reg.");
2189 if (var_name_begin < var_name_end)
2190 {
2191 std::string reg_name (var_name_begin, var_name_end);
2192 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2193 if (reg_info)
2194 var_success = true;
2195 }
Greg Clayton1b654882010-09-19 02:33:57 +00002196 }
2197 }
2198 }
2199 }
2200 }
2201 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
2202 {
2203 if (sc && (sc->function != NULL || sc->symbol != NULL))
2204 {
2205 var_name_begin += ::strlen ("function.");
2206 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2207 {
2208 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002209 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002210 else
2211 s.Printf("symbol[%u]", sc->symbol->GetID());
2212
2213 var_success = true;
2214 }
2215 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2216 {
2217 if (sc->function)
2218 cstr = sc->function->GetName().AsCString (NULL);
2219 else if (sc->symbol)
2220 cstr = sc->symbol->GetName().AsCString (NULL);
2221 if (cstr)
2222 {
2223 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002224
2225 if (sc->block)
2226 {
2227 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2228 if (inline_block)
2229 {
2230 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2231 if (inline_info)
2232 {
2233 s.PutCString(" [inlined] ");
2234 inline_info->GetName().Dump(&s);
2235 }
2236 }
2237 }
Greg Clayton1b654882010-09-19 02:33:57 +00002238 var_success = true;
2239 }
2240 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002241 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2242 {
2243 // Print the function name with arguments in it
2244
2245 if (sc->function)
2246 {
2247 var_success = true;
2248 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2249 cstr = sc->function->GetName().AsCString (NULL);
2250 if (cstr)
2251 {
2252 const InlineFunctionInfo *inline_info = NULL;
2253 VariableListSP variable_list_sp;
2254 bool get_function_vars = true;
2255 if (sc->block)
2256 {
2257 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2258
2259 if (inline_block)
2260 {
2261 get_function_vars = false;
2262 inline_info = sc->block->GetInlinedFunctionInfo();
2263 if (inline_info)
2264 variable_list_sp = inline_block->GetBlockVariableList (true);
2265 }
2266 }
2267
2268 if (get_function_vars)
2269 {
2270 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2271 }
2272
2273 if (inline_info)
2274 {
2275 s.PutCString (cstr);
2276 s.PutCString (" [inlined] ");
2277 cstr = inline_info->GetName().GetCString();
2278 }
2279
2280 VariableList args;
2281 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002282 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002283 if (args.GetSize() > 0)
2284 {
2285 const char *open_paren = strchr (cstr, '(');
2286 const char *close_paren = NULL;
2287 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002288 {
2289 if (strncmp(open_paren, "(anonymous namespace)", strlen("(anonymous namespace)")) == 0)
2290 {
2291 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2292 if (open_paren)
2293 close_paren = strchr (open_paren, ')');
2294 }
2295 else
2296 close_paren = strchr (open_paren, ')');
2297 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002298
2299 if (open_paren)
2300 s.Write(cstr, open_paren - cstr + 1);
2301 else
2302 {
2303 s.PutCString (cstr);
2304 s.PutChar ('(');
2305 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002306 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002307 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2308 {
2309 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2310 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2311 const char *var_name = var_value_sp->GetName().GetCString();
2312 const char *var_value = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002313 if (arg_idx > 0)
2314 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002315 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002316 {
2317 if (var_value)
2318 s.Printf ("%s=%s", var_name, var_value);
2319 else
2320 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2321 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002322 else
2323 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002324 }
2325
2326 if (close_paren)
2327 s.PutCString (close_paren);
2328 else
2329 s.PutChar(')');
2330
2331 }
2332 else
2333 {
2334 s.PutCString(cstr);
2335 }
2336 }
2337 }
2338 else if (sc->symbol)
2339 {
2340 cstr = sc->symbol->GetName().AsCString (NULL);
2341 if (cstr)
2342 {
2343 s.PutCString(cstr);
2344 var_success = true;
2345 }
2346 }
2347 }
Greg Clayton1b654882010-09-19 02:33:57 +00002348 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2349 {
2350 var_success = addr != NULL;
2351 if (var_success)
2352 {
2353 format_addr = *addr;
2354 calculate_format_addr_function_offset = true;
2355 }
2356 }
2357 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2358 {
2359 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2360 if (var_success)
2361 {
2362 format_addr = sc->line_entry.range.GetBaseAddress();
2363 calculate_format_addr_function_offset = true;
2364 }
2365 }
2366 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2367 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002368 StackFrame *frame = exe_ctx->GetFramePtr();
2369 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002370 if (var_success)
2371 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002372 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002373 calculate_format_addr_function_offset = true;
2374 }
2375 }
2376 }
2377 }
2378 break;
2379
2380 case 'l':
2381 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2382 {
2383 if (sc && sc->line_entry.IsValid())
2384 {
2385 var_name_begin += ::strlen ("line.");
2386 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2387 {
2388 var_name_begin += ::strlen ("file.");
2389
2390 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2391 {
2392 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2393 var_success = format_file_spec;
2394 }
2395 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2396 {
2397 format_file_spec = sc->line_entry.file;
2398 var_success = format_file_spec;
2399 }
2400 }
2401 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2402 {
2403 var_success = true;
2404 s.Printf("%u", sc->line_entry.line);
2405 }
2406 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2407 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2408 {
2409 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2410 if (var_success)
2411 {
2412 format_addr = sc->line_entry.range.GetBaseAddress();
2413 if (var_name_begin[0] == 'e')
2414 format_addr.Slide (sc->line_entry.range.GetByteSize());
2415 }
2416 }
2417 }
2418 }
2419 break;
2420 }
2421
2422 if (var_success)
2423 {
2424 // If format addr is valid, then we need to print an address
2425 if (reg_num != LLDB_INVALID_REGNUM)
2426 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002427 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002428 // We have a register value to display...
2429 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2430 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002431 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002432 }
2433 else
2434 {
2435 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002436 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002437
2438 if (reg_ctx)
2439 {
2440 if (reg_kind != kNumRegisterKinds)
2441 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2442 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2443 var_success = reg_info != NULL;
2444 }
2445 }
2446 }
2447
2448 if (reg_info != NULL)
2449 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002450 RegisterValue reg_value;
2451 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2452 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002453 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002454 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002455 }
2456 }
2457
2458 if (format_file_spec)
2459 {
2460 s << format_file_spec;
2461 }
2462
2463 // If format addr is valid, then we need to print an address
2464 if (format_addr.IsValid())
2465 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002466 var_success = false;
2467
Greg Clayton1b654882010-09-19 02:33:57 +00002468 if (calculate_format_addr_function_offset)
2469 {
2470 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002471
Greg Clayton0603aa92010-10-04 01:05:56 +00002472 if (sc)
2473 {
2474 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002475 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002476 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002477 if (sc->block)
2478 {
2479 // Check to make sure we aren't in an inline
2480 // function. If we are, use the inline block
2481 // range that contains "format_addr" since
2482 // blocks can be discontiguous.
2483 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2484 AddressRange inline_range;
2485 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2486 func_addr = inline_range.GetBaseAddress();
2487 }
2488 }
Greg Claytone7612132012-03-07 21:03:09 +00002489 else if (sc->symbol && sc->symbol->ValueIsAddress())
2490 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002491 }
2492
2493 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002494 {
2495 if (func_addr.GetSection() == format_addr.GetSection())
2496 {
2497 addr_t func_file_addr = func_addr.GetFileAddress();
2498 addr_t addr_file_addr = format_addr.GetFileAddress();
2499 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002500 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002501 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002502 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002503 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002504 }
2505 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002506 {
2507 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2508 if (target)
2509 {
2510 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2511 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2512 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002513 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002514 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002515 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002516 var_success = true;
2517 }
2518 }
Greg Clayton1b654882010-09-19 02:33:57 +00002519 }
2520 }
2521 else
2522 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002523 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002524 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002525 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2526 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002527 if (vaddr == LLDB_INVALID_ADDRESS)
2528 vaddr = format_addr.GetFileAddress ();
2529
2530 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002531 {
Greg Clayton514487e2011-02-15 21:59:32 +00002532 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002533 if (addr_width == 0)
2534 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002535 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002536 var_success = true;
2537 }
Greg Clayton1b654882010-09-19 02:33:57 +00002538 }
2539 }
2540 }
2541
2542 if (var_success == false)
2543 success = false;
2544 }
2545 p = var_name_end;
2546 }
2547 else
2548 break;
2549 }
2550 else
2551 {
2552 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2553 s.PutChar(*p);
2554 }
2555 }
2556 else if (*p == '\\')
2557 {
2558 ++p; // skip the slash
2559 switch (*p)
2560 {
2561 case 'a': s.PutChar ('\a'); break;
2562 case 'b': s.PutChar ('\b'); break;
2563 case 'f': s.PutChar ('\f'); break;
2564 case 'n': s.PutChar ('\n'); break;
2565 case 'r': s.PutChar ('\r'); break;
2566 case 't': s.PutChar ('\t'); break;
2567 case 'v': s.PutChar ('\v'); break;
2568 case '\'': s.PutChar ('\''); break;
2569 case '\\': s.PutChar ('\\'); break;
2570 case '0':
2571 // 1 to 3 octal chars
2572 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002573 // Make a string that can hold onto the initial zero char,
2574 // up to 3 octal digits, and a terminating NULL.
2575 char oct_str[5] = { 0, 0, 0, 0, 0 };
2576
2577 int i;
2578 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2579 oct_str[i] = p[i];
2580
2581 // We don't want to consume the last octal character since
2582 // the main for loop will do this for us, so we advance p by
2583 // one less than i (even if i is zero)
2584 p += i - 1;
2585 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2586 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002587 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002588 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002589 }
Greg Clayton1b654882010-09-19 02:33:57 +00002590 }
2591 break;
2592
2593 case 'x':
2594 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002595 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002596 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002597 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002598
Greg Clayton0603aa92010-10-04 01:05:56 +00002599 // Make a string that can hold onto two hex chars plus a
2600 // NULL terminator
2601 char hex_str[3] = { 0,0,0 };
2602 hex_str[0] = *p;
2603 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002604 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002605 ++p; // Skip the first of the two hex chars
2606 hex_str[1] = *p;
2607 }
2608
2609 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2610 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002611 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002612 }
2613 else
2614 {
2615 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002616 }
2617 break;
2618
2619 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002620 // Just desensitize any other character by just printing what
2621 // came after the '\'
2622 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002623 break;
2624
2625 }
2626
2627 }
2628 }
2629 if (end)
2630 *end = p;
2631 return success;
2632}
2633
Jim Ingham228063c2012-02-21 02:23:08 +00002634void
2635Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2636{
Jim Ingham4f02b222012-02-22 22:49:20 +00002637 // For simplicity's sake, I am not going to deal with how to close down any
2638 // open logging streams, I just redirect everything from here on out to the
2639 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002640 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2641}
2642
2643bool
2644Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2645{
2646 Log::Callbacks log_callbacks;
2647
2648 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002649 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002650 {
2651 log_stream_sp = m_log_callback_stream_sp;
2652 // For now when using the callback mode you always get thread & timestamp.
2653 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2654 }
2655 else if (log_file == NULL || *log_file == '\0')
2656 {
2657 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2658 }
2659 else
2660 {
2661 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002662 if (pos != m_log_streams.end())
2663 log_stream_sp = pos->second.lock();
2664 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002665 {
2666 log_stream_sp.reset (new StreamFile (log_file));
2667 m_log_streams[log_file] = log_stream_sp;
2668 }
Jim Ingham228063c2012-02-21 02:23:08 +00002669 }
2670 assert (log_stream_sp.get());
2671
2672 if (log_options == 0)
2673 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2674
Greg Clayton57abc5d2013-05-10 21:47:16 +00002675 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002676 {
2677 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2678 return true;
2679 }
2680 else
2681 {
2682 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2683 if (log_channel_sp)
2684 {
2685 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2686 {
2687 return true;
2688 }
2689 else
2690 {
2691 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2692 return false;
2693 }
2694 }
2695 else
2696 {
2697 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2698 return false;
2699 }
2700 }
2701 return false;
2702}
2703
Greg Clayton9585fbf2013-03-19 00:20:55 +00002704SourceManager &
2705Debugger::GetSourceManager ()
2706{
2707 if (m_source_manager_ap.get() == NULL)
2708 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2709 return *m_source_manager_ap;
2710}
2711
2712