blob: bb982f3e4db26bfc60e7f5025808268de399d0cd [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"
Jason Molenda705b1802014-06-13 02:37:02 +000018#include "llvm/ADT/StringRef.h"
Enrico Granata4becb372011-06-29 22:27:15 +000019
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/lldb-private.h"
21#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000023#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000024#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000026#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000027#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000028#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000029#include "lldb/Core/StreamString.h"
Jason Molenda705b1802014-06-13 02:37:02 +000030#include "lldb/Core/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000032#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000033#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000034#include "lldb/DataFormatters/DataVisualization.h"
35#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000036#include "lldb/DataFormatters/TypeSummary.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000037#include "lldb/Host/DynamicLibrary.h"
Greg Claytona3406612011-02-07 23:24:47 +000038#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000039#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000040#include "lldb/Interpreter/OptionValueSInt64.h"
41#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000042#include "lldb/Symbol/ClangASTContext.h"
43#include "lldb/Symbol/CompileUnit.h"
44#include "lldb/Symbol/Function.h"
45#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000046#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Target/TargetList.h"
48#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000049#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000050#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000051#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000052#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000054#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
56using namespace lldb;
57using namespace lldb_private;
58
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Greg Clayton1b654882010-09-19 02:33:57 +000060static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000061static lldb::user_id_t g_unique_id = 1;
62
Greg Clayton1b654882010-09-19 02:33:57 +000063#pragma mark Static Functions
64
65static Mutex &
66GetDebuggerListMutex ()
67{
68 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
69 return g_mutex;
70}
71
72typedef std::vector<DebuggerSP> DebuggerList;
73
74static DebuggerList &
75GetDebuggerList()
76{
77 // hide the static debugger list inside a singleton accessor to avoid
78 // global init contructors
79 static DebuggerList g_list;
80 return g_list;
81}
Greg Claytone372b982011-11-21 21:44:34 +000082
83OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000084g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000085{
Greg Clayton67cc0632012-08-22 17:17:09 +000086 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
87 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
88 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000089 { 0, NULL, NULL }
90};
91
Greg Clayton67cc0632012-08-22 17:17:09 +000092OptionEnumValueElement
93g_language_enumerators[] =
94{
95 { eScriptLanguageNone, "none", "Disable scripting languages."},
96 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
97 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000098 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000099};
Greg Claytone372b982011-11-21 21:44:34 +0000100
Greg Clayton67cc0632012-08-22 17:17:09 +0000101#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
102#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
103
Michael Sartain0769b2b2013-07-30 16:44:36 +0000104#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000105 "{, ${frame.pc}}"\
106 MODULE_WITH_FUNC\
107 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000108 "{, name = '${thread.name}'}"\
109 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000110 "{, activity = '${thread.info.activity.name}'}" \
111 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000112 "{, stop reason = ${thread.stop-reason}}"\
113 "{\\nReturn value: ${thread.return-value}}"\
114 "\\n"
115
116#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
117 MODULE_WITH_FUNC\
118 FILE_AND_LINE\
119 "\\n"
120
121
122
Greg Clayton754a9362012-08-23 00:22:02 +0000123static PropertyDefinition
124g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000125{
126{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
127{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
128{ "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 +0000129{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000130{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
131{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
132{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
133{ "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." },
134{ "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." },
135{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
136{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
137{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000138{ "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
Enrico Granata90a8db32013-10-31 21:01:07 +0000139{ "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 +0000140
141 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000142};
143
144enum
145{
146 ePropertyAutoConfirm = 0,
147 ePropertyFrameFormat,
148 ePropertyNotiftVoid,
149 ePropertyPrompt,
150 ePropertyScriptLanguage,
151 ePropertyStopDisassemblyCount,
152 ePropertyStopDisassemblyDisplay,
153 ePropertyStopLineCountAfter,
154 ePropertyStopLineCountBefore,
155 ePropertyTerminalWidth,
156 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000157 ePropertyUseExternalEditor,
158 ePropertyUseColor,
Enrico Granata90a8db32013-10-31 21:01:07 +0000159 ePropertyAutoOneLineSummaries
Greg Clayton67cc0632012-08-22 17:17:09 +0000160};
161
Greg Clayton5fb8f792013-12-02 19:35:49 +0000162Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000163
164Error
165Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
166 VarSetOperationType op,
167 const char *property_path,
168 const char *value)
169{
Enrico Granata84a53df2013-05-20 22:29:23 +0000170 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
171 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000172 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000173 if (is_load_script && exe_ctx->GetTargetSP())
174 {
175 target_sp = exe_ctx->GetTargetSP();
176 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
177 }
Greg Clayton4c054102012-09-01 00:38:36 +0000178 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
179 if (error.Success())
180 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000181 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000182 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
183 {
184 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000185 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
186 if (str.length())
187 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000188 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000189 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
190 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
191 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000192 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
193 {
194 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
195 SetPrompt (GetPrompt());
196 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000197 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000198 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000199 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000200 {
201 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000202 StreamString feedback_stream;
203 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000204 {
Greg Clayton44d93782014-01-27 23:43:24 +0000205 StreamFileSP stream_sp (GetErrorFile());
206 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000207 {
Greg Clayton44d93782014-01-27 23:43:24 +0000208 for (auto error : errors)
209 {
210 stream_sp->Printf("%s\n",error.AsCString());
211 }
212 if (feedback_stream.GetSize())
213 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000214 }
215 }
216 }
217 }
Greg Clayton4c054102012-09-01 00:38:36 +0000218 }
219 return error;
220}
221
Greg Clayton67cc0632012-08-22 17:17:09 +0000222bool
223Debugger::GetAutoConfirm () const
224{
225 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000226 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000227}
228
229const char *
230Debugger::GetFrameFormat() const
231{
232 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000233 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000234}
235
236bool
237Debugger::GetNotifyVoid () const
238{
239 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000240 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000241}
242
243const char *
244Debugger::GetPrompt() const
245{
246 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000247 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000248}
249
250void
251Debugger::SetPrompt(const char *p)
252{
253 const uint32_t idx = ePropertyPrompt;
254 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
255 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000256 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
257 if (str.length())
258 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000259 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000260}
261
262const char *
263Debugger::GetThreadFormat() const
264{
265 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000266 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000267}
268
269lldb::ScriptLanguage
270Debugger::GetScriptLanguage() const
271{
272 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000273 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000274}
275
276bool
277Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
278{
279 const uint32_t idx = ePropertyScriptLanguage;
280 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
281}
282
283uint32_t
284Debugger::GetTerminalWidth () const
285{
286 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000287 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000288}
289
290bool
291Debugger::SetTerminalWidth (uint32_t term_width)
292{
293 const uint32_t idx = ePropertyTerminalWidth;
294 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
295}
296
297bool
298Debugger::GetUseExternalEditor () const
299{
300 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000301 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000302}
303
304bool
305Debugger::SetUseExternalEditor (bool b)
306{
307 const uint32_t idx = ePropertyUseExternalEditor;
308 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
309}
310
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000311bool
312Debugger::GetUseColor () const
313{
314 const uint32_t idx = ePropertyUseColor;
315 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
316}
317
318bool
319Debugger::SetUseColor (bool b)
320{
321 const uint32_t idx = ePropertyUseColor;
322 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
323 SetPrompt (GetPrompt());
324 return ret;
325}
326
Greg Clayton67cc0632012-08-22 17:17:09 +0000327uint32_t
328Debugger::GetStopSourceLineCount (bool before) const
329{
330 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000331 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000332}
333
334Debugger::StopDisassemblyType
335Debugger::GetStopDisassemblyDisplay () const
336{
337 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000338 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000339}
340
341uint32_t
342Debugger::GetDisassemblyLineCount () const
343{
344 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000345 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000346}
Greg Claytone372b982011-11-21 21:44:34 +0000347
Enrico Granata553fad52013-10-25 23:09:40 +0000348bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000349Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000350{
Enrico Granata90a8db32013-10-31 21:01:07 +0000351 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000352 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
353
354}
355
Greg Clayton1b654882010-09-19 02:33:57 +0000356#pragma mark Debugger
357
Greg Clayton67cc0632012-08-22 17:17:09 +0000358//const DebuggerPropertiesSP &
359//Debugger::GetSettings() const
360//{
361// return m_properties_sp;
362//}
363//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000364
Caroline Tice2f88aad2011-01-14 00:29:16 +0000365int
366Debugger::TestDebuggerRefCount ()
367{
368 return g_shared_debugger_refcount;
369}
370
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371void
Greg Clayton5fb8f792013-12-02 19:35:49 +0000372Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000374 g_load_plugin_callback = load_plugin_callback;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000375 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000376 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377}
378
379void
380Debugger::Terminate ()
381{
Greg Clayton66111032010-06-23 01:19:29 +0000382 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 {
Greg Clayton66111032010-06-23 01:19:29 +0000384 g_shared_debugger_refcount--;
385 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 {
Greg Claytondbe54502010-11-19 03:46:01 +0000387 lldb_private::WillTerminate();
388 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000389
390 // Clear our master list of debugger objects
391 Mutex::Locker locker (GetDebuggerListMutex ());
392 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 }
395}
396
Caroline Tice20bd37f2011-03-10 22:14:10 +0000397void
398Debugger::SettingsInitialize ()
399{
Greg Clayton6920b522012-08-22 18:39:03 +0000400 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000401}
402
403void
404Debugger::SettingsTerminate ()
405{
Greg Clayton6920b522012-08-22 18:39:03 +0000406 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000407}
408
Enrico Granata21dfcd92012-09-28 23:57:51 +0000409bool
Enrico Granatae743c782013-04-24 21:29:08 +0000410Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000411{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000412 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000413 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000414 lldb::DynamicLibrarySP dynlib_sp = g_load_plugin_callback (shared_from_this(), spec, error);
415 if (dynlib_sp)
416 {
417 m_loaded_plugins.push_back(dynlib_sp);
418 return true;
419 }
Enrico Granatae743c782013-04-24 21:29:08 +0000420 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000421 else
Enrico Granatae743c782013-04-24 21:29:08 +0000422 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000423 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
424 // and if the public API layer isn't available (code is linking against
425 // all of the internal LLDB static libraries), then we can't load plugins
426 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000427 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000428 return false;
429}
430
431static FileSpec::EnumerateDirectoryResult
432LoadPluginCallback
433(
434 void *baton,
435 FileSpec::FileType file_type,
436 const FileSpec &file_spec
437 )
438{
439 Error error;
440
441 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000442 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000443
444 if (!baton)
445 return FileSpec::eEnumerateDirectoryResultQuit;
446
447 Debugger *debugger = (Debugger*)baton;
448
449 // If we have a regular file, a symbolic link or unknown file type, try
450 // and process the file. We must handle unknown as sometimes the directory
451 // enumeration might be enumerating a file system that doesn't have correct
452 // file type information.
453 if (file_type == FileSpec::eFileTypeRegular ||
454 file_type == FileSpec::eFileTypeSymbolicLink ||
455 file_type == FileSpec::eFileTypeUnknown )
456 {
457 FileSpec plugin_file_spec (file_spec);
458 plugin_file_spec.ResolvePath ();
459
Michael Sartain3cf443d2013-07-17 00:26:30 +0000460 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
461 plugin_file_spec.GetFileNameExtension() != g_solibext)
462 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000463 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000464 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000465
Enrico Granatae743c782013-04-24 21:29:08 +0000466 Error plugin_load_error;
467 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000468
469 return FileSpec::eEnumerateDirectoryResultNext;
470 }
471
472 else if (file_type == FileSpec::eFileTypeUnknown ||
473 file_type == FileSpec::eFileTypeDirectory ||
474 file_type == FileSpec::eFileTypeSymbolicLink )
475 {
476 // Try and recurse into anything that a directory or symbolic link.
477 // We must also do this for unknown as sometimes the directory enumeration
478 // might be enurating a file system that doesn't have correct file type
479 // information.
480 return FileSpec::eEnumerateDirectoryResultEnter;
481 }
482
483 return FileSpec::eEnumerateDirectoryResultNext;
484}
485
486void
487Debugger::InstanceInitialize ()
488{
489 FileSpec dir_spec;
490 const bool find_directories = true;
491 const bool find_files = true;
492 const bool find_other = true;
493 char dir_path[PATH_MAX];
494 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
495 {
496 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
497 {
498 FileSpec::EnumerateDirectory (dir_path,
499 find_directories,
500 find_files,
501 find_other,
502 LoadPluginCallback,
503 this);
504 }
505 }
506
507 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
508 {
509 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
510 {
511 FileSpec::EnumerateDirectory (dir_path,
512 find_directories,
513 find_files,
514 find_other,
515 LoadPluginCallback,
516 this);
517 }
518 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000519
520 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000521}
522
Greg Clayton66111032010-06-23 01:19:29 +0000523DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000524Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000525{
Jim Ingham228063c2012-02-21 02:23:08 +0000526 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000527 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000528 {
529 Mutex::Locker locker (GetDebuggerListMutex ());
530 GetDebuggerList().push_back(debugger_sp);
531 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000532 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000533 return debugger_sp;
534}
535
Caroline Ticee02657b2011-01-22 01:02:07 +0000536void
Greg Clayton4d122c42011-09-17 08:33:22 +0000537Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000538{
539 if (debugger_sp.get() == NULL)
540 return;
541
Jim Ingham8314c522011-09-15 21:36:42 +0000542 debugger_sp->Clear();
543
Greg Claytonc15f55e2012-03-30 20:53:46 +0000544 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000545 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000546 Mutex::Locker locker (GetDebuggerListMutex ());
547 DebuggerList &debugger_list = GetDebuggerList ();
548 DebuggerList::iterator pos, end = debugger_list.end();
549 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000550 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000551 if ((*pos).get() == debugger_sp.get())
552 {
553 debugger_list.erase (pos);
554 return;
555 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000556 }
557 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000558}
559
Greg Clayton4d122c42011-09-17 08:33:22 +0000560DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000561Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
562{
Greg Clayton4d122c42011-09-17 08:33:22 +0000563 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000564 if (g_shared_debugger_refcount > 0)
565 {
566 Mutex::Locker locker (GetDebuggerListMutex ());
567 DebuggerList &debugger_list = GetDebuggerList();
568 DebuggerList::iterator pos, end = debugger_list.end();
569
570 for (pos = debugger_list.begin(); pos != end; ++pos)
571 {
572 if ((*pos).get()->m_instance_name == instance_name)
573 {
574 debugger_sp = *pos;
575 break;
576 }
577 }
578 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000579 return debugger_sp;
580}
Greg Clayton66111032010-06-23 01:19:29 +0000581
582TargetSP
583Debugger::FindTargetWithProcessID (lldb::pid_t pid)
584{
Greg Clayton4d122c42011-09-17 08:33:22 +0000585 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000586 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000587 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000588 Mutex::Locker locker (GetDebuggerListMutex ());
589 DebuggerList &debugger_list = GetDebuggerList();
590 DebuggerList::iterator pos, end = debugger_list.end();
591 for (pos = debugger_list.begin(); pos != end; ++pos)
592 {
593 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
594 if (target_sp)
595 break;
596 }
Greg Clayton66111032010-06-23 01:19:29 +0000597 }
598 return target_sp;
599}
600
Greg Claytone4e45922011-11-16 05:37:56 +0000601TargetSP
602Debugger::FindTargetWithProcess (Process *process)
603{
604 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000605 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000606 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000607 Mutex::Locker locker (GetDebuggerListMutex ());
608 DebuggerList &debugger_list = GetDebuggerList();
609 DebuggerList::iterator pos, end = debugger_list.end();
610 for (pos = debugger_list.begin(); pos != end; ++pos)
611 {
612 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
613 if (target_sp)
614 break;
615 }
Greg Claytone4e45922011-11-16 05:37:56 +0000616 }
617 return target_sp;
618}
619
Jim Ingham228063c2012-02-21 02:23:08 +0000620Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000621 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000622 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Clayton44d93782014-01-27 23:43:24 +0000623 m_input_file_sp (new StreamFile (stdin, false)),
624 m_output_file_sp (new StreamFile (stdout, false)),
625 m_error_file_sp (new StreamFile (stderr, false)),
Jim Inghamc5917d92012-11-30 20:23:19 +0000626 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000627 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000628 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 m_listener ("lldb.Debugger"),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000630 m_source_manager_ap(),
Jim Inghame37d6052011-09-13 00:29:56 +0000631 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000632 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000633 m_input_reader_stack (),
Greg Clayton44d93782014-01-27 23:43:24 +0000634 m_instance_name (),
635 m_loaded_plugins (),
636 m_event_handler_thread (LLDB_INVALID_HOST_THREAD),
Greg Claytonb4874f12014-02-28 18:22:24 +0000637 m_io_handler_thread (LLDB_INVALID_HOST_THREAD)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638{
Greg Clayton67cc0632012-08-22 17:17:09 +0000639 char instance_cstr[256];
640 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
641 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000642 if (log_callback)
643 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000644 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000645 // Always add our default platform to the platform list
646 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
647 assert (default_platform_sp.get());
648 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000649
Greg Clayton754a9362012-08-23 00:22:02 +0000650 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000651 m_collection_sp->AppendProperty (ConstString("target"),
652 ConstString("Settings specify to debugging targets."),
653 true,
654 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000655 if (m_command_interpreter_ap.get())
656 {
657 m_collection_sp->AppendProperty (ConstString("interpreter"),
658 ConstString("Settings specify to the debugger's command interpreter."),
659 true,
660 m_command_interpreter_ap->GetValueProperties());
661 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000662 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
663 term_width->SetMinimumValue(10);
664 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000665
666 // Turn off use-color if this is a dumb terminal.
667 const char *term = getenv ("TERM");
668 if (term && !strcmp (term, "dumb"))
669 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670}
671
672Debugger::~Debugger ()
673{
Jim Ingham8314c522011-09-15 21:36:42 +0000674 Clear();
675}
676
677void
678Debugger::Clear()
679{
Greg Clayton44d93782014-01-27 23:43:24 +0000680 ClearIOHandlers();
681 StopIOHandlerThread();
682 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000683 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000684 int num_targets = m_target_list.GetNumTargets();
685 for (int i = 0; i < num_targets; i++)
686 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000687 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
688 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000689 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000690 ProcessSP process_sp (target_sp->GetProcessSP());
691 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000692 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000693 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000694 }
Greg Clayton66111032010-06-23 01:19:29 +0000695 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000696 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000697
698 // Close the input file _before_ we close the input read communications class
699 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000700 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000701 if (m_input_file_sp)
702 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000703
704 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000705}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706
707bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000708Debugger::GetCloseInputOnEOF () const
709{
Greg Clayton44d93782014-01-27 23:43:24 +0000710// return m_input_comm.GetCloseOnEOF();
711 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000712}
713
714void
715Debugger::SetCloseInputOnEOF (bool b)
716{
Greg Clayton44d93782014-01-27 23:43:24 +0000717// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000718}
719
720bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721Debugger::GetAsyncExecution ()
722{
Greg Clayton66111032010-06-23 01:19:29 +0000723 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724}
725
726void
727Debugger::SetAsyncExecution (bool async_execution)
728{
Greg Clayton66111032010-06-23 01:19:29 +0000729 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730}
731
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732
733void
734Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
735{
Greg Clayton44d93782014-01-27 23:43:24 +0000736 if (m_input_file_sp)
737 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
738 else
739 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
740
741 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000742 if (in_file.IsValid() == false)
743 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744
Jim Inghamc5917d92012-11-30 20:23:19 +0000745 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
746 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747}
748
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749void
750Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
751{
Greg Clayton44d93782014-01-27 23:43:24 +0000752 if (m_output_file_sp)
753 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
754 else
755 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
756
757 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000758 if (out_file.IsValid() == false)
759 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000760
Enrico Granatab5887262012-10-29 21:18:03 +0000761 // do not create the ScriptInterpreter just for setting the output file handle
762 // as the constructor will know how to do the right thing on its own
763 const bool can_create = false;
764 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
765 if (script_interpreter)
766 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767}
768
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769void
770Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
771{
Greg Clayton44d93782014-01-27 23:43:24 +0000772 if (m_error_file_sp)
773 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
774 else
775 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
776
777 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000778 if (err_file.IsValid() == false)
779 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780}
781
Jim Inghamc5917d92012-11-30 20:23:19 +0000782void
783Debugger::SaveInputTerminalState ()
784{
Greg Clayton44d93782014-01-27 23:43:24 +0000785 if (m_input_file_sp)
786 {
787 File &in_file = m_input_file_sp->GetFile();
788 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
789 m_terminal_state.Save(in_file.GetDescriptor(), true);
790 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000791}
792
793void
794Debugger::RestoreInputTerminalState ()
795{
796 m_terminal_state.Restore();
797}
798
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000800Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801{
802 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000803 TargetSP target_sp(GetSelectedTarget());
804 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805
806 if (target_sp)
807 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000808 ProcessSP process_sp (target_sp->GetProcessSP());
809 exe_ctx.SetProcessSP (process_sp);
810 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000812 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
813 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000815 exe_ctx.SetThreadSP (thread_sp);
816 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
817 if (exe_ctx.GetFramePtr() == NULL)
818 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819 }
820 }
821 }
822 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823}
824
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825void
Caroline Ticeefed6132010-11-19 20:47:54 +0000826Debugger::DispatchInputInterrupt ()
827{
Greg Clayton44d93782014-01-27 23:43:24 +0000828 Mutex::Locker locker (m_input_reader_stack.GetMutex());
829 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000830 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000831 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000832}
833
834void
835Debugger::DispatchInputEndOfFile ()
836{
Greg Clayton44d93782014-01-27 23:43:24 +0000837 Mutex::Locker locker (m_input_reader_stack.GetMutex());
838 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000839 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000840 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000841}
842
843void
Greg Clayton44d93782014-01-27 23:43:24 +0000844Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000845{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000846 // 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 +0000847 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000848 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000849 {
Greg Clayton44d93782014-01-27 23:43:24 +0000850 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000851 if (reader_sp)
852 {
Greg Clayton44d93782014-01-27 23:43:24 +0000853 m_input_reader_stack.Pop();
854 reader_sp->SetIsDone(true);
Greg Claytone68f5d62014-02-24 22:50:57 +0000855 reader_sp->Cancel();
Caroline Tice3d6086f2010-12-20 18:35:50 +0000856 }
857 }
858}
859
860void
Greg Clayton44d93782014-01-27 23:43:24 +0000861Debugger::ExecuteIOHanders()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000862{
Caroline Tice9088b062011-05-09 23:06:58 +0000863
Greg Clayton44d93782014-01-27 23:43:24 +0000864 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 {
Greg Clayton44d93782014-01-27 23:43:24 +0000866 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867 if (!reader_sp)
868 break;
869
Greg Clayton44d93782014-01-27 23:43:24 +0000870 reader_sp->Activate();
871 reader_sp->Run();
872 reader_sp->Deactivate();
873
874 // Remove all input readers that are done from the top of the stack
875 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876 {
Greg Clayton44d93782014-01-27 23:43:24 +0000877 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
878 if (top_reader_sp && top_reader_sp->GetIsDone())
879 m_input_reader_stack.Pop();
880 else
881 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882 }
883 }
Greg Clayton44d93782014-01-27 23:43:24 +0000884 ClearIOHandlers();
885}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886
Greg Clayton44d93782014-01-27 23:43:24 +0000887bool
888Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
889{
890 return m_input_reader_stack.IsTop (reader_sp);
891}
892
893
894ConstString
895Debugger::GetTopIOHandlerControlSequence(char ch)
896{
897 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898}
899
900void
Greg Clayton44d93782014-01-27 23:43:24 +0000901Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
902{
903 Mutex::Locker locker (m_input_reader_stack.GetMutex());
904 PushIOHandler (reader_sp);
905 reader_sp->Activate();
906 reader_sp->Run();
907 PopIOHandler (reader_sp);
908}
909
910void
911Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
912{
913 // Before an IOHandler runs, it must have in/out/err streams.
914 // This function is called when one ore more of the streams
915 // are NULL. We use the top input reader's in/out/err streams,
916 // or fall back to the debugger file handles, or we fall back
917 // onto stdin/stdout/stderr as a last resort.
918
919 Mutex::Locker locker (m_input_reader_stack.GetMutex());
920 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
921 // If no STDIN has been set, then set it appropriately
922 if (!in)
923 {
924 if (top_reader_sp)
925 in = top_reader_sp->GetInputStreamFile();
926 else
927 in = GetInputFile();
928
929 // If there is nothing, use stdin
930 if (!in)
931 in = StreamFileSP(new StreamFile(stdin, false));
932 }
933 // If no STDOUT has been set, then set it appropriately
934 if (!out)
935 {
936 if (top_reader_sp)
937 out = top_reader_sp->GetOutputStreamFile();
938 else
939 out = GetOutputFile();
940
941 // If there is nothing, use stdout
942 if (!out)
943 out = StreamFileSP(new StreamFile(stdout, false));
944 }
945 // If no STDERR has been set, then set it appropriately
946 if (!err)
947 {
948 if (top_reader_sp)
949 err = top_reader_sp->GetErrorStreamFile();
950 else
951 err = GetErrorFile();
952
953 // If there is nothing, use stderr
954 if (!err)
955 err = StreamFileSP(new StreamFile(stdout, false));
956
957 }
958}
959
960void
961Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962{
963 if (!reader_sp)
964 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000965
Greg Clayton44d93782014-01-27 23:43:24 +0000966 // Got the current top input reader...
967 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000968
Greg Claytonb4874f12014-02-28 18:22:24 +0000969 // Don't push the same IO handler twice...
970 if (reader_sp.get() != top_reader_sp.get())
971 {
972 // Push our new input reader
973 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000974
Greg Claytonb4874f12014-02-28 18:22:24 +0000975 // Interrupt the top input reader to it will exit its Run() function
976 // and let this new input reader take over
977 if (top_reader_sp)
978 top_reader_sp->Deactivate();
979 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980}
981
982bool
Greg Clayton44d93782014-01-27 23:43:24 +0000983Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984{
985 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +0000986
987 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988
989 // The reader on the stop of the stack is done, so let the next
990 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000991 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 {
Greg Clayton44d93782014-01-27 23:43:24 +0000993 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994
995 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
996 {
Greg Clayton44d93782014-01-27 23:43:24 +0000997 reader_sp->Deactivate();
Greg Claytonb4874f12014-02-28 18:22:24 +0000998 reader_sp->Cancel();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000999 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +00001000
1001 reader_sp = m_input_reader_stack.Top();
1002 if (reader_sp)
1003 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004
Greg Clayton44d93782014-01-27 23:43:24 +00001005 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001006 }
1007 }
1008 return result;
1009}
1010
1011bool
Greg Clayton44d93782014-01-27 23:43:24 +00001012Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013{
Greg Clayton44d93782014-01-27 23:43:24 +00001014 Mutex::Locker locker;
1015
1016 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017 {
Greg Clayton44d93782014-01-27 23:43:24 +00001018 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1019 if (reader_sp)
1020 reader_sp->Hide();
1021 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 }
Greg Clayton44d93782014-01-27 23:43:24 +00001023 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001024}
1025
1026void
Greg Clayton44d93782014-01-27 23:43:24 +00001027Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028{
Greg Clayton44d93782014-01-27 23:43:24 +00001029 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1030 if (reader_sp)
1031 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001032}
Greg Clayton66111032010-06-23 01:19:29 +00001033
Greg Clayton44d93782014-01-27 23:43:24 +00001034
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001035StreamSP
1036Debugger::GetAsyncOutputStream ()
1037{
1038 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1039 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1040}
1041
1042StreamSP
1043Debugger::GetAsyncErrorStream ()
1044{
1045 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1046 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1047}
1048
Greg Claytonc7bece562013-01-25 18:06:21 +00001049size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001050Debugger::GetNumDebuggers()
1051{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001052 if (g_shared_debugger_refcount > 0)
1053 {
1054 Mutex::Locker locker (GetDebuggerListMutex ());
1055 return GetDebuggerList().size();
1056 }
1057 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001058}
1059
1060lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001061Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001062{
1063 DebuggerSP debugger_sp;
1064
Greg Claytonc15f55e2012-03-30 20:53:46 +00001065 if (g_shared_debugger_refcount > 0)
1066 {
1067 Mutex::Locker locker (GetDebuggerListMutex ());
1068 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001069
Greg Claytonc15f55e2012-03-30 20:53:46 +00001070 if (index < debugger_list.size())
1071 debugger_sp = debugger_list[index];
1072 }
1073
Enrico Granata061858c2012-02-15 02:34:21 +00001074 return debugger_sp;
1075}
1076
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001077DebuggerSP
1078Debugger::FindDebuggerWithID (lldb::user_id_t id)
1079{
Greg Clayton4d122c42011-09-17 08:33:22 +00001080 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001081
Greg Claytonc15f55e2012-03-30 20:53:46 +00001082 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001083 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001084 Mutex::Locker locker (GetDebuggerListMutex ());
1085 DebuggerList &debugger_list = GetDebuggerList();
1086 DebuggerList::iterator pos, end = debugger_list.end();
1087 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001088 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001089 if ((*pos).get()->GetID() == id)
1090 {
1091 debugger_sp = *pos;
1092 break;
1093 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001094 }
1095 }
1096 return debugger_sp;
1097}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001098
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001099#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001100static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001101TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001102{
1103 if (frame == NULL)
1104 return;
1105
1106 StreamString s;
1107 const char *prompt_format =
1108 "{addr = '${addr}'\n}"
1109 "{process.id = '${process.id}'\n}"
1110 "{process.name = '${process.name}'\n}"
1111 "{process.file.basename = '${process.file.basename}'\n}"
1112 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1113 "{thread.id = '${thread.id}'\n}"
1114 "{thread.index = '${thread.index}'\n}"
1115 "{thread.name = '${thread.name}'\n}"
1116 "{thread.queue = '${thread.queue}'\n}"
1117 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1118 "{target.arch = '${target.arch}'\n}"
1119 "{module.file.basename = '${module.file.basename}'\n}"
1120 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1121 "{file.basename = '${file.basename}'\n}"
1122 "{file.fullpath = '${file.fullpath}'\n}"
1123 "{frame.index = '${frame.index}'\n}"
1124 "{frame.pc = '${frame.pc}'\n}"
1125 "{frame.sp = '${frame.sp}'\n}"
1126 "{frame.fp = '${frame.fp}'\n}"
1127 "{frame.flags = '${frame.flags}'\n}"
1128 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1129 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1130 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1131 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1132 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1133 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1134 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1135 "{function.id = '${function.id}'\n}"
1136 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001137 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001138 "{function.addr-offset = '${function.addr-offset}'\n}"
1139 "{function.line-offset = '${function.line-offset}'\n}"
1140 "{function.pc-offset = '${function.pc-offset}'\n}"
1141 "{line.file.basename = '${line.file.basename}'\n}"
1142 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1143 "{line.number = '${line.number}'\n}"
1144 "{line.start-addr = '${line.start-addr}'\n}"
1145 "{line.end-addr = '${line.end-addr}'\n}"
1146;
1147
1148 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1149 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001150 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001151 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001152 {
1153 printf("%s\n", s.GetData());
1154 }
1155 else
1156 {
Greg Clayton1b654882010-09-19 02:33:57 +00001157 printf ("what we got: %s\n", s.GetData());
1158 }
1159}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001160#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001161
Enrico Granata9fc19442011-07-06 02:13:41 +00001162static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001163ScanFormatDescriptor (const char* var_name_begin,
1164 const char* var_name_end,
1165 const char** var_name_final,
1166 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001167 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001168 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001169{
Greg Clayton5160ce52013-03-27 23:08:40 +00001170 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001171 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001172 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001173 {
1174 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001175 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001176 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001177 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001178 else
1179 {
1180 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001181 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001182 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001183 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001184 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001185 true,
1186 *custom_format) )
1187 {
Enrico Granatae992a082011-07-22 17:03:19 +00001188 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001189 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001190
1191 switch (format_name.front())
1192 {
1193 case '@': // if this is an @ sign, print ObjC description
1194 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1195 break;
1196 case 'V': // if this is a V, print the value using the default format
1197 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1198 break;
1199 case 'L': // if this is an L, print the location of the value
1200 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1201 break;
1202 case 'S': // if this is an S, print the summary after all
1203 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1204 break;
1205 case '#': // if this is a '#', print the number of children
1206 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1207 break;
1208 case 'T': // if this is a 'T', print the type
1209 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1210 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001211 case 'N': // if this is a 'N', print the name
1212 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1213 break;
1214 case '>': // if this is a '>', print the name
1215 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1216 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001217 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001218 if (log)
1219 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001220 break;
1221 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001222 }
1223 // a good custom format tells us to print the value using it
1224 else
Enrico Granatae992a082011-07-22 17:03:19 +00001225 {
1226 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001227 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001228 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001229 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001230 }
Enrico Granatae992a082011-07-22 17:03:19 +00001231 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001232 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001233 *custom_format,
1234 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001235 return true;
1236}
1237
1238static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001239ScanBracketedRange (const char* var_name_begin,
1240 const char* var_name_end,
1241 const char* var_name_final,
1242 const char** open_bracket_position,
1243 const char** separator_position,
1244 const char** close_bracket_position,
1245 const char** var_name_final_if_array_range,
1246 int64_t* index_lower,
1247 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001248{
Greg Clayton5160ce52013-03-27 23:08:40 +00001249 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001250 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001251 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001252 {
1253 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1254 *close_bracket_position = ::strchr(*open_bracket_position,']');
1255 // as usual, we assume that [] will come before %
1256 //printf("trying to expand a []\n");
1257 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001258 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001259 {
Enrico Granatae992a082011-07-22 17:03:19 +00001260 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001261 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001262 *index_lower = 0;
1263 }
1264 else if (*separator_position == NULL || *separator_position > var_name_end)
1265 {
1266 char *end = NULL;
1267 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1268 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001269 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001270 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001271 }
Greg Clayton34132752011-07-06 04:07:21 +00001272 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001273 {
1274 char *end = NULL;
1275 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1276 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001277 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001278 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001279 }
1280 else
Enrico Granatae992a082011-07-22 17:03:19 +00001281 {
1282 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001283 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001284 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001285 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001286 if (*index_lower > *index_higher && *index_higher > 0)
1287 {
Enrico Granatae992a082011-07-22 17:03:19 +00001288 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001289 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001290 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001291 *index_lower = *index_higher;
1292 *index_higher = temp;
1293 }
1294 }
Enrico Granatae992a082011-07-22 17:03:19 +00001295 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001296 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001297 return true;
1298}
1299
Michael Sartain0769b2b2013-07-30 16:44:36 +00001300template <typename T>
1301static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1302{
1303 if (script_interpreter)
1304 {
1305 Error script_error;
1306 std::string script_output;
1307
1308 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1309 {
1310 s.Printf("%s", script_output.c_str());
1311 return true;
1312 }
1313 else
1314 {
1315 s.Printf("<error: %s>",script_error.AsCString());
1316 }
1317 }
1318 return false;
1319}
1320
Enrico Granata9fc19442011-07-06 02:13:41 +00001321static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001322ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001323 size_t index,
Jason Molendab57e4a12013-11-04 09:33:30 +00001324 StackFrame* frame,
Enrico Granatadc940732011-08-23 00:32:52 +00001325 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001326{
Greg Clayton5160ce52013-03-27 23:08:40 +00001327 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001328 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001329 std::string ptr_deref_buffer(10,0);
1330 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001331 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001332 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001333 const char* first_unparsed;
1334 ValueObject::GetValueForExpressionPathOptions options;
1335 ValueObject::ExpressionPathEndResultType final_value_type;
1336 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001337 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001338 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001339 &first_unparsed,
1340 &reason_to_stop,
1341 &final_value_type,
1342 options,
1343 &what_next);
1344 if (!item)
1345 {
Enrico Granatae992a082011-07-22 17:03:19 +00001346 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001347 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001348 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001349 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001350 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001351 else
1352 {
Enrico Granatae992a082011-07-22 17:03:19 +00001353 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001354 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001355 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001356 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001357 }
1358 return item;
1359}
1360
Michael Sartain0769b2b2013-07-30 16:44:36 +00001361static inline bool
1362IsToken(const char *var_name_begin, const char *var)
1363{
1364 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1365}
1366
1367static bool
1368IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1369 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1370{
1371 int var_len = strlen(var);
1372 if (::strncmp (var_name_begin, var, var_len) == 0)
1373 {
1374 var_name_begin += var_len;
1375 if (*var_name_begin == '}')
1376 {
1377 format = default_format;
1378 return true;
1379 }
1380 else if (*var_name_begin == '%')
1381 {
1382 // Allow format specifiers: x|X|u with optional width specifiers.
1383 // ${thread.id%x} ; hex
1384 // ${thread.id%X} ; uppercase hex
1385 // ${thread.id%u} ; unsigned decimal
1386 // ${thread.id%8.8X} ; width.precision + specifier
1387 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1388 int dot_count = 0;
1389 const char *specifier = NULL;
1390 int width_precision_length = 0;
1391 const char *width_precision = ++var_name_begin;
1392 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1393 {
1394 dot_count += (*var_name_begin == '.');
1395 if (dot_count > 1)
1396 break;
1397 var_name_begin++;
1398 width_precision_length++;
1399 }
1400
1401 if (IsToken (var_name_begin, "tid}"))
1402 {
1403 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1404 if (target)
1405 {
1406 ArchSpec arch (target->GetArchitecture ());
1407 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1408 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1409 specifier = PRIu64;
1410 }
1411 if (!specifier)
1412 {
1413 format = default_format;
1414 return true;
1415 }
1416 }
1417 else if (IsToken (var_name_begin, "x}"))
1418 specifier = PRIx64;
1419 else if (IsToken (var_name_begin, "X}"))
1420 specifier = PRIX64;
1421 else if (IsToken (var_name_begin, "u}"))
1422 specifier = PRIu64;
1423
1424 if (specifier)
1425 {
1426 format = "%";
1427 if (width_precision_length)
1428 format += std::string(width_precision, width_precision_length);
1429 format += specifier;
1430 return true;
1431 }
1432 }
1433 }
1434 return false;
1435}
1436
Jason Molenda705b1802014-06-13 02:37:02 +00001437// Find information for the "thread.info.*" specifiers in a format string
1438static bool
1439FormatThreadExtendedInfoRecurse
1440(
1441 const char *var_name_begin,
1442 StructuredData::ObjectSP thread_info_dictionary,
1443 const SymbolContext *sc,
1444 const ExecutionContext *exe_ctx,
1445 Stream &s
1446)
1447{
1448 bool var_success = false;
1449 std::string token_format;
1450
1451 llvm::StringRef var_name(var_name_begin);
1452 size_t percent_idx = var_name.find('%');
1453 size_t close_curly_idx = var_name.find('}');
1454 llvm::StringRef path = var_name;
1455 llvm::StringRef formatter = var_name;
1456
1457 // 'path' will be the dot separated list of objects to transverse up until we hit
1458 // a close curly brace, a percent sign, or an end of string.
1459 if (percent_idx != llvm::StringRef::npos || close_curly_idx != llvm::StringRef::npos)
1460 {
1461 if (percent_idx != llvm::StringRef::npos && close_curly_idx != llvm::StringRef::npos)
1462 {
1463 if (percent_idx < close_curly_idx)
1464 {
1465 path = var_name.slice(0, percent_idx);
1466 formatter = var_name.substr (percent_idx);
1467 }
1468 else
1469 {
1470 path = var_name.slice(0, close_curly_idx);
1471 formatter = var_name.substr (close_curly_idx);
1472 }
1473 }
1474 else if (percent_idx != llvm::StringRef::npos)
1475 {
1476 path = var_name.slice(0, percent_idx);
1477 formatter = var_name.substr (percent_idx);
1478 }
1479 else if (close_curly_idx != llvm::StringRef::npos)
1480 {
1481 path = var_name.slice(0, close_curly_idx);
1482 formatter = var_name.substr (close_curly_idx);
1483 }
1484 }
1485
1486 StructuredData::ObjectSP value = thread_info_dictionary->GetObjectForDotSeparatedPath (path);
1487
1488 if (value.get())
1489 {
1490 if (value->GetType() == StructuredData::Type::eTypeInteger)
1491 {
1492 if (IsTokenWithFormat (formatter.str().c_str(), "", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
1493 {
1494 s.Printf(token_format.c_str(), value->GetAsInteger()->GetValue());
1495 var_success = true;
1496 }
1497 }
1498 else if (value->GetType() == StructuredData::Type::eTypeFloat)
1499 {
1500 s.Printf ("%f", value->GetAsFloat()->GetValue());
1501 var_success = true;
1502 }
1503 else if (value->GetType() == StructuredData::Type::eTypeString)
1504 {
1505 s.Printf("%s", value->GetAsString()->GetValue().c_str());
1506 var_success = true;
1507 }
1508 else if (value->GetType() == StructuredData::Type::eTypeArray)
1509 {
1510 if (value->GetAsArray()->GetSize() > 0)
1511 {
1512 s.Printf ("%zu", value->GetAsArray()->GetSize());
1513 var_success = true;
1514 }
1515 }
1516 else if (value->GetType() == StructuredData::Type::eTypeDictionary)
1517 {
1518 s.Printf ("%zu", value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize());
1519 var_success = true;
1520 }
1521 }
1522
1523 return var_success;
1524}
1525
1526
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001527static bool
1528FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001529(
1530 const char *format,
1531 const SymbolContext *sc,
1532 const ExecutionContext *exe_ctx,
1533 const Address *addr,
1534 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001535 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001536 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001537)
1538{
Enrico Granatac482a192011-08-17 22:13:59 +00001539 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001540 bool success = true;
1541 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001542 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001543
Greg Clayton1b654882010-09-19 02:33:57 +00001544 for (p = format; *p != '\0'; ++p)
1545 {
Enrico Granatac482a192011-08-17 22:13:59 +00001546 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001547 {
Enrico Granatac482a192011-08-17 22:13:59 +00001548 valobj = realvalobj;
1549 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001550 }
Greg Clayton1b654882010-09-19 02:33:57 +00001551 size_t non_special_chars = ::strcspn (p, "${}\\");
1552 if (non_special_chars > 0)
1553 {
1554 if (success)
1555 s.Write (p, non_special_chars);
1556 p += non_special_chars;
1557 }
1558
1559 if (*p == '\0')
1560 {
1561 break;
1562 }
1563 else if (*p == '{')
1564 {
1565 // Start a new scope that must have everything it needs if it is to
1566 // to make it into the final output stream "s". If you want to make
1567 // a format that only prints out the function or symbol name if there
1568 // is one in the symbol context you can use:
1569 // "{function =${function.name}}"
1570 // The first '{' starts a new scope that end with the matching '}' at
1571 // the end of the string. The contents "function =${function.name}"
1572 // will then be evaluated and only be output if there is a function
1573 // or symbol with a valid name.
1574 StreamString sub_strm;
1575
1576 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001577
1578 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001579 {
1580 // The stream had all it needed
1581 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1582 }
1583 if (*p != '}')
1584 {
1585 success = false;
1586 break;
1587 }
1588 }
1589 else if (*p == '}')
1590 {
1591 // End of a enclosing scope
1592 break;
1593 }
1594 else if (*p == '$')
1595 {
1596 // We have a prompt variable to print
1597 ++p;
1598 if (*p == '{')
1599 {
1600 ++p;
1601 const char *var_name_begin = p;
1602 const char *var_name_end = ::strchr (p, '}');
1603
1604 if (var_name_end && var_name_begin < var_name_end)
1605 {
1606 // if we have already failed to parse, skip this variable
1607 if (success)
1608 {
1609 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001610 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001611 Address format_addr;
1612 bool calculate_format_addr_function_offset = false;
1613 // Set reg_kind and reg_num to invalid values
1614 RegisterKind reg_kind = kNumRegisterKinds;
1615 uint32_t reg_num = LLDB_INVALID_REGNUM;
1616 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001617 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001618 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001619 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001620 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1621 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001622
Greg Clayton1b654882010-09-19 02:33:57 +00001623 // Each variable must set success to true below...
1624 bool var_success = false;
1625 switch (var_name_begin[0])
1626 {
Enrico Granata4becb372011-06-29 22:27:15 +00001627 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001628 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001629 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001630 {
Enrico Granatac482a192011-08-17 22:13:59 +00001631 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001632 break;
1633
Enrico Granatac3e320a2011-08-02 17:27:39 +00001634 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001635 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001636
Enrico Granata6f3533f2011-07-29 19:53:35 +00001637 // check for *var and *svar
1638 if (*var_name_begin == '*')
1639 {
1640 do_deref_pointer = true;
1641 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001642 if (log)
1643 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001644 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001645
Enrico Granata6f3533f2011-07-29 19:53:35 +00001646 if (*var_name_begin == 's')
1647 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001648 if (!valobj->IsSynthetic())
1649 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001650 if (!valobj)
1651 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001652 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001653 if (log)
1654 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001655 }
1656
1657 // should be a 'v' by now
1658 if (*var_name_begin != 'v')
1659 break;
1660
Enrico Granatac3e320a2011-08-02 17:27:39 +00001661 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001662 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001663
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001664 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001665 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001666 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001667 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001668 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001669 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001670 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001671 const char* var_name_final = NULL;
1672 const char* var_name_final_if_array_range = NULL;
1673 const char* close_bracket_position = NULL;
1674 int64_t index_lower = -1;
1675 int64_t index_higher = -1;
1676 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001677 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001678 bool was_plain_var = false;
1679 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001680 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001681
Enrico Granatac482a192011-08-17 22:13:59 +00001682 if (!valobj) break;
1683 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001684 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001685 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001686 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001687 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001688 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001689 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001690 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001691 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001692 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001693 // this is a variable with some custom format applied to it
1694 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001695 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001696 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001697 ScanFormatDescriptor (var_name_begin,
1698 var_name_end,
1699 &var_name_final,
1700 &percent_position,
1701 &custom_format,
1702 &val_obj_display);
1703 }
1704 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001705 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001706 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001707 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001708 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001709 const char* percent_position;
1710 ScanFormatDescriptor (var_name_begin,
1711 var_name_end,
1712 &var_name_final,
1713 &percent_position,
1714 &custom_format,
1715 &val_obj_display);
1716
1717 const char* open_bracket_position;
1718 const char* separator_position;
1719 ScanBracketedRange (var_name_begin,
1720 var_name_end,
1721 var_name_final,
1722 &open_bracket_position,
1723 &separator_position,
1724 &close_bracket_position,
1725 &var_name_final_if_array_range,
1726 &index_lower,
1727 &index_higher);
1728
1729 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001730
Enrico Granata599171a2013-02-01 23:59:44 +00001731 std::string expr_path(var_name_final-var_name_begin-1,0);
1732 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1733
1734 if (log)
1735 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1736
1737 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001738 &first_unparsed,
1739 &reason_to_stop,
1740 &final_value_type,
1741 options,
1742 &what_next).get();
1743
1744 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001745 {
Enrico Granatae992a082011-07-22 17:03:19 +00001746 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001747 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001748 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001749 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001750 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001751 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001752 else
1753 {
Enrico Granatae992a082011-07-22 17:03:19 +00001754 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001755 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001756 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001757 first_unparsed, reason_to_stop, final_value_type);
1758 }
Enrico Granata4becb372011-06-29 22:27:15 +00001759 }
Greg Clayton34132752011-07-06 04:07:21 +00001760 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001761 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001762
Enrico Granata86cc9822012-03-19 22:58:49 +00001763 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1764 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001765
Enrico Granata86cc9822012-03-19 22:58:49 +00001766 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001767
Enrico Granataa7187d02011-07-06 19:27:11 +00001768 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001769 {
Greg Clayton34132752011-07-06 04:07:21 +00001770 // I have not deref-ed yet, let's do it
1771 // this happens when we are not going through GetValueForVariableExpressionPath
1772 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001773 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001774 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001775 if (error.Fail())
1776 {
1777 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001778 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001779 break;
1780 }
Greg Clayton34132752011-07-06 04:07:21 +00001781 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001782 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001783
Jim Inghamf164d942014-03-11 18:17:23 +00001784 if (!target)
1785 {
1786 if (log)
1787 log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
1788 break;
1789 }
1790
Enrico Granataa777dc22012-05-08 21:49:57 +00001791 // we do not want to use the summary for a bitfield of type T:n
1792 // if we were originally dealing with just a T - that would get
1793 // us into an endless recursion
1794 if (target->IsBitfield() && was_var_indexed)
1795 {
1796 // TODO: check for a (T:n)-specific summary - we should still obey that
1797 StreamString bitfield_name;
1798 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1799 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1800 if (!DataVisualization::GetSummaryForType(type_sp))
1801 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1802 }
1803
Enrico Granata85933ed2011-08-18 16:38:26 +00001804 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001805 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1806 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1807 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1808 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001809
Enrico Granata86cc9822012-03-19 22:58:49 +00001810 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 +00001811 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001812 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001813 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001814 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001815
Greg Clayton5088c482013-03-25 21:06:13 +00001816 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001817 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001818 // try to use the special cases
1819 var_success = target->DumpPrintableRepresentation(str_temp,
1820 val_obj_display,
1821 custom_format);
1822 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001823 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001824
1825 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001826 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001827 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001828 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001829 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001830 }
1831 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001832 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001833 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001834 {
1835 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1836 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001837 else if (is_pointer) // if pointer, value is the address stored
1838 {
Greg Clayton23f59502012-07-17 03:23:13 +00001839 target->DumpPrintableRepresentation (s,
1840 val_obj_display,
1841 custom_format,
1842 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001843 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001844 var_success = true;
1845 break;
1846 }
1847 }
1848
1849 // if directly trying to print ${var}, and this is an aggregate, display a nice
1850 // type @ location message
1851 if (is_aggregate && was_plain_var)
1852 {
1853 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1854 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001855 break;
1856 }
1857
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001858 // 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 +00001859 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001860 {
1861 s << "<invalid use of aggregate type>";
1862 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001863 break;
1864 }
Greg Clayton34132752011-07-06 04:07:21 +00001865
1866 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001867 {
1868 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001869 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001870 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001871 }
Greg Clayton34132752011-07-06 04:07:21 +00001872 else
Enrico Granatae992a082011-07-22 17:03:19 +00001873 {
1874 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001875 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001876 if (!is_array && !is_pointer)
1877 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001878 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001879 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001880 const char* special_directions = NULL;
1881 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001882 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1883 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001884 ConstString additional_data;
1885 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1886 special_directions_writer.Printf("${%svar%s}",
1887 do_deref_pointer ? "*" : "",
1888 additional_data.GetCString());
1889 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001890 }
1891
1892 // let us display items index_lower thru index_higher of this array
1893 s.PutChar('[');
1894 var_success = true;
1895
1896 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001897 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001898
Greg Claytoncc4d0142012-02-17 07:49:44 +00001899 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001900
Greg Clayton34132752011-07-06 04:07:21 +00001901 for (;index_lower<=index_higher;index_lower++)
1902 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001903 ValueObject* item = ExpandIndexedExpression (target,
1904 index_lower,
1905 exe_ctx->GetFramePtr(),
1906 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001907
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001908 if (!item)
1909 {
Enrico Granatae992a082011-07-22 17:03:19 +00001910 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001911 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001912 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001913 else
1914 {
Enrico Granatae992a082011-07-22 17:03:19 +00001915 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001916 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001917 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001918
Greg Clayton34132752011-07-06 04:07:21 +00001919 if (!special_directions)
1920 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1921 else
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001922 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
Greg Clayton34132752011-07-06 04:07:21 +00001923
Enrico Granata22c55d12011-08-12 02:00:06 +00001924 if (--max_num_children == 0)
1925 {
1926 s.PutCString(", ...");
1927 break;
1928 }
1929
Greg Clayton34132752011-07-06 04:07:21 +00001930 if (index_lower < index_higher)
1931 s.PutChar(',');
1932 }
1933 s.PutChar(']');
1934 }
Enrico Granata4becb372011-06-29 22:27:15 +00001935 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001936 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001937 case 'a':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001938 if (IsToken (var_name_begin, "addr}"))
Greg Clayton1b654882010-09-19 02:33:57 +00001939 {
1940 if (addr && addr->IsValid())
1941 {
1942 var_success = true;
1943 format_addr = *addr;
1944 }
1945 }
1946 break;
1947
1948 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001949 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00001950 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001951 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001952 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001953 Process *process = exe_ctx->GetProcessPtr();
1954 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001955 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001956 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001957 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001958 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001959 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001960 var_success = true;
1961 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001962 else if ((IsToken (var_name_begin, "name}")) ||
1963 (IsToken (var_name_begin, "file.basename}")) ||
1964 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00001965 {
1966 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1967 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001968 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001969 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1970 {
1971 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001972 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001973 }
1974 else
1975 {
1976 format_file_spec = exe_module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001977 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001978 }
Greg Clayton1b654882010-09-19 02:33:57 +00001979 }
1980 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001981 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001982 {
1983 var_name_begin += ::strlen("script:");
1984 std::string script_name(var_name_begin,var_name_end);
1985 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001986 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
1987 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001988 }
Greg Clayton1b654882010-09-19 02:33:57 +00001989 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001990 }
Greg Clayton1b654882010-09-19 02:33:57 +00001991 }
1992 break;
1993
1994 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001995 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00001996 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001997 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001998 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001999 Thread *thread = exe_ctx->GetThreadPtr();
2000 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00002001 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002002 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002003 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002004 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002005 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002006 var_success = true;
2007 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002008 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00002009 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002010 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00002011 var_success = true;
2012 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002013 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00002014 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002015 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002016 var_success = true;
2017 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002018 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002019 {
2020 cstr = thread->GetName();
2021 var_success = cstr && cstr[0];
2022 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002023 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00002024 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002025 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002026 {
2027 cstr = thread->GetQueueName();
2028 var_success = cstr && cstr[0];
2029 if (var_success)
2030 s.PutCString(cstr);
2031 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002032 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002033 {
2034 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002035 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00002036 {
2037 cstr = stop_info_sp->GetDescription();
2038 if (cstr && cstr[0])
2039 {
2040 s.PutCString(cstr);
2041 var_success = true;
2042 }
Greg Clayton1b654882010-09-19 02:33:57 +00002043 }
2044 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002045 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00002046 {
2047 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002048 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00002049 {
2050 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2051 if (return_valobj_sp)
2052 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00002053 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00002054 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00002055 }
2056 }
2057 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002058 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002059 {
2060 var_name_begin += ::strlen("script:");
2061 std::string script_name(var_name_begin,var_name_end);
2062 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002063 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
2064 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002065 }
Jason Molenda705b1802014-06-13 02:37:02 +00002066 else if (IsToken (var_name_begin, "info."))
2067 {
2068 var_name_begin += ::strlen("info.");
2069 StructuredData::ObjectSP object_sp = thread->GetExtendedInfo();
2070 if (object_sp && object_sp->GetType() == StructuredData::Type::eTypeDictionary)
2071 {
2072 var_success = FormatThreadExtendedInfoRecurse (var_name_begin, object_sp, sc, exe_ctx, s);
2073 }
2074 }
Greg Clayton1b654882010-09-19 02:33:57 +00002075 }
2076 }
2077 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002078 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00002079 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002080 // TODO: hookup properties
2081// if (!target_properties_sp)
2082// {
2083// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2084// if (target)
2085// target_properties_sp = target->GetProperties();
2086// }
2087//
2088// if (target_properties_sp)
2089// {
2090// var_name_begin += ::strlen ("target.");
2091// const char *end_property = strchr(var_name_begin, '}');
2092// if (end_property)
2093// {
2094// ConstString property_name(var_name_begin, end_property - var_name_begin);
2095// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2096// if (!property_value.empty())
2097// {
2098// s.PutCString (property_value.c_str());
2099// var_success = true;
2100// }
2101// }
2102// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002103 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2104 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002105 {
Greg Clayton1b654882010-09-19 02:33:57 +00002106 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002107 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002108 {
2109 ArchSpec arch (target->GetArchitecture ());
2110 if (arch.IsValid())
2111 {
Greg Clayton64195a22011-02-23 00:35:02 +00002112 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002113 var_success = true;
2114 }
2115 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002116 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002117 {
2118 var_name_begin += ::strlen("script:");
2119 std::string script_name(var_name_begin,var_name_end);
2120 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002121 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2122 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002123 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002124 }
Greg Clayton1b654882010-09-19 02:33:57 +00002125 }
2126 break;
2127
2128
2129 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002130 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002131 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002132 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002133 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002134 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002135 var_name_begin += ::strlen ("module.");
2136
Michael Sartain0769b2b2013-07-30 16:44:36 +00002137 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002138 {
2139 if (module->GetFileSpec())
2140 {
2141 var_name_begin += ::strlen ("file.");
2142
Michael Sartain0769b2b2013-07-30 16:44:36 +00002143 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002144 {
2145 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002146 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002147 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002148 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002149 {
2150 format_file_spec = module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002151 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002152 }
2153 }
2154 }
2155 }
2156 }
2157 break;
2158
2159
2160 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002161 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002162 {
2163 if (sc && sc->comp_unit != NULL)
2164 {
2165 var_name_begin += ::strlen ("file.");
2166
Michael Sartain0769b2b2013-07-30 16:44:36 +00002167 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002168 {
2169 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002170 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002171 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002172 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002173 {
2174 format_file_spec = *sc->comp_unit;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002175 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002176 }
2177 }
2178 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002179 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002180 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002181 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002182 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002183 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002184 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002185 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002186 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002187 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002188 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002189 s.Printf("%u", frame->GetFrameIndex());
2190 var_success = true;
2191 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002192 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002193 {
2194 reg_kind = eRegisterKindGeneric;
2195 reg_num = LLDB_REGNUM_GENERIC_PC;
2196 var_success = true;
2197 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002198 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002199 {
2200 reg_kind = eRegisterKindGeneric;
2201 reg_num = LLDB_REGNUM_GENERIC_SP;
2202 var_success = true;
2203 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002204 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002205 {
2206 reg_kind = eRegisterKindGeneric;
2207 reg_num = LLDB_REGNUM_GENERIC_FP;
2208 var_success = true;
2209 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002210 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002211 {
2212 reg_kind = eRegisterKindGeneric;
2213 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2214 var_success = true;
2215 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002216 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002217 {
2218 reg_ctx = frame->GetRegisterContext().get();
2219 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002220 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002221 var_name_begin += ::strlen ("reg.");
2222 if (var_name_begin < var_name_end)
2223 {
2224 std::string reg_name (var_name_begin, var_name_end);
2225 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2226 if (reg_info)
2227 var_success = true;
2228 }
Greg Clayton1b654882010-09-19 02:33:57 +00002229 }
2230 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002231 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002232 {
2233 var_name_begin += ::strlen("script:");
2234 std::string script_name(var_name_begin,var_name_end);
2235 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002236 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2237 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002238 }
Greg Clayton1b654882010-09-19 02:33:57 +00002239 }
2240 }
2241 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002242 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002243 {
2244 if (sc && (sc->function != NULL || sc->symbol != NULL))
2245 {
2246 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002247 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002248 {
2249 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002250 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002251 else
2252 s.Printf("symbol[%u]", sc->symbol->GetID());
2253
2254 var_success = true;
2255 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002256 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002257 {
2258 if (sc->function)
2259 cstr = sc->function->GetName().AsCString (NULL);
2260 else if (sc->symbol)
2261 cstr = sc->symbol->GetName().AsCString (NULL);
2262 if (cstr)
2263 {
2264 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002265
2266 if (sc->block)
2267 {
2268 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2269 if (inline_block)
2270 {
2271 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2272 if (inline_info)
2273 {
2274 s.PutCString(" [inlined] ");
2275 inline_info->GetName().Dump(&s);
2276 }
2277 }
2278 }
Greg Clayton1b654882010-09-19 02:33:57 +00002279 var_success = true;
2280 }
2281 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002282 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002283 {
2284 // Print the function name with arguments in it
2285
2286 if (sc->function)
2287 {
2288 var_success = true;
2289 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2290 cstr = sc->function->GetName().AsCString (NULL);
2291 if (cstr)
2292 {
2293 const InlineFunctionInfo *inline_info = NULL;
2294 VariableListSP variable_list_sp;
2295 bool get_function_vars = true;
2296 if (sc->block)
2297 {
2298 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2299
2300 if (inline_block)
2301 {
2302 get_function_vars = false;
2303 inline_info = sc->block->GetInlinedFunctionInfo();
2304 if (inline_info)
2305 variable_list_sp = inline_block->GetBlockVariableList (true);
2306 }
2307 }
2308
2309 if (get_function_vars)
2310 {
2311 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2312 }
2313
2314 if (inline_info)
2315 {
2316 s.PutCString (cstr);
2317 s.PutCString (" [inlined] ");
2318 cstr = inline_info->GetName().GetCString();
2319 }
2320
2321 VariableList args;
2322 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002323 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002324 if (args.GetSize() > 0)
2325 {
2326 const char *open_paren = strchr (cstr, '(');
2327 const char *close_paren = NULL;
2328 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002329 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002330 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002331 {
2332 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2333 if (open_paren)
2334 close_paren = strchr (open_paren, ')');
2335 }
2336 else
2337 close_paren = strchr (open_paren, ')');
2338 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002339
2340 if (open_paren)
2341 s.Write(cstr, open_paren - cstr + 1);
2342 else
2343 {
2344 s.PutCString (cstr);
2345 s.PutChar ('(');
2346 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002347 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002348 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2349 {
Enrico Granata894f7352014-03-25 22:03:52 +00002350 std::string buffer;
2351
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002352 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2353 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
Enrico Granata894f7352014-03-25 22:03:52 +00002354 const char *var_representation = nullptr;
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002355 const char *var_name = var_value_sp->GetName().GetCString();
Enrico Granata894f7352014-03-25 22:03:52 +00002356 if (var_value_sp->GetClangType().IsAggregateType() &&
2357 DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
2358 {
2359 static StringSummaryFormat format(TypeSummaryImpl::Flags()
2360 .SetHideItemNames(false)
2361 .SetShowMembersOneLiner(true),
2362 "");
2363 format.FormatObject(var_value_sp.get(), buffer);
2364 var_representation = buffer.c_str();
2365 }
2366 else
2367 var_representation = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002368 if (arg_idx > 0)
2369 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002370 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002371 {
Enrico Granata894f7352014-03-25 22:03:52 +00002372 if (var_representation)
2373 s.Printf ("%s=%s", var_name, var_representation);
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002374 else
2375 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2376 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002377 else
2378 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002379 }
2380
2381 if (close_paren)
2382 s.PutCString (close_paren);
2383 else
2384 s.PutChar(')');
2385
2386 }
2387 else
2388 {
2389 s.PutCString(cstr);
2390 }
2391 }
2392 }
2393 else if (sc->symbol)
2394 {
2395 cstr = sc->symbol->GetName().AsCString (NULL);
2396 if (cstr)
2397 {
2398 s.PutCString(cstr);
2399 var_success = true;
2400 }
2401 }
2402 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002403 else if (IsToken (var_name_begin, "addr-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002404 {
2405 var_success = addr != NULL;
2406 if (var_success)
2407 {
2408 format_addr = *addr;
2409 calculate_format_addr_function_offset = true;
2410 }
2411 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002412 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002413 {
2414 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2415 if (var_success)
2416 {
2417 format_addr = sc->line_entry.range.GetBaseAddress();
2418 calculate_format_addr_function_offset = true;
2419 }
2420 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002421 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002422 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002423 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002424 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002425 if (var_success)
2426 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002427 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002428 calculate_format_addr_function_offset = true;
2429 }
2430 }
2431 }
2432 }
2433 break;
2434
2435 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002436 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002437 {
2438 if (sc && sc->line_entry.IsValid())
2439 {
2440 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002441 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002442 {
2443 var_name_begin += ::strlen ("file.");
2444
Michael Sartain0769b2b2013-07-30 16:44:36 +00002445 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002446 {
2447 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002448 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002449 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002450 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002451 {
2452 format_file_spec = sc->line_entry.file;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002453 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002454 }
2455 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002456 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002457 {
2458 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002459 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002460 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002461 else if ((IsToken (var_name_begin, "start-addr}")) ||
2462 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002463 {
2464 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2465 if (var_success)
2466 {
2467 format_addr = sc->line_entry.range.GetBaseAddress();
2468 if (var_name_begin[0] == 'e')
2469 format_addr.Slide (sc->line_entry.range.GetByteSize());
2470 }
2471 }
2472 }
2473 }
2474 break;
2475 }
2476
2477 if (var_success)
2478 {
2479 // If format addr is valid, then we need to print an address
2480 if (reg_num != LLDB_INVALID_REGNUM)
2481 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002482 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002483 // We have a register value to display...
2484 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2485 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002486 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002487 }
2488 else
2489 {
2490 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002491 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002492
2493 if (reg_ctx)
2494 {
2495 if (reg_kind != kNumRegisterKinds)
2496 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2497 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2498 var_success = reg_info != NULL;
2499 }
2500 }
2501 }
2502
2503 if (reg_info != NULL)
2504 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002505 RegisterValue reg_value;
2506 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2507 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002508 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002509 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002510 }
2511 }
2512
2513 if (format_file_spec)
2514 {
2515 s << format_file_spec;
2516 }
2517
2518 // If format addr is valid, then we need to print an address
2519 if (format_addr.IsValid())
2520 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002521 var_success = false;
2522
Greg Clayton1b654882010-09-19 02:33:57 +00002523 if (calculate_format_addr_function_offset)
2524 {
2525 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002526
Greg Clayton0603aa92010-10-04 01:05:56 +00002527 if (sc)
2528 {
2529 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002530 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002531 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002532 if (sc->block)
2533 {
2534 // Check to make sure we aren't in an inline
2535 // function. If we are, use the inline block
2536 // range that contains "format_addr" since
2537 // blocks can be discontiguous.
2538 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2539 AddressRange inline_range;
2540 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2541 func_addr = inline_range.GetBaseAddress();
2542 }
2543 }
Greg Claytone7612132012-03-07 21:03:09 +00002544 else if (sc->symbol && sc->symbol->ValueIsAddress())
2545 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002546 }
2547
2548 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002549 {
2550 if (func_addr.GetSection() == format_addr.GetSection())
2551 {
2552 addr_t func_file_addr = func_addr.GetFileAddress();
2553 addr_t addr_file_addr = format_addr.GetFileAddress();
2554 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002555 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002556 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002557 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002558 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002559 }
2560 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002561 {
2562 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2563 if (target)
2564 {
2565 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2566 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2567 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002568 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002569 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002570 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002571 var_success = true;
2572 }
2573 }
Greg Clayton1b654882010-09-19 02:33:57 +00002574 }
2575 }
2576 else
2577 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002578 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002579 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002580 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2581 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002582 if (vaddr == LLDB_INVALID_ADDRESS)
2583 vaddr = format_addr.GetFileAddress ();
2584
2585 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002586 {
Greg Clayton514487e2011-02-15 21:59:32 +00002587 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002588 if (addr_width == 0)
2589 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002590 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002591 var_success = true;
2592 }
Greg Clayton1b654882010-09-19 02:33:57 +00002593 }
2594 }
2595 }
2596
2597 if (var_success == false)
2598 success = false;
2599 }
2600 p = var_name_end;
2601 }
2602 else
2603 break;
2604 }
2605 else
2606 {
2607 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2608 s.PutChar(*p);
2609 }
2610 }
2611 else if (*p == '\\')
2612 {
2613 ++p; // skip the slash
2614 switch (*p)
2615 {
2616 case 'a': s.PutChar ('\a'); break;
2617 case 'b': s.PutChar ('\b'); break;
2618 case 'f': s.PutChar ('\f'); break;
2619 case 'n': s.PutChar ('\n'); break;
2620 case 'r': s.PutChar ('\r'); break;
2621 case 't': s.PutChar ('\t'); break;
2622 case 'v': s.PutChar ('\v'); break;
2623 case '\'': s.PutChar ('\''); break;
2624 case '\\': s.PutChar ('\\'); break;
2625 case '0':
2626 // 1 to 3 octal chars
2627 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002628 // Make a string that can hold onto the initial zero char,
2629 // up to 3 octal digits, and a terminating NULL.
2630 char oct_str[5] = { 0, 0, 0, 0, 0 };
2631
2632 int i;
2633 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2634 oct_str[i] = p[i];
2635
2636 // We don't want to consume the last octal character since
2637 // the main for loop will do this for us, so we advance p by
2638 // one less than i (even if i is zero)
2639 p += i - 1;
2640 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2641 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002642 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002643 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002644 }
Greg Clayton1b654882010-09-19 02:33:57 +00002645 }
2646 break;
2647
2648 case 'x':
2649 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002650 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002651 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002652 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002653
Greg Clayton0603aa92010-10-04 01:05:56 +00002654 // Make a string that can hold onto two hex chars plus a
2655 // NULL terminator
2656 char hex_str[3] = { 0,0,0 };
2657 hex_str[0] = *p;
2658 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002659 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002660 ++p; // Skip the first of the two hex chars
2661 hex_str[1] = *p;
2662 }
2663
2664 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2665 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002666 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002667 }
2668 else
2669 {
2670 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002671 }
2672 break;
2673
2674 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002675 // Just desensitize any other character by just printing what
2676 // came after the '\'
2677 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002678 break;
2679
2680 }
2681
2682 }
2683 }
2684 if (end)
2685 *end = p;
2686 return success;
2687}
2688
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002689bool
2690Debugger::FormatPrompt
2691(
2692 const char *format,
2693 const SymbolContext *sc,
2694 const ExecutionContext *exe_ctx,
2695 const Address *addr,
2696 Stream &s,
2697 ValueObject* valobj
2698)
2699{
2700 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2701 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2702 if (format_str.length())
2703 format = format_str.c_str();
2704 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
2705}
2706
Jim Ingham228063c2012-02-21 02:23:08 +00002707void
2708Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2709{
Jim Ingham4f02b222012-02-22 22:49:20 +00002710 // For simplicity's sake, I am not going to deal with how to close down any
2711 // open logging streams, I just redirect everything from here on out to the
2712 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002713 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2714}
2715
2716bool
2717Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2718{
2719 Log::Callbacks log_callbacks;
2720
2721 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002722 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002723 {
2724 log_stream_sp = m_log_callback_stream_sp;
2725 // For now when using the callback mode you always get thread & timestamp.
2726 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2727 }
2728 else if (log_file == NULL || *log_file == '\0')
2729 {
Greg Clayton44d93782014-01-27 23:43:24 +00002730 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00002731 }
2732 else
2733 {
2734 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002735 if (pos != m_log_streams.end())
2736 log_stream_sp = pos->second.lock();
2737 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002738 {
2739 log_stream_sp.reset (new StreamFile (log_file));
2740 m_log_streams[log_file] = log_stream_sp;
2741 }
Jim Ingham228063c2012-02-21 02:23:08 +00002742 }
2743 assert (log_stream_sp.get());
2744
2745 if (log_options == 0)
2746 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2747
Greg Clayton57abc5d2013-05-10 21:47:16 +00002748 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002749 {
2750 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2751 return true;
2752 }
2753 else
2754 {
2755 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2756 if (log_channel_sp)
2757 {
2758 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2759 {
2760 return true;
2761 }
2762 else
2763 {
2764 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2765 return false;
2766 }
2767 }
2768 else
2769 {
2770 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2771 return false;
2772 }
2773 }
2774 return false;
2775}
2776
Greg Clayton9585fbf2013-03-19 00:20:55 +00002777SourceManager &
2778Debugger::GetSourceManager ()
2779{
2780 if (m_source_manager_ap.get() == NULL)
2781 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2782 return *m_source_manager_ap;
2783}
2784
2785
Greg Clayton44d93782014-01-27 23:43:24 +00002786
2787// This function handles events that were broadcast by the process.
2788void
2789Debugger::HandleBreakpointEvent (const EventSP &event_sp)
2790{
2791 using namespace lldb;
2792 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
2793
2794// if (event_type & eBreakpointEventTypeAdded
2795// || event_type & eBreakpointEventTypeRemoved
2796// || event_type & eBreakpointEventTypeEnabled
2797// || event_type & eBreakpointEventTypeDisabled
2798// || event_type & eBreakpointEventTypeCommandChanged
2799// || event_type & eBreakpointEventTypeConditionChanged
2800// || event_type & eBreakpointEventTypeIgnoreChanged
2801// || event_type & eBreakpointEventTypeLocationsResolved)
2802// {
2803// // Don't do anything about these events, since the breakpoint commands already echo these actions.
2804// }
2805//
2806 if (event_type & eBreakpointEventTypeLocationsAdded)
2807 {
2808 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
2809 if (num_new_locations > 0)
2810 {
2811 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
2812 StreamFileSP output_sp (GetOutputFile());
2813 if (output_sp)
2814 {
2815 output_sp->Printf("%d location%s added to breakpoint %d\n",
2816 num_new_locations,
2817 num_new_locations == 1 ? "" : "s",
2818 breakpoint->GetID());
2819 RefreshTopIOHandler();
2820 }
2821 }
2822 }
2823// else if (event_type & eBreakpointEventTypeLocationsRemoved)
2824// {
2825// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
2826// }
2827// else if (event_type & eBreakpointEventTypeLocationsResolved)
2828// {
2829// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
2830// }
2831}
2832
2833size_t
2834Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
2835{
2836 size_t total_bytes = 0;
2837 if (stream == NULL)
2838 stream = GetOutputFile().get();
2839
2840 if (stream)
2841 {
2842 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
2843 if (process == NULL)
2844 {
2845 TargetSP target_sp = GetTargetList().GetSelectedTarget();
2846 if (target_sp)
2847 process = target_sp->GetProcessSP().get();
2848 }
2849 if (process)
2850 {
2851 Error error;
2852 size_t len;
2853 char stdio_buffer[1024];
2854 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2855 {
2856 stream->Write(stdio_buffer, len);
2857 total_bytes += len;
2858 }
2859 }
2860 stream->Flush();
2861 }
2862 return total_bytes;
2863}
2864
2865size_t
2866Debugger::GetProcessSTDERR (Process *process, Stream *stream)
2867{
2868 size_t total_bytes = 0;
2869 if (stream == NULL)
2870 stream = GetOutputFile().get();
2871
2872 if (stream)
2873 {
2874 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2875 if (process == NULL)
2876 {
2877 TargetSP target_sp = GetTargetList().GetSelectedTarget();
2878 if (target_sp)
2879 process = target_sp->GetProcessSP().get();
2880 }
2881 if (process)
2882 {
2883 Error error;
2884 size_t len;
2885 char stdio_buffer[1024];
2886 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2887 {
2888 stream->Write(stdio_buffer, len);
2889 total_bytes += len;
2890 }
2891 }
2892 stream->Flush();
2893 }
2894 return total_bytes;
2895}
2896
2897// This function handles events that were broadcast by the process.
2898void
2899Debugger::HandleProcessEvent (const EventSP &event_sp)
2900{
2901 using namespace lldb;
2902 const uint32_t event_type = event_sp->GetType();
2903 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
2904
Greg Claytonb4874f12014-02-28 18:22:24 +00002905 StreamString output_stream;
2906 StreamString error_stream;
Greg Clayton44d93782014-01-27 23:43:24 +00002907 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00002908
Greg Claytonb4874f12014-02-28 18:22:24 +00002909 if (!gui_enabled)
2910 {
2911 bool pop_process_io_handler = false;
2912 assert (process_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00002913
Greg Claytonb4874f12014-02-28 18:22:24 +00002914 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
Greg Clayton44d93782014-01-27 23:43:24 +00002915 {
Greg Claytonb4874f12014-02-28 18:22:24 +00002916 GetProcessSTDOUT (process_sp.get(), &output_stream);
2917 }
2918
2919 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
2920 {
2921 GetProcessSTDERR (process_sp.get(), &error_stream);
2922 }
2923
2924 if (event_type & Process::eBroadcastBitStateChanged)
2925 {
2926
2927 // Drain all stout and stderr so we don't see any output come after
2928 // we print our prompts
Greg Clayton44d93782014-01-27 23:43:24 +00002929 // Something changed in the process; get the event and report the process's current status and location to
2930 // the user.
2931 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
2932 if (event_state == eStateInvalid)
2933 return;
2934
2935 switch (event_state)
2936 {
2937 case eStateInvalid:
2938 case eStateUnloaded:
2939 case eStateConnected:
2940 case eStateAttaching:
2941 case eStateLaunching:
2942 case eStateStepping:
2943 case eStateDetached:
2944 {
Greg Claytonb4874f12014-02-28 18:22:24 +00002945 output_stream.Printf("Process %" PRIu64 " %s\n",
2946 process_sp->GetID(),
2947 StateAsCString (event_state));
2948
2949 if (event_state == eStateDetached)
2950 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00002951 }
2952 break;
2953
2954 case eStateRunning:
2955 // Don't be chatty when we run...
2956 break;
2957
2958 case eStateExited:
Greg Claytonb4874f12014-02-28 18:22:24 +00002959 process_sp->GetStatus(output_stream);
2960 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00002961 break;
2962
2963 case eStateStopped:
2964 case eStateCrashed:
2965 case eStateSuspended:
2966 // Make sure the program hasn't been auto-restarted:
2967 if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get()))
2968 {
2969 size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get());
2970 if (num_reasons > 0)
2971 {
2972 // FIXME: Do we want to report this, or would that just be annoyingly chatty?
2973 if (num_reasons == 1)
2974 {
2975 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0);
Greg Claytonb4874f12014-02-28 18:22:24 +00002976 output_stream.Printf("Process %" PRIu64 " stopped and restarted: %s\n",
2977 process_sp->GetID(),
2978 reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00002979 }
2980 else
2981 {
Greg Claytonb4874f12014-02-28 18:22:24 +00002982 output_stream.Printf("Process %" PRIu64 " stopped and restarted, reasons:\n",
2983 process_sp->GetID());
Greg Clayton44d93782014-01-27 23:43:24 +00002984
2985
2986 for (size_t i = 0; i < num_reasons; i++)
2987 {
2988 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i);
Greg Claytonb4874f12014-02-28 18:22:24 +00002989 output_stream.Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00002990 }
2991 }
2992 }
2993 }
2994 else
2995 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002996 // 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 +00002997 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00002998 ThreadList &thread_list = process_sp->GetThreadList();
2999 Mutex::Locker locker (thread_list.GetMutex());
3000
3001 ThreadSP curr_thread (thread_list.GetSelectedThread());
3002 ThreadSP thread;
3003 StopReason curr_thread_stop_reason = eStopReasonInvalid;
3004 if (curr_thread)
3005 curr_thread_stop_reason = curr_thread->GetStopReason();
3006 if (!curr_thread ||
3007 !curr_thread->IsValid() ||
3008 curr_thread_stop_reason == eStopReasonInvalid ||
3009 curr_thread_stop_reason == eStopReasonNone)
Greg Clayton44d93782014-01-27 23:43:24 +00003010 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003011 // Prefer a thread that has just completed its plan over another thread as current thread.
3012 ThreadSP plan_thread;
3013 ThreadSP other_thread;
3014 const size_t num_threads = thread_list.GetSize();
3015 size_t i;
3016 for (i = 0; i < num_threads; ++i)
Greg Clayton44d93782014-01-27 23:43:24 +00003017 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003018 thread = thread_list.GetThreadAtIndex(i);
3019 StopReason thread_stop_reason = thread->GetStopReason();
3020 switch (thread_stop_reason)
3021 {
3022 case eStopReasonInvalid:
3023 case eStopReasonNone:
3024 break;
3025
3026 case eStopReasonTrace:
3027 case eStopReasonBreakpoint:
3028 case eStopReasonWatchpoint:
3029 case eStopReasonSignal:
3030 case eStopReasonException:
3031 case eStopReasonExec:
3032 case eStopReasonThreadExiting:
3033 if (!other_thread)
3034 other_thread = thread;
3035 break;
3036 case eStopReasonPlanComplete:
3037 if (!plan_thread)
3038 plan_thread = thread;
3039 break;
3040 }
3041 }
3042 if (plan_thread)
3043 thread_list.SetSelectedThreadByID (plan_thread->GetID());
3044 else if (other_thread)
3045 thread_list.SetSelectedThreadByID (other_thread->GetID());
3046 else
3047 {
3048 if (curr_thread && curr_thread->IsValid())
3049 thread = curr_thread;
3050 else
3051 thread = thread_list.GetThreadAtIndex(0);
3052
3053 if (thread)
3054 thread_list.SetSelectedThreadByID (thread->GetID());
Greg Clayton44d93782014-01-27 23:43:24 +00003055 }
3056 }
Greg Clayton44d93782014-01-27 23:43:24 +00003057 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00003058 // Drop the ThreadList mutex by here, since GetThreadStatus below might have to run code,
3059 // e.g. for Data formatters, and if we hold the ThreadList mutex, then the process is going to
3060 // have a hard time restarting the process.
Greg Clayton44d93782014-01-27 23:43:24 +00003061
3062 if (GetTargetList().GetSelectedTarget().get() == &process_sp->GetTarget())
3063 {
3064 const bool only_threads_with_stop_reason = true;
3065 const uint32_t start_frame = 0;
3066 const uint32_t num_frames = 1;
3067 const uint32_t num_frames_with_source = 1;
Greg Claytonb4874f12014-02-28 18:22:24 +00003068 process_sp->GetStatus(output_stream);
3069 process_sp->GetThreadStatus (output_stream,
Greg Clayton44d93782014-01-27 23:43:24 +00003070 only_threads_with_stop_reason,
3071 start_frame,
3072 num_frames,
3073 num_frames_with_source);
3074 }
3075 else
3076 {
3077 uint32_t target_idx = GetTargetList().GetIndexOfTarget(process_sp->GetTarget().shared_from_this());
3078 if (target_idx != UINT32_MAX)
Greg Claytonb4874f12014-02-28 18:22:24 +00003079 output_stream.Printf ("Target %d: (", target_idx);
Greg Clayton44d93782014-01-27 23:43:24 +00003080 else
Greg Claytonb4874f12014-02-28 18:22:24 +00003081 output_stream.Printf ("Target <unknown index>: (");
3082 process_sp->GetTarget().Dump (&output_stream, eDescriptionLevelBrief);
3083 output_stream.Printf (") stopped.\n");
Greg Clayton44d93782014-01-27 23:43:24 +00003084 }
Greg Claytonb4874f12014-02-28 18:22:24 +00003085
3086 // Pop the process IO handler
3087 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00003088 }
3089 break;
3090 }
3091 }
Greg Clayton44d93782014-01-27 23:43:24 +00003092
Greg Claytonb4874f12014-02-28 18:22:24 +00003093 if (output_stream.GetSize() || error_stream.GetSize())
3094 {
3095 StreamFileSP error_stream_sp (GetOutputFile());
Greg Clayton6fea17e2014-03-03 19:15:20 +00003096 bool top_io_handler_hid = false;
3097
3098 if (process_sp->ProcessIOHandlerIsActive() == false)
3099 top_io_handler_hid = HideTopIOHandler();
Greg Claytonb4874f12014-02-28 18:22:24 +00003100
3101 if (output_stream.GetSize())
3102 {
3103 StreamFileSP output_stream_sp (GetOutputFile());
3104 if (output_stream_sp)
3105 output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
3106 }
3107
3108 if (error_stream.GetSize())
3109 {
3110 StreamFileSP error_stream_sp (GetErrorFile());
3111 if (error_stream_sp)
3112 error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
3113 }
3114
3115 if (top_io_handler_hid)
3116 RefreshTopIOHandler();
3117 }
3118
3119 if (pop_process_io_handler)
3120 process_sp->PopProcessIOHandler();
3121 }
Greg Clayton44d93782014-01-27 23:43:24 +00003122}
3123
3124void
3125Debugger::HandleThreadEvent (const EventSP &event_sp)
3126{
3127 // At present the only thread event we handle is the Frame Changed event,
3128 // and all we do for that is just reprint the thread status for that thread.
3129 using namespace lldb;
3130 const uint32_t event_type = event_sp->GetType();
3131 if (event_type == Thread::eBroadcastBitStackChanged ||
3132 event_type == Thread::eBroadcastBitThreadSelected )
3133 {
3134 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
3135 if (thread_sp)
3136 {
3137 HideTopIOHandler();
3138 StreamFileSP stream_sp (GetOutputFile());
3139 thread_sp->GetStatus(*stream_sp, 0, 1, 1);
3140 RefreshTopIOHandler();
3141 }
3142 }
3143}
3144
3145bool
3146Debugger::IsForwardingEvents ()
3147{
3148 return (bool)m_forward_listener_sp;
3149}
3150
3151void
3152Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
3153{
3154 m_forward_listener_sp = listener_sp;
3155}
3156
3157void
3158Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
3159{
3160 m_forward_listener_sp.reset();
3161}
3162
3163
3164void
3165Debugger::DefaultEventHandler()
3166{
3167 Listener& listener(GetListener());
3168 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
3169 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
3170 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
3171 BroadcastEventSpec target_event_spec (broadcaster_class_target,
3172 Target::eBroadcastBitBreakpointChanged);
3173
3174 BroadcastEventSpec process_event_spec (broadcaster_class_process,
3175 Process::eBroadcastBitStateChanged |
3176 Process::eBroadcastBitSTDOUT |
3177 Process::eBroadcastBitSTDERR);
3178
3179 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
3180 Thread::eBroadcastBitStackChanged |
3181 Thread::eBroadcastBitThreadSelected );
3182
3183 listener.StartListeningForEventSpec (*this, target_event_spec);
3184 listener.StartListeningForEventSpec (*this, process_event_spec);
3185 listener.StartListeningForEventSpec (*this, thread_event_spec);
3186 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
3187 CommandInterpreter::eBroadcastBitQuitCommandReceived |
3188 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
3189 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
3190
3191 bool done = false;
3192 while (!done)
3193 {
3194// Mutex::Locker locker;
3195// if (locker.TryLock(m_input_reader_stack.GetMutex()))
3196// {
3197// if (m_input_reader_stack.IsEmpty())
3198// break;
3199// }
3200//
3201 EventSP event_sp;
3202 if (listener.WaitForEvent(NULL, event_sp))
3203 {
3204 if (event_sp)
3205 {
3206 Broadcaster *broadcaster = event_sp->GetBroadcaster();
3207 if (broadcaster)
3208 {
3209 uint32_t event_type = event_sp->GetType();
3210 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
3211 if (broadcaster_class == broadcaster_class_process)
3212 {
3213 HandleProcessEvent (event_sp);
3214 }
3215 else if (broadcaster_class == broadcaster_class_target)
3216 {
3217 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
3218 {
3219 HandleBreakpointEvent (event_sp);
3220 }
3221 }
3222 else if (broadcaster_class == broadcaster_class_thread)
3223 {
3224 HandleThreadEvent (event_sp);
3225 }
3226 else if (broadcaster == m_command_interpreter_ap.get())
3227 {
3228 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
3229 {
3230 done = true;
3231 }
3232 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
3233 {
3234 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3235 if (data && data[0])
3236 {
3237 StreamFileSP error_sp (GetErrorFile());
3238 if (error_sp)
3239 {
3240 HideTopIOHandler();
3241 error_sp->PutCString(data);
3242 error_sp->Flush();
3243 RefreshTopIOHandler();
3244 }
3245 }
3246 }
3247 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
3248 {
3249 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3250 if (data && data[0])
3251 {
3252 StreamFileSP output_sp (GetOutputFile());
3253 if (output_sp)
3254 {
3255 HideTopIOHandler();
3256 output_sp->PutCString(data);
3257 output_sp->Flush();
3258 RefreshTopIOHandler();
3259 }
3260 }
3261 }
3262 }
3263 }
3264
3265 if (m_forward_listener_sp)
3266 m_forward_listener_sp->AddEvent(event_sp);
3267 }
3268 }
3269 }
3270}
3271
3272lldb::thread_result_t
3273Debugger::EventHandlerThread (lldb::thread_arg_t arg)
3274{
3275 ((Debugger *)arg)->DefaultEventHandler();
3276 return NULL;
3277}
3278
3279bool
3280Debugger::StartEventHandlerThread()
3281{
3282 if (!IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3283 m_event_handler_thread = Host::ThreadCreate("lldb.debugger.event-handler", EventHandlerThread, this, NULL);
3284 return IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread);
3285}
3286
3287void
3288Debugger::StopEventHandlerThread()
3289{
3290 if (IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3291 {
3292 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
3293 Host::ThreadJoin(m_event_handler_thread, NULL, NULL);
3294 m_event_handler_thread = LLDB_INVALID_HOST_THREAD;
3295 }
3296}
3297
3298
3299lldb::thread_result_t
3300Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3301{
3302 Debugger *debugger = (Debugger *)arg;
3303 debugger->ExecuteIOHanders();
3304 debugger->StopEventHandlerThread();
3305 return NULL;
3306}
3307
3308bool
3309Debugger::StartIOHandlerThread()
3310{
3311 if (!IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3312 m_io_handler_thread = Host::ThreadCreate("lldb.debugger.io-handler", IOHandlerThread, this, NULL);
3313 return IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread);
3314}
3315
3316void
3317Debugger::StopIOHandlerThread()
3318{
3319 if (IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3320 {
3321 if (m_input_file_sp)
3322 m_input_file_sp->GetFile().Close();
3323 Host::ThreadJoin(m_io_handler_thread, NULL, NULL);
3324 m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
3325 }
3326}
3327
3328