blob: 5a910a9040ae1a09ea9cf32113d4a49c22fce6b7 [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{
917 Mutex::Locker locker (m_input_reader_stack.GetMutex());
918 PushIOHandler (reader_sp);
Greg Clayton577508d2014-06-20 00:23:57 +0000919
920 IOHandlerSP top_reader_sp = reader_sp;
921 while (top_reader_sp)
922 {
923 top_reader_sp->Activate();
924 top_reader_sp->Run();
925 top_reader_sp->Deactivate();
926
927 if (top_reader_sp.get() == reader_sp.get())
928 {
929 if (PopIOHandler (reader_sp))
930 break;
931 }
932
933 while (1)
934 {
935 top_reader_sp = m_input_reader_stack.Top();
936 if (top_reader_sp && top_reader_sp->GetIsDone())
937 m_input_reader_stack.Pop();
938 else
939 break;
940 }
941 }
Greg Clayton44d93782014-01-27 23:43:24 +0000942}
943
944void
945Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
946{
947 // Before an IOHandler runs, it must have in/out/err streams.
948 // This function is called when one ore more of the streams
949 // are NULL. We use the top input reader's in/out/err streams,
950 // or fall back to the debugger file handles, or we fall back
951 // onto stdin/stdout/stderr as a last resort.
952
953 Mutex::Locker locker (m_input_reader_stack.GetMutex());
954 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
955 // If no STDIN has been set, then set it appropriately
956 if (!in)
957 {
958 if (top_reader_sp)
959 in = top_reader_sp->GetInputStreamFile();
960 else
961 in = GetInputFile();
962
963 // If there is nothing, use stdin
964 if (!in)
965 in = StreamFileSP(new StreamFile(stdin, false));
966 }
967 // If no STDOUT has been set, then set it appropriately
968 if (!out)
969 {
970 if (top_reader_sp)
971 out = top_reader_sp->GetOutputStreamFile();
972 else
973 out = GetOutputFile();
974
975 // If there is nothing, use stdout
976 if (!out)
977 out = StreamFileSP(new StreamFile(stdout, false));
978 }
979 // If no STDERR has been set, then set it appropriately
980 if (!err)
981 {
982 if (top_reader_sp)
983 err = top_reader_sp->GetErrorStreamFile();
984 else
985 err = GetErrorFile();
986
987 // If there is nothing, use stderr
988 if (!err)
989 err = StreamFileSP(new StreamFile(stdout, false));
990
991 }
992}
993
994void
995Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996{
997 if (!reader_sp)
998 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000999
Greg Clayton44d93782014-01-27 23:43:24 +00001000 // Got the current top input reader...
1001 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +00001002
Greg Claytonb4874f12014-02-28 18:22:24 +00001003 // Don't push the same IO handler twice...
1004 if (reader_sp.get() != top_reader_sp.get())
1005 {
1006 // Push our new input reader
1007 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00001008
Greg Claytonb4874f12014-02-28 18:22:24 +00001009 // Interrupt the top input reader to it will exit its Run() function
1010 // and let this new input reader take over
1011 if (top_reader_sp)
1012 top_reader_sp->Deactivate();
1013 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014}
1015
1016bool
Greg Clayton44d93782014-01-27 23:43:24 +00001017Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018{
1019 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +00001020
1021 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022
1023 // The reader on the stop of the stack is done, so let the next
Bruce Mitchener6a7f3332014-06-27 02:42:12 +00001024 // read on the stack refresh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001025 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 {
Greg Clayton44d93782014-01-27 23:43:24 +00001027 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028
1029 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
1030 {
Greg Clayton44d93782014-01-27 23:43:24 +00001031 reader_sp->Deactivate();
Greg Claytonb4874f12014-02-28 18:22:24 +00001032 reader_sp->Cancel();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001033 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +00001034
1035 reader_sp = m_input_reader_stack.Top();
1036 if (reader_sp)
1037 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038
Greg Clayton44d93782014-01-27 23:43:24 +00001039 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040 }
1041 }
1042 return result;
1043}
1044
1045bool
Greg Clayton44d93782014-01-27 23:43:24 +00001046Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047{
Greg Clayton44d93782014-01-27 23:43:24 +00001048 Mutex::Locker locker;
1049
1050 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051 {
Greg Clayton44d93782014-01-27 23:43:24 +00001052 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1053 if (reader_sp)
1054 reader_sp->Hide();
1055 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056 }
Greg Clayton44d93782014-01-27 23:43:24 +00001057 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058}
1059
1060void
Greg Clayton44d93782014-01-27 23:43:24 +00001061Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062{
Greg Clayton44d93782014-01-27 23:43:24 +00001063 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1064 if (reader_sp)
1065 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066}
Greg Clayton66111032010-06-23 01:19:29 +00001067
Greg Clayton44d93782014-01-27 23:43:24 +00001068
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001069StreamSP
1070Debugger::GetAsyncOutputStream ()
1071{
1072 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1073 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1074}
1075
1076StreamSP
1077Debugger::GetAsyncErrorStream ()
1078{
1079 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1080 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1081}
1082
Greg Claytonc7bece562013-01-25 18:06:21 +00001083size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001084Debugger::GetNumDebuggers()
1085{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001086 if (g_shared_debugger_refcount > 0)
1087 {
1088 Mutex::Locker locker (GetDebuggerListMutex ());
1089 return GetDebuggerList().size();
1090 }
1091 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001092}
1093
1094lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001095Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001096{
1097 DebuggerSP debugger_sp;
1098
Greg Claytonc15f55e2012-03-30 20:53:46 +00001099 if (g_shared_debugger_refcount > 0)
1100 {
1101 Mutex::Locker locker (GetDebuggerListMutex ());
1102 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001103
Greg Claytonc15f55e2012-03-30 20:53:46 +00001104 if (index < debugger_list.size())
1105 debugger_sp = debugger_list[index];
1106 }
1107
Enrico Granata061858c2012-02-15 02:34:21 +00001108 return debugger_sp;
1109}
1110
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001111DebuggerSP
1112Debugger::FindDebuggerWithID (lldb::user_id_t id)
1113{
Greg Clayton4d122c42011-09-17 08:33:22 +00001114 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001115
Greg Claytonc15f55e2012-03-30 20:53:46 +00001116 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001117 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001118 Mutex::Locker locker (GetDebuggerListMutex ());
1119 DebuggerList &debugger_list = GetDebuggerList();
1120 DebuggerList::iterator pos, end = debugger_list.end();
1121 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001122 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001123 if ((*pos).get()->GetID() == id)
1124 {
1125 debugger_sp = *pos;
1126 break;
1127 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001128 }
1129 }
1130 return debugger_sp;
1131}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001132
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001133#if 0
Greg Clayton1b654882010-09-19 02:33:57 +00001134static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001135TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001136{
1137 if (frame == NULL)
1138 return;
1139
1140 StreamString s;
1141 const char *prompt_format =
1142 "{addr = '${addr}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001143 "{addr-file-or-load = '${addr-file-or-load}'\n}"
1144 "{current-pc-arrow = '${current-pc-arrow}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001145 "{process.id = '${process.id}'\n}"
1146 "{process.name = '${process.name}'\n}"
1147 "{process.file.basename = '${process.file.basename}'\n}"
1148 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1149 "{thread.id = '${thread.id}'\n}"
1150 "{thread.index = '${thread.index}'\n}"
1151 "{thread.name = '${thread.name}'\n}"
1152 "{thread.queue = '${thread.queue}'\n}"
1153 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1154 "{target.arch = '${target.arch}'\n}"
1155 "{module.file.basename = '${module.file.basename}'\n}"
1156 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1157 "{file.basename = '${file.basename}'\n}"
1158 "{file.fullpath = '${file.fullpath}'\n}"
1159 "{frame.index = '${frame.index}'\n}"
1160 "{frame.pc = '${frame.pc}'\n}"
1161 "{frame.sp = '${frame.sp}'\n}"
1162 "{frame.fp = '${frame.fp}'\n}"
1163 "{frame.flags = '${frame.flags}'\n}"
1164 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1165 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1166 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1167 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1168 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1169 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1170 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1171 "{function.id = '${function.id}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001172 "{function.changed = '${function.changed}'\n}"
1173 "{function.initial-function = '${function.initial-function}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001174 "{function.name = '${function.name}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001175 "{function.name-without-args = '${function.name-without-args}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001176 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001177 "{function.addr-offset = '${function.addr-offset}'\n}"
Jason Molendaaff1b352014-10-10 23:07:36 +00001178 "{function.concrete-only-addr-offset-no-padding = '${function.concrete-only-addr-offset-no-padding}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001179 "{function.line-offset = '${function.line-offset}'\n}"
1180 "{function.pc-offset = '${function.pc-offset}'\n}"
1181 "{line.file.basename = '${line.file.basename}'\n}"
1182 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1183 "{line.number = '${line.number}'\n}"
1184 "{line.start-addr = '${line.start-addr}'\n}"
1185 "{line.end-addr = '${line.end-addr}'\n}"
1186;
1187
1188 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1189 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001190 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001191 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001192 {
1193 printf("%s\n", s.GetData());
1194 }
1195 else
1196 {
Greg Clayton1b654882010-09-19 02:33:57 +00001197 printf ("what we got: %s\n", s.GetData());
1198 }
1199}
Saleem Abdulrasool2643b902014-03-20 06:08:21 +00001200#endif
Greg Clayton1b654882010-09-19 02:33:57 +00001201
Enrico Granata9fc19442011-07-06 02:13:41 +00001202static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001203ScanFormatDescriptor (const char* var_name_begin,
1204 const char* var_name_end,
1205 const char** var_name_final,
1206 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001207 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001208 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001209{
Greg Clayton5160ce52013-03-27 23:08:40 +00001210 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001211 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001212 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001213 {
1214 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001215 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001216 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001217 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001218 else
1219 {
1220 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001221 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001222 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001223 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001224 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001225 true,
1226 *custom_format) )
1227 {
Enrico Granatae992a082011-07-22 17:03:19 +00001228 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001229 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001230
1231 switch (format_name.front())
1232 {
1233 case '@': // if this is an @ sign, print ObjC description
1234 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1235 break;
1236 case 'V': // if this is a V, print the value using the default format
1237 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1238 break;
1239 case 'L': // if this is an L, print the location of the value
1240 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1241 break;
1242 case 'S': // if this is an S, print the summary after all
1243 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1244 break;
1245 case '#': // if this is a '#', print the number of children
1246 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1247 break;
1248 case 'T': // if this is a 'T', print the type
1249 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1250 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001251 case 'N': // if this is a 'N', print the name
1252 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1253 break;
1254 case '>': // if this is a '>', print the name
1255 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1256 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001257 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001258 if (log)
1259 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001260 break;
1261 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001262 }
1263 // a good custom format tells us to print the value using it
1264 else
Enrico Granatae992a082011-07-22 17:03:19 +00001265 {
1266 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001267 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001268 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001269 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001270 }
Enrico Granatae992a082011-07-22 17:03:19 +00001271 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001272 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001273 *custom_format,
1274 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001275 return true;
1276}
1277
1278static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001279ScanBracketedRange (const char* var_name_begin,
1280 const char* var_name_end,
1281 const char* var_name_final,
1282 const char** open_bracket_position,
1283 const char** separator_position,
1284 const char** close_bracket_position,
1285 const char** var_name_final_if_array_range,
1286 int64_t* index_lower,
1287 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001288{
Greg Clayton5160ce52013-03-27 23:08:40 +00001289 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001290 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001291 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001292 {
1293 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1294 *close_bracket_position = ::strchr(*open_bracket_position,']');
1295 // as usual, we assume that [] will come before %
1296 //printf("trying to expand a []\n");
1297 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001298 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001299 {
Enrico Granatae992a082011-07-22 17:03:19 +00001300 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001301 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001302 *index_lower = 0;
1303 }
1304 else if (*separator_position == NULL || *separator_position > var_name_end)
1305 {
1306 char *end = NULL;
1307 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1308 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001309 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001310 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001311 }
Greg Clayton34132752011-07-06 04:07:21 +00001312 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001313 {
1314 char *end = NULL;
1315 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1316 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001317 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001318 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001319 }
1320 else
Enrico Granatae992a082011-07-22 17:03:19 +00001321 {
1322 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001323 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001324 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001325 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001326 if (*index_lower > *index_higher && *index_higher > 0)
1327 {
Enrico Granatae992a082011-07-22 17:03:19 +00001328 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001329 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001330 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001331 *index_lower = *index_higher;
1332 *index_higher = temp;
1333 }
1334 }
Enrico Granatae992a082011-07-22 17:03:19 +00001335 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001336 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001337 return true;
1338}
1339
Michael Sartain0769b2b2013-07-30 16:44:36 +00001340template <typename T>
1341static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1342{
1343 if (script_interpreter)
1344 {
1345 Error script_error;
1346 std::string script_output;
1347
1348 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1349 {
1350 s.Printf("%s", script_output.c_str());
1351 return true;
1352 }
1353 else
1354 {
1355 s.Printf("<error: %s>",script_error.AsCString());
1356 }
1357 }
1358 return false;
1359}
1360
Enrico Granata9fc19442011-07-06 02:13:41 +00001361static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001362ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001363 size_t index,
Jason Molendab57e4a12013-11-04 09:33:30 +00001364 StackFrame* frame,
Enrico Granatadc940732011-08-23 00:32:52 +00001365 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001366{
Greg Clayton5160ce52013-03-27 23:08:40 +00001367 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001368 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001369 std::string ptr_deref_buffer(10,0);
1370 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001371 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001372 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001373 const char* first_unparsed;
1374 ValueObject::GetValueForExpressionPathOptions options;
1375 ValueObject::ExpressionPathEndResultType final_value_type;
1376 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001377 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001378 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001379 &first_unparsed,
1380 &reason_to_stop,
1381 &final_value_type,
1382 options,
1383 &what_next);
1384 if (!item)
1385 {
Enrico Granatae992a082011-07-22 17:03:19 +00001386 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001387 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001388 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001389 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001390 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001391 else
1392 {
Enrico Granatae992a082011-07-22 17:03:19 +00001393 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001394 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001395 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001396 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001397 }
1398 return item;
1399}
1400
Michael Sartain0769b2b2013-07-30 16:44:36 +00001401static inline bool
1402IsToken(const char *var_name_begin, const char *var)
1403{
1404 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1405}
1406
1407static bool
1408IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1409 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1410{
1411 int var_len = strlen(var);
1412 if (::strncmp (var_name_begin, var, var_len) == 0)
1413 {
1414 var_name_begin += var_len;
1415 if (*var_name_begin == '}')
1416 {
1417 format = default_format;
1418 return true;
1419 }
1420 else if (*var_name_begin == '%')
1421 {
1422 // Allow format specifiers: x|X|u with optional width specifiers.
1423 // ${thread.id%x} ; hex
1424 // ${thread.id%X} ; uppercase hex
1425 // ${thread.id%u} ; unsigned decimal
1426 // ${thread.id%8.8X} ; width.precision + specifier
1427 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1428 int dot_count = 0;
1429 const char *specifier = NULL;
1430 int width_precision_length = 0;
1431 const char *width_precision = ++var_name_begin;
1432 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1433 {
1434 dot_count += (*var_name_begin == '.');
1435 if (dot_count > 1)
1436 break;
1437 var_name_begin++;
1438 width_precision_length++;
1439 }
1440
1441 if (IsToken (var_name_begin, "tid}"))
1442 {
1443 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1444 if (target)
1445 {
1446 ArchSpec arch (target->GetArchitecture ());
1447 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1448 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1449 specifier = PRIu64;
1450 }
1451 if (!specifier)
1452 {
1453 format = default_format;
1454 return true;
1455 }
1456 }
1457 else if (IsToken (var_name_begin, "x}"))
1458 specifier = PRIx64;
1459 else if (IsToken (var_name_begin, "X}"))
1460 specifier = PRIX64;
1461 else if (IsToken (var_name_begin, "u}"))
1462 specifier = PRIu64;
1463
1464 if (specifier)
1465 {
1466 format = "%";
1467 if (width_precision_length)
1468 format += std::string(width_precision, width_precision_length);
1469 format += specifier;
1470 return true;
1471 }
1472 }
1473 }
1474 return false;
1475}
1476
Jason Molenda705b1802014-06-13 02:37:02 +00001477// Find information for the "thread.info.*" specifiers in a format string
1478static bool
1479FormatThreadExtendedInfoRecurse
1480(
1481 const char *var_name_begin,
1482 StructuredData::ObjectSP thread_info_dictionary,
1483 const SymbolContext *sc,
1484 const ExecutionContext *exe_ctx,
1485 Stream &s
1486)
1487{
1488 bool var_success = false;
1489 std::string token_format;
1490
1491 llvm::StringRef var_name(var_name_begin);
1492 size_t percent_idx = var_name.find('%');
1493 size_t close_curly_idx = var_name.find('}');
1494 llvm::StringRef path = var_name;
1495 llvm::StringRef formatter = var_name;
1496
1497 // 'path' will be the dot separated list of objects to transverse up until we hit
1498 // a close curly brace, a percent sign, or an end of string.
1499 if (percent_idx != llvm::StringRef::npos || close_curly_idx != llvm::StringRef::npos)
1500 {
1501 if (percent_idx != llvm::StringRef::npos && close_curly_idx != llvm::StringRef::npos)
1502 {
1503 if (percent_idx < close_curly_idx)
1504 {
1505 path = var_name.slice(0, percent_idx);
1506 formatter = var_name.substr (percent_idx);
1507 }
1508 else
1509 {
1510 path = var_name.slice(0, close_curly_idx);
1511 formatter = var_name.substr (close_curly_idx);
1512 }
1513 }
1514 else if (percent_idx != llvm::StringRef::npos)
1515 {
1516 path = var_name.slice(0, percent_idx);
1517 formatter = var_name.substr (percent_idx);
1518 }
1519 else if (close_curly_idx != llvm::StringRef::npos)
1520 {
1521 path = var_name.slice(0, close_curly_idx);
1522 formatter = var_name.substr (close_curly_idx);
1523 }
1524 }
1525
1526 StructuredData::ObjectSP value = thread_info_dictionary->GetObjectForDotSeparatedPath (path);
1527
1528 if (value.get())
1529 {
1530 if (value->GetType() == StructuredData::Type::eTypeInteger)
1531 {
1532 if (IsTokenWithFormat (formatter.str().c_str(), "", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
1533 {
1534 s.Printf(token_format.c_str(), value->GetAsInteger()->GetValue());
1535 var_success = true;
1536 }
1537 }
1538 else if (value->GetType() == StructuredData::Type::eTypeFloat)
1539 {
1540 s.Printf ("%f", value->GetAsFloat()->GetValue());
1541 var_success = true;
1542 }
1543 else if (value->GetType() == StructuredData::Type::eTypeString)
1544 {
1545 s.Printf("%s", value->GetAsString()->GetValue().c_str());
1546 var_success = true;
1547 }
1548 else if (value->GetType() == StructuredData::Type::eTypeArray)
1549 {
1550 if (value->GetAsArray()->GetSize() > 0)
1551 {
1552 s.Printf ("%zu", value->GetAsArray()->GetSize());
1553 var_success = true;
1554 }
1555 }
1556 else if (value->GetType() == StructuredData::Type::eTypeDictionary)
1557 {
1558 s.Printf ("%zu", value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize());
1559 var_success = true;
1560 }
1561 }
1562
1563 return var_success;
1564}
1565
1566
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001567static bool
1568FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001569(
1570 const char *format,
1571 const SymbolContext *sc,
1572 const ExecutionContext *exe_ctx,
1573 const Address *addr,
1574 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001575 const char **end,
Jason Molendaaff1b352014-10-10 23:07:36 +00001576 ValueObject* valobj,
1577 bool function_changed,
1578 bool initial_function
Greg Clayton1b654882010-09-19 02:33:57 +00001579)
1580{
Enrico Granatac482a192011-08-17 22:13:59 +00001581 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001582 bool success = true;
1583 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001584 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001585
Greg Clayton1b654882010-09-19 02:33:57 +00001586 for (p = format; *p != '\0'; ++p)
1587 {
Enrico Granatac482a192011-08-17 22:13:59 +00001588 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001589 {
Enrico Granatac482a192011-08-17 22:13:59 +00001590 valobj = realvalobj;
1591 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001592 }
Greg Clayton1b654882010-09-19 02:33:57 +00001593 size_t non_special_chars = ::strcspn (p, "${}\\");
1594 if (non_special_chars > 0)
1595 {
1596 if (success)
1597 s.Write (p, non_special_chars);
1598 p += non_special_chars;
1599 }
1600
1601 if (*p == '\0')
1602 {
1603 break;
1604 }
1605 else if (*p == '{')
1606 {
1607 // Start a new scope that must have everything it needs if it is to
1608 // to make it into the final output stream "s". If you want to make
1609 // a format that only prints out the function or symbol name if there
1610 // is one in the symbol context you can use:
1611 // "{function =${function.name}}"
1612 // The first '{' starts a new scope that end with the matching '}' at
1613 // the end of the string. The contents "function =${function.name}"
1614 // will then be evaluated and only be output if there is a function
1615 // or symbol with a valid name.
1616 StreamString sub_strm;
1617
1618 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001619
Jason Molendaaff1b352014-10-10 23:07:36 +00001620 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj, function_changed, initial_function))
Greg Clayton1b654882010-09-19 02:33:57 +00001621 {
1622 // The stream had all it needed
1623 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1624 }
1625 if (*p != '}')
1626 {
1627 success = false;
1628 break;
1629 }
1630 }
1631 else if (*p == '}')
1632 {
1633 // End of a enclosing scope
1634 break;
1635 }
1636 else if (*p == '$')
1637 {
1638 // We have a prompt variable to print
1639 ++p;
1640 if (*p == '{')
1641 {
1642 ++p;
1643 const char *var_name_begin = p;
1644 const char *var_name_end = ::strchr (p, '}');
1645
1646 if (var_name_end && var_name_begin < var_name_end)
1647 {
1648 // if we have already failed to parse, skip this variable
1649 if (success)
1650 {
1651 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001652 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001653 Address format_addr;
Jason Molendaaff1b352014-10-10 23:07:36 +00001654
1655 // normally "addr" means print a raw address but
1656 // "file-addr-or-load-addr" means print a module + file addr if there's no load addr
1657 bool print_file_addr_or_load_addr = false;
1658 bool addr_offset_concrete_func_only = false;
1659 bool addr_offset_print_with_no_padding = false;
Greg Clayton1b654882010-09-19 02:33:57 +00001660 bool calculate_format_addr_function_offset = false;
1661 // Set reg_kind and reg_num to invalid values
1662 RegisterKind reg_kind = kNumRegisterKinds;
1663 uint32_t reg_num = LLDB_INVALID_REGNUM;
1664 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001665 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001666 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001667 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001668 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1669 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001670
Greg Clayton1b654882010-09-19 02:33:57 +00001671 // Each variable must set success to true below...
1672 bool var_success = false;
1673 switch (var_name_begin[0])
1674 {
Enrico Granata4becb372011-06-29 22:27:15 +00001675 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001676 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001677 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001678 {
Enrico Granatac482a192011-08-17 22:13:59 +00001679 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001680 break;
1681
Enrico Granatac3e320a2011-08-02 17:27:39 +00001682 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001683 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001684
Enrico Granata6f3533f2011-07-29 19:53:35 +00001685 // check for *var and *svar
1686 if (*var_name_begin == '*')
1687 {
1688 do_deref_pointer = true;
1689 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001690 if (log)
1691 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001692 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001693
Enrico Granata6f3533f2011-07-29 19:53:35 +00001694 if (*var_name_begin == 's')
1695 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001696 if (!valobj->IsSynthetic())
1697 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001698 if (!valobj)
1699 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001700 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001701 if (log)
1702 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001703 }
1704
1705 // should be a 'v' by now
1706 if (*var_name_begin != 'v')
1707 break;
1708
Enrico Granatac3e320a2011-08-02 17:27:39 +00001709 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001710 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001711
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001712 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001713 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001714 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001715 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001716 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001717 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001718 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001719 const char* var_name_final = NULL;
1720 const char* var_name_final_if_array_range = NULL;
1721 const char* close_bracket_position = NULL;
1722 int64_t index_lower = -1;
1723 int64_t index_higher = -1;
1724 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001725 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001726 bool was_plain_var = false;
1727 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001728 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001729
Enrico Granatac482a192011-08-17 22:13:59 +00001730 if (!valobj) break;
1731 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001732 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001733 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001734 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001735 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001736 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001737 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001738 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001739 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001740 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001741 // this is a variable with some custom format applied to it
1742 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001743 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001744 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001745 ScanFormatDescriptor (var_name_begin,
1746 var_name_end,
1747 &var_name_final,
1748 &percent_position,
1749 &custom_format,
1750 &val_obj_display);
1751 }
1752 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001753 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001754 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001755 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001756 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001757 const char* percent_position;
1758 ScanFormatDescriptor (var_name_begin,
1759 var_name_end,
1760 &var_name_final,
1761 &percent_position,
1762 &custom_format,
1763 &val_obj_display);
1764
1765 const char* open_bracket_position;
1766 const char* separator_position;
1767 ScanBracketedRange (var_name_begin,
1768 var_name_end,
1769 var_name_final,
1770 &open_bracket_position,
1771 &separator_position,
1772 &close_bracket_position,
1773 &var_name_final_if_array_range,
1774 &index_lower,
1775 &index_higher);
1776
1777 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001778
Enrico Granata599171a2013-02-01 23:59:44 +00001779 std::string expr_path(var_name_final-var_name_begin-1,0);
1780 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1781
1782 if (log)
1783 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1784
1785 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001786 &first_unparsed,
1787 &reason_to_stop,
1788 &final_value_type,
1789 options,
1790 &what_next).get();
1791
1792 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001793 {
Enrico Granatae992a082011-07-22 17:03:19 +00001794 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001795 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001796 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001797 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001798 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001799 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001800 else
1801 {
Enrico Granatae992a082011-07-22 17:03:19 +00001802 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001803 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001804 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001805 first_unparsed, reason_to_stop, final_value_type);
1806 }
Enrico Granata4becb372011-06-29 22:27:15 +00001807 }
Greg Clayton34132752011-07-06 04:07:21 +00001808 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001809 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001810
Enrico Granata86cc9822012-03-19 22:58:49 +00001811 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1812 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001813
Enrico Granata86cc9822012-03-19 22:58:49 +00001814 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001815
Enrico Granataa7187d02011-07-06 19:27:11 +00001816 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001817 {
Greg Clayton34132752011-07-06 04:07:21 +00001818 // I have not deref-ed yet, let's do it
1819 // this happens when we are not going through GetValueForVariableExpressionPath
1820 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001821 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001822 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001823 if (error.Fail())
1824 {
1825 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001826 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001827 break;
1828 }
Greg Clayton34132752011-07-06 04:07:21 +00001829 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001830 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001831
Jim Inghamf164d942014-03-11 18:17:23 +00001832 if (!target)
1833 {
1834 if (log)
1835 log->Printf("[Debugger::FormatPrompt] could not calculate target for prompt expression");
1836 break;
1837 }
1838
Enrico Granataa777dc22012-05-08 21:49:57 +00001839 // we do not want to use the summary for a bitfield of type T:n
1840 // if we were originally dealing with just a T - that would get
1841 // us into an endless recursion
1842 if (target->IsBitfield() && was_var_indexed)
1843 {
1844 // TODO: check for a (T:n)-specific summary - we should still obey that
1845 StreamString bitfield_name;
1846 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1847 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1848 if (!DataVisualization::GetSummaryForType(type_sp))
1849 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1850 }
1851
Enrico Granata85933ed2011-08-18 16:38:26 +00001852 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001853 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1854 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1855 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1856 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001857
Enrico Granata86cc9822012-03-19 22:58:49 +00001858 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 +00001859 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001860 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001861 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001862 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001863
Greg Clayton5088c482013-03-25 21:06:13 +00001864 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001865 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001866 // try to use the special cases
1867 var_success = target->DumpPrintableRepresentation(str_temp,
1868 val_obj_display,
1869 custom_format);
1870 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001871 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001872
1873 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001874 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001875 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001876 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001877 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001878 }
1879 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001880 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001881 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001882 {
1883 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1884 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001885 else if (is_pointer) // if pointer, value is the address stored
1886 {
Greg Clayton23f59502012-07-17 03:23:13 +00001887 target->DumpPrintableRepresentation (s,
1888 val_obj_display,
1889 custom_format,
1890 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001891 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001892 var_success = true;
1893 break;
1894 }
1895 }
1896
1897 // if directly trying to print ${var}, and this is an aggregate, display a nice
1898 // type @ location message
1899 if (is_aggregate && was_plain_var)
1900 {
1901 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1902 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001903 break;
1904 }
1905
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001906 // 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 +00001907 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001908 {
1909 s << "<invalid use of aggregate type>";
1910 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001911 break;
1912 }
Greg Clayton34132752011-07-06 04:07:21 +00001913
1914 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001915 {
1916 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001917 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001918 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001919 }
Greg Clayton34132752011-07-06 04:07:21 +00001920 else
Enrico Granatae992a082011-07-22 17:03:19 +00001921 {
1922 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001923 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001924 if (!is_array && !is_pointer)
1925 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001926 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001927 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001928 const char* special_directions = NULL;
1929 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001930 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1931 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001932 ConstString additional_data;
1933 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1934 special_directions_writer.Printf("${%svar%s}",
1935 do_deref_pointer ? "*" : "",
1936 additional_data.GetCString());
1937 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001938 }
1939
1940 // let us display items index_lower thru index_higher of this array
1941 s.PutChar('[');
1942 var_success = true;
1943
1944 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001945 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001946
Greg Claytoncc4d0142012-02-17 07:49:44 +00001947 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001948
Greg Clayton34132752011-07-06 04:07:21 +00001949 for (;index_lower<=index_higher;index_lower++)
1950 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001951 ValueObject* item = ExpandIndexedExpression (target,
1952 index_lower,
1953 exe_ctx->GetFramePtr(),
1954 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001955
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001956 if (!item)
1957 {
Enrico Granatae992a082011-07-22 17:03:19 +00001958 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001959 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001960 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001961 else
1962 {
Enrico Granatae992a082011-07-22 17:03:19 +00001963 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001964 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001965 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001966
Greg Clayton34132752011-07-06 04:07:21 +00001967 if (!special_directions)
1968 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1969 else
Jason Molendaaff1b352014-10-10 23:07:36 +00001970 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item, function_changed, initial_function);
Greg Clayton34132752011-07-06 04:07:21 +00001971
Enrico Granata22c55d12011-08-12 02:00:06 +00001972 if (--max_num_children == 0)
1973 {
1974 s.PutCString(", ...");
1975 break;
1976 }
1977
Greg Clayton34132752011-07-06 04:07:21 +00001978 if (index_lower < index_higher)
1979 s.PutChar(',');
1980 }
1981 s.PutChar(']');
1982 }
Enrico Granata4becb372011-06-29 22:27:15 +00001983 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001984 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001985 case 'a':
Jason Molendaaff1b352014-10-10 23:07:36 +00001986 if (IsToken (var_name_begin, "addr-file-or-load}"))
1987 {
1988 print_file_addr_or_load_addr = true;
1989 }
1990 if (IsToken (var_name_begin, "addr}")
1991 || IsToken (var_name_begin, "addr-file-or-load}"))
Greg Clayton1b654882010-09-19 02:33:57 +00001992 {
1993 if (addr && addr->IsValid())
1994 {
1995 var_success = true;
1996 format_addr = *addr;
1997 }
1998 }
1999 break;
2000
2001 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002002 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00002003 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002004 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002005 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002006 Process *process = exe_ctx->GetProcessPtr();
2007 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00002008 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002009 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002010 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002011 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002012 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002013 var_success = true;
2014 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002015 else if ((IsToken (var_name_begin, "name}")) ||
2016 (IsToken (var_name_begin, "file.basename}")) ||
2017 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00002018 {
2019 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
2020 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00002021 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002022 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
2023 {
2024 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002025 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00002026 }
2027 else
2028 {
2029 format_file_spec = exe_module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002030 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00002031 }
Greg Clayton1b654882010-09-19 02:33:57 +00002032 }
2033 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002034 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002035 {
2036 var_name_begin += ::strlen("script:");
2037 std::string script_name(var_name_begin,var_name_end);
2038 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002039 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
2040 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002041 }
Greg Clayton1b654882010-09-19 02:33:57 +00002042 }
Greg Claytonc14ee322011-09-22 04:58:26 +00002043 }
Greg Clayton1b654882010-09-19 02:33:57 +00002044 }
2045 break;
2046
2047 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002048 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00002049 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002050 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002051 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002052 Thread *thread = exe_ctx->GetThreadPtr();
2053 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00002054 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002055 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002056 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002057 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002058 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002059 var_success = true;
2060 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002061 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00002062 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002063 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00002064 var_success = true;
2065 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002066 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00002067 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002068 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00002069 var_success = true;
2070 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002071 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002072 {
2073 cstr = thread->GetName();
2074 var_success = cstr && cstr[0];
2075 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002076 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00002077 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002078 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002079 {
2080 cstr = thread->GetQueueName();
2081 var_success = cstr && cstr[0];
2082 if (var_success)
2083 s.PutCString(cstr);
2084 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002085 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002086 {
2087 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002088 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00002089 {
2090 cstr = stop_info_sp->GetDescription();
2091 if (cstr && cstr[0])
2092 {
2093 s.PutCString(cstr);
2094 var_success = true;
2095 }
Greg Clayton1b654882010-09-19 02:33:57 +00002096 }
2097 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002098 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00002099 {
2100 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00002101 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00002102 {
2103 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
2104 if (return_valobj_sp)
2105 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00002106 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00002107 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00002108 }
2109 }
2110 }
Jim Ingham30fadaf2014-07-08 01:07:32 +00002111 else if (IsToken (var_name_begin, "completed-expression}"))
2112 {
2113 StopInfoSP stop_info_sp = thread->GetStopInfo ();
2114 if (stop_info_sp && stop_info_sp->IsValid())
2115 {
2116 ClangExpressionVariableSP expression_var_sp = StopInfo::GetExpressionVariable (stop_info_sp);
2117 if (expression_var_sp && expression_var_sp->GetValueObject())
2118 {
2119 expression_var_sp->GetValueObject()->Dump(s);
2120 var_success = true;
2121 }
2122 }
2123 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002124 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002125 {
2126 var_name_begin += ::strlen("script:");
2127 std::string script_name(var_name_begin,var_name_end);
2128 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002129 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
2130 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002131 }
Jason Molenda705b1802014-06-13 02:37:02 +00002132 else if (IsToken (var_name_begin, "info."))
2133 {
2134 var_name_begin += ::strlen("info.");
2135 StructuredData::ObjectSP object_sp = thread->GetExtendedInfo();
2136 if (object_sp && object_sp->GetType() == StructuredData::Type::eTypeDictionary)
2137 {
2138 var_success = FormatThreadExtendedInfoRecurse (var_name_begin, object_sp, sc, exe_ctx, s);
2139 }
2140 }
Greg Clayton1b654882010-09-19 02:33:57 +00002141 }
2142 }
2143 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002144 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00002145 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002146 // TODO: hookup properties
2147// if (!target_properties_sp)
2148// {
2149// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2150// if (target)
2151// target_properties_sp = target->GetProperties();
2152// }
2153//
2154// if (target_properties_sp)
2155// {
2156// var_name_begin += ::strlen ("target.");
2157// const char *end_property = strchr(var_name_begin, '}');
2158// if (end_property)
2159// {
2160// ConstString property_name(var_name_begin, end_property - var_name_begin);
2161// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2162// if (!property_value.empty())
2163// {
2164// s.PutCString (property_value.c_str());
2165// var_success = true;
2166// }
2167// }
2168// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002169 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2170 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002171 {
Greg Clayton1b654882010-09-19 02:33:57 +00002172 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002173 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002174 {
2175 ArchSpec arch (target->GetArchitecture ());
2176 if (arch.IsValid())
2177 {
Greg Clayton64195a22011-02-23 00:35:02 +00002178 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002179 var_success = true;
2180 }
2181 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002182 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002183 {
2184 var_name_begin += ::strlen("script:");
2185 std::string script_name(var_name_begin,var_name_end);
2186 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002187 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2188 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002189 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002190 }
Greg Clayton1b654882010-09-19 02:33:57 +00002191 }
2192 break;
Jason Molendaaff1b352014-10-10 23:07:36 +00002193
Greg Clayton1b654882010-09-19 02:33:57 +00002194 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002195 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002196 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002197 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002198 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002199 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002200 var_name_begin += ::strlen ("module.");
2201
Michael Sartain0769b2b2013-07-30 16:44:36 +00002202 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002203 {
2204 if (module->GetFileSpec())
2205 {
2206 var_name_begin += ::strlen ("file.");
2207
Michael Sartain0769b2b2013-07-30 16:44:36 +00002208 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002209 {
2210 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002211 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002212 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002213 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002214 {
2215 format_file_spec = module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002216 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002217 }
2218 }
2219 }
2220 }
2221 }
2222 break;
2223
2224
2225 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002226 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002227 {
2228 if (sc && sc->comp_unit != NULL)
2229 {
2230 var_name_begin += ::strlen ("file.");
2231
Michael Sartain0769b2b2013-07-30 16:44:36 +00002232 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002233 {
2234 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002235 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002236 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002237 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002238 {
2239 format_file_spec = *sc->comp_unit;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002240 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002241 }
2242 }
2243 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002244 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002245 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002246 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002247 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002248 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002249 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002250 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002251 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002252 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002253 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002254 s.Printf("%u", frame->GetFrameIndex());
2255 var_success = true;
2256 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002257 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002258 {
2259 reg_kind = eRegisterKindGeneric;
2260 reg_num = LLDB_REGNUM_GENERIC_PC;
2261 var_success = true;
2262 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002263 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002264 {
2265 reg_kind = eRegisterKindGeneric;
2266 reg_num = LLDB_REGNUM_GENERIC_SP;
2267 var_success = true;
2268 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002269 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002270 {
2271 reg_kind = eRegisterKindGeneric;
2272 reg_num = LLDB_REGNUM_GENERIC_FP;
2273 var_success = true;
2274 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002275 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002276 {
2277 reg_kind = eRegisterKindGeneric;
2278 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2279 var_success = true;
2280 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002281 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002282 {
2283 reg_ctx = frame->GetRegisterContext().get();
2284 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002285 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002286 var_name_begin += ::strlen ("reg.");
2287 if (var_name_begin < var_name_end)
2288 {
2289 std::string reg_name (var_name_begin, var_name_end);
2290 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2291 if (reg_info)
2292 var_success = true;
2293 }
Greg Clayton1b654882010-09-19 02:33:57 +00002294 }
2295 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002296 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002297 {
2298 var_name_begin += ::strlen("script:");
2299 std::string script_name(var_name_begin,var_name_end);
2300 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002301 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2302 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002303 }
Greg Clayton1b654882010-09-19 02:33:57 +00002304 }
2305 }
2306 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002307 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002308 {
2309 if (sc && (sc->function != NULL || sc->symbol != NULL))
2310 {
2311 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002312 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002313 {
2314 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002315 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002316 else
2317 s.Printf("symbol[%u]", sc->symbol->GetID());
2318
2319 var_success = true;
2320 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002321 if (IsToken (var_name_begin, "changed}") && function_changed)
2322 {
2323 var_success = true;
2324 }
2325 if (IsToken (var_name_begin, "initial-function}") && initial_function)
2326 {
2327 var_success = true;
2328 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002329 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002330 {
2331 if (sc->function)
2332 cstr = sc->function->GetName().AsCString (NULL);
2333 else if (sc->symbol)
2334 cstr = sc->symbol->GetName().AsCString (NULL);
2335 if (cstr)
2336 {
2337 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002338
2339 if (sc->block)
2340 {
2341 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2342 if (inline_block)
2343 {
2344 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2345 if (inline_info)
2346 {
2347 s.PutCString(" [inlined] ");
2348 inline_info->GetName().Dump(&s);
2349 }
2350 }
2351 }
Greg Clayton1b654882010-09-19 02:33:57 +00002352 var_success = true;
2353 }
2354 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002355 else if (IsToken (var_name_begin, "name-without-args}"))
2356 {
2357 ConstString name;
2358 if (sc->function)
2359 name = sc->function->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments);
2360 else if (sc->symbol)
2361 name = sc->symbol->GetMangled().GetName (Mangled::ePreferDemangledWithoutArguments);
2362 if (name)
2363 {
2364 s.PutCString(name.GetCString());
2365 var_success = true;
2366 }
2367 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002368 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002369 {
2370 // Print the function name with arguments in it
2371
2372 if (sc->function)
2373 {
2374 var_success = true;
2375 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2376 cstr = sc->function->GetName().AsCString (NULL);
2377 if (cstr)
2378 {
2379 const InlineFunctionInfo *inline_info = NULL;
2380 VariableListSP variable_list_sp;
2381 bool get_function_vars = true;
2382 if (sc->block)
2383 {
2384 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2385
2386 if (inline_block)
2387 {
2388 get_function_vars = false;
2389 inline_info = sc->block->GetInlinedFunctionInfo();
2390 if (inline_info)
2391 variable_list_sp = inline_block->GetBlockVariableList (true);
2392 }
2393 }
2394
2395 if (get_function_vars)
2396 {
2397 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2398 }
2399
2400 if (inline_info)
2401 {
2402 s.PutCString (cstr);
2403 s.PutCString (" [inlined] ");
2404 cstr = inline_info->GetName().GetCString();
2405 }
2406
2407 VariableList args;
2408 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002409 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002410 if (args.GetSize() > 0)
2411 {
2412 const char *open_paren = strchr (cstr, '(');
Enrico Granatae4a4f5d2014-08-16 00:56:04 +00002413 const char *close_paren = nullptr;
2414 const char *generic = strchr(cstr, '<');
2415 // if before the arguments list begins there is a template sign
2416 // then scan to the end of the generic args before you try to find
2417 // the arguments list
2418 if (generic && open_paren && generic < open_paren)
2419 {
2420 int generic_depth = 1;
2421 ++generic;
2422 for (;
2423 *generic && generic_depth > 0;
2424 generic++)
2425 {
2426 if (*generic == '<')
2427 generic_depth++;
2428 if (*generic == '>')
2429 generic_depth--;
2430 }
2431 if (*generic)
2432 open_paren = strchr(generic, '(');
2433 else
2434 open_paren = nullptr;
2435 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002436 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002437 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002438 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002439 {
2440 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2441 if (open_paren)
2442 close_paren = strchr (open_paren, ')');
2443 }
2444 else
2445 close_paren = strchr (open_paren, ')');
2446 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002447
2448 if (open_paren)
2449 s.Write(cstr, open_paren - cstr + 1);
2450 else
2451 {
2452 s.PutCString (cstr);
2453 s.PutChar ('(');
2454 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002455 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002456 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2457 {
Enrico Granata894f7352014-03-25 22:03:52 +00002458 std::string buffer;
2459
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002460 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2461 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
Enrico Granata894f7352014-03-25 22:03:52 +00002462 const char *var_representation = nullptr;
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002463 const char *var_name = var_value_sp->GetName().GetCString();
Enrico Granata894f7352014-03-25 22:03:52 +00002464 if (var_value_sp->GetClangType().IsAggregateType() &&
2465 DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get()))
2466 {
2467 static StringSummaryFormat format(TypeSummaryImpl::Flags()
2468 .SetHideItemNames(false)
2469 .SetShowMembersOneLiner(true),
2470 "");
2471 format.FormatObject(var_value_sp.get(), buffer);
2472 var_representation = buffer.c_str();
2473 }
2474 else
2475 var_representation = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002476 if (arg_idx > 0)
2477 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002478 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002479 {
Enrico Granata894f7352014-03-25 22:03:52 +00002480 if (var_representation)
2481 s.Printf ("%s=%s", var_name, var_representation);
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002482 else
2483 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2484 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002485 else
2486 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002487 }
2488
2489 if (close_paren)
2490 s.PutCString (close_paren);
2491 else
2492 s.PutChar(')');
2493
2494 }
2495 else
2496 {
2497 s.PutCString(cstr);
2498 }
2499 }
2500 }
2501 else if (sc->symbol)
2502 {
2503 cstr = sc->symbol->GetName().AsCString (NULL);
2504 if (cstr)
2505 {
2506 s.PutCString(cstr);
2507 var_success = true;
2508 }
2509 }
2510 }
Jason Molendaaff1b352014-10-10 23:07:36 +00002511 else if (IsToken (var_name_begin, "addr-offset}")
2512 || IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002513 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002514 if (IsToken (var_name_begin, "concrete-only-addr-offset-no-padding}"))
2515 {
2516 addr_offset_print_with_no_padding = true;
2517 addr_offset_concrete_func_only = true;
2518 }
Greg Clayton1b654882010-09-19 02:33:57 +00002519 var_success = addr != NULL;
2520 if (var_success)
2521 {
2522 format_addr = *addr;
2523 calculate_format_addr_function_offset = true;
2524 }
2525 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002526 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002527 {
2528 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2529 if (var_success)
2530 {
2531 format_addr = sc->line_entry.range.GetBaseAddress();
2532 calculate_format_addr_function_offset = true;
2533 }
2534 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002535 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002536 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002537 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002538 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002539 if (var_success)
2540 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002541 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002542 calculate_format_addr_function_offset = true;
2543 }
2544 }
2545 }
2546 }
2547 break;
2548
2549 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002550 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002551 {
2552 if (sc && sc->line_entry.IsValid())
2553 {
2554 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002555 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002556 {
2557 var_name_begin += ::strlen ("file.");
2558
Michael Sartain0769b2b2013-07-30 16:44:36 +00002559 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002560 {
2561 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002562 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002563 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002564 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002565 {
2566 format_file_spec = sc->line_entry.file;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002567 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002568 }
2569 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002570 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002571 {
2572 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002573 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002574 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002575 else if ((IsToken (var_name_begin, "start-addr}")) ||
2576 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002577 {
2578 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2579 if (var_success)
2580 {
2581 format_addr = sc->line_entry.range.GetBaseAddress();
2582 if (var_name_begin[0] == 'e')
2583 format_addr.Slide (sc->line_entry.range.GetByteSize());
2584 }
2585 }
2586 }
2587 }
2588 break;
Jason Molendaaff1b352014-10-10 23:07:36 +00002589 case 'c':
2590 if (IsToken (var_name_begin, "current-pc-arrow"))
2591 {
2592 if (addr && exe_ctx && exe_ctx->GetFramePtr())
2593 {
2594 RegisterContextSP reg_ctx = exe_ctx->GetFramePtr()->GetRegisterContextSP();
2595 if (reg_ctx.get())
2596 {
2597 addr_t pc_loadaddr = reg_ctx->GetPC();
2598 if (pc_loadaddr != LLDB_INVALID_ADDRESS)
2599 {
2600 Address pc;
2601 pc.SetLoadAddress (pc_loadaddr, exe_ctx->GetTargetPtr());
2602 if (pc == *addr)
2603 {
2604 s.Printf ("->");
2605 var_success = true;
2606 }
2607 else
2608 {
2609 s.Printf(" ");
2610 var_success = true;
2611 }
2612 }
2613 }
2614 }
2615 }
2616 break;
Greg Clayton1b654882010-09-19 02:33:57 +00002617 }
2618
2619 if (var_success)
2620 {
2621 // If format addr is valid, then we need to print an address
2622 if (reg_num != LLDB_INVALID_REGNUM)
2623 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002624 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002625 // We have a register value to display...
2626 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2627 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002628 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002629 }
2630 else
2631 {
2632 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002633 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002634
2635 if (reg_ctx)
2636 {
2637 if (reg_kind != kNumRegisterKinds)
2638 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2639 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2640 var_success = reg_info != NULL;
2641 }
2642 }
2643 }
2644
2645 if (reg_info != NULL)
2646 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002647 RegisterValue reg_value;
2648 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2649 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002650 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002651 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002652 }
2653 }
2654
2655 if (format_file_spec)
2656 {
2657 s << format_file_spec;
2658 }
2659
2660 // If format addr is valid, then we need to print an address
2661 if (format_addr.IsValid())
2662 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002663 var_success = false;
2664
Greg Clayton1b654882010-09-19 02:33:57 +00002665 if (calculate_format_addr_function_offset)
2666 {
2667 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002668
Greg Clayton0603aa92010-10-04 01:05:56 +00002669 if (sc)
2670 {
2671 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002672 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002673 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Jason Molendaaff1b352014-10-10 23:07:36 +00002674 if (sc->block && addr_offset_concrete_func_only == false)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002675 {
2676 // Check to make sure we aren't in an inline
2677 // function. If we are, use the inline block
2678 // range that contains "format_addr" since
2679 // blocks can be discontiguous.
2680 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2681 AddressRange inline_range;
2682 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2683 func_addr = inline_range.GetBaseAddress();
2684 }
2685 }
Greg Claytone7612132012-03-07 21:03:09 +00002686 else if (sc->symbol && sc->symbol->ValueIsAddress())
2687 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002688 }
2689
2690 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002691 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002692 const char *addr_offset_padding = " ";
2693 if (addr_offset_print_with_no_padding)
2694 {
2695 addr_offset_padding = "";
2696 }
Greg Clayton1b654882010-09-19 02:33:57 +00002697 if (func_addr.GetSection() == format_addr.GetSection())
2698 {
2699 addr_t func_file_addr = func_addr.GetFileAddress();
2700 addr_t addr_file_addr = format_addr.GetFileAddress();
2701 if (addr_file_addr > func_file_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002702 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002703 else if (addr_file_addr < func_file_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002704 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002705 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002706 }
2707 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002708 {
2709 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2710 if (target)
2711 {
2712 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2713 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2714 if (addr_load_addr > func_load_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002715 s.Printf("%s+%s%" PRIu64, addr_offset_padding, addr_offset_padding, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002716 else if (addr_load_addr < func_load_addr)
Jason Molendaaff1b352014-10-10 23:07:36 +00002717 s.Printf("%s-%s%" PRIu64, addr_offset_padding, addr_offset_padding, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002718 var_success = true;
2719 }
2720 }
Greg Clayton1b654882010-09-19 02:33:57 +00002721 }
2722 }
2723 else
2724 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002725 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002726 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002727 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2728 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002729 if (vaddr == LLDB_INVALID_ADDRESS)
2730 vaddr = format_addr.GetFileAddress ();
2731
2732 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002733 {
Jason Molendaaff1b352014-10-10 23:07:36 +00002734 int addr_width = 0;
2735 if (exe_ctx && target)
2736 {
2737 target->GetArchitecture().GetAddressByteSize() * 2;
2738 }
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002739 if (addr_width == 0)
2740 addr_width = 16;
Jason Molendaaff1b352014-10-10 23:07:36 +00002741 if (print_file_addr_or_load_addr)
2742 {
2743 format_addr.Dump (&s, exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress, 0);
2744 }
2745 else
2746 {
2747 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
2748 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002749 var_success = true;
2750 }
Greg Clayton1b654882010-09-19 02:33:57 +00002751 }
2752 }
2753 }
2754
2755 if (var_success == false)
2756 success = false;
2757 }
2758 p = var_name_end;
2759 }
2760 else
2761 break;
2762 }
2763 else
2764 {
2765 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2766 s.PutChar(*p);
2767 }
2768 }
2769 else if (*p == '\\')
2770 {
2771 ++p; // skip the slash
2772 switch (*p)
2773 {
2774 case 'a': s.PutChar ('\a'); break;
2775 case 'b': s.PutChar ('\b'); break;
2776 case 'f': s.PutChar ('\f'); break;
2777 case 'n': s.PutChar ('\n'); break;
2778 case 'r': s.PutChar ('\r'); break;
2779 case 't': s.PutChar ('\t'); break;
2780 case 'v': s.PutChar ('\v'); break;
2781 case '\'': s.PutChar ('\''); break;
2782 case '\\': s.PutChar ('\\'); break;
2783 case '0':
2784 // 1 to 3 octal chars
2785 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002786 // Make a string that can hold onto the initial zero char,
2787 // up to 3 octal digits, and a terminating NULL.
2788 char oct_str[5] = { 0, 0, 0, 0, 0 };
2789
2790 int i;
2791 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2792 oct_str[i] = p[i];
2793
2794 // We don't want to consume the last octal character since
2795 // the main for loop will do this for us, so we advance p by
2796 // one less than i (even if i is zero)
2797 p += i - 1;
2798 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2799 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002800 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002801 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002802 }
Greg Clayton1b654882010-09-19 02:33:57 +00002803 }
2804 break;
2805
2806 case 'x':
2807 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002808 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002809 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002810 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002811
Greg Clayton0603aa92010-10-04 01:05:56 +00002812 // Make a string that can hold onto two hex chars plus a
2813 // NULL terminator
2814 char hex_str[3] = { 0,0,0 };
2815 hex_str[0] = *p;
2816 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002817 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002818 ++p; // Skip the first of the two hex chars
2819 hex_str[1] = *p;
2820 }
2821
2822 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2823 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002824 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002825 }
2826 else
2827 {
2828 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002829 }
2830 break;
2831
2832 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002833 // Just desensitize any other character by just printing what
2834 // came after the '\'
2835 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002836 break;
2837
2838 }
2839
2840 }
2841 }
2842 if (end)
2843 *end = p;
2844 return success;
2845}
2846
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002847bool
2848Debugger::FormatPrompt
2849(
2850 const char *format,
2851 const SymbolContext *sc,
2852 const ExecutionContext *exe_ctx,
2853 const Address *addr,
2854 Stream &s,
2855 ValueObject* valobj
2856)
2857{
2858 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2859 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2860 if (format_str.length())
2861 format = format_str.c_str();
Jason Molendaaff1b352014-10-10 23:07:36 +00002862 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj, false, false);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002863}
2864
Jason Molendaaff1b352014-10-10 23:07:36 +00002865bool
2866Debugger::FormatDisassemblerAddress (const char *format,
2867 const SymbolContext *sc,
2868 const SymbolContext *prev_sc,
2869 const ExecutionContext *exe_ctx,
2870 const Address *addr,
2871 Stream &s)
2872{
2873 if (format == NULL && exe_ctx != NULL && exe_ctx->HasTargetScope())
2874 {
2875 format = exe_ctx->GetTargetRef().GetDebugger().GetDisassemblyFormat();
2876 }
2877 bool function_changed = false;
2878 bool initial_function = false;
2879 if (prev_sc && (prev_sc->function || prev_sc->symbol))
2880 {
2881 if (sc && (sc->function || sc->symbol))
2882 {
2883 if (prev_sc->symbol && sc->symbol)
2884 {
2885 if (!sc->symbol->Compare (prev_sc->symbol->GetName(), prev_sc->symbol->GetType()))
2886 {
2887 function_changed = true;
2888 }
2889 }
2890 else if (prev_sc->function && sc->function)
2891 {
2892 if (prev_sc->function->GetMangled() != sc->function->GetMangled())
2893 {
2894 function_changed = true;
2895 }
2896 }
2897 }
2898 }
2899 // The first context on a list of instructions will have a prev_sc that
2900 // has no Function or Symbol -- if SymbolContext had an IsValid() method, it
2901 // would return false. But we do get a prev_sc pointer.
2902 if ((sc && (sc->function || sc->symbol))
2903 && prev_sc && (prev_sc->function == NULL && prev_sc->symbol == NULL))
2904 {
2905 initial_function = true;
2906 }
2907 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, NULL, function_changed, initial_function);
2908}
2909
2910
Jim Ingham228063c2012-02-21 02:23:08 +00002911void
2912Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2913{
Jim Ingham4f02b222012-02-22 22:49:20 +00002914 // For simplicity's sake, I am not going to deal with how to close down any
2915 // open logging streams, I just redirect everything from here on out to the
2916 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002917 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2918}
2919
2920bool
2921Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2922{
2923 Log::Callbacks log_callbacks;
2924
2925 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002926 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002927 {
2928 log_stream_sp = m_log_callback_stream_sp;
2929 // For now when using the callback mode you always get thread & timestamp.
2930 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2931 }
2932 else if (log_file == NULL || *log_file == '\0')
2933 {
Greg Clayton44d93782014-01-27 23:43:24 +00002934 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00002935 }
2936 else
2937 {
2938 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002939 if (pos != m_log_streams.end())
2940 log_stream_sp = pos->second.lock();
2941 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002942 {
2943 log_stream_sp.reset (new StreamFile (log_file));
2944 m_log_streams[log_file] = log_stream_sp;
2945 }
Jim Ingham228063c2012-02-21 02:23:08 +00002946 }
2947 assert (log_stream_sp.get());
2948
2949 if (log_options == 0)
2950 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2951
Greg Clayton57abc5d2013-05-10 21:47:16 +00002952 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002953 {
2954 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2955 return true;
2956 }
2957 else
2958 {
2959 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2960 if (log_channel_sp)
2961 {
2962 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2963 {
2964 return true;
2965 }
2966 else
2967 {
2968 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2969 return false;
2970 }
2971 }
2972 else
2973 {
2974 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2975 return false;
2976 }
2977 }
2978 return false;
2979}
2980
Greg Clayton9585fbf2013-03-19 00:20:55 +00002981SourceManager &
2982Debugger::GetSourceManager ()
2983{
2984 if (m_source_manager_ap.get() == NULL)
2985 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2986 return *m_source_manager_ap;
2987}
2988
2989
Greg Clayton44d93782014-01-27 23:43:24 +00002990
2991// This function handles events that were broadcast by the process.
2992void
2993Debugger::HandleBreakpointEvent (const EventSP &event_sp)
2994{
2995 using namespace lldb;
2996 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
2997
2998// if (event_type & eBreakpointEventTypeAdded
2999// || event_type & eBreakpointEventTypeRemoved
3000// || event_type & eBreakpointEventTypeEnabled
3001// || event_type & eBreakpointEventTypeDisabled
3002// || event_type & eBreakpointEventTypeCommandChanged
3003// || event_type & eBreakpointEventTypeConditionChanged
3004// || event_type & eBreakpointEventTypeIgnoreChanged
3005// || event_type & eBreakpointEventTypeLocationsResolved)
3006// {
3007// // Don't do anything about these events, since the breakpoint commands already echo these actions.
3008// }
3009//
3010 if (event_type & eBreakpointEventTypeLocationsAdded)
3011 {
3012 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
3013 if (num_new_locations > 0)
3014 {
3015 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
3016 StreamFileSP output_sp (GetOutputFile());
3017 if (output_sp)
3018 {
3019 output_sp->Printf("%d location%s added to breakpoint %d\n",
3020 num_new_locations,
3021 num_new_locations == 1 ? "" : "s",
3022 breakpoint->GetID());
3023 RefreshTopIOHandler();
3024 }
3025 }
3026 }
3027// else if (event_type & eBreakpointEventTypeLocationsRemoved)
3028// {
3029// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
3030// }
3031// else if (event_type & eBreakpointEventTypeLocationsResolved)
3032// {
3033// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
3034// }
3035}
3036
3037size_t
3038Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
3039{
3040 size_t total_bytes = 0;
3041 if (stream == NULL)
3042 stream = GetOutputFile().get();
3043
3044 if (stream)
3045 {
3046 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
3047 if (process == NULL)
3048 {
3049 TargetSP target_sp = GetTargetList().GetSelectedTarget();
3050 if (target_sp)
3051 process = target_sp->GetProcessSP().get();
3052 }
3053 if (process)
3054 {
3055 Error error;
3056 size_t len;
3057 char stdio_buffer[1024];
3058 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3059 {
3060 stream->Write(stdio_buffer, len);
3061 total_bytes += len;
3062 }
3063 }
3064 stream->Flush();
3065 }
3066 return total_bytes;
3067}
3068
3069size_t
3070Debugger::GetProcessSTDERR (Process *process, Stream *stream)
3071{
3072 size_t total_bytes = 0;
3073 if (stream == NULL)
3074 stream = GetOutputFile().get();
3075
3076 if (stream)
3077 {
3078 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
3079 if (process == NULL)
3080 {
3081 TargetSP target_sp = GetTargetList().GetSelectedTarget();
3082 if (target_sp)
3083 process = target_sp->GetProcessSP().get();
3084 }
3085 if (process)
3086 {
3087 Error error;
3088 size_t len;
3089 char stdio_buffer[1024];
3090 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3091 {
3092 stream->Write(stdio_buffer, len);
3093 total_bytes += len;
3094 }
3095 }
3096 stream->Flush();
3097 }
3098 return total_bytes;
3099}
3100
3101// This function handles events that were broadcast by the process.
3102void
3103Debugger::HandleProcessEvent (const EventSP &event_sp)
3104{
3105 using namespace lldb;
3106 const uint32_t event_type = event_sp->GetType();
3107 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
3108
Greg Claytonb4874f12014-02-28 18:22:24 +00003109 StreamString output_stream;
3110 StreamString error_stream;
Greg Clayton44d93782014-01-27 23:43:24 +00003111 const bool gui_enabled = IsForwardingEvents();
Greg Clayton44d93782014-01-27 23:43:24 +00003112
Greg Claytonb4874f12014-02-28 18:22:24 +00003113 if (!gui_enabled)
3114 {
3115 bool pop_process_io_handler = false;
3116 assert (process_sp);
Greg Clayton44d93782014-01-27 23:43:24 +00003117
Greg Claytonb4874f12014-02-28 18:22:24 +00003118 if (event_type & Process::eBroadcastBitSTDOUT || event_type & Process::eBroadcastBitStateChanged)
Greg Clayton44d93782014-01-27 23:43:24 +00003119 {
Greg Claytonb4874f12014-02-28 18:22:24 +00003120 GetProcessSTDOUT (process_sp.get(), &output_stream);
3121 }
3122
3123 if (event_type & Process::eBroadcastBitSTDERR || event_type & Process::eBroadcastBitStateChanged)
3124 {
3125 GetProcessSTDERR (process_sp.get(), &error_stream);
3126 }
3127
3128 if (event_type & Process::eBroadcastBitStateChanged)
3129 {
3130
3131 // Drain all stout and stderr so we don't see any output come after
3132 // we print our prompts
Greg Clayton44d93782014-01-27 23:43:24 +00003133 // Something changed in the process; get the event and report the process's current status and location to
3134 // the user.
3135 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
3136 if (event_state == eStateInvalid)
3137 return;
3138
3139 switch (event_state)
3140 {
3141 case eStateInvalid:
3142 case eStateUnloaded:
3143 case eStateConnected:
3144 case eStateAttaching:
3145 case eStateLaunching:
3146 case eStateStepping:
3147 case eStateDetached:
3148 {
Greg Claytonb4874f12014-02-28 18:22:24 +00003149 output_stream.Printf("Process %" PRIu64 " %s\n",
3150 process_sp->GetID(),
3151 StateAsCString (event_state));
3152
3153 if (event_state == eStateDetached)
3154 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00003155 }
3156 break;
3157
3158 case eStateRunning:
3159 // Don't be chatty when we run...
3160 break;
3161
3162 case eStateExited:
Greg Claytonb4874f12014-02-28 18:22:24 +00003163 process_sp->GetStatus(output_stream);
3164 pop_process_io_handler = true;
Greg Clayton44d93782014-01-27 23:43:24 +00003165 break;
3166
3167 case eStateStopped:
3168 case eStateCrashed:
3169 case eStateSuspended:
3170 // Make sure the program hasn't been auto-restarted:
3171 if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get()))
3172 {
3173 size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get());
3174 if (num_reasons > 0)
3175 {
3176 // FIXME: Do we want to report this, or would that just be annoyingly chatty?
3177 if (num_reasons == 1)
3178 {
3179 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0);
Greg Claytonb4874f12014-02-28 18:22:24 +00003180 output_stream.Printf("Process %" PRIu64 " stopped and restarted: %s\n",
3181 process_sp->GetID(),
3182 reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00003183 }
3184 else
3185 {
Greg Claytonb4874f12014-02-28 18:22:24 +00003186 output_stream.Printf("Process %" PRIu64 " stopped and restarted, reasons:\n",
3187 process_sp->GetID());
Greg Clayton44d93782014-01-27 23:43:24 +00003188
3189
3190 for (size_t i = 0; i < num_reasons; i++)
3191 {
3192 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i);
Greg Claytonb4874f12014-02-28 18:22:24 +00003193 output_stream.Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>");
Greg Clayton44d93782014-01-27 23:43:24 +00003194 }
3195 }
3196 }
3197 }
3198 else
3199 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003200 // 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 +00003201 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003202 ThreadList &thread_list = process_sp->GetThreadList();
3203 Mutex::Locker locker (thread_list.GetMutex());
3204
3205 ThreadSP curr_thread (thread_list.GetSelectedThread());
3206 ThreadSP thread;
3207 StopReason curr_thread_stop_reason = eStopReasonInvalid;
3208 if (curr_thread)
3209 curr_thread_stop_reason = curr_thread->GetStopReason();
3210 if (!curr_thread ||
3211 !curr_thread->IsValid() ||
3212 curr_thread_stop_reason == eStopReasonInvalid ||
3213 curr_thread_stop_reason == eStopReasonNone)
Greg Clayton44d93782014-01-27 23:43:24 +00003214 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003215 // Prefer a thread that has just completed its plan over another thread as current thread.
3216 ThreadSP plan_thread;
3217 ThreadSP other_thread;
3218 const size_t num_threads = thread_list.GetSize();
3219 size_t i;
3220 for (i = 0; i < num_threads; ++i)
Greg Clayton44d93782014-01-27 23:43:24 +00003221 {
Jim Ingham4a65fb12014-03-07 11:20:03 +00003222 thread = thread_list.GetThreadAtIndex(i);
3223 StopReason thread_stop_reason = thread->GetStopReason();
3224 switch (thread_stop_reason)
3225 {
3226 case eStopReasonInvalid:
3227 case eStopReasonNone:
3228 break;
3229
3230 case eStopReasonTrace:
3231 case eStopReasonBreakpoint:
3232 case eStopReasonWatchpoint:
3233 case eStopReasonSignal:
3234 case eStopReasonException:
3235 case eStopReasonExec:
3236 case eStopReasonThreadExiting:
3237 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())
Zachary Turner39de3112014-09-09 20:54:56 +00003487 m_event_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.event-handler", EventHandlerThread, this, NULL);
Zachary Turneracee96a2014-09-23 18:32:09 +00003488 return m_event_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00003489}
3490
3491void
3492Debugger::StopEventHandlerThread()
3493{
Zachary Turneracee96a2014-09-23 18:32:09 +00003494 if (m_event_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00003495 {
3496 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
Zachary Turner39de3112014-09-09 20:54:56 +00003497 m_event_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00003498 }
3499}
3500
3501
3502lldb::thread_result_t
3503Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3504{
3505 Debugger *debugger = (Debugger *)arg;
3506 debugger->ExecuteIOHanders();
3507 debugger->StopEventHandlerThread();
3508 return NULL;
3509}
3510
3511bool
3512Debugger::StartIOHandlerThread()
3513{
Zachary Turneracee96a2014-09-23 18:32:09 +00003514 if (!m_io_handler_thread.IsJoinable())
Zachary Turner39de3112014-09-09 20:54:56 +00003515 m_io_handler_thread = ThreadLauncher::LaunchThread("lldb.debugger.io-handler", IOHandlerThread, this, NULL);
Zachary Turneracee96a2014-09-23 18:32:09 +00003516 return m_io_handler_thread.IsJoinable();
Greg Clayton44d93782014-01-27 23:43:24 +00003517}
3518
3519void
3520Debugger::StopIOHandlerThread()
3521{
Zachary Turneracee96a2014-09-23 18:32:09 +00003522 if (m_io_handler_thread.IsJoinable())
Greg Clayton44d93782014-01-27 23:43:24 +00003523 {
3524 if (m_input_file_sp)
3525 m_input_file_sp->GetFile().Close();
Zachary Turner39de3112014-09-09 20:54:56 +00003526 m_io_handler_thread.Join(nullptr);
Greg Clayton44d93782014-01-27 23:43:24 +00003527 }
3528}
3529
3530