blob: 70038d33536f44dce84d39aa0c8a57f1e45d1758 [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;
67
Greg Clayton1b654882010-09-19 02:33:57 +000068#pragma mark Static Functions
69
70static Mutex &
71GetDebuggerListMutex ()
72{
73 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
74 return g_mutex;
75}
76
77typedef std::vector<DebuggerSP> DebuggerList;
78
79static DebuggerList &
80GetDebuggerList()
81{
82 // hide the static debugger list inside a singleton accessor to avoid
Bruce Mitchener6a7f3332014-06-27 02:42:12 +000083 // global init constructors
Greg Clayton1b654882010-09-19 02:33:57 +000084 static DebuggerList g_list;
85 return g_list;
86}
Greg Claytone372b982011-11-21 21:44:34 +000087
88OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000089g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000090{
Greg Clayton67cc0632012-08-22 17:17:09 +000091 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
92 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
93 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000094 { 0, NULL, NULL }
95};
96
Greg Clayton67cc0632012-08-22 17:17:09 +000097OptionEnumValueElement
98g_language_enumerators[] =
99{
100 { eScriptLanguageNone, "none", "Disable scripting languages."},
101 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
102 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +0000103 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000104};
Greg Claytone372b982011-11-21 21:44:34 +0000105
Greg Clayton67cc0632012-08-22 17:17:09 +0000106#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
107#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
108
Michael Sartain0769b2b2013-07-30 16:44:36 +0000109#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000110 "{, ${frame.pc}}"\
111 MODULE_WITH_FUNC\
112 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000113 "{, name = '${thread.name}'}"\
114 "{, queue = '${thread.queue}'}"\
Jason Molenda705b1802014-06-13 02:37:02 +0000115 "{, activity = '${thread.info.activity.name}'}" \
116 "{, ${thread.info.trace_messages} messages}" \
Greg Clayton67cc0632012-08-22 17:17:09 +0000117 "{, stop reason = ${thread.stop-reason}}"\
118 "{\\nReturn value: ${thread.return-value}}"\
Jim Ingham30fadaf2014-07-08 01:07:32 +0000119 "{\\nCompleted expression: ${thread.completed-expression}}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000120 "\\n"
121
122#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
123 MODULE_WITH_FUNC\
124 FILE_AND_LINE\
125 "\\n"
126
Jason Molendaaff1b352014-10-10 23:07:36 +0000127#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 +0000128
129
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 Granata90a8db32013-10-31 21:01:07 +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)." },
Greg Claytone8cd0c92012-10-19 18:02:49 +0000148
149 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000150};
151
152enum
153{
154 ePropertyAutoConfirm = 0,
Jason Molendaaff1b352014-10-10 23:07:36 +0000155 ePropertyDisassemblyFormat,
Greg Clayton67cc0632012-08-22 17:17:09 +0000156 ePropertyFrameFormat,
157 ePropertyNotiftVoid,
158 ePropertyPrompt,
159 ePropertyScriptLanguage,
160 ePropertyStopDisassemblyCount,
161 ePropertyStopDisassemblyDisplay,
162 ePropertyStopLineCountAfter,
163 ePropertyStopLineCountBefore,
164 ePropertyTerminalWidth,
165 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000166 ePropertyUseExternalEditor,
167 ePropertyUseColor,
Enrico Granata90a8db32013-10-31 21:01:07 +0000168 ePropertyAutoOneLineSummaries
Greg Clayton67cc0632012-08-22 17:17:09 +0000169};
170
Greg Clayton5fb8f792013-12-02 19:35:49 +0000171Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000172
173Error
174Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
175 VarSetOperationType op,
176 const char *property_path,
177 const char *value)
178{
Enrico Granata84a53df2013-05-20 22:29:23 +0000179 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
180 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000181 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000182 if (is_load_script && exe_ctx->GetTargetSP())
183 {
184 target_sp = exe_ctx->GetTargetSP();
185 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
186 }
Greg Clayton4c054102012-09-01 00:38:36 +0000187 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
188 if (error.Success())
189 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000190 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000191 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
192 {
193 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000194 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
195 if (str.length())
196 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000197 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000198 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
199 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
200 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000201 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
202 {
203 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
204 SetPrompt (GetPrompt());
205 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000206 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000207 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000208 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000209 {
210 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000211 StreamString feedback_stream;
212 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000213 {
Greg Clayton44d93782014-01-27 23:43:24 +0000214 StreamFileSP stream_sp (GetErrorFile());
215 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000216 {
Greg Clayton44d93782014-01-27 23:43:24 +0000217 for (auto error : errors)
218 {
219 stream_sp->Printf("%s\n",error.AsCString());
220 }
221 if (feedback_stream.GetSize())
222 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000223 }
224 }
225 }
226 }
Greg Clayton4c054102012-09-01 00:38:36 +0000227 }
228 return error;
229}
230
Greg Clayton67cc0632012-08-22 17:17:09 +0000231bool
232Debugger::GetAutoConfirm () const
233{
234 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000235 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000236}
237
238const char *
Jason Molendaaff1b352014-10-10 23:07:36 +0000239Debugger::GetDisassemblyFormat() const
240{
241 const uint32_t idx = ePropertyDisassemblyFormat;
242 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
243}
244
245const char *
Greg Clayton67cc0632012-08-22 17:17:09 +0000246Debugger::GetFrameFormat() const
247{
248 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000249 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000250}
251
252bool
253Debugger::GetNotifyVoid () const
254{
255 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000256 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000257}
258
259const char *
260Debugger::GetPrompt() const
261{
262 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000263 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000264}
265
266void
267Debugger::SetPrompt(const char *p)
268{
269 const uint32_t idx = ePropertyPrompt;
270 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
271 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000272 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
273 if (str.length())
274 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000275 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000276}
277
278const char *
279Debugger::GetThreadFormat() const
280{
281 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000282 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000283}
284
285lldb::ScriptLanguage
286Debugger::GetScriptLanguage() const
287{
288 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000289 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000290}
291
292bool
293Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
294{
295 const uint32_t idx = ePropertyScriptLanguage;
296 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
297}
298
299uint32_t
300Debugger::GetTerminalWidth () const
301{
302 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000303 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000304}
305
306bool
307Debugger::SetTerminalWidth (uint32_t term_width)
308{
309 const uint32_t idx = ePropertyTerminalWidth;
310 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
311}
312
313bool
314Debugger::GetUseExternalEditor () const
315{
316 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000317 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000318}
319
320bool
321Debugger::SetUseExternalEditor (bool b)
322{
323 const uint32_t idx = ePropertyUseExternalEditor;
324 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
325}
326
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000327bool
328Debugger::GetUseColor () const
329{
330 const uint32_t idx = ePropertyUseColor;
331 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
332}
333
334bool
335Debugger::SetUseColor (bool b)
336{
337 const uint32_t idx = ePropertyUseColor;
338 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
339 SetPrompt (GetPrompt());
340 return ret;
341}
342
Greg Clayton67cc0632012-08-22 17:17:09 +0000343uint32_t
344Debugger::GetStopSourceLineCount (bool before) const
345{
346 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000347 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000348}
349
350Debugger::StopDisassemblyType
351Debugger::GetStopDisassemblyDisplay () const
352{
353 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000354 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000355}
356
357uint32_t
358Debugger::GetDisassemblyLineCount () const
359{
360 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000361 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000362}
Greg Claytone372b982011-11-21 21:44:34 +0000363
Enrico Granata553fad52013-10-25 23:09:40 +0000364bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000365Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000366{
Enrico Granata90a8db32013-10-31 21:01:07 +0000367 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000368 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
369
370}
371
Greg Clayton1b654882010-09-19 02:33:57 +0000372#pragma mark Debugger
373
Greg Clayton67cc0632012-08-22 17:17:09 +0000374//const DebuggerPropertiesSP &
375//Debugger::GetSettings() const
376//{
377// return m_properties_sp;
378//}
379//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000380
Caroline Tice2f88aad2011-01-14 00:29:16 +0000381int
382Debugger::TestDebuggerRefCount ()
383{
384 return g_shared_debugger_refcount;
385}
386
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387void
Greg Clayton5fb8f792013-12-02 19:35:49 +0000388Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000390 g_load_plugin_callback = load_plugin_callback;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000391 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000392 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393}
394
395void
396Debugger::Terminate ()
397{
Greg Clayton66111032010-06-23 01:19:29 +0000398 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 {
Greg Clayton66111032010-06-23 01:19:29 +0000400 g_shared_debugger_refcount--;
401 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 {
Greg Claytondbe54502010-11-19 03:46:01 +0000403 lldb_private::WillTerminate();
404 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000405
406 // Clear our master list of debugger objects
407 Mutex::Locker locker (GetDebuggerListMutex ());
408 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 }
411}
412
Caroline Tice20bd37f2011-03-10 22:14:10 +0000413void
414Debugger::SettingsInitialize ()
415{
Greg Clayton6920b522012-08-22 18:39:03 +0000416 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000417}
418
419void
420Debugger::SettingsTerminate ()
421{
Greg Clayton6920b522012-08-22 18:39:03 +0000422 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000423}
424
Enrico Granata21dfcd92012-09-28 23:57:51 +0000425bool
Enrico Granatae743c782013-04-24 21:29:08 +0000426Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000427{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000428 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000429 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000430 llvm::sys::DynamicLibrary dynlib = g_load_plugin_callback (shared_from_this(), spec, error);
431 if (dynlib.isValid())
Greg Clayton5fb8f792013-12-02 19:35:49 +0000432 {
Zachary Turner58a559c2014-08-27 20:15:09 +0000433 m_loaded_plugins.push_back(dynlib);
Greg Clayton5fb8f792013-12-02 19:35:49 +0000434 return true;
435 }
Enrico Granatae743c782013-04-24 21:29:08 +0000436 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000437 else
Enrico Granatae743c782013-04-24 21:29:08 +0000438 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000439 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
440 // and if the public API layer isn't available (code is linking against
441 // all of the internal LLDB static libraries), then we can't load plugins
442 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000443 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000444 return false;
445}
446
447static FileSpec::EnumerateDirectoryResult
448LoadPluginCallback
449(
450 void *baton,
451 FileSpec::FileType file_type,
452 const FileSpec &file_spec
453 )
454{
455 Error error;
456
457 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000458 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000459
460 if (!baton)
461 return FileSpec::eEnumerateDirectoryResultQuit;
462
463 Debugger *debugger = (Debugger*)baton;
464
465 // If we have a regular file, a symbolic link or unknown file type, try
466 // and process the file. We must handle unknown as sometimes the directory
467 // enumeration might be enumerating a file system that doesn't have correct
468 // file type information.
469 if (file_type == FileSpec::eFileTypeRegular ||
470 file_type == FileSpec::eFileTypeSymbolicLink ||
471 file_type == FileSpec::eFileTypeUnknown )
472 {
473 FileSpec plugin_file_spec (file_spec);
474 plugin_file_spec.ResolvePath ();
475
Michael Sartain3cf443d2013-07-17 00:26:30 +0000476 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
477 plugin_file_spec.GetFileNameExtension() != g_solibext)
478 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000479 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000480 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000481
Enrico Granatae743c782013-04-24 21:29:08 +0000482 Error plugin_load_error;
483 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000484
485 return FileSpec::eEnumerateDirectoryResultNext;
486 }
487
488 else if (file_type == FileSpec::eFileTypeUnknown ||
489 file_type == FileSpec::eFileTypeDirectory ||
490 file_type == FileSpec::eFileTypeSymbolicLink )
491 {
492 // Try and recurse into anything that a directory or symbolic link.
493 // We must also do this for unknown as sometimes the directory enumeration
Bruce Mitchener6a7f3332014-06-27 02:42:12 +0000494 // might be enumerating a file system that doesn't have correct file type
Enrico Granata21dfcd92012-09-28 23:57:51 +0000495 // information.
496 return FileSpec::eEnumerateDirectoryResultEnter;
497 }
498
499 return FileSpec::eEnumerateDirectoryResultNext;
500}
501
502void
503Debugger::InstanceInitialize ()
504{
505 FileSpec dir_spec;
506 const bool find_directories = true;
507 const bool find_files = true;
508 const bool find_other = true;
509 char dir_path[PATH_MAX];
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000510 if (HostInfo::GetLLDBPath(ePathTypeLLDBSystemPlugins, dir_spec))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000511 {
512 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
513 {
514 FileSpec::EnumerateDirectory (dir_path,
515 find_directories,
516 find_files,
517 find_other,
518 LoadPluginCallback,
519 this);
520 }
521 }
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000522
523 if (HostInfo::GetLLDBPath(ePathTypeLLDBUserPlugins, 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 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000535
536 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000537}
538
Greg Clayton66111032010-06-23 01:19:29 +0000539DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000540Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000541{
Jim Ingham228063c2012-02-21 02:23:08 +0000542 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000543 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000544 {
545 Mutex::Locker locker (GetDebuggerListMutex ());
546 GetDebuggerList().push_back(debugger_sp);
547 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000548 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000549 return debugger_sp;
550}
551
Caroline Ticee02657b2011-01-22 01:02:07 +0000552void
Greg Clayton4d122c42011-09-17 08:33:22 +0000553Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000554{
555 if (debugger_sp.get() == NULL)
556 return;
557
Jim Ingham8314c522011-09-15 21:36:42 +0000558 debugger_sp->Clear();
559
Greg Claytonc15f55e2012-03-30 20:53:46 +0000560 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000561 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000562 Mutex::Locker locker (GetDebuggerListMutex ());
563 DebuggerList &debugger_list = GetDebuggerList ();
564 DebuggerList::iterator pos, end = debugger_list.end();
565 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000566 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000567 if ((*pos).get() == debugger_sp.get())
568 {
569 debugger_list.erase (pos);
570 return;
571 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000572 }
573 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000574}
575
Greg Clayton4d122c42011-09-17 08:33:22 +0000576DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000577Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
578{
Greg Clayton4d122c42011-09-17 08:33:22 +0000579 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000580 if (g_shared_debugger_refcount > 0)
581 {
582 Mutex::Locker locker (GetDebuggerListMutex ());
583 DebuggerList &debugger_list = GetDebuggerList();
584 DebuggerList::iterator pos, end = debugger_list.end();
585
586 for (pos = debugger_list.begin(); pos != end; ++pos)
587 {
588 if ((*pos).get()->m_instance_name == instance_name)
589 {
590 debugger_sp = *pos;
591 break;
592 }
593 }
594 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000595 return debugger_sp;
596}
Greg Clayton66111032010-06-23 01:19:29 +0000597
598TargetSP
599Debugger::FindTargetWithProcessID (lldb::pid_t pid)
600{
Greg Clayton4d122c42011-09-17 08:33:22 +0000601 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000602 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000603 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000604 Mutex::Locker locker (GetDebuggerListMutex ());
605 DebuggerList &debugger_list = GetDebuggerList();
606 DebuggerList::iterator pos, end = debugger_list.end();
607 for (pos = debugger_list.begin(); pos != end; ++pos)
608 {
609 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
610 if (target_sp)
611 break;
612 }
Greg Clayton66111032010-06-23 01:19:29 +0000613 }
614 return target_sp;
615}
616
Greg Claytone4e45922011-11-16 05:37:56 +0000617TargetSP
618Debugger::FindTargetWithProcess (Process *process)
619{
620 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000621 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000622 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000623 Mutex::Locker locker (GetDebuggerListMutex ());
624 DebuggerList &debugger_list = GetDebuggerList();
625 DebuggerList::iterator pos, end = debugger_list.end();
626 for (pos = debugger_list.begin(); pos != end; ++pos)
627 {
628 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
629 if (target_sp)
630 break;
631 }
Greg Claytone4e45922011-11-16 05:37:56 +0000632 }
633 return target_sp;
634}
635
Jason Molendae6481c72014-09-12 01:50:46 +0000636Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) :
637 UserID(g_unique_id++),
638 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
639 m_input_file_sp(new StreamFile(stdin, false)),
640 m_output_file_sp(new StreamFile(stdout, false)),
641 m_error_file_sp(new StreamFile(stderr, false)),
642 m_terminal_state(),
643 m_target_list(*this),
644 m_platform_list(),
645 m_listener("lldb.Debugger"),
646 m_source_manager_ap(),
647 m_source_file_cache(),
648 m_command_interpreter_ap(new CommandInterpreter(*this, eScriptLanguageDefault, false)),
649 m_input_reader_stack(),
650 m_instance_name(),
651 m_loaded_plugins()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652{
Greg Clayton67cc0632012-08-22 17:17:09 +0000653 char instance_cstr[256];
654 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
655 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000656 if (log_callback)
657 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000658 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000659 // Always add our default platform to the platform list
Greg Clayton615eb7e2014-09-19 20:11:50 +0000660 PlatformSP default_platform_sp (Platform::GetHostPlatform());
Greg Claytonded470d2011-03-19 01:12:21 +0000661 assert (default_platform_sp.get());
662 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000663
Greg Clayton754a9362012-08-23 00:22:02 +0000664 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000665 m_collection_sp->AppendProperty (ConstString("target"),
666 ConstString("Settings specify to debugging targets."),
667 true,
668 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000669 if (m_command_interpreter_ap.get())
670 {
671 m_collection_sp->AppendProperty (ConstString("interpreter"),
672 ConstString("Settings specify to the debugger's command interpreter."),
673 true,
674 m_command_interpreter_ap->GetValueProperties());
675 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000676 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
677 term_width->SetMinimumValue(10);
678 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000679
680 // Turn off use-color if this is a dumb terminal.
681 const char *term = getenv ("TERM");
682 if (term && !strcmp (term, "dumb"))
683 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684}
685
686Debugger::~Debugger ()
687{
Jim Ingham8314c522011-09-15 21:36:42 +0000688 Clear();
689}
690
691void
692Debugger::Clear()
693{
Greg Clayton44d93782014-01-27 23:43:24 +0000694 ClearIOHandlers();
695 StopIOHandlerThread();
696 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000697 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000698 int num_targets = m_target_list.GetNumTargets();
699 for (int i = 0; i < num_targets; i++)
700 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000701 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
702 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000703 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000704 ProcessSP process_sp (target_sp->GetProcessSP());
705 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000706 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000707 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000708 }
Greg Clayton66111032010-06-23 01:19:29 +0000709 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000710 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000711
712 // Close the input file _before_ we close the input read communications class
713 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000714 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000715 if (m_input_file_sp)
716 m_input_file_sp->GetFile().Close ();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000717
718 m_command_interpreter_ap->Clear();
Jim Ingham8314c522011-09-15 21:36:42 +0000719}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720
721bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000722Debugger::GetCloseInputOnEOF () const
723{
Greg Clayton44d93782014-01-27 23:43:24 +0000724// return m_input_comm.GetCloseOnEOF();
725 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000726}
727
728void
729Debugger::SetCloseInputOnEOF (bool b)
730{
Greg Clayton44d93782014-01-27 23:43:24 +0000731// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000732}
733
734bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735Debugger::GetAsyncExecution ()
736{
Greg Clayton66111032010-06-23 01:19:29 +0000737 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738}
739
740void
741Debugger::SetAsyncExecution (bool async_execution)
742{
Greg Clayton66111032010-06-23 01:19:29 +0000743 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744}
745
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746
747void
748Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
749{
Greg Clayton44d93782014-01-27 23:43:24 +0000750 if (m_input_file_sp)
751 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
752 else
753 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
754
755 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000756 if (in_file.IsValid() == false)
757 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758
Jim Inghamc5917d92012-11-30 20:23:19 +0000759 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
760 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761}
762
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763void
764Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
765{
Greg Clayton44d93782014-01-27 23:43:24 +0000766 if (m_output_file_sp)
767 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
768 else
769 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
770
771 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000772 if (out_file.IsValid() == false)
773 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000774
Enrico Granatab5887262012-10-29 21:18:03 +0000775 // do not create the ScriptInterpreter just for setting the output file handle
776 // as the constructor will know how to do the right thing on its own
777 const bool can_create = false;
778 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
779 if (script_interpreter)
780 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781}
782
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783void
784Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
785{
Greg Clayton44d93782014-01-27 23:43:24 +0000786 if (m_error_file_sp)
787 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
788 else
789 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
790
791 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000792 if (err_file.IsValid() == false)
793 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794}
795
Jim Inghamc5917d92012-11-30 20:23:19 +0000796void
797Debugger::SaveInputTerminalState ()
798{
Greg Clayton44d93782014-01-27 23:43:24 +0000799 if (m_input_file_sp)
800 {
801 File &in_file = m_input_file_sp->GetFile();
802 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
803 m_terminal_state.Save(in_file.GetDescriptor(), true);
804 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000805}
806
807void
808Debugger::RestoreInputTerminalState ()
809{
810 m_terminal_state.Restore();
811}
812
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000814Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815{
816 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000817 TargetSP target_sp(GetSelectedTarget());
818 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819
820 if (target_sp)
821 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000822 ProcessSP process_sp (target_sp->GetProcessSP());
823 exe_ctx.SetProcessSP (process_sp);
824 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000826 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
827 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000829 exe_ctx.SetThreadSP (thread_sp);
830 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
831 if (exe_ctx.GetFramePtr() == NULL)
832 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833 }
834 }
835 }
836 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837}
838
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839void
Caroline Ticeefed6132010-11-19 20:47:54 +0000840Debugger::DispatchInputInterrupt ()
841{
Greg Clayton44d93782014-01-27 23:43:24 +0000842 Mutex::Locker locker (m_input_reader_stack.GetMutex());
843 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000844 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000845 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000846}
847
848void
849Debugger::DispatchInputEndOfFile ()
850{
Greg Clayton44d93782014-01-27 23:43:24 +0000851 Mutex::Locker locker (m_input_reader_stack.GetMutex());
852 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000853 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000854 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000855}
856
857void
Greg Clayton44d93782014-01-27 23:43:24 +0000858Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000859{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000860 // 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 +0000861 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000862 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000863 {
Greg Clayton44d93782014-01-27 23:43:24 +0000864 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000865 if (reader_sp)
866 {
Greg Clayton44d93782014-01-27 23:43:24 +0000867 m_input_reader_stack.Pop();
868 reader_sp->SetIsDone(true);
Greg Claytone68f5d62014-02-24 22:50:57 +0000869 reader_sp->Cancel();
Caroline Tice3d6086f2010-12-20 18:35:50 +0000870 }
871 }
872}
873
874void
Greg Clayton44d93782014-01-27 23:43:24 +0000875Debugger::ExecuteIOHanders()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000876{
Caroline Tice9088b062011-05-09 23:06:58 +0000877
Greg Clayton44d93782014-01-27 23:43:24 +0000878 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879 {
Greg Clayton44d93782014-01-27 23:43:24 +0000880 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881 if (!reader_sp)
882 break;
883
Greg Clayton44d93782014-01-27 23:43:24 +0000884 reader_sp->Activate();
885 reader_sp->Run();
886 reader_sp->Deactivate();
887
888 // Remove all input readers that are done from the top of the stack
889 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890 {
Greg Clayton44d93782014-01-27 23:43:24 +0000891 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
892 if (top_reader_sp && top_reader_sp->GetIsDone())
893 m_input_reader_stack.Pop();
894 else
895 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896 }
897 }
Greg Clayton44d93782014-01-27 23:43:24 +0000898 ClearIOHandlers();
899}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900
Greg Clayton44d93782014-01-27 23:43:24 +0000901bool
902Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
903{
904 return m_input_reader_stack.IsTop (reader_sp);
905}
906
907
908ConstString
909Debugger::GetTopIOHandlerControlSequence(char ch)
910{
911 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912}
913
914void
Greg Clayton44d93782014-01-27 23:43:24 +0000915Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
916{
Greg Clayton44d93782014-01-27 23:43:24 +0000917 PushIOHandler (reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +0000918
919 IOHandlerSP top_reader_sp = reader_sp;
920 while (top_reader_sp)
921 {
922 top_reader_sp->Activate();
923 top_reader_sp->Run();
924 top_reader_sp->Deactivate();
925
926 if (top_reader_sp.get() == reader_sp.get())
927 {
928 if (PopIOHandler (reader_sp))
929 break;
930 }
931
932 while (1)
933 {
934 top_reader_sp = m_input_reader_stack.Top();
935 if (top_reader_sp && top_reader_sp->GetIsDone())
936 m_input_reader_stack.Pop();
937 else
938 break;
939 }
940 }
Greg Clayton44d93782014-01-27 23:43:24 +0000941}
942
943void
944Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
945{
946 // Before an IOHandler runs, it must have in/out/err streams.
947 // This function is called when one ore more of the streams
948 // are NULL. We use the top input reader's in/out/err streams,
949 // or fall back to the debugger file handles, or we fall back
950 // onto stdin/stdout/stderr as a last resort.
951
952 Mutex::Locker locker (m_input_reader_stack.GetMutex());
953 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
954 // If no STDIN has been set, then set it appropriately
955 if (!in)
956 {
957 if (top_reader_sp)
958 in = top_reader_sp->GetInputStreamFile();
959 else
960 in = GetInputFile();
961
962 // If there is nothing, use stdin
963 if (!in)
964 in = StreamFileSP(new StreamFile(stdin, false));
965 }
966 // If no STDOUT has been set, then set it appropriately
967 if (!out)
968 {
969 if (top_reader_sp)
970 out = top_reader_sp->GetOutputStreamFile();
971 else
972 out = GetOutputFile();
973
974 // If there is nothing, use stdout
975 if (!out)
976 out = StreamFileSP(new StreamFile(stdout, false));
977 }
978 // If no STDERR has been set, then set it appropriately
979 if (!err)
980 {
981 if (top_reader_sp)
982 err = top_reader_sp->GetErrorStreamFile();
983 else
984 err = GetErrorFile();
985
986 // If there is nothing, use stderr
987 if (!err)
988 err = StreamFileSP(new StreamFile(stdout, false));
989
990 }
991}
992
993void
994Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995{
996 if (!reader_sp)
997 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000998
Greg Clayton44d93782014-01-27 23:43:24 +0000999 // Got the current top input reader...
1000 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001001
Greg Claytonb4874f12014-02-28 18:22:24 +00001002 // Don't push the same IO handler twice...
1003 if (reader_sp.get() != top_reader_sp.get())
1004 {
1005 // Push our new input reader
1006 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00001007
Greg Claytonb4874f12014-02-28 18:22:24 +00001008 // Interrupt the top input reader to it will exit its Run() function
1009 // and let this new input reader take over
1010 if (top_reader_sp)
1011 top_reader_sp->Deactivate();
1012 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013}
1014
1015bool
Greg Clayton44d93782014-01-27 23:43:24 +00001016Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017{
1018 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +00001019
1020 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021
1022 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001023 // read on the stack refresh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001024 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025 {
Greg Clayton44d93782014-01-27 23:43:24 +00001026 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027
1028 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
1029 {
Greg Clayton44d93782014-01-27 23:43:24 +00001030 reader_sp->Deactivate();
Greg Claytonb4874f12014-02-28 18:22:24 +00001031 reader_sp->Cancel();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001032 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +00001033
1034 reader_sp = m_input_reader_stack.Top();
1035 if (reader_sp)
1036 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037
Greg Clayton44d93782014-01-27 23:43:24 +00001038 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 }
1040 }
1041 return result;
1042}
1043
1044bool
Greg Clayton44d93782014-01-27 23:43:24 +00001045Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046{
Greg Clayton44d93782014-01-27 23:43:24 +00001047 Mutex::Locker locker;
1048
1049 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050 {
Greg Clayton44d93782014-01-27 23:43:24 +00001051 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1052 if (reader_sp)
1053 reader_sp->Hide();
1054 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055 }
Greg Clayton44d93782014-01-27 23:43:24 +00001056 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057}
1058
1059void
Greg Clayton44d93782014-01-27 23:43:24 +00001060Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061{
Greg Clayton44d93782014-01-27 23:43:24 +00001062 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1063 if (reader_sp)
1064 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065}
Greg Clayton66111032010-06-23 01:19:29 +00001066
Greg Clayton44d93782014-01-27 23:43:24 +00001067
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001068StreamSP
1069Debugger::GetAsyncOutputStream ()
1070{
1071 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1072 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1073}
1074
1075StreamSP
1076Debugger::GetAsyncErrorStream ()
1077{
1078 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1079 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1080}
1081
Greg Claytonc7bece562013-01-25 18:06:21 +00001082size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001083Debugger::GetNumDebuggers()
1084{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001085 if (g_shared_debugger_refcount > 0)
1086 {
1087 Mutex::Locker locker (GetDebuggerListMutex ());
1088 return GetDebuggerList().size();
1089 }
1090 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001091}
1092
1093lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001094Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001095{
1096 DebuggerSP debugger_sp;
1097
Greg Claytonc15f55e2012-03-30 20:53:46 +00001098 if (g_shared_debugger_refcount > 0)
1099 {
1100 Mutex::Locker locker (GetDebuggerListMutex ());
1101 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001102
Greg Claytonc15f55e2012-03-30 20:53:46 +00001103 if (index < debugger_list.size())
1104 debugger_sp = debugger_list[index];
1105 }
1106
Enrico Granata061858c2012-02-15 02:34:21 +00001107 return debugger_sp;
1108}
1109
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001110DebuggerSP
1111Debugger::FindDebuggerWithID (lldb::user_id_t id)
1112{
Greg Clayton4d122c42011-09-17 08:33:22 +00001113 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001114
Greg Claytonc15f55e2012-03-30 20:53:46 +00001115 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001116 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001117 Mutex::Locker locker (GetDebuggerListMutex ());
1118 DebuggerList &debugger_list = GetDebuggerList();
1119 DebuggerList::iterator pos, end = debugger_list.end();
1120 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001121 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001122 if ((*pos).get()->GetID() == id)
1123 {
1124 debugger_sp = *pos;
1125 break;
1126 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001127 }
1128 }
1129 return debugger_sp;
1130}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001131
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001132#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001133static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001134TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001135{
1136 if (frame == NULL)
1137 return;
1138
1139 StreamString s;
1140 const char *prompt_format =
1141 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001142 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1143 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001144 "{process.id = '${process.id}'\n}"
1145 "{process.name = '${process.name}'\n}"
1146 "{process.file.basename = '${process.file.basename}'\n}"
1147 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1148 "{thread.id = '${thread.id}'\n}"
1149 "{thread.index = '${thread.index}'\n}"
1150 "{thread.name = '${thread.name}'\n}"
1151 "{thread.queue = '${thread.queue}'\n}"
1152 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1153 "{target.arch = '${target.arch}'\n}"
1154 "{module.file.basename = '${module.file.basename}'\n}"
1155 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1156 "{file.basename = '${file.basename}'\n}"
1157 "{file.fullpath = '${file.fullpath}'\n}"
1158 "{frame.index = '${frame.index}'\n}"
1159 "{frame.pc = '${frame.pc}'\n}"
1160 "{frame.sp = '${frame.sp}'\n}"
1161 "{frame.fp = '${frame.fp}'\n}"
1162 "{frame.flags = '${frame.flags}'\n}"
1163 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1164 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1165 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1166 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1167 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1168 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1169 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1170 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001171 "{function.changed = '${function.changed}'\n}"
1172 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001173 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001174 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001175 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001176 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001177 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001178 "{function.line-offset = '${function.line-offset}'\n}"
1179 "{function.pc-offset = '${function.pc-offset}'\n}"
1180 "{line.file.basename = '${line.file.basename}'\n}"
1181 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1182 "{line.number = '${line.number}'\n}"
1183 "{line.start-addr = '${line.start-addr}'\n}"
1184 "{line.end-addr = '${line.end-addr}'\n}"
1185;
1186
1187 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1188 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001189 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001190 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001191 {
1192 printf("%s\n", s.GetData());
1193 }
1194 else
1195 {
Greg Clayton1b654882010-09-19 02:33:57 +00001196 printf ("what we got: %s\n", s.GetData());
1197 }
1198}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001199#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001200
Enrico Granata9fc19442011-07-06 02:13:41 +00001201static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001202ScanFormatDescriptor (const char* var_name_begin,
1203 const char* var_name_end,
1204 const char** var_name_final,
1205 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001206 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001207 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001208{
Greg Clayton5160ce52013-03-27 23:08:40 +00001209 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001210 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001211 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001212 {
1213 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001214 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001215 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001216 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001217 else
1218 {
1219 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001220 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001221 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001222 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001223 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001224 true,
1225 *custom_format) )
1226 {
Enrico Granatae992a082011-07-22 17:03:19 +00001227 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001228 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001229
1230 switch (format_name.front())
1231 {
1232 case '@': // if this is an @ sign, print ObjC description
1233 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1234 break;
1235 case 'V': // if this is a V, print the value using the default format
1236 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1237 break;
1238 case 'L': // if this is an L, print the location of the value
1239 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1240 break;
1241 case 'S': // if this is an S, print the summary after all
1242 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1243 break;
1244 case '#': // if this is a '#', print the number of children
1245 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1246 break;
1247 case 'T': // if this is a 'T', print the type
1248 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1249 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001250 case 'N': // if this is a 'N', print the name
1251 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1252 break;
1253 case '>': // if this is a '>', print the name
1254 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1255 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001256 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001257 if (log)
1258 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001259 break;
1260 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001261 }
1262 // a good custom format tells us to print the value using it
1263 else
Enrico Granatae992a082011-07-22 17:03:19 +00001264 {
1265 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001266 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001267 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001268 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001269 }
Enrico Granatae992a082011-07-22 17:03:19 +00001270 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001271 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001272 *custom_format,
1273 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001274 return true;
1275}
1276
1277static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001278ScanBracketedRange (const char* var_name_begin,
1279 const char* var_name_end,
1280 const char* var_name_final,
1281 const char** open_bracket_position,
1282 const char** separator_position,
1283 const char** close_bracket_position,
1284 const char** var_name_final_if_array_range,
1285 int64_t* index_lower,
1286 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001287{
Greg Clayton5160ce52013-03-27 23:08:40 +00001288 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001289 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001290 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001291 {
1292 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1293 *close_bracket_position = ::strchr(*open_bracket_position,']');
1294 // as usual, we assume that [] will come before %
1295 //printf("trying to expand a []\n");
1296 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001297 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001298 {
Enrico Granatae992a082011-07-22 17:03:19 +00001299 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001300 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001301 *index_lower = 0;
1302 }
1303 else if (*separator_position == NULL || *separator_position > var_name_end)
1304 {
1305 char *end = NULL;
1306 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1307 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001308 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001309 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001310 }
Greg Clayton34132752011-07-06 04:07:21 +00001311 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001312 {
1313 char *end = NULL;
1314 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1315 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001316 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001317 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001318 }
1319 else
Enrico Granatae992a082011-07-22 17:03:19 +00001320 {
1321 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001322 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001323 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001324 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001325 if (*index_lower > *index_higher && *index_higher > 0)
1326 {
Enrico Granatae992a082011-07-22 17:03:19 +00001327 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001328 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001329 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001330 *index_lower = *index_higher;
1331 *index_higher = temp;
1332 }
1333 }
Enrico Granatae992a082011-07-22 17:03:19 +00001334 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001335 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001336 return true;
1337}
1338
Michael Sartain0769b2b2013-07-30 16:44:36 +00001339template <typename T>
1340static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1341{
1342 if (script_interpreter)
1343 {
1344 Error script_error;
1345 std::string script_output;
1346
1347 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1348 {
1349 s.Printf("%s", script_output.c_str());
1350 return true;
1351 }
1352 else
1353 {
1354 s.Printf("<error: %s>",script_error.AsCString());
1355 }
1356 }
1357 return false;
1358}
1359
Enrico Granata9fc19442011-07-06 02:13:41 +00001360static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001361ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001362 size_t index,
Jason Molendab57e4a12013-11-04 09:33:30 +00001363 StackFrame* frame,
Enrico Granatadc940732011-08-23 00:32:52 +00001364 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001365{
Greg Clayton5160ce52013-03-27 23:08:40 +00001366 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001367 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001368 std::string ptr_deref_buffer(10,0);
1369 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001370 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001371 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001372 const char* first_unparsed;
1373 ValueObject::GetValueForExpressionPathOptions options;
1374 ValueObject::ExpressionPathEndResultType final_value_type;
1375 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001376 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001377 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001378 &first_unparsed,
1379 &reason_to_stop,
1380 &final_value_type,
1381 options,
1382 &what_next);
1383 if (!item)
1384 {
Enrico Granatae992a082011-07-22 17:03:19 +00001385 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001386 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001387 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001388 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001389 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001390 else
1391 {
Enrico Granatae992a082011-07-22 17:03:19 +00001392 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001393 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001394 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001395 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001396 }
1397 return item;
1398}
1399
Michael Sartain0769b2b2013-07-30 16:44:36 +00001400static inline bool
1401IsToken(const char *var_name_begin, const char *var)
1402{
1403 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1404}
1405
1406static bool
1407IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1408 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1409{
1410 int var_len = strlen(var);
1411 if (::strncmp (var_name_begin, var, var_len) == 0)
1412 {
1413 var_name_begin += var_len;
1414 if (*var_name_begin == '}')
1415 {
1416 format = default_format;
1417 return true;
1418 }
1419 else if (*var_name_begin == '%')
1420 {
1421 // Allow format specifiers: x|X|u with optional width specifiers.
1422 // ${thread.id%x} ; hex
1423 // ${thread.id%X} ; uppercase hex
1424 // ${thread.id%u} ; unsigned decimal
1425 // ${thread.id%8.8X} ; width.precision + specifier
1426 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1427 int dot_count = 0;
1428 const char *specifier = NULL;
1429 int width_precision_length = 0;
1430 const char *width_precision = ++var_name_begin;
1431 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1432 {
1433 dot_count += (*var_name_begin == '.');
1434 if (dot_count > 1)
1435 break;
1436 var_name_begin++;
1437 width_precision_length++;
1438 }
1439
1440 if (IsToken (var_name_begin, "tid}"))
1441 {
1442 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1443 if (target)
1444 {
1445 ArchSpec arch (target->GetArchitecture ());
1446 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1447 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1448 specifier = PRIu64;
1449 }
1450 if (!specifier)
1451 {
1452 format = default_format;
1453 return true;
1454 }
1455 }
1456 else if (IsToken (var_name_begin, "x}"))
1457 specifier = PRIx64;
1458 else if (IsToken (var_name_begin, "X}"))
1459 specifier = PRIX64;
1460 else if (IsToken (var_name_begin, "u}"))
1461 specifier = PRIu64;
1462
1463 if (specifier)
1464 {
1465 format = "%";
1466 if (width_precision_length)
1467 format += std::string(width_precision, width_precision_length);
1468 format += specifier;
1469 return true;
1470 }
1471 }
1472 }
1473 return false;
1474}
1475
Jason Molenda705b1802014-06-13 02:37:02 +00001476// Find information for the "thread.info.*" specifiers in a format string
1477static bool
1478FormatThreadExtendedInfoRecurse
1479(
1480 const char *var_name_begin,
1481 StructuredData::ObjectSP thread_info_dictionary,
1482 const SymbolContext *sc,
1483 const ExecutionContext *exe_ctx,
1484 Stream &s
1485)
1486{
1487 bool var_success = false;
1488 std::string token_format;
1489
1490 llvm::StringRef var_name(var_name_begin);
1491 size_t percent_idx = var_name.find('%');
1492 size_t close_curly_idx = var_name.find('}');
1493 llvm::StringRef path = var_name;
1494 llvm::StringRef formatter = var_name;
1495
1496 // 'path' will be the dot separated list of objects to transverse up until we hit
1497 // a close curly brace, a percent sign, or an end of string.
1498 if (percent_idx != llvm::StringRef::npos || close_curly_idx != llvm::StringRef::npos)
1499 {
1500 if (percent_idx != llvm::StringRef::npos && close_curly_idx != llvm::StringRef::npos)
1501 {
1502 if (percent_idx < close_curly_idx)
1503 {
1504 path = var_name.slice(0, percent_idx);
1505 formatter = var_name.substr (percent_idx);
1506 }
1507 else
1508 {
1509 path = var_name.slice(0, close_curly_idx);
1510 formatter = var_name.substr (close_curly_idx);
1511 }
1512 }
1513 else if (percent_idx != llvm::StringRef::npos)
1514 {
1515 path = var_name.slice(0, percent_idx);
1516 formatter = var_name.substr (percent_idx);
1517 }
1518 else if (close_curly_idx != llvm::StringRef::npos)
1519 {
1520 path = var_name.slice(0, close_curly_idx);
1521 formatter = var_name.substr (close_curly_idx);
1522 }
1523 }
1524
1525 StructuredData::ObjectSP value = thread_info_dictionary->GetObjectForDotSeparatedPath (path);
1526
1527 if (value.get())
1528 {
1529 if (value->GetType() == StructuredData::Type::eTypeInteger)
1530 {
1531 if (IsTokenWithFormat (formatter.str().c_str(), "", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
1532 {
1533 s.Printf(token_format.c_str(), value->GetAsInteger()->GetValue());
1534 var_success = true;
1535 }
1536 }
1537 else if (value->GetType() == StructuredData::Type::eTypeFloat)
1538 {
1539 s.Printf ("%f", value->GetAsFloat()->GetValue());
1540 var_success = true;
1541 }
1542 else if (value->GetType() == StructuredData::Type::eTypeString)
1543 {
1544 s.Printf("%s", value->GetAsString()->GetValue().c_str());
1545 var_success = true;
1546 }
1547 else if (value->GetType() == StructuredData::Type::eTypeArray)
1548 {
1549 if (value->GetAsArray()->GetSize() > 0)
1550 {
1551 s.Printf ("%zu", value->GetAsArray()->GetSize());
1552 var_success = true;
1553 }
1554 }
1555 else if (value->GetType() == StructuredData::Type::eTypeDictionary)
1556 {
1557 s.Printf ("%zu", value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize());
1558 var_success = true;
1559 }
1560 }
1561
1562 return var_success;
1563}
1564
1565
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001566static bool
1567FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001568(
1569 const char *format,
1570 const SymbolContext *sc,
1571 const ExecutionContext *exe_ctx,
1572 const Address *addr,
1573 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001574 const char **end,
Jason Molendaaff1b352014-10-10 23:07:36 +00001575 ValueObject* valobj,
1576 bool function_changed,
1577 bool initial_function
Greg Clayton1b654882010-09-19 02:33:57 +00001578)
1579{
Enrico Granatac482a192011-08-17 22:13:59 +00001580 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001581 bool success = true;
1582 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001583 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001584
Greg Clayton1b654882010-09-19 02:33:57 +00001585 for (p = format; *p != '\0'; ++p)
1586 {
Enrico Granatac482a192011-08-17 22:13:59 +00001587 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001588 {
Enrico Granatac482a192011-08-17 22:13:59 +00001589 valobj = realvalobj;
1590 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001591 }
Greg Clayton1b654882010-09-19 02:33:57 +00001592 size_t non_special_chars = ::strcspn (p, "${}\\");
1593 if (non_special_chars > 0)
1594 {
1595 if (success)
1596 s.Write (p, non_special_chars);
1597 p += non_special_chars;
1598 }
1599
1600 if (*p == '\0')
1601 {
1602 break;
1603 }
1604 else if (*p == '{')
1605 {
1606 // Start a new scope that must have everything it needs if it is to
1607 // to make it into the final output stream "s". If you want to make
1608 // a format that only prints out the function or symbol name if there
1609 // is one in the symbol context you can use:
1610 // "{function =${function.name}}"
1611 // The first '{' starts a new scope that end with the matching '}' at
1612 // the end of the string. The contents "function =${function.name}"
1613 // will then be evaluated and only be output if there is a function
1614 // or symbol with a valid name.
1615 StreamString sub_strm;
1616
1617 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001618
Jason Molendaaff1b352014-10-10 23:07:36 +00001619 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj, function_changed, initial_function))
Greg Clayton1b654882010-09-19 02:33:57 +00001620 {
1621 // The stream had all it needed
1622 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1623 }
1624 if (*p != '}')
1625 {
1626 success = false;
1627 break;
1628 }
1629 }
1630 else if (*p == '}')
1631 {
1632 // End of a enclosing scope
1633 break;
1634 }
1635 else if (*p == '$')
1636 {
1637 // We have a prompt variable to print
1638 ++p;
1639 if (*p == '{')
1640 {
1641 ++p;
1642 const char *var_name_begin = p;
1643 const char *var_name_end = ::strchr (p, '}');
1644
1645 if (var_name_end && var_name_begin < var_name_end)
1646 {
1647 // if we have already failed to parse, skip this variable
1648 if (success)
1649 {
1650 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001651 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001652 Address format_addr;
Jason Molendaaff1b352014-10-10 23:07:36 +00001653
1654 // normally "addr" means print a raw address but
1655 // "file-addr-or-load-addr" means print a module + file addr if there's no load addr
1656 bool print_file_addr_or_load_addr = false;
1657 bool addr_offset_concrete_func_only = false;
1658 bool addr_offset_print_with_no_padding = false;
Greg Clayton1b654882010-09-19 02:33:57 +00001659 bool calculate_format_addr_function_offset = false;
1660 // Set reg_kind and reg_num to invalid values
1661 RegisterKind reg_kind = kNumRegisterKinds;
1662 uint32_t reg_num = LLDB_INVALID_REGNUM;
1663 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001664 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001665 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001666 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001667 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1668 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001669
Greg Clayton1b654882010-09-19 02:33:57 +00001670 // Each variable must set success to true below...
1671 bool var_success = false;
1672 switch (var_name_begin[0])
1673 {
Enrico Granata4becb372011-06-29 22:27:15 +00001674 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001675 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001676 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001677 {
Enrico Granatac482a192011-08-17 22:13:59 +00001678 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001679 break;
1680
Enrico Granatac3e320a2011-08-02 17:27:39 +00001681 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001682 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001683
Enrico Granata6f3533f2011-07-29 19:53:35 +00001684 // check for *var and *svar
1685 if (*var_name_begin == '*')
1686 {
1687 do_deref_pointer = true;
1688 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001689 if (log)
1690 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001691 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001692
Enrico Granata6f3533f2011-07-29 19:53:35 +00001693 if (*var_name_begin == 's')
1694 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001695 if (!valobj->IsSynthetic())
1696 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001697 if (!valobj)
1698 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001699 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001700 if (log)
1701 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001702 }
1703
1704 // should be a 'v' by now
1705 if (*var_name_begin != 'v')
1706 break;
1707
Enrico Granatac3e320a2011-08-02 17:27:39 +00001708 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001709 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001710
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001711 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001712 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001713 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001714 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001715 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001716 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001717 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001718 const char* var_name_final = NULL;
1719 const char* var_name_final_if_array_range = NULL;
1720 const char* close_bracket_position = NULL;
1721 int64_t index_lower = -1;
1722 int64_t index_higher = -1;
1723 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001724 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001725 bool was_plain_var = false;
1726 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001727 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001728
Enrico Granatac482a192011-08-17 22:13:59 +00001729 if (!valobj) break;
1730 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001731 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001732 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001733 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001734 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001735 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001736 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001737 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001738 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001739 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001740 // this is a variable with some custom format applied to it
1741 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001742 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001743 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001744 ScanFormatDescriptor (var_name_begin,
1745 var_name_end,
1746 &var_name_final,
1747 &percent_position,
1748 &custom_format,
1749 &val_obj_display);
1750 }
1751 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001752 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001753 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001754 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001755 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001756 const char* percent_position;
1757 ScanFormatDescriptor (var_name_begin,
1758 var_name_end,
1759 &var_name_final,
1760 &percent_position,
1761 &custom_format,
1762 &val_obj_display);
1763
1764 const char* open_bracket_position;
1765 const char* separator_position;
1766 ScanBracketedRange (var_name_begin,
1767 var_name_end,
1768 var_name_final,
1769 &open_bracket_position,
1770 &separator_position,
1771 &close_bracket_position,
1772 &var_name_final_if_array_range,
1773 &index_lower,
1774 &index_higher);
1775
1776 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001777
Enrico Granata599171a2013-02-01 23:59:44 +00001778 std::string expr_path(var_name_final-var_name_begin-1,0);
1779 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1780
1781 if (log)
1782 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1783
1784 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001785 &first_unparsed,
1786 &reason_to_stop,
1787 &final_value_type,
1788 options,
1789 &what_next).get();
1790
1791 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001792 {
Enrico Granatae992a082011-07-22 17:03:19 +00001793 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001794 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001795 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001796 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001797 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001798 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001799 else
1800 {
Enrico Granatae992a082011-07-22 17:03:19 +00001801 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001802 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001803 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001804 first_unparsed, reason_to_stop, final_value_type);
1805 }
Enrico Granata4becb372011-06-29 22:27:15 +00001806 }
Greg Clayton34132752011-07-06 04:07:21 +00001807 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001808 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001809
Enrico Granata86cc9822012-03-19 22:58:49 +00001810 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1811 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001812
Enrico Granata86cc9822012-03-19 22:58:49 +00001813 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001814
Enrico Granataa7187d02011-07-06 19:27:11 +00001815 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001816 {
Greg Clayton34132752011-07-06 04:07:21 +00001817 // I have not deref-ed yet, let's do it
1818 // this happens when we are not going through GetValueForVariableExpressionPath
1819 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001820 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001821 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001822 if (error.Fail())
1823 {
1824 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001825 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001826 break;
1827 }
Greg Clayton34132752011-07-06 04:07:21 +00001828 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001829 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001830
Jim Inghamf164d942014-03-11 18:17:23 +00001831 if (!target)
1832 {
1833 if (log)
1834 log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
1835 break;
1836 }
1837
Enrico Granataa777dc22012-05-08 21:49:57 +00001838 // we do not want to use the summary for a bitfield of type T:n
1839 // if we were originally dealing with just a T - that would get
1840 // us into an endless recursion
1841 if (target->IsBitfield() && was_var_indexed)
1842 {
1843 // TODO: check for a (T:n)-specific summary - we should still obey that
1844 StreamString bitfield_name;
1845 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1846 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1847 if (!DataVisualization::GetSummaryForType(type_sp))
1848 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1849 }
1850
Enrico Granata85933ed2011-08-18 16:38:26 +00001851 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001852 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1853 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1854 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1855 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001856
Enrico Granata86cc9822012-03-19 22:58:49 +00001857 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 +00001858 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001859 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001860 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001861 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001862
Greg Clayton5088c482013-03-25 21:06:13 +00001863 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001864 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001865 // try to use the special cases
1866 var_success = target->DumpPrintableRepresentation(str_temp,
1867 val_obj_display,
1868 custom_format);
1869 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001870 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001871
1872 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001873 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001874 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001875 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001876 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001877 }
1878 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001879 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001880 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001881 {
1882 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1883 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001884 else if (is_pointer) // if pointer, value is the address stored
1885 {
Greg Clayton23f59502012-07-17 03:23:13 +00001886 target->DumpPrintableRepresentation (s,
1887 val_obj_display,
1888 custom_format,
1889 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001890 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001891 var_success = true;
1892 break;
1893 }
1894 }
1895
1896 // if directly trying to print ${var}, and this is an aggregate, display a nice
1897 // type @ location message
1898 if (is_aggregate && was_plain_var)
1899 {
1900 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1901 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001902 break;
1903 }
1904
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001905 // 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 +00001906 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001907 {
1908 s << "<invalid use of aggregate type>";
1909 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001910 break;
1911 }
Greg Clayton34132752011-07-06 04:07:21 +00001912
1913 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001914 {
1915 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001916 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001917 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001918 }
Greg Clayton34132752011-07-06 04:07:21 +00001919 else
Enrico Granatae992a082011-07-22 17:03:19 +00001920 {
1921 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001922 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001923 if (!is_array && !is_pointer)
1924 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001925 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001926 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001927 const char* special_directions = NULL;
1928 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001929 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1930 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001931 ConstString additional_data;
1932 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1933 special_directions_writer.Printf("${%svar%s}",
1934 do_deref_pointer ? "*" : "",
1935 additional_data.GetCString());
1936 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001937 }
1938
1939 // let us display items index_lower thru index_higher of this array
1940 s.PutChar('[');
1941 var_success = true;
1942
1943 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001944 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001945
Greg Claytoncc4d0142012-02-17 07:49:44 +00001946 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001947
Greg Clayton34132752011-07-06 04:07:21 +00001948 for (;index_lower<=index_higher;index_lower++)
1949 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001950 ValueObject* item = ExpandIndexedExpression (target,
1951 index_lower,
1952 exe_ctx->GetFramePtr(),
1953 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001954
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001955 if (!item)
1956 {
Enrico Granatae992a082011-07-22 17:03:19 +00001957 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001958 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001959 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001960 else
1961 {
Enrico Granatae992a082011-07-22 17:03:19 +00001962 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001963 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001964 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001965
Greg Clayton34132752011-07-06 04:07:21 +00001966 if (!special_directions)
1967 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1968 else
Jason Molendaaff1b352014-10-10 23:07:36 +00001969 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item, function_changed, initial_function);
Greg Clayton34132752011-07-06 04:07:21 +00001970
Enrico Granata22c55d12011-08-12 02:00:06 +00001971 if (--max_num_children == 0)
1972 {
1973 s.PutCString(", ...");
1974 break;
1975 }
1976
Greg Clayton34132752011-07-06 04:07:21 +00001977 if (index_lower < index_higher)
1978 s.PutChar(',');
1979 }
1980 s.PutChar(']');
1981 }
Enrico Granata4becb372011-06-29 22:27:15 +00001982 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001983 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001984 case 'a':
Jason Molendaaff1b352014-10-10 23:07:36 +00001985 if (IsToken (var_name_begin, "addr-file-or-load}"))
1986 {
1987 print_file_addr_or_load_addr = true;
1988 }
1989 if (IsToken (var_name_begin, "addr}")
1990 || IsToken (var_name_begin, "addr-file-or-load}"))
Greg Clayton1b654882010-09-19 02:33:57 +00001991 {
1992 if (addr && addr->IsValid())
1993 {
1994 var_success = true;
1995 format_addr = *addr;
1996 }
1997 }
1998 break;
1999
2000 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002001 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00002002 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002003 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002004 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002005 Process *process = exe_ctx->GetProcessPtr();
2006 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00002007 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002008 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002009 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002010 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002011 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002012 var_success = true;
2013 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002014 else if ((IsToken (var_name_begin, "name}")) ||
2015 (IsToken (var_name_begin, "file.basename}")) ||
2016 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00002017 {
2018 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
2019 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00002020 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002021 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
2022 {
2023 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002024 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00002025 }
2026 else
2027 {
2028 format_file_spec = exe_module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002029 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00002030 }
Greg Clayton1b654882010-09-19 02:33:57 +00002031 }
2032 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002033 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002034 {
2035 var_name_begin += ::strlen("script:");
2036 std::string script_name(var_name_begin,var_name_end);
2037 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002038 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
2039 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002040 }
Greg Clayton1b654882010-09-19 02:33:57 +00002041 }
Greg Claytonc14ee322011-09-22 04:58:26 +00002042 }
Greg Clayton1b654882010-09-19 02:33:57 +00002043 }
2044 break;
2045
2046 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002047 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00002048 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002049 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002050 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002051 Thread *thread = exe_ctx->GetThreadPtr();
2052 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00002053 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002054 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002055 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002056 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002057 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002058 var_success = true;
2059 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002060 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00002061 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002062 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00002063 var_success = true;
2064 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002065 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00002066 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002067 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002068 var_success = true;
2069 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002070 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002071 {
2072 cstr = thread->GetName();
2073 var_success = cstr && cstr[0];
2074 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002075 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00002076 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002077 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002078 {
2079 cstr = thread->GetQueueName();
2080 var_success = cstr && cstr[0];
2081 if (var_success)
2082 s.PutCString(cstr);
2083 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002084 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002085 {
2086 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002087 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00002088 {
2089 cstr = stop_info_sp->GetDescription();
2090 if (cstr && cstr[0])
2091 {
2092 s.PutCString(cstr);
2093 var_success = true;
2094 }
Greg Clayton1b654882010-09-19 02:33:57 +00002095 }
2096 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002097 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00002098 {
2099 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002100 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00002101 {
2102 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2103 if (return_valobj_sp)
2104 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00002105 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00002106 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00002107 }
2108 }
2109 }
Jim Ingham30fadaf2014-07-08 01:07:32 +00002110 else if (IsToken (var_name_begin, "completed-expression}"))
2111 {
2112 StopInfoSP stop_info_sp = thread->GetStopInfo ();
2113 if (stop_info_sp && stop_info_sp->IsValid())
2114 {
2115 ClangExpressionVariableSP expression_var_sp = StopInfo::GetExpressionVariable (stop_info_sp);
2116 if (expression_var_sp && expression_var_sp->GetValueObject())
2117 {
2118 expression_var_sp->GetValueObject()->Dump(s);
2119 var_success = true;
2120 }
2121 }
2122 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002123 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002124 {
2125 var_name_begin += ::strlen("script:");
2126 std::string script_name(var_name_begin,var_name_end);
2127 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002128 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
2129 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002130 }
Jason Molenda705b1802014-06-13 02:37:02 +00002131 else if (IsToken (var_name_begin, "info."))
2132 {
2133 var_name_begin += ::strlen("info.");
2134 StructuredData::ObjectSP object_sp = thread->GetExtendedInfo();
2135 if (object_sp && object_sp->GetType() == StructuredData::Type::eTypeDictionary)
2136 {
2137 var_success = FormatThreadExtendedInfoRecurse (var_name_begin, object_sp, sc, exe_ctx, s);
2138 }
2139 }
Greg Clayton1b654882010-09-19 02:33:57 +00002140 }
2141 }
2142 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002143 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00002144 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002145 // TODO: hookup properties
2146// if (!target_properties_sp)
2147// {
2148// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2149// if (target)
2150// target_properties_sp = target->GetProperties();
2151// }
2152//
2153// if (target_properties_sp)
2154// {
2155// var_name_begin += ::strlen ("target.");
2156// const char *end_property = strchr(var_name_begin, '}');
2157// if (end_property)
2158// {
2159// ConstString property_name(var_name_begin, end_property - var_name_begin);
2160// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2161// if (!property_value.empty())
2162// {
2163// s.PutCString (property_value.c_str());
2164// var_success = true;
2165// }
2166// }
2167// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002168 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2169 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002170 {
Greg Clayton1b654882010-09-19 02:33:57 +00002171 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002172 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002173 {
2174 ArchSpec arch (target->GetArchitecture ());
2175 if (arch.IsValid())
2176 {
Greg Clayton64195a22011-02-23 00:35:02 +00002177 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002178 var_success = true;
2179 }
2180 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002181 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002182 {
2183 var_name_begin += ::strlen("script:");
2184 std::string script_name(var_name_begin,var_name_end);
2185 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002186 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2187 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002188 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002189 }
Greg Clayton1b654882010-09-19 02:33:57 +00002190 }
2191 break;
Jason Molendaaff1b352014-10-10 23:07:36 +00002192
Greg Clayton1b654882010-09-19 02:33:57 +00002193 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002194 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002195 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002196 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002197 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002198 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002199 var_name_begin += ::strlen ("module.");
2200
Michael Sartain0769b2b2013-07-30 16:44:36 +00002201 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002202 {
2203 if (module->GetFileSpec())
2204 {
2205 var_name_begin += ::strlen ("file.");
2206
Michael Sartain0769b2b2013-07-30 16:44:36 +00002207 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002208 {
2209 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002210 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002211 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002212 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002213 {
2214 format_file_spec = module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002215 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002216 }
2217 }
2218 }
2219 }
2220 }
2221 break;
2222
2223
2224 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002225 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002226 {
2227 if (sc && sc->comp_unit != NULL)
2228 {
2229 var_name_begin += ::strlen ("file.");
2230
Michael Sartain0769b2b2013-07-30 16:44:36 +00002231 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002232 {
2233 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002234 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002235 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002236 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002237 {
2238 format_file_spec = *sc->comp_unit;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002239 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002240 }
2241 }
2242 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002243 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002244 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002245 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002246 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002247 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002248 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002249 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002250 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002251 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002252 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002253 s.Printf("%u", frame->GetFrameIndex());
2254 var_success = true;
2255 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002256 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002257 {
2258 reg_kind = eRegisterKindGeneric;
2259 reg_num = LLDB_REGNUM_GENERIC_PC;
2260 var_success = true;
2261 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002262 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002263 {
2264 reg_kind = eRegisterKindGeneric;
2265 reg_num = LLDB_REGNUM_GENERIC_SP;
2266 var_success = true;
2267 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002268 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002269 {
2270 reg_kind = eRegisterKindGeneric;
2271 reg_num = LLDB_REGNUM_GENERIC_FP;
2272 var_success = true;
2273 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002274 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002275 {
2276 reg_kind = eRegisterKindGeneric;
2277 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2278 var_success = true;
2279 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002280 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002281 {
2282 reg_ctx = frame->GetRegisterContext().get();
2283 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002284 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002285 var_name_begin += ::strlen ("reg.");
2286 if (var_name_begin < var_name_end)
2287 {
2288 std::string reg_name (var_name_begin, var_name_end);
2289 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2290 if (reg_info)
2291 var_success = true;
2292 }
Greg Clayton1b654882010-09-19 02:33:57 +00002293 }
2294 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002295 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002296 {
2297 var_name_begin += ::strlen("script:");
2298 std::string script_name(var_name_begin,var_name_end);
2299 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002300 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2301 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002302 }
Greg Clayton1b654882010-09-19 02:33:57 +00002303 }
2304 }
2305 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002306 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002307 {
2308 if (sc && (sc->function != NULL || sc->symbol != NULL))
2309 {
2310 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002311 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002312 {
2313 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002314 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002315 else
2316 s.Printf("symbol[%u]", sc->symbol->GetID());
2317
2318 var_success = true;
2319 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002320 if (IsToken (var_name_begin, "changed}") && function_changed)
2321 {
2322 var_success = true;
2323 }
2324 if (IsToken (var_name_begin, "initial-function}") && initial_function)
2325 {
2326 var_success = true;
2327 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002328 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002329 {
2330 if (sc->function)
2331 cstr = sc->function->GetName().AsCString (NULL);
2332 else if (sc->symbol)
2333 cstr = sc->symbol->GetName().AsCString (NULL);
2334 if (cstr)
2335 {
2336 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002337
2338 if (sc->block)
2339 {
2340 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2341 if (inline_block)
2342 {
2343 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2344 if (inline_info)
2345 {
2346 s.PutCString(" [inlined] ");
2347 inline_info->GetName().Dump(&s);
2348 }
2349 }
2350 }
Greg Clayton1b654882010-09-19 02:33:57 +00002351 var_success = true;
2352 }
2353 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002354 else if (IsToken (var_name_begin, "name-without-args}"))
2355 {
2356 ConstString name;
2357 if (sc->function)
2358 name = sc->function->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments);
2359 else if (sc->symbol)
2360 name = sc->symbol->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments);
2361 if (name)
2362 {
2363 s.PutCString(name.GetCString());
2364 var_success = true;
2365 }
2366 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002367 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002368 {
2369 // Print the function name with arguments in it
2370
2371 if (sc->function)
2372 {
2373 var_success = true;
2374 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2375 cstr = sc->function->GetName().AsCString (NULL);
2376 if (cstr)
2377 {
2378 const InlineFunctionInfo *inline_info = NULL;
2379 VariableListSP variable_list_sp;
2380 bool get_function_vars = true;
2381 if (sc->block)
2382 {
2383 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2384
2385 if (inline_block)
2386 {
2387 get_function_vars = false;
2388 inline_info = sc->block->GetInlinedFunctionInfo();
2389 if (inline_info)
2390 variable_list_sp = inline_block->GetBlockVariableList (true);
2391 }
2392 }
2393
2394 if (get_function_vars)
2395 {
2396 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2397 }
2398
2399 if (inline_info)
2400 {
2401 s.PutCString (cstr);
2402 s.PutCString (" [inlined] ");
2403 cstr = inline_info->GetName().GetCString();
2404 }
2405
2406 VariableList args;
2407 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002408 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002409 if (args.GetSize() > 0)
2410 {
2411 const char *open_paren = strchr (cstr, '(');
Enrico Granatae4a4f5d2014-08-16 00:56:04 +00002412 const char *close_paren = nullptr;
2413 const char *generic = strchr(cstr, '<');
2414 // if before the arguments list begins there is a template sign
2415 // then scan to the end of the generic args before you try to find
2416 // the arguments list
2417 if (generic && open_paren && generic < open_paren)
2418 {
2419 int generic_depth = 1;
2420 ++generic;
2421 for (;
2422 *generic && generic_depth > 0;
2423 generic++)
2424 {
2425 if (*generic == '<')
2426 generic_depth++;
2427 if (*generic == '>')
2428 generic_depth--;
2429 }
2430 if (*generic)
2431 open_paren = strchr(generic, '(');
2432 else
2433 open_paren = nullptr;
2434 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002435 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002436 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002437 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002438 {
2439 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2440 if (open_paren)
2441 close_paren = strchr (open_paren, ')');
2442 }
2443 else
2444 close_paren = strchr (open_paren, ')');
2445 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002446
2447 if (open_paren)
2448 s.Write(cstr, open_paren - cstr + 1);
2449 else
2450 {
2451 s.PutCString (cstr);
2452 s.PutChar ('(');
2453 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002454 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002455 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2456 {
Enrico Granata894f7352014-03-25 22:03:52 +00002457 std::string buffer;
2458
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002459 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2460 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
Enrico Granata894f7352014-03-25 22:03:52 +00002461 const char *var_representation = nullptr;
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002462 const char *var_name = var_value_sp->GetName().GetCString();
Enrico Granata894f7352014-03-25 22:03:52 +00002463 if (var_value_sp->GetClangType().IsAggregateType() &&
2464 DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
2465 {
2466 static StringSummaryFormat format(TypeSummaryImpl::Flags()
2467 .SetHideItemNames(false)
2468 .SetShowMembersOneLiner(true),
2469 "");
2470 format.FormatObject(var_value_sp.get(), buffer);
2471 var_representation = buffer.c_str();
2472 }
2473 else
2474 var_representation = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002475 if (arg_idx > 0)
2476 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002477 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002478 {
Enrico Granata894f7352014-03-25 22:03:52 +00002479 if (var_representation)
2480 s.Printf ("%s=%s", var_name, var_representation);
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002481 else
2482 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2483 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002484 else
2485 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002486 }
2487
2488 if (close_paren)
2489 s.PutCString (close_paren);
2490 else
2491 s.PutChar(')');
2492
2493 }
2494 else
2495 {
2496 s.PutCString(cstr);
2497 }
2498 }
2499 }
2500 else if (sc->symbol)
2501 {
2502 cstr = sc->symbol->GetName().AsCString (NULL);
2503 if (cstr)
2504 {
2505 s.PutCString(cstr);
2506 var_success = true;
2507 }
2508 }
2509 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002510 else if (IsToken (var_name_begin, "addr-offset}")
2511 || IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002512 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002513 if (IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}"))
2514 {
2515 addr_offset_print_with_no_padding = true;
2516 addr_offset_concrete_func_only = true;
2517 }
Greg Clayton1b654882010-09-19 02:33:57 +00002518 var_success = addr != NULL;
2519 if (var_success)
2520 {
2521 format_addr = *addr;
2522 calculate_format_addr_function_offset = true;
2523 }
2524 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002525 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002526 {
2527 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2528 if (var_success)
2529 {
2530 format_addr = sc->line_entry.range.GetBaseAddress();
2531 calculate_format_addr_function_offset = true;
2532 }
2533 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002534 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002535 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002536 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002537 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002538 if (var_success)
2539 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002540 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002541 calculate_format_addr_function_offset = true;
2542 }
2543 }
2544 }
2545 }
2546 break;
2547
2548 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002549 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002550 {
2551 if (sc && sc->line_entry.IsValid())
2552 {
2553 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002554 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002555 {
2556 var_name_begin += ::strlen ("file.");
2557
Michael Sartain0769b2b2013-07-30 16:44:36 +00002558 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002559 {
2560 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002561 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002562 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002563 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002564 {
2565 format_file_spec = sc->line_entry.file;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002566 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002567 }
2568 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002569 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002570 {
2571 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002572 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002573 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002574 else if ((IsToken (var_name_begin, "start-addr}")) ||
2575 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002576 {
2577 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2578 if (var_success)
2579 {
2580 format_addr = sc->line_entry.range.GetBaseAddress();
2581 if (var_name_begin[0] == 'e')
2582 format_addr.Slide (sc->line_entry.range.GetByteSize());
2583 }
2584 }
2585 }
2586 }
2587 break;
Jason Molendaaff1b352014-10-10 23:07:36 +00002588 case 'c':
2589 if (IsToken (var_name_begin, "current-pc-arrow"))
2590 {
2591 if (addr && exe_ctx && exe_ctx->GetFramePtr())
2592 {
2593 RegisterContextSP reg_ctx = exe_ctx->GetFramePtr()->GetRegisterContextSP();
2594 if (reg_ctx.get())
2595 {
2596 addr_t pc_loadaddr = reg_ctx->GetPC();
2597 if (pc_loadaddr != LLDB_INVALID_ADDRESS)
2598 {
2599 Address pc;
2600 pc.SetLoadAddress (pc_loadaddr, exe_ctx->GetTargetPtr());
2601 if (pc == *addr)
2602 {
2603 s.Printf ("->");
2604 var_success = true;
2605 }
2606 else
2607 {
2608 s.Printf(" ");
2609 var_success = true;
2610 }
2611 }
2612 }
2613 }
2614 }
2615 break;
Greg Clayton1b654882010-09-19 02:33:57 +00002616 }
2617
2618 if (var_success)
2619 {
2620 // If format addr is valid, then we need to print an address
2621 if (reg_num != LLDB_INVALID_REGNUM)
2622 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002623 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002624 // We have a register value to display...
2625 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2626 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002627 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002628 }
2629 else
2630 {
2631 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002632 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002633
2634 if (reg_ctx)
2635 {
2636 if (reg_kind != kNumRegisterKinds)
2637 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2638 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2639 var_success = reg_info != NULL;
2640 }
2641 }
2642 }
2643
2644 if (reg_info != NULL)
2645 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002646 RegisterValue reg_value;
2647 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2648 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002649 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002650 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002651 }
2652 }
2653
2654 if (format_file_spec)
2655 {
2656 s << format_file_spec;
2657 }
2658
2659 // If format addr is valid, then we need to print an address
2660 if (format_addr.IsValid())
2661 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002662 var_success = false;
2663
Greg Clayton1b654882010-09-19 02:33:57 +00002664 if (calculate_format_addr_function_offset)
2665 {
2666 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002667
Greg Clayton0603aa92010-10-04 01:05:56 +00002668 if (sc)
2669 {
2670 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002671 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002672 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Jason Molendaaff1b352014-10-10 23:07:36 +00002673 if (sc->block && addr_offset_concrete_func_only == false)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002674 {
2675 // Check to make sure we aren't in an inline
2676 // function. If we are, use the inline block
2677 // range that contains "format_addr" since
2678 // blocks can be discontiguous.
2679 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2680 AddressRange inline_range;
2681 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2682 func_addr = inline_range.GetBaseAddress();
2683 }
2684 }
Greg Claytone7612132012-03-07 21:03:09 +00002685 else if (sc->symbol && sc->symbol->ValueIsAddress())
2686 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002687 }
2688
2689 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002690 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002691 const char *addr_offset_padding = " ";
2692 if (addr_offset_print_with_no_padding)
2693 {
2694 addr_offset_padding = "";
2695 }
Greg Clayton1b654882010-09-19 02:33:57 +00002696 if (func_addr.GetSection() == format_addr.GetSection())
2697 {
2698 addr_t func_file_addr = func_addr.GetFileAddress();
2699 addr_t addr_file_addr = format_addr.GetFileAddress();
2700 if (addr_file_addr > func_file_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002701 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002702 else if (addr_file_addr < func_file_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002703 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002704 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002705 }
2706 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002707 {
2708 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2709 if (target)
2710 {
2711 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2712 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2713 if (addr_load_addr > func_load_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002714 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002715 else if (addr_load_addr < func_load_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002716 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002717 var_success = true;
2718 }
2719 }
Greg Clayton1b654882010-09-19 02:33:57 +00002720 }
2721 }
2722 else
2723 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002724 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002725 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002726 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2727 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002728 if (vaddr == LLDB_INVALID_ADDRESS)
2729 vaddr = format_addr.GetFileAddress ();
2730
2731 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002732 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002733 int addr_width = 0;
2734 if (exe_ctx && target)
2735 {
Eric Christopherfd1a9362014-10-11 00:04:42 +00002736 addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Jason Molendaaff1b352014-10-10 23:07:36 +00002737 }
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002738 if (addr_width == 0)
2739 addr_width = 16;
Jason Molendaaff1b352014-10-10 23:07:36 +00002740 if (print_file_addr_or_load_addr)
2741 {
2742 format_addr.Dump (&s, exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress, 0);
2743 }
2744 else
2745 {
2746 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
2747 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002748 var_success = true;
2749 }
Greg Clayton1b654882010-09-19 02:33:57 +00002750 }
2751 }
2752 }
2753
2754 if (var_success == false)
2755 success = false;
2756 }
2757 p = var_name_end;
2758 }
2759 else
2760 break;
2761 }
2762 else
2763 {
2764 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2765 s.PutChar(*p);
2766 }
2767 }
2768 else if (*p == '\\')
2769 {
2770 ++p; // skip the slash
2771 switch (*p)
2772 {
2773 case 'a': s.PutChar ('\a'); break;
2774 case 'b': s.PutChar ('\b'); break;
2775 case 'f': s.PutChar ('\f'); break;
2776 case 'n': s.PutChar ('\n'); break;
2777 case 'r': s.PutChar ('\r'); break;
2778 case 't': s.PutChar ('\t'); break;
2779 case 'v': s.PutChar ('\v'); break;
2780 case '\'': s.PutChar ('\''); break;
2781 case '\\': s.PutChar ('\\'); break;
2782 case '0':
2783 // 1 to 3 octal chars
2784 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002785 // Make a string that can hold onto the initial zero char,
2786 // up to 3 octal digits, and a terminating NULL.
2787 char oct_str[5] = { 0, 0, 0, 0, 0 };
2788
2789 int i;
2790 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2791 oct_str[i] = p[i];
2792
2793 // We don't want to consume the last octal character since
2794 // the main for loop will do this for us, so we advance p by
2795 // one less than i (even if i is zero)
2796 p += i - 1;
2797 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2798 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002799 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002800 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002801 }
Greg Clayton1b654882010-09-19 02:33:57 +00002802 }
2803 break;
2804
2805 case 'x':
2806 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002807 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002808 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002809 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002810
Greg Clayton0603aa92010-10-04 01:05:56 +00002811 // Make a string that can hold onto two hex chars plus a
2812 // NULL terminator
2813 char hex_str[3] = { 0,0,0 };
2814 hex_str[0] = *p;
2815 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002816 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002817 ++p; // Skip the first of the two hex chars
2818 hex_str[1] = *p;
2819 }
2820
2821 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2822 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002823 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002824 }
2825 else
2826 {
2827 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002828 }
2829 break;
2830
2831 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002832 // Just desensitize any other character by just printing what
2833 // came after the '\'
2834 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002835 break;
2836
2837 }
2838
2839 }
2840 }
2841 if (end)
2842 *end = p;
2843 return success;
2844}
2845
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002846bool
2847Debugger::FormatPrompt
2848(
2849 const char *format,
2850 const SymbolContext *sc,
2851 const ExecutionContext *exe_ctx,
2852 const Address *addr,
2853 Stream &s,
2854 ValueObject* valobj
2855)
2856{
2857 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2858 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2859 if (format_str.length())
2860 format = format_str.c_str();
Jason Molendaaff1b352014-10-10 23:07:36 +00002861 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj, false, false);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002862}
2863
Jason Molendaaff1b352014-10-10 23:07:36 +00002864bool
2865Debugger::FormatDisassemblerAddress (const char *format,
2866 const SymbolContext *sc,
2867 const SymbolContext *prev_sc,
2868 const ExecutionContext *exe_ctx,
2869 const Address *addr,
2870 Stream &s)
2871{
2872 if (format == NULL && exe_ctx != NULL && exe_ctx->HasTargetScope())
2873 {
2874 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
2875 }
2876 bool function_changed = false;
2877 bool initial_function = false;
2878 if (prev_sc && (prev_sc->function || prev_sc->symbol))
2879 {
2880 if (sc && (sc->function || sc->symbol))
2881 {
2882 if (prev_sc->symbol && sc->symbol)
2883 {
2884 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
2885 {
2886 function_changed = true;
2887 }
2888 }
2889 else if (prev_sc->function && sc->function)
2890 {
2891 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
2892 {
2893 function_changed = true;
2894 }
2895 }
2896 }
2897 }
2898 // The first context on a list of instructions will have a prev_sc that
2899 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
2900 // would return false. But we do get a prev_sc pointer.
2901 if ((sc && (sc->function || sc->symbol))
2902 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL))
2903 {
2904 initial_function = true;
2905 }
2906 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, NULL, function_changed, initial_function);
2907}
2908
2909
Jim Ingham228063c2012-02-21 02:23:08 +00002910void
2911Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2912{
Jim Ingham4f02b222012-02-22 22:49:20 +00002913 // For simplicity's sake, I am not going to deal with how to close down any
2914 // open logging streams, I just redirect everything from here on out to the
2915 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002916 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2917}
2918
2919bool
2920Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2921{
2922 Log::Callbacks log_callbacks;
2923
2924 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002925 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002926 {
2927 log_stream_sp = m_log_callback_stream_sp;
2928 // For now when using the callback mode you always get thread & timestamp.
2929 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2930 }
2931 else if (log_file == NULL || *log_file == '\0')
2932 {
Greg Clayton44d93782014-01-27 23:43:24 +00002933 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00002934 }
2935 else
2936 {
2937 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002938 if (pos != m_log_streams.end())
2939 log_stream_sp = pos->second.lock();
2940 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002941 {
2942 log_stream_sp.reset (new StreamFile (log_file));
2943 m_log_streams[log_file] = log_stream_sp;
2944 }
Jim Ingham228063c2012-02-21 02:23:08 +00002945 }
2946 assert (log_stream_sp.get());
2947
2948 if (log_options == 0)
2949 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2950
Greg Clayton57abc5d2013-05-10 21:47:16 +00002951 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002952 {
2953 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2954 return true;
2955 }
2956 else
2957 {
2958 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2959 if (log_channel_sp)
2960 {
2961 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2962 {
2963 return true;
2964 }
2965 else
2966 {
2967 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2968 return false;
2969 }
2970 }
2971 else
2972 {
2973 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2974 return false;
2975 }
2976 }
2977 return false;
2978}
2979
Greg Clayton9585fbf2013-03-19 00:20:55 +00002980SourceManager &
2981Debugger::GetSourceManager ()
2982{
2983 if (m_source_manager_ap.get() == NULL)
2984 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2985 return *m_source_manager_ap;
2986}
2987
2988
Greg Clayton44d93782014-01-27 23:43:24 +00002989
2990// This function handles events that were broadcast by the process.
2991void
2992Debugger::HandleBreakpointEvent (const EventSP &event_sp)
2993{
2994 using namespace lldb;
2995 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
2996
2997// if (event_type & eBreakpointEventTypeAdded
2998// || event_type & eBreakpointEventTypeRemoved
2999// || event_type & eBreakpointEventTypeEnabled
3000// || event_type & eBreakpointEventTypeDisabled
3001// || event_type & eBreakpointEventTypeCommandChanged
3002// || event_type & eBreakpointEventTypeConditionChanged
3003// || event_type & eBreakpointEventTypeIgnoreChanged
3004// || event_type & eBreakpointEventTypeLocationsResolved)
3005// {
3006// // Don't do anything about these events, since the breakpoint commands already echo these actions.
3007// }
3008//
3009 if (event_type & eBreakpointEventTypeLocationsAdded)
3010 {
3011 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
3012 if (num_new_locations > 0)
3013 {
3014 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
3015 StreamFileSP output_sp (GetOutputFile());
3016 if (output_sp)
3017 {
3018 output_sp->Printf("%d location%s added to breakpoint %d\n",
3019 num_new_locations,
3020 num_new_locations == 1 ? "" : "s",
3021 breakpoint->GetID());
3022 RefreshTopIOHandler();
3023 }
3024 }
3025 }
3026// else if (event_type & eBreakpointEventTypeLocationsRemoved)
3027// {
3028// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
3029// }
3030// else if (event_type & eBreakpointEventTypeLocationsResolved)
3031// {
3032// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
3033// }
3034}
3035
3036size_t
3037Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
3038{
3039 size_t total_bytes = 0;
3040 if (stream == NULL)
3041 stream = GetOutputFile().get();
3042
3043 if (stream)
3044 {
3045 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
3046 if (process == NULL)
3047 {
3048 TargetSP target_sp = GetTargetList().GetSelectedTarget();
3049 if (target_sp)
3050 process = target_sp->GetProcessSP().get();
3051 }
3052 if (process)
3053 {
3054 Error error;
3055 size_t len;
3056 char stdio_buffer[1024];
3057 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3058 {
3059 stream->Write(stdio_buffer, len);
3060 total_bytes += len;
3061 }
3062 }
3063 stream->Flush();
3064 }
3065 return total_bytes;
3066}
3067
3068size_t
3069Debugger::GetProcessSTDERR (Process *process, Stream *stream)
3070{
3071 size_t total_bytes = 0;
3072 if (stream == NULL)
3073 stream = GetOutputFile().get();
3074
3075 if (stream)
3076 {
3077 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
3078 if (process == NULL)
3079 {
3080 TargetSP target_sp = GetTargetList().GetSelectedTarget();
3081 if (target_sp)
3082 process = target_sp->GetProcessSP().get();
3083 }
3084 if (process)
3085 {
3086 Error error;
3087 size_t len;
3088 char stdio_buffer[1024];
3089 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3090 {
3091 stream->Write(stdio_buffer, len);
3092 total_bytes += len;
3093 }
3094 }
3095 stream->Flush();
3096 }
3097 return total_bytes;
3098}
3099
3100// This function handles events that were broadcast by the process.
3101void
3102Debugger::HandleProcessEvent (const EventSP &event_sp)
3103{
3104 using namespace lldb;
3105 const uint32_t event_type = event_sp->GetType();
3106 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
3107
Greg Claytonb4874f12014-02-28 18:22:24 +00003108 StreamString output_stream;
3109 StreamString error_stream;
Greg Clayton44d93782014-01-27 23:43:24 +00003110 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00003111
Greg Claytonb4874f12014-02-28 18:22:24 +00003112 if (!gui_enabled)
3113 {
3114 bool pop_process_io_handler = false;
3115 assert (process_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00003116
Greg Claytonb4874f12014-02-28 18:22:24 +00003117 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
Greg Clayton44d93782014-01-27 23:43:24 +00003118 {
Greg Claytonb4874f12014-02-28 18:22:24 +00003119 GetProcessSTDOUT (process_sp.get(), &output_stream);
3120 }
3121
3122 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
3123 {
3124 GetProcessSTDERR (process_sp.get(), &error_stream);
3125 }
3126
3127 if (event_type & Process::eBroadcastBitStateChanged)
3128 {
3129
3130 // Drain all stout and stderr so we don't see any output come after
3131 // we print our prompts
Greg Clayton44d93782014-01-27 23:43:24 +00003132 // Something changed in the process; get the event and report the process's current status and location to
3133 // the user.
3134 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
3135 if (event_state == eStateInvalid)
3136 return;
3137
3138 switch (event_state)
3139 {
3140 case eStateInvalid:
3141 case eStateUnloaded:
3142 case eStateConnected:
3143 case eStateAttaching:
3144 case eStateLaunching:
3145 case eStateStepping:
3146 case eStateDetached:
3147 {
Greg Claytonb4874f12014-02-28 18:22:24 +00003148 output_stream.Printf("Process %" PRIu64 " %s\n",
3149 process_sp->GetID(),
3150 StateAsCString (event_state));
3151
3152 if (event_state == eStateDetached)
3153 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00003154 }
3155 break;
3156
3157 case eStateRunning:
3158 // Don't be chatty when we run...
3159 break;
3160
3161 case eStateExited:
Greg Claytonb4874f12014-02-28 18:22:24 +00003162 process_sp->GetStatus(output_stream);
3163 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00003164 break;
3165
3166 case eStateStopped:
3167 case eStateCrashed:
3168 case eStateSuspended:
3169 // Make sure the program hasn't been auto-restarted:
3170 if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get()))
3171 {
3172 size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get());
3173 if (num_reasons > 0)
3174 {
3175 // FIXME: Do we want to report this, or would that just be annoyingly chatty?
3176 if (num_reasons == 1)
3177 {
3178 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0);
Greg Claytonb4874f12014-02-28 18:22:24 +00003179 output_stream.Printf("Process %" PRIu64 " stopped and restarted: %s\n",
3180 process_sp->GetID(),
3181 reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00003182 }
3183 else
3184 {
Greg Claytonb4874f12014-02-28 18:22:24 +00003185 output_stream.Printf("Process %" PRIu64 " stopped and restarted, reasons:\n",
3186 process_sp->GetID());
Greg Clayton44d93782014-01-27 23:43:24 +00003187
3188
3189 for (size_t i = 0; i < num_reasons; i++)
3190 {
3191 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i);
Greg Claytonb4874f12014-02-28 18:22:24 +00003192 output_stream.Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00003193 }
3194 }
3195 }
3196 }
3197 else
3198 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003199 // Lock the thread list so it doesn't change on us, this is the scope for the locker:
Greg Clayton44d93782014-01-27 23:43:24 +00003200 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003201 ThreadList &thread_list = process_sp->GetThreadList();
3202 Mutex::Locker locker (thread_list.GetMutex());
3203
3204 ThreadSP curr_thread (thread_list.GetSelectedThread());
3205 ThreadSP thread;
3206 StopReason curr_thread_stop_reason = eStopReasonInvalid;
3207 if (curr_thread)
3208 curr_thread_stop_reason = curr_thread->GetStopReason();
3209 if (!curr_thread ||
3210 !curr_thread->IsValid() ||
3211 curr_thread_stop_reason == eStopReasonInvalid ||
3212 curr_thread_stop_reason == eStopReasonNone)
Greg Clayton44d93782014-01-27 23:43:24 +00003213 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003214 // Prefer a thread that has just completed its plan over another thread as current thread.
3215 ThreadSP plan_thread;
3216 ThreadSP other_thread;
3217 const size_t num_threads = thread_list.GetSize();
3218 size_t i;
3219 for (i = 0; i < num_threads; ++i)
Greg Clayton44d93782014-01-27 23:43:24 +00003220 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003221 thread = thread_list.GetThreadAtIndex(i);
3222 StopReason thread_stop_reason = thread->GetStopReason();
3223 switch (thread_stop_reason)
3224 {
3225 case eStopReasonInvalid:
3226 case eStopReasonNone:
3227 break;
3228
3229 case eStopReasonTrace:
3230 case eStopReasonBreakpoint:
3231 case eStopReasonWatchpoint:
3232 case eStopReasonSignal:
3233 case eStopReasonException:
3234 case eStopReasonExec:
3235 case eStopReasonThreadExiting:
Kuba Breckaafdf8422014-10-10 23:43:03 +00003236 case eStopReasonInstrumentation:
Jim Ingham4a65fb12014-03-07 11:20:03 +00003237 if (!other_thread)
3238 other_thread = thread;
3239 break;
3240 case eStopReasonPlanComplete:
3241 if (!plan_thread)
3242 plan_thread = thread;
3243 break;
3244 }
3245 }
3246 if (plan_thread)
3247 thread_list.SetSelectedThreadByID (plan_thread->GetID());
3248 else if (other_thread)
3249 thread_list.SetSelectedThreadByID (other_thread->GetID());
3250 else
3251 {
3252 if (curr_thread && curr_thread->IsValid())
3253 thread = curr_thread;
3254 else
3255 thread = thread_list.GetThreadAtIndex(0);
3256
3257 if (thread)
3258 thread_list.SetSelectedThreadByID (thread->GetID());
Greg Clayton44d93782014-01-27 23:43:24 +00003259 }
3260 }
Greg Clayton44d93782014-01-27 23:43:24 +00003261 }
Jim Ingham4a65fb12014-03-07 11:20:03 +00003262 // Drop the ThreadList mutex by here, since GetThreadStatus below might have to run code,
3263 // e.g. for Data formatters, and if we hold the ThreadList mutex, then the process is going to
3264 // have a hard time restarting the process.
Greg Clayton44d93782014-01-27 23:43:24 +00003265
3266 if (GetTargetList().GetSelectedTarget().get() == &process_sp->GetTarget())
3267 {
3268 const bool only_threads_with_stop_reason = true;
3269 const uint32_t start_frame = 0;
3270 const uint32_t num_frames = 1;
3271 const uint32_t num_frames_with_source = 1;
Greg Claytonb4874f12014-02-28 18:22:24 +00003272 process_sp->GetStatus(output_stream);
3273 process_sp->GetThreadStatus (output_stream,
Greg Clayton44d93782014-01-27 23:43:24 +00003274 only_threads_with_stop_reason,
3275 start_frame,
3276 num_frames,
3277 num_frames_with_source);
3278 }
3279 else
3280 {
3281 uint32_t target_idx = GetTargetList().GetIndexOfTarget(process_sp->GetTarget().shared_from_this());
3282 if (target_idx != UINT32_MAX)
Greg Claytonb4874f12014-02-28 18:22:24 +00003283 output_stream.Printf ("Target %d: (", target_idx);
Greg Clayton44d93782014-01-27 23:43:24 +00003284 else
Greg Claytonb4874f12014-02-28 18:22:24 +00003285 output_stream.Printf ("Target <unknown index>: (");
3286 process_sp->GetTarget().Dump (&output_stream, eDescriptionLevelBrief);
3287 output_stream.Printf (") stopped.\n");
Greg Clayton44d93782014-01-27 23:43:24 +00003288 }
Greg Claytonb4874f12014-02-28 18:22:24 +00003289
3290 // Pop the process IO handler
3291 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00003292 }
3293 break;
3294 }
3295 }
Greg Clayton44d93782014-01-27 23:43:24 +00003296
Greg Claytonb4874f12014-02-28 18:22:24 +00003297 if (output_stream.GetSize() || error_stream.GetSize())
3298 {
3299 StreamFileSP error_stream_sp (GetOutputFile());
Greg Clayton6fea17e2014-03-03 19:15:20 +00003300 bool top_io_handler_hid = false;
3301
3302 if (process_sp->ProcessIOHandlerIsActive() == false)
3303 top_io_handler_hid = HideTopIOHandler();
Greg Claytonb4874f12014-02-28 18:22:24 +00003304
3305 if (output_stream.GetSize())
3306 {
3307 StreamFileSP output_stream_sp (GetOutputFile());
3308 if (output_stream_sp)
3309 output_stream_sp->Write (output_stream.GetData(), output_stream.GetSize());
3310 }
3311
3312 if (error_stream.GetSize())
3313 {
3314 StreamFileSP error_stream_sp (GetErrorFile());
3315 if (error_stream_sp)
3316 error_stream_sp->Write (error_stream.GetData(), error_stream.GetSize());
3317 }
3318
3319 if (top_io_handler_hid)
3320 RefreshTopIOHandler();
3321 }
3322
3323 if (pop_process_io_handler)
3324 process_sp->PopProcessIOHandler();
3325 }
Greg Clayton44d93782014-01-27 23:43:24 +00003326}
3327
3328void
3329Debugger::HandleThreadEvent (const EventSP &event_sp)
3330{
3331 // At present the only thread event we handle is the Frame Changed event,
3332 // and all we do for that is just reprint the thread status for that thread.
3333 using namespace lldb;
3334 const uint32_t event_type = event_sp->GetType();
3335 if (event_type == Thread::eBroadcastBitStackChanged ||
3336 event_type == Thread::eBroadcastBitThreadSelected )
3337 {
3338 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
3339 if (thread_sp)
3340 {
3341 HideTopIOHandler();
3342 StreamFileSP stream_sp (GetOutputFile());
3343 thread_sp->GetStatus(*stream_sp, 0, 1, 1);
3344 RefreshTopIOHandler();
3345 }
3346 }
3347}
3348
3349bool
3350Debugger::IsForwardingEvents ()
3351{
3352 return (bool)m_forward_listener_sp;
3353}
3354
3355void
3356Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
3357{
3358 m_forward_listener_sp = listener_sp;
3359}
3360
3361void
3362Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
3363{
3364 m_forward_listener_sp.reset();
3365}
3366
3367
3368void
3369Debugger::DefaultEventHandler()
3370{
3371 Listener& listener(GetListener());
3372 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
3373 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
3374 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
3375 BroadcastEventSpec target_event_spec (broadcaster_class_target,
3376 Target::eBroadcastBitBreakpointChanged);
3377
3378 BroadcastEventSpec process_event_spec (broadcaster_class_process,
3379 Process::eBroadcastBitStateChanged |
3380 Process::eBroadcastBitSTDOUT |
3381 Process::eBroadcastBitSTDERR);
3382
3383 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
3384 Thread::eBroadcastBitStackChanged |
3385 Thread::eBroadcastBitThreadSelected );
3386
3387 listener.StartListeningForEventSpec (*this, target_event_spec);
3388 listener.StartListeningForEventSpec (*this, process_event_spec);
3389 listener.StartListeningForEventSpec (*this, thread_event_spec);
3390 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
3391 CommandInterpreter::eBroadcastBitQuitCommandReceived |
3392 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
3393 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
3394
3395 bool done = false;
3396 while (!done)
3397 {
3398// Mutex::Locker locker;
3399// if (locker.TryLock(m_input_reader_stack.GetMutex()))
3400// {
3401// if (m_input_reader_stack.IsEmpty())
3402// break;
3403// }
3404//
3405 EventSP event_sp;
3406 if (listener.WaitForEvent(NULL, event_sp))
3407 {
3408 if (event_sp)
3409 {
3410 Broadcaster *broadcaster = event_sp->GetBroadcaster();
3411 if (broadcaster)
3412 {
3413 uint32_t event_type = event_sp->GetType();
3414 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
3415 if (broadcaster_class == broadcaster_class_process)
3416 {
3417 HandleProcessEvent (event_sp);
3418 }
3419 else if (broadcaster_class == broadcaster_class_target)
3420 {
3421 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
3422 {
3423 HandleBreakpointEvent (event_sp);
3424 }
3425 }
3426 else if (broadcaster_class == broadcaster_class_thread)
3427 {
3428 HandleThreadEvent (event_sp);
3429 }
3430 else if (broadcaster == m_command_interpreter_ap.get())
3431 {
3432 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
3433 {
3434 done = true;
3435 }
3436 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
3437 {
3438 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3439 if (data && data[0])
3440 {
3441 StreamFileSP error_sp (GetErrorFile());
3442 if (error_sp)
3443 {
3444 HideTopIOHandler();
3445 error_sp->PutCString(data);
3446 error_sp->Flush();
3447 RefreshTopIOHandler();
3448 }
3449 }
3450 }
3451 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
3452 {
3453 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3454 if (data && data[0])
3455 {
3456 StreamFileSP output_sp (GetOutputFile());
3457 if (output_sp)
3458 {
3459 HideTopIOHandler();
3460 output_sp->PutCString(data);
3461 output_sp->Flush();
3462 RefreshTopIOHandler();
3463 }
3464 }
3465 }
3466 }
3467 }
3468
3469 if (m_forward_listener_sp)
3470 m_forward_listener_sp->AddEvent(event_sp);
3471 }
3472 }
3473 }
3474}
3475
3476lldb::thread_result_t
3477Debugger::EventHandlerThread (lldb::thread_arg_t arg)
3478{
3479 ((Debugger *)arg)->DefaultEventHandler();
3480 return NULL;
3481}
3482
3483bool
3484Debugger::StartEventHandlerThread()
3485{
Zachary Turneracee96a2014-09-23 18:32:09 +00003486 if (!m_event_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00003487 {
3488 m_event_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.event-handler",
3489 EventHandlerThread,
3490 this,
3491 NULL,
3492 8*1024*1024); // Use larger 8MB stack for this thread
3493 }
Zachary Turneracee96a2014-09-23 18:32:09 +00003494 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00003495}
3496
3497void
3498Debugger::StopEventHandlerThread()
3499{
Zachary Turneracee96a2014-09-23 18:32:09 +00003500 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00003501 {
3502 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00003503 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00003504 }
3505}
3506
3507
3508lldb::thread_result_t
3509Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3510{
3511 Debugger *debugger = (Debugger *)arg;
3512 debugger->ExecuteIOHanders();
3513 debugger->StopEventHandlerThread();
3514 return NULL;
3515}
3516
3517bool
3518Debugger::StartIOHandlerThread()
3519{
Zachary Turneracee96a2014-09-23 18:32:09 +00003520 if (!m_io_handler_thread.IsJoinable())
Greg Clayton807b6b32014-10-15 18:03:59 +00003521 m_io_handler_thread = ThreadLauncher::LaunchThread ("lldb.debugger.io-handler",
3522 IOHandlerThread,
3523 this,
3524 NULL,
3525 8*1024*1024); // Use larger 8MB stack for this thread
Zachary Turneracee96a2014-09-23 18:32:09 +00003526 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00003527}
3528
3529void
3530Debugger::StopIOHandlerThread()
3531{
Zachary Turneracee96a2014-09-23 18:32:09 +00003532 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00003533 {
3534 if (m_input_file_sp)
3535 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00003536 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00003537 }
3538}
3539
3540