blob: caeed72e2a750f599f820509338f26d868186121 [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
Michael Sartain0769b2b2013-07-30 16:44:36 +0000102#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000103 "{, ${frame.pc}}"\
104 MODULE_WITH_FUNC\
105 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +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." },
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000134{ "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
Greg Claytone8cd0c92012-10-19 18:02:49 +0000135
136 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000137};
138
139enum
140{
141 ePropertyAutoConfirm = 0,
142 ePropertyFrameFormat,
143 ePropertyNotiftVoid,
144 ePropertyPrompt,
145 ePropertyScriptLanguage,
146 ePropertyStopDisassemblyCount,
147 ePropertyStopDisassemblyDisplay,
148 ePropertyStopLineCountAfter,
149 ePropertyStopLineCountBefore,
150 ePropertyTerminalWidth,
151 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000152 ePropertyUseExternalEditor,
153 ePropertyUseColor,
Greg Clayton67cc0632012-08-22 17:17:09 +0000154};
155
156//
157//const char *
158//Debugger::GetFrameFormat() const
159//{
160// return m_properties_sp->GetFrameFormat();
161//}
162//const char *
163//Debugger::GetThreadFormat() const
164//{
165// return m_properties_sp->GetThreadFormat();
166//}
167//
Greg Clayton4c054102012-09-01 00:38:36 +0000168
169
170Error
171Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
172 VarSetOperationType op,
173 const char *property_path,
174 const char *value)
175{
Enrico Granata84a53df2013-05-20 22:29:23 +0000176 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
177 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000178 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000179 if (is_load_script && exe_ctx->GetTargetSP())
180 {
181 target_sp = exe_ctx->GetTargetSP();
182 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
183 }
Greg Clayton4c054102012-09-01 00:38:36 +0000184 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
185 if (error.Success())
186 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000187 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000188 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
189 {
190 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000191 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
192 if (str.length())
193 new_prompt = str.c_str();
Greg Clayton4c054102012-09-01 00:38:36 +0000194 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
195 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
196 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000197 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
198 {
199 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
200 SetPrompt (GetPrompt());
201 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000202 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000203 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000204 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000205 {
206 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000207 StreamString feedback_stream;
208 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000209 {
210 for (auto error : errors)
211 {
Enrico Granata97303392013-05-21 00:00:30 +0000212 GetErrorStream().Printf("%s\n",error.AsCString());
Enrico Granata84a53df2013-05-20 22:29:23 +0000213 }
Enrico Granata97303392013-05-21 00:00:30 +0000214 if (feedback_stream.GetSize())
215 GetErrorStream().Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000216 }
217 }
218 }
Greg Clayton4c054102012-09-01 00:38:36 +0000219 }
220 return error;
221}
222
Greg Clayton67cc0632012-08-22 17:17:09 +0000223bool
224Debugger::GetAutoConfirm () const
225{
226 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000227 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000228}
229
230const char *
231Debugger::GetFrameFormat() const
232{
233 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000234 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000235}
236
237bool
238Debugger::GetNotifyVoid () const
239{
240 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000241 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000242}
243
244const char *
245Debugger::GetPrompt() const
246{
247 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000248 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000249}
250
251void
252Debugger::SetPrompt(const char *p)
253{
254 const uint32_t idx = ePropertyPrompt;
255 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
256 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000257 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
258 if (str.length())
259 new_prompt = str.c_str();
Greg Clayton67cc0632012-08-22 17:17:09 +0000260 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
261 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
262}
263
264const char *
265Debugger::GetThreadFormat() const
266{
267 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000268 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000269}
270
271lldb::ScriptLanguage
272Debugger::GetScriptLanguage() const
273{
274 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000275 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000276}
277
278bool
279Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
280{
281 const uint32_t idx = ePropertyScriptLanguage;
282 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
283}
284
285uint32_t
286Debugger::GetTerminalWidth () const
287{
288 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000289 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000290}
291
292bool
293Debugger::SetTerminalWidth (uint32_t term_width)
294{
295 const uint32_t idx = ePropertyTerminalWidth;
296 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
297}
298
299bool
300Debugger::GetUseExternalEditor () const
301{
302 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000303 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000304}
305
306bool
307Debugger::SetUseExternalEditor (bool b)
308{
309 const uint32_t idx = ePropertyUseExternalEditor;
310 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
311}
312
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000313bool
314Debugger::GetUseColor () const
315{
316 const uint32_t idx = ePropertyUseColor;
317 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
318}
319
320bool
321Debugger::SetUseColor (bool b)
322{
323 const uint32_t idx = ePropertyUseColor;
324 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
325 SetPrompt (GetPrompt());
326 return ret;
327}
328
Greg Clayton67cc0632012-08-22 17:17:09 +0000329uint32_t
330Debugger::GetStopSourceLineCount (bool before) const
331{
332 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000333 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000334}
335
336Debugger::StopDisassemblyType
337Debugger::GetStopDisassemblyDisplay () const
338{
339 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000340 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000341}
342
343uint32_t
344Debugger::GetDisassemblyLineCount () const
345{
346 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000347 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000348}
Greg Claytone372b982011-11-21 21:44:34 +0000349
Greg Clayton1b654882010-09-19 02:33:57 +0000350#pragma mark Debugger
351
Greg Clayton67cc0632012-08-22 17:17:09 +0000352//const DebuggerPropertiesSP &
353//Debugger::GetSettings() const
354//{
355// return m_properties_sp;
356//}
357//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000358
Caroline Tice2f88aad2011-01-14 00:29:16 +0000359int
360Debugger::TestDebuggerRefCount ()
361{
362 return g_shared_debugger_refcount;
363}
364
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365void
366Debugger::Initialize ()
367{
Greg Claytonc15f55e2012-03-30 20:53:46 +0000368 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000369 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370}
371
372void
373Debugger::Terminate ()
374{
Greg Clayton66111032010-06-23 01:19:29 +0000375 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 {
Greg Clayton66111032010-06-23 01:19:29 +0000377 g_shared_debugger_refcount--;
378 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 {
Greg Claytondbe54502010-11-19 03:46:01 +0000380 lldb_private::WillTerminate();
381 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000382
383 // Clear our master list of debugger objects
384 Mutex::Locker locker (GetDebuggerListMutex ());
385 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387 }
388}
389
Caroline Tice20bd37f2011-03-10 22:14:10 +0000390void
391Debugger::SettingsInitialize ()
392{
Greg Clayton6920b522012-08-22 18:39:03 +0000393 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000394}
395
396void
397Debugger::SettingsTerminate ()
398{
Greg Clayton6920b522012-08-22 18:39:03 +0000399 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000400}
401
Enrico Granata21dfcd92012-09-28 23:57:51 +0000402bool
Enrico Granatae743c782013-04-24 21:29:08 +0000403Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000404{
405 lldb::DynamicLibrarySP dynlib_sp(new lldb_private::DynamicLibrary(spec));
Enrico Granatae743c782013-04-24 21:29:08 +0000406 if (!dynlib_sp || dynlib_sp->IsValid() == false)
407 {
408 if (spec.Exists())
409 error.SetErrorString("this file does not represent a loadable dylib");
410 else
411 error.SetErrorString("no such file");
412 return false;
413 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000414 lldb::DebuggerSP debugger_sp(shared_from_this());
415 lldb::SBDebugger debugger_sb(debugger_sp);
Michael Sartain3cf443d2013-07-17 00:26:30 +0000416 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger) function.
Enrico Granata21dfcd92012-09-28 23:57:51 +0000417 // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
418 LLDBCommandPluginInit init_func = dynlib_sp->GetSymbol<LLDBCommandPluginInit>("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
419 if (!init_func)
Enrico Granatae743c782013-04-24 21:29:08 +0000420 {
421 error.SetErrorString("cannot find the initialization function lldb::PluginInitialize(lldb::SBDebugger)");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000422 return false;
Enrico Granatae743c782013-04-24 21:29:08 +0000423 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000424 if (init_func(debugger_sb))
425 {
426 m_loaded_plugins.push_back(dynlib_sp);
427 return true;
428 }
Enrico Granatae743c782013-04-24 21:29:08 +0000429 error.SetErrorString("dylib refused to be loaded");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000430 return false;
431}
432
433static FileSpec::EnumerateDirectoryResult
434LoadPluginCallback
435(
436 void *baton,
437 FileSpec::FileType file_type,
438 const FileSpec &file_spec
439 )
440{
441 Error error;
442
443 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000444 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000445
446 if (!baton)
447 return FileSpec::eEnumerateDirectoryResultQuit;
448
449 Debugger *debugger = (Debugger*)baton;
450
451 // If we have a regular file, a symbolic link or unknown file type, try
452 // and process the file. We must handle unknown as sometimes the directory
453 // enumeration might be enumerating a file system that doesn't have correct
454 // file type information.
455 if (file_type == FileSpec::eFileTypeRegular ||
456 file_type == FileSpec::eFileTypeSymbolicLink ||
457 file_type == FileSpec::eFileTypeUnknown )
458 {
459 FileSpec plugin_file_spec (file_spec);
460 plugin_file_spec.ResolvePath ();
461
Michael Sartain3cf443d2013-07-17 00:26:30 +0000462 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
463 plugin_file_spec.GetFileNameExtension() != g_solibext)
464 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000465 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000466 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000467
Enrico Granatae743c782013-04-24 21:29:08 +0000468 Error plugin_load_error;
469 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000470
471 return FileSpec::eEnumerateDirectoryResultNext;
472 }
473
474 else if (file_type == FileSpec::eFileTypeUnknown ||
475 file_type == FileSpec::eFileTypeDirectory ||
476 file_type == FileSpec::eFileTypeSymbolicLink )
477 {
478 // Try and recurse into anything that a directory or symbolic link.
479 // We must also do this for unknown as sometimes the directory enumeration
480 // might be enurating a file system that doesn't have correct file type
481 // information.
482 return FileSpec::eEnumerateDirectoryResultEnter;
483 }
484
485 return FileSpec::eEnumerateDirectoryResultNext;
486}
487
488void
489Debugger::InstanceInitialize ()
490{
491 FileSpec dir_spec;
492 const bool find_directories = true;
493 const bool find_files = true;
494 const bool find_other = true;
495 char dir_path[PATH_MAX];
496 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
497 {
498 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
499 {
500 FileSpec::EnumerateDirectory (dir_path,
501 find_directories,
502 find_files,
503 find_other,
504 LoadPluginCallback,
505 this);
506 }
507 }
508
509 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
510 {
511 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
512 {
513 FileSpec::EnumerateDirectory (dir_path,
514 find_directories,
515 find_files,
516 find_other,
517 LoadPluginCallback,
518 this);
519 }
520 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000521
522 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000523}
524
Greg Clayton66111032010-06-23 01:19:29 +0000525DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000526Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000527{
Jim Ingham228063c2012-02-21 02:23:08 +0000528 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000529 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000530 {
531 Mutex::Locker locker (GetDebuggerListMutex ());
532 GetDebuggerList().push_back(debugger_sp);
533 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000534 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000535 return debugger_sp;
536}
537
Caroline Ticee02657b2011-01-22 01:02:07 +0000538void
Greg Clayton4d122c42011-09-17 08:33:22 +0000539Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000540{
541 if (debugger_sp.get() == NULL)
542 return;
543
Jim Ingham8314c522011-09-15 21:36:42 +0000544 debugger_sp->Clear();
545
Greg Claytonc15f55e2012-03-30 20:53:46 +0000546 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000547 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000548 Mutex::Locker locker (GetDebuggerListMutex ());
549 DebuggerList &debugger_list = GetDebuggerList ();
550 DebuggerList::iterator pos, end = debugger_list.end();
551 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000552 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000553 if ((*pos).get() == debugger_sp.get())
554 {
555 debugger_list.erase (pos);
556 return;
557 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000558 }
559 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000560}
561
Greg Clayton4d122c42011-09-17 08:33:22 +0000562DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000563Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
564{
Greg Clayton4d122c42011-09-17 08:33:22 +0000565 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000566 if (g_shared_debugger_refcount > 0)
567 {
568 Mutex::Locker locker (GetDebuggerListMutex ());
569 DebuggerList &debugger_list = GetDebuggerList();
570 DebuggerList::iterator pos, end = debugger_list.end();
571
572 for (pos = debugger_list.begin(); pos != end; ++pos)
573 {
574 if ((*pos).get()->m_instance_name == instance_name)
575 {
576 debugger_sp = *pos;
577 break;
578 }
579 }
580 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000581 return debugger_sp;
582}
Greg Clayton66111032010-06-23 01:19:29 +0000583
584TargetSP
585Debugger::FindTargetWithProcessID (lldb::pid_t pid)
586{
Greg Clayton4d122c42011-09-17 08:33:22 +0000587 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000588 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000589 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000590 Mutex::Locker locker (GetDebuggerListMutex ());
591 DebuggerList &debugger_list = GetDebuggerList();
592 DebuggerList::iterator pos, end = debugger_list.end();
593 for (pos = debugger_list.begin(); pos != end; ++pos)
594 {
595 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
596 if (target_sp)
597 break;
598 }
Greg Clayton66111032010-06-23 01:19:29 +0000599 }
600 return target_sp;
601}
602
Greg Claytone4e45922011-11-16 05:37:56 +0000603TargetSP
604Debugger::FindTargetWithProcess (Process *process)
605{
606 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000607 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000608 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000609 Mutex::Locker locker (GetDebuggerListMutex ());
610 DebuggerList &debugger_list = GetDebuggerList();
611 DebuggerList::iterator pos, end = debugger_list.end();
612 for (pos = debugger_list.begin(); pos != end; ++pos)
613 {
614 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
615 if (target_sp)
616 break;
617 }
Greg Claytone4e45922011-11-16 05:37:56 +0000618 }
619 return target_sp;
620}
621
Jim Ingham228063c2012-02-21 02:23:08 +0000622Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000623 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000624 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000625 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 m_input_file (),
627 m_output_file (),
628 m_error_file (),
Jim Inghamc5917d92012-11-30 20:23:19 +0000629 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000630 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000631 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 m_listener ("lldb.Debugger"),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000633 m_source_manager_ap(),
Jim Inghame37d6052011-09-13 00:29:56 +0000634 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000635 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000636 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000637 m_input_reader_data (),
638 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639{
Greg Clayton67cc0632012-08-22 17:17:09 +0000640 char instance_cstr[256];
641 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
642 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000643 if (log_callback)
644 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000645 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000646 // Always add our default platform to the platform list
647 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
648 assert (default_platform_sp.get());
649 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000650
Greg Clayton754a9362012-08-23 00:22:02 +0000651 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000652 m_collection_sp->AppendProperty (ConstString("target"),
653 ConstString("Settings specify to debugging targets."),
654 true,
655 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000656 if (m_command_interpreter_ap.get())
657 {
658 m_collection_sp->AppendProperty (ConstString("interpreter"),
659 ConstString("Settings specify to the debugger's command interpreter."),
660 true,
661 m_command_interpreter_ap->GetValueProperties());
662 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000663 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
664 term_width->SetMinimumValue(10);
665 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000666
667 // Turn off use-color if this is a dumb terminal.
668 const char *term = getenv ("TERM");
669 if (term && !strcmp (term, "dumb"))
670 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671}
672
673Debugger::~Debugger ()
674{
Jim Ingham8314c522011-09-15 21:36:42 +0000675 Clear();
676}
677
678void
679Debugger::Clear()
680{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000681 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000682 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000683 int num_targets = m_target_list.GetNumTargets();
684 for (int i = 0; i < num_targets; i++)
685 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000686 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
687 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000688 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000689 ProcessSP process_sp (target_sp->GetProcessSP());
690 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000691 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000692 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000693 }
Greg Clayton66111032010-06-23 01:19:29 +0000694 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000695 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000696
697 // Close the input file _before_ we close the input read communications class
698 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000699 m_terminal_state.Clear();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000700 GetInputFile().Close ();
701 // Now that we have closed m_input_file, we can now tell our input communication
702 // class to close down. Its read thread should quickly exit after we close
703 // the input file handle above.
704 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000705}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706
707bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000708Debugger::GetCloseInputOnEOF () const
709{
710 return m_input_comm.GetCloseOnEOF();
711}
712
713void
714Debugger::SetCloseInputOnEOF (bool b)
715{
716 m_input_comm.SetCloseOnEOF(b);
717}
718
719bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720Debugger::GetAsyncExecution ()
721{
Greg Clayton66111032010-06-23 01:19:29 +0000722 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723}
724
725void
726Debugger::SetAsyncExecution (bool async_execution)
727{
Greg Clayton66111032010-06-23 01:19:29 +0000728 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729}
730
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731
732void
733Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
734{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000735 File &in_file = GetInputFile();
736 in_file.SetStream (fh, tranfer_ownership);
737 if (in_file.IsValid() == false)
738 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739
740 // Disconnect from any old connection if we had one
741 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000742 // Pass false as the second argument to ConnectionFileDescriptor below because
743 // our "in_file" above will already take ownership if requested and we don't
744 // want to objects trying to own and close a file descriptor.
745 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
Jim Inghamc5917d92012-11-30 20:23:19 +0000747
748 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
749 SaveInputTerminalState ();
750
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751 Error error;
752 if (m_input_comm.StartReadThread (&error) == false)
753 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000754 File &err_file = GetErrorFile();
755
756 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
757 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759}
760
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761void
762Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
763{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000764 File &out_file = GetOutputFile();
765 out_file.SetStream (fh, tranfer_ownership);
766 if (out_file.IsValid() == false)
767 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000768
Enrico Granatab5887262012-10-29 21:18:03 +0000769 // do not create the ScriptInterpreter just for setting the output file handle
770 // as the constructor will know how to do the right thing on its own
771 const bool can_create = false;
772 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
773 if (script_interpreter)
774 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775}
776
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777void
778Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
779{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000780 File &err_file = GetErrorFile();
781 err_file.SetStream (fh, tranfer_ownership);
782 if (err_file.IsValid() == false)
783 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784}
785
Jim Inghamc5917d92012-11-30 20:23:19 +0000786void
787Debugger::SaveInputTerminalState ()
788{
789 File &in_file = GetInputFile();
790 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
791 m_terminal_state.Save(in_file.GetDescriptor(), true);
792}
793
794void
795Debugger::RestoreInputTerminalState ()
796{
797 m_terminal_state.Restore();
798}
799
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000801Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802{
803 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000804 TargetSP target_sp(GetSelectedTarget());
805 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806
807 if (target_sp)
808 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000809 ProcessSP process_sp (target_sp->GetProcessSP());
810 exe_ctx.SetProcessSP (process_sp);
811 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000813 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
814 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000816 exe_ctx.SetThreadSP (thread_sp);
817 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
818 if (exe_ctx.GetFramePtr() == NULL)
819 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820 }
821 }
822 }
823 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000824}
825
Caroline Ticeb44880c2011-02-10 01:15:13 +0000826InputReaderSP
827Debugger::GetCurrentInputReader ()
828{
829 InputReaderSP reader_sp;
830
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000831 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000832 {
833 // Clear any finished readers from the stack
834 while (CheckIfTopInputReaderIsDone()) ;
835
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000836 if (!m_input_reader_stack.IsEmpty())
837 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000838 }
839
840 return reader_sp;
841}
842
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843void
844Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
845{
Caroline Ticeefed6132010-11-19 20:47:54 +0000846 if (bytes_len > 0)
847 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
848 else
849 ((Debugger *)baton)->DispatchInputEndOfFile ();
850}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851
852
853void
854Debugger::DispatchInput (const char *bytes, size_t bytes_len)
855{
Caroline Ticeefed6132010-11-19 20:47:54 +0000856 if (bytes == NULL || bytes_len == 0)
857 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858
859 WriteToDefaultReader (bytes, bytes_len);
860}
861
862void
Caroline Ticeefed6132010-11-19 20:47:54 +0000863Debugger::DispatchInputInterrupt ()
864{
865 m_input_reader_data.clear();
866
Caroline Ticeb44880c2011-02-10 01:15:13 +0000867 InputReaderSP reader_sp (GetCurrentInputReader ());
868 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000869 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000870 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000871
Caroline Ticeb44880c2011-02-10 01:15:13 +0000872 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000873 while (CheckIfTopInputReaderIsDone ()) ;
874 }
875}
876
877void
878Debugger::DispatchInputEndOfFile ()
879{
880 m_input_reader_data.clear();
881
Caroline Ticeb44880c2011-02-10 01:15:13 +0000882 InputReaderSP reader_sp (GetCurrentInputReader ());
883 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000884 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000885 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000886
Caroline Ticeb44880c2011-02-10 01:15:13 +0000887 // 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 +0000888 while (CheckIfTopInputReaderIsDone ()) ;
889 }
890}
891
892void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000893Debugger::CleanUpInputReaders ()
894{
895 m_input_reader_data.clear();
896
Caroline Ticeb44880c2011-02-10 01:15:13 +0000897 // 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 +0000898 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000899 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000900 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000901 if (reader_sp)
902 {
903 reader_sp->Notify (eInputReaderEndOfFile);
904 reader_sp->SetIsDone (true);
905 }
906 }
907}
908
909void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000910Debugger::NotifyTopInputReader (InputReaderAction notification)
911{
912 InputReaderSP reader_sp (GetCurrentInputReader());
913 if (reader_sp)
914 {
915 reader_sp->Notify (notification);
916
917 // Flush out any input readers that are done.
918 while (CheckIfTopInputReaderIsDone ())
919 /* Do nothing. */;
920 }
921}
922
Caroline Tice9088b062011-05-09 23:06:58 +0000923bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000924Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000925{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000926 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000927
Caroline Ticed61c10b2011-06-16 16:27:19 +0000928 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000929}
930
931
Caroline Tice969ed3d2011-05-02 20:41:46 +0000932void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
934{
935 if (bytes && bytes_len)
936 m_input_reader_data.append (bytes, bytes_len);
937
938 if (m_input_reader_data.empty())
939 return;
940
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000941 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000944 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945 if (!reader_sp)
946 break;
947
Greg Clayton471b31c2010-07-20 22:52:08 +0000948 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949 m_input_reader_data.size());
950 if (bytes_handled)
951 {
952 m_input_reader_data.erase (0, bytes_handled);
953 }
954 else
955 {
956 // No bytes were handled, we might not have reached our
957 // granularity, just return and wait for more data
958 break;
959 }
960 }
961
Caroline Ticeb44880c2011-02-10 01:15:13 +0000962 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 while (CheckIfTopInputReaderIsDone ())
964 /* Do nothing. */;
965
966}
967
968void
969Debugger::PushInputReader (const InputReaderSP& reader_sp)
970{
971 if (!reader_sp)
972 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000973
974 // Deactivate the old top reader
975 InputReaderSP top_reader_sp (GetCurrentInputReader ());
976
977 if (top_reader_sp)
978 top_reader_sp->Notify (eInputReaderDeactivate);
979
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000980 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 reader_sp->Notify (eInputReaderActivate);
982 ActivateInputReader (reader_sp);
983}
984
985bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000986Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987{
988 bool result = false;
989
990 // The reader on the stop of the stack is done, so let the next
991 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000992 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000994 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000995 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996
997 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
998 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000999 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000 reader_sp->Notify (eInputReaderDeactivate);
1001 reader_sp->Notify (eInputReaderDone);
1002 result = true;
1003
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001004 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001006 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001007 if (reader_sp)
1008 {
1009 ActivateInputReader (reader_sp);
1010 reader_sp->Notify (eInputReaderReactivate);
1011 }
1012 }
1013 }
1014 }
1015 return result;
1016}
1017
1018bool
1019Debugger::CheckIfTopInputReaderIsDone ()
1020{
1021 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001022 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023 {
Caroline Ticeb44880c2011-02-10 01:15:13 +00001024 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001025 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026
1027 if (reader_sp && reader_sp->IsDone())
1028 {
1029 result = true;
1030 PopInputReader (reader_sp);
1031 }
1032 }
1033 return result;
1034}
1035
1036void
1037Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
1038{
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001039 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001041 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001043 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +00001044
1045 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046
Greg Claytona3406612011-02-07 23:24:47 +00001047 switch (reader_sp->GetGranularity())
1048 {
1049 case eInputReaderGranularityByte:
1050 case eInputReaderGranularityWord:
1051 tty.SetCanonical (false);
1052 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001053
Greg Claytona3406612011-02-07 23:24:47 +00001054 case eInputReaderGranularityLine:
1055 case eInputReaderGranularityAll:
1056 tty.SetCanonical (true);
1057 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058
Greg Claytona3406612011-02-07 23:24:47 +00001059 default:
1060 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061 }
1062 }
1063}
Greg Clayton66111032010-06-23 01:19:29 +00001064
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001065StreamSP
1066Debugger::GetAsyncOutputStream ()
1067{
1068 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1069 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1070}
1071
1072StreamSP
1073Debugger::GetAsyncErrorStream ()
1074{
1075 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1076 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1077}
1078
Greg Claytonc7bece562013-01-25 18:06:21 +00001079size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001080Debugger::GetNumDebuggers()
1081{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001082 if (g_shared_debugger_refcount > 0)
1083 {
1084 Mutex::Locker locker (GetDebuggerListMutex ());
1085 return GetDebuggerList().size();
1086 }
1087 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001088}
1089
1090lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001091Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001092{
1093 DebuggerSP debugger_sp;
1094
Greg Claytonc15f55e2012-03-30 20:53:46 +00001095 if (g_shared_debugger_refcount > 0)
1096 {
1097 Mutex::Locker locker (GetDebuggerListMutex ());
1098 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001099
Greg Claytonc15f55e2012-03-30 20:53:46 +00001100 if (index < debugger_list.size())
1101 debugger_sp = debugger_list[index];
1102 }
1103
Enrico Granata061858c2012-02-15 02:34:21 +00001104 return debugger_sp;
1105}
1106
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001107DebuggerSP
1108Debugger::FindDebuggerWithID (lldb::user_id_t id)
1109{
Greg Clayton4d122c42011-09-17 08:33:22 +00001110 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001111
Greg Claytonc15f55e2012-03-30 20:53:46 +00001112 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001113 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001114 Mutex::Locker locker (GetDebuggerListMutex ());
1115 DebuggerList &debugger_list = GetDebuggerList();
1116 DebuggerList::iterator pos, end = debugger_list.end();
1117 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001118 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001119 if ((*pos).get()->GetID() == id)
1120 {
1121 debugger_sp = *pos;
1122 break;
1123 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001124 }
1125 }
1126 return debugger_sp;
1127}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001128
Greg Clayton1b654882010-09-19 02:33:57 +00001129static void
1130TestPromptFormats (StackFrame *frame)
1131{
1132 if (frame == NULL)
1133 return;
1134
1135 StreamString s;
1136 const char *prompt_format =
1137 "{addr = '${addr}'\n}"
1138 "{process.id = '${process.id}'\n}"
1139 "{process.name = '${process.name}'\n}"
1140 "{process.file.basename = '${process.file.basename}'\n}"
1141 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1142 "{thread.id = '${thread.id}'\n}"
1143 "{thread.index = '${thread.index}'\n}"
1144 "{thread.name = '${thread.name}'\n}"
1145 "{thread.queue = '${thread.queue}'\n}"
1146 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1147 "{target.arch = '${target.arch}'\n}"
1148 "{module.file.basename = '${module.file.basename}'\n}"
1149 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1150 "{file.basename = '${file.basename}'\n}"
1151 "{file.fullpath = '${file.fullpath}'\n}"
1152 "{frame.index = '${frame.index}'\n}"
1153 "{frame.pc = '${frame.pc}'\n}"
1154 "{frame.sp = '${frame.sp}'\n}"
1155 "{frame.fp = '${frame.fp}'\n}"
1156 "{frame.flags = '${frame.flags}'\n}"
1157 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1158 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1159 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1160 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1161 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1162 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1163 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1164 "{function.id = '${function.id}'\n}"
1165 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001166 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001167 "{function.addr-offset = '${function.addr-offset}'\n}"
1168 "{function.line-offset = '${function.line-offset}'\n}"
1169 "{function.pc-offset = '${function.pc-offset}'\n}"
1170 "{line.file.basename = '${line.file.basename}'\n}"
1171 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1172 "{line.number = '${line.number}'\n}"
1173 "{line.start-addr = '${line.start-addr}'\n}"
1174 "{line.end-addr = '${line.end-addr}'\n}"
1175;
1176
1177 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1178 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001179 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001180 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001181 {
1182 printf("%s\n", s.GetData());
1183 }
1184 else
1185 {
Greg Clayton1b654882010-09-19 02:33:57 +00001186 printf ("what we got: %s\n", s.GetData());
1187 }
1188}
1189
Enrico Granata9fc19442011-07-06 02:13:41 +00001190static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001191ScanFormatDescriptor (const char* var_name_begin,
1192 const char* var_name_end,
1193 const char** var_name_final,
1194 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001195 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001196 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001197{
Greg Clayton5160ce52013-03-27 23:08:40 +00001198 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001199 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001200 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001201 {
1202 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001203 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001204 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001205 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001206 else
1207 {
1208 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001209 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001210 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001211 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001212 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001213 true,
1214 *custom_format) )
1215 {
Enrico Granatae992a082011-07-22 17:03:19 +00001216 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001217 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001218
1219 switch (format_name.front())
1220 {
1221 case '@': // if this is an @ sign, print ObjC description
1222 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1223 break;
1224 case 'V': // if this is a V, print the value using the default format
1225 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1226 break;
1227 case 'L': // if this is an L, print the location of the value
1228 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1229 break;
1230 case 'S': // if this is an S, print the summary after all
1231 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1232 break;
1233 case '#': // if this is a '#', print the number of children
1234 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1235 break;
1236 case 'T': // if this is a 'T', print the type
1237 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1238 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001239 case 'N': // if this is a 'N', print the name
1240 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1241 break;
1242 case '>': // if this is a '>', print the name
1243 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1244 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001245 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001246 if (log)
1247 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001248 break;
1249 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001250 }
1251 // a good custom format tells us to print the value using it
1252 else
Enrico Granatae992a082011-07-22 17:03:19 +00001253 {
1254 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001255 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001256 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001257 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001258 }
Enrico Granatae992a082011-07-22 17:03:19 +00001259 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001260 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001261 *custom_format,
1262 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001263 return true;
1264}
1265
1266static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001267ScanBracketedRange (const char* var_name_begin,
1268 const char* var_name_end,
1269 const char* var_name_final,
1270 const char** open_bracket_position,
1271 const char** separator_position,
1272 const char** close_bracket_position,
1273 const char** var_name_final_if_array_range,
1274 int64_t* index_lower,
1275 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001276{
Greg Clayton5160ce52013-03-27 23:08:40 +00001277 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001278 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001279 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001280 {
1281 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1282 *close_bracket_position = ::strchr(*open_bracket_position,']');
1283 // as usual, we assume that [] will come before %
1284 //printf("trying to expand a []\n");
1285 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001286 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001287 {
Enrico Granatae992a082011-07-22 17:03:19 +00001288 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001289 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001290 *index_lower = 0;
1291 }
1292 else if (*separator_position == NULL || *separator_position > var_name_end)
1293 {
1294 char *end = NULL;
1295 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1296 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001297 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001298 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001299 }
Greg Clayton34132752011-07-06 04:07:21 +00001300 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001301 {
1302 char *end = NULL;
1303 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1304 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001305 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001306 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001307 }
1308 else
Enrico Granatae992a082011-07-22 17:03:19 +00001309 {
1310 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001311 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001312 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001313 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001314 if (*index_lower > *index_higher && *index_higher > 0)
1315 {
Enrico Granatae992a082011-07-22 17:03:19 +00001316 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001317 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001318 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001319 *index_lower = *index_higher;
1320 *index_higher = temp;
1321 }
1322 }
Enrico Granatae992a082011-07-22 17:03:19 +00001323 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001324 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001325 return true;
1326}
1327
Michael Sartain0769b2b2013-07-30 16:44:36 +00001328template <typename T>
1329static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1330{
1331 if (script_interpreter)
1332 {
1333 Error script_error;
1334 std::string script_output;
1335
1336 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1337 {
1338 s.Printf("%s", script_output.c_str());
1339 return true;
1340 }
1341 else
1342 {
1343 s.Printf("<error: %s>",script_error.AsCString());
1344 }
1345 }
1346 return false;
1347}
1348
Enrico Granata9fc19442011-07-06 02:13:41 +00001349static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001350ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001351 size_t index,
Enrico Granatadc940732011-08-23 00:32:52 +00001352 StackFrame* frame,
1353 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001354{
Greg Clayton5160ce52013-03-27 23:08:40 +00001355 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001356 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001357 std::string ptr_deref_buffer(10,0);
1358 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001359 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001360 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001361 const char* first_unparsed;
1362 ValueObject::GetValueForExpressionPathOptions options;
1363 ValueObject::ExpressionPathEndResultType final_value_type;
1364 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001365 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001366 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001367 &first_unparsed,
1368 &reason_to_stop,
1369 &final_value_type,
1370 options,
1371 &what_next);
1372 if (!item)
1373 {
Enrico Granatae992a082011-07-22 17:03:19 +00001374 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001375 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001376 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001377 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001378 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001379 else
1380 {
Enrico Granatae992a082011-07-22 17:03:19 +00001381 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001382 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001383 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001384 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001385 }
1386 return item;
1387}
1388
Michael Sartain0769b2b2013-07-30 16:44:36 +00001389static inline bool
1390IsToken(const char *var_name_begin, const char *var)
1391{
1392 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1393}
1394
1395static bool
1396IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1397 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1398{
1399 int var_len = strlen(var);
1400 if (::strncmp (var_name_begin, var, var_len) == 0)
1401 {
1402 var_name_begin += var_len;
1403 if (*var_name_begin == '}')
1404 {
1405 format = default_format;
1406 return true;
1407 }
1408 else if (*var_name_begin == '%')
1409 {
1410 // Allow format specifiers: x|X|u with optional width specifiers.
1411 // ${thread.id%x} ; hex
1412 // ${thread.id%X} ; uppercase hex
1413 // ${thread.id%u} ; unsigned decimal
1414 // ${thread.id%8.8X} ; width.precision + specifier
1415 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1416 int dot_count = 0;
1417 const char *specifier = NULL;
1418 int width_precision_length = 0;
1419 const char *width_precision = ++var_name_begin;
1420 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1421 {
1422 dot_count += (*var_name_begin == '.');
1423 if (dot_count > 1)
1424 break;
1425 var_name_begin++;
1426 width_precision_length++;
1427 }
1428
1429 if (IsToken (var_name_begin, "tid}"))
1430 {
1431 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1432 if (target)
1433 {
1434 ArchSpec arch (target->GetArchitecture ());
1435 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1436 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1437 specifier = PRIu64;
1438 }
1439 if (!specifier)
1440 {
1441 format = default_format;
1442 return true;
1443 }
1444 }
1445 else if (IsToken (var_name_begin, "x}"))
1446 specifier = PRIx64;
1447 else if (IsToken (var_name_begin, "X}"))
1448 specifier = PRIX64;
1449 else if (IsToken (var_name_begin, "u}"))
1450 specifier = PRIu64;
1451
1452 if (specifier)
1453 {
1454 format = "%";
1455 if (width_precision_length)
1456 format += std::string(width_precision, width_precision_length);
1457 format += specifier;
1458 return true;
1459 }
1460 }
1461 }
1462 return false;
1463}
1464
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001465static bool
1466FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001467(
1468 const char *format,
1469 const SymbolContext *sc,
1470 const ExecutionContext *exe_ctx,
1471 const Address *addr,
1472 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001473 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001474 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001475)
1476{
Enrico Granatac482a192011-08-17 22:13:59 +00001477 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001478 bool success = true;
1479 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001480 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001481
Greg Clayton1b654882010-09-19 02:33:57 +00001482 for (p = format; *p != '\0'; ++p)
1483 {
Enrico Granatac482a192011-08-17 22:13:59 +00001484 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001485 {
Enrico Granatac482a192011-08-17 22:13:59 +00001486 valobj = realvalobj;
1487 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001488 }
Greg Clayton1b654882010-09-19 02:33:57 +00001489 size_t non_special_chars = ::strcspn (p, "${}\\");
1490 if (non_special_chars > 0)
1491 {
1492 if (success)
1493 s.Write (p, non_special_chars);
1494 p += non_special_chars;
1495 }
1496
1497 if (*p == '\0')
1498 {
1499 break;
1500 }
1501 else if (*p == '{')
1502 {
1503 // Start a new scope that must have everything it needs if it is to
1504 // to make it into the final output stream "s". If you want to make
1505 // a format that only prints out the function or symbol name if there
1506 // is one in the symbol context you can use:
1507 // "{function =${function.name}}"
1508 // The first '{' starts a new scope that end with the matching '}' at
1509 // the end of the string. The contents "function =${function.name}"
1510 // will then be evaluated and only be output if there is a function
1511 // or symbol with a valid name.
1512 StreamString sub_strm;
1513
1514 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001515
1516 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001517 {
1518 // The stream had all it needed
1519 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1520 }
1521 if (*p != '}')
1522 {
1523 success = false;
1524 break;
1525 }
1526 }
1527 else if (*p == '}')
1528 {
1529 // End of a enclosing scope
1530 break;
1531 }
1532 else if (*p == '$')
1533 {
1534 // We have a prompt variable to print
1535 ++p;
1536 if (*p == '{')
1537 {
1538 ++p;
1539 const char *var_name_begin = p;
1540 const char *var_name_end = ::strchr (p, '}');
1541
1542 if (var_name_end && var_name_begin < var_name_end)
1543 {
1544 // if we have already failed to parse, skip this variable
1545 if (success)
1546 {
1547 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001548 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001549 Address format_addr;
1550 bool calculate_format_addr_function_offset = false;
1551 // Set reg_kind and reg_num to invalid values
1552 RegisterKind reg_kind = kNumRegisterKinds;
1553 uint32_t reg_num = LLDB_INVALID_REGNUM;
1554 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001555 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001556 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001557 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001558 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1559 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001560
Greg Clayton1b654882010-09-19 02:33:57 +00001561 // Each variable must set success to true below...
1562 bool var_success = false;
1563 switch (var_name_begin[0])
1564 {
Enrico Granata4becb372011-06-29 22:27:15 +00001565 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001566 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001567 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001568 {
Enrico Granatac482a192011-08-17 22:13:59 +00001569 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001570 break;
1571
Enrico Granatac3e320a2011-08-02 17:27:39 +00001572 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001573 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001574
Enrico Granata6f3533f2011-07-29 19:53:35 +00001575 // check for *var and *svar
1576 if (*var_name_begin == '*')
1577 {
1578 do_deref_pointer = true;
1579 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001580 if (log)
1581 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001582 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001583
Enrico Granata6f3533f2011-07-29 19:53:35 +00001584 if (*var_name_begin == 's')
1585 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001586 if (!valobj->IsSynthetic())
1587 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001588 if (!valobj)
1589 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001590 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001591 if (log)
1592 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001593 }
1594
1595 // should be a 'v' by now
1596 if (*var_name_begin != 'v')
1597 break;
1598
Enrico Granatac3e320a2011-08-02 17:27:39 +00001599 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001600 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001601
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001602 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001603 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001604 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001605 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001606 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001607 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001608 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001609 const char* var_name_final = NULL;
1610 const char* var_name_final_if_array_range = NULL;
1611 const char* close_bracket_position = NULL;
1612 int64_t index_lower = -1;
1613 int64_t index_higher = -1;
1614 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001615 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001616 bool was_plain_var = false;
1617 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001618 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001619
Enrico Granatac482a192011-08-17 22:13:59 +00001620 if (!valobj) break;
1621 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001622 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001623 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001624 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001625 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001626 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001627 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001628 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001629 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001630 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001631 // this is a variable with some custom format applied to it
1632 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001633 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001634 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001635 ScanFormatDescriptor (var_name_begin,
1636 var_name_end,
1637 &var_name_final,
1638 &percent_position,
1639 &custom_format,
1640 &val_obj_display);
1641 }
1642 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001643 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001644 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001645 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001646 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001647 const char* percent_position;
1648 ScanFormatDescriptor (var_name_begin,
1649 var_name_end,
1650 &var_name_final,
1651 &percent_position,
1652 &custom_format,
1653 &val_obj_display);
1654
1655 const char* open_bracket_position;
1656 const char* separator_position;
1657 ScanBracketedRange (var_name_begin,
1658 var_name_end,
1659 var_name_final,
1660 &open_bracket_position,
1661 &separator_position,
1662 &close_bracket_position,
1663 &var_name_final_if_array_range,
1664 &index_lower,
1665 &index_higher);
1666
1667 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001668
Enrico Granata599171a2013-02-01 23:59:44 +00001669 std::string expr_path(var_name_final-var_name_begin-1,0);
1670 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1671
1672 if (log)
1673 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1674
1675 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001676 &first_unparsed,
1677 &reason_to_stop,
1678 &final_value_type,
1679 options,
1680 &what_next).get();
1681
1682 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001683 {
Enrico Granatae992a082011-07-22 17:03:19 +00001684 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001685 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001686 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001687 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001688 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001689 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001690 else
1691 {
Enrico Granatae992a082011-07-22 17:03:19 +00001692 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001693 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001694 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001695 first_unparsed, reason_to_stop, final_value_type);
1696 }
Enrico Granata4becb372011-06-29 22:27:15 +00001697 }
Greg Clayton34132752011-07-06 04:07:21 +00001698 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001699 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001700
Enrico Granata86cc9822012-03-19 22:58:49 +00001701 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1702 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001703
Enrico Granata86cc9822012-03-19 22:58:49 +00001704 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001705
Enrico Granataa7187d02011-07-06 19:27:11 +00001706 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001707 {
Greg Clayton34132752011-07-06 04:07:21 +00001708 // I have not deref-ed yet, let's do it
1709 // this happens when we are not going through GetValueForVariableExpressionPath
1710 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001711 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001712 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001713 if (error.Fail())
1714 {
1715 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001716 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001717 break;
1718 }
Greg Clayton34132752011-07-06 04:07:21 +00001719 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001720 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001721
Enrico Granataa777dc22012-05-08 21:49:57 +00001722 // we do not want to use the summary for a bitfield of type T:n
1723 // if we were originally dealing with just a T - that would get
1724 // us into an endless recursion
1725 if (target->IsBitfield() && was_var_indexed)
1726 {
1727 // TODO: check for a (T:n)-specific summary - we should still obey that
1728 StreamString bitfield_name;
1729 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1730 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1731 if (!DataVisualization::GetSummaryForType(type_sp))
1732 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1733 }
1734
Enrico Granata85933ed2011-08-18 16:38:26 +00001735 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001736 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1737 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1738 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1739 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001740
Enrico Granata86cc9822012-03-19 22:58:49 +00001741 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 +00001742 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001743 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001744 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001745 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001746
Greg Clayton5088c482013-03-25 21:06:13 +00001747 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001748 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001749 // try to use the special cases
1750 var_success = target->DumpPrintableRepresentation(str_temp,
1751 val_obj_display,
1752 custom_format);
1753 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001754 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001755
1756 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001757 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001758 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001759 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001760 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001761 }
1762 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001763 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001764 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001765 {
1766 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1767 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001768 else if (is_pointer) // if pointer, value is the address stored
1769 {
Greg Clayton23f59502012-07-17 03:23:13 +00001770 target->DumpPrintableRepresentation (s,
1771 val_obj_display,
1772 custom_format,
1773 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001774 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001775 var_success = true;
1776 break;
1777 }
1778 }
1779
1780 // if directly trying to print ${var}, and this is an aggregate, display a nice
1781 // type @ location message
1782 if (is_aggregate && was_plain_var)
1783 {
1784 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1785 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001786 break;
1787 }
1788
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001789 // 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 +00001790 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001791 {
1792 s << "<invalid use of aggregate type>";
1793 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001794 break;
1795 }
Greg Clayton34132752011-07-06 04:07:21 +00001796
1797 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001798 {
1799 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001800 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001801 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001802 }
Greg Clayton34132752011-07-06 04:07:21 +00001803 else
Enrico Granatae992a082011-07-22 17:03:19 +00001804 {
1805 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001806 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001807 if (!is_array && !is_pointer)
1808 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001809 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001810 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001811 const char* special_directions = NULL;
1812 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001813 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1814 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001815 ConstString additional_data;
1816 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1817 special_directions_writer.Printf("${%svar%s}",
1818 do_deref_pointer ? "*" : "",
1819 additional_data.GetCString());
1820 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001821 }
1822
1823 // let us display items index_lower thru index_higher of this array
1824 s.PutChar('[');
1825 var_success = true;
1826
1827 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001828 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001829
Greg Claytoncc4d0142012-02-17 07:49:44 +00001830 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001831
Greg Clayton34132752011-07-06 04:07:21 +00001832 for (;index_lower<=index_higher;index_lower++)
1833 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001834 ValueObject* item = ExpandIndexedExpression (target,
1835 index_lower,
1836 exe_ctx->GetFramePtr(),
1837 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001838
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001839 if (!item)
1840 {
Enrico Granatae992a082011-07-22 17:03:19 +00001841 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001842 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001843 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001844 else
1845 {
Enrico Granatae992a082011-07-22 17:03:19 +00001846 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001847 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001848 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001849
Greg Clayton34132752011-07-06 04:07:21 +00001850 if (!special_directions)
1851 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1852 else
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001853 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
Greg Clayton34132752011-07-06 04:07:21 +00001854
Enrico Granata22c55d12011-08-12 02:00:06 +00001855 if (--max_num_children == 0)
1856 {
1857 s.PutCString(", ...");
1858 break;
1859 }
1860
Greg Clayton34132752011-07-06 04:07:21 +00001861 if (index_lower < index_higher)
1862 s.PutChar(',');
1863 }
1864 s.PutChar(']');
1865 }
Enrico Granata4becb372011-06-29 22:27:15 +00001866 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001867 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001868 case 'a':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001869 if (IsToken (var_name_begin, "addr}"))
Greg Clayton1b654882010-09-19 02:33:57 +00001870 {
1871 if (addr && addr->IsValid())
1872 {
1873 var_success = true;
1874 format_addr = *addr;
1875 }
1876 }
1877 break;
1878
1879 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001880 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00001881 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001882 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001883 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001884 Process *process = exe_ctx->GetProcessPtr();
1885 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001886 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001887 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001888 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001889 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001890 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001891 var_success = true;
1892 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001893 else if ((IsToken (var_name_begin, "name}")) ||
1894 (IsToken (var_name_begin, "file.basename}")) ||
1895 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00001896 {
1897 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1898 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001899 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001900 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1901 {
1902 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1903 var_success = format_file_spec;
1904 }
1905 else
1906 {
1907 format_file_spec = exe_module->GetFileSpec();
1908 var_success = format_file_spec;
1909 }
Greg Clayton1b654882010-09-19 02:33:57 +00001910 }
1911 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001912 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001913 {
1914 var_name_begin += ::strlen("script:");
1915 std::string script_name(var_name_begin,var_name_end);
1916 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001917 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
1918 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001919 }
Greg Clayton1b654882010-09-19 02:33:57 +00001920 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001921 }
Greg Clayton1b654882010-09-19 02:33:57 +00001922 }
1923 break;
1924
1925 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001926 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00001927 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001928 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001929 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001930 Thread *thread = exe_ctx->GetThreadPtr();
1931 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001932 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001933 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001934 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001935 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001936 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001937 var_success = true;
1938 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001939 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00001940 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001941 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00001942 var_success = true;
1943 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001944 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00001945 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001946 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001947 var_success = true;
1948 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001949 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001950 {
1951 cstr = thread->GetName();
1952 var_success = cstr && cstr[0];
1953 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001954 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001955 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001956 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001957 {
1958 cstr = thread->GetQueueName();
1959 var_success = cstr && cstr[0];
1960 if (var_success)
1961 s.PutCString(cstr);
1962 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001963 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001964 {
1965 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001966 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001967 {
1968 cstr = stop_info_sp->GetDescription();
1969 if (cstr && cstr[0])
1970 {
1971 s.PutCString(cstr);
1972 var_success = true;
1973 }
Greg Clayton1b654882010-09-19 02:33:57 +00001974 }
1975 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001976 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00001977 {
1978 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001979 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001980 {
1981 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1982 if (return_valobj_sp)
1983 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00001984 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00001985 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001986 }
1987 }
1988 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001989 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001990 {
1991 var_name_begin += ::strlen("script:");
1992 std::string script_name(var_name_begin,var_name_end);
1993 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001994 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
1995 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001996 }
Greg Clayton1b654882010-09-19 02:33:57 +00001997 }
1998 }
1999 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002000 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00002001 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002002 // TODO: hookup properties
2003// if (!target_properties_sp)
2004// {
2005// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2006// if (target)
2007// target_properties_sp = target->GetProperties();
2008// }
2009//
2010// if (target_properties_sp)
2011// {
2012// var_name_begin += ::strlen ("target.");
2013// const char *end_property = strchr(var_name_begin, '}');
2014// if (end_property)
2015// {
2016// ConstString property_name(var_name_begin, end_property - var_name_begin);
2017// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2018// if (!property_value.empty())
2019// {
2020// s.PutCString (property_value.c_str());
2021// var_success = true;
2022// }
2023// }
2024// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002025 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2026 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002027 {
Greg Clayton1b654882010-09-19 02:33:57 +00002028 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002029 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002030 {
2031 ArchSpec arch (target->GetArchitecture ());
2032 if (arch.IsValid())
2033 {
Greg Clayton64195a22011-02-23 00:35:02 +00002034 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002035 var_success = true;
2036 }
2037 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002038 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002039 {
2040 var_name_begin += ::strlen("script:");
2041 std::string script_name(var_name_begin,var_name_end);
2042 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002043 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2044 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002045 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002046 }
Greg Clayton1b654882010-09-19 02:33:57 +00002047 }
2048 break;
2049
2050
2051 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002052 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002053 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002054 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002055 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002056 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002057 var_name_begin += ::strlen ("module.");
2058
Michael Sartain0769b2b2013-07-30 16:44:36 +00002059 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002060 {
2061 if (module->GetFileSpec())
2062 {
2063 var_name_begin += ::strlen ("file.");
2064
Michael Sartain0769b2b2013-07-30 16:44:36 +00002065 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002066 {
2067 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
2068 var_success = format_file_spec;
2069 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002070 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002071 {
2072 format_file_spec = module->GetFileSpec();
2073 var_success = format_file_spec;
2074 }
2075 }
2076 }
2077 }
2078 }
2079 break;
2080
2081
2082 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002083 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002084 {
2085 if (sc && sc->comp_unit != NULL)
2086 {
2087 var_name_begin += ::strlen ("file.");
2088
Michael Sartain0769b2b2013-07-30 16:44:36 +00002089 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002090 {
2091 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
2092 var_success = format_file_spec;
2093 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002094 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002095 {
2096 format_file_spec = *sc->comp_unit;
2097 var_success = format_file_spec;
2098 }
2099 }
2100 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002101 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002102 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002103 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002104 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002105 StackFrame *frame = exe_ctx->GetFramePtr();
2106 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002107 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002108 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002109 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002110 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002111 s.Printf("%u", frame->GetFrameIndex());
2112 var_success = true;
2113 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002114 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002115 {
2116 reg_kind = eRegisterKindGeneric;
2117 reg_num = LLDB_REGNUM_GENERIC_PC;
2118 var_success = true;
2119 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002120 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002121 {
2122 reg_kind = eRegisterKindGeneric;
2123 reg_num = LLDB_REGNUM_GENERIC_SP;
2124 var_success = true;
2125 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002126 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002127 {
2128 reg_kind = eRegisterKindGeneric;
2129 reg_num = LLDB_REGNUM_GENERIC_FP;
2130 var_success = true;
2131 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002132 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002133 {
2134 reg_kind = eRegisterKindGeneric;
2135 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2136 var_success = true;
2137 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002138 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002139 {
2140 reg_ctx = frame->GetRegisterContext().get();
2141 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002142 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002143 var_name_begin += ::strlen ("reg.");
2144 if (var_name_begin < var_name_end)
2145 {
2146 std::string reg_name (var_name_begin, var_name_end);
2147 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2148 if (reg_info)
2149 var_success = true;
2150 }
Greg Clayton1b654882010-09-19 02:33:57 +00002151 }
2152 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002153 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002154 {
2155 var_name_begin += ::strlen("script:");
2156 std::string script_name(var_name_begin,var_name_end);
2157 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002158 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2159 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002160 }
Greg Clayton1b654882010-09-19 02:33:57 +00002161 }
2162 }
2163 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002164 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002165 {
2166 if (sc && (sc->function != NULL || sc->symbol != NULL))
2167 {
2168 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002169 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002170 {
2171 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002172 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002173 else
2174 s.Printf("symbol[%u]", sc->symbol->GetID());
2175
2176 var_success = true;
2177 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002178 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002179 {
2180 if (sc->function)
2181 cstr = sc->function->GetName().AsCString (NULL);
2182 else if (sc->symbol)
2183 cstr = sc->symbol->GetName().AsCString (NULL);
2184 if (cstr)
2185 {
2186 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002187
2188 if (sc->block)
2189 {
2190 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2191 if (inline_block)
2192 {
2193 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2194 if (inline_info)
2195 {
2196 s.PutCString(" [inlined] ");
2197 inline_info->GetName().Dump(&s);
2198 }
2199 }
2200 }
Greg Clayton1b654882010-09-19 02:33:57 +00002201 var_success = true;
2202 }
2203 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002204 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002205 {
2206 // Print the function name with arguments in it
2207
2208 if (sc->function)
2209 {
2210 var_success = true;
2211 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2212 cstr = sc->function->GetName().AsCString (NULL);
2213 if (cstr)
2214 {
2215 const InlineFunctionInfo *inline_info = NULL;
2216 VariableListSP variable_list_sp;
2217 bool get_function_vars = true;
2218 if (sc->block)
2219 {
2220 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2221
2222 if (inline_block)
2223 {
2224 get_function_vars = false;
2225 inline_info = sc->block->GetInlinedFunctionInfo();
2226 if (inline_info)
2227 variable_list_sp = inline_block->GetBlockVariableList (true);
2228 }
2229 }
2230
2231 if (get_function_vars)
2232 {
2233 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2234 }
2235
2236 if (inline_info)
2237 {
2238 s.PutCString (cstr);
2239 s.PutCString (" [inlined] ");
2240 cstr = inline_info->GetName().GetCString();
2241 }
2242
2243 VariableList args;
2244 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002245 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002246 if (args.GetSize() > 0)
2247 {
2248 const char *open_paren = strchr (cstr, '(');
2249 const char *close_paren = NULL;
2250 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002251 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002252 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002253 {
2254 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2255 if (open_paren)
2256 close_paren = strchr (open_paren, ')');
2257 }
2258 else
2259 close_paren = strchr (open_paren, ')');
2260 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002261
2262 if (open_paren)
2263 s.Write(cstr, open_paren - cstr + 1);
2264 else
2265 {
2266 s.PutCString (cstr);
2267 s.PutChar ('(');
2268 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002269 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002270 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2271 {
2272 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2273 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2274 const char *var_name = var_value_sp->GetName().GetCString();
2275 const char *var_value = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002276 if (arg_idx > 0)
2277 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002278 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002279 {
2280 if (var_value)
2281 s.Printf ("%s=%s", var_name, var_value);
2282 else
2283 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2284 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002285 else
2286 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002287 }
2288
2289 if (close_paren)
2290 s.PutCString (close_paren);
2291 else
2292 s.PutChar(')');
2293
2294 }
2295 else
2296 {
2297 s.PutCString(cstr);
2298 }
2299 }
2300 }
2301 else if (sc->symbol)
2302 {
2303 cstr = sc->symbol->GetName().AsCString (NULL);
2304 if (cstr)
2305 {
2306 s.PutCString(cstr);
2307 var_success = true;
2308 }
2309 }
2310 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002311 else if (IsToken (var_name_begin, "addr-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002312 {
2313 var_success = addr != NULL;
2314 if (var_success)
2315 {
2316 format_addr = *addr;
2317 calculate_format_addr_function_offset = true;
2318 }
2319 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002320 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002321 {
2322 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2323 if (var_success)
2324 {
2325 format_addr = sc->line_entry.range.GetBaseAddress();
2326 calculate_format_addr_function_offset = true;
2327 }
2328 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002329 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002330 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002331 StackFrame *frame = exe_ctx->GetFramePtr();
2332 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002333 if (var_success)
2334 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002335 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002336 calculate_format_addr_function_offset = true;
2337 }
2338 }
2339 }
2340 }
2341 break;
2342
2343 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002344 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002345 {
2346 if (sc && sc->line_entry.IsValid())
2347 {
2348 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002349 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002350 {
2351 var_name_begin += ::strlen ("file.");
2352
Michael Sartain0769b2b2013-07-30 16:44:36 +00002353 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002354 {
2355 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2356 var_success = format_file_spec;
2357 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002358 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002359 {
2360 format_file_spec = sc->line_entry.file;
2361 var_success = format_file_spec;
2362 }
2363 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002364 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002365 {
2366 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002367 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002368 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002369 else if ((IsToken (var_name_begin, "start-addr}")) ||
2370 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002371 {
2372 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2373 if (var_success)
2374 {
2375 format_addr = sc->line_entry.range.GetBaseAddress();
2376 if (var_name_begin[0] == 'e')
2377 format_addr.Slide (sc->line_entry.range.GetByteSize());
2378 }
2379 }
2380 }
2381 }
2382 break;
2383 }
2384
2385 if (var_success)
2386 {
2387 // If format addr is valid, then we need to print an address
2388 if (reg_num != LLDB_INVALID_REGNUM)
2389 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002390 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002391 // We have a register value to display...
2392 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2393 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002394 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002395 }
2396 else
2397 {
2398 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002399 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002400
2401 if (reg_ctx)
2402 {
2403 if (reg_kind != kNumRegisterKinds)
2404 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2405 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2406 var_success = reg_info != NULL;
2407 }
2408 }
2409 }
2410
2411 if (reg_info != NULL)
2412 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002413 RegisterValue reg_value;
2414 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2415 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002416 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002417 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002418 }
2419 }
2420
2421 if (format_file_spec)
2422 {
2423 s << format_file_spec;
2424 }
2425
2426 // If format addr is valid, then we need to print an address
2427 if (format_addr.IsValid())
2428 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002429 var_success = false;
2430
Greg Clayton1b654882010-09-19 02:33:57 +00002431 if (calculate_format_addr_function_offset)
2432 {
2433 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002434
Greg Clayton0603aa92010-10-04 01:05:56 +00002435 if (sc)
2436 {
2437 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002438 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002439 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002440 if (sc->block)
2441 {
2442 // Check to make sure we aren't in an inline
2443 // function. If we are, use the inline block
2444 // range that contains "format_addr" since
2445 // blocks can be discontiguous.
2446 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2447 AddressRange inline_range;
2448 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2449 func_addr = inline_range.GetBaseAddress();
2450 }
2451 }
Greg Claytone7612132012-03-07 21:03:09 +00002452 else if (sc->symbol && sc->symbol->ValueIsAddress())
2453 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002454 }
2455
2456 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002457 {
2458 if (func_addr.GetSection() == format_addr.GetSection())
2459 {
2460 addr_t func_file_addr = func_addr.GetFileAddress();
2461 addr_t addr_file_addr = format_addr.GetFileAddress();
2462 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002463 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002464 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002465 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002466 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002467 }
2468 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002469 {
2470 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2471 if (target)
2472 {
2473 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2474 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2475 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002476 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002477 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002478 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002479 var_success = true;
2480 }
2481 }
Greg Clayton1b654882010-09-19 02:33:57 +00002482 }
2483 }
2484 else
2485 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002486 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002487 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002488 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2489 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002490 if (vaddr == LLDB_INVALID_ADDRESS)
2491 vaddr = format_addr.GetFileAddress ();
2492
2493 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002494 {
Greg Clayton514487e2011-02-15 21:59:32 +00002495 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002496 if (addr_width == 0)
2497 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002498 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002499 var_success = true;
2500 }
Greg Clayton1b654882010-09-19 02:33:57 +00002501 }
2502 }
2503 }
2504
2505 if (var_success == false)
2506 success = false;
2507 }
2508 p = var_name_end;
2509 }
2510 else
2511 break;
2512 }
2513 else
2514 {
2515 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2516 s.PutChar(*p);
2517 }
2518 }
2519 else if (*p == '\\')
2520 {
2521 ++p; // skip the slash
2522 switch (*p)
2523 {
2524 case 'a': s.PutChar ('\a'); break;
2525 case 'b': s.PutChar ('\b'); break;
2526 case 'f': s.PutChar ('\f'); break;
2527 case 'n': s.PutChar ('\n'); break;
2528 case 'r': s.PutChar ('\r'); break;
2529 case 't': s.PutChar ('\t'); break;
2530 case 'v': s.PutChar ('\v'); break;
2531 case '\'': s.PutChar ('\''); break;
2532 case '\\': s.PutChar ('\\'); break;
2533 case '0':
2534 // 1 to 3 octal chars
2535 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002536 // Make a string that can hold onto the initial zero char,
2537 // up to 3 octal digits, and a terminating NULL.
2538 char oct_str[5] = { 0, 0, 0, 0, 0 };
2539
2540 int i;
2541 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2542 oct_str[i] = p[i];
2543
2544 // We don't want to consume the last octal character since
2545 // the main for loop will do this for us, so we advance p by
2546 // one less than i (even if i is zero)
2547 p += i - 1;
2548 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2549 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002550 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002551 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002552 }
Greg Clayton1b654882010-09-19 02:33:57 +00002553 }
2554 break;
2555
2556 case 'x':
2557 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002558 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002559 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002560 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002561
Greg Clayton0603aa92010-10-04 01:05:56 +00002562 // Make a string that can hold onto two hex chars plus a
2563 // NULL terminator
2564 char hex_str[3] = { 0,0,0 };
2565 hex_str[0] = *p;
2566 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002567 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002568 ++p; // Skip the first of the two hex chars
2569 hex_str[1] = *p;
2570 }
2571
2572 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2573 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002574 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002575 }
2576 else
2577 {
2578 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002579 }
2580 break;
2581
2582 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002583 // Just desensitize any other character by just printing what
2584 // came after the '\'
2585 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002586 break;
2587
2588 }
2589
2590 }
2591 }
2592 if (end)
2593 *end = p;
2594 return success;
2595}
2596
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002597bool
2598Debugger::FormatPrompt
2599(
2600 const char *format,
2601 const SymbolContext *sc,
2602 const ExecutionContext *exe_ctx,
2603 const Address *addr,
2604 Stream &s,
2605 ValueObject* valobj
2606)
2607{
2608 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2609 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2610 if (format_str.length())
2611 format = format_str.c_str();
2612 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
2613}
2614
Jim Ingham228063c2012-02-21 02:23:08 +00002615void
2616Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2617{
Jim Ingham4f02b222012-02-22 22:49:20 +00002618 // For simplicity's sake, I am not going to deal with how to close down any
2619 // open logging streams, I just redirect everything from here on out to the
2620 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002621 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2622}
2623
2624bool
2625Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2626{
2627 Log::Callbacks log_callbacks;
2628
2629 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002630 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002631 {
2632 log_stream_sp = m_log_callback_stream_sp;
2633 // For now when using the callback mode you always get thread & timestamp.
2634 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2635 }
2636 else if (log_file == NULL || *log_file == '\0')
2637 {
2638 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2639 }
2640 else
2641 {
2642 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002643 if (pos != m_log_streams.end())
2644 log_stream_sp = pos->second.lock();
2645 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002646 {
2647 log_stream_sp.reset (new StreamFile (log_file));
2648 m_log_streams[log_file] = log_stream_sp;
2649 }
Jim Ingham228063c2012-02-21 02:23:08 +00002650 }
2651 assert (log_stream_sp.get());
2652
2653 if (log_options == 0)
2654 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2655
Greg Clayton57abc5d2013-05-10 21:47:16 +00002656 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002657 {
2658 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2659 return true;
2660 }
2661 else
2662 {
2663 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2664 if (log_channel_sp)
2665 {
2666 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2667 {
2668 return true;
2669 }
2670 else
2671 {
2672 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2673 return false;
2674 }
2675 }
2676 else
2677 {
2678 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2679 return false;
2680 }
2681 }
2682 return false;
2683}
2684
Greg Clayton9585fbf2013-03-19 00:20:55 +00002685SourceManager &
2686Debugger::GetSourceManager ()
2687{
2688 if (m_source_manager_ap.get() == NULL)
2689 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2690 return *m_source_manager_ap;
2691}
2692
2693