blob: 434029d89a8a4be3c00ee1cf191f7d26b1f1b6a0 [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
Greg Clayton4a33d312011-06-23 17:59:56 +000012#include "lldb/Core/Debugger.h"
13
14#include <map>
15
Enrico Granata4becb372011-06-29 22:27:15 +000016#include "clang/AST/DeclCXX.h"
17#include "clang/AST/Type.h"
18
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/lldb-private.h"
20#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/Module.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000022#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000023#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000025#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000026#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000027#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000028#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000030#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000031#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000032#include "lldb/DataFormatters/DataVisualization.h"
33#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000034#include "lldb/DataFormatters/TypeSummary.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000035#include "lldb/Host/DynamicLibrary.h"
Greg Claytona3406612011-02-07 23:24:47 +000036#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000037#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000038#include "lldb/Interpreter/OptionValueSInt64.h"
39#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000040#include "lldb/Symbol/ClangASTContext.h"
41#include "lldb/Symbol/CompileUnit.h"
42#include "lldb/Symbol/Function.h"
43#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000044#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Target/TargetList.h"
46#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000047#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000048#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000049#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000050#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000052#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54using namespace lldb;
55using namespace lldb_private;
56
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
Greg Clayton1b654882010-09-19 02:33:57 +000058static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000059static lldb::user_id_t g_unique_id = 1;
60
Greg Clayton1b654882010-09-19 02:33:57 +000061#pragma mark Static Functions
62
63static Mutex &
64GetDebuggerListMutex ()
65{
66 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
67 return g_mutex;
68}
69
70typedef std::vector<DebuggerSP> DebuggerList;
71
72static DebuggerList &
73GetDebuggerList()
74{
75 // hide the static debugger list inside a singleton accessor to avoid
76 // global init contructors
77 static DebuggerList g_list;
78 return g_list;
79}
Greg Claytone372b982011-11-21 21:44:34 +000080
81OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000082g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000083{
Greg Clayton67cc0632012-08-22 17:17:09 +000084 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
85 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
86 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000087 { 0, NULL, NULL }
88};
89
Greg Clayton67cc0632012-08-22 17:17:09 +000090OptionEnumValueElement
91g_language_enumerators[] =
92{
93 { eScriptLanguageNone, "none", "Disable scripting languages."},
94 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
95 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000096 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000097};
Greg Claytone372b982011-11-21 21:44:34 +000098
Greg Clayton67cc0632012-08-22 17:17:09 +000099#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
100#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
101
Michael Sartain0769b2b2013-07-30 16:44:36 +0000102#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000103 "{, ${frame.pc}}"\
104 MODULE_WITH_FUNC\
105 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000106 "{, name = '${thread.name}'}"\
107 "{, queue = '${thread.queue}'}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000108 "{, stop reason = ${thread.stop-reason}}"\
109 "{\\nReturn value: ${thread.return-value}}"\
110 "\\n"
111
112#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
113 MODULE_WITH_FUNC\
114 FILE_AND_LINE\
115 "\\n"
116
117
118
Greg Clayton754a9362012-08-23 00:22:02 +0000119static PropertyDefinition
120g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000121{
122{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
123{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
124{ "notify-void", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Notify the user explicitly if an expression returns void (default: false)." },
Greg Clayton4c054102012-09-01 00:38:36 +0000125{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000126{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
127{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
128{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
129{ "stop-line-count-after", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come after the current source line when displaying a stopped context." },
130{ "stop-line-count-before", OptionValue::eTypeSInt64 , true, 3 , NULL, NULL, "The number of sources lines to display that come before the current source line when displaying a stopped context." },
131{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
132{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
133{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000134{ "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
Enrico Granata90a8db32013-10-31 21:01:07 +0000135{ "auto-one-line-summaries", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
Greg Claytone8cd0c92012-10-19 18:02:49 +0000136
137 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000138};
139
140enum
141{
142 ePropertyAutoConfirm = 0,
143 ePropertyFrameFormat,
144 ePropertyNotiftVoid,
145 ePropertyPrompt,
146 ePropertyScriptLanguage,
147 ePropertyStopDisassemblyCount,
148 ePropertyStopDisassemblyDisplay,
149 ePropertyStopLineCountAfter,
150 ePropertyStopLineCountBefore,
151 ePropertyTerminalWidth,
152 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000153 ePropertyUseExternalEditor,
154 ePropertyUseColor,
Enrico Granata90a8db32013-10-31 21:01:07 +0000155 ePropertyAutoOneLineSummaries
Greg Clayton67cc0632012-08-22 17:17:09 +0000156};
157
Greg Clayton5fb8f792013-12-02 19:35:49 +0000158Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000159
160Error
161Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
162 VarSetOperationType op,
163 const char *property_path,
164 const char *value)
165{
Enrico Granata84a53df2013-05-20 22:29:23 +0000166 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
167 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000168 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000169 if (is_load_script && exe_ctx->GetTargetSP())
170 {
171 target_sp = exe_ctx->GetTargetSP();
172 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
173 }
Greg Clayton4c054102012-09-01 00:38:36 +0000174 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
175 if (error.Success())
176 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000177 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000178 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
179 {
180 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000181 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
182 if (str.length())
183 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000184 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000185 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
186 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
187 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000188 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
189 {
190 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
191 SetPrompt (GetPrompt());
192 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000193 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000194 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000195 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000196 {
197 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000198 StreamString feedback_stream;
199 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000200 {
Greg Clayton44d93782014-01-27 23:43:24 +0000201 StreamFileSP stream_sp (GetErrorFile());
202 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000203 {
Greg Clayton44d93782014-01-27 23:43:24 +0000204 for (auto error : errors)
205 {
206 stream_sp->Printf("%s\n",error.AsCString());
207 }
208 if (feedback_stream.GetSize())
209 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000210 }
211 }
212 }
213 }
Greg Clayton4c054102012-09-01 00:38:36 +0000214 }
215 return error;
216}
217
Greg Clayton67cc0632012-08-22 17:17:09 +0000218bool
219Debugger::GetAutoConfirm () const
220{
221 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000222 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000223}
224
225const char *
226Debugger::GetFrameFormat() const
227{
228 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000229 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000230}
231
232bool
233Debugger::GetNotifyVoid () const
234{
235 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000236 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000237}
238
239const char *
240Debugger::GetPrompt() const
241{
242 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000243 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000244}
245
246void
247Debugger::SetPrompt(const char *p)
248{
249 const uint32_t idx = ePropertyPrompt;
250 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
251 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000252 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
253 if (str.length())
254 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000255 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000256}
257
258const char *
259Debugger::GetThreadFormat() const
260{
261 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000262 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000263}
264
265lldb::ScriptLanguage
266Debugger::GetScriptLanguage() const
267{
268 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000269 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000270}
271
272bool
273Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
274{
275 const uint32_t idx = ePropertyScriptLanguage;
276 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
277}
278
279uint32_t
280Debugger::GetTerminalWidth () const
281{
282 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000283 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000284}
285
286bool
287Debugger::SetTerminalWidth (uint32_t term_width)
288{
289 const uint32_t idx = ePropertyTerminalWidth;
290 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
291}
292
293bool
294Debugger::GetUseExternalEditor () const
295{
296 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000297 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000298}
299
300bool
301Debugger::SetUseExternalEditor (bool b)
302{
303 const uint32_t idx = ePropertyUseExternalEditor;
304 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
305}
306
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000307bool
308Debugger::GetUseColor () const
309{
310 const uint32_t idx = ePropertyUseColor;
311 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
312}
313
314bool
315Debugger::SetUseColor (bool b)
316{
317 const uint32_t idx = ePropertyUseColor;
318 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
319 SetPrompt (GetPrompt());
320 return ret;
321}
322
Greg Clayton67cc0632012-08-22 17:17:09 +0000323uint32_t
324Debugger::GetStopSourceLineCount (bool before) const
325{
326 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000327 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000328}
329
330Debugger::StopDisassemblyType
331Debugger::GetStopDisassemblyDisplay () const
332{
333 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000334 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000335}
336
337uint32_t
338Debugger::GetDisassemblyLineCount () const
339{
340 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000341 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000342}
Greg Claytone372b982011-11-21 21:44:34 +0000343
Enrico Granata553fad52013-10-25 23:09:40 +0000344bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000345Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000346{
Enrico Granata90a8db32013-10-31 21:01:07 +0000347 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000348 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
349
350}
351
Greg Clayton1b654882010-09-19 02:33:57 +0000352#pragma mark Debugger
353
Greg Clayton67cc0632012-08-22 17:17:09 +0000354//const DebuggerPropertiesSP &
355//Debugger::GetSettings() const
356//{
357// return m_properties_sp;
358//}
359//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000360
Caroline Tice2f88aad2011-01-14 00:29:16 +0000361int
362Debugger::TestDebuggerRefCount ()
363{
364 return g_shared_debugger_refcount;
365}
366
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367void
Greg Clayton5fb8f792013-12-02 19:35:49 +0000368Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000370 g_load_plugin_callback = load_plugin_callback;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000371 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000372 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373}
374
375void
376Debugger::Terminate ()
377{
Greg Clayton66111032010-06-23 01:19:29 +0000378 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 {
Greg Clayton66111032010-06-23 01:19:29 +0000380 g_shared_debugger_refcount--;
381 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 {
Greg Claytondbe54502010-11-19 03:46:01 +0000383 lldb_private::WillTerminate();
384 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000385
386 // Clear our master list of debugger objects
387 Mutex::Locker locker (GetDebuggerListMutex ());
388 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 }
391}
392
Caroline Tice20bd37f2011-03-10 22:14:10 +0000393void
394Debugger::SettingsInitialize ()
395{
Greg Clayton6920b522012-08-22 18:39:03 +0000396 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000397}
398
399void
400Debugger::SettingsTerminate ()
401{
Greg Clayton6920b522012-08-22 18:39:03 +0000402 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000403}
404
Enrico Granata21dfcd92012-09-28 23:57:51 +0000405bool
Enrico Granatae743c782013-04-24 21:29:08 +0000406Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000407{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000408 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000409 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000410 lldb::DynamicLibrarySP dynlib_sp = g_load_plugin_callback (shared_from_this(), spec, error);
411 if (dynlib_sp)
412 {
413 m_loaded_plugins.push_back(dynlib_sp);
414 return true;
415 }
Enrico Granatae743c782013-04-24 21:29:08 +0000416 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000417 else
Enrico Granatae743c782013-04-24 21:29:08 +0000418 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000419 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
420 // and if the public API layer isn't available (code is linking against
421 // all of the internal LLDB static libraries), then we can't load plugins
422 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000423 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000424 return false;
425}
426
427static FileSpec::EnumerateDirectoryResult
428LoadPluginCallback
429(
430 void *baton,
431 FileSpec::FileType file_type,
432 const FileSpec &file_spec
433 )
434{
435 Error error;
436
437 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000438 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000439
440 if (!baton)
441 return FileSpec::eEnumerateDirectoryResultQuit;
442
443 Debugger *debugger = (Debugger*)baton;
444
445 // If we have a regular file, a symbolic link or unknown file type, try
446 // and process the file. We must handle unknown as sometimes the directory
447 // enumeration might be enumerating a file system that doesn't have correct
448 // file type information.
449 if (file_type == FileSpec::eFileTypeRegular ||
450 file_type == FileSpec::eFileTypeSymbolicLink ||
451 file_type == FileSpec::eFileTypeUnknown )
452 {
453 FileSpec plugin_file_spec (file_spec);
454 plugin_file_spec.ResolvePath ();
455
Michael Sartain3cf443d2013-07-17 00:26:30 +0000456 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
457 plugin_file_spec.GetFileNameExtension() != g_solibext)
458 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000459 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000460 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000461
Enrico Granatae743c782013-04-24 21:29:08 +0000462 Error plugin_load_error;
463 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000464
465 return FileSpec::eEnumerateDirectoryResultNext;
466 }
467
468 else if (file_type == FileSpec::eFileTypeUnknown ||
469 file_type == FileSpec::eFileTypeDirectory ||
470 file_type == FileSpec::eFileTypeSymbolicLink )
471 {
472 // Try and recurse into anything that a directory or symbolic link.
473 // We must also do this for unknown as sometimes the directory enumeration
474 // might be enurating a file system that doesn't have correct file type
475 // information.
476 return FileSpec::eEnumerateDirectoryResultEnter;
477 }
478
479 return FileSpec::eEnumerateDirectoryResultNext;
480}
481
482void
483Debugger::InstanceInitialize ()
484{
485 FileSpec dir_spec;
486 const bool find_directories = true;
487 const bool find_files = true;
488 const bool find_other = true;
489 char dir_path[PATH_MAX];
490 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
491 {
492 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
493 {
494 FileSpec::EnumerateDirectory (dir_path,
495 find_directories,
496 find_files,
497 find_other,
498 LoadPluginCallback,
499 this);
500 }
501 }
502
503 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
504 {
505 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
506 {
507 FileSpec::EnumerateDirectory (dir_path,
508 find_directories,
509 find_files,
510 find_other,
511 LoadPluginCallback,
512 this);
513 }
514 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000515
516 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000517}
518
Greg Clayton66111032010-06-23 01:19:29 +0000519DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000520Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000521{
Jim Ingham228063c2012-02-21 02:23:08 +0000522 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000523 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000524 {
525 Mutex::Locker locker (GetDebuggerListMutex ());
526 GetDebuggerList().push_back(debugger_sp);
527 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000528 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000529 return debugger_sp;
530}
531
Caroline Ticee02657b2011-01-22 01:02:07 +0000532void
Greg Clayton4d122c42011-09-17 08:33:22 +0000533Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000534{
535 if (debugger_sp.get() == NULL)
536 return;
537
Jim Ingham8314c522011-09-15 21:36:42 +0000538 debugger_sp->Clear();
539
Greg Claytonc15f55e2012-03-30 20:53:46 +0000540 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000541 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000542 Mutex::Locker locker (GetDebuggerListMutex ());
543 DebuggerList &debugger_list = GetDebuggerList ();
544 DebuggerList::iterator pos, end = debugger_list.end();
545 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000546 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000547 if ((*pos).get() == debugger_sp.get())
548 {
549 debugger_list.erase (pos);
550 return;
551 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000552 }
553 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000554}
555
Greg Clayton4d122c42011-09-17 08:33:22 +0000556DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000557Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
558{
Greg Clayton4d122c42011-09-17 08:33:22 +0000559 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000560 if (g_shared_debugger_refcount > 0)
561 {
562 Mutex::Locker locker (GetDebuggerListMutex ());
563 DebuggerList &debugger_list = GetDebuggerList();
564 DebuggerList::iterator pos, end = debugger_list.end();
565
566 for (pos = debugger_list.begin(); pos != end; ++pos)
567 {
568 if ((*pos).get()->m_instance_name == instance_name)
569 {
570 debugger_sp = *pos;
571 break;
572 }
573 }
574 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000575 return debugger_sp;
576}
Greg Clayton66111032010-06-23 01:19:29 +0000577
578TargetSP
579Debugger::FindTargetWithProcessID (lldb::pid_t pid)
580{
Greg Clayton4d122c42011-09-17 08:33:22 +0000581 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000582 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000583 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000584 Mutex::Locker locker (GetDebuggerListMutex ());
585 DebuggerList &debugger_list = GetDebuggerList();
586 DebuggerList::iterator pos, end = debugger_list.end();
587 for (pos = debugger_list.begin(); pos != end; ++pos)
588 {
589 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
590 if (target_sp)
591 break;
592 }
Greg Clayton66111032010-06-23 01:19:29 +0000593 }
594 return target_sp;
595}
596
Greg Claytone4e45922011-11-16 05:37:56 +0000597TargetSP
598Debugger::FindTargetWithProcess (Process *process)
599{
600 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000601 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000602 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000603 Mutex::Locker locker (GetDebuggerListMutex ());
604 DebuggerList &debugger_list = GetDebuggerList();
605 DebuggerList::iterator pos, end = debugger_list.end();
606 for (pos = debugger_list.begin(); pos != end; ++pos)
607 {
608 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
609 if (target_sp)
610 break;
611 }
Greg Claytone4e45922011-11-16 05:37:56 +0000612 }
613 return target_sp;
614}
615
Jim Ingham228063c2012-02-21 02:23:08 +0000616Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000617 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000618 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Clayton44d93782014-01-27 23:43:24 +0000619 m_input_file_sp (new StreamFile (stdin, false)),
620 m_output_file_sp (new StreamFile (stdout, false)),
621 m_error_file_sp (new StreamFile (stderr, false)),
Jim Inghamc5917d92012-11-30 20:23:19 +0000622 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000623 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000624 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 m_listener ("lldb.Debugger"),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000626 m_source_manager_ap(),
Jim Inghame37d6052011-09-13 00:29:56 +0000627 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000628 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000629 m_input_reader_stack (),
Greg Clayton44d93782014-01-27 23:43:24 +0000630 m_instance_name (),
631 m_loaded_plugins (),
632 m_event_handler_thread (LLDB_INVALID_HOST_THREAD),
Greg Claytonb4874f12014-02-28 18:22:24 +0000633 m_io_handler_thread (LLDB_INVALID_HOST_THREAD)
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{
Greg Clayton44d93782014-01-27 23:43:24 +0000676 ClearIOHandlers();
677 StopIOHandlerThread();
678 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000679 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000680 int num_targets = m_target_list.GetNumTargets();
681 for (int i = 0; i < num_targets; i++)
682 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000683 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
684 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000685 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000686 ProcessSP process_sp (target_sp->GetProcessSP());
687 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000688 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000689 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000690 }
Greg Clayton66111032010-06-23 01:19:29 +0000691 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000692 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000693
694 // Close the input file _before_ we close the input read communications class
695 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000696 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000697 if (m_input_file_sp)
698 m_input_file_sp->GetFile().Close ();
Jim Ingham8314c522011-09-15 21:36:42 +0000699}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700
701bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000702Debugger::GetCloseInputOnEOF () const
703{
Greg Clayton44d93782014-01-27 23:43:24 +0000704// return m_input_comm.GetCloseOnEOF();
705 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000706}
707
708void
709Debugger::SetCloseInputOnEOF (bool b)
710{
Greg Clayton44d93782014-01-27 23:43:24 +0000711// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000712}
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 Clayton44d93782014-01-27 23:43:24 +0000730 if (m_input_file_sp)
731 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
732 else
733 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
734
735 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000736 if (in_file.IsValid() == false)
737 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738
Jim Inghamc5917d92012-11-30 20:23:19 +0000739 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
740 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741}
742
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743void
744Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
745{
Greg Clayton44d93782014-01-27 23:43:24 +0000746 if (m_output_file_sp)
747 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
748 else
749 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
750
751 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000752 if (out_file.IsValid() == false)
753 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000754
Enrico Granatab5887262012-10-29 21:18:03 +0000755 // do not create the ScriptInterpreter just for setting the output file handle
756 // as the constructor will know how to do the right thing on its own
757 const bool can_create = false;
758 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
759 if (script_interpreter)
760 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761}
762
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763void
764Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
765{
Greg Clayton44d93782014-01-27 23:43:24 +0000766 if (m_error_file_sp)
767 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
768 else
769 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
770
771 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000772 if (err_file.IsValid() == false)
773 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774}
775
Jim Inghamc5917d92012-11-30 20:23:19 +0000776void
777Debugger::SaveInputTerminalState ()
778{
Greg Clayton44d93782014-01-27 23:43:24 +0000779 if (m_input_file_sp)
780 {
781 File &in_file = m_input_file_sp->GetFile();
782 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
783 m_terminal_state.Save(in_file.GetDescriptor(), true);
784 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000785}
786
787void
788Debugger::RestoreInputTerminalState ()
789{
790 m_terminal_state.Restore();
791}
792
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000794Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795{
796 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000797 TargetSP target_sp(GetSelectedTarget());
798 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799
800 if (target_sp)
801 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000802 ProcessSP process_sp (target_sp->GetProcessSP());
803 exe_ctx.SetProcessSP (process_sp);
804 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000806 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
807 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000809 exe_ctx.SetThreadSP (thread_sp);
810 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
811 if (exe_ctx.GetFramePtr() == NULL)
812 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 }
814 }
815 }
816 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817}
818
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819void
Caroline Ticeefed6132010-11-19 20:47:54 +0000820Debugger::DispatchInputInterrupt ()
821{
Greg Clayton44d93782014-01-27 23:43:24 +0000822 Mutex::Locker locker (m_input_reader_stack.GetMutex());
823 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000824 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000825 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000826}
827
828void
829Debugger::DispatchInputEndOfFile ()
830{
Greg Clayton44d93782014-01-27 23:43:24 +0000831 Mutex::Locker locker (m_input_reader_stack.GetMutex());
832 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000833 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000834 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000835}
836
837void
Greg Clayton44d93782014-01-27 23:43:24 +0000838Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000839{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000840 // The bottom input reader should be the main debugger input reader. We do not want to close that one here.
Greg Clayton44d93782014-01-27 23:43:24 +0000841 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000842 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000843 {
Greg Clayton44d93782014-01-27 23:43:24 +0000844 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000845 if (reader_sp)
846 {
Greg Clayton44d93782014-01-27 23:43:24 +0000847 m_input_reader_stack.Pop();
848 reader_sp->SetIsDone(true);
Greg Claytone68f5d62014-02-24 22:50:57 +0000849 reader_sp->Cancel();
Caroline Tice3d6086f2010-12-20 18:35:50 +0000850 }
851 }
852}
853
854void
Greg Clayton44d93782014-01-27 23:43:24 +0000855Debugger::ExecuteIOHanders()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000856{
Caroline Tice9088b062011-05-09 23:06:58 +0000857
Greg Clayton44d93782014-01-27 23:43:24 +0000858 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 {
Greg Clayton44d93782014-01-27 23:43:24 +0000860 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 if (!reader_sp)
862 break;
863
Greg Clayton44d93782014-01-27 23:43:24 +0000864 reader_sp->Activate();
865 reader_sp->Run();
866 reader_sp->Deactivate();
867
868 // Remove all input readers that are done from the top of the stack
869 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870 {
Greg Clayton44d93782014-01-27 23:43:24 +0000871 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
872 if (top_reader_sp && top_reader_sp->GetIsDone())
873 m_input_reader_stack.Pop();
874 else
875 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876 }
877 }
Greg Clayton44d93782014-01-27 23:43:24 +0000878 ClearIOHandlers();
879}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880
Greg Clayton44d93782014-01-27 23:43:24 +0000881bool
882Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
883{
884 return m_input_reader_stack.IsTop (reader_sp);
885}
886
887
888ConstString
889Debugger::GetTopIOHandlerControlSequence(char ch)
890{
891 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892}
893
894void
Greg Clayton44d93782014-01-27 23:43:24 +0000895Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
896{
897 Mutex::Locker locker (m_input_reader_stack.GetMutex());
898 PushIOHandler (reader_sp);
899 reader_sp->Activate();
900 reader_sp->Run();
901 PopIOHandler (reader_sp);
902}
903
904void
905Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
906{
907 // Before an IOHandler runs, it must have in/out/err streams.
908 // This function is called when one ore more of the streams
909 // are NULL. We use the top input reader's in/out/err streams,
910 // or fall back to the debugger file handles, or we fall back
911 // onto stdin/stdout/stderr as a last resort.
912
913 Mutex::Locker locker (m_input_reader_stack.GetMutex());
914 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
915 // If no STDIN has been set, then set it appropriately
916 if (!in)
917 {
918 if (top_reader_sp)
919 in = top_reader_sp->GetInputStreamFile();
920 else
921 in = GetInputFile();
922
923 // If there is nothing, use stdin
924 if (!in)
925 in = StreamFileSP(new StreamFile(stdin, false));
926 }
927 // If no STDOUT has been set, then set it appropriately
928 if (!out)
929 {
930 if (top_reader_sp)
931 out = top_reader_sp->GetOutputStreamFile();
932 else
933 out = GetOutputFile();
934
935 // If there is nothing, use stdout
936 if (!out)
937 out = StreamFileSP(new StreamFile(stdout, false));
938 }
939 // If no STDERR has been set, then set it appropriately
940 if (!err)
941 {
942 if (top_reader_sp)
943 err = top_reader_sp->GetErrorStreamFile();
944 else
945 err = GetErrorFile();
946
947 // If there is nothing, use stderr
948 if (!err)
949 err = StreamFileSP(new StreamFile(stdout, false));
950
951 }
952}
953
954void
955Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956{
957 if (!reader_sp)
958 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000959
Greg Clayton44d93782014-01-27 23:43:24 +0000960 // Got the current top input reader...
961 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000962
Greg Claytonb4874f12014-02-28 18:22:24 +0000963 // Don't push the same IO handler twice...
964 if (reader_sp.get() != top_reader_sp.get())
965 {
966 // Push our new input reader
967 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000968
Greg Claytonb4874f12014-02-28 18:22:24 +0000969 // Interrupt the top input reader to it will exit its Run() function
970 // and let this new input reader take over
971 if (top_reader_sp)
972 top_reader_sp->Deactivate();
973 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974}
975
976bool
Greg Clayton44d93782014-01-27 23:43:24 +0000977Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000978{
979 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +0000980
981 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982
983 // The reader on the stop of the stack is done, so let the next
984 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000985 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 {
Greg Clayton44d93782014-01-27 23:43:24 +0000987 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988
989 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
990 {
Greg Clayton44d93782014-01-27 23:43:24 +0000991 reader_sp->Deactivate();
Greg Claytonb4874f12014-02-28 18:22:24 +0000992 reader_sp->Cancel();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000993 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +0000994
995 reader_sp = m_input_reader_stack.Top();
996 if (reader_sp)
997 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998
Greg Clayton44d93782014-01-27 23:43:24 +0000999 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000 }
1001 }
1002 return result;
1003}
1004
1005bool
Greg Clayton44d93782014-01-27 23:43:24 +00001006Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001007{
Greg Clayton44d93782014-01-27 23:43:24 +00001008 Mutex::Locker locker;
1009
1010 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 {
Greg Clayton44d93782014-01-27 23:43:24 +00001012 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1013 if (reader_sp)
1014 reader_sp->Hide();
1015 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016 }
Greg Clayton44d93782014-01-27 23:43:24 +00001017 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018}
1019
1020void
Greg Clayton44d93782014-01-27 23:43:24 +00001021Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022{
Greg Clayton44d93782014-01-27 23:43:24 +00001023 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1024 if (reader_sp)
1025 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026}
Greg Clayton66111032010-06-23 01:19:29 +00001027
Greg Clayton44d93782014-01-27 23:43:24 +00001028
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001029StreamSP
1030Debugger::GetAsyncOutputStream ()
1031{
1032 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1033 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1034}
1035
1036StreamSP
1037Debugger::GetAsyncErrorStream ()
1038{
1039 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1040 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1041}
1042
Greg Claytonc7bece562013-01-25 18:06:21 +00001043size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001044Debugger::GetNumDebuggers()
1045{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001046 if (g_shared_debugger_refcount > 0)
1047 {
1048 Mutex::Locker locker (GetDebuggerListMutex ());
1049 return GetDebuggerList().size();
1050 }
1051 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001052}
1053
1054lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001055Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001056{
1057 DebuggerSP debugger_sp;
1058
Greg Claytonc15f55e2012-03-30 20:53:46 +00001059 if (g_shared_debugger_refcount > 0)
1060 {
1061 Mutex::Locker locker (GetDebuggerListMutex ());
1062 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001063
Greg Claytonc15f55e2012-03-30 20:53:46 +00001064 if (index < debugger_list.size())
1065 debugger_sp = debugger_list[index];
1066 }
1067
Enrico Granata061858c2012-02-15 02:34:21 +00001068 return debugger_sp;
1069}
1070
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001071DebuggerSP
1072Debugger::FindDebuggerWithID (lldb::user_id_t id)
1073{
Greg Clayton4d122c42011-09-17 08:33:22 +00001074 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001075
Greg Claytonc15f55e2012-03-30 20:53:46 +00001076 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001077 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001078 Mutex::Locker locker (GetDebuggerListMutex ());
1079 DebuggerList &debugger_list = GetDebuggerList();
1080 DebuggerList::iterator pos, end = debugger_list.end();
1081 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001082 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001083 if ((*pos).get()->GetID() == id)
1084 {
1085 debugger_sp = *pos;
1086 break;
1087 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001088 }
1089 }
1090 return debugger_sp;
1091}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001092
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001093#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001094static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001095TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001096{
1097 if (frame == NULL)
1098 return;
1099
1100 StreamString s;
1101 const char *prompt_format =
1102 "{addr = '${addr}'\n}"
1103 "{process.id = '${process.id}'\n}"
1104 "{process.name = '${process.name}'\n}"
1105 "{process.file.basename = '${process.file.basename}'\n}"
1106 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1107 "{thread.id = '${thread.id}'\n}"
1108 "{thread.index = '${thread.index}'\n}"
1109 "{thread.name = '${thread.name}'\n}"
1110 "{thread.queue = '${thread.queue}'\n}"
1111 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1112 "{target.arch = '${target.arch}'\n}"
1113 "{module.file.basename = '${module.file.basename}'\n}"
1114 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1115 "{file.basename = '${file.basename}'\n}"
1116 "{file.fullpath = '${file.fullpath}'\n}"
1117 "{frame.index = '${frame.index}'\n}"
1118 "{frame.pc = '${frame.pc}'\n}"
1119 "{frame.sp = '${frame.sp}'\n}"
1120 "{frame.fp = '${frame.fp}'\n}"
1121 "{frame.flags = '${frame.flags}'\n}"
1122 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1123 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1124 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1125 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1126 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1127 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1128 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1129 "{function.id = '${function.id}'\n}"
1130 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001131 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001132 "{function.addr-offset = '${function.addr-offset}'\n}"
1133 "{function.line-offset = '${function.line-offset}'\n}"
1134 "{function.pc-offset = '${function.pc-offset}'\n}"
1135 "{line.file.basename = '${line.file.basename}'\n}"
1136 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1137 "{line.number = '${line.number}'\n}"
1138 "{line.start-addr = '${line.start-addr}'\n}"
1139 "{line.end-addr = '${line.end-addr}'\n}"
1140;
1141
1142 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1143 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001144 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001145 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001146 {
1147 printf("%s\n", s.GetData());
1148 }
1149 else
1150 {
Greg Clayton1b654882010-09-19 02:33:57 +00001151 printf ("what we got: %s\n", s.GetData());
1152 }
1153}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001154#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001155
Enrico Granata9fc19442011-07-06 02:13:41 +00001156static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001157ScanFormatDescriptor (const char* var_name_begin,
1158 const char* var_name_end,
1159 const char** var_name_final,
1160 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001161 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001162 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001163{
Greg Clayton5160ce52013-03-27 23:08:40 +00001164 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001165 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001166 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001167 {
1168 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001169 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001170 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001171 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001172 else
1173 {
1174 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001175 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001176 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001177 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001178 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001179 true,
1180 *custom_format) )
1181 {
Enrico Granatae992a082011-07-22 17:03:19 +00001182 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001183 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001184
1185 switch (format_name.front())
1186 {
1187 case '@': // if this is an @ sign, print ObjC description
1188 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1189 break;
1190 case 'V': // if this is a V, print the value using the default format
1191 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1192 break;
1193 case 'L': // if this is an L, print the location of the value
1194 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1195 break;
1196 case 'S': // if this is an S, print the summary after all
1197 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1198 break;
1199 case '#': // if this is a '#', print the number of children
1200 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1201 break;
1202 case 'T': // if this is a 'T', print the type
1203 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1204 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001205 case 'N': // if this is a 'N', print the name
1206 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1207 break;
1208 case '>': // if this is a '>', print the name
1209 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1210 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001211 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001212 if (log)
1213 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001214 break;
1215 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001216 }
1217 // a good custom format tells us to print the value using it
1218 else
Enrico Granatae992a082011-07-22 17:03:19 +00001219 {
1220 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001221 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001222 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001223 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001224 }
Enrico Granatae992a082011-07-22 17:03:19 +00001225 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001226 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001227 *custom_format,
1228 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001229 return true;
1230}
1231
1232static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001233ScanBracketedRange (const char* var_name_begin,
1234 const char* var_name_end,
1235 const char* var_name_final,
1236 const char** open_bracket_position,
1237 const char** separator_position,
1238 const char** close_bracket_position,
1239 const char** var_name_final_if_array_range,
1240 int64_t* index_lower,
1241 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001242{
Greg Clayton5160ce52013-03-27 23:08:40 +00001243 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001244 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001245 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001246 {
1247 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1248 *close_bracket_position = ::strchr(*open_bracket_position,']');
1249 // as usual, we assume that [] will come before %
1250 //printf("trying to expand a []\n");
1251 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001252 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001253 {
Enrico Granatae992a082011-07-22 17:03:19 +00001254 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001255 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001256 *index_lower = 0;
1257 }
1258 else if (*separator_position == NULL || *separator_position > var_name_end)
1259 {
1260 char *end = NULL;
1261 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1262 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001263 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001264 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001265 }
Greg Clayton34132752011-07-06 04:07:21 +00001266 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001267 {
1268 char *end = NULL;
1269 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1270 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001271 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001272 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001273 }
1274 else
Enrico Granatae992a082011-07-22 17:03:19 +00001275 {
1276 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001277 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001278 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001279 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001280 if (*index_lower > *index_higher && *index_higher > 0)
1281 {
Enrico Granatae992a082011-07-22 17:03:19 +00001282 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001283 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001284 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001285 *index_lower = *index_higher;
1286 *index_higher = temp;
1287 }
1288 }
Enrico Granatae992a082011-07-22 17:03:19 +00001289 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001290 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001291 return true;
1292}
1293
Michael Sartain0769b2b2013-07-30 16:44:36 +00001294template <typename T>
1295static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1296{
1297 if (script_interpreter)
1298 {
1299 Error script_error;
1300 std::string script_output;
1301
1302 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1303 {
1304 s.Printf("%s", script_output.c_str());
1305 return true;
1306 }
1307 else
1308 {
1309 s.Printf("<error: %s>",script_error.AsCString());
1310 }
1311 }
1312 return false;
1313}
1314
Enrico Granata9fc19442011-07-06 02:13:41 +00001315static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001316ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001317 size_t index,
Jason Molendab57e4a12013-11-04 09:33:30 +00001318 StackFrame* frame,
Enrico Granatadc940732011-08-23 00:32:52 +00001319 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001320{
Greg Clayton5160ce52013-03-27 23:08:40 +00001321 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001322 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001323 std::string ptr_deref_buffer(10,0);
1324 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001325 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001326 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001327 const char* first_unparsed;
1328 ValueObject::GetValueForExpressionPathOptions options;
1329 ValueObject::ExpressionPathEndResultType final_value_type;
1330 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001331 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001332 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001333 &first_unparsed,
1334 &reason_to_stop,
1335 &final_value_type,
1336 options,
1337 &what_next);
1338 if (!item)
1339 {
Enrico Granatae992a082011-07-22 17:03:19 +00001340 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001341 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001342 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001343 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001344 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001345 else
1346 {
Enrico Granatae992a082011-07-22 17:03:19 +00001347 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001348 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001349 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001350 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001351 }
1352 return item;
1353}
1354
Michael Sartain0769b2b2013-07-30 16:44:36 +00001355static inline bool
1356IsToken(const char *var_name_begin, const char *var)
1357{
1358 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1359}
1360
1361static bool
1362IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1363 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1364{
1365 int var_len = strlen(var);
1366 if (::strncmp (var_name_begin, var, var_len) == 0)
1367 {
1368 var_name_begin += var_len;
1369 if (*var_name_begin == '}')
1370 {
1371 format = default_format;
1372 return true;
1373 }
1374 else if (*var_name_begin == '%')
1375 {
1376 // Allow format specifiers: x|X|u with optional width specifiers.
1377 // ${thread.id%x} ; hex
1378 // ${thread.id%X} ; uppercase hex
1379 // ${thread.id%u} ; unsigned decimal
1380 // ${thread.id%8.8X} ; width.precision + specifier
1381 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1382 int dot_count = 0;
1383 const char *specifier = NULL;
1384 int width_precision_length = 0;
1385 const char *width_precision = ++var_name_begin;
1386 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1387 {
1388 dot_count += (*var_name_begin == '.');
1389 if (dot_count > 1)
1390 break;
1391 var_name_begin++;
1392 width_precision_length++;
1393 }
1394
1395 if (IsToken (var_name_begin, "tid}"))
1396 {
1397 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1398 if (target)
1399 {
1400 ArchSpec arch (target->GetArchitecture ());
1401 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1402 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1403 specifier = PRIu64;
1404 }
1405 if (!specifier)
1406 {
1407 format = default_format;
1408 return true;
1409 }
1410 }
1411 else if (IsToken (var_name_begin, "x}"))
1412 specifier = PRIx64;
1413 else if (IsToken (var_name_begin, "X}"))
1414 specifier = PRIX64;
1415 else if (IsToken (var_name_begin, "u}"))
1416 specifier = PRIu64;
1417
1418 if (specifier)
1419 {
1420 format = "%";
1421 if (width_precision_length)
1422 format += std::string(width_precision, width_precision_length);
1423 format += specifier;
1424 return true;
1425 }
1426 }
1427 }
1428 return false;
1429}
1430
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001431static bool
1432FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001433(
1434 const char *format,
1435 const SymbolContext *sc,
1436 const ExecutionContext *exe_ctx,
1437 const Address *addr,
1438 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001439 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001440 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001441)
1442{
Enrico Granatac482a192011-08-17 22:13:59 +00001443 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001444 bool success = true;
1445 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001446 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001447
Greg Clayton1b654882010-09-19 02:33:57 +00001448 for (p = format; *p != '\0'; ++p)
1449 {
Enrico Granatac482a192011-08-17 22:13:59 +00001450 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001451 {
Enrico Granatac482a192011-08-17 22:13:59 +00001452 valobj = realvalobj;
1453 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001454 }
Greg Clayton1b654882010-09-19 02:33:57 +00001455 size_t non_special_chars = ::strcspn (p, "${}\\");
1456 if (non_special_chars > 0)
1457 {
1458 if (success)
1459 s.Write (p, non_special_chars);
1460 p += non_special_chars;
1461 }
1462
1463 if (*p == '\0')
1464 {
1465 break;
1466 }
1467 else if (*p == '{')
1468 {
1469 // Start a new scope that must have everything it needs if it is to
1470 // to make it into the final output stream "s". If you want to make
1471 // a format that only prints out the function or symbol name if there
1472 // is one in the symbol context you can use:
1473 // "{function =${function.name}}"
1474 // The first '{' starts a new scope that end with the matching '}' at
1475 // the end of the string. The contents "function =${function.name}"
1476 // will then be evaluated and only be output if there is a function
1477 // or symbol with a valid name.
1478 StreamString sub_strm;
1479
1480 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001481
1482 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001483 {
1484 // The stream had all it needed
1485 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1486 }
1487 if (*p != '}')
1488 {
1489 success = false;
1490 break;
1491 }
1492 }
1493 else if (*p == '}')
1494 {
1495 // End of a enclosing scope
1496 break;
1497 }
1498 else if (*p == '$')
1499 {
1500 // We have a prompt variable to print
1501 ++p;
1502 if (*p == '{')
1503 {
1504 ++p;
1505 const char *var_name_begin = p;
1506 const char *var_name_end = ::strchr (p, '}');
1507
1508 if (var_name_end && var_name_begin < var_name_end)
1509 {
1510 // if we have already failed to parse, skip this variable
1511 if (success)
1512 {
1513 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001514 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001515 Address format_addr;
1516 bool calculate_format_addr_function_offset = false;
1517 // Set reg_kind and reg_num to invalid values
1518 RegisterKind reg_kind = kNumRegisterKinds;
1519 uint32_t reg_num = LLDB_INVALID_REGNUM;
1520 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001521 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001522 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001523 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001524 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1525 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001526
Greg Clayton1b654882010-09-19 02:33:57 +00001527 // Each variable must set success to true below...
1528 bool var_success = false;
1529 switch (var_name_begin[0])
1530 {
Enrico Granata4becb372011-06-29 22:27:15 +00001531 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001532 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001533 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001534 {
Enrico Granatac482a192011-08-17 22:13:59 +00001535 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001536 break;
1537
Enrico Granatac3e320a2011-08-02 17:27:39 +00001538 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001539 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001540
Enrico Granata6f3533f2011-07-29 19:53:35 +00001541 // check for *var and *svar
1542 if (*var_name_begin == '*')
1543 {
1544 do_deref_pointer = true;
1545 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001546 if (log)
1547 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001548 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001549
Enrico Granata6f3533f2011-07-29 19:53:35 +00001550 if (*var_name_begin == 's')
1551 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001552 if (!valobj->IsSynthetic())
1553 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001554 if (!valobj)
1555 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001556 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001557 if (log)
1558 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001559 }
1560
1561 // should be a 'v' by now
1562 if (*var_name_begin != 'v')
1563 break;
1564
Enrico Granatac3e320a2011-08-02 17:27:39 +00001565 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001566 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001567
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001568 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001569 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001570 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001571 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001572 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001573 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001574 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001575 const char* var_name_final = NULL;
1576 const char* var_name_final_if_array_range = NULL;
1577 const char* close_bracket_position = NULL;
1578 int64_t index_lower = -1;
1579 int64_t index_higher = -1;
1580 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001581 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001582 bool was_plain_var = false;
1583 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001584 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001585
Enrico Granatac482a192011-08-17 22:13:59 +00001586 if (!valobj) break;
1587 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001588 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001589 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001590 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001591 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001592 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001593 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001594 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001595 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001596 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001597 // this is a variable with some custom format applied to it
1598 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001599 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001600 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001601 ScanFormatDescriptor (var_name_begin,
1602 var_name_end,
1603 &var_name_final,
1604 &percent_position,
1605 &custom_format,
1606 &val_obj_display);
1607 }
1608 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001609 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001610 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001611 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001612 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001613 const char* percent_position;
1614 ScanFormatDescriptor (var_name_begin,
1615 var_name_end,
1616 &var_name_final,
1617 &percent_position,
1618 &custom_format,
1619 &val_obj_display);
1620
1621 const char* open_bracket_position;
1622 const char* separator_position;
1623 ScanBracketedRange (var_name_begin,
1624 var_name_end,
1625 var_name_final,
1626 &open_bracket_position,
1627 &separator_position,
1628 &close_bracket_position,
1629 &var_name_final_if_array_range,
1630 &index_lower,
1631 &index_higher);
1632
1633 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001634
Enrico Granata599171a2013-02-01 23:59:44 +00001635 std::string expr_path(var_name_final-var_name_begin-1,0);
1636 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1637
1638 if (log)
1639 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1640
1641 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001642 &first_unparsed,
1643 &reason_to_stop,
1644 &final_value_type,
1645 options,
1646 &what_next).get();
1647
1648 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001649 {
Enrico Granatae992a082011-07-22 17:03:19 +00001650 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001651 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001652 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001653 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001654 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001655 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001656 else
1657 {
Enrico Granatae992a082011-07-22 17:03:19 +00001658 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001659 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001660 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001661 first_unparsed, reason_to_stop, final_value_type);
1662 }
Enrico Granata4becb372011-06-29 22:27:15 +00001663 }
Greg Clayton34132752011-07-06 04:07:21 +00001664 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001665 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001666
Enrico Granata86cc9822012-03-19 22:58:49 +00001667 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1668 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001669
Enrico Granata86cc9822012-03-19 22:58:49 +00001670 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001671
Enrico Granataa7187d02011-07-06 19:27:11 +00001672 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001673 {
Greg Clayton34132752011-07-06 04:07:21 +00001674 // I have not deref-ed yet, let's do it
1675 // this happens when we are not going through GetValueForVariableExpressionPath
1676 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001677 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001678 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001679 if (error.Fail())
1680 {
1681 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001682 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001683 break;
1684 }
Greg Clayton34132752011-07-06 04:07:21 +00001685 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001686 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001687
Jim Inghamf164d942014-03-11 18:17:23 +00001688 if (!target)
1689 {
1690 if (log)
1691 log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
1692 break;
1693 }
1694
Enrico Granataa777dc22012-05-08 21:49:57 +00001695 // we do not want to use the summary for a bitfield of type T:n
1696 // if we were originally dealing with just a T - that would get
1697 // us into an endless recursion
1698 if (target->IsBitfield() && was_var_indexed)
1699 {
1700 // TODO: check for a (T:n)-specific summary - we should still obey that
1701 StreamString bitfield_name;
1702 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1703 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1704 if (!DataVisualization::GetSummaryForType(type_sp))
1705 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1706 }
1707
Enrico Granata85933ed2011-08-18 16:38:26 +00001708 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001709 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1710 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1711 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1712 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001713
Enrico Granata86cc9822012-03-19 22:58:49 +00001714 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 +00001715 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001716 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001717 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001718 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001719
Greg Clayton5088c482013-03-25 21:06:13 +00001720 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001721 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001722 // try to use the special cases
1723 var_success = target->DumpPrintableRepresentation(str_temp,
1724 val_obj_display,
1725 custom_format);
1726 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001727 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001728
1729 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001730 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001731 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001732 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001733 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001734 }
1735 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001736 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001737 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001738 {
1739 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1740 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001741 else if (is_pointer) // if pointer, value is the address stored
1742 {
Greg Clayton23f59502012-07-17 03:23:13 +00001743 target->DumpPrintableRepresentation (s,
1744 val_obj_display,
1745 custom_format,
1746 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001747 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001748 var_success = true;
1749 break;
1750 }
1751 }
1752
1753 // if directly trying to print ${var}, and this is an aggregate, display a nice
1754 // type @ location message
1755 if (is_aggregate && was_plain_var)
1756 {
1757 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1758 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001759 break;
1760 }
1761
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001762 // 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 +00001763 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001764 {
1765 s << "<invalid use of aggregate type>";
1766 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001767 break;
1768 }
Greg Clayton34132752011-07-06 04:07:21 +00001769
1770 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001771 {
1772 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001773 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001774 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001775 }
Greg Clayton34132752011-07-06 04:07:21 +00001776 else
Enrico Granatae992a082011-07-22 17:03:19 +00001777 {
1778 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001779 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001780 if (!is_array && !is_pointer)
1781 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001782 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001783 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001784 const char* special_directions = NULL;
1785 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001786 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1787 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001788 ConstString additional_data;
1789 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1790 special_directions_writer.Printf("${%svar%s}",
1791 do_deref_pointer ? "*" : "",
1792 additional_data.GetCString());
1793 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001794 }
1795
1796 // let us display items index_lower thru index_higher of this array
1797 s.PutChar('[');
1798 var_success = true;
1799
1800 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001801 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001802
Greg Claytoncc4d0142012-02-17 07:49:44 +00001803 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001804
Greg Clayton34132752011-07-06 04:07:21 +00001805 for (;index_lower<=index_higher;index_lower++)
1806 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001807 ValueObject* item = ExpandIndexedExpression (target,
1808 index_lower,
1809 exe_ctx->GetFramePtr(),
1810 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001811
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001812 if (!item)
1813 {
Enrico Granatae992a082011-07-22 17:03:19 +00001814 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001815 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001816 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001817 else
1818 {
Enrico Granatae992a082011-07-22 17:03:19 +00001819 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001820 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001821 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001822
Greg Clayton34132752011-07-06 04:07:21 +00001823 if (!special_directions)
1824 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1825 else
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001826 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
Greg Clayton34132752011-07-06 04:07:21 +00001827
Enrico Granata22c55d12011-08-12 02:00:06 +00001828 if (--max_num_children == 0)
1829 {
1830 s.PutCString(", ...");
1831 break;
1832 }
1833
Greg Clayton34132752011-07-06 04:07:21 +00001834 if (index_lower < index_higher)
1835 s.PutChar(',');
1836 }
1837 s.PutChar(']');
1838 }
Enrico Granata4becb372011-06-29 22:27:15 +00001839 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001840 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001841 case 'a':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001842 if (IsToken (var_name_begin, "addr}"))
Greg Clayton1b654882010-09-19 02:33:57 +00001843 {
1844 if (addr && addr->IsValid())
1845 {
1846 var_success = true;
1847 format_addr = *addr;
1848 }
1849 }
1850 break;
1851
1852 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001853 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00001854 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001855 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001856 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001857 Process *process = exe_ctx->GetProcessPtr();
1858 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001859 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001860 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001861 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001862 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001863 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001864 var_success = true;
1865 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001866 else if ((IsToken (var_name_begin, "name}")) ||
1867 (IsToken (var_name_begin, "file.basename}")) ||
1868 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00001869 {
1870 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1871 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001872 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001873 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1874 {
1875 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001876 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001877 }
1878 else
1879 {
1880 format_file_spec = exe_module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001881 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001882 }
Greg Clayton1b654882010-09-19 02:33:57 +00001883 }
1884 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001885 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001886 {
1887 var_name_begin += ::strlen("script:");
1888 std::string script_name(var_name_begin,var_name_end);
1889 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001890 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
1891 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001892 }
Greg Clayton1b654882010-09-19 02:33:57 +00001893 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001894 }
Greg Clayton1b654882010-09-19 02:33:57 +00001895 }
1896 break;
1897
1898 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001899 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00001900 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001901 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001902 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001903 Thread *thread = exe_ctx->GetThreadPtr();
1904 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001905 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001906 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001907 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001908 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001909 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001910 var_success = true;
1911 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001912 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00001913 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001914 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00001915 var_success = true;
1916 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001917 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00001918 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001919 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001920 var_success = true;
1921 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001922 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001923 {
1924 cstr = thread->GetName();
1925 var_success = cstr && cstr[0];
1926 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001927 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001928 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001929 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001930 {
1931 cstr = thread->GetQueueName();
1932 var_success = cstr && cstr[0];
1933 if (var_success)
1934 s.PutCString(cstr);
1935 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001936 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001937 {
1938 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001939 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001940 {
1941 cstr = stop_info_sp->GetDescription();
1942 if (cstr && cstr[0])
1943 {
1944 s.PutCString(cstr);
1945 var_success = true;
1946 }
Greg Clayton1b654882010-09-19 02:33:57 +00001947 }
1948 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001949 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00001950 {
1951 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001952 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001953 {
1954 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1955 if (return_valobj_sp)
1956 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00001957 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00001958 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001959 }
1960 }
1961 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001962 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001963 {
1964 var_name_begin += ::strlen("script:");
1965 std::string script_name(var_name_begin,var_name_end);
1966 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001967 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
1968 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001969 }
Greg Clayton1b654882010-09-19 02:33:57 +00001970 }
1971 }
1972 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001973 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00001974 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001975 // TODO: hookup properties
1976// if (!target_properties_sp)
1977// {
1978// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1979// if (target)
1980// target_properties_sp = target->GetProperties();
1981// }
1982//
1983// if (target_properties_sp)
1984// {
1985// var_name_begin += ::strlen ("target.");
1986// const char *end_property = strchr(var_name_begin, '}');
1987// if (end_property)
1988// {
1989// ConstString property_name(var_name_begin, end_property - var_name_begin);
1990// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
1991// if (!property_value.empty())
1992// {
1993// s.PutCString (property_value.c_str());
1994// var_success = true;
1995// }
1996// }
1997// }
Greg Clayton0603aa92010-10-04 01:05:56 +00001998 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1999 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002000 {
Greg Clayton1b654882010-09-19 02:33:57 +00002001 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002002 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002003 {
2004 ArchSpec arch (target->GetArchitecture ());
2005 if (arch.IsValid())
2006 {
Greg Clayton64195a22011-02-23 00:35:02 +00002007 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002008 var_success = true;
2009 }
2010 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002011 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002012 {
2013 var_name_begin += ::strlen("script:");
2014 std::string script_name(var_name_begin,var_name_end);
2015 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002016 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2017 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002018 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002019 }
Greg Clayton1b654882010-09-19 02:33:57 +00002020 }
2021 break;
2022
2023
2024 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002025 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002026 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002027 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002028 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002029 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002030 var_name_begin += ::strlen ("module.");
2031
Michael Sartain0769b2b2013-07-30 16:44:36 +00002032 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002033 {
2034 if (module->GetFileSpec())
2035 {
2036 var_name_begin += ::strlen ("file.");
2037
Michael Sartain0769b2b2013-07-30 16:44:36 +00002038 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002039 {
2040 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002041 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002042 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002043 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002044 {
2045 format_file_spec = module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002046 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002047 }
2048 }
2049 }
2050 }
2051 }
2052 break;
2053
2054
2055 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002056 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002057 {
2058 if (sc && sc->comp_unit != NULL)
2059 {
2060 var_name_begin += ::strlen ("file.");
2061
Michael Sartain0769b2b2013-07-30 16:44:36 +00002062 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002063 {
2064 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002065 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002066 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002067 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002068 {
2069 format_file_spec = *sc->comp_unit;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002070 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002071 }
2072 }
2073 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002074 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002075 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002076 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002077 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002078 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002079 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002080 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002081 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002082 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002083 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002084 s.Printf("%u", frame->GetFrameIndex());
2085 var_success = true;
2086 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002087 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002088 {
2089 reg_kind = eRegisterKindGeneric;
2090 reg_num = LLDB_REGNUM_GENERIC_PC;
2091 var_success = true;
2092 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002093 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002094 {
2095 reg_kind = eRegisterKindGeneric;
2096 reg_num = LLDB_REGNUM_GENERIC_SP;
2097 var_success = true;
2098 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002099 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002100 {
2101 reg_kind = eRegisterKindGeneric;
2102 reg_num = LLDB_REGNUM_GENERIC_FP;
2103 var_success = true;
2104 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002105 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002106 {
2107 reg_kind = eRegisterKindGeneric;
2108 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2109 var_success = true;
2110 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002111 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002112 {
2113 reg_ctx = frame->GetRegisterContext().get();
2114 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002115 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002116 var_name_begin += ::strlen ("reg.");
2117 if (var_name_begin < var_name_end)
2118 {
2119 std::string reg_name (var_name_begin, var_name_end);
2120 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2121 if (reg_info)
2122 var_success = true;
2123 }
Greg Clayton1b654882010-09-19 02:33:57 +00002124 }
2125 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002126 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002127 {
2128 var_name_begin += ::strlen("script:");
2129 std::string script_name(var_name_begin,var_name_end);
2130 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002131 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2132 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002133 }
Greg Clayton1b654882010-09-19 02:33:57 +00002134 }
2135 }
2136 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002137 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002138 {
2139 if (sc && (sc->function != NULL || sc->symbol != NULL))
2140 {
2141 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002142 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002143 {
2144 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002145 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002146 else
2147 s.Printf("symbol[%u]", sc->symbol->GetID());
2148
2149 var_success = true;
2150 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002151 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002152 {
2153 if (sc->function)
2154 cstr = sc->function->GetName().AsCString (NULL);
2155 else if (sc->symbol)
2156 cstr = sc->symbol->GetName().AsCString (NULL);
2157 if (cstr)
2158 {
2159 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002160
2161 if (sc->block)
2162 {
2163 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2164 if (inline_block)
2165 {
2166 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2167 if (inline_info)
2168 {
2169 s.PutCString(" [inlined] ");
2170 inline_info->GetName().Dump(&s);
2171 }
2172 }
2173 }
Greg Clayton1b654882010-09-19 02:33:57 +00002174 var_success = true;
2175 }
2176 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002177 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002178 {
2179 // Print the function name with arguments in it
2180
2181 if (sc->function)
2182 {
2183 var_success = true;
2184 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2185 cstr = sc->function->GetName().AsCString (NULL);
2186 if (cstr)
2187 {
2188 const InlineFunctionInfo *inline_info = NULL;
2189 VariableListSP variable_list_sp;
2190 bool get_function_vars = true;
2191 if (sc->block)
2192 {
2193 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2194
2195 if (inline_block)
2196 {
2197 get_function_vars = false;
2198 inline_info = sc->block->GetInlinedFunctionInfo();
2199 if (inline_info)
2200 variable_list_sp = inline_block->GetBlockVariableList (true);
2201 }
2202 }
2203
2204 if (get_function_vars)
2205 {
2206 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2207 }
2208
2209 if (inline_info)
2210 {
2211 s.PutCString (cstr);
2212 s.PutCString (" [inlined] ");
2213 cstr = inline_info->GetName().GetCString();
2214 }
2215
2216 VariableList args;
2217 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002218 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002219 if (args.GetSize() > 0)
2220 {
2221 const char *open_paren = strchr (cstr, '(');
2222 const char *close_paren = NULL;
2223 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002224 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002225 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002226 {
2227 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2228 if (open_paren)
2229 close_paren = strchr (open_paren, ')');
2230 }
2231 else
2232 close_paren = strchr (open_paren, ')');
2233 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002234
2235 if (open_paren)
2236 s.Write(cstr, open_paren - cstr + 1);
2237 else
2238 {
2239 s.PutCString (cstr);
2240 s.PutChar ('(');
2241 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002242 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002243 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2244 {
Enrico Granata894f7352014-03-25 22:03:52 +00002245 std::string buffer;
2246
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002247 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2248 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
Enrico Granata894f7352014-03-25 22:03:52 +00002249 const char *var_representation = nullptr;
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002250 const char *var_name = var_value_sp->GetName().GetCString();
Enrico Granata894f7352014-03-25 22:03:52 +00002251 if (var_value_sp->GetClangType().IsAggregateType() &&
2252 DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
2253 {
2254 static StringSummaryFormat format(TypeSummaryImpl::Flags()
2255 .SetHideItemNames(false)
2256 .SetShowMembersOneLiner(true),
2257 "");
2258 format.FormatObject(var_value_sp.get(), buffer);
2259 var_representation = buffer.c_str();
2260 }
2261 else
2262 var_representation = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002263 if (arg_idx > 0)
2264 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002265 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002266 {
Enrico Granata894f7352014-03-25 22:03:52 +00002267 if (var_representation)
2268 s.Printf ("%s=%s", var_name, var_representation);
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002269 else
2270 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2271 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002272 else
2273 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002274 }
2275
2276 if (close_paren)
2277 s.PutCString (close_paren);
2278 else
2279 s.PutChar(')');
2280
2281 }
2282 else
2283 {
2284 s.PutCString(cstr);
2285 }
2286 }
2287 }
2288 else if (sc->symbol)
2289 {
2290 cstr = sc->symbol->GetName().AsCString (NULL);
2291 if (cstr)
2292 {
2293 s.PutCString(cstr);
2294 var_success = true;
2295 }
2296 }
2297 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002298 else if (IsToken (var_name_begin, "addr-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002299 {
2300 var_success = addr != NULL;
2301 if (var_success)
2302 {
2303 format_addr = *addr;
2304 calculate_format_addr_function_offset = true;
2305 }
2306 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002307 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002308 {
2309 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2310 if (var_success)
2311 {
2312 format_addr = sc->line_entry.range.GetBaseAddress();
2313 calculate_format_addr_function_offset = true;
2314 }
2315 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002316 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002317 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002318 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002319 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002320 if (var_success)
2321 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002322 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002323 calculate_format_addr_function_offset = true;
2324 }
2325 }
2326 }
2327 }
2328 break;
2329
2330 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002331 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002332 {
2333 if (sc && sc->line_entry.IsValid())
2334 {
2335 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002336 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002337 {
2338 var_name_begin += ::strlen ("file.");
2339
Michael Sartain0769b2b2013-07-30 16:44:36 +00002340 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002341 {
2342 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002343 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002344 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002345 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002346 {
2347 format_file_spec = sc->line_entry.file;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002348 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002349 }
2350 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002351 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002352 {
2353 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002354 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002355 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002356 else if ((IsToken (var_name_begin, "start-addr}")) ||
2357 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002358 {
2359 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2360 if (var_success)
2361 {
2362 format_addr = sc->line_entry.range.GetBaseAddress();
2363 if (var_name_begin[0] == 'e')
2364 format_addr.Slide (sc->line_entry.range.GetByteSize());
2365 }
2366 }
2367 }
2368 }
2369 break;
2370 }
2371
2372 if (var_success)
2373 {
2374 // If format addr is valid, then we need to print an address
2375 if (reg_num != LLDB_INVALID_REGNUM)
2376 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002377 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002378 // We have a register value to display...
2379 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2380 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002381 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002382 }
2383 else
2384 {
2385 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002386 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002387
2388 if (reg_ctx)
2389 {
2390 if (reg_kind != kNumRegisterKinds)
2391 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2392 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2393 var_success = reg_info != NULL;
2394 }
2395 }
2396 }
2397
2398 if (reg_info != NULL)
2399 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002400 RegisterValue reg_value;
2401 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2402 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002403 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002404 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002405 }
2406 }
2407
2408 if (format_file_spec)
2409 {
2410 s << format_file_spec;
2411 }
2412
2413 // If format addr is valid, then we need to print an address
2414 if (format_addr.IsValid())
2415 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002416 var_success = false;
2417
Greg Clayton1b654882010-09-19 02:33:57 +00002418 if (calculate_format_addr_function_offset)
2419 {
2420 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002421
Greg Clayton0603aa92010-10-04 01:05:56 +00002422 if (sc)
2423 {
2424 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002425 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002426 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002427 if (sc->block)
2428 {
2429 // Check to make sure we aren't in an inline
2430 // function. If we are, use the inline block
2431 // range that contains "format_addr" since
2432 // blocks can be discontiguous.
2433 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2434 AddressRange inline_range;
2435 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2436 func_addr = inline_range.GetBaseAddress();
2437 }
2438 }
Greg Claytone7612132012-03-07 21:03:09 +00002439 else if (sc->symbol && sc->symbol->ValueIsAddress())
2440 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002441 }
2442
2443 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002444 {
2445 if (func_addr.GetSection() == format_addr.GetSection())
2446 {
2447 addr_t func_file_addr = func_addr.GetFileAddress();
2448 addr_t addr_file_addr = format_addr.GetFileAddress();
2449 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002450 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002451 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002452 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002453 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002454 }
2455 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002456 {
2457 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2458 if (target)
2459 {
2460 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2461 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2462 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002463 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002464 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002465 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002466 var_success = true;
2467 }
2468 }
Greg Clayton1b654882010-09-19 02:33:57 +00002469 }
2470 }
2471 else
2472 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002473 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002474 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002475 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2476 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002477 if (vaddr == LLDB_INVALID_ADDRESS)
2478 vaddr = format_addr.GetFileAddress ();
2479
2480 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002481 {
Greg Clayton514487e2011-02-15 21:59:32 +00002482 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002483 if (addr_width == 0)
2484 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002485 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002486 var_success = true;
2487 }
Greg Clayton1b654882010-09-19 02:33:57 +00002488 }
2489 }
2490 }
2491
2492 if (var_success == false)
2493 success = false;
2494 }
2495 p = var_name_end;
2496 }
2497 else
2498 break;
2499 }
2500 else
2501 {
2502 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2503 s.PutChar(*p);
2504 }
2505 }
2506 else if (*p == '\\')
2507 {
2508 ++p; // skip the slash
2509 switch (*p)
2510 {
2511 case 'a': s.PutChar ('\a'); break;
2512 case 'b': s.PutChar ('\b'); break;
2513 case 'f': s.PutChar ('\f'); break;
2514 case 'n': s.PutChar ('\n'); break;
2515 case 'r': s.PutChar ('\r'); break;
2516 case 't': s.PutChar ('\t'); break;
2517 case 'v': s.PutChar ('\v'); break;
2518 case '\'': s.PutChar ('\''); break;
2519 case '\\': s.PutChar ('\\'); break;
2520 case '0':
2521 // 1 to 3 octal chars
2522 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002523 // Make a string that can hold onto the initial zero char,
2524 // up to 3 octal digits, and a terminating NULL.
2525 char oct_str[5] = { 0, 0, 0, 0, 0 };
2526
2527 int i;
2528 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2529 oct_str[i] = p[i];
2530
2531 // We don't want to consume the last octal character since
2532 // the main for loop will do this for us, so we advance p by
2533 // one less than i (even if i is zero)
2534 p += i - 1;
2535 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2536 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002537 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002538 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002539 }
Greg Clayton1b654882010-09-19 02:33:57 +00002540 }
2541 break;
2542
2543 case 'x':
2544 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002545 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002546 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002547 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002548
Greg Clayton0603aa92010-10-04 01:05:56 +00002549 // Make a string that can hold onto two hex chars plus a
2550 // NULL terminator
2551 char hex_str[3] = { 0,0,0 };
2552 hex_str[0] = *p;
2553 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002554 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002555 ++p; // Skip the first of the two hex chars
2556 hex_str[1] = *p;
2557 }
2558
2559 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2560 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002561 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002562 }
2563 else
2564 {
2565 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002566 }
2567 break;
2568
2569 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002570 // Just desensitize any other character by just printing what
2571 // came after the '\'
2572 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002573 break;
2574
2575 }
2576
2577 }
2578 }
2579 if (end)
2580 *end = p;
2581 return success;
2582}
2583
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002584bool
2585Debugger::FormatPrompt
2586(
2587 const char *format,
2588 const SymbolContext *sc,
2589 const ExecutionContext *exe_ctx,
2590 const Address *addr,
2591 Stream &s,
2592 ValueObject* valobj
2593)
2594{
2595 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2596 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2597 if (format_str.length())
2598 format = format_str.c_str();
2599 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
2600}
2601
Jim Ingham228063c2012-02-21 02:23:08 +00002602void
2603Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2604{
Jim Ingham4f02b222012-02-22 22:49:20 +00002605 // For simplicity's sake, I am not going to deal with how to close down any
2606 // open logging streams, I just redirect everything from here on out to the
2607 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002608 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2609}
2610
2611bool
2612Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2613{
2614 Log::Callbacks log_callbacks;
2615
2616 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002617 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002618 {
2619 log_stream_sp = m_log_callback_stream_sp;
2620 // For now when using the callback mode you always get thread & timestamp.
2621 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2622 }
2623 else if (log_file == NULL || *log_file == '\0')
2624 {
Greg Clayton44d93782014-01-27 23:43:24 +00002625 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00002626 }
2627 else
2628 {
2629 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002630 if (pos != m_log_streams.end())
2631 log_stream_sp = pos->second.lock();
2632 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002633 {
2634 log_stream_sp.reset (new StreamFile (log_file));
2635 m_log_streams[log_file] = log_stream_sp;
2636 }
Jim Ingham228063c2012-02-21 02:23:08 +00002637 }
2638 assert (log_stream_sp.get());
2639
2640 if (log_options == 0)
2641 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2642
Greg Clayton57abc5d2013-05-10 21:47:16 +00002643 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002644 {
2645 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2646 return true;
2647 }
2648 else
2649 {
2650 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2651 if (log_channel_sp)
2652 {
2653 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2654 {
2655 return true;
2656 }
2657 else
2658 {
2659 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2660 return false;
2661 }
2662 }
2663 else
2664 {
2665 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2666 return false;
2667 }
2668 }
2669 return false;
2670}
2671
Greg Clayton9585fbf2013-03-19 00:20:55 +00002672SourceManager &
2673Debugger::GetSourceManager ()
2674{
2675 if (m_source_manager_ap.get() == NULL)
2676 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2677 return *m_source_manager_ap;
2678}
2679
2680
Greg Clayton44d93782014-01-27 23:43:24 +00002681
2682// This function handles events that were broadcast by the process.
2683void
2684Debugger::HandleBreakpointEvent (const EventSP &event_sp)
2685{
2686 using namespace lldb;
2687 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
2688
2689// if (event_type & eBreakpointEventTypeAdded
2690// || event_type & eBreakpointEventTypeRemoved
2691// || event_type & eBreakpointEventTypeEnabled
2692// || event_type & eBreakpointEventTypeDisabled
2693// || event_type & eBreakpointEventTypeCommandChanged
2694// || event_type & eBreakpointEventTypeConditionChanged
2695// || event_type & eBreakpointEventTypeIgnoreChanged
2696// || event_type & eBreakpointEventTypeLocationsResolved)
2697// {
2698// // Don't do anything about these events, since the breakpoint commands already echo these actions.
2699// }
2700//
2701 if (event_type & eBreakpointEventTypeLocationsAdded)
2702 {
2703 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
2704 if (num_new_locations > 0)
2705 {
2706 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
2707 StreamFileSP output_sp (GetOutputFile());
2708 if (output_sp)
2709 {
2710 output_sp->Printf("%d location%s added to breakpoint %d\n",
2711 num_new_locations,
2712 num_new_locations == 1 ? "" : "s",
2713 breakpoint->GetID());
2714 RefreshTopIOHandler();
2715 }
2716 }
2717 }
2718// else if (event_type & eBreakpointEventTypeLocationsRemoved)
2719// {
2720// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
2721// }
2722// else if (event_type & eBreakpointEventTypeLocationsResolved)
2723// {
2724// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
2725// }
2726}
2727
2728size_t
2729Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
2730{
2731 size_t total_bytes = 0;
2732 if (stream == NULL)
2733 stream = GetOutputFile().get();
2734
2735 if (stream)
2736 {
2737 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
2738 if (process == NULL)
2739 {
2740 TargetSP target_sp = GetTargetList().GetSelectedTarget();
2741 if (target_sp)
2742 process = target_sp->GetProcessSP().get();
2743 }
2744 if (process)
2745 {
2746 Error error;
2747 size_t len;
2748 char stdio_buffer[1024];
2749 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2750 {
2751 stream->Write(stdio_buffer, len);
2752 total_bytes += len;
2753 }
2754 }
2755 stream->Flush();
2756 }
2757 return total_bytes;
2758}
2759
2760size_t
2761Debugger::GetProcessSTDERR (Process *process, Stream *stream)
2762{
2763 size_t total_bytes = 0;
2764 if (stream == NULL)
2765 stream = GetOutputFile().get();
2766
2767 if (stream)
2768 {
2769 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2770 if (process == NULL)
2771 {
2772 TargetSP target_sp = GetTargetList().GetSelectedTarget();
2773 if (target_sp)
2774 process = target_sp->GetProcessSP().get();
2775 }
2776 if (process)
2777 {
2778 Error error;
2779 size_t len;
2780 char stdio_buffer[1024];
2781 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2782 {
2783 stream->Write(stdio_buffer, len);
2784 total_bytes += len;
2785 }
2786 }
2787 stream->Flush();
2788 }
2789 return total_bytes;
2790}
2791
2792// This function handles events that were broadcast by the process.
2793void
2794Debugger::HandleProcessEvent (const EventSP &event_sp)
2795{
2796 using namespace lldb;
2797 const uint32_t event_type = event_sp->GetType();
2798 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
2799
Greg Claytonb4874f12014-02-28 18:22:24 +00002800 StreamString output_stream;
2801 StreamString error_stream;
Greg Clayton44d93782014-01-27 23:43:24 +00002802 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00002803
Greg Claytonb4874f12014-02-28 18:22:24 +00002804 if (!gui_enabled)
2805 {
2806 bool pop_process_io_handler = false;
2807 assert (process_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00002808
Greg Claytonb4874f12014-02-28 18:22:24 +00002809 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
Greg Clayton44d93782014-01-27 23:43:24 +00002810 {
Greg Claytonb4874f12014-02-28 18:22:24 +00002811 GetProcessSTDOUT (process_sp.get(), &output_stream);
2812 }
2813
2814 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
2815 {
2816 GetProcessSTDERR (process_sp.get(), &error_stream);
2817 }
2818
2819 if (event_type & Process::eBroadcastBitStateChanged)
2820 {
2821
2822 // Drain all stout and stderr so we don't see any output come after
2823 // we print our prompts
Greg Clayton44d93782014-01-27 23:43:24 +00002824 // Something changed in the process; get the event and report the process's current status and location to
2825 // the user.
2826 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
2827 if (event_state == eStateInvalid)
2828 return;
2829
2830 switch (event_state)
2831 {
2832 case eStateInvalid:
2833 case eStateUnloaded:
2834 case eStateConnected:
2835 case eStateAttaching:
2836 case eStateLaunching:
2837 case eStateStepping:
2838 case eStateDetached:
2839 {
Greg Claytonb4874f12014-02-28 18:22:24 +00002840 output_stream.Printf("Process %" PRIu64 " %s\n",
2841 process_sp->GetID(),
2842 StateAsCString (event_state));
2843
2844 if (event_state == eStateDetached)
2845 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00002846 }
2847 break;
2848
2849 case eStateRunning:
2850 // Don't be chatty when we run...
2851 break;
2852
2853 case eStateExited:
Greg Claytonb4874f12014-02-28 18:22:24 +00002854 process_sp->GetStatus(output_stream);
2855 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00002856 break;
2857
2858 case eStateStopped:
2859 case eStateCrashed:
2860 case eStateSuspended:
2861 // Make sure the program hasn't been auto-restarted:
2862 if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get()))
2863 {
2864 size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get());
2865 if (num_reasons > 0)
2866 {
2867 // FIXME: Do we want to report this, or would that just be annoyingly chatty?
2868 if (num_reasons == 1)
2869 {
2870 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0);
Greg Claytonb4874f12014-02-28 18:22:24 +00002871 output_stream.Printf("Process %" PRIu64 " stopped and restarted: %s\n",
2872 process_sp->GetID(),
2873 reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00002874 }
2875 else
2876 {
Greg Claytonb4874f12014-02-28 18:22:24 +00002877 output_stream.Printf("Process %" PRIu64 " stopped and restarted, reasons:\n",
2878 process_sp->GetID());
Greg Clayton44d93782014-01-27 23:43:24 +00002879
2880
2881 for (size_t i = 0; i < num_reasons; i++)
2882 {
2883 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i);
Greg Claytonb4874f12014-02-28 18:22:24 +00002884 output_stream.Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00002885 }
2886 }
2887 }
2888 }
2889 else
2890 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002891 // Lock the thread list so it doesn't change on us, this is the scope for the locker:
Greg Clayton44d93782014-01-27 23:43:24 +00002892 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002893 ThreadList &thread_list = process_sp->GetThreadList();
2894 Mutex::Locker locker (thread_list.GetMutex());
2895
2896 ThreadSP curr_thread (thread_list.GetSelectedThread());
2897 ThreadSP thread;
2898 StopReason curr_thread_stop_reason = eStopReasonInvalid;
2899 if (curr_thread)
2900 curr_thread_stop_reason = curr_thread->GetStopReason();
2901 if (!curr_thread ||
2902 !curr_thread->IsValid() ||
2903 curr_thread_stop_reason == eStopReasonInvalid ||
2904 curr_thread_stop_reason == eStopReasonNone)
Greg Clayton44d93782014-01-27 23:43:24 +00002905 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002906 // Prefer a thread that has just completed its plan over another thread as current thread.
2907 ThreadSP plan_thread;
2908 ThreadSP other_thread;
2909 const size_t num_threads = thread_list.GetSize();
2910 size_t i;
2911 for (i = 0; i < num_threads; ++i)
Greg Clayton44d93782014-01-27 23:43:24 +00002912 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002913 thread = thread_list.GetThreadAtIndex(i);
2914 StopReason thread_stop_reason = thread->GetStopReason();
2915 switch (thread_stop_reason)
2916 {
2917 case eStopReasonInvalid:
2918 case eStopReasonNone:
2919 break;
2920
2921 case eStopReasonTrace:
2922 case eStopReasonBreakpoint:
2923 case eStopReasonWatchpoint:
2924 case eStopReasonSignal:
2925 case eStopReasonException:
2926 case eStopReasonExec:
2927 case eStopReasonThreadExiting:
2928 if (!other_thread)
2929 other_thread = thread;
2930 break;
2931 case eStopReasonPlanComplete:
2932 if (!plan_thread)
2933 plan_thread = thread;
2934 break;
2935 }
2936 }
2937 if (plan_thread)
2938 thread_list.SetSelectedThreadByID (plan_thread->GetID());
2939 else if (other_thread)
2940 thread_list.SetSelectedThreadByID (other_thread->GetID());
2941 else
2942 {
2943 if (curr_thread && curr_thread->IsValid())
2944 thread = curr_thread;
2945 else
2946 thread = thread_list.GetThreadAtIndex(0);
2947
2948 if (thread)
2949 thread_list.SetSelectedThreadByID (thread->GetID());
Greg Clayton44d93782014-01-27 23:43:24 +00002950 }
2951 }
Greg Clayton44d93782014-01-27 23:43:24 +00002952 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00002953 // Drop the ThreadList mutex by here, since GetThreadStatus below might have to run code,
2954 // e.g. for Data formatters, and if we hold the ThreadList mutex, then the process is going to
2955 // have a hard time restarting the process.
Greg Clayton44d93782014-01-27 23:43:24 +00002956
2957 if (GetTargetList().GetSelectedTarget().get() == &process_sp->GetTarget())
2958 {
2959 const bool only_threads_with_stop_reason = true;
2960 const uint32_t start_frame = 0;
2961 const uint32_t num_frames = 1;
2962 const uint32_t num_frames_with_source = 1;
Greg Claytonb4874f12014-02-28 18:22:24 +00002963 process_sp->GetStatus(output_stream);
2964 process_sp->GetThreadStatus (output_stream,
Greg Clayton44d93782014-01-27 23:43:24 +00002965 only_threads_with_stop_reason,
2966 start_frame,
2967 num_frames,
2968 num_frames_with_source);
2969 }
2970 else
2971 {
2972 uint32_t target_idx = GetTargetList().GetIndexOfTarget(process_sp->GetTarget().shared_from_this());
2973 if (target_idx != UINT32_MAX)
Greg Claytonb4874f12014-02-28 18:22:24 +00002974 output_stream.Printf ("Target %d: (", target_idx);
Greg Clayton44d93782014-01-27 23:43:24 +00002975 else
Greg Claytonb4874f12014-02-28 18:22:24 +00002976 output_stream.Printf ("Target <unknown index>: (");
2977 process_sp->GetTarget().Dump (&output_stream, eDescriptionLevelBrief);
2978 output_stream.Printf (") stopped.\n");
Greg Clayton44d93782014-01-27 23:43:24 +00002979 }
Greg Claytonb4874f12014-02-28 18:22:24 +00002980
2981 // Pop the process IO handler
2982 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00002983 }
2984 break;
2985 }
2986 }
Greg Clayton44d93782014-01-27 23:43:24 +00002987
Greg Claytonb4874f12014-02-28 18:22:24 +00002988 if (output_stream.GetSize() || error_stream.GetSize())
2989 {
2990 StreamFileSP error_stream_sp (GetOutputFile());
Greg Clayton6fea17e2014-03-03 19:15:20 +00002991 bool top_io_handler_hid = false;
2992
2993 if (process_sp->ProcessIOHandlerIsActive() == false)
2994 top_io_handler_hid = HideTopIOHandler();
Greg Claytonb4874f12014-02-28 18:22:24 +00002995
2996 if (output_stream.GetSize())
2997 {
2998 StreamFileSP output_stream_sp (GetOutputFile());
2999 if (output_stream_sp)
3000 output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
3001 }
3002
3003 if (error_stream.GetSize())
3004 {
3005 StreamFileSP error_stream_sp (GetErrorFile());
3006 if (error_stream_sp)
3007 error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
3008 }
3009
3010 if (top_io_handler_hid)
3011 RefreshTopIOHandler();
3012 }
3013
3014 if (pop_process_io_handler)
3015 process_sp->PopProcessIOHandler();
3016 }
Greg Clayton44d93782014-01-27 23:43:24 +00003017}
3018
3019void
3020Debugger::HandleThreadEvent (const EventSP &event_sp)
3021{
3022 // At present the only thread event we handle is the Frame Changed event,
3023 // and all we do for that is just reprint the thread status for that thread.
3024 using namespace lldb;
3025 const uint32_t event_type = event_sp->GetType();
3026 if (event_type == Thread::eBroadcastBitStackChanged ||
3027 event_type == Thread::eBroadcastBitThreadSelected )
3028 {
3029 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
3030 if (thread_sp)
3031 {
3032 HideTopIOHandler();
3033 StreamFileSP stream_sp (GetOutputFile());
3034 thread_sp->GetStatus(*stream_sp, 0, 1, 1);
3035 RefreshTopIOHandler();
3036 }
3037 }
3038}
3039
3040bool
3041Debugger::IsForwardingEvents ()
3042{
3043 return (bool)m_forward_listener_sp;
3044}
3045
3046void
3047Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
3048{
3049 m_forward_listener_sp = listener_sp;
3050}
3051
3052void
3053Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
3054{
3055 m_forward_listener_sp.reset();
3056}
3057
3058
3059void
3060Debugger::DefaultEventHandler()
3061{
3062 Listener& listener(GetListener());
3063 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
3064 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
3065 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
3066 BroadcastEventSpec target_event_spec (broadcaster_class_target,
3067 Target::eBroadcastBitBreakpointChanged);
3068
3069 BroadcastEventSpec process_event_spec (broadcaster_class_process,
3070 Process::eBroadcastBitStateChanged |
3071 Process::eBroadcastBitSTDOUT |
3072 Process::eBroadcastBitSTDERR);
3073
3074 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
3075 Thread::eBroadcastBitStackChanged |
3076 Thread::eBroadcastBitThreadSelected );
3077
3078 listener.StartListeningForEventSpec (*this, target_event_spec);
3079 listener.StartListeningForEventSpec (*this, process_event_spec);
3080 listener.StartListeningForEventSpec (*this, thread_event_spec);
3081 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
3082 CommandInterpreter::eBroadcastBitQuitCommandReceived |
3083 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
3084 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
3085
3086 bool done = false;
3087 while (!done)
3088 {
3089// Mutex::Locker locker;
3090// if (locker.TryLock(m_input_reader_stack.GetMutex()))
3091// {
3092// if (m_input_reader_stack.IsEmpty())
3093// break;
3094// }
3095//
3096 EventSP event_sp;
3097 if (listener.WaitForEvent(NULL, event_sp))
3098 {
3099 if (event_sp)
3100 {
3101 Broadcaster *broadcaster = event_sp->GetBroadcaster();
3102 if (broadcaster)
3103 {
3104 uint32_t event_type = event_sp->GetType();
3105 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
3106 if (broadcaster_class == broadcaster_class_process)
3107 {
3108 HandleProcessEvent (event_sp);
3109 }
3110 else if (broadcaster_class == broadcaster_class_target)
3111 {
3112 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
3113 {
3114 HandleBreakpointEvent (event_sp);
3115 }
3116 }
3117 else if (broadcaster_class == broadcaster_class_thread)
3118 {
3119 HandleThreadEvent (event_sp);
3120 }
3121 else if (broadcaster == m_command_interpreter_ap.get())
3122 {
3123 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
3124 {
3125 done = true;
3126 }
3127 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
3128 {
3129 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3130 if (data && data[0])
3131 {
3132 StreamFileSP error_sp (GetErrorFile());
3133 if (error_sp)
3134 {
3135 HideTopIOHandler();
3136 error_sp->PutCString(data);
3137 error_sp->Flush();
3138 RefreshTopIOHandler();
3139 }
3140 }
3141 }
3142 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
3143 {
3144 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3145 if (data && data[0])
3146 {
3147 StreamFileSP output_sp (GetOutputFile());
3148 if (output_sp)
3149 {
3150 HideTopIOHandler();
3151 output_sp->PutCString(data);
3152 output_sp->Flush();
3153 RefreshTopIOHandler();
3154 }
3155 }
3156 }
3157 }
3158 }
3159
3160 if (m_forward_listener_sp)
3161 m_forward_listener_sp->AddEvent(event_sp);
3162 }
3163 }
3164 }
3165}
3166
3167lldb::thread_result_t
3168Debugger::EventHandlerThread (lldb::thread_arg_t arg)
3169{
3170 ((Debugger *)arg)->DefaultEventHandler();
3171 return NULL;
3172}
3173
3174bool
3175Debugger::StartEventHandlerThread()
3176{
3177 if (!IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3178 m_event_handler_thread = Host::ThreadCreate("lldb.debugger.event-handler", EventHandlerThread, this, NULL);
3179 return IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread);
3180}
3181
3182void
3183Debugger::StopEventHandlerThread()
3184{
3185 if (IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3186 {
3187 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
3188 Host::ThreadJoin(m_event_handler_thread, NULL, NULL);
3189 m_event_handler_thread = LLDB_INVALID_HOST_THREAD;
3190 }
3191}
3192
3193
3194lldb::thread_result_t
3195Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3196{
3197 Debugger *debugger = (Debugger *)arg;
3198 debugger->ExecuteIOHanders();
3199 debugger->StopEventHandlerThread();
3200 return NULL;
3201}
3202
3203bool
3204Debugger::StartIOHandlerThread()
3205{
3206 if (!IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3207 m_io_handler_thread = Host::ThreadCreate("lldb.debugger.io-handler", IOHandlerThread, this, NULL);
3208 return IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread);
3209}
3210
3211void
3212Debugger::StopIOHandlerThread()
3213{
3214 if (IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3215 {
3216 if (m_input_file_sp)
3217 m_input_file_sp->GetFile().Close();
3218 Host::ThreadJoin(m_io_handler_thread, NULL, NULL);
3219 m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
3220 }
3221}
3222
3223