blob: 34e0e329f0923155354be0dbfabc6b1c3eabfeab [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Debugger.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Clayton4a33d312011-06-23 17:59:56 +000012#include "lldb/Core/Debugger.h"
13
14#include <map>
15
Enrico Granata4becb372011-06-29 22:27:15 +000016#include "clang/AST/DeclCXX.h"
17#include "clang/AST/Type.h"
18
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/lldb-private.h"
20#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/Module.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000022#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000023#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000025#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000026#include "lldb/Core/StreamCallback.h"
Greg Clayton44d93782014-01-27 23:43:24 +000027#include "lldb/Core/StreamFile.h"
Greg Clayton1b654882010-09-19 02:33:57 +000028#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000030#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000031#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000032#include "lldb/DataFormatters/DataVisualization.h"
33#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000034#include "lldb/Host/DynamicLibrary.h"
Greg Claytona3406612011-02-07 23:24:47 +000035#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000036#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000037#include "lldb/Interpreter/OptionValueSInt64.h"
38#include "lldb/Interpreter/OptionValueString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000039#include "lldb/Symbol/ClangASTContext.h"
40#include "lldb/Symbol/CompileUnit.h"
41#include "lldb/Symbol/Function.h"
42#include "lldb/Symbol/Symbol.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000043#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "lldb/Target/TargetList.h"
45#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000046#include "lldb/Target/RegisterContext.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000047#include "lldb/Target/SectionLoadList.h"
Greg Clayton1b654882010-09-19 02:33:57 +000048#include "lldb/Target/StopInfo.h"
Enrico Granata84a53df2013-05-20 22:29:23 +000049#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000051#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
53using namespace lldb;
54using namespace lldb_private;
55
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Greg Clayton1b654882010-09-19 02:33:57 +000057static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000058static lldb::user_id_t g_unique_id = 1;
59
Greg Clayton1b654882010-09-19 02:33:57 +000060#pragma mark Static Functions
61
62static Mutex &
63GetDebuggerListMutex ()
64{
65 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
66 return g_mutex;
67}
68
69typedef std::vector<DebuggerSP> DebuggerList;
70
71static DebuggerList &
72GetDebuggerList()
73{
74 // hide the static debugger list inside a singleton accessor to avoid
75 // global init contructors
76 static DebuggerList g_list;
77 return g_list;
78}
Greg Claytone372b982011-11-21 21:44:34 +000079
80OptionEnumValueElement
Greg Clayton67cc0632012-08-22 17:17:09 +000081g_show_disassembly_enum_values[] =
Greg Claytone372b982011-11-21 21:44:34 +000082{
Greg Clayton67cc0632012-08-22 17:17:09 +000083 { Debugger::eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
84 { Debugger::eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
85 { Debugger::eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
Greg Claytone372b982011-11-21 21:44:34 +000086 { 0, NULL, NULL }
87};
88
Greg Clayton67cc0632012-08-22 17:17:09 +000089OptionEnumValueElement
90g_language_enumerators[] =
91{
92 { eScriptLanguageNone, "none", "Disable scripting languages."},
93 { eScriptLanguagePython, "python", "Select python as the default scripting language."},
94 { eScriptLanguageDefault, "default", "Select the lldb default as the default scripting language."},
Greg Claytona12993c2012-09-13 23:03:20 +000095 { 0, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +000096};
Greg Claytone372b982011-11-21 21:44:34 +000097
Greg Clayton67cc0632012-08-22 17:17:09 +000098#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name-with-args}${function.pc-offset}}}"
99#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
100
Michael Sartain0769b2b2013-07-30 16:44:36 +0000101#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id%tid}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000102 "{, ${frame.pc}}"\
103 MODULE_WITH_FUNC\
104 FILE_AND_LINE\
Michael Sartain0769b2b2013-07-30 16:44:36 +0000105 "{, name = '${thread.name}'}"\
106 "{, queue = '${thread.queue}'}"\
Greg Clayton67cc0632012-08-22 17:17:09 +0000107 "{, stop reason = ${thread.stop-reason}}"\
108 "{\\nReturn value: ${thread.return-value}}"\
109 "\\n"
110
111#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
112 MODULE_WITH_FUNC\
113 FILE_AND_LINE\
114 "\\n"
115
116
117
Greg Clayton754a9362012-08-23 00:22:02 +0000118static PropertyDefinition
119g_properties[] =
Greg Clayton67cc0632012-08-22 17:17:09 +0000120{
121{ "auto-confirm", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true all confirmation prompts will receive their default reply." },
122{ "frame-format", OptionValue::eTypeString , true, 0 , DEFAULT_FRAME_FORMAT, NULL, "The default frame format string to use when displaying stack frame information for threads." },
123{ "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 +0000124{ "prompt", OptionValue::eTypeString , true, OptionValueString::eOptionEncodeCharacterEscapeSequences, "(lldb) ", NULL, "The debugger command line prompt displayed for the user." },
Greg Clayton67cc0632012-08-22 17:17:09 +0000125{ "script-lang", OptionValue::eTypeEnum , true, eScriptLanguagePython, NULL, g_language_enumerators, "The script language to be used for evaluating user-written scripts." },
126{ "stop-disassembly-count", OptionValue::eTypeSInt64 , true, 4 , NULL, NULL, "The number of disassembly lines to show when displaying a stopped context." },
127{ "stop-disassembly-display", OptionValue::eTypeEnum , true, Debugger::eStopDisassemblyTypeNoSource, NULL, g_show_disassembly_enum_values, "Control when to display disassembly when displaying a stopped context." },
128{ "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." },
129{ "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." },
130{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
131{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
132{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000133{ "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
Enrico Granata90a8db32013-10-31 21:01:07 +0000134{ "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 +0000135
136 { NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
Greg Clayton67cc0632012-08-22 17:17:09 +0000137};
138
139enum
140{
141 ePropertyAutoConfirm = 0,
142 ePropertyFrameFormat,
143 ePropertyNotiftVoid,
144 ePropertyPrompt,
145 ePropertyScriptLanguage,
146 ePropertyStopDisassemblyCount,
147 ePropertyStopDisassemblyDisplay,
148 ePropertyStopLineCountAfter,
149 ePropertyStopLineCountBefore,
150 ePropertyTerminalWidth,
151 ePropertyThreadFormat,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000152 ePropertyUseExternalEditor,
153 ePropertyUseColor,
Enrico Granata90a8db32013-10-31 21:01:07 +0000154 ePropertyAutoOneLineSummaries
Greg Clayton67cc0632012-08-22 17:17:09 +0000155};
156
Greg Clayton5fb8f792013-12-02 19:35:49 +0000157Debugger::LoadPluginCallbackType Debugger::g_load_plugin_callback = NULL;
Greg Clayton4c054102012-09-01 00:38:36 +0000158
159Error
160Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
161 VarSetOperationType op,
162 const char *property_path,
163 const char *value)
164{
Enrico Granata84a53df2013-05-20 22:29:23 +0000165 bool is_load_script = strcmp(property_path,"target.load-script-from-symbol-file") == 0;
166 TargetSP target_sp;
Enrico Granata397ddd52013-05-21 20:13:34 +0000167 LoadScriptFromSymFile load_script_old_value;
Enrico Granata84a53df2013-05-20 22:29:23 +0000168 if (is_load_script && exe_ctx->GetTargetSP())
169 {
170 target_sp = exe_ctx->GetTargetSP();
171 load_script_old_value = target_sp->TargetProperties::GetLoadScriptFromSymbolFile();
172 }
Greg Clayton4c054102012-09-01 00:38:36 +0000173 Error error (Properties::SetPropertyValue (exe_ctx, op, property_path, value));
174 if (error.Success())
175 {
Enrico Granata84a53df2013-05-20 22:29:23 +0000176 // FIXME it would be nice to have "on-change" callbacks for properties
Greg Clayton4c054102012-09-01 00:38:36 +0000177 if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
178 {
179 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000180 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
181 if (str.length())
182 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000183 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton4c054102012-09-01 00:38:36 +0000184 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
185 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
186 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000187 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
188 {
189 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
190 SetPrompt (GetPrompt());
191 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000192 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000193 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000194 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000195 {
196 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000197 StreamString feedback_stream;
198 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000199 {
Greg Clayton44d93782014-01-27 23:43:24 +0000200 StreamFileSP stream_sp (GetErrorFile());
201 if (stream_sp)
Enrico Granata84a53df2013-05-20 22:29:23 +0000202 {
Greg Clayton44d93782014-01-27 23:43:24 +0000203 for (auto error : errors)
204 {
205 stream_sp->Printf("%s\n",error.AsCString());
206 }
207 if (feedback_stream.GetSize())
208 stream_sp->Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000209 }
210 }
211 }
212 }
Greg Clayton4c054102012-09-01 00:38:36 +0000213 }
214 return error;
215}
216
Greg Clayton67cc0632012-08-22 17:17:09 +0000217bool
218Debugger::GetAutoConfirm () const
219{
220 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000221 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000222}
223
224const char *
225Debugger::GetFrameFormat() const
226{
227 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000228 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000229}
230
231bool
232Debugger::GetNotifyVoid () const
233{
234 const uint32_t idx = ePropertyNotiftVoid;
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 *
239Debugger::GetPrompt() const
240{
241 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000242 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000243}
244
245void
246Debugger::SetPrompt(const char *p)
247{
248 const uint32_t idx = ePropertyPrompt;
249 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
250 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000251 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
252 if (str.length())
253 new_prompt = str.c_str();
Greg Clayton44d93782014-01-27 23:43:24 +0000254 GetCommandInterpreter().UpdatePrompt(new_prompt);
Greg Clayton67cc0632012-08-22 17:17:09 +0000255}
256
257const char *
258Debugger::GetThreadFormat() const
259{
260 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000261 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000262}
263
264lldb::ScriptLanguage
265Debugger::GetScriptLanguage() const
266{
267 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000268 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000269}
270
271bool
272Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
273{
274 const uint32_t idx = ePropertyScriptLanguage;
275 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
276}
277
278uint32_t
279Debugger::GetTerminalWidth () const
280{
281 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000282 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000283}
284
285bool
286Debugger::SetTerminalWidth (uint32_t term_width)
287{
288 const uint32_t idx = ePropertyTerminalWidth;
289 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
290}
291
292bool
293Debugger::GetUseExternalEditor () const
294{
295 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000296 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000297}
298
299bool
300Debugger::SetUseExternalEditor (bool b)
301{
302 const uint32_t idx = ePropertyUseExternalEditor;
303 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
304}
305
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000306bool
307Debugger::GetUseColor () const
308{
309 const uint32_t idx = ePropertyUseColor;
310 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
311}
312
313bool
314Debugger::SetUseColor (bool b)
315{
316 const uint32_t idx = ePropertyUseColor;
317 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
318 SetPrompt (GetPrompt());
319 return ret;
320}
321
Greg Clayton67cc0632012-08-22 17:17:09 +0000322uint32_t
323Debugger::GetStopSourceLineCount (bool before) const
324{
325 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000326 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000327}
328
329Debugger::StopDisassemblyType
330Debugger::GetStopDisassemblyDisplay () const
331{
332 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000333 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000334}
335
336uint32_t
337Debugger::GetDisassemblyLineCount () const
338{
339 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000340 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000341}
Greg Claytone372b982011-11-21 21:44:34 +0000342
Enrico Granata553fad52013-10-25 23:09:40 +0000343bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000344Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000345{
Enrico Granata90a8db32013-10-31 21:01:07 +0000346 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000347 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
348
349}
350
Greg Clayton1b654882010-09-19 02:33:57 +0000351#pragma mark Debugger
352
Greg Clayton67cc0632012-08-22 17:17:09 +0000353//const DebuggerPropertiesSP &
354//Debugger::GetSettings() const
355//{
356// return m_properties_sp;
357//}
358//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000359
Caroline Tice2f88aad2011-01-14 00:29:16 +0000360int
361Debugger::TestDebuggerRefCount ()
362{
363 return g_shared_debugger_refcount;
364}
365
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366void
Greg Clayton5fb8f792013-12-02 19:35:49 +0000367Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000369 g_load_plugin_callback = load_plugin_callback;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000370 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000371 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372}
373
374void
375Debugger::Terminate ()
376{
Greg Clayton66111032010-06-23 01:19:29 +0000377 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 {
Greg Clayton66111032010-06-23 01:19:29 +0000379 g_shared_debugger_refcount--;
380 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 {
Greg Claytondbe54502010-11-19 03:46:01 +0000382 lldb_private::WillTerminate();
383 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000384
385 // Clear our master list of debugger objects
386 Mutex::Locker locker (GetDebuggerListMutex ());
387 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 }
390}
391
Caroline Tice20bd37f2011-03-10 22:14:10 +0000392void
393Debugger::SettingsInitialize ()
394{
Greg Clayton6920b522012-08-22 18:39:03 +0000395 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000396}
397
398void
399Debugger::SettingsTerminate ()
400{
Greg Clayton6920b522012-08-22 18:39:03 +0000401 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000402}
403
Enrico Granata21dfcd92012-09-28 23:57:51 +0000404bool
Enrico Granatae743c782013-04-24 21:29:08 +0000405Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000406{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000407 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000408 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000409 lldb::DynamicLibrarySP dynlib_sp = g_load_plugin_callback (shared_from_this(), spec, error);
410 if (dynlib_sp)
411 {
412 m_loaded_plugins.push_back(dynlib_sp);
413 return true;
414 }
Enrico Granatae743c782013-04-24 21:29:08 +0000415 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000416 else
Enrico Granatae743c782013-04-24 21:29:08 +0000417 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000418 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
419 // and if the public API layer isn't available (code is linking against
420 // all of the internal LLDB static libraries), then we can't load plugins
421 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000422 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000423 return false;
424}
425
426static FileSpec::EnumerateDirectoryResult
427LoadPluginCallback
428(
429 void *baton,
430 FileSpec::FileType file_type,
431 const FileSpec &file_spec
432 )
433{
434 Error error;
435
436 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000437 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000438
439 if (!baton)
440 return FileSpec::eEnumerateDirectoryResultQuit;
441
442 Debugger *debugger = (Debugger*)baton;
443
444 // If we have a regular file, a symbolic link or unknown file type, try
445 // and process the file. We must handle unknown as sometimes the directory
446 // enumeration might be enumerating a file system that doesn't have correct
447 // file type information.
448 if (file_type == FileSpec::eFileTypeRegular ||
449 file_type == FileSpec::eFileTypeSymbolicLink ||
450 file_type == FileSpec::eFileTypeUnknown )
451 {
452 FileSpec plugin_file_spec (file_spec);
453 plugin_file_spec.ResolvePath ();
454
Michael Sartain3cf443d2013-07-17 00:26:30 +0000455 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
456 plugin_file_spec.GetFileNameExtension() != g_solibext)
457 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000458 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000459 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000460
Enrico Granatae743c782013-04-24 21:29:08 +0000461 Error plugin_load_error;
462 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000463
464 return FileSpec::eEnumerateDirectoryResultNext;
465 }
466
467 else if (file_type == FileSpec::eFileTypeUnknown ||
468 file_type == FileSpec::eFileTypeDirectory ||
469 file_type == FileSpec::eFileTypeSymbolicLink )
470 {
471 // Try and recurse into anything that a directory or symbolic link.
472 // We must also do this for unknown as sometimes the directory enumeration
473 // might be enurating a file system that doesn't have correct file type
474 // information.
475 return FileSpec::eEnumerateDirectoryResultEnter;
476 }
477
478 return FileSpec::eEnumerateDirectoryResultNext;
479}
480
481void
482Debugger::InstanceInitialize ()
483{
484 FileSpec dir_spec;
485 const bool find_directories = true;
486 const bool find_files = true;
487 const bool find_other = true;
488 char dir_path[PATH_MAX];
489 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
490 {
491 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
492 {
493 FileSpec::EnumerateDirectory (dir_path,
494 find_directories,
495 find_files,
496 find_other,
497 LoadPluginCallback,
498 this);
499 }
500 }
501
502 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
503 {
504 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
505 {
506 FileSpec::EnumerateDirectory (dir_path,
507 find_directories,
508 find_files,
509 find_other,
510 LoadPluginCallback,
511 this);
512 }
513 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000514
515 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000516}
517
Greg Clayton66111032010-06-23 01:19:29 +0000518DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000519Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000520{
Jim Ingham228063c2012-02-21 02:23:08 +0000521 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000522 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000523 {
524 Mutex::Locker locker (GetDebuggerListMutex ());
525 GetDebuggerList().push_back(debugger_sp);
526 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000527 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000528 return debugger_sp;
529}
530
Caroline Ticee02657b2011-01-22 01:02:07 +0000531void
Greg Clayton4d122c42011-09-17 08:33:22 +0000532Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000533{
534 if (debugger_sp.get() == NULL)
535 return;
536
Jim Ingham8314c522011-09-15 21:36:42 +0000537 debugger_sp->Clear();
538
Greg Claytonc15f55e2012-03-30 20:53:46 +0000539 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000540 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000541 Mutex::Locker locker (GetDebuggerListMutex ());
542 DebuggerList &debugger_list = GetDebuggerList ();
543 DebuggerList::iterator pos, end = debugger_list.end();
544 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000545 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000546 if ((*pos).get() == debugger_sp.get())
547 {
548 debugger_list.erase (pos);
549 return;
550 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000551 }
552 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000553}
554
Greg Clayton4d122c42011-09-17 08:33:22 +0000555DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000556Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
557{
Greg Clayton4d122c42011-09-17 08:33:22 +0000558 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000559 if (g_shared_debugger_refcount > 0)
560 {
561 Mutex::Locker locker (GetDebuggerListMutex ());
562 DebuggerList &debugger_list = GetDebuggerList();
563 DebuggerList::iterator pos, end = debugger_list.end();
564
565 for (pos = debugger_list.begin(); pos != end; ++pos)
566 {
567 if ((*pos).get()->m_instance_name == instance_name)
568 {
569 debugger_sp = *pos;
570 break;
571 }
572 }
573 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000574 return debugger_sp;
575}
Greg Clayton66111032010-06-23 01:19:29 +0000576
577TargetSP
578Debugger::FindTargetWithProcessID (lldb::pid_t pid)
579{
Greg Clayton4d122c42011-09-17 08:33:22 +0000580 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000581 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000582 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000583 Mutex::Locker locker (GetDebuggerListMutex ());
584 DebuggerList &debugger_list = GetDebuggerList();
585 DebuggerList::iterator pos, end = debugger_list.end();
586 for (pos = debugger_list.begin(); pos != end; ++pos)
587 {
588 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
589 if (target_sp)
590 break;
591 }
Greg Clayton66111032010-06-23 01:19:29 +0000592 }
593 return target_sp;
594}
595
Greg Claytone4e45922011-11-16 05:37:56 +0000596TargetSP
597Debugger::FindTargetWithProcess (Process *process)
598{
599 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000600 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000601 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000602 Mutex::Locker locker (GetDebuggerListMutex ());
603 DebuggerList &debugger_list = GetDebuggerList();
604 DebuggerList::iterator pos, end = debugger_list.end();
605 for (pos = debugger_list.begin(); pos != end; ++pos)
606 {
607 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
608 if (target_sp)
609 break;
610 }
Greg Claytone4e45922011-11-16 05:37:56 +0000611 }
612 return target_sp;
613}
614
Jim Ingham228063c2012-02-21 02:23:08 +0000615Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000616 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000617 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Clayton44d93782014-01-27 23:43:24 +0000618 m_input_file_sp (new StreamFile (stdin, false)),
619 m_output_file_sp (new StreamFile (stdout, false)),
620 m_error_file_sp (new StreamFile (stderr, false)),
Jim Inghamc5917d92012-11-30 20:23:19 +0000621 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000622 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000623 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624 m_listener ("lldb.Debugger"),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000625 m_source_manager_ap(),
Jim Inghame37d6052011-09-13 00:29:56 +0000626 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000627 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000628 m_input_reader_stack (),
Greg Clayton44d93782014-01-27 23:43:24 +0000629 m_instance_name (),
630 m_loaded_plugins (),
631 m_event_handler_thread (LLDB_INVALID_HOST_THREAD),
632 m_io_handler_thread (LLDB_INVALID_HOST_THREAD),
633 m_event_handler_thread_alive(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634{
Greg Clayton67cc0632012-08-22 17:17:09 +0000635 char instance_cstr[256];
636 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
637 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000638 if (log_callback)
639 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000640 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000641 // Always add our default platform to the platform list
642 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
643 assert (default_platform_sp.get());
644 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000645
Greg Clayton754a9362012-08-23 00:22:02 +0000646 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000647 m_collection_sp->AppendProperty (ConstString("target"),
648 ConstString("Settings specify to debugging targets."),
649 true,
650 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000651 if (m_command_interpreter_ap.get())
652 {
653 m_collection_sp->AppendProperty (ConstString("interpreter"),
654 ConstString("Settings specify to the debugger's command interpreter."),
655 true,
656 m_command_interpreter_ap->GetValueProperties());
657 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000658 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
659 term_width->SetMinimumValue(10);
660 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000661
662 // Turn off use-color if this is a dumb terminal.
663 const char *term = getenv ("TERM");
664 if (term && !strcmp (term, "dumb"))
665 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666}
667
668Debugger::~Debugger ()
669{
Jim Ingham8314c522011-09-15 21:36:42 +0000670 Clear();
671}
672
673void
674Debugger::Clear()
675{
Greg Clayton44d93782014-01-27 23:43:24 +0000676 ClearIOHandlers();
677 StopIOHandlerThread();
678 StopEventHandlerThread();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000679 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000680 int num_targets = m_target_list.GetNumTargets();
681 for (int i = 0; i < num_targets; i++)
682 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000683 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
684 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000685 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000686 ProcessSP process_sp (target_sp->GetProcessSP());
687 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000688 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000689 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000690 }
Greg Clayton66111032010-06-23 01:19:29 +0000691 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000692 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000693
694 // Close the input file _before_ we close the input read communications class
695 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000696 m_terminal_state.Clear();
Greg Clayton44d93782014-01-27 23:43:24 +0000697 if (m_input_file_sp)
698 m_input_file_sp->GetFile().Close ();
Jim Ingham8314c522011-09-15 21:36:42 +0000699}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700
701bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000702Debugger::GetCloseInputOnEOF () const
703{
Greg Clayton44d93782014-01-27 23:43:24 +0000704// return m_input_comm.GetCloseOnEOF();
705 return false;
Greg Claytonfc3f0272011-05-29 04:06:55 +0000706}
707
708void
709Debugger::SetCloseInputOnEOF (bool b)
710{
Greg Clayton44d93782014-01-27 23:43:24 +0000711// m_input_comm.SetCloseOnEOF(b);
Greg Claytonfc3f0272011-05-29 04:06:55 +0000712}
713
714bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715Debugger::GetAsyncExecution ()
716{
Greg Clayton66111032010-06-23 01:19:29 +0000717 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718}
719
720void
721Debugger::SetAsyncExecution (bool async_execution)
722{
Greg Clayton66111032010-06-23 01:19:29 +0000723 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724}
725
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726
727void
728Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
729{
Greg Clayton44d93782014-01-27 23:43:24 +0000730 if (m_input_file_sp)
731 m_input_file_sp->GetFile().SetStream (fh, tranfer_ownership);
732 else
733 m_input_file_sp.reset (new StreamFile (fh, tranfer_ownership));
734
735 File &in_file = m_input_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000736 if (in_file.IsValid() == false)
737 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738
Jim Inghamc5917d92012-11-30 20:23:19 +0000739 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
740 SaveInputTerminalState ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741}
742
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743void
744Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
745{
Greg Clayton44d93782014-01-27 23:43:24 +0000746 if (m_output_file_sp)
747 m_output_file_sp->GetFile().SetStream (fh, tranfer_ownership);
748 else
749 m_output_file_sp.reset (new StreamFile (fh, tranfer_ownership));
750
751 File &out_file = m_output_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000752 if (out_file.IsValid() == false)
753 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000754
Enrico Granatab5887262012-10-29 21:18:03 +0000755 // do not create the ScriptInterpreter just for setting the output file handle
756 // as the constructor will know how to do the right thing on its own
757 const bool can_create = false;
758 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
759 if (script_interpreter)
760 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761}
762
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763void
764Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
765{
Greg Clayton44d93782014-01-27 23:43:24 +0000766 if (m_error_file_sp)
767 m_error_file_sp->GetFile().SetStream (fh, tranfer_ownership);
768 else
769 m_error_file_sp.reset (new StreamFile (fh, tranfer_ownership));
770
771 File &err_file = m_error_file_sp->GetFile();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000772 if (err_file.IsValid() == false)
773 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774}
775
Jim Inghamc5917d92012-11-30 20:23:19 +0000776void
777Debugger::SaveInputTerminalState ()
778{
Greg Clayton44d93782014-01-27 23:43:24 +0000779 if (m_input_file_sp)
780 {
781 File &in_file = m_input_file_sp->GetFile();
782 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
783 m_terminal_state.Save(in_file.GetDescriptor(), true);
784 }
Jim Inghamc5917d92012-11-30 20:23:19 +0000785}
786
787void
788Debugger::RestoreInputTerminalState ()
789{
790 m_terminal_state.Restore();
791}
792
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000794Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795{
796 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000797 TargetSP target_sp(GetSelectedTarget());
798 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799
800 if (target_sp)
801 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000802 ProcessSP process_sp (target_sp->GetProcessSP());
803 exe_ctx.SetProcessSP (process_sp);
804 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000806 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
807 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000809 exe_ctx.SetThreadSP (thread_sp);
810 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
811 if (exe_ctx.GetFramePtr() == NULL)
812 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 }
814 }
815 }
816 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817}
818
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819void
Caroline Ticeefed6132010-11-19 20:47:54 +0000820Debugger::DispatchInputInterrupt ()
821{
Greg Clayton44d93782014-01-27 23:43:24 +0000822 Mutex::Locker locker (m_input_reader_stack.GetMutex());
823 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000824 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000825 reader_sp->Interrupt();
Caroline Ticeefed6132010-11-19 20:47:54 +0000826}
827
828void
829Debugger::DispatchInputEndOfFile ()
830{
Greg Clayton44d93782014-01-27 23:43:24 +0000831 Mutex::Locker locker (m_input_reader_stack.GetMutex());
832 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000833 if (reader_sp)
Greg Clayton44d93782014-01-27 23:43:24 +0000834 reader_sp->GotEOF();
Caroline Ticeefed6132010-11-19 20:47:54 +0000835}
836
837void
Greg Clayton44d93782014-01-27 23:43:24 +0000838Debugger::ClearIOHandlers ()
Caroline Tice3d6086f2010-12-20 18:35:50 +0000839{
Caroline Ticeb44880c2011-02-10 01:15:13 +0000840 // The bottom input reader should be the main debugger input reader. We do not want to close that one here.
Greg Clayton44d93782014-01-27 23:43:24 +0000841 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000842 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000843 {
Greg Clayton44d93782014-01-27 23:43:24 +0000844 IOHandlerSP reader_sp (m_input_reader_stack.Top());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000845 if (reader_sp)
846 {
Greg Clayton44d93782014-01-27 23:43:24 +0000847 m_input_reader_stack.Pop();
848 reader_sp->SetIsDone(true);
Greg Claytone68f5d62014-02-24 22:50:57 +0000849 reader_sp->Cancel();
Caroline Tice3d6086f2010-12-20 18:35:50 +0000850 }
851 }
852}
853
854void
Greg Clayton44d93782014-01-27 23:43:24 +0000855Debugger::ExecuteIOHanders()
Caroline Tice969ed3d2011-05-02 20:41:46 +0000856{
Caroline Tice9088b062011-05-09 23:06:58 +0000857
Greg Clayton44d93782014-01-27 23:43:24 +0000858 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 {
Greg Clayton44d93782014-01-27 23:43:24 +0000860 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 if (!reader_sp)
862 break;
863
Greg Clayton44d93782014-01-27 23:43:24 +0000864 reader_sp->Activate();
865 reader_sp->Run();
866 reader_sp->Deactivate();
867
868 // Remove all input readers that are done from the top of the stack
869 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870 {
Greg Clayton44d93782014-01-27 23:43:24 +0000871 IOHandlerSP top_reader_sp = m_input_reader_stack.Top();
872 if (top_reader_sp && top_reader_sp->GetIsDone())
873 m_input_reader_stack.Pop();
874 else
875 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876 }
877 }
Greg Clayton44d93782014-01-27 23:43:24 +0000878 ClearIOHandlers();
879}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880
Greg Clayton44d93782014-01-27 23:43:24 +0000881bool
882Debugger::IsTopIOHandler (const lldb::IOHandlerSP& reader_sp)
883{
884 return m_input_reader_stack.IsTop (reader_sp);
885}
886
887
888ConstString
889Debugger::GetTopIOHandlerControlSequence(char ch)
890{
891 return m_input_reader_stack.GetTopIOHandlerControlSequence (ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892}
893
894void
Greg Clayton44d93782014-01-27 23:43:24 +0000895Debugger::RunIOHandler (const IOHandlerSP& reader_sp)
896{
897 Mutex::Locker locker (m_input_reader_stack.GetMutex());
898 PushIOHandler (reader_sp);
899 reader_sp->Activate();
900 reader_sp->Run();
901 PopIOHandler (reader_sp);
902}
903
904void
905Debugger::AdoptTopIOHandlerFilesIfInvalid (StreamFileSP &in, StreamFileSP &out, StreamFileSP &err)
906{
907 // Before an IOHandler runs, it must have in/out/err streams.
908 // This function is called when one ore more of the streams
909 // are NULL. We use the top input reader's in/out/err streams,
910 // or fall back to the debugger file handles, or we fall back
911 // onto stdin/stdout/stderr as a last resort.
912
913 Mutex::Locker locker (m_input_reader_stack.GetMutex());
914 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
915 // If no STDIN has been set, then set it appropriately
916 if (!in)
917 {
918 if (top_reader_sp)
919 in = top_reader_sp->GetInputStreamFile();
920 else
921 in = GetInputFile();
922
923 // If there is nothing, use stdin
924 if (!in)
925 in = StreamFileSP(new StreamFile(stdin, false));
926 }
927 // If no STDOUT has been set, then set it appropriately
928 if (!out)
929 {
930 if (top_reader_sp)
931 out = top_reader_sp->GetOutputStreamFile();
932 else
933 out = GetOutputFile();
934
935 // If there is nothing, use stdout
936 if (!out)
937 out = StreamFileSP(new StreamFile(stdout, false));
938 }
939 // If no STDERR has been set, then set it appropriately
940 if (!err)
941 {
942 if (top_reader_sp)
943 err = top_reader_sp->GetErrorStreamFile();
944 else
945 err = GetErrorFile();
946
947 // If there is nothing, use stderr
948 if (!err)
949 err = StreamFileSP(new StreamFile(stdout, false));
950
951 }
952}
953
954void
955Debugger::PushIOHandler (const IOHandlerSP& reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956{
957 if (!reader_sp)
958 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000959
Greg Clayton44d93782014-01-27 23:43:24 +0000960 // Got the current top input reader...
961 IOHandlerSP top_reader_sp (m_input_reader_stack.Top());
Caroline Ticeb44880c2011-02-10 01:15:13 +0000962
Greg Clayton44d93782014-01-27 23:43:24 +0000963 // Push our new input reader
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000964 m_input_reader_stack.Push (reader_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000965
966 // Interrupt the top input reader to it will exit its Run() function
967 // and let this new input reader take over
968 if (top_reader_sp)
969 top_reader_sp->Deactivate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000970}
971
972bool
Greg Clayton44d93782014-01-27 23:43:24 +0000973Debugger::PopIOHandler (const IOHandlerSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974{
975 bool result = false;
Greg Clayton44d93782014-01-27 23:43:24 +0000976
977 Mutex::Locker locker (m_input_reader_stack.GetMutex());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000978
979 // The reader on the stop of the stack is done, so let the next
980 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000981 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982 {
Greg Clayton44d93782014-01-27 23:43:24 +0000983 IOHandlerSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984
985 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
986 {
Greg Clayton44d93782014-01-27 23:43:24 +0000987 reader_sp->Deactivate();
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000988 m_input_reader_stack.Pop ();
Greg Clayton44d93782014-01-27 23:43:24 +0000989
990 reader_sp = m_input_reader_stack.Top();
991 if (reader_sp)
992 reader_sp->Activate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993
Greg Clayton44d93782014-01-27 23:43:24 +0000994 result = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995 }
996 }
997 return result;
998}
999
1000bool
Greg Clayton44d93782014-01-27 23:43:24 +00001001Debugger::HideTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002{
Greg Clayton44d93782014-01-27 23:43:24 +00001003 Mutex::Locker locker;
1004
1005 if (locker.TryLock(m_input_reader_stack.GetMutex()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001006 {
Greg Clayton44d93782014-01-27 23:43:24 +00001007 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1008 if (reader_sp)
1009 reader_sp->Hide();
1010 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011 }
Greg Clayton44d93782014-01-27 23:43:24 +00001012 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013}
1014
1015void
Greg Clayton44d93782014-01-27 23:43:24 +00001016Debugger::RefreshTopIOHandler()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017{
Greg Clayton44d93782014-01-27 23:43:24 +00001018 IOHandlerSP reader_sp(m_input_reader_stack.Top());
1019 if (reader_sp)
1020 reader_sp->Refresh();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021}
Greg Clayton66111032010-06-23 01:19:29 +00001022
Greg Clayton44d93782014-01-27 23:43:24 +00001023
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001024StreamSP
1025Debugger::GetAsyncOutputStream ()
1026{
1027 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1028 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1029}
1030
1031StreamSP
1032Debugger::GetAsyncErrorStream ()
1033{
1034 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1035 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1036}
1037
Greg Claytonc7bece562013-01-25 18:06:21 +00001038size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001039Debugger::GetNumDebuggers()
1040{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001041 if (g_shared_debugger_refcount > 0)
1042 {
1043 Mutex::Locker locker (GetDebuggerListMutex ());
1044 return GetDebuggerList().size();
1045 }
1046 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001047}
1048
1049lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001050Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001051{
1052 DebuggerSP debugger_sp;
1053
Greg Claytonc15f55e2012-03-30 20:53:46 +00001054 if (g_shared_debugger_refcount > 0)
1055 {
1056 Mutex::Locker locker (GetDebuggerListMutex ());
1057 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001058
Greg Claytonc15f55e2012-03-30 20:53:46 +00001059 if (index < debugger_list.size())
1060 debugger_sp = debugger_list[index];
1061 }
1062
Enrico Granata061858c2012-02-15 02:34:21 +00001063 return debugger_sp;
1064}
1065
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001066DebuggerSP
1067Debugger::FindDebuggerWithID (lldb::user_id_t id)
1068{
Greg Clayton4d122c42011-09-17 08:33:22 +00001069 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001070
Greg Claytonc15f55e2012-03-30 20:53:46 +00001071 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001072 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001073 Mutex::Locker locker (GetDebuggerListMutex ());
1074 DebuggerList &debugger_list = GetDebuggerList();
1075 DebuggerList::iterator pos, end = debugger_list.end();
1076 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001077 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001078 if ((*pos).get()->GetID() == id)
1079 {
1080 debugger_sp = *pos;
1081 break;
1082 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001083 }
1084 }
1085 return debugger_sp;
1086}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001087
Greg Clayton1b654882010-09-19 02:33:57 +00001088static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001089TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001090{
1091 if (frame == NULL)
1092 return;
1093
1094 StreamString s;
1095 const char *prompt_format =
1096 "{addr = '${addr}'\n}"
1097 "{process.id = '${process.id}'\n}"
1098 "{process.name = '${process.name}'\n}"
1099 "{process.file.basename = '${process.file.basename}'\n}"
1100 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1101 "{thread.id = '${thread.id}'\n}"
1102 "{thread.index = '${thread.index}'\n}"
1103 "{thread.name = '${thread.name}'\n}"
1104 "{thread.queue = '${thread.queue}'\n}"
1105 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1106 "{target.arch = '${target.arch}'\n}"
1107 "{module.file.basename = '${module.file.basename}'\n}"
1108 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1109 "{file.basename = '${file.basename}'\n}"
1110 "{file.fullpath = '${file.fullpath}'\n}"
1111 "{frame.index = '${frame.index}'\n}"
1112 "{frame.pc = '${frame.pc}'\n}"
1113 "{frame.sp = '${frame.sp}'\n}"
1114 "{frame.fp = '${frame.fp}'\n}"
1115 "{frame.flags = '${frame.flags}'\n}"
1116 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1117 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1118 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1119 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1120 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1121 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1122 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1123 "{function.id = '${function.id}'\n}"
1124 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001125 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001126 "{function.addr-offset = '${function.addr-offset}'\n}"
1127 "{function.line-offset = '${function.line-offset}'\n}"
1128 "{function.pc-offset = '${function.pc-offset}'\n}"
1129 "{line.file.basename = '${line.file.basename}'\n}"
1130 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1131 "{line.number = '${line.number}'\n}"
1132 "{line.start-addr = '${line.start-addr}'\n}"
1133 "{line.end-addr = '${line.end-addr}'\n}"
1134;
1135
1136 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1137 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001138 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001139 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001140 {
1141 printf("%s\n", s.GetData());
1142 }
1143 else
1144 {
Greg Clayton1b654882010-09-19 02:33:57 +00001145 printf ("what we got: %s\n", s.GetData());
1146 }
1147}
1148
Enrico Granata9fc19442011-07-06 02:13:41 +00001149static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001150ScanFormatDescriptor (const char* var_name_begin,
1151 const char* var_name_end,
1152 const char** var_name_final,
1153 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001154 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001155 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001156{
Greg Clayton5160ce52013-03-27 23:08:40 +00001157 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001158 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001159 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001160 {
1161 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001162 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001163 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001164 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001165 else
1166 {
1167 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001168 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001169 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001170 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001171 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001172 true,
1173 *custom_format) )
1174 {
Enrico Granatae992a082011-07-22 17:03:19 +00001175 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001176 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001177
1178 switch (format_name.front())
1179 {
1180 case '@': // if this is an @ sign, print ObjC description
1181 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1182 break;
1183 case 'V': // if this is a V, print the value using the default format
1184 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1185 break;
1186 case 'L': // if this is an L, print the location of the value
1187 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1188 break;
1189 case 'S': // if this is an S, print the summary after all
1190 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1191 break;
1192 case '#': // if this is a '#', print the number of children
1193 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1194 break;
1195 case 'T': // if this is a 'T', print the type
1196 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1197 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001198 case 'N': // if this is a 'N', print the name
1199 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1200 break;
1201 case '>': // if this is a '>', print the name
1202 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1203 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001204 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001205 if (log)
1206 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001207 break;
1208 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001209 }
1210 // a good custom format tells us to print the value using it
1211 else
Enrico Granatae992a082011-07-22 17:03:19 +00001212 {
1213 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001214 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001215 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001216 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001217 }
Enrico Granatae992a082011-07-22 17:03:19 +00001218 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001219 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001220 *custom_format,
1221 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001222 return true;
1223}
1224
1225static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001226ScanBracketedRange (const char* var_name_begin,
1227 const char* var_name_end,
1228 const char* var_name_final,
1229 const char** open_bracket_position,
1230 const char** separator_position,
1231 const char** close_bracket_position,
1232 const char** var_name_final_if_array_range,
1233 int64_t* index_lower,
1234 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001235{
Greg Clayton5160ce52013-03-27 23:08:40 +00001236 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001237 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001238 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001239 {
1240 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1241 *close_bracket_position = ::strchr(*open_bracket_position,']');
1242 // as usual, we assume that [] will come before %
1243 //printf("trying to expand a []\n");
1244 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001245 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001246 {
Enrico Granatae992a082011-07-22 17:03:19 +00001247 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001248 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001249 *index_lower = 0;
1250 }
1251 else if (*separator_position == NULL || *separator_position > var_name_end)
1252 {
1253 char *end = NULL;
1254 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1255 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001256 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001257 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001258 }
Greg Clayton34132752011-07-06 04:07:21 +00001259 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001260 {
1261 char *end = NULL;
1262 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1263 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001264 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001265 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001266 }
1267 else
Enrico Granatae992a082011-07-22 17:03:19 +00001268 {
1269 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001270 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001271 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001272 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001273 if (*index_lower > *index_higher && *index_higher > 0)
1274 {
Enrico Granatae992a082011-07-22 17:03:19 +00001275 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001276 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001277 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001278 *index_lower = *index_higher;
1279 *index_higher = temp;
1280 }
1281 }
Enrico Granatae992a082011-07-22 17:03:19 +00001282 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001283 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001284 return true;
1285}
1286
Michael Sartain0769b2b2013-07-30 16:44:36 +00001287template <typename T>
1288static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1289{
1290 if (script_interpreter)
1291 {
1292 Error script_error;
1293 std::string script_output;
1294
1295 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1296 {
1297 s.Printf("%s", script_output.c_str());
1298 return true;
1299 }
1300 else
1301 {
1302 s.Printf("<error: %s>",script_error.AsCString());
1303 }
1304 }
1305 return false;
1306}
1307
Enrico Granata9fc19442011-07-06 02:13:41 +00001308static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001309ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001310 size_t index,
Jason Molendab57e4a12013-11-04 09:33:30 +00001311 StackFrame* frame,
Enrico Granatadc940732011-08-23 00:32:52 +00001312 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001313{
Greg Clayton5160ce52013-03-27 23:08:40 +00001314 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001315 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001316 std::string ptr_deref_buffer(10,0);
1317 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001318 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001319 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001320 const char* first_unparsed;
1321 ValueObject::GetValueForExpressionPathOptions options;
1322 ValueObject::ExpressionPathEndResultType final_value_type;
1323 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001324 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001325 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001326 &first_unparsed,
1327 &reason_to_stop,
1328 &final_value_type,
1329 options,
1330 &what_next);
1331 if (!item)
1332 {
Enrico Granatae992a082011-07-22 17:03:19 +00001333 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001334 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001335 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001336 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001337 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001338 else
1339 {
Enrico Granatae992a082011-07-22 17:03:19 +00001340 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001341 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001342 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001343 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001344 }
1345 return item;
1346}
1347
Michael Sartain0769b2b2013-07-30 16:44:36 +00001348static inline bool
1349IsToken(const char *var_name_begin, const char *var)
1350{
1351 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1352}
1353
1354static bool
1355IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1356 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1357{
1358 int var_len = strlen(var);
1359 if (::strncmp (var_name_begin, var, var_len) == 0)
1360 {
1361 var_name_begin += var_len;
1362 if (*var_name_begin == '}')
1363 {
1364 format = default_format;
1365 return true;
1366 }
1367 else if (*var_name_begin == '%')
1368 {
1369 // Allow format specifiers: x|X|u with optional width specifiers.
1370 // ${thread.id%x} ; hex
1371 // ${thread.id%X} ; uppercase hex
1372 // ${thread.id%u} ; unsigned decimal
1373 // ${thread.id%8.8X} ; width.precision + specifier
1374 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1375 int dot_count = 0;
1376 const char *specifier = NULL;
1377 int width_precision_length = 0;
1378 const char *width_precision = ++var_name_begin;
1379 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1380 {
1381 dot_count += (*var_name_begin == '.');
1382 if (dot_count > 1)
1383 break;
1384 var_name_begin++;
1385 width_precision_length++;
1386 }
1387
1388 if (IsToken (var_name_begin, "tid}"))
1389 {
1390 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1391 if (target)
1392 {
1393 ArchSpec arch (target->GetArchitecture ());
1394 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1395 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1396 specifier = PRIu64;
1397 }
1398 if (!specifier)
1399 {
1400 format = default_format;
1401 return true;
1402 }
1403 }
1404 else if (IsToken (var_name_begin, "x}"))
1405 specifier = PRIx64;
1406 else if (IsToken (var_name_begin, "X}"))
1407 specifier = PRIX64;
1408 else if (IsToken (var_name_begin, "u}"))
1409 specifier = PRIu64;
1410
1411 if (specifier)
1412 {
1413 format = "%";
1414 if (width_precision_length)
1415 format += std::string(width_precision, width_precision_length);
1416 format += specifier;
1417 return true;
1418 }
1419 }
1420 }
1421 return false;
1422}
1423
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001424static bool
1425FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001426(
1427 const char *format,
1428 const SymbolContext *sc,
1429 const ExecutionContext *exe_ctx,
1430 const Address *addr,
1431 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001432 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001433 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001434)
1435{
Enrico Granatac482a192011-08-17 22:13:59 +00001436 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001437 bool success = true;
1438 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001439 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001440
Greg Clayton1b654882010-09-19 02:33:57 +00001441 for (p = format; *p != '\0'; ++p)
1442 {
Enrico Granatac482a192011-08-17 22:13:59 +00001443 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001444 {
Enrico Granatac482a192011-08-17 22:13:59 +00001445 valobj = realvalobj;
1446 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001447 }
Greg Clayton1b654882010-09-19 02:33:57 +00001448 size_t non_special_chars = ::strcspn (p, "${}\\");
1449 if (non_special_chars > 0)
1450 {
1451 if (success)
1452 s.Write (p, non_special_chars);
1453 p += non_special_chars;
1454 }
1455
1456 if (*p == '\0')
1457 {
1458 break;
1459 }
1460 else if (*p == '{')
1461 {
1462 // Start a new scope that must have everything it needs if it is to
1463 // to make it into the final output stream "s". If you want to make
1464 // a format that only prints out the function or symbol name if there
1465 // is one in the symbol context you can use:
1466 // "{function =${function.name}}"
1467 // The first '{' starts a new scope that end with the matching '}' at
1468 // the end of the string. The contents "function =${function.name}"
1469 // will then be evaluated and only be output if there is a function
1470 // or symbol with a valid name.
1471 StreamString sub_strm;
1472
1473 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001474
1475 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001476 {
1477 // The stream had all it needed
1478 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1479 }
1480 if (*p != '}')
1481 {
1482 success = false;
1483 break;
1484 }
1485 }
1486 else if (*p == '}')
1487 {
1488 // End of a enclosing scope
1489 break;
1490 }
1491 else if (*p == '$')
1492 {
1493 // We have a prompt variable to print
1494 ++p;
1495 if (*p == '{')
1496 {
1497 ++p;
1498 const char *var_name_begin = p;
1499 const char *var_name_end = ::strchr (p, '}');
1500
1501 if (var_name_end && var_name_begin < var_name_end)
1502 {
1503 // if we have already failed to parse, skip this variable
1504 if (success)
1505 {
1506 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001507 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001508 Address format_addr;
1509 bool calculate_format_addr_function_offset = false;
1510 // Set reg_kind and reg_num to invalid values
1511 RegisterKind reg_kind = kNumRegisterKinds;
1512 uint32_t reg_num = LLDB_INVALID_REGNUM;
1513 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001514 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001515 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001516 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001517 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1518 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001519
Greg Clayton1b654882010-09-19 02:33:57 +00001520 // Each variable must set success to true below...
1521 bool var_success = false;
1522 switch (var_name_begin[0])
1523 {
Enrico Granata4becb372011-06-29 22:27:15 +00001524 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001525 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001526 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001527 {
Enrico Granatac482a192011-08-17 22:13:59 +00001528 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001529 break;
1530
Enrico Granatac3e320a2011-08-02 17:27:39 +00001531 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001532 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001533
Enrico Granata6f3533f2011-07-29 19:53:35 +00001534 // check for *var and *svar
1535 if (*var_name_begin == '*')
1536 {
1537 do_deref_pointer = true;
1538 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001539 if (log)
1540 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001541 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001542
Enrico Granata6f3533f2011-07-29 19:53:35 +00001543 if (*var_name_begin == 's')
1544 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001545 if (!valobj->IsSynthetic())
1546 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001547 if (!valobj)
1548 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001549 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001550 if (log)
1551 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001552 }
1553
1554 // should be a 'v' by now
1555 if (*var_name_begin != 'v')
1556 break;
1557
Enrico Granatac3e320a2011-08-02 17:27:39 +00001558 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001559 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001560
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001561 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001562 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001563 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001564 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001565 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001566 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001567 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001568 const char* var_name_final = NULL;
1569 const char* var_name_final_if_array_range = NULL;
1570 const char* close_bracket_position = NULL;
1571 int64_t index_lower = -1;
1572 int64_t index_higher = -1;
1573 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001574 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001575 bool was_plain_var = false;
1576 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001577 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001578
Enrico Granatac482a192011-08-17 22:13:59 +00001579 if (!valobj) break;
1580 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001581 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001582 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001583 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001584 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001585 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001586 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001587 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001588 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001589 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001590 // this is a variable with some custom format applied to it
1591 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001592 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001593 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001594 ScanFormatDescriptor (var_name_begin,
1595 var_name_end,
1596 &var_name_final,
1597 &percent_position,
1598 &custom_format,
1599 &val_obj_display);
1600 }
1601 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001602 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001603 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001604 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001605 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001606 const char* percent_position;
1607 ScanFormatDescriptor (var_name_begin,
1608 var_name_end,
1609 &var_name_final,
1610 &percent_position,
1611 &custom_format,
1612 &val_obj_display);
1613
1614 const char* open_bracket_position;
1615 const char* separator_position;
1616 ScanBracketedRange (var_name_begin,
1617 var_name_end,
1618 var_name_final,
1619 &open_bracket_position,
1620 &separator_position,
1621 &close_bracket_position,
1622 &var_name_final_if_array_range,
1623 &index_lower,
1624 &index_higher);
1625
1626 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001627
Enrico Granata599171a2013-02-01 23:59:44 +00001628 std::string expr_path(var_name_final-var_name_begin-1,0);
1629 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1630
1631 if (log)
1632 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1633
1634 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001635 &first_unparsed,
1636 &reason_to_stop,
1637 &final_value_type,
1638 options,
1639 &what_next).get();
1640
1641 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001642 {
Enrico Granatae992a082011-07-22 17:03:19 +00001643 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001644 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001645 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001646 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001647 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001648 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001649 else
1650 {
Enrico Granatae992a082011-07-22 17:03:19 +00001651 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001652 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001653 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001654 first_unparsed, reason_to_stop, final_value_type);
1655 }
Enrico Granata4becb372011-06-29 22:27:15 +00001656 }
Greg Clayton34132752011-07-06 04:07:21 +00001657 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001658 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001659
Enrico Granata86cc9822012-03-19 22:58:49 +00001660 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1661 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001662
Enrico Granata86cc9822012-03-19 22:58:49 +00001663 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001664
Enrico Granataa7187d02011-07-06 19:27:11 +00001665 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001666 {
Greg Clayton34132752011-07-06 04:07:21 +00001667 // I have not deref-ed yet, let's do it
1668 // this happens when we are not going through GetValueForVariableExpressionPath
1669 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001670 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001671 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001672 if (error.Fail())
1673 {
1674 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001675 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001676 break;
1677 }
Greg Clayton34132752011-07-06 04:07:21 +00001678 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001679 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001680
Enrico Granataa777dc22012-05-08 21:49:57 +00001681 // we do not want to use the summary for a bitfield of type T:n
1682 // if we were originally dealing with just a T - that would get
1683 // us into an endless recursion
1684 if (target->IsBitfield() && was_var_indexed)
1685 {
1686 // TODO: check for a (T:n)-specific summary - we should still obey that
1687 StreamString bitfield_name;
1688 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1689 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1690 if (!DataVisualization::GetSummaryForType(type_sp))
1691 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1692 }
1693
Enrico Granata85933ed2011-08-18 16:38:26 +00001694 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001695 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1696 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1697 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1698 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001699
Enrico Granata86cc9822012-03-19 22:58:49 +00001700 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 +00001701 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001702 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001703 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001704 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001705
Greg Clayton5088c482013-03-25 21:06:13 +00001706 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001707 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001708 // try to use the special cases
1709 var_success = target->DumpPrintableRepresentation(str_temp,
1710 val_obj_display,
1711 custom_format);
1712 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001713 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001714
1715 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001716 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001717 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001718 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001719 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001720 }
1721 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001722 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001723 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001724 {
1725 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1726 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001727 else if (is_pointer) // if pointer, value is the address stored
1728 {
Greg Clayton23f59502012-07-17 03:23:13 +00001729 target->DumpPrintableRepresentation (s,
1730 val_obj_display,
1731 custom_format,
1732 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001733 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001734 var_success = true;
1735 break;
1736 }
1737 }
1738
1739 // if directly trying to print ${var}, and this is an aggregate, display a nice
1740 // type @ location message
1741 if (is_aggregate && was_plain_var)
1742 {
1743 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1744 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001745 break;
1746 }
1747
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001748 // 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 +00001749 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001750 {
1751 s << "<invalid use of aggregate type>";
1752 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001753 break;
1754 }
Greg Clayton34132752011-07-06 04:07:21 +00001755
1756 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001757 {
1758 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001759 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001760 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001761 }
Greg Clayton34132752011-07-06 04:07:21 +00001762 else
Enrico Granatae992a082011-07-22 17:03:19 +00001763 {
1764 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001765 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001766 if (!is_array && !is_pointer)
1767 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001768 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001769 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001770 const char* special_directions = NULL;
1771 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001772 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1773 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001774 ConstString additional_data;
1775 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1776 special_directions_writer.Printf("${%svar%s}",
1777 do_deref_pointer ? "*" : "",
1778 additional_data.GetCString());
1779 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001780 }
1781
1782 // let us display items index_lower thru index_higher of this array
1783 s.PutChar('[');
1784 var_success = true;
1785
1786 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001787 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001788
Greg Claytoncc4d0142012-02-17 07:49:44 +00001789 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001790
Greg Clayton34132752011-07-06 04:07:21 +00001791 for (;index_lower<=index_higher;index_lower++)
1792 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001793 ValueObject* item = ExpandIndexedExpression (target,
1794 index_lower,
1795 exe_ctx->GetFramePtr(),
1796 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001797
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001798 if (!item)
1799 {
Enrico Granatae992a082011-07-22 17:03:19 +00001800 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001801 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001802 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001803 else
1804 {
Enrico Granatae992a082011-07-22 17:03:19 +00001805 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001806 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001807 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001808
Greg Clayton34132752011-07-06 04:07:21 +00001809 if (!special_directions)
1810 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1811 else
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001812 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
Greg Clayton34132752011-07-06 04:07:21 +00001813
Enrico Granata22c55d12011-08-12 02:00:06 +00001814 if (--max_num_children == 0)
1815 {
1816 s.PutCString(", ...");
1817 break;
1818 }
1819
Greg Clayton34132752011-07-06 04:07:21 +00001820 if (index_lower < index_higher)
1821 s.PutChar(',');
1822 }
1823 s.PutChar(']');
1824 }
Enrico Granata4becb372011-06-29 22:27:15 +00001825 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001826 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001827 case 'a':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001828 if (IsToken (var_name_begin, "addr}"))
Greg Clayton1b654882010-09-19 02:33:57 +00001829 {
1830 if (addr && addr->IsValid())
1831 {
1832 var_success = true;
1833 format_addr = *addr;
1834 }
1835 }
1836 break;
1837
1838 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001839 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00001840 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001841 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001842 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001843 Process *process = exe_ctx->GetProcessPtr();
1844 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001845 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001846 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001847 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001848 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001849 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001850 var_success = true;
1851 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001852 else if ((IsToken (var_name_begin, "name}")) ||
1853 (IsToken (var_name_begin, "file.basename}")) ||
1854 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00001855 {
1856 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1857 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001858 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001859 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1860 {
1861 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001862 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001863 }
1864 else
1865 {
1866 format_file_spec = exe_module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001867 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001868 }
Greg Clayton1b654882010-09-19 02:33:57 +00001869 }
1870 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001871 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001872 {
1873 var_name_begin += ::strlen("script:");
1874 std::string script_name(var_name_begin,var_name_end);
1875 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001876 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
1877 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001878 }
Greg Clayton1b654882010-09-19 02:33:57 +00001879 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001880 }
Greg Clayton1b654882010-09-19 02:33:57 +00001881 }
1882 break;
1883
1884 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001885 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00001886 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001887 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001888 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001889 Thread *thread = exe_ctx->GetThreadPtr();
1890 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001891 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001892 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001893 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001894 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001895 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001896 var_success = true;
1897 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001898 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00001899 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001900 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00001901 var_success = true;
1902 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001903 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00001904 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001905 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001906 var_success = true;
1907 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001908 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001909 {
1910 cstr = thread->GetName();
1911 var_success = cstr && cstr[0];
1912 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001913 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001914 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001915 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001916 {
1917 cstr = thread->GetQueueName();
1918 var_success = cstr && cstr[0];
1919 if (var_success)
1920 s.PutCString(cstr);
1921 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001922 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001923 {
1924 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001925 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001926 {
1927 cstr = stop_info_sp->GetDescription();
1928 if (cstr && cstr[0])
1929 {
1930 s.PutCString(cstr);
1931 var_success = true;
1932 }
Greg Clayton1b654882010-09-19 02:33:57 +00001933 }
1934 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001935 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00001936 {
1937 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001938 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001939 {
1940 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1941 if (return_valobj_sp)
1942 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00001943 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00001944 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001945 }
1946 }
1947 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001948 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001949 {
1950 var_name_begin += ::strlen("script:");
1951 std::string script_name(var_name_begin,var_name_end);
1952 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001953 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
1954 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001955 }
Greg Clayton1b654882010-09-19 02:33:57 +00001956 }
1957 }
1958 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001959 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00001960 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001961 // TODO: hookup properties
1962// if (!target_properties_sp)
1963// {
1964// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1965// if (target)
1966// target_properties_sp = target->GetProperties();
1967// }
1968//
1969// if (target_properties_sp)
1970// {
1971// var_name_begin += ::strlen ("target.");
1972// const char *end_property = strchr(var_name_begin, '}');
1973// if (end_property)
1974// {
1975// ConstString property_name(var_name_begin, end_property - var_name_begin);
1976// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
1977// if (!property_value.empty())
1978// {
1979// s.PutCString (property_value.c_str());
1980// var_success = true;
1981// }
1982// }
1983// }
Greg Clayton0603aa92010-10-04 01:05:56 +00001984 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1985 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001986 {
Greg Clayton1b654882010-09-19 02:33:57 +00001987 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001988 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00001989 {
1990 ArchSpec arch (target->GetArchitecture ());
1991 if (arch.IsValid())
1992 {
Greg Clayton64195a22011-02-23 00:35:02 +00001993 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001994 var_success = true;
1995 }
1996 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001997 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001998 {
1999 var_name_begin += ::strlen("script:");
2000 std::string script_name(var_name_begin,var_name_end);
2001 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002002 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2003 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002004 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002005 }
Greg Clayton1b654882010-09-19 02:33:57 +00002006 }
2007 break;
2008
2009
2010 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002011 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002012 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002013 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002014 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002015 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002016 var_name_begin += ::strlen ("module.");
2017
Michael Sartain0769b2b2013-07-30 16:44:36 +00002018 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002019 {
2020 if (module->GetFileSpec())
2021 {
2022 var_name_begin += ::strlen ("file.");
2023
Michael Sartain0769b2b2013-07-30 16:44:36 +00002024 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002025 {
2026 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002027 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002028 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002029 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002030 {
2031 format_file_spec = module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002032 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002033 }
2034 }
2035 }
2036 }
2037 }
2038 break;
2039
2040
2041 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002042 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002043 {
2044 if (sc && sc->comp_unit != NULL)
2045 {
2046 var_name_begin += ::strlen ("file.");
2047
Michael Sartain0769b2b2013-07-30 16:44:36 +00002048 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002049 {
2050 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002051 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002052 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002053 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002054 {
2055 format_file_spec = *sc->comp_unit;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002056 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002057 }
2058 }
2059 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002060 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002061 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002062 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002063 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002064 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002065 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002066 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002067 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002068 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002069 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002070 s.Printf("%u", frame->GetFrameIndex());
2071 var_success = true;
2072 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002073 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002074 {
2075 reg_kind = eRegisterKindGeneric;
2076 reg_num = LLDB_REGNUM_GENERIC_PC;
2077 var_success = true;
2078 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002079 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002080 {
2081 reg_kind = eRegisterKindGeneric;
2082 reg_num = LLDB_REGNUM_GENERIC_SP;
2083 var_success = true;
2084 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002085 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002086 {
2087 reg_kind = eRegisterKindGeneric;
2088 reg_num = LLDB_REGNUM_GENERIC_FP;
2089 var_success = true;
2090 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002091 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002092 {
2093 reg_kind = eRegisterKindGeneric;
2094 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2095 var_success = true;
2096 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002097 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002098 {
2099 reg_ctx = frame->GetRegisterContext().get();
2100 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002101 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002102 var_name_begin += ::strlen ("reg.");
2103 if (var_name_begin < var_name_end)
2104 {
2105 std::string reg_name (var_name_begin, var_name_end);
2106 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2107 if (reg_info)
2108 var_success = true;
2109 }
Greg Clayton1b654882010-09-19 02:33:57 +00002110 }
2111 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002112 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002113 {
2114 var_name_begin += ::strlen("script:");
2115 std::string script_name(var_name_begin,var_name_end);
2116 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002117 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2118 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002119 }
Greg Clayton1b654882010-09-19 02:33:57 +00002120 }
2121 }
2122 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002123 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002124 {
2125 if (sc && (sc->function != NULL || sc->symbol != NULL))
2126 {
2127 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002128 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002129 {
2130 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002131 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002132 else
2133 s.Printf("symbol[%u]", sc->symbol->GetID());
2134
2135 var_success = true;
2136 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002137 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002138 {
2139 if (sc->function)
2140 cstr = sc->function->GetName().AsCString (NULL);
2141 else if (sc->symbol)
2142 cstr = sc->symbol->GetName().AsCString (NULL);
2143 if (cstr)
2144 {
2145 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002146
2147 if (sc->block)
2148 {
2149 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2150 if (inline_block)
2151 {
2152 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2153 if (inline_info)
2154 {
2155 s.PutCString(" [inlined] ");
2156 inline_info->GetName().Dump(&s);
2157 }
2158 }
2159 }
Greg Clayton1b654882010-09-19 02:33:57 +00002160 var_success = true;
2161 }
2162 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002163 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002164 {
2165 // Print the function name with arguments in it
2166
2167 if (sc->function)
2168 {
2169 var_success = true;
2170 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2171 cstr = sc->function->GetName().AsCString (NULL);
2172 if (cstr)
2173 {
2174 const InlineFunctionInfo *inline_info = NULL;
2175 VariableListSP variable_list_sp;
2176 bool get_function_vars = true;
2177 if (sc->block)
2178 {
2179 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2180
2181 if (inline_block)
2182 {
2183 get_function_vars = false;
2184 inline_info = sc->block->GetInlinedFunctionInfo();
2185 if (inline_info)
2186 variable_list_sp = inline_block->GetBlockVariableList (true);
2187 }
2188 }
2189
2190 if (get_function_vars)
2191 {
2192 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2193 }
2194
2195 if (inline_info)
2196 {
2197 s.PutCString (cstr);
2198 s.PutCString (" [inlined] ");
2199 cstr = inline_info->GetName().GetCString();
2200 }
2201
2202 VariableList args;
2203 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002204 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002205 if (args.GetSize() > 0)
2206 {
2207 const char *open_paren = strchr (cstr, '(');
2208 const char *close_paren = NULL;
2209 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002210 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002211 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002212 {
2213 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2214 if (open_paren)
2215 close_paren = strchr (open_paren, ')');
2216 }
2217 else
2218 close_paren = strchr (open_paren, ')');
2219 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002220
2221 if (open_paren)
2222 s.Write(cstr, open_paren - cstr + 1);
2223 else
2224 {
2225 s.PutCString (cstr);
2226 s.PutChar ('(');
2227 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002228 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002229 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2230 {
2231 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2232 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2233 const char *var_name = var_value_sp->GetName().GetCString();
2234 const char *var_value = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002235 if (arg_idx > 0)
2236 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002237 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002238 {
2239 if (var_value)
2240 s.Printf ("%s=%s", var_name, var_value);
2241 else
2242 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2243 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002244 else
2245 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002246 }
2247
2248 if (close_paren)
2249 s.PutCString (close_paren);
2250 else
2251 s.PutChar(')');
2252
2253 }
2254 else
2255 {
2256 s.PutCString(cstr);
2257 }
2258 }
2259 }
2260 else if (sc->symbol)
2261 {
2262 cstr = sc->symbol->GetName().AsCString (NULL);
2263 if (cstr)
2264 {
2265 s.PutCString(cstr);
2266 var_success = true;
2267 }
2268 }
2269 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002270 else if (IsToken (var_name_begin, "addr-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002271 {
2272 var_success = addr != NULL;
2273 if (var_success)
2274 {
2275 format_addr = *addr;
2276 calculate_format_addr_function_offset = true;
2277 }
2278 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002279 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002280 {
2281 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2282 if (var_success)
2283 {
2284 format_addr = sc->line_entry.range.GetBaseAddress();
2285 calculate_format_addr_function_offset = true;
2286 }
2287 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002288 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002289 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002290 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002291 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002292 if (var_success)
2293 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002294 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002295 calculate_format_addr_function_offset = true;
2296 }
2297 }
2298 }
2299 }
2300 break;
2301
2302 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002303 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002304 {
2305 if (sc && sc->line_entry.IsValid())
2306 {
2307 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002308 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002309 {
2310 var_name_begin += ::strlen ("file.");
2311
Michael Sartain0769b2b2013-07-30 16:44:36 +00002312 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002313 {
2314 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002315 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002316 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002317 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002318 {
2319 format_file_spec = sc->line_entry.file;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002320 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002321 }
2322 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002323 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002324 {
2325 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002326 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002327 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002328 else if ((IsToken (var_name_begin, "start-addr}")) ||
2329 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002330 {
2331 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2332 if (var_success)
2333 {
2334 format_addr = sc->line_entry.range.GetBaseAddress();
2335 if (var_name_begin[0] == 'e')
2336 format_addr.Slide (sc->line_entry.range.GetByteSize());
2337 }
2338 }
2339 }
2340 }
2341 break;
2342 }
2343
2344 if (var_success)
2345 {
2346 // If format addr is valid, then we need to print an address
2347 if (reg_num != LLDB_INVALID_REGNUM)
2348 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002349 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002350 // We have a register value to display...
2351 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2352 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002353 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002354 }
2355 else
2356 {
2357 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002358 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002359
2360 if (reg_ctx)
2361 {
2362 if (reg_kind != kNumRegisterKinds)
2363 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2364 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2365 var_success = reg_info != NULL;
2366 }
2367 }
2368 }
2369
2370 if (reg_info != NULL)
2371 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002372 RegisterValue reg_value;
2373 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2374 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002375 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002376 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002377 }
2378 }
2379
2380 if (format_file_spec)
2381 {
2382 s << format_file_spec;
2383 }
2384
2385 // If format addr is valid, then we need to print an address
2386 if (format_addr.IsValid())
2387 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002388 var_success = false;
2389
Greg Clayton1b654882010-09-19 02:33:57 +00002390 if (calculate_format_addr_function_offset)
2391 {
2392 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002393
Greg Clayton0603aa92010-10-04 01:05:56 +00002394 if (sc)
2395 {
2396 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002397 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002398 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002399 if (sc->block)
2400 {
2401 // Check to make sure we aren't in an inline
2402 // function. If we are, use the inline block
2403 // range that contains "format_addr" since
2404 // blocks can be discontiguous.
2405 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2406 AddressRange inline_range;
2407 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2408 func_addr = inline_range.GetBaseAddress();
2409 }
2410 }
Greg Claytone7612132012-03-07 21:03:09 +00002411 else if (sc->symbol && sc->symbol->ValueIsAddress())
2412 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002413 }
2414
2415 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002416 {
2417 if (func_addr.GetSection() == format_addr.GetSection())
2418 {
2419 addr_t func_file_addr = func_addr.GetFileAddress();
2420 addr_t addr_file_addr = format_addr.GetFileAddress();
2421 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002422 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002423 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002424 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002425 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002426 }
2427 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002428 {
2429 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2430 if (target)
2431 {
2432 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2433 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2434 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002435 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002436 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002437 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002438 var_success = true;
2439 }
2440 }
Greg Clayton1b654882010-09-19 02:33:57 +00002441 }
2442 }
2443 else
2444 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002445 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002446 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002447 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2448 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002449 if (vaddr == LLDB_INVALID_ADDRESS)
2450 vaddr = format_addr.GetFileAddress ();
2451
2452 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002453 {
Greg Clayton514487e2011-02-15 21:59:32 +00002454 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002455 if (addr_width == 0)
2456 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002457 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002458 var_success = true;
2459 }
Greg Clayton1b654882010-09-19 02:33:57 +00002460 }
2461 }
2462 }
2463
2464 if (var_success == false)
2465 success = false;
2466 }
2467 p = var_name_end;
2468 }
2469 else
2470 break;
2471 }
2472 else
2473 {
2474 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2475 s.PutChar(*p);
2476 }
2477 }
2478 else if (*p == '\\')
2479 {
2480 ++p; // skip the slash
2481 switch (*p)
2482 {
2483 case 'a': s.PutChar ('\a'); break;
2484 case 'b': s.PutChar ('\b'); break;
2485 case 'f': s.PutChar ('\f'); break;
2486 case 'n': s.PutChar ('\n'); break;
2487 case 'r': s.PutChar ('\r'); break;
2488 case 't': s.PutChar ('\t'); break;
2489 case 'v': s.PutChar ('\v'); break;
2490 case '\'': s.PutChar ('\''); break;
2491 case '\\': s.PutChar ('\\'); break;
2492 case '0':
2493 // 1 to 3 octal chars
2494 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002495 // Make a string that can hold onto the initial zero char,
2496 // up to 3 octal digits, and a terminating NULL.
2497 char oct_str[5] = { 0, 0, 0, 0, 0 };
2498
2499 int i;
2500 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2501 oct_str[i] = p[i];
2502
2503 // We don't want to consume the last octal character since
2504 // the main for loop will do this for us, so we advance p by
2505 // one less than i (even if i is zero)
2506 p += i - 1;
2507 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2508 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002509 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002510 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002511 }
Greg Clayton1b654882010-09-19 02:33:57 +00002512 }
2513 break;
2514
2515 case 'x':
2516 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002517 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002518 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002519 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002520
Greg Clayton0603aa92010-10-04 01:05:56 +00002521 // Make a string that can hold onto two hex chars plus a
2522 // NULL terminator
2523 char hex_str[3] = { 0,0,0 };
2524 hex_str[0] = *p;
2525 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002526 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002527 ++p; // Skip the first of the two hex chars
2528 hex_str[1] = *p;
2529 }
2530
2531 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2532 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002533 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002534 }
2535 else
2536 {
2537 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002538 }
2539 break;
2540
2541 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002542 // Just desensitize any other character by just printing what
2543 // came after the '\'
2544 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002545 break;
2546
2547 }
2548
2549 }
2550 }
2551 if (end)
2552 *end = p;
2553 return success;
2554}
2555
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002556bool
2557Debugger::FormatPrompt
2558(
2559 const char *format,
2560 const SymbolContext *sc,
2561 const ExecutionContext *exe_ctx,
2562 const Address *addr,
2563 Stream &s,
2564 ValueObject* valobj
2565)
2566{
2567 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2568 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2569 if (format_str.length())
2570 format = format_str.c_str();
2571 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
2572}
2573
Jim Ingham228063c2012-02-21 02:23:08 +00002574void
2575Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2576{
Jim Ingham4f02b222012-02-22 22:49:20 +00002577 // For simplicity's sake, I am not going to deal with how to close down any
2578 // open logging streams, I just redirect everything from here on out to the
2579 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002580 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2581}
2582
2583bool
2584Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2585{
2586 Log::Callbacks log_callbacks;
2587
2588 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002589 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002590 {
2591 log_stream_sp = m_log_callback_stream_sp;
2592 // For now when using the callback mode you always get thread & timestamp.
2593 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2594 }
2595 else if (log_file == NULL || *log_file == '\0')
2596 {
Greg Clayton44d93782014-01-27 23:43:24 +00002597 log_stream_sp = GetOutputFile();
Jim Ingham228063c2012-02-21 02:23:08 +00002598 }
2599 else
2600 {
2601 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002602 if (pos != m_log_streams.end())
2603 log_stream_sp = pos->second.lock();
2604 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002605 {
2606 log_stream_sp.reset (new StreamFile (log_file));
2607 m_log_streams[log_file] = log_stream_sp;
2608 }
Jim Ingham228063c2012-02-21 02:23:08 +00002609 }
2610 assert (log_stream_sp.get());
2611
2612 if (log_options == 0)
2613 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2614
Greg Clayton57abc5d2013-05-10 21:47:16 +00002615 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002616 {
2617 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2618 return true;
2619 }
2620 else
2621 {
2622 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2623 if (log_channel_sp)
2624 {
2625 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2626 {
2627 return true;
2628 }
2629 else
2630 {
2631 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2632 return false;
2633 }
2634 }
2635 else
2636 {
2637 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2638 return false;
2639 }
2640 }
2641 return false;
2642}
2643
Greg Clayton9585fbf2013-03-19 00:20:55 +00002644SourceManager &
2645Debugger::GetSourceManager ()
2646{
2647 if (m_source_manager_ap.get() == NULL)
2648 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2649 return *m_source_manager_ap;
2650}
2651
2652
Greg Clayton44d93782014-01-27 23:43:24 +00002653
2654// This function handles events that were broadcast by the process.
2655void
2656Debugger::HandleBreakpointEvent (const EventSP &event_sp)
2657{
2658 using namespace lldb;
2659 const uint32_t event_type = Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (event_sp);
2660
2661// if (event_type & eBreakpointEventTypeAdded
2662// || event_type & eBreakpointEventTypeRemoved
2663// || event_type & eBreakpointEventTypeEnabled
2664// || event_type & eBreakpointEventTypeDisabled
2665// || event_type & eBreakpointEventTypeCommandChanged
2666// || event_type & eBreakpointEventTypeConditionChanged
2667// || event_type & eBreakpointEventTypeIgnoreChanged
2668// || event_type & eBreakpointEventTypeLocationsResolved)
2669// {
2670// // Don't do anything about these events, since the breakpoint commands already echo these actions.
2671// }
2672//
2673 if (event_type & eBreakpointEventTypeLocationsAdded)
2674 {
2675 uint32_t num_new_locations = Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(event_sp);
2676 if (num_new_locations > 0)
2677 {
2678 BreakpointSP breakpoint = Breakpoint::BreakpointEventData::GetBreakpointFromEvent(event_sp);
2679 StreamFileSP output_sp (GetOutputFile());
2680 if (output_sp)
2681 {
2682 output_sp->Printf("%d location%s added to breakpoint %d\n",
2683 num_new_locations,
2684 num_new_locations == 1 ? "" : "s",
2685 breakpoint->GetID());
2686 RefreshTopIOHandler();
2687 }
2688 }
2689 }
2690// else if (event_type & eBreakpointEventTypeLocationsRemoved)
2691// {
2692// // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
2693// }
2694// else if (event_type & eBreakpointEventTypeLocationsResolved)
2695// {
2696// // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
2697// }
2698}
2699
2700size_t
2701Debugger::GetProcessSTDOUT (Process *process, Stream *stream)
2702{
2703 size_t total_bytes = 0;
2704 if (stream == NULL)
2705 stream = GetOutputFile().get();
2706
2707 if (stream)
2708 {
2709 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
2710 if (process == NULL)
2711 {
2712 TargetSP target_sp = GetTargetList().GetSelectedTarget();
2713 if (target_sp)
2714 process = target_sp->GetProcessSP().get();
2715 }
2716 if (process)
2717 {
2718 Error error;
2719 size_t len;
2720 char stdio_buffer[1024];
2721 while ((len = process->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2722 {
2723 stream->Write(stdio_buffer, len);
2724 total_bytes += len;
2725 }
2726 }
2727 stream->Flush();
2728 }
2729 return total_bytes;
2730}
2731
2732size_t
2733Debugger::GetProcessSTDERR (Process *process, Stream *stream)
2734{
2735 size_t total_bytes = 0;
2736 if (stream == NULL)
2737 stream = GetOutputFile().get();
2738
2739 if (stream)
2740 {
2741 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2742 if (process == NULL)
2743 {
2744 TargetSP target_sp = GetTargetList().GetSelectedTarget();
2745 if (target_sp)
2746 process = target_sp->GetProcessSP().get();
2747 }
2748 if (process)
2749 {
2750 Error error;
2751 size_t len;
2752 char stdio_buffer[1024];
2753 while ((len = process->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2754 {
2755 stream->Write(stdio_buffer, len);
2756 total_bytes += len;
2757 }
2758 }
2759 stream->Flush();
2760 }
2761 return total_bytes;
2762}
2763
2764// This function handles events that were broadcast by the process.
2765void
2766Debugger::HandleProcessEvent (const EventSP &event_sp)
2767{
2768 using namespace lldb;
2769 const uint32_t event_type = event_sp->GetType();
2770 ProcessSP process_sp = Process::ProcessEventData::GetProcessFromEvent(event_sp.get());
2771
2772 const bool gui_enabled = IsForwardingEvents();
2773 bool top_io_handler_hid = false;
2774 if (gui_enabled == false)
2775 top_io_handler_hid = HideTopIOHandler();
2776
2777 assert (process_sp);
2778
2779 if (event_type & Process::eBroadcastBitSTDOUT)
2780 {
2781 // The process has stdout available, get it and write it out to the
2782 // appropriate place.
2783 if (top_io_handler_hid)
2784 GetProcessSTDOUT (process_sp.get(), NULL);
2785 }
2786 else if (event_type & Process::eBroadcastBitSTDERR)
2787 {
2788 // The process has stderr available, get it and write it out to the
2789 // appropriate place.
2790 if (top_io_handler_hid)
2791 GetProcessSTDERR (process_sp.get(), NULL);
2792 }
2793 else if (event_type & Process::eBroadcastBitStateChanged)
2794 {
2795 // Drain all stout and stderr so we don't see any output come after
2796 // we print our prompts
2797 if (top_io_handler_hid)
2798 {
2799 StreamFileSP stream_sp (GetOutputFile());
2800 GetProcessSTDOUT (process_sp.get(), stream_sp.get());
2801 GetProcessSTDERR (process_sp.get(), NULL);
2802 // Something changed in the process; get the event and report the process's current status and location to
2803 // the user.
2804 StateType event_state = Process::ProcessEventData::GetStateFromEvent (event_sp.get());
2805 if (event_state == eStateInvalid)
2806 return;
2807
2808 switch (event_state)
2809 {
2810 case eStateInvalid:
2811 case eStateUnloaded:
2812 case eStateConnected:
2813 case eStateAttaching:
2814 case eStateLaunching:
2815 case eStateStepping:
2816 case eStateDetached:
2817 {
2818 stream_sp->Printf("Process %" PRIu64 " %s\n",
2819 process_sp->GetID(),
2820 StateAsCString (event_state));
2821 }
2822 break;
2823
2824 case eStateRunning:
2825 // Don't be chatty when we run...
2826 break;
2827
2828 case eStateExited:
2829 process_sp->GetStatus(*stream_sp);
2830 break;
2831
2832 case eStateStopped:
2833 case eStateCrashed:
2834 case eStateSuspended:
2835 // Make sure the program hasn't been auto-restarted:
2836 if (Process::ProcessEventData::GetRestartedFromEvent (event_sp.get()))
2837 {
2838 size_t num_reasons = Process::ProcessEventData::GetNumRestartedReasons(event_sp.get());
2839 if (num_reasons > 0)
2840 {
2841 // FIXME: Do we want to report this, or would that just be annoyingly chatty?
2842 if (num_reasons == 1)
2843 {
2844 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), 0);
2845 stream_sp->Printf("Process %" PRIu64 " stopped and restarted: %s\n",
2846 process_sp->GetID(),
2847 reason ? reason : "<UNKNOWN REASON>");
2848 }
2849 else
2850 {
2851 stream_sp->Printf("Process %" PRIu64 " stopped and restarted, reasons:\n",
2852 process_sp->GetID());
2853
2854
2855 for (size_t i = 0; i < num_reasons; i++)
2856 {
2857 const char *reason = Process::ProcessEventData::GetRestartedReasonAtIndex (event_sp.get(), i);
2858 stream_sp->Printf("\t%s\n", reason ? reason : "<UNKNOWN REASON>");
2859 }
2860 }
2861 }
2862 }
2863 else
2864 {
2865 // Lock the thread list so it doesn't change on us
2866 ThreadList &thread_list = process_sp->GetThreadList();
2867 Mutex::Locker locker (thread_list.GetMutex());
2868
2869 ThreadSP curr_thread (thread_list.GetSelectedThread());
2870 ThreadSP thread;
2871 StopReason curr_thread_stop_reason = eStopReasonInvalid;
2872 if (curr_thread)
2873 curr_thread_stop_reason = curr_thread->GetStopReason();
Greg Claytonc809cbc2014-01-28 00:36:31 +00002874 if (!curr_thread ||
2875 !curr_thread->IsValid() ||
Greg Clayton44d93782014-01-27 23:43:24 +00002876 curr_thread_stop_reason == eStopReasonInvalid ||
2877 curr_thread_stop_reason == eStopReasonNone)
2878 {
2879 // Prefer a thread that has just completed its plan over another thread as current thread.
2880 ThreadSP plan_thread;
2881 ThreadSP other_thread;
2882 const size_t num_threads = thread_list.GetSize();
2883 size_t i;
2884 for (i = 0; i < num_threads; ++i)
2885 {
2886 thread = thread_list.GetThreadAtIndex(i);
2887 StopReason thread_stop_reason = thread->GetStopReason();
2888 switch (thread_stop_reason)
2889 {
2890 case eStopReasonInvalid:
2891 case eStopReasonNone:
2892 break;
2893
2894 case eStopReasonTrace:
2895 case eStopReasonBreakpoint:
2896 case eStopReasonWatchpoint:
2897 case eStopReasonSignal:
2898 case eStopReasonException:
2899 case eStopReasonExec:
2900 case eStopReasonThreadExiting:
2901 if (!other_thread)
2902 other_thread = thread;
2903 break;
2904 case eStopReasonPlanComplete:
2905 if (!plan_thread)
2906 plan_thread = thread;
2907 break;
2908 }
2909 }
2910 if (plan_thread)
2911 thread_list.SetSelectedThreadByID (plan_thread->GetID());
2912 else if (other_thread)
2913 thread_list.SetSelectedThreadByID (other_thread->GetID());
2914 else
2915 {
Jim Ingham4046a302014-01-31 01:01:23 +00002916 if (curr_thread && curr_thread->IsValid())
Greg Clayton44d93782014-01-27 23:43:24 +00002917 thread = curr_thread;
2918 else
2919 thread = thread_list.GetThreadAtIndex(0);
2920
2921 if (thread)
2922 thread_list.SetSelectedThreadByID (thread->GetID());
2923 }
2924 }
2925
2926 if (GetTargetList().GetSelectedTarget().get() == &process_sp->GetTarget())
2927 {
2928 const bool only_threads_with_stop_reason = true;
2929 const uint32_t start_frame = 0;
2930 const uint32_t num_frames = 1;
2931 const uint32_t num_frames_with_source = 1;
2932 process_sp->GetStatus(*stream_sp);
2933 process_sp->GetThreadStatus (*stream_sp,
2934 only_threads_with_stop_reason,
2935 start_frame,
2936 num_frames,
2937 num_frames_with_source);
2938 }
2939 else
2940 {
2941 uint32_t target_idx = GetTargetList().GetIndexOfTarget(process_sp->GetTarget().shared_from_this());
2942 if (target_idx != UINT32_MAX)
2943 stream_sp->Printf ("Target %d: (", target_idx);
2944 else
2945 stream_sp->Printf ("Target <unknown index>: (");
2946 process_sp->GetTarget().Dump (stream_sp.get(), eDescriptionLevelBrief);
2947 stream_sp->Printf (") stopped.\n");
2948 }
2949 }
2950 break;
2951 }
2952 }
2953 }
2954
2955 if (top_io_handler_hid)
2956 RefreshTopIOHandler();
2957}
2958
2959void
2960Debugger::HandleThreadEvent (const EventSP &event_sp)
2961{
2962 // At present the only thread event we handle is the Frame Changed event,
2963 // and all we do for that is just reprint the thread status for that thread.
2964 using namespace lldb;
2965 const uint32_t event_type = event_sp->GetType();
2966 if (event_type == Thread::eBroadcastBitStackChanged ||
2967 event_type == Thread::eBroadcastBitThreadSelected )
2968 {
2969 ThreadSP thread_sp (Thread::ThreadEventData::GetThreadFromEvent (event_sp.get()));
2970 if (thread_sp)
2971 {
2972 HideTopIOHandler();
2973 StreamFileSP stream_sp (GetOutputFile());
2974 thread_sp->GetStatus(*stream_sp, 0, 1, 1);
2975 RefreshTopIOHandler();
2976 }
2977 }
2978}
2979
2980bool
2981Debugger::IsForwardingEvents ()
2982{
2983 return (bool)m_forward_listener_sp;
2984}
2985
2986void
2987Debugger::EnableForwardEvents (const ListenerSP &listener_sp)
2988{
2989 m_forward_listener_sp = listener_sp;
2990}
2991
2992void
2993Debugger::CancelForwardEvents (const ListenerSP &listener_sp)
2994{
2995 m_forward_listener_sp.reset();
2996}
2997
2998
2999void
3000Debugger::DefaultEventHandler()
3001{
3002 Listener& listener(GetListener());
3003 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
3004 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
3005 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
3006 BroadcastEventSpec target_event_spec (broadcaster_class_target,
3007 Target::eBroadcastBitBreakpointChanged);
3008
3009 BroadcastEventSpec process_event_spec (broadcaster_class_process,
3010 Process::eBroadcastBitStateChanged |
3011 Process::eBroadcastBitSTDOUT |
3012 Process::eBroadcastBitSTDERR);
3013
3014 BroadcastEventSpec thread_event_spec (broadcaster_class_thread,
3015 Thread::eBroadcastBitStackChanged |
3016 Thread::eBroadcastBitThreadSelected );
3017
3018 listener.StartListeningForEventSpec (*this, target_event_spec);
3019 listener.StartListeningForEventSpec (*this, process_event_spec);
3020 listener.StartListeningForEventSpec (*this, thread_event_spec);
3021 listener.StartListeningForEvents (m_command_interpreter_ap.get(),
3022 CommandInterpreter::eBroadcastBitQuitCommandReceived |
3023 CommandInterpreter::eBroadcastBitAsynchronousOutputData |
3024 CommandInterpreter::eBroadcastBitAsynchronousErrorData );
3025
3026 bool done = false;
3027 while (!done)
3028 {
3029// Mutex::Locker locker;
3030// if (locker.TryLock(m_input_reader_stack.GetMutex()))
3031// {
3032// if (m_input_reader_stack.IsEmpty())
3033// break;
3034// }
3035//
3036 EventSP event_sp;
3037 if (listener.WaitForEvent(NULL, event_sp))
3038 {
3039 if (event_sp)
3040 {
3041 Broadcaster *broadcaster = event_sp->GetBroadcaster();
3042 if (broadcaster)
3043 {
3044 uint32_t event_type = event_sp->GetType();
3045 ConstString broadcaster_class (broadcaster->GetBroadcasterClass());
3046 if (broadcaster_class == broadcaster_class_process)
3047 {
3048 HandleProcessEvent (event_sp);
3049 }
3050 else if (broadcaster_class == broadcaster_class_target)
3051 {
3052 if (Breakpoint::BreakpointEventData::GetEventDataFromEvent(event_sp.get()))
3053 {
3054 HandleBreakpointEvent (event_sp);
3055 }
3056 }
3057 else if (broadcaster_class == broadcaster_class_thread)
3058 {
3059 HandleThreadEvent (event_sp);
3060 }
3061 else if (broadcaster == m_command_interpreter_ap.get())
3062 {
3063 if (event_type & CommandInterpreter::eBroadcastBitQuitCommandReceived)
3064 {
3065 done = true;
3066 }
3067 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData)
3068 {
3069 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3070 if (data && data[0])
3071 {
3072 StreamFileSP error_sp (GetErrorFile());
3073 if (error_sp)
3074 {
3075 HideTopIOHandler();
3076 error_sp->PutCString(data);
3077 error_sp->Flush();
3078 RefreshTopIOHandler();
3079 }
3080 }
3081 }
3082 else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousOutputData)
3083 {
3084 const char *data = reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event_sp.get()));
3085 if (data && data[0])
3086 {
3087 StreamFileSP output_sp (GetOutputFile());
3088 if (output_sp)
3089 {
3090 HideTopIOHandler();
3091 output_sp->PutCString(data);
3092 output_sp->Flush();
3093 RefreshTopIOHandler();
3094 }
3095 }
3096 }
3097 }
3098 }
3099
3100 if (m_forward_listener_sp)
3101 m_forward_listener_sp->AddEvent(event_sp);
3102 }
3103 }
3104 }
3105}
3106
3107lldb::thread_result_t
3108Debugger::EventHandlerThread (lldb::thread_arg_t arg)
3109{
3110 ((Debugger *)arg)->DefaultEventHandler();
3111 return NULL;
3112}
3113
3114bool
3115Debugger::StartEventHandlerThread()
3116{
3117 if (!IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3118 m_event_handler_thread = Host::ThreadCreate("lldb.debugger.event-handler", EventHandlerThread, this, NULL);
3119 return IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread);
3120}
3121
3122void
3123Debugger::StopEventHandlerThread()
3124{
3125 if (IS_VALID_LLDB_HOST_THREAD(m_event_handler_thread))
3126 {
3127 GetCommandInterpreter().BroadcastEvent(CommandInterpreter::eBroadcastBitQuitCommandReceived);
3128 Host::ThreadJoin(m_event_handler_thread, NULL, NULL);
3129 m_event_handler_thread = LLDB_INVALID_HOST_THREAD;
3130 }
3131}
3132
3133
3134lldb::thread_result_t
3135Debugger::IOHandlerThread (lldb::thread_arg_t arg)
3136{
3137 Debugger *debugger = (Debugger *)arg;
3138 debugger->ExecuteIOHanders();
3139 debugger->StopEventHandlerThread();
3140 return NULL;
3141}
3142
3143bool
3144Debugger::StartIOHandlerThread()
3145{
3146 if (!IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3147 m_io_handler_thread = Host::ThreadCreate("lldb.debugger.io-handler", IOHandlerThread, this, NULL);
3148 return IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread);
3149}
3150
3151void
3152Debugger::StopIOHandlerThread()
3153{
3154 if (IS_VALID_LLDB_HOST_THREAD(m_io_handler_thread))
3155 {
3156 if (m_input_file_sp)
3157 m_input_file_sp->GetFile().Close();
3158 Host::ThreadJoin(m_io_handler_thread, NULL, NULL);
3159 m_io_handler_thread = LLDB_INVALID_HOST_THREAD;
3160 }
3161}
3162
3163