blob: 45c2b0578f25ff96ff5f235501b7ebf734408a70 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Debugger.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Enrico Granata21dfcd92012-09-28 23:57:51 +000012#include "lldb/API/SBDebugger.h"
13
Greg Clayton4a33d312011-06-23 17:59:56 +000014#include "lldb/Core/Debugger.h"
15
16#include <map>
17
Enrico Granata4becb372011-06-29 22:27:15 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Type.h"
20
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/lldb-private.h"
22#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/InputReader.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Core/Module.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000025#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000026#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000028#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000029#include "lldb/Core/StreamCallback.h"
Greg Clayton1b654882010-09-19 02:33:57 +000030#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000032#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000033#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000034#include "lldb/DataFormatters/DataVisualization.h"
35#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000036#include "lldb/Host/DynamicLibrary.h"
Greg Claytona3406612011-02-07 23:24:47 +000037#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000038#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000039#include "lldb/Interpreter/OptionValueSInt64.h"
40#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000041#include "lldb/Symbol/ClangASTContext.h"
42#include "lldb/Symbol/CompileUnit.h"
43#include "lldb/Symbol/Function.h"
44#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000045#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Target/TargetList.h"
47#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000048#include "lldb/Target/RegisterContext.h"
49#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000050#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000052#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54using namespace lldb;
55using namespace lldb_private;
56
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
Greg Clayton1b654882010-09-19 02:33:57 +000058static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000059static lldb::user_id_t g_unique_id = 1;
60
Greg Clayton1b654882010-09-19 02:33:57 +000061#pragma mark Static Functions
62
63static Mutex &
64GetDebuggerListMutex ()
65{
66 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
67 return g_mutex;
68}
69
70typedef std::vector<DebuggerSP> DebuggerList;
71
72static DebuggerList &
73GetDebuggerList()
74{
75 // hide the static debugger list inside a singleton accessor to avoid
76 // global init contructors
77 static DebuggerList g_list;
78 return g_list;
79}
Greg Claytone372b982011-11-21 21:44:34 +000080
81OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000082g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000083{
Greg Clayton67cc0632012-08-22 17:17:09 +000084 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
85 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
86 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000087 { 0, NULL, NULL }
88};
89
Greg Clayton67cc0632012-08-22 17:17:09 +000090OptionEnumValueElement
91g_language_enumerators[] =
92{
93 { eScriptLanguageNone, "none", "Disable scripting languages."},
94 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
95 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000096 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000097};
Greg Claytone372b982011-11-21 21:44:34 +000098
Greg Clayton67cc0632012-08-22 17:17:09 +000099#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
100#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
101
102#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
103 "{, ${frame.pc}}"\
104 MODULE_WITH_FUNC\
105 FILE_AND_LINE\
Greg Clayton85d0c572013-04-04 20:40:35 +0000106 "{, name = '${thread.name}}"\
107 "{, queue = '${thread.queue}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000108 "{, stop reason = ${thread.stop-reason}}"\
109 "{\\nReturn value: ${thread.return-value}}"\
110 "\\n"
111
112#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
113 MODULE_WITH_FUNC\
114 FILE_AND_LINE\
115 "\\n"
116
117
118
Greg Clayton754a9362012-08-23 00:22:02 +0000119static PropertyDefinition
120g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000121{
122{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
123{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
124{ "notify-void", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
Greg Clayton4c054102012-09-01 00:38:36 +0000125{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000126{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
127{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
128{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
129{ "stop-line-count-after", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come after the current source line when displaying a stopped context." },
130{ "stop-line-count-before", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come before the current source line when displaying a stopped context." },
131{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
132{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
133{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
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);
416 // TODO: mangle this differently for your system - on OSX, the first underscore needs to be removed and the second one stays
417 LLDBCommandPluginInit init_func = dynlib_sp->GetSymbol<LLDBCommandPluginInit>("_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
418 if (!init_func)
Enrico Granatae743c782013-04-24 21:29:08 +0000419 {
420 error.SetErrorString("cannot find the initialization function lldb::PluginInitialize(lldb::SBDebugger)");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000421 return false;
Enrico Granatae743c782013-04-24 21:29:08 +0000422 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000423 if (init_func(debugger_sb))
424 {
425 m_loaded_plugins.push_back(dynlib_sp);
426 return true;
427 }
Enrico Granatae743c782013-04-24 21:29:08 +0000428 error.SetErrorString("dylib refused to be loaded");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000429 return false;
430}
431
432static FileSpec::EnumerateDirectoryResult
433LoadPluginCallback
434(
435 void *baton,
436 FileSpec::FileType file_type,
437 const FileSpec &file_spec
438 )
439{
440 Error error;
441
442 static ConstString g_dylibext("dylib");
443
444 if (!baton)
445 return FileSpec::eEnumerateDirectoryResultQuit;
446
447 Debugger *debugger = (Debugger*)baton;
448
449 // If we have a regular file, a symbolic link or unknown file type, try
450 // and process the file. We must handle unknown as sometimes the directory
451 // enumeration might be enumerating a file system that doesn't have correct
452 // file type information.
453 if (file_type == FileSpec::eFileTypeRegular ||
454 file_type == FileSpec::eFileTypeSymbolicLink ||
455 file_type == FileSpec::eFileTypeUnknown )
456 {
457 FileSpec plugin_file_spec (file_spec);
458 plugin_file_spec.ResolvePath ();
459
460 if (plugin_file_spec.GetFileNameExtension() != g_dylibext)
461 return FileSpec::eEnumerateDirectoryResultNext;
462
Enrico Granatae743c782013-04-24 21:29:08 +0000463 Error plugin_load_error;
464 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000465
466 return FileSpec::eEnumerateDirectoryResultNext;
467 }
468
469 else if (file_type == FileSpec::eFileTypeUnknown ||
470 file_type == FileSpec::eFileTypeDirectory ||
471 file_type == FileSpec::eFileTypeSymbolicLink )
472 {
473 // Try and recurse into anything that a directory or symbolic link.
474 // We must also do this for unknown as sometimes the directory enumeration
475 // might be enurating a file system that doesn't have correct file type
476 // information.
477 return FileSpec::eEnumerateDirectoryResultEnter;
478 }
479
480 return FileSpec::eEnumerateDirectoryResultNext;
481}
482
483void
484Debugger::InstanceInitialize ()
485{
486 FileSpec dir_spec;
487 const bool find_directories = true;
488 const bool find_files = true;
489 const bool find_other = true;
490 char dir_path[PATH_MAX];
491 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
492 {
493 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
494 {
495 FileSpec::EnumerateDirectory (dir_path,
496 find_directories,
497 find_files,
498 find_other,
499 LoadPluginCallback,
500 this);
501 }
502 }
503
504 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
505 {
506 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
507 {
508 FileSpec::EnumerateDirectory (dir_path,
509 find_directories,
510 find_files,
511 find_other,
512 LoadPluginCallback,
513 this);
514 }
515 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000516
517 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000518}
519
Greg Clayton66111032010-06-23 01:19:29 +0000520DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000521Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000522{
Jim Ingham228063c2012-02-21 02:23:08 +0000523 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000524 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000525 {
526 Mutex::Locker locker (GetDebuggerListMutex ());
527 GetDebuggerList().push_back(debugger_sp);
528 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000529 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000530 return debugger_sp;
531}
532
Caroline Ticee02657b2011-01-22 01:02:07 +0000533void
Greg Clayton4d122c42011-09-17 08:33:22 +0000534Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000535{
536 if (debugger_sp.get() == NULL)
537 return;
538
Jim Ingham8314c522011-09-15 21:36:42 +0000539 debugger_sp->Clear();
540
Greg Claytonc15f55e2012-03-30 20:53:46 +0000541 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000542 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000543 Mutex::Locker locker (GetDebuggerListMutex ());
544 DebuggerList &debugger_list = GetDebuggerList ();
545 DebuggerList::iterator pos, end = debugger_list.end();
546 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000547 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000548 if ((*pos).get() == debugger_sp.get())
549 {
550 debugger_list.erase (pos);
551 return;
552 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000553 }
554 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000555}
556
Greg Clayton4d122c42011-09-17 08:33:22 +0000557DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000558Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
559{
Greg Clayton4d122c42011-09-17 08:33:22 +0000560 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000561 if (g_shared_debugger_refcount > 0)
562 {
563 Mutex::Locker locker (GetDebuggerListMutex ());
564 DebuggerList &debugger_list = GetDebuggerList();
565 DebuggerList::iterator pos, end = debugger_list.end();
566
567 for (pos = debugger_list.begin(); pos != end; ++pos)
568 {
569 if ((*pos).get()->m_instance_name == instance_name)
570 {
571 debugger_sp = *pos;
572 break;
573 }
574 }
575 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000576 return debugger_sp;
577}
Greg Clayton66111032010-06-23 01:19:29 +0000578
579TargetSP
580Debugger::FindTargetWithProcessID (lldb::pid_t pid)
581{
Greg Clayton4d122c42011-09-17 08:33:22 +0000582 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000583 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000584 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000585 Mutex::Locker locker (GetDebuggerListMutex ());
586 DebuggerList &debugger_list = GetDebuggerList();
587 DebuggerList::iterator pos, end = debugger_list.end();
588 for (pos = debugger_list.begin(); pos != end; ++pos)
589 {
590 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
591 if (target_sp)
592 break;
593 }
Greg Clayton66111032010-06-23 01:19:29 +0000594 }
595 return target_sp;
596}
597
Greg Claytone4e45922011-11-16 05:37:56 +0000598TargetSP
599Debugger::FindTargetWithProcess (Process *process)
600{
601 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000602 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000603 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000604 Mutex::Locker locker (GetDebuggerListMutex ());
605 DebuggerList &debugger_list = GetDebuggerList();
606 DebuggerList::iterator pos, end = debugger_list.end();
607 for (pos = debugger_list.begin(); pos != end; ++pos)
608 {
609 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
610 if (target_sp)
611 break;
612 }
Greg Claytone4e45922011-11-16 05:37:56 +0000613 }
614 return target_sp;
615}
616
Jim Ingham228063c2012-02-21 02:23:08 +0000617Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000618 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000619 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000620 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 m_input_file (),
622 m_output_file (),
623 m_error_file (),
Jim Inghamc5917d92012-11-30 20:23:19 +0000624 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000625 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000626 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 m_listener ("lldb.Debugger"),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000628 m_source_manager_ap(),
Jim Inghame37d6052011-09-13 00:29:56 +0000629 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000630 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000631 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000632 m_input_reader_data (),
633 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634{
Greg Clayton67cc0632012-08-22 17:17:09 +0000635 char instance_cstr[256];
636 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
637 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000638 if (log_callback)
639 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000640 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000641 // Always add our default platform to the platform list
642 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
643 assert (default_platform_sp.get());
644 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000645
Greg Clayton754a9362012-08-23 00:22:02 +0000646 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000647 m_collection_sp->AppendProperty (ConstString("target"),
648 ConstString("Settings specify to debugging targets."),
649 true,
650 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000651 if (m_command_interpreter_ap.get())
652 {
653 m_collection_sp->AppendProperty (ConstString("interpreter"),
654 ConstString("Settings specify to the debugger's command interpreter."),
655 true,
656 m_command_interpreter_ap->GetValueProperties());
657 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000658 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
659 term_width->SetMinimumValue(10);
660 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000661
662 // Turn off use-color if this is a dumb terminal.
663 const char *term = getenv ("TERM");
664 if (term && !strcmp (term, "dumb"))
665 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666}
667
668Debugger::~Debugger ()
669{
Jim Ingham8314c522011-09-15 21:36:42 +0000670 Clear();
671}
672
673void
674Debugger::Clear()
675{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000676 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000677 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000678 int num_targets = m_target_list.GetNumTargets();
679 for (int i = 0; i < num_targets; i++)
680 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000681 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
682 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000683 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000684 ProcessSP process_sp (target_sp->GetProcessSP());
685 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000686 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000687 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000688 }
Greg Clayton66111032010-06-23 01:19:29 +0000689 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000690 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000691
692 // Close the input file _before_ we close the input read communications class
693 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000694 m_terminal_state.Clear();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000695 GetInputFile().Close ();
696 // Now that we have closed m_input_file, we can now tell our input communication
697 // class to close down. Its read thread should quickly exit after we close
698 // the input file handle above.
699 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000700}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701
702bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000703Debugger::GetCloseInputOnEOF () const
704{
705 return m_input_comm.GetCloseOnEOF();
706}
707
708void
709Debugger::SetCloseInputOnEOF (bool b)
710{
711 m_input_comm.SetCloseOnEOF(b);
712}
713
714bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715Debugger::GetAsyncExecution ()
716{
Greg Clayton66111032010-06-23 01:19:29 +0000717 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718}
719
720void
721Debugger::SetAsyncExecution (bool async_execution)
722{
Greg Clayton66111032010-06-23 01:19:29 +0000723 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724}
725
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726
727void
728Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
729{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000730 File &in_file = GetInputFile();
731 in_file.SetStream (fh, tranfer_ownership);
732 if (in_file.IsValid() == false)
733 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734
735 // Disconnect from any old connection if we had one
736 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000737 // Pass false as the second argument to ConnectionFileDescriptor below because
738 // our "in_file" above will already take ownership if requested and we don't
739 // want to objects trying to own and close a file descriptor.
740 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
Jim Inghamc5917d92012-11-30 20:23:19 +0000742
743 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
744 SaveInputTerminalState ();
745
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746 Error error;
747 if (m_input_comm.StartReadThread (&error) == false)
748 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000749 File &err_file = GetErrorFile();
750
751 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
752 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754}
755
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756void
757Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
758{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000759 File &out_file = GetOutputFile();
760 out_file.SetStream (fh, tranfer_ownership);
761 if (out_file.IsValid() == false)
762 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000763
Enrico Granatab5887262012-10-29 21:18:03 +0000764 // do not create the ScriptInterpreter just for setting the output file handle
765 // as the constructor will know how to do the right thing on its own
766 const bool can_create = false;
767 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
768 if (script_interpreter)
769 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770}
771
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772void
773Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
774{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000775 File &err_file = GetErrorFile();
776 err_file.SetStream (fh, tranfer_ownership);
777 if (err_file.IsValid() == false)
778 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779}
780
Jim Inghamc5917d92012-11-30 20:23:19 +0000781void
782Debugger::SaveInputTerminalState ()
783{
784 File &in_file = GetInputFile();
785 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
786 m_terminal_state.Save(in_file.GetDescriptor(), true);
787}
788
789void
790Debugger::RestoreInputTerminalState ()
791{
792 m_terminal_state.Restore();
793}
794
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000796Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797{
798 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000799 TargetSP target_sp(GetSelectedTarget());
800 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801
802 if (target_sp)
803 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000804 ProcessSP process_sp (target_sp->GetProcessSP());
805 exe_ctx.SetProcessSP (process_sp);
806 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000808 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
809 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000811 exe_ctx.SetThreadSP (thread_sp);
812 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
813 if (exe_ctx.GetFramePtr() == NULL)
814 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815 }
816 }
817 }
818 return exe_ctx;
819
820}
821
Caroline Ticeb44880c2011-02-10 01:15:13 +0000822InputReaderSP
823Debugger::GetCurrentInputReader ()
824{
825 InputReaderSP reader_sp;
826
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000827 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000828 {
829 // Clear any finished readers from the stack
830 while (CheckIfTopInputReaderIsDone()) ;
831
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000832 if (!m_input_reader_stack.IsEmpty())
833 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000834 }
835
836 return reader_sp;
837}
838
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839void
840Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
841{
Caroline Ticeefed6132010-11-19 20:47:54 +0000842 if (bytes_len > 0)
843 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
844 else
845 ((Debugger *)baton)->DispatchInputEndOfFile ();
846}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847
848
849void
850Debugger::DispatchInput (const char *bytes, size_t bytes_len)
851{
Caroline Ticeefed6132010-11-19 20:47:54 +0000852 if (bytes == NULL || bytes_len == 0)
853 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854
855 WriteToDefaultReader (bytes, bytes_len);
856}
857
858void
Caroline Ticeefed6132010-11-19 20:47:54 +0000859Debugger::DispatchInputInterrupt ()
860{
861 m_input_reader_data.clear();
862
Caroline Ticeb44880c2011-02-10 01:15:13 +0000863 InputReaderSP reader_sp (GetCurrentInputReader ());
864 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000865 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000866 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000867
Caroline Ticeb44880c2011-02-10 01:15:13 +0000868 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000869 while (CheckIfTopInputReaderIsDone ()) ;
870 }
871}
872
873void
874Debugger::DispatchInputEndOfFile ()
875{
876 m_input_reader_data.clear();
877
Caroline Ticeb44880c2011-02-10 01:15:13 +0000878 InputReaderSP reader_sp (GetCurrentInputReader ());
879 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000880 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000881 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000882
Caroline Ticeb44880c2011-02-10 01:15:13 +0000883 // 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 +0000884 while (CheckIfTopInputReaderIsDone ()) ;
885 }
886}
887
888void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000889Debugger::CleanUpInputReaders ()
890{
891 m_input_reader_data.clear();
892
Caroline Ticeb44880c2011-02-10 01:15:13 +0000893 // 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 +0000894 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000895 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000896 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000897 if (reader_sp)
898 {
899 reader_sp->Notify (eInputReaderEndOfFile);
900 reader_sp->SetIsDone (true);
901 }
902 }
903}
904
905void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000906Debugger::NotifyTopInputReader (InputReaderAction notification)
907{
908 InputReaderSP reader_sp (GetCurrentInputReader());
909 if (reader_sp)
910 {
911 reader_sp->Notify (notification);
912
913 // Flush out any input readers that are done.
914 while (CheckIfTopInputReaderIsDone ())
915 /* Do nothing. */;
916 }
917}
918
Caroline Tice9088b062011-05-09 23:06:58 +0000919bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000920Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000921{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000922 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000923
Caroline Ticed61c10b2011-06-16 16:27:19 +0000924 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000925}
926
927
Caroline Tice969ed3d2011-05-02 20:41:46 +0000928void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
930{
931 if (bytes && bytes_len)
932 m_input_reader_data.append (bytes, bytes_len);
933
934 if (m_input_reader_data.empty())
935 return;
936
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000937 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000940 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 if (!reader_sp)
942 break;
943
Greg Clayton471b31c2010-07-20 22:52:08 +0000944 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945 m_input_reader_data.size());
946 if (bytes_handled)
947 {
948 m_input_reader_data.erase (0, bytes_handled);
949 }
950 else
951 {
952 // No bytes were handled, we might not have reached our
953 // granularity, just return and wait for more data
954 break;
955 }
956 }
957
Caroline Ticeb44880c2011-02-10 01:15:13 +0000958 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000959 while (CheckIfTopInputReaderIsDone ())
960 /* Do nothing. */;
961
962}
963
964void
965Debugger::PushInputReader (const InputReaderSP& reader_sp)
966{
967 if (!reader_sp)
968 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000969
970 // Deactivate the old top reader
971 InputReaderSP top_reader_sp (GetCurrentInputReader ());
972
973 if (top_reader_sp)
974 top_reader_sp->Notify (eInputReaderDeactivate);
975
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000976 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 reader_sp->Notify (eInputReaderActivate);
978 ActivateInputReader (reader_sp);
979}
980
981bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000982Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983{
984 bool result = false;
985
986 // The reader on the stop of the stack is done, so let the next
987 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000988 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000990 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000991 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992
993 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
994 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000995 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 reader_sp->Notify (eInputReaderDeactivate);
997 reader_sp->Notify (eInputReaderDone);
998 result = true;
999
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001000 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001002 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003 if (reader_sp)
1004 {
1005 ActivateInputReader (reader_sp);
1006 reader_sp->Notify (eInputReaderReactivate);
1007 }
1008 }
1009 }
1010 }
1011 return result;
1012}
1013
1014bool
1015Debugger::CheckIfTopInputReaderIsDone ()
1016{
1017 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001018 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019 {
Caroline Ticeb44880c2011-02-10 01:15:13 +00001020 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001021 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022
1023 if (reader_sp && reader_sp->IsDone())
1024 {
1025 result = true;
1026 PopInputReader (reader_sp);
1027 }
1028 }
1029 return result;
1030}
1031
1032void
1033Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
1034{
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001035 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001036
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001037 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001039 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +00001040
1041 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042
Greg Claytona3406612011-02-07 23:24:47 +00001043 switch (reader_sp->GetGranularity())
1044 {
1045 case eInputReaderGranularityByte:
1046 case eInputReaderGranularityWord:
1047 tty.SetCanonical (false);
1048 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049
Greg Claytona3406612011-02-07 23:24:47 +00001050 case eInputReaderGranularityLine:
1051 case eInputReaderGranularityAll:
1052 tty.SetCanonical (true);
1053 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054
Greg Claytona3406612011-02-07 23:24:47 +00001055 default:
1056 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057 }
1058 }
1059}
Greg Clayton66111032010-06-23 01:19:29 +00001060
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001061StreamSP
1062Debugger::GetAsyncOutputStream ()
1063{
1064 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1065 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1066}
1067
1068StreamSP
1069Debugger::GetAsyncErrorStream ()
1070{
1071 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1072 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1073}
1074
Greg Claytonc7bece562013-01-25 18:06:21 +00001075size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001076Debugger::GetNumDebuggers()
1077{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001078 if (g_shared_debugger_refcount > 0)
1079 {
1080 Mutex::Locker locker (GetDebuggerListMutex ());
1081 return GetDebuggerList().size();
1082 }
1083 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001084}
1085
1086lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001087Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001088{
1089 DebuggerSP debugger_sp;
1090
Greg Claytonc15f55e2012-03-30 20:53:46 +00001091 if (g_shared_debugger_refcount > 0)
1092 {
1093 Mutex::Locker locker (GetDebuggerListMutex ());
1094 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001095
Greg Claytonc15f55e2012-03-30 20:53:46 +00001096 if (index < debugger_list.size())
1097 debugger_sp = debugger_list[index];
1098 }
1099
Enrico Granata061858c2012-02-15 02:34:21 +00001100 return debugger_sp;
1101}
1102
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001103DebuggerSP
1104Debugger::FindDebuggerWithID (lldb::user_id_t id)
1105{
Greg Clayton4d122c42011-09-17 08:33:22 +00001106 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001107
Greg Claytonc15f55e2012-03-30 20:53:46 +00001108 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001109 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001110 Mutex::Locker locker (GetDebuggerListMutex ());
1111 DebuggerList &debugger_list = GetDebuggerList();
1112 DebuggerList::iterator pos, end = debugger_list.end();
1113 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001114 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001115 if ((*pos).get()->GetID() == id)
1116 {
1117 debugger_sp = *pos;
1118 break;
1119 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001120 }
1121 }
1122 return debugger_sp;
1123}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001124
Greg Clayton1b654882010-09-19 02:33:57 +00001125static void
1126TestPromptFormats (StackFrame *frame)
1127{
1128 if (frame == NULL)
1129 return;
1130
1131 StreamString s;
1132 const char *prompt_format =
1133 "{addr = '${addr}'\n}"
1134 "{process.id = '${process.id}'\n}"
1135 "{process.name = '${process.name}'\n}"
1136 "{process.file.basename = '${process.file.basename}'\n}"
1137 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1138 "{thread.id = '${thread.id}'\n}"
1139 "{thread.index = '${thread.index}'\n}"
1140 "{thread.name = '${thread.name}'\n}"
1141 "{thread.queue = '${thread.queue}'\n}"
1142 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1143 "{target.arch = '${target.arch}'\n}"
1144 "{module.file.basename = '${module.file.basename}'\n}"
1145 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1146 "{file.basename = '${file.basename}'\n}"
1147 "{file.fullpath = '${file.fullpath}'\n}"
1148 "{frame.index = '${frame.index}'\n}"
1149 "{frame.pc = '${frame.pc}'\n}"
1150 "{frame.sp = '${frame.sp}'\n}"
1151 "{frame.fp = '${frame.fp}'\n}"
1152 "{frame.flags = '${frame.flags}'\n}"
1153 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1154 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1155 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1156 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1157 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1158 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1159 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1160 "{function.id = '${function.id}'\n}"
1161 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001162 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001163 "{function.addr-offset = '${function.addr-offset}'\n}"
1164 "{function.line-offset = '${function.line-offset}'\n}"
1165 "{function.pc-offset = '${function.pc-offset}'\n}"
1166 "{line.file.basename = '${line.file.basename}'\n}"
1167 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1168 "{line.number = '${line.number}'\n}"
1169 "{line.start-addr = '${line.start-addr}'\n}"
1170 "{line.end-addr = '${line.end-addr}'\n}"
1171;
1172
1173 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1174 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001175 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001176 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001177 {
1178 printf("%s\n", s.GetData());
1179 }
1180 else
1181 {
Greg Clayton1b654882010-09-19 02:33:57 +00001182 printf ("what we got: %s\n", s.GetData());
1183 }
1184}
1185
Enrico Granata9fc19442011-07-06 02:13:41 +00001186static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001187ScanFormatDescriptor (const char* var_name_begin,
1188 const char* var_name_end,
1189 const char** var_name_final,
1190 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001191 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001192 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001193{
Greg Clayton5160ce52013-03-27 23:08:40 +00001194 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001195 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001196 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001197 {
1198 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001199 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001200 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001201 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001202 else
1203 {
1204 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001205 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001206 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001207 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001208 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001209 true,
1210 *custom_format) )
1211 {
Enrico Granatae992a082011-07-22 17:03:19 +00001212 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001213 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001214
1215 switch (format_name.front())
1216 {
1217 case '@': // if this is an @ sign, print ObjC description
1218 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1219 break;
1220 case 'V': // if this is a V, print the value using the default format
1221 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1222 break;
1223 case 'L': // if this is an L, print the location of the value
1224 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1225 break;
1226 case 'S': // if this is an S, print the summary after all
1227 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1228 break;
1229 case '#': // if this is a '#', print the number of children
1230 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1231 break;
1232 case 'T': // if this is a 'T', print the type
1233 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1234 break;
1235 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001236 if (log)
1237 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001238 break;
1239 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001240 }
1241 // a good custom format tells us to print the value using it
1242 else
Enrico Granatae992a082011-07-22 17:03:19 +00001243 {
1244 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001245 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001246 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001247 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001248 }
Enrico Granatae992a082011-07-22 17:03:19 +00001249 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001250 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001251 *custom_format,
1252 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001253 return true;
1254}
1255
1256static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001257ScanBracketedRange (const char* var_name_begin,
1258 const char* var_name_end,
1259 const char* var_name_final,
1260 const char** open_bracket_position,
1261 const char** separator_position,
1262 const char** close_bracket_position,
1263 const char** var_name_final_if_array_range,
1264 int64_t* index_lower,
1265 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001266{
Greg Clayton5160ce52013-03-27 23:08:40 +00001267 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001268 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001269 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001270 {
1271 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1272 *close_bracket_position = ::strchr(*open_bracket_position,']');
1273 // as usual, we assume that [] will come before %
1274 //printf("trying to expand a []\n");
1275 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001276 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001277 {
Enrico Granatae992a082011-07-22 17:03:19 +00001278 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001279 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001280 *index_lower = 0;
1281 }
1282 else if (*separator_position == NULL || *separator_position > var_name_end)
1283 {
1284 char *end = NULL;
1285 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1286 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001287 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001288 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001289 }
Greg Clayton34132752011-07-06 04:07:21 +00001290 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001291 {
1292 char *end = NULL;
1293 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1294 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001295 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001296 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001297 }
1298 else
Enrico Granatae992a082011-07-22 17:03:19 +00001299 {
1300 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001301 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001302 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001303 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001304 if (*index_lower > *index_higher && *index_higher > 0)
1305 {
Enrico Granatae992a082011-07-22 17:03:19 +00001306 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001307 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001308 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001309 *index_lower = *index_higher;
1310 *index_higher = temp;
1311 }
1312 }
Enrico Granatae992a082011-07-22 17:03:19 +00001313 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001314 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001315 return true;
1316}
1317
Enrico Granata9fc19442011-07-06 02:13:41 +00001318static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001319ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001320 size_t index,
Enrico Granatadc940732011-08-23 00:32:52 +00001321 StackFrame* frame,
1322 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001323{
Greg Clayton5160ce52013-03-27 23:08:40 +00001324 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001325 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001326 std::string ptr_deref_buffer(10,0);
1327 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001328 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001329 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001330 const char* first_unparsed;
1331 ValueObject::GetValueForExpressionPathOptions options;
1332 ValueObject::ExpressionPathEndResultType final_value_type;
1333 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001334 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001335 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001336 &first_unparsed,
1337 &reason_to_stop,
1338 &final_value_type,
1339 options,
1340 &what_next);
1341 if (!item)
1342 {
Enrico Granatae992a082011-07-22 17:03:19 +00001343 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001344 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001345 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001346 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001347 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001348 else
1349 {
Enrico Granatae992a082011-07-22 17:03:19 +00001350 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001351 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001352 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001353 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001354 }
1355 return item;
1356}
1357
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001358static bool
1359FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001360(
1361 const char *format,
1362 const SymbolContext *sc,
1363 const ExecutionContext *exe_ctx,
1364 const Address *addr,
1365 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001366 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001367 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001368)
1369{
Enrico Granatac482a192011-08-17 22:13:59 +00001370 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001371 bool success = true;
1372 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001373 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001374
Greg Clayton1b654882010-09-19 02:33:57 +00001375 for (p = format; *p != '\0'; ++p)
1376 {
Enrico Granatac482a192011-08-17 22:13:59 +00001377 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001378 {
Enrico Granatac482a192011-08-17 22:13:59 +00001379 valobj = realvalobj;
1380 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001381 }
Greg Clayton1b654882010-09-19 02:33:57 +00001382 size_t non_special_chars = ::strcspn (p, "${}\\");
1383 if (non_special_chars > 0)
1384 {
1385 if (success)
1386 s.Write (p, non_special_chars);
1387 p += non_special_chars;
1388 }
1389
1390 if (*p == '\0')
1391 {
1392 break;
1393 }
1394 else if (*p == '{')
1395 {
1396 // Start a new scope that must have everything it needs if it is to
1397 // to make it into the final output stream "s". If you want to make
1398 // a format that only prints out the function or symbol name if there
1399 // is one in the symbol context you can use:
1400 // "{function =${function.name}}"
1401 // The first '{' starts a new scope that end with the matching '}' at
1402 // the end of the string. The contents "function =${function.name}"
1403 // will then be evaluated and only be output if there is a function
1404 // or symbol with a valid name.
1405 StreamString sub_strm;
1406
1407 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001408
1409 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001410 {
1411 // The stream had all it needed
1412 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1413 }
1414 if (*p != '}')
1415 {
1416 success = false;
1417 break;
1418 }
1419 }
1420 else if (*p == '}')
1421 {
1422 // End of a enclosing scope
1423 break;
1424 }
1425 else if (*p == '$')
1426 {
1427 // We have a prompt variable to print
1428 ++p;
1429 if (*p == '{')
1430 {
1431 ++p;
1432 const char *var_name_begin = p;
1433 const char *var_name_end = ::strchr (p, '}');
1434
1435 if (var_name_end && var_name_begin < var_name_end)
1436 {
1437 // if we have already failed to parse, skip this variable
1438 if (success)
1439 {
1440 const char *cstr = NULL;
1441 Address format_addr;
1442 bool calculate_format_addr_function_offset = false;
1443 // Set reg_kind and reg_num to invalid values
1444 RegisterKind reg_kind = kNumRegisterKinds;
1445 uint32_t reg_num = LLDB_INVALID_REGNUM;
1446 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001447 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001448 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001449 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001450 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1451 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001452
Greg Clayton1b654882010-09-19 02:33:57 +00001453 // Each variable must set success to true below...
1454 bool var_success = false;
1455 switch (var_name_begin[0])
1456 {
Enrico Granata4becb372011-06-29 22:27:15 +00001457 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001458 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001459 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001460 {
Enrico Granatac482a192011-08-17 22:13:59 +00001461 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001462 break;
1463
Enrico Granatac3e320a2011-08-02 17:27:39 +00001464 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001465 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001466
Enrico Granata6f3533f2011-07-29 19:53:35 +00001467 // check for *var and *svar
1468 if (*var_name_begin == '*')
1469 {
1470 do_deref_pointer = true;
1471 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001472 if (log)
1473 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001474 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001475
Enrico Granata6f3533f2011-07-29 19:53:35 +00001476 if (*var_name_begin == 's')
1477 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001478 if (!valobj->IsSynthetic())
1479 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001480 if (!valobj)
1481 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001482 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001483 if (log)
1484 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001485 }
1486
1487 // should be a 'v' by now
1488 if (*var_name_begin != 'v')
1489 break;
1490
Enrico Granatac3e320a2011-08-02 17:27:39 +00001491 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001492 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001493
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001494 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001495 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001496 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001497 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001498 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001499 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001500 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001501 const char* var_name_final = NULL;
1502 const char* var_name_final_if_array_range = NULL;
1503 const char* close_bracket_position = NULL;
1504 int64_t index_lower = -1;
1505 int64_t index_higher = -1;
1506 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001507 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001508 bool was_plain_var = false;
1509 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001510 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001511
Enrico Granatac482a192011-08-17 22:13:59 +00001512 if (!valobj) break;
1513 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001514 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001515 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001516 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001517 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001518 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001519 }
1520 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1521 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001522 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001523 // this is a variable with some custom format applied to it
1524 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001525 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001526 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001527 ScanFormatDescriptor (var_name_begin,
1528 var_name_end,
1529 &var_name_final,
1530 &percent_position,
1531 &custom_format,
1532 &val_obj_display);
1533 }
1534 // this is ${var.something} or multiple .something nested
1535 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1536 {
Enrico Granataa777dc22012-05-08 21:49:57 +00001537 if (::strncmp(var_name_begin, "var[", strlen("var[")) == 0)
1538 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001539 const char* percent_position;
1540 ScanFormatDescriptor (var_name_begin,
1541 var_name_end,
1542 &var_name_final,
1543 &percent_position,
1544 &custom_format,
1545 &val_obj_display);
1546
1547 const char* open_bracket_position;
1548 const char* separator_position;
1549 ScanBracketedRange (var_name_begin,
1550 var_name_end,
1551 var_name_final,
1552 &open_bracket_position,
1553 &separator_position,
1554 &close_bracket_position,
1555 &var_name_final_if_array_range,
1556 &index_lower,
1557 &index_higher);
1558
1559 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001560
Enrico Granata599171a2013-02-01 23:59:44 +00001561 std::string expr_path(var_name_final-var_name_begin-1,0);
1562 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1563
1564 if (log)
1565 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1566
1567 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001568 &first_unparsed,
1569 &reason_to_stop,
1570 &final_value_type,
1571 options,
1572 &what_next).get();
1573
1574 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001575 {
Enrico Granatae992a082011-07-22 17:03:19 +00001576 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001577 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001578 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001579 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001580 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001581 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001582 else
1583 {
Enrico Granatae992a082011-07-22 17:03:19 +00001584 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001585 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001586 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001587 first_unparsed, reason_to_stop, final_value_type);
1588 }
Enrico Granata4becb372011-06-29 22:27:15 +00001589 }
Greg Clayton34132752011-07-06 04:07:21 +00001590 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001591 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001592
Enrico Granata86cc9822012-03-19 22:58:49 +00001593 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1594 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001595
Enrico Granata86cc9822012-03-19 22:58:49 +00001596 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001597
Enrico Granataa7187d02011-07-06 19:27:11 +00001598 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001599 {
Greg Clayton34132752011-07-06 04:07:21 +00001600 // I have not deref-ed yet, let's do it
1601 // this happens when we are not going through GetValueForVariableExpressionPath
1602 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001603 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001604 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001605 if (error.Fail())
1606 {
1607 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001608 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001609 break;
1610 }
Greg Clayton34132752011-07-06 04:07:21 +00001611 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001612 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001613
Enrico Granataa777dc22012-05-08 21:49:57 +00001614 // <rdar://problem/11338654>
1615 // we do not want to use the summary for a bitfield of type T:n
1616 // if we were originally dealing with just a T - that would get
1617 // us into an endless recursion
1618 if (target->IsBitfield() && was_var_indexed)
1619 {
1620 // TODO: check for a (T:n)-specific summary - we should still obey that
1621 StreamString bitfield_name;
1622 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1623 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1624 if (!DataVisualization::GetSummaryForType(type_sp))
1625 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1626 }
1627
Enrico Granata85933ed2011-08-18 16:38:26 +00001628 // TODO use flags for these
Greg Clayton4ef877f2012-12-06 02:33:54 +00001629 bool is_array = ClangASTContext::IsArrayType(target->GetClangType(), NULL, NULL, NULL);
Enrico Granataf4efecd2011-07-12 22:56:10 +00001630 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001631 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001632
Enrico Granata86cc9822012-03-19 22:58:49 +00001633 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 +00001634 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001635 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001636 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001637 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001638
Greg Clayton5088c482013-03-25 21:06:13 +00001639 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001640 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001641 // try to use the special cases
1642 var_success = target->DumpPrintableRepresentation(str_temp,
1643 val_obj_display,
1644 custom_format);
1645 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001646 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001647
1648 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001649 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001650 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001651 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001652 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001653 }
1654 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001655 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001656 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001657 {
1658 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1659 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001660 else if (is_pointer) // if pointer, value is the address stored
1661 {
Greg Clayton23f59502012-07-17 03:23:13 +00001662 target->DumpPrintableRepresentation (s,
1663 val_obj_display,
1664 custom_format,
1665 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001666 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001667 var_success = true;
1668 break;
1669 }
1670 }
1671
1672 // if directly trying to print ${var}, and this is an aggregate, display a nice
1673 // type @ location message
1674 if (is_aggregate && was_plain_var)
1675 {
1676 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1677 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001678 break;
1679 }
1680
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001681 // 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 +00001682 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001683 {
1684 s << "<invalid use of aggregate type>";
1685 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001686 break;
1687 }
Greg Clayton34132752011-07-06 04:07:21 +00001688
1689 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001690 {
1691 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001692 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001693 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001694 }
Greg Clayton34132752011-07-06 04:07:21 +00001695 else
Enrico Granatae992a082011-07-22 17:03:19 +00001696 {
1697 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001698 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001699 if (!is_array && !is_pointer)
1700 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001701 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001702 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001703 const char* special_directions = NULL;
1704 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001705 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1706 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001707 ConstString additional_data;
1708 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1709 special_directions_writer.Printf("${%svar%s}",
1710 do_deref_pointer ? "*" : "",
1711 additional_data.GetCString());
1712 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001713 }
1714
1715 // let us display items index_lower thru index_higher of this array
1716 s.PutChar('[');
1717 var_success = true;
1718
1719 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001720 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001721
Greg Claytoncc4d0142012-02-17 07:49:44 +00001722 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001723
Greg Clayton34132752011-07-06 04:07:21 +00001724 for (;index_lower<=index_higher;index_lower++)
1725 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001726 ValueObject* item = ExpandIndexedExpression (target,
1727 index_lower,
1728 exe_ctx->GetFramePtr(),
1729 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001730
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001731 if (!item)
1732 {
Enrico Granatae992a082011-07-22 17:03:19 +00001733 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001734 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001735 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001736 else
1737 {
Enrico Granatae992a082011-07-22 17:03:19 +00001738 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001739 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001740 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001741
Greg Clayton34132752011-07-06 04:07:21 +00001742 if (!special_directions)
1743 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1744 else
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001745 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
Greg Clayton34132752011-07-06 04:07:21 +00001746
Enrico Granata22c55d12011-08-12 02:00:06 +00001747 if (--max_num_children == 0)
1748 {
1749 s.PutCString(", ...");
1750 break;
1751 }
1752
Greg Clayton34132752011-07-06 04:07:21 +00001753 if (index_lower < index_higher)
1754 s.PutChar(',');
1755 }
1756 s.PutChar(']');
1757 }
Enrico Granata4becb372011-06-29 22:27:15 +00001758 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001759 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001760 case 'a':
1761 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1762 {
1763 if (addr && addr->IsValid())
1764 {
1765 var_success = true;
1766 format_addr = *addr;
1767 }
1768 }
1769 break;
1770
1771 case 'p':
1772 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1773 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001774 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001775 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001776 Process *process = exe_ctx->GetProcessPtr();
1777 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001778 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001779 var_name_begin += ::strlen ("process.");
1780 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001781 {
Daniel Malead01b2952012-11-29 21:49:15 +00001782 s.Printf("%" PRIu64, process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001783 var_success = true;
1784 }
1785 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1786 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1787 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1788 {
1789 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1790 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001791 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001792 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1793 {
1794 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1795 var_success = format_file_spec;
1796 }
1797 else
1798 {
1799 format_file_spec = exe_module->GetFileSpec();
1800 var_success = format_file_spec;
1801 }
Greg Clayton1b654882010-09-19 02:33:57 +00001802 }
1803 }
1804 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001805 }
Greg Clayton1b654882010-09-19 02:33:57 +00001806 }
1807 break;
1808
1809 case 't':
1810 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1811 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001812 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001813 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001814 Thread *thread = exe_ctx->GetThreadPtr();
1815 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001816 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001817 var_name_begin += ::strlen ("thread.");
1818 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001819 {
Daniel Malead01b2952012-11-29 21:49:15 +00001820 s.Printf("0x%4.4" PRIx64, thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001821 var_success = true;
1822 }
Greg Clayton160c9d82013-05-01 21:54:04 +00001823 else if (::strncmp (var_name_begin, "protocol_id}", strlen("protocol_id}")) == 0)
1824 {
1825 s.Printf("0x%4.4" PRIx64, thread->GetProtocolID());
1826 var_success = true;
1827 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001828 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1829 {
1830 s.Printf("%u", thread->GetIndexID());
1831 var_success = true;
1832 }
1833 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1834 {
1835 cstr = thread->GetName();
1836 var_success = cstr && cstr[0];
1837 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001838 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001839 }
1840 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1841 {
1842 cstr = thread->GetQueueName();
1843 var_success = cstr && cstr[0];
1844 if (var_success)
1845 s.PutCString(cstr);
1846 }
1847 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1848 {
1849 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001850 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001851 {
1852 cstr = stop_info_sp->GetDescription();
1853 if (cstr && cstr[0])
1854 {
1855 s.PutCString(cstr);
1856 var_success = true;
1857 }
Greg Clayton1b654882010-09-19 02:33:57 +00001858 }
1859 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001860 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1861 {
1862 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001863 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001864 {
1865 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1866 if (return_valobj_sp)
1867 {
Enrico Granata9fb5ab52013-03-26 18:04:53 +00001868 ValueObject::DumpValueObject (s, return_valobj_sp.get());
Jim Inghamef651602011-12-22 19:12:40 +00001869 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001870 }
1871 }
1872 }
Greg Clayton1b654882010-09-19 02:33:57 +00001873 }
1874 }
1875 }
1876 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1877 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001878 // TODO: hookup properties
1879// if (!target_properties_sp)
1880// {
1881// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1882// if (target)
1883// target_properties_sp = target->GetProperties();
1884// }
1885//
1886// if (target_properties_sp)
1887// {
1888// var_name_begin += ::strlen ("target.");
1889// const char *end_property = strchr(var_name_begin, '}');
1890// if (end_property)
1891// {
1892// ConstString property_name(var_name_begin, end_property - var_name_begin);
1893// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
1894// if (!property_value.empty())
1895// {
1896// s.PutCString (property_value.c_str());
1897// var_success = true;
1898// }
1899// }
1900// }
Greg Clayton0603aa92010-10-04 01:05:56 +00001901 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1902 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001903 {
Greg Clayton1b654882010-09-19 02:33:57 +00001904 var_name_begin += ::strlen ("target.");
1905 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1906 {
1907 ArchSpec arch (target->GetArchitecture ());
1908 if (arch.IsValid())
1909 {
Greg Clayton64195a22011-02-23 00:35:02 +00001910 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001911 var_success = true;
1912 }
1913 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001914 }
Greg Clayton1b654882010-09-19 02:33:57 +00001915 }
1916 break;
1917
1918
1919 case 'm':
1920 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1921 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001922 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001923 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001924 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001925 var_name_begin += ::strlen ("module.");
1926
1927 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1928 {
1929 if (module->GetFileSpec())
1930 {
1931 var_name_begin += ::strlen ("file.");
1932
1933 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1934 {
1935 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1936 var_success = format_file_spec;
1937 }
1938 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1939 {
1940 format_file_spec = module->GetFileSpec();
1941 var_success = format_file_spec;
1942 }
1943 }
1944 }
1945 }
1946 }
1947 break;
1948
1949
1950 case 'f':
1951 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1952 {
1953 if (sc && sc->comp_unit != NULL)
1954 {
1955 var_name_begin += ::strlen ("file.");
1956
1957 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1958 {
1959 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1960 var_success = format_file_spec;
1961 }
1962 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1963 {
1964 format_file_spec = *sc->comp_unit;
1965 var_success = format_file_spec;
1966 }
1967 }
1968 }
1969 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1970 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001971 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001972 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001973 StackFrame *frame = exe_ctx->GetFramePtr();
1974 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001975 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001976 var_name_begin += ::strlen ("frame.");
1977 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001978 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001979 s.Printf("%u", frame->GetFrameIndex());
1980 var_success = true;
1981 }
1982 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1983 {
1984 reg_kind = eRegisterKindGeneric;
1985 reg_num = LLDB_REGNUM_GENERIC_PC;
1986 var_success = true;
1987 }
1988 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1989 {
1990 reg_kind = eRegisterKindGeneric;
1991 reg_num = LLDB_REGNUM_GENERIC_SP;
1992 var_success = true;
1993 }
1994 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1995 {
1996 reg_kind = eRegisterKindGeneric;
1997 reg_num = LLDB_REGNUM_GENERIC_FP;
1998 var_success = true;
1999 }
2000 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
2001 {
2002 reg_kind = eRegisterKindGeneric;
2003 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2004 var_success = true;
2005 }
2006 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
2007 {
2008 reg_ctx = frame->GetRegisterContext().get();
2009 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002010 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002011 var_name_begin += ::strlen ("reg.");
2012 if (var_name_begin < var_name_end)
2013 {
2014 std::string reg_name (var_name_begin, var_name_end);
2015 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2016 if (reg_info)
2017 var_success = true;
2018 }
Greg Clayton1b654882010-09-19 02:33:57 +00002019 }
2020 }
2021 }
2022 }
2023 }
2024 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
2025 {
2026 if (sc && (sc->function != NULL || sc->symbol != NULL))
2027 {
2028 var_name_begin += ::strlen ("function.");
2029 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
2030 {
2031 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002032 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002033 else
2034 s.Printf("symbol[%u]", sc->symbol->GetID());
2035
2036 var_success = true;
2037 }
2038 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
2039 {
2040 if (sc->function)
2041 cstr = sc->function->GetName().AsCString (NULL);
2042 else if (sc->symbol)
2043 cstr = sc->symbol->GetName().AsCString (NULL);
2044 if (cstr)
2045 {
2046 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002047
2048 if (sc->block)
2049 {
2050 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2051 if (inline_block)
2052 {
2053 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2054 if (inline_info)
2055 {
2056 s.PutCString(" [inlined] ");
2057 inline_info->GetName().Dump(&s);
2058 }
2059 }
2060 }
Greg Clayton1b654882010-09-19 02:33:57 +00002061 var_success = true;
2062 }
2063 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002064 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
2065 {
2066 // Print the function name with arguments in it
2067
2068 if (sc->function)
2069 {
2070 var_success = true;
2071 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2072 cstr = sc->function->GetName().AsCString (NULL);
2073 if (cstr)
2074 {
2075 const InlineFunctionInfo *inline_info = NULL;
2076 VariableListSP variable_list_sp;
2077 bool get_function_vars = true;
2078 if (sc->block)
2079 {
2080 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2081
2082 if (inline_block)
2083 {
2084 get_function_vars = false;
2085 inline_info = sc->block->GetInlinedFunctionInfo();
2086 if (inline_info)
2087 variable_list_sp = inline_block->GetBlockVariableList (true);
2088 }
2089 }
2090
2091 if (get_function_vars)
2092 {
2093 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2094 }
2095
2096 if (inline_info)
2097 {
2098 s.PutCString (cstr);
2099 s.PutCString (" [inlined] ");
2100 cstr = inline_info->GetName().GetCString();
2101 }
2102
2103 VariableList args;
2104 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002105 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002106 if (args.GetSize() > 0)
2107 {
2108 const char *open_paren = strchr (cstr, '(');
2109 const char *close_paren = NULL;
2110 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002111 {
2112 if (strncmp(open_paren, "(anonymous namespace)", strlen("(anonymous namespace)")) == 0)
2113 {
2114 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2115 if (open_paren)
2116 close_paren = strchr (open_paren, ')');
2117 }
2118 else
2119 close_paren = strchr (open_paren, ')');
2120 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002121
2122 if (open_paren)
2123 s.Write(cstr, open_paren - cstr + 1);
2124 else
2125 {
2126 s.PutCString (cstr);
2127 s.PutChar ('(');
2128 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002129 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002130 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2131 {
2132 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2133 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2134 const char *var_name = var_value_sp->GetName().GetCString();
2135 const char *var_value = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002136 if (arg_idx > 0)
2137 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002138 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002139 {
2140 if (var_value)
2141 s.Printf ("%s=%s", var_name, var_value);
2142 else
2143 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2144 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002145 else
2146 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002147 }
2148
2149 if (close_paren)
2150 s.PutCString (close_paren);
2151 else
2152 s.PutChar(')');
2153
2154 }
2155 else
2156 {
2157 s.PutCString(cstr);
2158 }
2159 }
2160 }
2161 else if (sc->symbol)
2162 {
2163 cstr = sc->symbol->GetName().AsCString (NULL);
2164 if (cstr)
2165 {
2166 s.PutCString(cstr);
2167 var_success = true;
2168 }
2169 }
2170 }
Greg Clayton1b654882010-09-19 02:33:57 +00002171 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2172 {
2173 var_success = addr != NULL;
2174 if (var_success)
2175 {
2176 format_addr = *addr;
2177 calculate_format_addr_function_offset = true;
2178 }
2179 }
2180 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2181 {
2182 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2183 if (var_success)
2184 {
2185 format_addr = sc->line_entry.range.GetBaseAddress();
2186 calculate_format_addr_function_offset = true;
2187 }
2188 }
2189 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2190 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002191 StackFrame *frame = exe_ctx->GetFramePtr();
2192 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002193 if (var_success)
2194 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002195 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002196 calculate_format_addr_function_offset = true;
2197 }
2198 }
2199 }
2200 }
2201 break;
2202
2203 case 'l':
2204 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2205 {
2206 if (sc && sc->line_entry.IsValid())
2207 {
2208 var_name_begin += ::strlen ("line.");
2209 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2210 {
2211 var_name_begin += ::strlen ("file.");
2212
2213 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2214 {
2215 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2216 var_success = format_file_spec;
2217 }
2218 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2219 {
2220 format_file_spec = sc->line_entry.file;
2221 var_success = format_file_spec;
2222 }
2223 }
2224 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2225 {
2226 var_success = true;
2227 s.Printf("%u", sc->line_entry.line);
2228 }
2229 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2230 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2231 {
2232 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2233 if (var_success)
2234 {
2235 format_addr = sc->line_entry.range.GetBaseAddress();
2236 if (var_name_begin[0] == 'e')
2237 format_addr.Slide (sc->line_entry.range.GetByteSize());
2238 }
2239 }
2240 }
2241 }
2242 break;
2243 }
2244
2245 if (var_success)
2246 {
2247 // If format addr is valid, then we need to print an address
2248 if (reg_num != LLDB_INVALID_REGNUM)
2249 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002250 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002251 // We have a register value to display...
2252 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2253 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002254 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002255 }
2256 else
2257 {
2258 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002259 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002260
2261 if (reg_ctx)
2262 {
2263 if (reg_kind != kNumRegisterKinds)
2264 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2265 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2266 var_success = reg_info != NULL;
2267 }
2268 }
2269 }
2270
2271 if (reg_info != NULL)
2272 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002273 RegisterValue reg_value;
2274 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2275 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002276 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002277 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002278 }
2279 }
2280
2281 if (format_file_spec)
2282 {
2283 s << format_file_spec;
2284 }
2285
2286 // If format addr is valid, then we need to print an address
2287 if (format_addr.IsValid())
2288 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002289 var_success = false;
2290
Greg Clayton1b654882010-09-19 02:33:57 +00002291 if (calculate_format_addr_function_offset)
2292 {
2293 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002294
Greg Clayton0603aa92010-10-04 01:05:56 +00002295 if (sc)
2296 {
2297 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002298 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002299 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002300 if (sc->block)
2301 {
2302 // Check to make sure we aren't in an inline
2303 // function. If we are, use the inline block
2304 // range that contains "format_addr" since
2305 // blocks can be discontiguous.
2306 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2307 AddressRange inline_range;
2308 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2309 func_addr = inline_range.GetBaseAddress();
2310 }
2311 }
Greg Claytone7612132012-03-07 21:03:09 +00002312 else if (sc->symbol && sc->symbol->ValueIsAddress())
2313 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002314 }
2315
2316 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002317 {
2318 if (func_addr.GetSection() == format_addr.GetSection())
2319 {
2320 addr_t func_file_addr = func_addr.GetFileAddress();
2321 addr_t addr_file_addr = format_addr.GetFileAddress();
2322 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002323 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002324 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002325 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002326 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002327 }
2328 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002329 {
2330 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2331 if (target)
2332 {
2333 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2334 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2335 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002336 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002337 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002338 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002339 var_success = true;
2340 }
2341 }
Greg Clayton1b654882010-09-19 02:33:57 +00002342 }
2343 }
2344 else
2345 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002346 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002347 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002348 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2349 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002350 if (vaddr == LLDB_INVALID_ADDRESS)
2351 vaddr = format_addr.GetFileAddress ();
2352
2353 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002354 {
Greg Clayton514487e2011-02-15 21:59:32 +00002355 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002356 if (addr_width == 0)
2357 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002358 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002359 var_success = true;
2360 }
Greg Clayton1b654882010-09-19 02:33:57 +00002361 }
2362 }
2363 }
2364
2365 if (var_success == false)
2366 success = false;
2367 }
2368 p = var_name_end;
2369 }
2370 else
2371 break;
2372 }
2373 else
2374 {
2375 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2376 s.PutChar(*p);
2377 }
2378 }
2379 else if (*p == '\\')
2380 {
2381 ++p; // skip the slash
2382 switch (*p)
2383 {
2384 case 'a': s.PutChar ('\a'); break;
2385 case 'b': s.PutChar ('\b'); break;
2386 case 'f': s.PutChar ('\f'); break;
2387 case 'n': s.PutChar ('\n'); break;
2388 case 'r': s.PutChar ('\r'); break;
2389 case 't': s.PutChar ('\t'); break;
2390 case 'v': s.PutChar ('\v'); break;
2391 case '\'': s.PutChar ('\''); break;
2392 case '\\': s.PutChar ('\\'); break;
2393 case '0':
2394 // 1 to 3 octal chars
2395 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002396 // Make a string that can hold onto the initial zero char,
2397 // up to 3 octal digits, and a terminating NULL.
2398 char oct_str[5] = { 0, 0, 0, 0, 0 };
2399
2400 int i;
2401 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2402 oct_str[i] = p[i];
2403
2404 // We don't want to consume the last octal character since
2405 // the main for loop will do this for us, so we advance p by
2406 // one less than i (even if i is zero)
2407 p += i - 1;
2408 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2409 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002410 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002411 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002412 }
Greg Clayton1b654882010-09-19 02:33:57 +00002413 }
2414 break;
2415
2416 case 'x':
2417 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002418 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002419 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002420 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002421
Greg Clayton0603aa92010-10-04 01:05:56 +00002422 // Make a string that can hold onto two hex chars plus a
2423 // NULL terminator
2424 char hex_str[3] = { 0,0,0 };
2425 hex_str[0] = *p;
2426 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002427 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002428 ++p; // Skip the first of the two hex chars
2429 hex_str[1] = *p;
2430 }
2431
2432 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2433 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002434 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002435 }
2436 else
2437 {
2438 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002439 }
2440 break;
2441
2442 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002443 // Just desensitize any other character by just printing what
2444 // came after the '\'
2445 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002446 break;
2447
2448 }
2449
2450 }
2451 }
2452 if (end)
2453 *end = p;
2454 return success;
2455}
2456
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002457bool
2458Debugger::FormatPrompt
2459(
2460 const char *format,
2461 const SymbolContext *sc,
2462 const ExecutionContext *exe_ctx,
2463 const Address *addr,
2464 Stream &s,
2465 ValueObject* valobj
2466)
2467{
2468 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2469 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2470 if (format_str.length())
2471 format = format_str.c_str();
2472 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
2473}
2474
Jim Ingham228063c2012-02-21 02:23:08 +00002475void
2476Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2477{
Jim Ingham4f02b222012-02-22 22:49:20 +00002478 // For simplicity's sake, I am not going to deal with how to close down any
2479 // open logging streams, I just redirect everything from here on out to the
2480 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002481 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2482}
2483
2484bool
2485Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2486{
2487 Log::Callbacks log_callbacks;
2488
2489 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002490 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002491 {
2492 log_stream_sp = m_log_callback_stream_sp;
2493 // For now when using the callback mode you always get thread & timestamp.
2494 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2495 }
2496 else if (log_file == NULL || *log_file == '\0')
2497 {
2498 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2499 }
2500 else
2501 {
2502 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002503 if (pos != m_log_streams.end())
2504 log_stream_sp = pos->second.lock();
2505 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002506 {
2507 log_stream_sp.reset (new StreamFile (log_file));
2508 m_log_streams[log_file] = log_stream_sp;
2509 }
Jim Ingham228063c2012-02-21 02:23:08 +00002510 }
2511 assert (log_stream_sp.get());
2512
2513 if (log_options == 0)
2514 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2515
Greg Clayton57abc5d2013-05-10 21:47:16 +00002516 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002517 {
2518 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2519 return true;
2520 }
2521 else
2522 {
2523 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2524 if (log_channel_sp)
2525 {
2526 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2527 {
2528 return true;
2529 }
2530 else
2531 {
2532 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2533 return false;
2534 }
2535 }
2536 else
2537 {
2538 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2539 return false;
2540 }
2541 }
2542 return false;
2543}
2544
Greg Clayton9585fbf2013-03-19 00:20:55 +00002545SourceManager &
2546Debugger::GetSourceManager ()
2547{
2548 if (m_source_manager_ap.get() == NULL)
2549 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2550 return *m_source_manager_ap;
2551}
2552
2553