blob: cc75a1e2dec3e2483b385305cadd2f1ae101a7fc [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"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/Module.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000022#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000023#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000025#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000026#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000027#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000028#include "lldb/Core/StreamString.h"
Jason Molenda705b1802014-06-13 02:37:02 +000029#include "lldb/Core/StructuredData.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000031#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000032#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000033#include "lldb/DataFormatters/DataVisualization.h"
34#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata894f7352014-03-25 22:03:52 +000035#include "lldb/DataFormatters/TypeSummary.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000036#include "lldb/Host/ConnectionFileDescriptor.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000037#include "lldb/Host/HostInfo.h"
Greg Claytona3406612011-02-07 23:24:47 +000038#include "lldb/Host/Terminal.h"
Zachary Turner39de3112014-09-09 20:54:56 +000039#include "lldb/Host/ThreadLauncher.h"
Greg Clayton66111032010-06-23 01:19:29 +000040#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000041#include "lldb/Interpreter/OptionValueSInt64.h"
42#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000043#include "lldb/Symbol/ClangASTContext.h"
44#include "lldb/Symbol/CompileUnit.h"
45#include "lldb/Symbol/Function.h"
46#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000047#include "lldb/Symbol/VariableList.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000048#include "lldb/Target/CPPLanguageRuntime.h"
49#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Target/TargetList.h"
51#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000052#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000053#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000054#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000055#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000057#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
Zachary Turner58a559c2014-08-27 20:15:09 +000059#include "llvm/Support/DynamicLibrary.h"
60
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061using namespace lldb;
62using namespace lldb_private;
63
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
Greg Clayton1b654882010-09-19 02:33:57 +000065static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000066static lldb::user_id_t g_unique_id = 1;
Zachary Turner7c2896a2014-10-24 22:06:29 +000067static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000068
Greg Clayton1b654882010-09-19 02:33:57 +000069#pragma mark Static Functions
70
71static Mutex &
72GetDebuggerListMutex ()
73{
74 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
75 return g_mutex;
76}
77
78typedef std::vector<DebuggerSP> DebuggerList;
79
80static DebuggerList &
81GetDebuggerList()
82{
83 // hide the static debugger list inside a singleton accessor to avoid
Bruce Mitchener6a7f3332014-06-27 02:42:12 +000084 // global init constructors
Greg Clayton1b654882010-09-19 02:33:57 +000085 static DebuggerList g_list;
86 return g_list;
87}
Greg Claytone372b982011-11-21 21:44:34 +000088
89OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000090g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000091{
Greg Clayton67cc0632012-08-22 17:17:09 +000092 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
93 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
94 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000095 { 0, NULL, NULL }
96};
97
Greg Clayton67cc0632012-08-22 17:17:09 +000098OptionEnumValueElement
99g_language_enumerators[] =
100{
101 { eScriptLanguageNone, "none", "Disable scripting languages."},
102 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
103 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +0000104 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000105};
Greg Claytone372b982011-11-21 21:44:34 +0000106
Greg Clayton67cc0632012-08-22 17:17:09 +0000107#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
108#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
109
Michael Sartain0769b2b2013-07-30 16:44:36 +0000110#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000111 "{, ${frame.pc}}"\
112 MODULE_WITH_FUNC\
113 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000114 "{, name = '${thread.name}'}"\
115 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000116 "{, activity = '${thread.info.activity.name}'}" \
117 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000118 "{, stop reason = ${thread.stop-reason}}"\
119 "{\\nReturn value: ${thread.return-value}}"\
Jim Ingham30fadaf2014-07-08 01:07:32 +0000120 "{\\nCompleted expression: ${thread.completed-expression}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000121 "\\n"
122
123#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
124 MODULE_WITH_FUNC\
125 FILE_AND_LINE\
126 "\\n"
127
Jason Molenda0abae872014-10-31 03:40:06 +0000128#define DEFAULT_DISASSEMBLY_FORMAT "${addr-file-or-load}{ <${function.name-without-args}${function.concrete-only-addr-offset-no-padding}>}: "
Greg Clayton67cc0632012-08-22 17:17:09 +0000129
Greg Clayton754a9362012-08-23 00:22:02 +0000130static PropertyDefinition
131g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000132{
133{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
Jason Molendaaff1b352014-10-10 23:07:36 +0000134{ "disassembly-format", OptionValue::eTypeString , true, 0 , DEFAULT_DISASSEMBLY_FORMAT, NULL, "The default disassembly format string to use when disassembling instruction sequences." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000135{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
136{ "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 +0000137{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000138{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
139{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
140{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
141{ "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." },
142{ "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." },
143{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
144{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
145{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000146{ "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000147{ "auto-one-line-summaries", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will automatically display small structs in one-liner format (default: true)." },
148{ "escape-non-printables", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will automatically escape non-printable and escape characters when formatting strings." },
Greg Claytone8cd0c92012-10-19 18:02:49 +0000149
150 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000151};
152
153enum
154{
155 ePropertyAutoConfirm = 0,
Jason Molendaaff1b352014-10-10 23:07:36 +0000156 ePropertyDisassemblyFormat,
Greg Clayton67cc0632012-08-22 17:17:09 +0000157 ePropertyFrameFormat,
158 ePropertyNotiftVoid,
159 ePropertyPrompt,
160 ePropertyScriptLanguage,
161 ePropertyStopDisassemblyCount,
162 ePropertyStopDisassemblyDisplay,
163 ePropertyStopLineCountAfter,
164 ePropertyStopLineCountBefore,
165 ePropertyTerminalWidth,
166 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000167 ePropertyUseExternalEditor,
168 ePropertyUseColor,
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000169 ePropertyAutoOneLineSummaries,
170 ePropertyEscapeNonPrintables
Greg Clayton67cc0632012-08-22 17:17:09 +0000171};
172
Greg Clayton5fb8f792013-12-02 19:35:49 +0000173Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000174
175Error
176Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
177 VarSetOperationType op,
178 const char *property_path,
179 const char *value)
180{
Enrico Granata84a53df2013-05-20 22:29:23 +0000181 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000182 bool is_escape_non_printables = strcmp(property_path, "escape-non-printables") == 0;
Enrico Granata84a53df2013-05-20 22:29:23 +0000183 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000184 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000185 if (is_load_script && exe_ctx->GetTargetSP())
186 {
187 target_sp = exe_ctx->GetTargetSP();
188 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
189 }
Greg Clayton4c054102012-09-01 00:38:36 +0000190 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
191 if (error.Success())
192 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000193 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000194 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
195 {
196 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000197 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
198 if (str.length())
199 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000200 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000201 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
202 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
203 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000204 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
205 {
206 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
207 SetPrompt (GetPrompt());
208 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000209 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000210 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000211 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000212 {
213 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000214 StreamString feedback_stream;
215 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000216 {
Greg Clayton44d93782014-01-27 23:43:24 +0000217 StreamFileSP stream_sp (GetErrorFile());
218 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000219 {
Greg Clayton44d93782014-01-27 23:43:24 +0000220 for (auto error : errors)
221 {
222 stream_sp->Printf("%s\n",error.AsCString());
223 }
224 if (feedback_stream.GetSize())
225 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000226 }
227 }
228 }
229 }
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000230 else if (is_escape_non_printables)
231 {
232 DataVisualization::ForceUpdate();
233 }
Greg Clayton4c054102012-09-01 00:38:36 +0000234 }
235 return error;
236}
237
Greg Clayton67cc0632012-08-22 17:17:09 +0000238bool
239Debugger::GetAutoConfirm () const
240{
241 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000242 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000243}
244
245const char *
Jason Molendaaff1b352014-10-10 23:07:36 +0000246Debugger::GetDisassemblyFormat() const
247{
248 const uint32_t idx = ePropertyDisassemblyFormat;
249 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
250}
251
252const char *
Greg Clayton67cc0632012-08-22 17:17:09 +0000253Debugger::GetFrameFormat() const
254{
255 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000256 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000257}
258
259bool
260Debugger::GetNotifyVoid () const
261{
262 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000263 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000264}
265
266const char *
267Debugger::GetPrompt() const
268{
269 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000270 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000271}
272
273void
274Debugger::SetPrompt(const char *p)
275{
276 const uint32_t idx = ePropertyPrompt;
277 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
278 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000279 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
280 if (str.length())
281 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000282 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000283}
284
285const char *
286Debugger::GetThreadFormat() const
287{
288 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000289 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000290}
291
292lldb::ScriptLanguage
293Debugger::GetScriptLanguage() const
294{
295 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000296 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000297}
298
299bool
300Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
301{
302 const uint32_t idx = ePropertyScriptLanguage;
303 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
304}
305
306uint32_t
307Debugger::GetTerminalWidth () const
308{
309 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000310 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000311}
312
313bool
314Debugger::SetTerminalWidth (uint32_t term_width)
315{
316 const uint32_t idx = ePropertyTerminalWidth;
317 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
318}
319
320bool
321Debugger::GetUseExternalEditor () const
322{
323 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000324 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000325}
326
327bool
328Debugger::SetUseExternalEditor (bool b)
329{
330 const uint32_t idx = ePropertyUseExternalEditor;
331 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
332}
333
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000334bool
335Debugger::GetUseColor () const
336{
337 const uint32_t idx = ePropertyUseColor;
338 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
339}
340
341bool
342Debugger::SetUseColor (bool b)
343{
344 const uint32_t idx = ePropertyUseColor;
345 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
346 SetPrompt (GetPrompt());
347 return ret;
348}
349
Greg Clayton67cc0632012-08-22 17:17:09 +0000350uint32_t
351Debugger::GetStopSourceLineCount (bool before) const
352{
353 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000354 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000355}
356
357Debugger::StopDisassemblyType
358Debugger::GetStopDisassemblyDisplay () const
359{
360 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000361 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000362}
363
364uint32_t
365Debugger::GetDisassemblyLineCount () const
366{
367 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000368 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000369}
Greg Claytone372b982011-11-21 21:44:34 +0000370
Enrico Granata553fad52013-10-25 23:09:40 +0000371bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000372Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000373{
Enrico Granata90a8db32013-10-31 21:01:07 +0000374 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000375 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000376}
Enrico Granata553fad52013-10-25 23:09:40 +0000377
Enrico Granataebdc1ac2014-11-05 21:20:48 +0000378bool
379Debugger::GetEscapeNonPrintables () const
380{
381 const uint32_t idx = ePropertyEscapeNonPrintables;
382 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
Enrico Granata553fad52013-10-25 23:09:40 +0000383}
384
Greg Clayton1b654882010-09-19 02:33:57 +0000385#pragma mark Debugger
386
Greg Clayton67cc0632012-08-22 17:17:09 +0000387//const DebuggerPropertiesSP &
388//Debugger::GetSettings() const
389//{
390// return m_properties_sp;
391//}
392//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000393
Caroline Tice2f88aad2011-01-14 00:29:16 +0000394int
395Debugger::TestDebuggerRefCount ()
396{
397 return g_shared_debugger_refcount;
398}
399
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400void
Greg Clayton5fb8f792013-12-02 19:35:49 +0000401Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000403 g_load_plugin_callback = load_plugin_callback;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000404 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000405 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406}
407
408void
409Debugger::Terminate ()
410{
Greg Clayton66111032010-06-23 01:19:29 +0000411 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 {
Greg Clayton66111032010-06-23 01:19:29 +0000413 g_shared_debugger_refcount--;
414 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 {
Greg Claytondbe54502010-11-19 03:46:01 +0000416 lldb_private::WillTerminate();
417 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000418
419 // Clear our master list of debugger objects
420 Mutex::Locker locker (GetDebuggerListMutex ());
421 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 }
424}
425
Caroline Tice20bd37f2011-03-10 22:14:10 +0000426void
427Debugger::SettingsInitialize ()
428{
Greg Clayton6920b522012-08-22 18:39:03 +0000429 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000430}
431
432void
433Debugger::SettingsTerminate ()
434{
Greg Clayton6920b522012-08-22 18:39:03 +0000435 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000436}
437
Enrico Granata21dfcd92012-09-28 23:57:51 +0000438bool
Enrico Granatae743c782013-04-24 21:29:08 +0000439Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000440{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000441 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000442 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000443 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
444 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000445 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000446 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000447 return true;
448 }
Enrico Granatae743c782013-04-24 21:29:08 +0000449 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000450 else
Enrico Granatae743c782013-04-24 21:29:08 +0000451 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000452 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
453 // and if the public API layer isn't available (code is linking against
454 // all of the internal LLDB static libraries), then we can't load plugins
455 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000456 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000457 return false;
458}
459
460static FileSpec::EnumerateDirectoryResult
461LoadPluginCallback
462(
463 void *baton,
464 FileSpec::FileType file_type,
465 const FileSpec &file_spec
466 )
467{
468 Error error;
469
470 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000471 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000472
473 if (!baton)
474 return FileSpec::eEnumerateDirectoryResultQuit;
475
476 Debugger *debugger = (Debugger*)baton;
477
478 // If we have a regular file, a symbolic link or unknown file type, try
479 // and process the file. We must handle unknown as sometimes the directory
480 // enumeration might be enumerating a file system that doesn't have correct
481 // file type information.
482 if (file_type == FileSpec::eFileTypeRegular ||
483 file_type == FileSpec::eFileTypeSymbolicLink ||
484 file_type == FileSpec::eFileTypeUnknown )
485 {
486 FileSpec plugin_file_spec (file_spec);
487 plugin_file_spec.ResolvePath ();
488
Michael Sartain3cf443d2013-07-17 00:26:30 +0000489 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
490 plugin_file_spec.GetFileNameExtension() != g_solibext)
491 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000492 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000493 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000494
Enrico Granatae743c782013-04-24 21:29:08 +0000495 Error plugin_load_error;
496 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000497
498 return FileSpec::eEnumerateDirectoryResultNext;
499 }
500
501 else if (file_type == FileSpec::eFileTypeUnknown ||
502 file_type == FileSpec::eFileTypeDirectory ||
503 file_type == FileSpec::eFileTypeSymbolicLink )
504 {
505 // Try and recurse into anything that a directory or symbolic link.
506 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000507 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000508 // information.
509 return FileSpec::eEnumerateDirectoryResultEnter;
510 }
511
512 return FileSpec::eEnumerateDirectoryResultNext;
513}
514
515void
516Debugger::InstanceInitialize ()
517{
518 FileSpec dir_spec;
519 const bool find_directories = true;
520 const bool find_files = true;
521 const bool find_other = true;
522 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000523 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000524 {
525 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
526 {
527 FileSpec::EnumerateDirectory (dir_path,
528 find_directories,
529 find_files,
530 find_other,
531 LoadPluginCallback,
532 this);
533 }
534 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000535
536 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000537 {
538 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
539 {
540 FileSpec::EnumerateDirectory (dir_path,
541 find_directories,
542 find_files,
543 find_other,
544 LoadPluginCallback,
545 this);
546 }
547 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000548
549 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000550}
551
Greg Clayton66111032010-06-23 01:19:29 +0000552DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000553Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000554{
Jim Ingham228063c2012-02-21 02:23:08 +0000555 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000556 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000557 {
558 Mutex::Locker locker (GetDebuggerListMutex ());
559 GetDebuggerList().push_back(debugger_sp);
560 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000561 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000562 return debugger_sp;
563}
564
Caroline Ticee02657b2011-01-22 01:02:07 +0000565void
Greg Clayton4d122c42011-09-17 08:33:22 +0000566Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000567{
568 if (debugger_sp.get() == NULL)
569 return;
570
Jim Ingham8314c522011-09-15 21:36:42 +0000571 debugger_sp->Clear();
572
Greg Claytonc15f55e2012-03-30 20:53:46 +0000573 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000574 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000575 Mutex::Locker locker (GetDebuggerListMutex ());
576 DebuggerList &debugger_list = GetDebuggerList ();
577 DebuggerList::iterator pos, end = debugger_list.end();
578 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000579 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000580 if ((*pos).get() == debugger_sp.get())
581 {
582 debugger_list.erase (pos);
583 return;
584 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000585 }
586 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000587}
588
Greg Clayton4d122c42011-09-17 08:33:22 +0000589DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000590Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
591{
Greg Clayton4d122c42011-09-17 08:33:22 +0000592 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000593 if (g_shared_debugger_refcount > 0)
594 {
595 Mutex::Locker locker (GetDebuggerListMutex ());
596 DebuggerList &debugger_list = GetDebuggerList();
597 DebuggerList::iterator pos, end = debugger_list.end();
598
599 for (pos = debugger_list.begin(); pos != end; ++pos)
600 {
601 if ((*pos).get()->m_instance_name == instance_name)
602 {
603 debugger_sp = *pos;
604 break;
605 }
606 }
607 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000608 return debugger_sp;
609}
Greg Clayton66111032010-06-23 01:19:29 +0000610
611TargetSP
612Debugger::FindTargetWithProcessID (lldb::pid_t pid)
613{
Greg Clayton4d122c42011-09-17 08:33:22 +0000614 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000615 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000616 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000617 Mutex::Locker locker (GetDebuggerListMutex ());
618 DebuggerList &debugger_list = GetDebuggerList();
619 DebuggerList::iterator pos, end = debugger_list.end();
620 for (pos = debugger_list.begin(); pos != end; ++pos)
621 {
622 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
623 if (target_sp)
624 break;
625 }
Greg Clayton66111032010-06-23 01:19:29 +0000626 }
627 return target_sp;
628}
629
Greg Claytone4e45922011-11-16 05:37:56 +0000630TargetSP
631Debugger::FindTargetWithProcess (Process *process)
632{
633 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000634 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000635 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000636 Mutex::Locker locker (GetDebuggerListMutex ());
637 DebuggerList &debugger_list = GetDebuggerList();
638 DebuggerList::iterator pos, end = debugger_list.end();
639 for (pos = debugger_list.begin(); pos != end; ++pos)
640 {
641 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
642 if (target_sp)
643 break;
644 }
Greg Claytone4e45922011-11-16 05:37:56 +0000645 }
646 return target_sp;
647}
648
Jason Molendae6481c72014-09-12 01:50:46 +0000649Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
650 UserID(g_unique_id++),
651 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
652 m_input_file_sp(new StreamFile(stdin, false)),
653 m_output_file_sp(new StreamFile(stdout, false)),
654 m_error_file_sp(new StreamFile(stderr, false)),
655 m_terminal_state(),
656 m_target_list(*this),
657 m_platform_list(),
658 m_listener("lldb.Debugger"),
659 m_source_manager_ap(),
660 m_source_file_cache(),
661 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
662 m_input_reader_stack(),
663 m_instance_name(),
664 m_loaded_plugins()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665{
Greg Clayton67cc0632012-08-22 17:17:09 +0000666 char instance_cstr[256];
667 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
668 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000669 if (log_callback)
670 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000671 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000672 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000673 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Greg Claytonded470d2011-03-19 01:12:21 +0000674 assert (default_platform_sp.get());
675 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000676
Greg Clayton754a9362012-08-23 00:22:02 +0000677 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000678 m_collection_sp->AppendProperty (ConstString("target"),
679 ConstString("Settings specify to debugging targets."),
680 true,
681 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000682 if (m_command_interpreter_ap.get())
683 {
684 m_collection_sp->AppendProperty (ConstString("interpreter"),
685 ConstString("Settings specify to the debugger's command interpreter."),
686 true,
687 m_command_interpreter_ap->GetValueProperties());
688 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000689 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
690 term_width->SetMinimumValue(10);
691 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000692
693 // Turn off use-color if this is a dumb terminal.
694 const char *term = getenv ("TERM");
695 if (term && !strcmp (term, "dumb"))
696 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697}
698
699Debugger::~Debugger ()
700{
Jim Ingham8314c522011-09-15 21:36:42 +0000701 Clear();
702}
703
704void
705Debugger::Clear()
706{
Greg Clayton44d93782014-01-27 23:43:24 +0000707 ClearIOHandlers();
708 StopIOHandlerThread();
709 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000710 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000711 int num_targets = m_target_list.GetNumTargets();
712 for (int i = 0; i < num_targets; i++)
713 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000714 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
715 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000716 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000717 ProcessSP process_sp (target_sp->GetProcessSP());
718 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000719 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000720 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000721 }
Greg Clayton66111032010-06-23 01:19:29 +0000722 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000723 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000724
725 // Close the input file _before_ we close the input read communications class
726 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000727 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000728 if (m_input_file_sp)
729 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000730
731 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000732}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733
734bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000735Debugger::GetCloseInputOnEOF () const
736{
Greg Clayton44d93782014-01-27 23:43:24 +0000737// return m_input_comm.GetCloseOnEOF();
738 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000739}
740
741void
742Debugger::SetCloseInputOnEOF (bool b)
743{
Greg Clayton44d93782014-01-27 23:43:24 +0000744// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000745}
746
747bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748Debugger::GetAsyncExecution ()
749{
Greg Clayton66111032010-06-23 01:19:29 +0000750 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751}
752
753void
754Debugger::SetAsyncExecution (bool async_execution)
755{
Greg Clayton66111032010-06-23 01:19:29 +0000756 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757}
758
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759
760void
761Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
762{
Greg Clayton44d93782014-01-27 23:43:24 +0000763 if (m_input_file_sp)
764 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
765 else
766 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
767
768 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000769 if (in_file.IsValid() == false)
770 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771
Jim Inghamc5917d92012-11-30 20:23:19 +0000772 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
773 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774}
775
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776void
777Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
778{
Greg Clayton44d93782014-01-27 23:43:24 +0000779 if (m_output_file_sp)
780 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
781 else
782 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
783
784 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000785 if (out_file.IsValid() == false)
786 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000787
Enrico Granatab5887262012-10-29 21:18:03 +0000788 // do not create the ScriptInterpreter just for setting the output file handle
789 // as the constructor will know how to do the right thing on its own
790 const bool can_create = false;
791 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
792 if (script_interpreter)
793 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794}
795
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796void
797Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
798{
Greg Clayton44d93782014-01-27 23:43:24 +0000799 if (m_error_file_sp)
800 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
801 else
802 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
803
804 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000805 if (err_file.IsValid() == false)
806 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807}
808
Jim Inghamc5917d92012-11-30 20:23:19 +0000809void
810Debugger::SaveInputTerminalState ()
811{
Greg Clayton44d93782014-01-27 23:43:24 +0000812 if (m_input_file_sp)
813 {
814 File &in_file = m_input_file_sp->GetFile();
815 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
816 m_terminal_state.Save(in_file.GetDescriptor(), true);
817 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000818}
819
820void
821Debugger::RestoreInputTerminalState ()
822{
823 m_terminal_state.Restore();
824}
825
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000827Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828{
829 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000830 TargetSP target_sp(GetSelectedTarget());
831 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832
833 if (target_sp)
834 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000835 ProcessSP process_sp (target_sp->GetProcessSP());
836 exe_ctx.SetProcessSP (process_sp);
837 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000839 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
840 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000842 exe_ctx.SetThreadSP (thread_sp);
843 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
844 if (exe_ctx.GetFramePtr() == NULL)
845 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846 }
847 }
848 }
849 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850}
851
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852void
Caroline Ticeefed6132010-11-19 20:47:54 +0000853Debugger::DispatchInputInterrupt ()
854{
Greg Clayton44d93782014-01-27 23:43:24 +0000855 Mutex::Locker locker (m_input_reader_stack.GetMutex());
856 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000857 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000858 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000859}
860
861void
862Debugger::DispatchInputEndOfFile ()
863{
Greg Clayton44d93782014-01-27 23:43:24 +0000864 Mutex::Locker locker (m_input_reader_stack.GetMutex());
865 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000866 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000867 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000868}
869
870void
Greg Clayton44d93782014-01-27 23:43:24 +0000871Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000872{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000873 // 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 +0000874 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000875 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000876 {
Greg Clayton44d93782014-01-27 23:43:24 +0000877 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000878 if (reader_sp)
879 {
Greg Clayton44d93782014-01-27 23:43:24 +0000880 m_input_reader_stack.Pop();
881 reader_sp->SetIsDone(true);
Greg Claytone68f5d62014-02-24 22:50:57 +0000882 reader_sp->Cancel();
Caroline Tice3d6086f2010-12-20 18:35:50 +0000883 }
884 }
885}
886
887void
Greg Clayton44d93782014-01-27 23:43:24 +0000888Debugger::ExecuteIOHanders()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000889{
Caroline Tice9088b062011-05-09 23:06:58 +0000890
Greg Clayton44d93782014-01-27 23:43:24 +0000891 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 {
Greg Clayton44d93782014-01-27 23:43:24 +0000893 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000894 if (!reader_sp)
895 break;
896
Greg Clayton44d93782014-01-27 23:43:24 +0000897 reader_sp->Activate();
898 reader_sp->Run();
899 reader_sp->Deactivate();
900
901 // Remove all input readers that are done from the top of the stack
902 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903 {
Greg Clayton44d93782014-01-27 23:43:24 +0000904 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
905 if (top_reader_sp && top_reader_sp->GetIsDone())
906 m_input_reader_stack.Pop();
907 else
908 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909 }
910 }
Greg Clayton44d93782014-01-27 23:43:24 +0000911 ClearIOHandlers();
912}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913
Greg Clayton44d93782014-01-27 23:43:24 +0000914bool
915Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
916{
917 return m_input_reader_stack.IsTop (reader_sp);
918}
919
920
921ConstString
922Debugger::GetTopIOHandlerControlSequence(char ch)
923{
924 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925}
926
927void
Greg Clayton44d93782014-01-27 23:43:24 +0000928Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
929{
Greg Clayton44d93782014-01-27 23:43:24 +0000930 PushIOHandler (reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +0000931
932 IOHandlerSP top_reader_sp = reader_sp;
933 while (top_reader_sp)
934 {
935 top_reader_sp->Activate();
936 top_reader_sp->Run();
937 top_reader_sp->Deactivate();
938
939 if (top_reader_sp.get() == reader_sp.get())
940 {
941 if (PopIOHandler (reader_sp))
942 break;
943 }
944
945 while (1)
946 {
947 top_reader_sp = m_input_reader_stack.Top();
948 if (top_reader_sp && top_reader_sp->GetIsDone())
949 m_input_reader_stack.Pop();
950 else
951 break;
952 }
953 }
Greg Clayton44d93782014-01-27 23:43:24 +0000954}
955
956void
957Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
958{
959 // Before an IOHandler runs, it must have in/out/err streams.
960 // This function is called when one ore more of the streams
961 // are NULL. We use the top input reader's in/out/err streams,
962 // or fall back to the debugger file handles, or we fall back
963 // onto stdin/stdout/stderr as a last resort.
964
965 Mutex::Locker locker (m_input_reader_stack.GetMutex());
966 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
967 // If no STDIN has been set, then set it appropriately
968 if (!in)
969 {
970 if (top_reader_sp)
971 in = top_reader_sp->GetInputStreamFile();
972 else
973 in = GetInputFile();
974
975 // If there is nothing, use stdin
976 if (!in)
977 in = StreamFileSP(new StreamFile(stdin, false));
978 }
979 // If no STDOUT has been set, then set it appropriately
980 if (!out)
981 {
982 if (top_reader_sp)
983 out = top_reader_sp->GetOutputStreamFile();
984 else
985 out = GetOutputFile();
986
987 // If there is nothing, use stdout
988 if (!out)
989 out = StreamFileSP(new StreamFile(stdout, false));
990 }
991 // If no STDERR has been set, then set it appropriately
992 if (!err)
993 {
994 if (top_reader_sp)
995 err = top_reader_sp->GetErrorStreamFile();
996 else
997 err = GetErrorFile();
998
999 // If there is nothing, use stderr
1000 if (!err)
1001 err = StreamFileSP(new StreamFile(stdout, false));
1002
1003 }
1004}
1005
1006void
1007Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008{
1009 if (!reader_sp)
1010 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +00001011
Greg Clayton44d93782014-01-27 23:43:24 +00001012 // Got the current top input reader...
1013 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001014
Greg Claytonb4874f12014-02-28 18:22:24 +00001015 // Don't push the same IO handler twice...
1016 if (reader_sp.get() != top_reader_sp.get())
1017 {
1018 // Push our new input reader
1019 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00001020
Greg Claytonb4874f12014-02-28 18:22:24 +00001021 // Interrupt the top input reader to it will exit its Run() function
1022 // and let this new input reader take over
1023 if (top_reader_sp)
1024 top_reader_sp->Deactivate();
1025 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026}
1027
1028bool
Greg Clayton44d93782014-01-27 23:43:24 +00001029Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030{
1031 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +00001032
1033 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034
1035 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001036 // read on the stack refresh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001037 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 {
Greg Clayton44d93782014-01-27 23:43:24 +00001039 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040
1041 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
1042 {
Greg Clayton44d93782014-01-27 23:43:24 +00001043 reader_sp->Deactivate();
Greg Claytonb4874f12014-02-28 18:22:24 +00001044 reader_sp->Cancel();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001045 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +00001046
1047 reader_sp = m_input_reader_stack.Top();
1048 if (reader_sp)
1049 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050
Greg Clayton44d93782014-01-27 23:43:24 +00001051 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 }
1053 }
1054 return result;
1055}
1056
1057bool
Greg Clayton44d93782014-01-27 23:43:24 +00001058Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059{
Greg Clayton44d93782014-01-27 23:43:24 +00001060 Mutex::Locker locker;
1061
1062 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063 {
Greg Clayton44d93782014-01-27 23:43:24 +00001064 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1065 if (reader_sp)
1066 reader_sp->Hide();
1067 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068 }
Greg Clayton44d93782014-01-27 23:43:24 +00001069 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070}
1071
1072void
Greg Clayton44d93782014-01-27 23:43:24 +00001073Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074{
Greg Clayton44d93782014-01-27 23:43:24 +00001075 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1076 if (reader_sp)
1077 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078}
Greg Clayton66111032010-06-23 01:19:29 +00001079
Greg Clayton44d93782014-01-27 23:43:24 +00001080
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001081StreamSP
1082Debugger::GetAsyncOutputStream ()
1083{
1084 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1085 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1086}
1087
1088StreamSP
1089Debugger::GetAsyncErrorStream ()
1090{
1091 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1092 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1093}
1094
Greg Claytonc7bece562013-01-25 18:06:21 +00001095size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001096Debugger::GetNumDebuggers()
1097{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001098 if (g_shared_debugger_refcount > 0)
1099 {
1100 Mutex::Locker locker (GetDebuggerListMutex ());
1101 return GetDebuggerList().size();
1102 }
1103 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001104}
1105
1106lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001107Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001108{
1109 DebuggerSP debugger_sp;
1110
Greg Claytonc15f55e2012-03-30 20:53:46 +00001111 if (g_shared_debugger_refcount > 0)
1112 {
1113 Mutex::Locker locker (GetDebuggerListMutex ());
1114 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001115
Greg Claytonc15f55e2012-03-30 20:53:46 +00001116 if (index < debugger_list.size())
1117 debugger_sp = debugger_list[index];
1118 }
1119
Enrico Granata061858c2012-02-15 02:34:21 +00001120 return debugger_sp;
1121}
1122
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001123DebuggerSP
1124Debugger::FindDebuggerWithID (lldb::user_id_t id)
1125{
Greg Clayton4d122c42011-09-17 08:33:22 +00001126 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001127
Greg Claytonc15f55e2012-03-30 20:53:46 +00001128 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001129 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001130 Mutex::Locker locker (GetDebuggerListMutex ());
1131 DebuggerList &debugger_list = GetDebuggerList();
1132 DebuggerList::iterator pos, end = debugger_list.end();
1133 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001134 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001135 if ((*pos).get()->GetID() == id)
1136 {
1137 debugger_sp = *pos;
1138 break;
1139 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001140 }
1141 }
1142 return debugger_sp;
1143}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001144
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001145#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001146static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001147TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001148{
1149 if (frame == NULL)
1150 return;
1151
1152 StreamString s;
1153 const char *prompt_format =
1154 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001155 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1156 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001157 "{process.id = '${process.id}'\n}"
1158 "{process.name = '${process.name}'\n}"
1159 "{process.file.basename = '${process.file.basename}'\n}"
1160 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1161 "{thread.id = '${thread.id}'\n}"
1162 "{thread.index = '${thread.index}'\n}"
1163 "{thread.name = '${thread.name}'\n}"
1164 "{thread.queue = '${thread.queue}'\n}"
1165 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1166 "{target.arch = '${target.arch}'\n}"
1167 "{module.file.basename = '${module.file.basename}'\n}"
1168 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1169 "{file.basename = '${file.basename}'\n}"
1170 "{file.fullpath = '${file.fullpath}'\n}"
1171 "{frame.index = '${frame.index}'\n}"
1172 "{frame.pc = '${frame.pc}'\n}"
1173 "{frame.sp = '${frame.sp}'\n}"
1174 "{frame.fp = '${frame.fp}'\n}"
1175 "{frame.flags = '${frame.flags}'\n}"
1176 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1177 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1178 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1179 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1180 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1181 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1182 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1183 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001184 "{function.changed = '${function.changed}'\n}"
1185 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001186 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001187 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001188 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001189 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001190 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001191 "{function.line-offset = '${function.line-offset}'\n}"
1192 "{function.pc-offset = '${function.pc-offset}'\n}"
1193 "{line.file.basename = '${line.file.basename}'\n}"
1194 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1195 "{line.number = '${line.number}'\n}"
1196 "{line.start-addr = '${line.start-addr}'\n}"
1197 "{line.end-addr = '${line.end-addr}'\n}"
1198;
1199
1200 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1201 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001202 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001203 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001204 {
1205 printf("%s\n", s.GetData());
1206 }
1207 else
1208 {
Greg Clayton1b654882010-09-19 02:33:57 +00001209 printf ("what we got: %s\n", s.GetData());
1210 }
1211}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001212#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001213
Enrico Granata9fc19442011-07-06 02:13:41 +00001214static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001215ScanFormatDescriptor (const char* var_name_begin,
1216 const char* var_name_end,
1217 const char** var_name_final,
1218 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001219 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001220 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001221{
Greg Clayton5160ce52013-03-27 23:08:40 +00001222 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001223 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001224 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001225 {
1226 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001227 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001228 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001229 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001230 else
1231 {
1232 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001233 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001234 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001235 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001236 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001237 true,
1238 *custom_format) )
1239 {
Enrico Granatae992a082011-07-22 17:03:19 +00001240 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001241 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001242
1243 switch (format_name.front())
1244 {
1245 case '@': // if this is an @ sign, print ObjC description
1246 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1247 break;
1248 case 'V': // if this is a V, print the value using the default format
1249 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1250 break;
1251 case 'L': // if this is an L, print the location of the value
1252 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1253 break;
1254 case 'S': // if this is an S, print the summary after all
1255 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1256 break;
1257 case '#': // if this is a '#', print the number of children
1258 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1259 break;
1260 case 'T': // if this is a 'T', print the type
1261 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1262 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001263 case 'N': // if this is a 'N', print the name
1264 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1265 break;
1266 case '>': // if this is a '>', print the name
1267 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1268 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001269 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001270 if (log)
1271 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001272 break;
1273 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001274 }
1275 // a good custom format tells us to print the value using it
1276 else
Enrico Granatae992a082011-07-22 17:03:19 +00001277 {
1278 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001279 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001280 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001281 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001282 }
Enrico Granatae992a082011-07-22 17:03:19 +00001283 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001284 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001285 *custom_format,
1286 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001287 return true;
1288}
1289
1290static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001291ScanBracketedRange (const char* var_name_begin,
1292 const char* var_name_end,
1293 const char* var_name_final,
1294 const char** open_bracket_position,
1295 const char** separator_position,
1296 const char** close_bracket_position,
1297 const char** var_name_final_if_array_range,
1298 int64_t* index_lower,
1299 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001300{
Greg Clayton5160ce52013-03-27 23:08:40 +00001301 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001302 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001303 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001304 {
1305 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1306 *close_bracket_position = ::strchr(*open_bracket_position,']');
1307 // as usual, we assume that [] will come before %
1308 //printf("trying to expand a []\n");
1309 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001310 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001311 {
Enrico Granatae992a082011-07-22 17:03:19 +00001312 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001313 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001314 *index_lower = 0;
1315 }
1316 else if (*separator_position == NULL || *separator_position > var_name_end)
1317 {
1318 char *end = NULL;
1319 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1320 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001321 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001322 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001323 }
Greg Clayton34132752011-07-06 04:07:21 +00001324 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001325 {
1326 char *end = NULL;
1327 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1328 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001329 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001330 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001331 }
1332 else
Enrico Granatae992a082011-07-22 17:03:19 +00001333 {
1334 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001335 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001336 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001337 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001338 if (*index_lower > *index_higher && *index_higher > 0)
1339 {
Enrico Granatae992a082011-07-22 17:03:19 +00001340 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001341 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001342 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001343 *index_lower = *index_higher;
1344 *index_higher = temp;
1345 }
1346 }
Enrico Granatae992a082011-07-22 17:03:19 +00001347 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001348 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001349 return true;
1350}
1351
Michael Sartain0769b2b2013-07-30 16:44:36 +00001352template <typename T>
1353static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1354{
1355 if (script_interpreter)
1356 {
1357 Error script_error;
1358 std::string script_output;
1359
1360 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1361 {
1362 s.Printf("%s", script_output.c_str());
1363 return true;
1364 }
1365 else
1366 {
1367 s.Printf("<error: %s>",script_error.AsCString());
1368 }
1369 }
1370 return false;
1371}
1372
Enrico Granata9fc19442011-07-06 02:13:41 +00001373static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001374ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001375 size_t index,
Jason Molendab57e4a12013-11-04 09:33:30 +00001376 StackFrame* frame,
Enrico Granatadc940732011-08-23 00:32:52 +00001377 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001378{
Greg Clayton5160ce52013-03-27 23:08:40 +00001379 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001380 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001381 std::string ptr_deref_buffer(10,0);
1382 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001383 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001384 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001385 const char* first_unparsed;
1386 ValueObject::GetValueForExpressionPathOptions options;
1387 ValueObject::ExpressionPathEndResultType final_value_type;
1388 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001389 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001390 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001391 &first_unparsed,
1392 &reason_to_stop,
1393 &final_value_type,
1394 options,
1395 &what_next);
1396 if (!item)
1397 {
Enrico Granatae992a082011-07-22 17:03:19 +00001398 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001399 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001400 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001401 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001402 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001403 else
1404 {
Enrico Granatae992a082011-07-22 17:03:19 +00001405 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001406 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001407 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001408 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001409 }
1410 return item;
1411}
1412
Michael Sartain0769b2b2013-07-30 16:44:36 +00001413static inline bool
1414IsToken(const char *var_name_begin, const char *var)
1415{
1416 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1417}
1418
1419static bool
1420IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1421 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1422{
1423 int var_len = strlen(var);
1424 if (::strncmp (var_name_begin, var, var_len) == 0)
1425 {
1426 var_name_begin += var_len;
1427 if (*var_name_begin == '}')
1428 {
1429 format = default_format;
1430 return true;
1431 }
1432 else if (*var_name_begin == '%')
1433 {
1434 // Allow format specifiers: x|X|u with optional width specifiers.
1435 // ${thread.id%x} ; hex
1436 // ${thread.id%X} ; uppercase hex
1437 // ${thread.id%u} ; unsigned decimal
1438 // ${thread.id%8.8X} ; width.precision + specifier
1439 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1440 int dot_count = 0;
1441 const char *specifier = NULL;
1442 int width_precision_length = 0;
1443 const char *width_precision = ++var_name_begin;
1444 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1445 {
1446 dot_count += (*var_name_begin == '.');
1447 if (dot_count > 1)
1448 break;
1449 var_name_begin++;
1450 width_precision_length++;
1451 }
1452
1453 if (IsToken (var_name_begin, "tid}"))
1454 {
1455 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1456 if (target)
1457 {
1458 ArchSpec arch (target->GetArchitecture ());
1459 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1460 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1461 specifier = PRIu64;
1462 }
1463 if (!specifier)
1464 {
1465 format = default_format;
1466 return true;
1467 }
1468 }
1469 else if (IsToken (var_name_begin, "x}"))
1470 specifier = PRIx64;
1471 else if (IsToken (var_name_begin, "X}"))
1472 specifier = PRIX64;
1473 else if (IsToken (var_name_begin, "u}"))
1474 specifier = PRIu64;
1475
1476 if (specifier)
1477 {
1478 format = "%";
1479 if (width_precision_length)
1480 format += std::string(width_precision, width_precision_length);
1481 format += specifier;
1482 return true;
1483 }
1484 }
1485 }
1486 return false;
1487}
1488
Jason Molenda705b1802014-06-13 02:37:02 +00001489// Find information for the "thread.info.*" specifiers in a format string
1490static bool
1491FormatThreadExtendedInfoRecurse
1492(
1493 const char *var_name_begin,
1494 StructuredData::ObjectSP thread_info_dictionary,
1495 const SymbolContext *sc,
1496 const ExecutionContext *exe_ctx,
1497 Stream &s
1498)
1499{
1500 bool var_success = false;
1501 std::string token_format;
1502
1503 llvm::StringRef var_name(var_name_begin);
1504 size_t percent_idx = var_name.find('%');
1505 size_t close_curly_idx = var_name.find('}');
1506 llvm::StringRef path = var_name;
1507 llvm::StringRef formatter = var_name;
1508
1509 // 'path' will be the dot separated list of objects to transverse up until we hit
1510 // a close curly brace, a percent sign, or an end of string.
1511 if (percent_idx != llvm::StringRef::npos || close_curly_idx != llvm::StringRef::npos)
1512 {
1513 if (percent_idx != llvm::StringRef::npos && close_curly_idx != llvm::StringRef::npos)
1514 {
1515 if (percent_idx < close_curly_idx)
1516 {
1517 path = var_name.slice(0, percent_idx);
1518 formatter = var_name.substr (percent_idx);
1519 }
1520 else
1521 {
1522 path = var_name.slice(0, close_curly_idx);
1523 formatter = var_name.substr (close_curly_idx);
1524 }
1525 }
1526 else if (percent_idx != llvm::StringRef::npos)
1527 {
1528 path = var_name.slice(0, percent_idx);
1529 formatter = var_name.substr (percent_idx);
1530 }
1531 else if (close_curly_idx != llvm::StringRef::npos)
1532 {
1533 path = var_name.slice(0, close_curly_idx);
1534 formatter = var_name.substr (close_curly_idx);
1535 }
1536 }
1537
1538 StructuredData::ObjectSP value = thread_info_dictionary->GetObjectForDotSeparatedPath (path);
1539
1540 if (value.get())
1541 {
1542 if (value->GetType() == StructuredData::Type::eTypeInteger)
1543 {
1544 if (IsTokenWithFormat (formatter.str().c_str(), "", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
1545 {
1546 s.Printf(token_format.c_str(), value->GetAsInteger()->GetValue());
1547 var_success = true;
1548 }
1549 }
1550 else if (value->GetType() == StructuredData::Type::eTypeFloat)
1551 {
1552 s.Printf ("%f", value->GetAsFloat()->GetValue());
1553 var_success = true;
1554 }
1555 else if (value->GetType() == StructuredData::Type::eTypeString)
1556 {
1557 s.Printf("%s", value->GetAsString()->GetValue().c_str());
1558 var_success = true;
1559 }
1560 else if (value->GetType() == StructuredData::Type::eTypeArray)
1561 {
1562 if (value->GetAsArray()->GetSize() > 0)
1563 {
1564 s.Printf ("%zu", value->GetAsArray()->GetSize());
1565 var_success = true;
1566 }
1567 }
1568 else if (value->GetType() == StructuredData::Type::eTypeDictionary)
1569 {
1570 s.Printf ("%zu", value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize());
1571 var_success = true;
1572 }
1573 }
1574
1575 return var_success;
1576}
1577
1578
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001579static bool
1580FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001581(
1582 const char *format,
1583 const SymbolContext *sc,
1584 const ExecutionContext *exe_ctx,
1585 const Address *addr,
1586 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001587 const char **end,
Jason Molendaaff1b352014-10-10 23:07:36 +00001588 ValueObject* valobj,
1589 bool function_changed,
1590 bool initial_function
Greg Clayton1b654882010-09-19 02:33:57 +00001591)
1592{
Enrico Granatac482a192011-08-17 22:13:59 +00001593 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001594 bool success = true;
1595 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001596 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001597
Greg Clayton1b654882010-09-19 02:33:57 +00001598 for (p = format; *p != '\0'; ++p)
1599 {
Enrico Granatac482a192011-08-17 22:13:59 +00001600 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001601 {
Enrico Granatac482a192011-08-17 22:13:59 +00001602 valobj = realvalobj;
1603 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001604 }
Greg Clayton1b654882010-09-19 02:33:57 +00001605 size_t non_special_chars = ::strcspn (p, "${}\\");
1606 if (non_special_chars > 0)
1607 {
1608 if (success)
1609 s.Write (p, non_special_chars);
1610 p += non_special_chars;
1611 }
1612
1613 if (*p == '\0')
1614 {
1615 break;
1616 }
1617 else if (*p == '{')
1618 {
1619 // Start a new scope that must have everything it needs if it is to
1620 // to make it into the final output stream "s". If you want to make
1621 // a format that only prints out the function or symbol name if there
1622 // is one in the symbol context you can use:
1623 // "{function =${function.name}}"
1624 // The first '{' starts a new scope that end with the matching '}' at
1625 // the end of the string. The contents "function =${function.name}"
1626 // will then be evaluated and only be output if there is a function
1627 // or symbol with a valid name.
1628 StreamString sub_strm;
1629
1630 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001631
Jason Molendaaff1b352014-10-10 23:07:36 +00001632 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj, function_changed, initial_function))
Greg Clayton1b654882010-09-19 02:33:57 +00001633 {
1634 // The stream had all it needed
1635 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1636 }
1637 if (*p != '}')
1638 {
1639 success = false;
1640 break;
1641 }
1642 }
1643 else if (*p == '}')
1644 {
1645 // End of a enclosing scope
1646 break;
1647 }
1648 else if (*p == '$')
1649 {
1650 // We have a prompt variable to print
1651 ++p;
1652 if (*p == '{')
1653 {
1654 ++p;
1655 const char *var_name_begin = p;
1656 const char *var_name_end = ::strchr (p, '}');
1657
1658 if (var_name_end && var_name_begin < var_name_end)
1659 {
1660 // if we have already failed to parse, skip this variable
1661 if (success)
1662 {
1663 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001664 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001665 Address format_addr;
Jason Molendaaff1b352014-10-10 23:07:36 +00001666
1667 // normally "addr" means print a raw address but
1668 // "file-addr-or-load-addr" means print a module + file addr if there's no load addr
1669 bool print_file_addr_or_load_addr = false;
1670 bool addr_offset_concrete_func_only = false;
1671 bool addr_offset_print_with_no_padding = false;
Greg Clayton1b654882010-09-19 02:33:57 +00001672 bool calculate_format_addr_function_offset = false;
1673 // Set reg_kind and reg_num to invalid values
1674 RegisterKind reg_kind = kNumRegisterKinds;
1675 uint32_t reg_num = LLDB_INVALID_REGNUM;
1676 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001677 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001678 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001679 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001680 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1681 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001682
Greg Clayton1b654882010-09-19 02:33:57 +00001683 // Each variable must set success to true below...
1684 bool var_success = false;
1685 switch (var_name_begin[0])
1686 {
Enrico Granata4becb372011-06-29 22:27:15 +00001687 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001688 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001689 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001690 {
Enrico Granatac482a192011-08-17 22:13:59 +00001691 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001692 break;
1693
Enrico Granatac3e320a2011-08-02 17:27:39 +00001694 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001695 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001696
Enrico Granata6f3533f2011-07-29 19:53:35 +00001697 // check for *var and *svar
1698 if (*var_name_begin == '*')
1699 {
1700 do_deref_pointer = true;
1701 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001702 if (log)
1703 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001704 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001705
Enrico Granata6f3533f2011-07-29 19:53:35 +00001706 if (*var_name_begin == 's')
1707 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001708 if (!valobj->IsSynthetic())
1709 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001710 if (!valobj)
1711 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001712 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001713 if (log)
1714 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001715 }
1716
1717 // should be a 'v' by now
1718 if (*var_name_begin != 'v')
1719 break;
1720
Enrico Granatac3e320a2011-08-02 17:27:39 +00001721 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001722 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001723
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001724 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001725 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001726 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001727 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001728 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001729 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001730 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001731 const char* var_name_final = NULL;
1732 const char* var_name_final_if_array_range = NULL;
1733 const char* close_bracket_position = NULL;
1734 int64_t index_lower = -1;
1735 int64_t index_higher = -1;
1736 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001737 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001738 bool was_plain_var = false;
1739 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001740 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001741
Enrico Granatac482a192011-08-17 22:13:59 +00001742 if (!valobj) break;
1743 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001744 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001745 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001746 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001747 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001748 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001749 }
Enrico Granata88282c62014-10-28 21:07:00 +00001750 else if (IsToken (var_name_begin, "var.script:"))
1751 {
1752 var_name_begin += ::strlen("var.script:");
1753 std::string script_name(var_name_begin,var_name_end);
1754 ScriptInterpreter* script_interpreter = valobj->GetTargetSP()->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
1755 if (RunScriptFormatKeyword (s, script_interpreter, valobj, script_name))
1756 var_success = true;
1757 break;
1758 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001759 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001760 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001761 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001762 // this is a variable with some custom format applied to it
1763 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001764 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001765 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001766 ScanFormatDescriptor (var_name_begin,
1767 var_name_end,
1768 &var_name_final,
1769 &percent_position,
1770 &custom_format,
1771 &val_obj_display);
1772 }
1773 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001774 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001775 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001776 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001777 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001778 const char* percent_position;
1779 ScanFormatDescriptor (var_name_begin,
1780 var_name_end,
1781 &var_name_final,
1782 &percent_position,
1783 &custom_format,
1784 &val_obj_display);
1785
1786 const char* open_bracket_position;
1787 const char* separator_position;
1788 ScanBracketedRange (var_name_begin,
1789 var_name_end,
1790 var_name_final,
1791 &open_bracket_position,
1792 &separator_position,
1793 &close_bracket_position,
1794 &var_name_final_if_array_range,
1795 &index_lower,
1796 &index_higher);
1797
1798 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001799
Enrico Granata599171a2013-02-01 23:59:44 +00001800 std::string expr_path(var_name_final-var_name_begin-1,0);
1801 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1802
1803 if (log)
1804 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1805
1806 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001807 &first_unparsed,
1808 &reason_to_stop,
1809 &final_value_type,
1810 options,
1811 &what_next).get();
1812
1813 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001814 {
Enrico Granatae992a082011-07-22 17:03:19 +00001815 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001816 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001817 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001818 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001819 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001820 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001821 else
1822 {
Enrico Granatae992a082011-07-22 17:03:19 +00001823 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001824 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001825 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001826 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata50bed5e2014-10-22 20:14:09 +00001827 target = target->GetQualifiedRepresentationIfAvailable(target->GetDynamicValueType(), true).get();
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001828 }
Enrico Granata4becb372011-06-29 22:27:15 +00001829 }
Greg Clayton34132752011-07-06 04:07:21 +00001830 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001831 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001832
Enrico Granata86cc9822012-03-19 22:58:49 +00001833 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1834 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001835
Enrico Granata86cc9822012-03-19 22:58:49 +00001836 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001837
Enrico Granataa7187d02011-07-06 19:27:11 +00001838 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001839 {
Greg Clayton34132752011-07-06 04:07:21 +00001840 // I have not deref-ed yet, let's do it
1841 // this happens when we are not going through GetValueForVariableExpressionPath
1842 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001843 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001844 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001845 if (error.Fail())
1846 {
1847 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001848 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001849 break;
1850 }
Greg Clayton34132752011-07-06 04:07:21 +00001851 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001852 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001853
Jim Inghamf164d942014-03-11 18:17:23 +00001854 if (!target)
1855 {
1856 if (log)
1857 log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
1858 break;
1859 }
1860
Enrico Granataa777dc22012-05-08 21:49:57 +00001861 // we do not want to use the summary for a bitfield of type T:n
1862 // if we were originally dealing with just a T - that would get
1863 // us into an endless recursion
1864 if (target->IsBitfield() && was_var_indexed)
1865 {
1866 // TODO: check for a (T:n)-specific summary - we should still obey that
1867 StreamString bitfield_name;
1868 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1869 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1870 if (!DataVisualization::GetSummaryForType(type_sp))
1871 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1872 }
1873
Enrico Granata85933ed2011-08-18 16:38:26 +00001874 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001875 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
Enrico Granata622be232014-10-21 20:52:14 +00001876 bool is_array = (type_info_flags & eTypeIsArray) != 0;
1877 bool is_pointer = (type_info_flags & eTypeIsPointer) != 0;
Greg Clayton57ee3062013-07-11 22:46:58 +00001878 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001879
Enrico Granata86cc9822012-03-19 22:58:49 +00001880 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 +00001881 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001882 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001883 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001884 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001885
Greg Clayton5088c482013-03-25 21:06:13 +00001886 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001887 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001888 // try to use the special cases
1889 var_success = target->DumpPrintableRepresentation(str_temp,
1890 val_obj_display,
1891 custom_format);
1892 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001893 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001894
1895 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001896 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001897 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001898 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001899 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001900 }
1901 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001902 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001903 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001904 {
1905 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1906 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001907 else if (is_pointer) // if pointer, value is the address stored
1908 {
Greg Clayton23f59502012-07-17 03:23:13 +00001909 target->DumpPrintableRepresentation (s,
1910 val_obj_display,
1911 custom_format,
1912 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001913 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001914 var_success = true;
1915 break;
1916 }
1917 }
1918
1919 // if directly trying to print ${var}, and this is an aggregate, display a nice
1920 // type @ location message
1921 if (is_aggregate && was_plain_var)
1922 {
1923 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1924 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001925 break;
1926 }
1927
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001928 // 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 +00001929 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001930 {
1931 s << "<invalid use of aggregate type>";
1932 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001933 break;
1934 }
Greg Clayton34132752011-07-06 04:07:21 +00001935
1936 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001937 {
1938 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001939 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001940 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001941 }
Greg Clayton34132752011-07-06 04:07:21 +00001942 else
Enrico Granatae992a082011-07-22 17:03:19 +00001943 {
1944 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001945 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001946 if (!is_array && !is_pointer)
1947 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001948 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001949 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001950 const char* special_directions = NULL;
1951 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001952 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1953 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001954 ConstString additional_data;
1955 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1956 special_directions_writer.Printf("${%svar%s}",
1957 do_deref_pointer ? "*" : "",
1958 additional_data.GetCString());
1959 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001960 }
1961
1962 // let us display items index_lower thru index_higher of this array
1963 s.PutChar('[');
1964 var_success = true;
1965
1966 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001967 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001968
Greg Claytoncc4d0142012-02-17 07:49:44 +00001969 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001970
Greg Clayton34132752011-07-06 04:07:21 +00001971 for (;index_lower<=index_higher;index_lower++)
1972 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001973 ValueObject* item = ExpandIndexedExpression (target,
1974 index_lower,
1975 exe_ctx->GetFramePtr(),
1976 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001977
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001978 if (!item)
1979 {
Enrico Granatae992a082011-07-22 17:03:19 +00001980 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001981 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001982 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001983 else
1984 {
Enrico Granatae992a082011-07-22 17:03:19 +00001985 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001986 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001987 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001988
Greg Clayton34132752011-07-06 04:07:21 +00001989 if (!special_directions)
1990 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1991 else
Jason Molendaaff1b352014-10-10 23:07:36 +00001992 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item, function_changed, initial_function);
Greg Clayton34132752011-07-06 04:07:21 +00001993
Enrico Granata22c55d12011-08-12 02:00:06 +00001994 if (--max_num_children == 0)
1995 {
1996 s.PutCString(", ...");
1997 break;
1998 }
1999
Greg Clayton34132752011-07-06 04:07:21 +00002000 if (index_lower < index_higher)
2001 s.PutChar(',');
2002 }
2003 s.PutChar(']');
2004 }
Enrico Granata4becb372011-06-29 22:27:15 +00002005 }
Enrico Granata9fc19442011-07-06 02:13:41 +00002006 break;
Greg Clayton1b654882010-09-19 02:33:57 +00002007 case 'a':
Jason Molendaaff1b352014-10-10 23:07:36 +00002008 if (IsToken (var_name_begin, "addr-file-or-load}"))
2009 {
2010 print_file_addr_or_load_addr = true;
2011 }
2012 if (IsToken (var_name_begin, "addr}")
2013 || IsToken (var_name_begin, "addr-file-or-load}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002014 {
2015 if (addr && addr->IsValid())
2016 {
2017 var_success = true;
2018 format_addr = *addr;
2019 }
2020 }
2021 break;
2022
2023 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002024 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00002025 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002026 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002027 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002028 Process *process = exe_ctx->GetProcessPtr();
2029 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00002030 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002031 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002032 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002033 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002034 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002035 var_success = true;
2036 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002037 else if ((IsToken (var_name_begin, "name}")) ||
2038 (IsToken (var_name_begin, "file.basename}")) ||
2039 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00002040 {
2041 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
2042 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00002043 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002044 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
2045 {
2046 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002047 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00002048 }
2049 else
2050 {
2051 format_file_spec = exe_module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002052 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00002053 }
Greg Clayton1b654882010-09-19 02:33:57 +00002054 }
2055 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002056 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002057 {
2058 var_name_begin += ::strlen("script:");
2059 std::string script_name(var_name_begin,var_name_end);
2060 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002061 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
2062 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002063 }
Greg Clayton1b654882010-09-19 02:33:57 +00002064 }
Greg Claytonc14ee322011-09-22 04:58:26 +00002065 }
Greg Clayton1b654882010-09-19 02:33:57 +00002066 }
2067 break;
2068
2069 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002070 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00002071 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002072 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002073 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002074 Thread *thread = exe_ctx->GetThreadPtr();
2075 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00002076 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002077 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002078 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002079 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002080 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002081 var_success = true;
2082 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002083 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00002084 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002085 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00002086 var_success = true;
2087 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002088 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00002089 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002090 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002091 var_success = true;
2092 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002093 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002094 {
2095 cstr = thread->GetName();
2096 var_success = cstr && cstr[0];
2097 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002098 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00002099 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002100 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002101 {
2102 cstr = thread->GetQueueName();
2103 var_success = cstr && cstr[0];
2104 if (var_success)
2105 s.PutCString(cstr);
2106 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002107 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002108 {
2109 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002110 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00002111 {
2112 cstr = stop_info_sp->GetDescription();
2113 if (cstr && cstr[0])
2114 {
2115 s.PutCString(cstr);
2116 var_success = true;
2117 }
Greg Clayton1b654882010-09-19 02:33:57 +00002118 }
2119 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002120 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00002121 {
2122 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002123 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00002124 {
2125 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2126 if (return_valobj_sp)
2127 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00002128 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00002129 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00002130 }
2131 }
2132 }
Jim Ingham30fadaf2014-07-08 01:07:32 +00002133 else if (IsToken (var_name_begin, "completed-expression}"))
2134 {
2135 StopInfoSP stop_info_sp = thread->GetStopInfo ();
2136 if (stop_info_sp && stop_info_sp->IsValid())
2137 {
2138 ClangExpressionVariableSP expression_var_sp = StopInfo::GetExpressionVariable (stop_info_sp);
2139 if (expression_var_sp && expression_var_sp->GetValueObject())
2140 {
2141 expression_var_sp->GetValueObject()->Dump(s);
2142 var_success = true;
2143 }
2144 }
2145 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002146 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002147 {
2148 var_name_begin += ::strlen("script:");
2149 std::string script_name(var_name_begin,var_name_end);
2150 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002151 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
2152 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002153 }
Jason Molenda705b1802014-06-13 02:37:02 +00002154 else if (IsToken (var_name_begin, "info."))
2155 {
2156 var_name_begin += ::strlen("info.");
2157 StructuredData::ObjectSP object_sp = thread->GetExtendedInfo();
2158 if (object_sp && object_sp->GetType() == StructuredData::Type::eTypeDictionary)
2159 {
2160 var_success = FormatThreadExtendedInfoRecurse (var_name_begin, object_sp, sc, exe_ctx, s);
2161 }
2162 }
Greg Clayton1b654882010-09-19 02:33:57 +00002163 }
2164 }
2165 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002166 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00002167 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002168 // TODO: hookup properties
2169// if (!target_properties_sp)
2170// {
2171// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2172// if (target)
2173// target_properties_sp = target->GetProperties();
2174// }
2175//
2176// if (target_properties_sp)
2177// {
2178// var_name_begin += ::strlen ("target.");
2179// const char *end_property = strchr(var_name_begin, '}');
2180// if (end_property)
2181// {
2182// ConstString property_name(var_name_begin, end_property - var_name_begin);
2183// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2184// if (!property_value.empty())
2185// {
2186// s.PutCString (property_value.c_str());
2187// var_success = true;
2188// }
2189// }
2190// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002191 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2192 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002193 {
Greg Clayton1b654882010-09-19 02:33:57 +00002194 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002195 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002196 {
2197 ArchSpec arch (target->GetArchitecture ());
2198 if (arch.IsValid())
2199 {
Greg Clayton64195a22011-02-23 00:35:02 +00002200 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002201 var_success = true;
2202 }
2203 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002204 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002205 {
2206 var_name_begin += ::strlen("script:");
2207 std::string script_name(var_name_begin,var_name_end);
2208 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002209 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2210 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002211 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002212 }
Greg Clayton1b654882010-09-19 02:33:57 +00002213 }
2214 break;
Jason Molendaaff1b352014-10-10 23:07:36 +00002215
Greg Clayton1b654882010-09-19 02:33:57 +00002216 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002217 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002218 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002219 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002220 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002221 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002222 var_name_begin += ::strlen ("module.");
2223
Michael Sartain0769b2b2013-07-30 16:44:36 +00002224 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002225 {
2226 if (module->GetFileSpec())
2227 {
2228 var_name_begin += ::strlen ("file.");
2229
Michael Sartain0769b2b2013-07-30 16:44:36 +00002230 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002231 {
2232 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002233 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002234 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002235 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002236 {
2237 format_file_spec = module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002238 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002239 }
2240 }
2241 }
2242 }
2243 }
2244 break;
2245
2246
2247 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002248 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002249 {
2250 if (sc && sc->comp_unit != NULL)
2251 {
2252 var_name_begin += ::strlen ("file.");
2253
Michael Sartain0769b2b2013-07-30 16:44:36 +00002254 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002255 {
2256 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002257 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002258 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002259 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002260 {
2261 format_file_spec = *sc->comp_unit;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002262 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002263 }
2264 }
2265 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002266 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002267 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002268 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002269 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002270 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002271 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002272 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002273 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002274 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002275 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002276 s.Printf("%u", frame->GetFrameIndex());
2277 var_success = true;
2278 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002279 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002280 {
2281 reg_kind = eRegisterKindGeneric;
2282 reg_num = LLDB_REGNUM_GENERIC_PC;
2283 var_success = true;
2284 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002285 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002286 {
2287 reg_kind = eRegisterKindGeneric;
2288 reg_num = LLDB_REGNUM_GENERIC_SP;
2289 var_success = true;
2290 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002291 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002292 {
2293 reg_kind = eRegisterKindGeneric;
2294 reg_num = LLDB_REGNUM_GENERIC_FP;
2295 var_success = true;
2296 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002297 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002298 {
2299 reg_kind = eRegisterKindGeneric;
2300 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2301 var_success = true;
2302 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002303 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002304 {
2305 reg_ctx = frame->GetRegisterContext().get();
2306 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002307 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002308 var_name_begin += ::strlen ("reg.");
2309 if (var_name_begin < var_name_end)
2310 {
2311 std::string reg_name (var_name_begin, var_name_end);
2312 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2313 if (reg_info)
2314 var_success = true;
2315 }
Greg Clayton1b654882010-09-19 02:33:57 +00002316 }
2317 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002318 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002319 {
2320 var_name_begin += ::strlen("script:");
2321 std::string script_name(var_name_begin,var_name_end);
2322 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002323 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2324 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002325 }
Greg Clayton1b654882010-09-19 02:33:57 +00002326 }
2327 }
2328 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002329 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002330 {
2331 if (sc && (sc->function != NULL || sc->symbol != NULL))
2332 {
2333 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002334 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002335 {
2336 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002337 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002338 else
2339 s.Printf("symbol[%u]", sc->symbol->GetID());
2340
2341 var_success = true;
2342 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002343 if (IsToken (var_name_begin, "changed}") && function_changed)
2344 {
2345 var_success = true;
2346 }
2347 if (IsToken (var_name_begin, "initial-function}") && initial_function)
2348 {
2349 var_success = true;
2350 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002351 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002352 {
2353 if (sc->function)
2354 cstr = sc->function->GetName().AsCString (NULL);
2355 else if (sc->symbol)
2356 cstr = sc->symbol->GetName().AsCString (NULL);
2357 if (cstr)
2358 {
2359 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002360
2361 if (sc->block)
2362 {
2363 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2364 if (inline_block)
2365 {
2366 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2367 if (inline_info)
2368 {
2369 s.PutCString(" [inlined] ");
2370 inline_info->GetName().Dump(&s);
2371 }
2372 }
2373 }
Greg Clayton1b654882010-09-19 02:33:57 +00002374 var_success = true;
2375 }
2376 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002377 else if (IsToken (var_name_begin, "name-without-args}"))
2378 {
2379 ConstString name;
2380 if (sc->function)
2381 name = sc->function->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments);
2382 else if (sc->symbol)
2383 name = sc->symbol->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments);
2384 if (name)
2385 {
2386 s.PutCString(name.GetCString());
2387 var_success = true;
2388 }
2389 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002390 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002391 {
2392 // Print the function name with arguments in it
2393
2394 if (sc->function)
2395 {
2396 var_success = true;
2397 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2398 cstr = sc->function->GetName().AsCString (NULL);
2399 if (cstr)
2400 {
2401 const InlineFunctionInfo *inline_info = NULL;
2402 VariableListSP variable_list_sp;
2403 bool get_function_vars = true;
2404 if (sc->block)
2405 {
2406 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2407
2408 if (inline_block)
2409 {
2410 get_function_vars = false;
2411 inline_info = sc->block->GetInlinedFunctionInfo();
2412 if (inline_info)
2413 variable_list_sp = inline_block->GetBlockVariableList (true);
2414 }
2415 }
2416
2417 if (get_function_vars)
2418 {
2419 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2420 }
2421
2422 if (inline_info)
2423 {
2424 s.PutCString (cstr);
2425 s.PutCString (" [inlined] ");
2426 cstr = inline_info->GetName().GetCString();
2427 }
2428
2429 VariableList args;
2430 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002431 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002432 if (args.GetSize() > 0)
2433 {
2434 const char *open_paren = strchr (cstr, '(');
Enrico Granatae4a4f5d2014-08-16 00:56:04 +00002435 const char *close_paren = nullptr;
2436 const char *generic = strchr(cstr, '<');
2437 // if before the arguments list begins there is a template sign
2438 // then scan to the end of the generic args before you try to find
2439 // the arguments list
2440 if (generic && open_paren && generic < open_paren)
2441 {
2442 int generic_depth = 1;
2443 ++generic;
2444 for (;
2445 *generic && generic_depth > 0;
2446 generic++)
2447 {
2448 if (*generic == '<')
2449 generic_depth++;
2450 if (*generic == '>')
2451 generic_depth--;
2452 }
2453 if (*generic)
2454 open_paren = strchr(generic, '(');
2455 else
2456 open_paren = nullptr;
2457 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002458 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002459 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002460 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002461 {
2462 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2463 if (open_paren)
2464 close_paren = strchr (open_paren, ')');
2465 }
2466 else
2467 close_paren = strchr (open_paren, ')');
2468 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002469
2470 if (open_paren)
2471 s.Write(cstr, open_paren - cstr + 1);
2472 else
2473 {
2474 s.PutCString (cstr);
2475 s.PutChar ('(');
2476 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002477 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002478 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2479 {
Enrico Granata894f7352014-03-25 22:03:52 +00002480 std::string buffer;
2481
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002482 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2483 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
Enrico Granata894f7352014-03-25 22:03:52 +00002484 const char *var_representation = nullptr;
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002485 const char *var_name = var_value_sp->GetName().GetCString();
Enrico Granata894f7352014-03-25 22:03:52 +00002486 if (var_value_sp->GetClangType().IsAggregateType() &&
2487 DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
2488 {
2489 static StringSummaryFormat format(TypeSummaryImpl::Flags()
2490 .SetHideItemNames(false)
2491 .SetShowMembersOneLiner(true),
2492 "");
2493 format.FormatObject(var_value_sp.get(), buffer);
2494 var_representation = buffer.c_str();
2495 }
2496 else
2497 var_representation = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002498 if (arg_idx > 0)
2499 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002500 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002501 {
Enrico Granata894f7352014-03-25 22:03:52 +00002502 if (var_representation)
2503 s.Printf ("%s=%s", var_name, var_representation);
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002504 else
2505 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2506 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002507 else
2508 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002509 }
2510
2511 if (close_paren)
2512 s.PutCString (close_paren);
2513 else
2514 s.PutChar(')');
2515
2516 }
2517 else
2518 {
2519 s.PutCString(cstr);
2520 }
2521 }
2522 }
2523 else if (sc->symbol)
2524 {
2525 cstr = sc->symbol->GetName().AsCString (NULL);
2526 if (cstr)
2527 {
2528 s.PutCString(cstr);
2529 var_success = true;
2530 }
2531 }
2532 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002533 else if (IsToken (var_name_begin, "addr-offset}")
2534 || IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002535 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002536 if (IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}"))
2537 {
2538 addr_offset_print_with_no_padding = true;
2539 addr_offset_concrete_func_only = true;
2540 }
Greg Clayton1b654882010-09-19 02:33:57 +00002541 var_success = addr != NULL;
2542 if (var_success)
2543 {
2544 format_addr = *addr;
2545 calculate_format_addr_function_offset = true;
2546 }
2547 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002548 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002549 {
2550 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2551 if (var_success)
2552 {
2553 format_addr = sc->line_entry.range.GetBaseAddress();
2554 calculate_format_addr_function_offset = true;
2555 }
2556 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002557 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002558 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002559 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002560 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002561 if (var_success)
2562 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002563 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002564 calculate_format_addr_function_offset = true;
2565 }
2566 }
2567 }
2568 }
2569 break;
2570
2571 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002572 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002573 {
2574 if (sc && sc->line_entry.IsValid())
2575 {
2576 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002577 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002578 {
2579 var_name_begin += ::strlen ("file.");
2580
Michael Sartain0769b2b2013-07-30 16:44:36 +00002581 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002582 {
2583 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002584 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002585 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002586 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002587 {
2588 format_file_spec = sc->line_entry.file;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002589 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002590 }
2591 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002592 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002593 {
2594 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002595 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002596 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002597 else if ((IsToken (var_name_begin, "start-addr}")) ||
2598 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002599 {
2600 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2601 if (var_success)
2602 {
2603 format_addr = sc->line_entry.range.GetBaseAddress();
2604 if (var_name_begin[0] == 'e')
2605 format_addr.Slide (sc->line_entry.range.GetByteSize());
2606 }
2607 }
2608 }
2609 }
2610 break;
Jason Molendaaff1b352014-10-10 23:07:36 +00002611 case 'c':
2612 if (IsToken (var_name_begin, "current-pc-arrow"))
2613 {
2614 if (addr && exe_ctx && exe_ctx->GetFramePtr())
2615 {
2616 RegisterContextSP reg_ctx = exe_ctx->GetFramePtr()->GetRegisterContextSP();
2617 if (reg_ctx.get())
2618 {
2619 addr_t pc_loadaddr = reg_ctx->GetPC();
2620 if (pc_loadaddr != LLDB_INVALID_ADDRESS)
2621 {
2622 Address pc;
2623 pc.SetLoadAddress (pc_loadaddr, exe_ctx->GetTargetPtr());
2624 if (pc == *addr)
2625 {
2626 s.Printf ("->");
2627 var_success = true;
2628 }
2629 else
2630 {
2631 s.Printf(" ");
2632 var_success = true;
2633 }
2634 }
2635 }
2636 }
2637 }
2638 break;
Greg Clayton1b654882010-09-19 02:33:57 +00002639 }
2640
2641 if (var_success)
2642 {
2643 // If format addr is valid, then we need to print an address
2644 if (reg_num != LLDB_INVALID_REGNUM)
2645 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002646 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002647 // We have a register value to display...
2648 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2649 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002650 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002651 }
2652 else
2653 {
2654 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002655 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002656
2657 if (reg_ctx)
2658 {
2659 if (reg_kind != kNumRegisterKinds)
2660 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2661 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2662 var_success = reg_info != NULL;
2663 }
2664 }
2665 }
2666
2667 if (reg_info != NULL)
2668 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002669 RegisterValue reg_value;
2670 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2671 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002672 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002673 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002674 }
2675 }
2676
2677 if (format_file_spec)
2678 {
2679 s << format_file_spec;
2680 }
2681
2682 // If format addr is valid, then we need to print an address
2683 if (format_addr.IsValid())
2684 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002685 var_success = false;
2686
Greg Clayton1b654882010-09-19 02:33:57 +00002687 if (calculate_format_addr_function_offset)
2688 {
2689 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002690
Greg Clayton0603aa92010-10-04 01:05:56 +00002691 if (sc)
2692 {
2693 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002694 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002695 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Jason Molendaaff1b352014-10-10 23:07:36 +00002696 if (sc->block && addr_offset_concrete_func_only == false)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002697 {
2698 // Check to make sure we aren't in an inline
2699 // function. If we are, use the inline block
2700 // range that contains "format_addr" since
2701 // blocks can be discontiguous.
2702 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2703 AddressRange inline_range;
2704 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2705 func_addr = inline_range.GetBaseAddress();
2706 }
2707 }
Greg Claytone7612132012-03-07 21:03:09 +00002708 else if (sc->symbol && sc->symbol->ValueIsAddress())
2709 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002710 }
2711
2712 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002713 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002714 const char *addr_offset_padding = " ";
2715 if (addr_offset_print_with_no_padding)
2716 {
2717 addr_offset_padding = "";
2718 }
Greg Clayton1b654882010-09-19 02:33:57 +00002719 if (func_addr.GetSection() == format_addr.GetSection())
2720 {
2721 addr_t func_file_addr = func_addr.GetFileAddress();
2722 addr_t addr_file_addr = format_addr.GetFileAddress();
2723 if (addr_file_addr > func_file_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002724 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002725 else if (addr_file_addr < func_file_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002726 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002727 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002728 }
2729 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002730 {
2731 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2732 if (target)
2733 {
2734 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2735 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2736 if (addr_load_addr > func_load_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002737 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002738 else if (addr_load_addr < func_load_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002739 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002740 var_success = true;
2741 }
2742 }
Greg Clayton1b654882010-09-19 02:33:57 +00002743 }
2744 }
2745 else
2746 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002747 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002748 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002749 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2750 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002751 if (vaddr == LLDB_INVALID_ADDRESS)
2752 vaddr = format_addr.GetFileAddress ();
2753
2754 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002755 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002756 int addr_width = 0;
2757 if (exe_ctx && target)
2758 {
Eric Christopherfd1a9362014-10-11 00:04:42 +00002759 addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Jason Molendaaff1b352014-10-10 23:07:36 +00002760 }
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002761 if (addr_width == 0)
2762 addr_width = 16;
Jason Molendaaff1b352014-10-10 23:07:36 +00002763 if (print_file_addr_or_load_addr)
2764 {
2765 format_addr.Dump (&s, exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress, 0);
2766 }
2767 else
2768 {
2769 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
2770 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002771 var_success = true;
2772 }
Greg Clayton1b654882010-09-19 02:33:57 +00002773 }
2774 }
2775 }
2776
2777 if (var_success == false)
2778 success = false;
2779 }
2780 p = var_name_end;
2781 }
2782 else
2783 break;
2784 }
2785 else
2786 {
2787 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2788 s.PutChar(*p);
2789 }
2790 }
2791 else if (*p == '\\')
2792 {
2793 ++p; // skip the slash
2794 switch (*p)
2795 {
2796 case 'a': s.PutChar ('\a'); break;
2797 case 'b': s.PutChar ('\b'); break;
2798 case 'f': s.PutChar ('\f'); break;
2799 case 'n': s.PutChar ('\n'); break;
2800 case 'r': s.PutChar ('\r'); break;
2801 case 't': s.PutChar ('\t'); break;
2802 case 'v': s.PutChar ('\v'); break;
2803 case '\'': s.PutChar ('\''); break;
2804 case '\\': s.PutChar ('\\'); break;
2805 case '0':
2806 // 1 to 3 octal chars
2807 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002808 // Make a string that can hold onto the initial zero char,
2809 // up to 3 octal digits, and a terminating NULL.
2810 char oct_str[5] = { 0, 0, 0, 0, 0 };
2811
2812 int i;
2813 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2814 oct_str[i] = p[i];
2815
2816 // We don't want to consume the last octal character since
2817 // the main for loop will do this for us, so we advance p by
2818 // one less than i (even if i is zero)
2819 p += i - 1;
2820 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2821 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002822 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002823 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002824 }
Greg Clayton1b654882010-09-19 02:33:57 +00002825 }
2826 break;
2827
2828 case 'x':
2829 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002830 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002831 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002832 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002833
Greg Clayton0603aa92010-10-04 01:05:56 +00002834 // Make a string that can hold onto two hex chars plus a
2835 // NULL terminator
2836 char hex_str[3] = { 0,0,0 };
2837 hex_str[0] = *p;
2838 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002839 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002840 ++p; // Skip the first of the two hex chars
2841 hex_str[1] = *p;
2842 }
2843
2844 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2845 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002846 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002847 }
2848 else
2849 {
2850 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002851 }
2852 break;
2853
2854 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002855 // Just desensitize any other character by just printing what
2856 // came after the '\'
2857 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002858 break;
2859
2860 }
2861
2862 }
2863 }
2864 if (end)
2865 *end = p;
2866 return success;
2867}
2868
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002869bool
2870Debugger::FormatPrompt
2871(
2872 const char *format,
2873 const SymbolContext *sc,
2874 const ExecutionContext *exe_ctx,
2875 const Address *addr,
2876 Stream &s,
2877 ValueObject* valobj
2878)
2879{
2880 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2881 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2882 if (format_str.length())
2883 format = format_str.c_str();
Jason Molendaaff1b352014-10-10 23:07:36 +00002884 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj, false, false);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002885}
2886
Jason Molendaaff1b352014-10-10 23:07:36 +00002887bool
2888Debugger::FormatDisassemblerAddress (const char *format,
2889 const SymbolContext *sc,
2890 const SymbolContext *prev_sc,
2891 const ExecutionContext *exe_ctx,
2892 const Address *addr,
2893 Stream &s)
2894{
2895 if (format == NULL && exe_ctx != NULL && exe_ctx->HasTargetScope())
2896 {
2897 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
2898 }
2899 bool function_changed = false;
2900 bool initial_function = false;
2901 if (prev_sc && (prev_sc->function || prev_sc->symbol))
2902 {
2903 if (sc && (sc->function || sc->symbol))
2904 {
2905 if (prev_sc->symbol && sc->symbol)
2906 {
2907 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
2908 {
2909 function_changed = true;
2910 }
2911 }
2912 else if (prev_sc->function && sc->function)
2913 {
2914 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
2915 {
2916 function_changed = true;
2917 }
2918 }
2919 }
2920 }
2921 // The first context on a list of instructions will have a prev_sc that
2922 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
2923 // would return false. But we do get a prev_sc pointer.
2924 if ((sc && (sc->function || sc->symbol))
2925 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL))
2926 {
2927 initial_function = true;
2928 }
2929 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, NULL, function_changed, initial_function);
2930}
2931
2932
Jim Ingham228063c2012-02-21 02:23:08 +00002933void
2934Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2935{
Jim Ingham4f02b222012-02-22 22:49:20 +00002936 // For simplicity's sake, I am not going to deal with how to close down any
2937 // open logging streams, I just redirect everything from here on out to the
2938 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002939 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2940}
2941
2942bool
2943Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2944{
2945 Log::Callbacks log_callbacks;
2946
2947 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002948 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002949 {
2950 log_stream_sp = m_log_callback_stream_sp;
2951 // For now when using the callback mode you always get thread & timestamp.
2952 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2953 }
2954 else if (log_file == NULL || *log_file == '\0')
2955 {
Greg Clayton44d93782014-01-27 23:43:24 +00002956 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00002957 }
2958 else
2959 {
2960 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002961 if (pos != m_log_streams.end())
2962 log_stream_sp = pos->second.lock();
2963 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002964 {
2965 log_stream_sp.reset (new StreamFile (log_file));
2966 m_log_streams[log_file] = log_stream_sp;
2967 }
Jim Ingham228063c2012-02-21 02:23:08 +00002968 }
2969 assert (log_stream_sp.get());
2970
2971 if (log_options == 0)
2972 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2973
Greg Clayton57abc5d2013-05-10 21:47:16 +00002974 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002975 {
2976 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2977 return true;
2978 }
2979 else
2980 {
2981 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2982 if (log_channel_sp)
2983 {
2984 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2985 {
2986 return true;
2987 }
2988 else
2989 {
2990 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2991 return false;
2992 }
2993 }
2994 else
2995 {
2996 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2997 return false;
2998 }
2999 }
3000 return false;
3001}
3002
Greg Clayton9585fbf2013-03-19 00:20:55 +00003003SourceManager &
3004Debugger::GetSourceManager ()
3005{
3006 if (m_source_manager_ap.get() == NULL)
3007 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
3008 return *m_source_manager_ap;
3009}
3010
3011
Greg Clayton44d93782014-01-27 23:43:24 +00003012
3013// This function handles events that were broadcast by the process.
3014void
3015Debugger::HandleBreakpointEvent (const EventSP &event_sp)
3016{
3017 using namespace lldb;
3018 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
3019
3020// if (event_type & eBreakpointEventTypeAdded
3021// || event_type & eBreakpointEventTypeRemoved
3022// || event_type & eBreakpointEventTypeEnabled
3023// || event_type & eBreakpointEventTypeDisabled
3024// || event_type & eBreakpointEventTypeCommandChanged
3025// || event_type & eBreakpointEventTypeConditionChanged
3026// || event_type & eBreakpointEventTypeIgnoreChanged
3027// || event_type & eBreakpointEventTypeLocationsResolved)
3028// {
3029// // Don't do anything about these events, since the breakpoint commands already echo these actions.
3030// }
3031//
3032 if (event_type & eBreakpointEventTypeLocationsAdded)
3033 {
3034 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
3035 if (num_new_locations > 0)
3036 {
3037 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
3038 StreamFileSP output_sp (GetOutputFile());
3039 if (output_sp)
3040 {
3041 output_sp->Printf("%d location%s added to breakpoint %d\n",
3042 num_new_locations,
3043 num_new_locations == 1 ? "" : "s",
3044 breakpoint->GetID());
3045 RefreshTopIOHandler();
3046 }
3047 }
3048 }
3049// else if (event_type & eBreakpointEventTypeLocationsRemoved)
3050// {
3051// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
3052// }
3053// else if (event_type & eBreakpointEventTypeLocationsResolved)
3054// {
3055// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
3056// }
3057}
3058
3059size_t
3060Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
3061{
3062 size_t total_bytes = 0;
3063 if (stream == NULL)
3064 stream = GetOutputFile().get();
3065
3066 if (stream)
3067 {
3068 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
3069 if (process == NULL)
3070 {
3071 TargetSP target_sp = GetTargetList().GetSelectedTarget();
3072 if (target_sp)
3073 process = target_sp->GetProcessSP().get();
3074 }
3075 if (process)
3076 {
3077 Error error;
3078 size_t len;
3079 char stdio_buffer[1024];
3080 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3081 {
3082 stream->Write(stdio_buffer, len);
3083 total_bytes += len;
3084 }
3085 }
3086 stream->Flush();
3087 }
3088 return total_bytes;
3089}
3090
3091size_t
3092Debugger::GetProcessSTDERR (Process *process, Stream *stream)
3093{
3094 size_t total_bytes = 0;
3095 if (stream == NULL)
3096 stream = GetOutputFile().get();
3097
3098 if (stream)
3099 {
3100 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
3101 if (process == NULL)
3102 {
3103 TargetSP target_sp = GetTargetList().GetSelectedTarget();
3104 if (target_sp)
3105 process = target_sp->GetProcessSP().get();
3106 }
3107 if (process)
3108 {
3109 Error error;
3110 size_t len;
3111 char stdio_buffer[1024];
3112 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3113 {
3114 stream->Write(stdio_buffer, len);
3115 total_bytes += len;
3116 }
3117 }
3118 stream->Flush();
3119 }
3120 return total_bytes;
3121}
3122
Greg Claytondc6224e2014-10-21 01:00:42 +00003123
Greg Clayton44d93782014-01-27 23:43:24 +00003124// This function handles events that were broadcast by the process.
3125void
3126Debugger::HandleProcessEvent (const EventSP &event_sp)
3127{
3128 using namespace lldb;
3129 const uint32_t event_type = event_sp->GetType();
3130 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
Greg Claytondc6224e2014-10-21 01:00:42 +00003131
Greg Claytonb4874f12014-02-28 18:22:24 +00003132 StreamString output_stream;
3133 StreamString error_stream;
Greg Clayton44d93782014-01-27 23:43:24 +00003134 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00003135
Greg Claytonb4874f12014-02-28 18:22:24 +00003136 if (!gui_enabled)
3137 {
3138 bool pop_process_io_handler = false;
3139 assert (process_sp);
Greg Claytondc6224e2014-10-21 01:00:42 +00003140
Greg Claytonb4874f12014-02-28 18:22:24 +00003141 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
Greg Clayton44d93782014-01-27 23:43:24 +00003142 {
Greg Claytonb4874f12014-02-28 18:22:24 +00003143 GetProcessSTDOUT (process_sp.get(), &output_stream);
3144 }
Greg Claytondc6224e2014-10-21 01:00:42 +00003145
Greg Claytonb4874f12014-02-28 18:22:24 +00003146 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
3147 {
3148 GetProcessSTDERR (process_sp.get(), &error_stream);
3149 }
Greg Claytondc6224e2014-10-21 01:00:42 +00003150
Greg Claytonb4874f12014-02-28 18:22:24 +00003151 if (event_type & Process::eBroadcastBitStateChanged)
3152 {
Greg Claytondc6224e2014-10-21 01:00:42 +00003153 Process::HandleProcessStateChangedEvent (event_sp, &output_stream, pop_process_io_handler);
Greg Clayton44d93782014-01-27 23:43:24 +00003154 }
Greg Claytondc6224e2014-10-21 01:00:42 +00003155
Greg Claytonb4874f12014-02-28 18:22:24 +00003156 if (output_stream.GetSize() || error_stream.GetSize())
3157 {
3158 StreamFileSP error_stream_sp (GetOutputFile());
Greg Clayton6fea17e2014-03-03 19:15:20 +00003159 bool top_io_handler_hid = false;
Greg Claytondc6224e2014-10-21 01:00:42 +00003160
Greg Clayton6fea17e2014-03-03 19:15:20 +00003161 if (process_sp->ProcessIOHandlerIsActive() == false)
3162 top_io_handler_hid = HideTopIOHandler();
Greg Claytonb4874f12014-02-28 18:22:24 +00003163
3164 if (output_stream.GetSize())
3165 {
3166 StreamFileSP output_stream_sp (GetOutputFile());
3167 if (output_stream_sp)
3168 output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
3169 }
3170
3171 if (error_stream.GetSize())
3172 {
3173 StreamFileSP error_stream_sp (GetErrorFile());
3174 if (error_stream_sp)
3175 error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
3176 }
3177
3178 if (top_io_handler_hid)
3179 RefreshTopIOHandler();
3180 }
3181
3182 if (pop_process_io_handler)
3183 process_sp->PopProcessIOHandler();
3184 }
Greg Clayton44d93782014-01-27 23:43:24 +00003185}
3186
3187void
3188Debugger::HandleThreadEvent (const EventSP &event_sp)
3189{
3190 // At present the only thread event we handle is the Frame Changed event,
3191 // and all we do for that is just reprint the thread status for that thread.
3192 using namespace lldb;
3193 const uint32_t event_type = event_sp->GetType();
3194 if (event_type == Thread::eBroadcastBitStackChanged ||
3195 event_type == Thread::eBroadcastBitThreadSelected )
3196 {
3197 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
3198 if (thread_sp)
3199 {
3200 HideTopIOHandler();
3201 StreamFileSP stream_sp (GetOutputFile());
3202 thread_sp->GetStatus(*stream_sp, 0, 1, 1);
3203 RefreshTopIOHandler();
3204 }
3205 }
3206}
3207
3208bool
3209Debugger::IsForwardingEvents ()
3210{
3211 return (bool)m_forward_listener_sp;
3212}
3213
3214void
3215Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
3216{
3217 m_forward_listener_sp = listener_sp;
3218}
3219
3220void
3221Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
3222{
3223 m_forward_listener_sp.reset();
3224}
3225
3226
3227void
3228Debugger::DefaultEventHandler()
3229{
3230 Listener& listener(GetListener());
3231 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
3232 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
3233 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
3234 BroadcastEventSpec target_event_spec (broadcaster_class_target,
3235 Target::eBroadcastBitBreakpointChanged);
3236
3237 BroadcastEventSpec process_event_spec (broadcaster_class_process,
3238 Process::eBroadcastBitStateChanged |
3239 Process::eBroadcastBitSTDOUT |
3240 Process::eBroadcastBitSTDERR);
3241
3242 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
3243 Thread::eBroadcastBitStackChanged |
3244 Thread::eBroadcastBitThreadSelected );
3245
3246 listener.StartListeningForEventSpec (*this, target_event_spec);
3247 listener.StartListeningForEventSpec (*this, process_event_spec);
3248 listener.StartListeningForEventSpec (*this, thread_event_spec);
3249 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
3250 CommandInterpreter::eBroadcastBitQuitCommandReceived |
3251 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
3252 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
3253
3254 bool done = false;
3255 while (!done)
3256 {
3257// Mutex::Locker locker;
3258// if (locker.TryLock(m_input_reader_stack.GetMutex()))
3259// {
3260// if (m_input_reader_stack.IsEmpty())
3261// break;
3262// }
3263//
3264 EventSP event_sp;
3265 if (listener.WaitForEvent(NULL, event_sp))
3266 {
3267 if (event_sp)
3268 {
3269 Broadcaster *broadcaster = event_sp->GetBroadcaster();
3270 if (broadcaster)
3271 {
3272 uint32_t event_type = event_sp->GetType();
3273 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
3274 if (broadcaster_class == broadcaster_class_process)
3275 {
3276 HandleProcessEvent (event_sp);
3277 }
3278 else if (broadcaster_class == broadcaster_class_target)
3279 {
3280 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
3281 {
3282 HandleBreakpointEvent (event_sp);
3283 }
3284 }
3285 else if (broadcaster_class == broadcaster_class_thread)
3286 {
3287 HandleThreadEvent (event_sp);
3288 }
3289 else if (broadcaster == m_command_interpreter_ap.get())
3290 {
3291 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
3292 {
3293 done = true;
3294 }
3295 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
3296 {
3297 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3298 if (data && data[0])
3299 {
3300 StreamFileSP error_sp (GetErrorFile());
3301 if (error_sp)
3302 {
3303 HideTopIOHandler();
3304 error_sp->PutCString(data);
3305 error_sp->Flush();
3306 RefreshTopIOHandler();
3307 }
3308 }
3309 }
3310 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
3311 {
3312 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3313 if (data && data[0])
3314 {
3315 StreamFileSP output_sp (GetOutputFile());
3316 if (output_sp)
3317 {
3318 HideTopIOHandler();
3319 output_sp->PutCString(data);
3320 output_sp->Flush();
3321 RefreshTopIOHandler();
3322 }
3323 }
3324 }
3325 }
3326 }
3327
3328 if (m_forward_listener_sp)
3329 m_forward_listener_sp->AddEvent(event_sp);
3330 }
3331 }
3332 }
3333}
3334
3335lldb::thread_result_t
3336Debugger::EventHandlerThread (lldb::thread_arg_t arg)
3337{
3338 ((Debugger *)arg)->DefaultEventHandler();
3339 return NULL;
3340}
3341
3342bool
3343Debugger::StartEventHandlerThread()
3344{
Zachary Turneracee96a2014-09-23 18:32:09 +00003345 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00003346 {
Zachary Turner7c2896a2014-10-24 22:06:29 +00003347 // Use larger 8MB stack for this thread
3348 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread, this, NULL,
3349 g_debugger_event_thread_stack_bytes);
Greg Clayton807b6b32014-10-15 18:03:59 +00003350 }
Zachary Turneracee96a2014-09-23 18:32:09 +00003351 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00003352}
3353
3354void
3355Debugger::StopEventHandlerThread()
3356{
Zachary Turneracee96a2014-09-23 18:32:09 +00003357 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00003358 {
3359 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00003360 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00003361 }
3362}
3363
3364
3365lldb::thread_result_t
3366Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3367{
3368 Debugger *debugger = (Debugger *)arg;
3369 debugger->ExecuteIOHanders();
3370 debugger->StopEventHandlerThread();
3371 return NULL;
3372}
3373
3374bool
3375Debugger::StartIOHandlerThread()
3376{
Zachary Turneracee96a2014-09-23 18:32:09 +00003377 if (!m_io_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00003378 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler",
3379 IOHandlerThread,
3380 this,
3381 NULL,
3382 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00003383 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00003384}
3385
3386void
3387Debugger::StopIOHandlerThread()
3388{
Zachary Turneracee96a2014-09-23 18:32:09 +00003389 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00003390 {
3391 if (m_input_file_sp)
3392 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00003393 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00003394 }
3395}
3396
3397