blob: b57c6051a961484d85cc6b9d832d288e05a04053 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/InputReader.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
Greg Claytone8cd0c92012-10-19 18:02:49 +000023#include "lldb/Core/PluginManager.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000024#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000026#include "lldb/Core/StreamAsynchronousIO.h"
Jim Ingham228063c2012-02-21 02:23:08 +000027#include "lldb/Core/StreamCallback.h"
Greg 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 Clayton4c054102012-09-01 00:38:36 +0000183 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
184 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
185 }
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000186 else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
187 {
188 // use-color changed. Ping the prompt so it can reset the ansi terminal codes.
189 SetPrompt (GetPrompt());
190 }
Enrico Granata397ddd52013-05-21 20:13:34 +0000191 else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
Enrico Granata84a53df2013-05-20 22:29:23 +0000192 {
Enrico Granata397ddd52013-05-21 20:13:34 +0000193 if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
Enrico Granata84a53df2013-05-20 22:29:23 +0000194 {
195 std::list<Error> errors;
Enrico Granata97303392013-05-21 00:00:30 +0000196 StreamString feedback_stream;
197 if (!target_sp->LoadScriptingResources(errors,&feedback_stream))
Enrico Granata84a53df2013-05-20 22:29:23 +0000198 {
199 for (auto error : errors)
200 {
Enrico Granata97303392013-05-21 00:00:30 +0000201 GetErrorStream().Printf("%s\n",error.AsCString());
Enrico Granata84a53df2013-05-20 22:29:23 +0000202 }
Enrico Granata97303392013-05-21 00:00:30 +0000203 if (feedback_stream.GetSize())
204 GetErrorStream().Printf("%s",feedback_stream.GetData());
Enrico Granata84a53df2013-05-20 22:29:23 +0000205 }
206 }
207 }
Greg Clayton4c054102012-09-01 00:38:36 +0000208 }
209 return error;
210}
211
Greg Clayton67cc0632012-08-22 17:17:09 +0000212bool
213Debugger::GetAutoConfirm () const
214{
215 const uint32_t idx = ePropertyAutoConfirm;
Greg Clayton754a9362012-08-23 00:22:02 +0000216 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000217}
218
219const char *
220Debugger::GetFrameFormat() const
221{
222 const uint32_t idx = ePropertyFrameFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000223 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000224}
225
226bool
227Debugger::GetNotifyVoid () const
228{
229 const uint32_t idx = ePropertyNotiftVoid;
Greg Clayton754a9362012-08-23 00:22:02 +0000230 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000231}
232
233const char *
234Debugger::GetPrompt() const
235{
236 const uint32_t idx = ePropertyPrompt;
Greg Clayton754a9362012-08-23 00:22:02 +0000237 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000238}
239
240void
241Debugger::SetPrompt(const char *p)
242{
243 const uint32_t idx = ePropertyPrompt;
244 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
245 const char *new_prompt = GetPrompt();
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000246 std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
247 if (str.length())
248 new_prompt = str.c_str();
Greg Clayton67cc0632012-08-22 17:17:09 +0000249 EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
250 GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
251}
252
253const char *
254Debugger::GetThreadFormat() const
255{
256 const uint32_t idx = ePropertyThreadFormat;
Greg Clayton754a9362012-08-23 00:22:02 +0000257 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, g_properties[idx].default_cstr_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000258}
259
260lldb::ScriptLanguage
261Debugger::GetScriptLanguage() const
262{
263 const uint32_t idx = ePropertyScriptLanguage;
Greg Clayton754a9362012-08-23 00:22:02 +0000264 return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000265}
266
267bool
268Debugger::SetScriptLanguage (lldb::ScriptLanguage script_lang)
269{
270 const uint32_t idx = ePropertyScriptLanguage;
271 return m_collection_sp->SetPropertyAtIndexAsEnumeration (NULL, idx, script_lang);
272}
273
274uint32_t
275Debugger::GetTerminalWidth () const
276{
277 const uint32_t idx = ePropertyTerminalWidth;
Greg Clayton754a9362012-08-23 00:22:02 +0000278 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000279}
280
281bool
282Debugger::SetTerminalWidth (uint32_t term_width)
283{
284 const uint32_t idx = ePropertyTerminalWidth;
285 return m_collection_sp->SetPropertyAtIndexAsSInt64 (NULL, idx, term_width);
286}
287
288bool
289Debugger::GetUseExternalEditor () const
290{
291 const uint32_t idx = ePropertyUseExternalEditor;
Greg Clayton754a9362012-08-23 00:22:02 +0000292 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000293}
294
295bool
296Debugger::SetUseExternalEditor (bool b)
297{
298 const uint32_t idx = ePropertyUseExternalEditor;
299 return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
300}
301
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000302bool
303Debugger::GetUseColor () const
304{
305 const uint32_t idx = ePropertyUseColor;
306 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
307}
308
309bool
310Debugger::SetUseColor (bool b)
311{
312 const uint32_t idx = ePropertyUseColor;
313 bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
314 SetPrompt (GetPrompt());
315 return ret;
316}
317
Greg Clayton67cc0632012-08-22 17:17:09 +0000318uint32_t
319Debugger::GetStopSourceLineCount (bool before) const
320{
321 const uint32_t idx = before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
Greg Clayton754a9362012-08-23 00:22:02 +0000322 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000323}
324
325Debugger::StopDisassemblyType
326Debugger::GetStopDisassemblyDisplay () const
327{
328 const uint32_t idx = ePropertyStopDisassemblyDisplay;
Greg Clayton754a9362012-08-23 00:22:02 +0000329 return (Debugger::StopDisassemblyType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000330}
331
332uint32_t
333Debugger::GetDisassemblyLineCount () const
334{
335 const uint32_t idx = ePropertyStopDisassemblyCount;
Greg Clayton754a9362012-08-23 00:22:02 +0000336 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +0000337}
Greg Claytone372b982011-11-21 21:44:34 +0000338
Enrico Granata553fad52013-10-25 23:09:40 +0000339bool
Enrico Granata90a8db32013-10-31 21:01:07 +0000340Debugger::GetAutoOneLineSummaries () const
Enrico Granata553fad52013-10-25 23:09:40 +0000341{
Enrico Granata90a8db32013-10-31 21:01:07 +0000342 const uint32_t idx = ePropertyAutoOneLineSummaries;
Enrico Granata553fad52013-10-25 23:09:40 +0000343 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, true);
344
345}
346
Greg Clayton1b654882010-09-19 02:33:57 +0000347#pragma mark Debugger
348
Greg Clayton67cc0632012-08-22 17:17:09 +0000349//const DebuggerPropertiesSP &
350//Debugger::GetSettings() const
351//{
352// return m_properties_sp;
353//}
354//
Greg Clayton99d0faf2010-11-18 23:32:35 +0000355
Caroline Tice2f88aad2011-01-14 00:29:16 +0000356int
357Debugger::TestDebuggerRefCount ()
358{
359 return g_shared_debugger_refcount;
360}
361
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362void
Greg Clayton5fb8f792013-12-02 19:35:49 +0000363Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000365 g_load_plugin_callback = load_plugin_callback;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000366 if (g_shared_debugger_refcount++ == 0)
Greg Claytondbe54502010-11-19 03:46:01 +0000367 lldb_private::Initialize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368}
369
370void
371Debugger::Terminate ()
372{
Greg Clayton66111032010-06-23 01:19:29 +0000373 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 {
Greg Clayton66111032010-06-23 01:19:29 +0000375 g_shared_debugger_refcount--;
376 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 {
Greg Claytondbe54502010-11-19 03:46:01 +0000378 lldb_private::WillTerminate();
379 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000380
381 // Clear our master list of debugger objects
382 Mutex::Locker locker (GetDebuggerListMutex ());
383 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 }
386}
387
Caroline Tice20bd37f2011-03-10 22:14:10 +0000388void
389Debugger::SettingsInitialize ()
390{
Greg Clayton6920b522012-08-22 18:39:03 +0000391 Target::SettingsInitialize ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000392}
393
394void
395Debugger::SettingsTerminate ()
396{
Greg Clayton6920b522012-08-22 18:39:03 +0000397 Target::SettingsTerminate ();
Caroline Tice20bd37f2011-03-10 22:14:10 +0000398}
399
Enrico Granata21dfcd92012-09-28 23:57:51 +0000400bool
Enrico Granatae743c782013-04-24 21:29:08 +0000401Debugger::LoadPlugin (const FileSpec& spec, Error& error)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000402{
Greg Clayton5fb8f792013-12-02 19:35:49 +0000403 if (g_load_plugin_callback)
Enrico Granatae743c782013-04-24 21:29:08 +0000404 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000405 lldb::DynamicLibrarySP dynlib_sp = g_load_plugin_callback (shared_from_this(), spec, error);
406 if (dynlib_sp)
407 {
408 m_loaded_plugins.push_back(dynlib_sp);
409 return true;
410 }
Enrico Granatae743c782013-04-24 21:29:08 +0000411 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000412 else
Enrico Granatae743c782013-04-24 21:29:08 +0000413 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000414 // The g_load_plugin_callback is registered in SBDebugger::Initialize()
415 // and if the public API layer isn't available (code is linking against
416 // all of the internal LLDB static libraries), then we can't load plugins
417 error.SetErrorString("Public API layer is not available");
Enrico Granatae743c782013-04-24 21:29:08 +0000418 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000419 return false;
420}
421
422static FileSpec::EnumerateDirectoryResult
423LoadPluginCallback
424(
425 void *baton,
426 FileSpec::FileType file_type,
427 const FileSpec &file_spec
428 )
429{
430 Error error;
431
432 static ConstString g_dylibext("dylib");
Michael Sartain3cf443d2013-07-17 00:26:30 +0000433 static ConstString g_solibext("so");
Enrico Granata21dfcd92012-09-28 23:57:51 +0000434
435 if (!baton)
436 return FileSpec::eEnumerateDirectoryResultQuit;
437
438 Debugger *debugger = (Debugger*)baton;
439
440 // If we have a regular file, a symbolic link or unknown file type, try
441 // and process the file. We must handle unknown as sometimes the directory
442 // enumeration might be enumerating a file system that doesn't have correct
443 // file type information.
444 if (file_type == FileSpec::eFileTypeRegular ||
445 file_type == FileSpec::eFileTypeSymbolicLink ||
446 file_type == FileSpec::eFileTypeUnknown )
447 {
448 FileSpec plugin_file_spec (file_spec);
449 plugin_file_spec.ResolvePath ();
450
Michael Sartain3cf443d2013-07-17 00:26:30 +0000451 if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
452 plugin_file_spec.GetFileNameExtension() != g_solibext)
453 {
Enrico Granata21dfcd92012-09-28 23:57:51 +0000454 return FileSpec::eEnumerateDirectoryResultNext;
Michael Sartain3cf443d2013-07-17 00:26:30 +0000455 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000456
Enrico Granatae743c782013-04-24 21:29:08 +0000457 Error plugin_load_error;
458 debugger->LoadPlugin (plugin_file_spec, plugin_load_error);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000459
460 return FileSpec::eEnumerateDirectoryResultNext;
461 }
462
463 else if (file_type == FileSpec::eFileTypeUnknown ||
464 file_type == FileSpec::eFileTypeDirectory ||
465 file_type == FileSpec::eFileTypeSymbolicLink )
466 {
467 // Try and recurse into anything that a directory or symbolic link.
468 // We must also do this for unknown as sometimes the directory enumeration
469 // might be enurating a file system that doesn't have correct file type
470 // information.
471 return FileSpec::eEnumerateDirectoryResultEnter;
472 }
473
474 return FileSpec::eEnumerateDirectoryResultNext;
475}
476
477void
478Debugger::InstanceInitialize ()
479{
480 FileSpec dir_spec;
481 const bool find_directories = true;
482 const bool find_files = true;
483 const bool find_other = true;
484 char dir_path[PATH_MAX];
485 if (Host::GetLLDBPath (ePathTypeLLDBSystemPlugins, dir_spec))
486 {
487 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
488 {
489 FileSpec::EnumerateDirectory (dir_path,
490 find_directories,
491 find_files,
492 find_other,
493 LoadPluginCallback,
494 this);
495 }
496 }
497
498 if (Host::GetLLDBPath (ePathTypeLLDBUserPlugins, dir_spec))
499 {
500 if (dir_spec.Exists() && dir_spec.GetPath(dir_path, sizeof(dir_path)))
501 {
502 FileSpec::EnumerateDirectory (dir_path,
503 find_directories,
504 find_files,
505 find_other,
506 LoadPluginCallback,
507 this);
508 }
509 }
Greg Claytone8cd0c92012-10-19 18:02:49 +0000510
511 PluginManager::DebuggerInitialize (*this);
Enrico Granata21dfcd92012-09-28 23:57:51 +0000512}
513
Greg Clayton66111032010-06-23 01:19:29 +0000514DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000515Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000516{
Jim Ingham228063c2012-02-21 02:23:08 +0000517 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Claytonc15f55e2012-03-30 20:53:46 +0000518 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000519 {
520 Mutex::Locker locker (GetDebuggerListMutex ());
521 GetDebuggerList().push_back(debugger_sp);
522 }
Enrico Granata21dfcd92012-09-28 23:57:51 +0000523 debugger_sp->InstanceInitialize ();
Greg Clayton66111032010-06-23 01:19:29 +0000524 return debugger_sp;
525}
526
Caroline Ticee02657b2011-01-22 01:02:07 +0000527void
Greg Clayton4d122c42011-09-17 08:33:22 +0000528Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000529{
530 if (debugger_sp.get() == NULL)
531 return;
532
Jim Ingham8314c522011-09-15 21:36:42 +0000533 debugger_sp->Clear();
534
Greg Claytonc15f55e2012-03-30 20:53:46 +0000535 if (g_shared_debugger_refcount > 0)
Caroline Ticee02657b2011-01-22 01:02:07 +0000536 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000537 Mutex::Locker locker (GetDebuggerListMutex ());
538 DebuggerList &debugger_list = GetDebuggerList ();
539 DebuggerList::iterator pos, end = debugger_list.end();
540 for (pos = debugger_list.begin (); pos != end; ++pos)
Caroline Ticee02657b2011-01-22 01:02:07 +0000541 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000542 if ((*pos).get() == debugger_sp.get())
543 {
544 debugger_list.erase (pos);
545 return;
546 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000547 }
548 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000549}
550
Greg Clayton4d122c42011-09-17 08:33:22 +0000551DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000552Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
553{
Greg Clayton4d122c42011-09-17 08:33:22 +0000554 DebuggerSP debugger_sp;
Greg Clayton6920b522012-08-22 18:39:03 +0000555 if (g_shared_debugger_refcount > 0)
556 {
557 Mutex::Locker locker (GetDebuggerListMutex ());
558 DebuggerList &debugger_list = GetDebuggerList();
559 DebuggerList::iterator pos, end = debugger_list.end();
560
561 for (pos = debugger_list.begin(); pos != end; ++pos)
562 {
563 if ((*pos).get()->m_instance_name == instance_name)
564 {
565 debugger_sp = *pos;
566 break;
567 }
568 }
569 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000570 return debugger_sp;
571}
Greg Clayton66111032010-06-23 01:19:29 +0000572
573TargetSP
574Debugger::FindTargetWithProcessID (lldb::pid_t pid)
575{
Greg Clayton4d122c42011-09-17 08:33:22 +0000576 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000577 if (g_shared_debugger_refcount > 0)
Greg Clayton66111032010-06-23 01:19:29 +0000578 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000579 Mutex::Locker locker (GetDebuggerListMutex ());
580 DebuggerList &debugger_list = GetDebuggerList();
581 DebuggerList::iterator pos, end = debugger_list.end();
582 for (pos = debugger_list.begin(); pos != end; ++pos)
583 {
584 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
585 if (target_sp)
586 break;
587 }
Greg Clayton66111032010-06-23 01:19:29 +0000588 }
589 return target_sp;
590}
591
Greg Claytone4e45922011-11-16 05:37:56 +0000592TargetSP
593Debugger::FindTargetWithProcess (Process *process)
594{
595 TargetSP target_sp;
Greg Claytonc15f55e2012-03-30 20:53:46 +0000596 if (g_shared_debugger_refcount > 0)
Greg Claytone4e45922011-11-16 05:37:56 +0000597 {
Greg Claytonc15f55e2012-03-30 20:53:46 +0000598 Mutex::Locker locker (GetDebuggerListMutex ());
599 DebuggerList &debugger_list = GetDebuggerList();
600 DebuggerList::iterator pos, end = debugger_list.end();
601 for (pos = debugger_list.begin(); pos != end; ++pos)
602 {
603 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
604 if (target_sp)
605 break;
606 }
Greg Claytone4e45922011-11-16 05:37:56 +0000607 }
608 return target_sp;
609}
610
Jim Ingham228063c2012-02-21 02:23:08 +0000611Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000612 UserID (g_unique_id++),
Greg Clayton67cc0632012-08-22 17:17:09 +0000613 Properties(OptionValuePropertiesSP(new OptionValueProperties())),
Greg Claytond46c87a2010-12-04 02:39:47 +0000614 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 m_input_file (),
616 m_output_file (),
617 m_error_file (),
Jim Inghamc5917d92012-11-30 20:23:19 +0000618 m_terminal_state (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000619 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000620 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 m_listener ("lldb.Debugger"),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000622 m_source_manager_ap(),
Jim Inghame37d6052011-09-13 00:29:56 +0000623 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000624 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000625 m_input_reader_stack (),
Greg Clayton67cc0632012-08-22 17:17:09 +0000626 m_input_reader_data (),
627 m_instance_name()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628{
Greg Clayton67cc0632012-08-22 17:17:09 +0000629 char instance_cstr[256];
630 snprintf(instance_cstr, sizeof(instance_cstr), "debugger_%d", (int)GetID());
631 m_instance_name.SetCString(instance_cstr);
Jim Ingham228063c2012-02-21 02:23:08 +0000632 if (log_callback)
633 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000634 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000635 // Always add our default platform to the platform list
636 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
637 assert (default_platform_sp.get());
638 m_platform_list.Append (default_platform_sp, true);
Greg Clayton67cc0632012-08-22 17:17:09 +0000639
Greg Clayton754a9362012-08-23 00:22:02 +0000640 m_collection_sp->Initialize (g_properties);
Greg Clayton67cc0632012-08-22 17:17:09 +0000641 m_collection_sp->AppendProperty (ConstString("target"),
642 ConstString("Settings specify to debugging targets."),
643 true,
644 Target::GetGlobalProperties()->GetValueProperties());
Greg Clayton754a9362012-08-23 00:22:02 +0000645 if (m_command_interpreter_ap.get())
646 {
647 m_collection_sp->AppendProperty (ConstString("interpreter"),
648 ConstString("Settings specify to the debugger's command interpreter."),
649 true,
650 m_command_interpreter_ap->GetValueProperties());
651 }
Greg Clayton67cc0632012-08-22 17:17:09 +0000652 OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
653 term_width->SetMinimumValue(10);
654 term_width->SetMaximumValue(1024);
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000655
656 // Turn off use-color if this is a dumb terminal.
657 const char *term = getenv ("TERM");
658 if (term && !strcmp (term, "dumb"))
659 SetUseColor (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660}
661
662Debugger::~Debugger ()
663{
Jim Ingham8314c522011-09-15 21:36:42 +0000664 Clear();
665}
666
667void
668Debugger::Clear()
669{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000670 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000671 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000672 int num_targets = m_target_list.GetNumTargets();
673 for (int i = 0; i < num_targets; i++)
674 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000675 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
676 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000677 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000678 ProcessSP process_sp (target_sp->GetProcessSP());
679 if (process_sp)
Jim Ingham1fd07052013-02-27 19:13:05 +0000680 process_sp->Finalize();
Greg Claytonccbc08e2012-01-14 17:04:19 +0000681 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000682 }
Greg Clayton66111032010-06-23 01:19:29 +0000683 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000684 BroadcasterManager::Clear ();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000685
686 // Close the input file _before_ we close the input read communications class
687 // as it does NOT own the input file, our m_input_file does.
Jim Inghamc5917d92012-11-30 20:23:19 +0000688 m_terminal_state.Clear();
Greg Clayton0d69a3a2012-05-16 00:11:54 +0000689 GetInputFile().Close ();
690 // Now that we have closed m_input_file, we can now tell our input communication
691 // class to close down. Its read thread should quickly exit after we close
692 // the input file handle above.
693 m_input_comm.Clear ();
Jim Ingham8314c522011-09-15 21:36:42 +0000694}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695
696bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000697Debugger::GetCloseInputOnEOF () const
698{
699 return m_input_comm.GetCloseOnEOF();
700}
701
702void
703Debugger::SetCloseInputOnEOF (bool b)
704{
705 m_input_comm.SetCloseOnEOF(b);
706}
707
708bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709Debugger::GetAsyncExecution ()
710{
Greg Clayton66111032010-06-23 01:19:29 +0000711 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712}
713
714void
715Debugger::SetAsyncExecution (bool async_execution)
716{
Greg Clayton66111032010-06-23 01:19:29 +0000717 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718}
719
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720
721void
722Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
723{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000724 File &in_file = GetInputFile();
725 in_file.SetStream (fh, tranfer_ownership);
726 if (in_file.IsValid() == false)
727 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728
729 // Disconnect from any old connection if we had one
730 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000731 // Pass false as the second argument to ConnectionFileDescriptor below because
732 // our "in_file" above will already take ownership if requested and we don't
733 // want to objects trying to own and close a file descriptor.
734 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
Jim Inghamc5917d92012-11-30 20:23:19 +0000736
737 // Save away the terminal state if that is relevant, so that we can restore it in RestoreInputState.
738 SaveInputTerminalState ();
739
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740 Error error;
741 if (m_input_comm.StartReadThread (&error) == false)
742 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000743 File &err_file = GetErrorFile();
744
745 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
746 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748}
749
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750void
751Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
752{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000753 File &out_file = GetOutputFile();
754 out_file.SetStream (fh, tranfer_ownership);
755 if (out_file.IsValid() == false)
756 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000757
Enrico Granatab5887262012-10-29 21:18:03 +0000758 // do not create the ScriptInterpreter just for setting the output file handle
759 // as the constructor will know how to do the right thing on its own
760 const bool can_create = false;
761 ScriptInterpreter* script_interpreter = GetCommandInterpreter().GetScriptInterpreter(can_create);
762 if (script_interpreter)
763 script_interpreter->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764}
765
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766void
767Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
768{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000769 File &err_file = GetErrorFile();
770 err_file.SetStream (fh, tranfer_ownership);
771 if (err_file.IsValid() == false)
772 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773}
774
Jim Inghamc5917d92012-11-30 20:23:19 +0000775void
776Debugger::SaveInputTerminalState ()
777{
778 File &in_file = GetInputFile();
779 if (in_file.GetDescriptor() != File::kInvalidDescriptor)
780 m_terminal_state.Save(in_file.GetDescriptor(), true);
781}
782
783void
784Debugger::RestoreInputTerminalState ()
785{
786 m_terminal_state.Restore();
787}
788
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000790Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791{
792 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000793 TargetSP target_sp(GetSelectedTarget());
794 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795
796 if (target_sp)
797 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000798 ProcessSP process_sp (target_sp->GetProcessSP());
799 exe_ctx.SetProcessSP (process_sp);
800 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000802 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
803 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000805 exe_ctx.SetThreadSP (thread_sp);
806 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
807 if (exe_ctx.GetFramePtr() == NULL)
808 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809 }
810 }
811 }
812 return exe_ctx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813}
814
Caroline Ticeb44880c2011-02-10 01:15:13 +0000815InputReaderSP
816Debugger::GetCurrentInputReader ()
817{
818 InputReaderSP reader_sp;
819
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000820 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000821 {
822 // Clear any finished readers from the stack
823 while (CheckIfTopInputReaderIsDone()) ;
824
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000825 if (!m_input_reader_stack.IsEmpty())
826 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000827 }
828
829 return reader_sp;
830}
831
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832void
833Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
834{
Caroline Ticeefed6132010-11-19 20:47:54 +0000835 if (bytes_len > 0)
836 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
837 else
838 ((Debugger *)baton)->DispatchInputEndOfFile ();
839}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840
841
842void
843Debugger::DispatchInput (const char *bytes, size_t bytes_len)
844{
Caroline Ticeefed6132010-11-19 20:47:54 +0000845 if (bytes == NULL || bytes_len == 0)
846 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847
848 WriteToDefaultReader (bytes, bytes_len);
849}
850
851void
Caroline Ticeefed6132010-11-19 20:47:54 +0000852Debugger::DispatchInputInterrupt ()
853{
854 m_input_reader_data.clear();
855
Caroline Ticeb44880c2011-02-10 01:15:13 +0000856 InputReaderSP reader_sp (GetCurrentInputReader ());
857 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000858 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000859 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000860
Caroline Ticeb44880c2011-02-10 01:15:13 +0000861 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000862 while (CheckIfTopInputReaderIsDone ()) ;
863 }
864}
865
866void
867Debugger::DispatchInputEndOfFile ()
868{
869 m_input_reader_data.clear();
870
Caroline Ticeb44880c2011-02-10 01:15:13 +0000871 InputReaderSP reader_sp (GetCurrentInputReader ());
872 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000873 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000874 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000875
Caroline Ticeb44880c2011-02-10 01:15:13 +0000876 // If notifying the reader of the end-of-file finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000877 while (CheckIfTopInputReaderIsDone ()) ;
878 }
879}
880
881void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000882Debugger::CleanUpInputReaders ()
883{
884 m_input_reader_data.clear();
885
Caroline Ticeb44880c2011-02-10 01:15:13 +0000886 // The bottom input reader should be the main debugger input reader. We do not want to close that one here.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000887 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000888 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000889 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000890 if (reader_sp)
891 {
892 reader_sp->Notify (eInputReaderEndOfFile);
893 reader_sp->SetIsDone (true);
894 }
895 }
896}
897
898void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000899Debugger::NotifyTopInputReader (InputReaderAction notification)
900{
901 InputReaderSP reader_sp (GetCurrentInputReader());
902 if (reader_sp)
903 {
904 reader_sp->Notify (notification);
905
906 // Flush out any input readers that are done.
907 while (CheckIfTopInputReaderIsDone ())
908 /* Do nothing. */;
909 }
910}
911
Caroline Tice9088b062011-05-09 23:06:58 +0000912bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000913Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000914{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000915 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000916
Caroline Ticed61c10b2011-06-16 16:27:19 +0000917 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000918}
919
920
Caroline Tice969ed3d2011-05-02 20:41:46 +0000921void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000922Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
923{
924 if (bytes && bytes_len)
925 m_input_reader_data.append (bytes, bytes_len);
926
927 if (m_input_reader_data.empty())
928 return;
929
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000930 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000933 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934 if (!reader_sp)
935 break;
936
Greg Clayton471b31c2010-07-20 22:52:08 +0000937 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938 m_input_reader_data.size());
939 if (bytes_handled)
940 {
941 m_input_reader_data.erase (0, bytes_handled);
942 }
943 else
944 {
945 // No bytes were handled, we might not have reached our
946 // granularity, just return and wait for more data
947 break;
948 }
949 }
950
Caroline Ticeb44880c2011-02-10 01:15:13 +0000951 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952 while (CheckIfTopInputReaderIsDone ())
953 /* Do nothing. */;
954
955}
956
957void
958Debugger::PushInputReader (const InputReaderSP& reader_sp)
959{
960 if (!reader_sp)
961 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000962
963 // Deactivate the old top reader
964 InputReaderSP top_reader_sp (GetCurrentInputReader ());
965
966 if (top_reader_sp)
967 top_reader_sp->Notify (eInputReaderDeactivate);
968
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000969 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000970 reader_sp->Notify (eInputReaderActivate);
971 ActivateInputReader (reader_sp);
972}
973
974bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000975Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976{
977 bool result = false;
978
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 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000983 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000984 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985
986 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
987 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000988 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989 reader_sp->Notify (eInputReaderDeactivate);
990 reader_sp->Notify (eInputReaderDone);
991 result = true;
992
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000993 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000995 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 if (reader_sp)
997 {
998 ActivateInputReader (reader_sp);
999 reader_sp->Notify (eInputReaderReactivate);
1000 }
1001 }
1002 }
1003 }
1004 return result;
1005}
1006
1007bool
1008Debugger::CheckIfTopInputReaderIsDone ()
1009{
1010 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001011 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 {
Caroline Ticeb44880c2011-02-10 01:15:13 +00001013 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +00001014 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015
1016 if (reader_sp && reader_sp->IsDone())
1017 {
1018 result = true;
1019 PopInputReader (reader_sp);
1020 }
1021 }
1022 return result;
1023}
1024
1025void
1026Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
1027{
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001028 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001030 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +00001032 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +00001033
1034 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001035
Greg Claytona3406612011-02-07 23:24:47 +00001036 switch (reader_sp->GetGranularity())
1037 {
1038 case eInputReaderGranularityByte:
1039 case eInputReaderGranularityWord:
1040 tty.SetCanonical (false);
1041 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042
Greg Claytona3406612011-02-07 23:24:47 +00001043 case eInputReaderGranularityLine:
1044 case eInputReaderGranularityAll:
1045 tty.SetCanonical (true);
1046 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047
Greg Claytona3406612011-02-07 23:24:47 +00001048 default:
1049 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050 }
1051 }
1052}
Greg Clayton66111032010-06-23 01:19:29 +00001053
Jim Ingham5b52f0c2011-06-02 23:58:26 +00001054StreamSP
1055Debugger::GetAsyncOutputStream ()
1056{
1057 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1058 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
1059}
1060
1061StreamSP
1062Debugger::GetAsyncErrorStream ()
1063{
1064 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
1065 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
1066}
1067
Greg Claytonc7bece562013-01-25 18:06:21 +00001068size_t
Enrico Granata061858c2012-02-15 02:34:21 +00001069Debugger::GetNumDebuggers()
1070{
Greg Claytonc15f55e2012-03-30 20:53:46 +00001071 if (g_shared_debugger_refcount > 0)
1072 {
1073 Mutex::Locker locker (GetDebuggerListMutex ());
1074 return GetDebuggerList().size();
1075 }
1076 return 0;
Enrico Granata061858c2012-02-15 02:34:21 +00001077}
1078
1079lldb::DebuggerSP
Greg Claytonc7bece562013-01-25 18:06:21 +00001080Debugger::GetDebuggerAtIndex (size_t index)
Enrico Granata061858c2012-02-15 02:34:21 +00001081{
1082 DebuggerSP debugger_sp;
1083
Greg Claytonc15f55e2012-03-30 20:53:46 +00001084 if (g_shared_debugger_refcount > 0)
1085 {
1086 Mutex::Locker locker (GetDebuggerListMutex ());
1087 DebuggerList &debugger_list = GetDebuggerList();
Enrico Granata061858c2012-02-15 02:34:21 +00001088
Greg Claytonc15f55e2012-03-30 20:53:46 +00001089 if (index < debugger_list.size())
1090 debugger_sp = debugger_list[index];
1091 }
1092
Enrico Granata061858c2012-02-15 02:34:21 +00001093 return debugger_sp;
1094}
1095
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001096DebuggerSP
1097Debugger::FindDebuggerWithID (lldb::user_id_t id)
1098{
Greg Clayton4d122c42011-09-17 08:33:22 +00001099 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001100
Greg Claytonc15f55e2012-03-30 20:53:46 +00001101 if (g_shared_debugger_refcount > 0)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001102 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001103 Mutex::Locker locker (GetDebuggerListMutex ());
1104 DebuggerList &debugger_list = GetDebuggerList();
1105 DebuggerList::iterator pos, end = debugger_list.end();
1106 for (pos = debugger_list.begin(); pos != end; ++pos)
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001107 {
Greg Claytonc15f55e2012-03-30 20:53:46 +00001108 if ((*pos).get()->GetID() == id)
1109 {
1110 debugger_sp = *pos;
1111 break;
1112 }
Caroline Ticeebc1bb22010-06-30 16:22:25 +00001113 }
1114 }
1115 return debugger_sp;
1116}
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001117
Greg Clayton1b654882010-09-19 02:33:57 +00001118static void
Jason Molendab57e4a12013-11-04 09:33:30 +00001119TestPromptFormats (StackFrame *frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001120{
1121 if (frame == NULL)
1122 return;
1123
1124 StreamString s;
1125 const char *prompt_format =
1126 "{addr = '${addr}'\n}"
1127 "{process.id = '${process.id}'\n}"
1128 "{process.name = '${process.name}'\n}"
1129 "{process.file.basename = '${process.file.basename}'\n}"
1130 "{process.file.fullpath = '${process.file.fullpath}'\n}"
1131 "{thread.id = '${thread.id}'\n}"
1132 "{thread.index = '${thread.index}'\n}"
1133 "{thread.name = '${thread.name}'\n}"
1134 "{thread.queue = '${thread.queue}'\n}"
1135 "{thread.stop-reason = '${thread.stop-reason}'\n}"
1136 "{target.arch = '${target.arch}'\n}"
1137 "{module.file.basename = '${module.file.basename}'\n}"
1138 "{module.file.fullpath = '${module.file.fullpath}'\n}"
1139 "{file.basename = '${file.basename}'\n}"
1140 "{file.fullpath = '${file.fullpath}'\n}"
1141 "{frame.index = '${frame.index}'\n}"
1142 "{frame.pc = '${frame.pc}'\n}"
1143 "{frame.sp = '${frame.sp}'\n}"
1144 "{frame.fp = '${frame.fp}'\n}"
1145 "{frame.flags = '${frame.flags}'\n}"
1146 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
1147 "{frame.reg.rip = '${frame.reg.rip}'\n}"
1148 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
1149 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
1150 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
1151 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
1152 "{frame.reg.carp = '${frame.reg.carp}'\n}"
1153 "{function.id = '${function.id}'\n}"
1154 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +00001155 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +00001156 "{function.addr-offset = '${function.addr-offset}'\n}"
1157 "{function.line-offset = '${function.line-offset}'\n}"
1158 "{function.pc-offset = '${function.pc-offset}'\n}"
1159 "{line.file.basename = '${line.file.basename}'\n}"
1160 "{line.file.fullpath = '${line.file.fullpath}'\n}"
1161 "{line.number = '${line.number}'\n}"
1162 "{line.start-addr = '${line.start-addr}'\n}"
1163 "{line.end-addr = '${line.end-addr}'\n}"
1164;
1165
1166 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
1167 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +00001168 frame->CalculateExecutionContext(exe_ctx);
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001169 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
Greg Clayton1b654882010-09-19 02:33:57 +00001170 {
1171 printf("%s\n", s.GetData());
1172 }
1173 else
1174 {
Greg Clayton1b654882010-09-19 02:33:57 +00001175 printf ("what we got: %s\n", s.GetData());
1176 }
1177}
1178
Enrico Granata9fc19442011-07-06 02:13:41 +00001179static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001180ScanFormatDescriptor (const char* var_name_begin,
1181 const char* var_name_end,
1182 const char** var_name_final,
1183 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +00001184 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +00001185 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +00001186{
Greg Clayton5160ce52013-03-27 23:08:40 +00001187 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001188 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +00001189 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +00001190 {
1191 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001192 log->Printf("[ScanFormatDescriptor] no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +00001193 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +00001194 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001195 else
1196 {
1197 *var_name_final = *percent_position;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001198 std::string format_name(*var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +00001199 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001200 log->Printf("[ScanFormatDescriptor] parsing %s as a format descriptor", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001201 if ( !FormatManager::GetFormatFromCString(format_name.c_str(),
Enrico Granata9fc19442011-07-06 02:13:41 +00001202 true,
1203 *custom_format) )
1204 {
Enrico Granatae992a082011-07-22 17:03:19 +00001205 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001206 log->Printf("[ScanFormatDescriptor] %s is an unknown format", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001207
1208 switch (format_name.front())
1209 {
1210 case '@': // if this is an @ sign, print ObjC description
1211 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
1212 break;
1213 case 'V': // if this is a V, print the value using the default format
1214 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1215 break;
1216 case 'L': // if this is an L, print the location of the value
1217 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
1218 break;
1219 case 'S': // if this is an S, print the summary after all
1220 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
1221 break;
1222 case '#': // if this is a '#', print the number of children
1223 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
1224 break;
1225 case 'T': // if this is a 'T', print the type
1226 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
1227 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001228 case 'N': // if this is a 'N', print the name
1229 *val_obj_display = ValueObject::eValueObjectRepresentationStyleName;
1230 break;
1231 case '>': // if this is a '>', print the name
1232 *val_obj_display = ValueObject::eValueObjectRepresentationStyleExpressionPath;
1233 break;
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001234 default:
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001235 if (log)
1236 log->Printf("ScanFormatDescriptor] %s is an error, leaving the previous value alone", format_name.c_str());
Enrico Granata36aa5ae2013-05-06 17:18:22 +00001237 break;
1238 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001239 }
1240 // a good custom format tells us to print the value using it
1241 else
Enrico Granatae992a082011-07-22 17:03:19 +00001242 {
1243 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001244 log->Printf("[ScanFormatDescriptor] will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +00001245 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +00001246 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001247 }
Enrico Granatae992a082011-07-22 17:03:19 +00001248 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001249 log->Printf("[ScanFormatDescriptor] final format description outcome: custom_format = %d, val_obj_display = %d",
Enrico Granatae992a082011-07-22 17:03:19 +00001250 *custom_format,
1251 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +00001252 return true;
1253}
1254
1255static bool
Enrico Granatadc940732011-08-23 00:32:52 +00001256ScanBracketedRange (const char* var_name_begin,
1257 const char* var_name_end,
1258 const char* var_name_final,
1259 const char** open_bracket_position,
1260 const char** separator_position,
1261 const char** close_bracket_position,
1262 const char** var_name_final_if_array_range,
1263 int64_t* index_lower,
1264 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +00001265{
Greg Clayton5160ce52013-03-27 23:08:40 +00001266 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +00001267 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +00001268 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +00001269 {
1270 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1271 *close_bracket_position = ::strchr(*open_bracket_position,']');
1272 // as usual, we assume that [] will come before %
1273 //printf("trying to expand a []\n");
1274 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +00001275 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +00001276 {
Enrico Granatae992a082011-07-22 17:03:19 +00001277 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001278 log->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +00001279 *index_lower = 0;
1280 }
1281 else if (*separator_position == NULL || *separator_position > var_name_end)
1282 {
1283 char *end = NULL;
1284 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1285 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +00001286 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001287 log->Printf("[ScanBracketedRange] [%" PRId64 "] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +00001288 }
Greg Clayton34132752011-07-06 04:07:21 +00001289 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +00001290 {
1291 char *end = NULL;
1292 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
1293 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +00001294 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001295 log->Printf("[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +00001296 }
1297 else
Enrico Granatae992a082011-07-22 17:03:19 +00001298 {
1299 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001300 log->Printf("[ScanBracketedRange] expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +00001301 return false;
Enrico Granatae992a082011-07-22 17:03:19 +00001302 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001303 if (*index_lower > *index_higher && *index_higher > 0)
1304 {
Enrico Granatae992a082011-07-22 17:03:19 +00001305 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001306 log->Printf("[ScanBracketedRange] swapping indices");
Greg Claytonc7bece562013-01-25 18:06:21 +00001307 int64_t temp = *index_lower;
Enrico Granata9fc19442011-07-06 02:13:41 +00001308 *index_lower = *index_higher;
1309 *index_higher = temp;
1310 }
1311 }
Enrico Granatae992a082011-07-22 17:03:19 +00001312 else if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001313 log->Printf("[ScanBracketedRange] no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +00001314 return true;
1315}
1316
Michael Sartain0769b2b2013-07-30 16:44:36 +00001317template <typename T>
1318static bool RunScriptFormatKeyword(Stream &s, ScriptInterpreter *script_interpreter, T t, const std::string& script_name)
1319{
1320 if (script_interpreter)
1321 {
1322 Error script_error;
1323 std::string script_output;
1324
1325 if (script_interpreter->RunScriptFormatKeyword(script_name.c_str(), t, script_output, script_error) && script_error.Success())
1326 {
1327 s.Printf("%s", script_output.c_str());
1328 return true;
1329 }
1330 else
1331 {
1332 s.Printf("<error: %s>",script_error.AsCString());
1333 }
1334 }
1335 return false;
1336}
1337
Enrico Granata9fc19442011-07-06 02:13:41 +00001338static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001339ExpandIndexedExpression (ValueObject* valobj,
Greg Claytonc7bece562013-01-25 18:06:21 +00001340 size_t index,
Jason Molendab57e4a12013-11-04 09:33:30 +00001341 StackFrame* frame,
Enrico Granatadc940732011-08-23 00:32:52 +00001342 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001343{
Greg Clayton5160ce52013-03-27 23:08:40 +00001344 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001345 const char* ptr_deref_format = "[%d]";
Enrico Granata599171a2013-02-01 23:59:44 +00001346 std::string ptr_deref_buffer(10,0);
1347 ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001348 if (log)
Enrico Granata599171a2013-02-01 23:59:44 +00001349 log->Printf("[ExpandIndexedExpression] name to deref: %s",ptr_deref_buffer.c_str());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001350 const char* first_unparsed;
1351 ValueObject::GetValueForExpressionPathOptions options;
1352 ValueObject::ExpressionPathEndResultType final_value_type;
1353 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001354 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granata599171a2013-02-01 23:59:44 +00001355 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001356 &first_unparsed,
1357 &reason_to_stop,
1358 &final_value_type,
1359 options,
1360 &what_next);
1361 if (!item)
1362 {
Enrico Granatae992a082011-07-22 17:03:19 +00001363 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001364 log->Printf("[ExpandIndexedExpression] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001365 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001366 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001367 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001368 else
1369 {
Enrico Granatae992a082011-07-22 17:03:19 +00001370 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001371 log->Printf("[ExpandIndexedExpression] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001372 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001373 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001374 }
1375 return item;
1376}
1377
Michael Sartain0769b2b2013-07-30 16:44:36 +00001378static inline bool
1379IsToken(const char *var_name_begin, const char *var)
1380{
1381 return (::strncmp (var_name_begin, var, strlen(var)) == 0);
1382}
1383
1384static bool
1385IsTokenWithFormat(const char *var_name_begin, const char *var, std::string &format, const char *default_format,
1386 const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1387{
1388 int var_len = strlen(var);
1389 if (::strncmp (var_name_begin, var, var_len) == 0)
1390 {
1391 var_name_begin += var_len;
1392 if (*var_name_begin == '}')
1393 {
1394 format = default_format;
1395 return true;
1396 }
1397 else if (*var_name_begin == '%')
1398 {
1399 // Allow format specifiers: x|X|u with optional width specifiers.
1400 // ${thread.id%x} ; hex
1401 // ${thread.id%X} ; uppercase hex
1402 // ${thread.id%u} ; unsigned decimal
1403 // ${thread.id%8.8X} ; width.precision + specifier
1404 // ${thread.id%tid} ; unsigned on FreeBSD/Linux, otherwise default_format (0x%4.4x for thread.id)
1405 int dot_count = 0;
1406 const char *specifier = NULL;
1407 int width_precision_length = 0;
1408 const char *width_precision = ++var_name_begin;
1409 while (isdigit(*var_name_begin) || *var_name_begin == '.')
1410 {
1411 dot_count += (*var_name_begin == '.');
1412 if (dot_count > 1)
1413 break;
1414 var_name_begin++;
1415 width_precision_length++;
1416 }
1417
1418 if (IsToken (var_name_begin, "tid}"))
1419 {
1420 Target *target = Target::GetTargetFromContexts (exe_ctx_ptr, sc_ptr);
1421 if (target)
1422 {
1423 ArchSpec arch (target->GetArchitecture ());
1424 llvm::Triple::OSType ostype = arch.IsValid() ? arch.GetTriple().getOS() : llvm::Triple::UnknownOS;
1425 if ((ostype == llvm::Triple::FreeBSD) || (ostype == llvm::Triple::Linux))
1426 specifier = PRIu64;
1427 }
1428 if (!specifier)
1429 {
1430 format = default_format;
1431 return true;
1432 }
1433 }
1434 else if (IsToken (var_name_begin, "x}"))
1435 specifier = PRIx64;
1436 else if (IsToken (var_name_begin, "X}"))
1437 specifier = PRIX64;
1438 else if (IsToken (var_name_begin, "u}"))
1439 specifier = PRIu64;
1440
1441 if (specifier)
1442 {
1443 format = "%";
1444 if (width_precision_length)
1445 format += std::string(width_precision, width_precision_length);
1446 format += specifier;
1447 return true;
1448 }
1449 }
1450 }
1451 return false;
1452}
1453
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001454static bool
1455FormatPromptRecurse
Greg Clayton1b654882010-09-19 02:33:57 +00001456(
1457 const char *format,
1458 const SymbolContext *sc,
1459 const ExecutionContext *exe_ctx,
1460 const Address *addr,
1461 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001462 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001463 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001464)
1465{
Enrico Granatac482a192011-08-17 22:13:59 +00001466 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001467 bool success = true;
1468 const char *p;
Greg Clayton5160ce52013-03-27 23:08:40 +00001469 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001470
Greg Clayton1b654882010-09-19 02:33:57 +00001471 for (p = format; *p != '\0'; ++p)
1472 {
Enrico Granatac482a192011-08-17 22:13:59 +00001473 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001474 {
Enrico Granatac482a192011-08-17 22:13:59 +00001475 valobj = realvalobj;
1476 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001477 }
Greg Clayton1b654882010-09-19 02:33:57 +00001478 size_t non_special_chars = ::strcspn (p, "${}\\");
1479 if (non_special_chars > 0)
1480 {
1481 if (success)
1482 s.Write (p, non_special_chars);
1483 p += non_special_chars;
1484 }
1485
1486 if (*p == '\0')
1487 {
1488 break;
1489 }
1490 else if (*p == '{')
1491 {
1492 // Start a new scope that must have everything it needs if it is to
1493 // to make it into the final output stream "s". If you want to make
1494 // a format that only prints out the function or symbol name if there
1495 // is one in the symbol context you can use:
1496 // "{function =${function.name}}"
1497 // The first '{' starts a new scope that end with the matching '}' at
1498 // the end of the string. The contents "function =${function.name}"
1499 // will then be evaluated and only be output if there is a function
1500 // or symbol with a valid name.
1501 StreamString sub_strm;
1502
1503 ++p; // Skip the '{'
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001504
1505 if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001506 {
1507 // The stream had all it needed
1508 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1509 }
1510 if (*p != '}')
1511 {
1512 success = false;
1513 break;
1514 }
1515 }
1516 else if (*p == '}')
1517 {
1518 // End of a enclosing scope
1519 break;
1520 }
1521 else if (*p == '$')
1522 {
1523 // We have a prompt variable to print
1524 ++p;
1525 if (*p == '{')
1526 {
1527 ++p;
1528 const char *var_name_begin = p;
1529 const char *var_name_end = ::strchr (p, '}');
1530
1531 if (var_name_end && var_name_begin < var_name_end)
1532 {
1533 // if we have already failed to parse, skip this variable
1534 if (success)
1535 {
1536 const char *cstr = NULL;
Michael Sartain0769b2b2013-07-30 16:44:36 +00001537 std::string token_format;
Greg Clayton1b654882010-09-19 02:33:57 +00001538 Address format_addr;
1539 bool calculate_format_addr_function_offset = false;
1540 // Set reg_kind and reg_num to invalid values
1541 RegisterKind reg_kind = kNumRegisterKinds;
1542 uint32_t reg_num = LLDB_INVALID_REGNUM;
1543 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001544 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001545 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001546 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001547 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1548 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001549
Greg Clayton1b654882010-09-19 02:33:57 +00001550 // Each variable must set success to true below...
1551 bool var_success = false;
1552 switch (var_name_begin[0])
1553 {
Enrico Granata4becb372011-06-29 22:27:15 +00001554 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001555 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001556 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001557 {
Enrico Granatac482a192011-08-17 22:13:59 +00001558 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001559 break;
1560
Enrico Granatac3e320a2011-08-02 17:27:39 +00001561 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001562 log->Printf("[Debugger::FormatPrompt] initial string: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001563
Enrico Granata6f3533f2011-07-29 19:53:35 +00001564 // check for *var and *svar
1565 if (*var_name_begin == '*')
1566 {
1567 do_deref_pointer = true;
1568 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001569 if (log)
1570 log->Printf("[Debugger::FormatPrompt] found a deref, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001571 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001572
Enrico Granata6f3533f2011-07-29 19:53:35 +00001573 if (*var_name_begin == 's')
1574 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001575 if (!valobj->IsSynthetic())
1576 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001577 if (!valobj)
1578 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001579 var_name_begin++;
Enrico Granata68ae4112013-06-18 18:23:07 +00001580 if (log)
1581 log->Printf("[Debugger::FormatPrompt] found a synthetic, new string is: %s",var_name_begin);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001582 }
1583
1584 // should be a 'v' by now
1585 if (*var_name_begin != 'v')
1586 break;
1587
Enrico Granatac3e320a2011-08-02 17:27:39 +00001588 if (log)
Enrico Granata68ae4112013-06-18 18:23:07 +00001589 log->Printf("[Debugger::FormatPrompt] string I am working with: %s",var_name_begin);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001590
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001591 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001592 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001593 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001594 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001595 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001596 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001597 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001598 const char* var_name_final = NULL;
1599 const char* var_name_final_if_array_range = NULL;
1600 const char* close_bracket_position = NULL;
1601 int64_t index_lower = -1;
1602 int64_t index_higher = -1;
1603 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001604 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001605 bool was_plain_var = false;
1606 bool was_var_format = false;
Enrico Granataa777dc22012-05-08 21:49:57 +00001607 bool was_var_indexed = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001608
Enrico Granatac482a192011-08-17 22:13:59 +00001609 if (!valobj) break;
1610 // simplest case ${var}, just print valobj's value
Michael Sartain0769b2b2013-07-30 16:44:36 +00001611 if (IsToken (var_name_begin, "var}"))
Enrico Granata4becb372011-06-29 22:27:15 +00001612 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001613 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001614 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001615 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001616 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001617 else if (IsToken (var_name_begin,"var%"))
Greg Clayton34132752011-07-06 04:07:21 +00001618 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001619 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001620 // this is a variable with some custom format applied to it
1621 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001622 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001623 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001624 ScanFormatDescriptor (var_name_begin,
1625 var_name_end,
1626 &var_name_final,
1627 &percent_position,
1628 &custom_format,
1629 &val_obj_display);
1630 }
1631 // this is ${var.something} or multiple .something nested
Michael Sartain0769b2b2013-07-30 16:44:36 +00001632 else if (IsToken (var_name_begin, "var"))
Greg Clayton34132752011-07-06 04:07:21 +00001633 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001634 if (IsToken (var_name_begin, "var["))
Enrico Granataa777dc22012-05-08 21:49:57 +00001635 was_var_indexed = true;
Greg Clayton34132752011-07-06 04:07:21 +00001636 const char* percent_position;
1637 ScanFormatDescriptor (var_name_begin,
1638 var_name_end,
1639 &var_name_final,
1640 &percent_position,
1641 &custom_format,
1642 &val_obj_display);
1643
1644 const char* open_bracket_position;
1645 const char* separator_position;
1646 ScanBracketedRange (var_name_begin,
1647 var_name_end,
1648 var_name_final,
1649 &open_bracket_position,
1650 &separator_position,
1651 &close_bracket_position,
1652 &var_name_final_if_array_range,
1653 &index_lower,
1654 &index_higher);
1655
1656 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001657
Enrico Granata599171a2013-02-01 23:59:44 +00001658 std::string expr_path(var_name_final-var_name_begin-1,0);
1659 memcpy(&expr_path[0], var_name_begin+3,var_name_final-var_name_begin-3);
1660
1661 if (log)
1662 log->Printf("[Debugger::FormatPrompt] symbol to expand: %s",expr_path.c_str());
1663
1664 target = valobj->GetValueForExpressionPath(expr_path.c_str(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001665 &first_unparsed,
1666 &reason_to_stop,
1667 &final_value_type,
1668 options,
1669 &what_next).get();
1670
1671 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001672 {
Enrico Granatae992a082011-07-22 17:03:19 +00001673 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001674 log->Printf("[Debugger::FormatPrompt] ERROR: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001675 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001676 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001677 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001678 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001679 else
1680 {
Enrico Granatae992a082011-07-22 17:03:19 +00001681 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001682 log->Printf("[Debugger::FormatPrompt] ALL RIGHT: unparsed portion = %s, why stopping = %d,"
Enrico Granatae992a082011-07-22 17:03:19 +00001683 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001684 first_unparsed, reason_to_stop, final_value_type);
1685 }
Enrico Granata4becb372011-06-29 22:27:15 +00001686 }
Greg Clayton34132752011-07-06 04:07:21 +00001687 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001688 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001689
Enrico Granata86cc9822012-03-19 22:58:49 +00001690 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1691 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001692
Enrico Granata86cc9822012-03-19 22:58:49 +00001693 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001694
Enrico Granataa7187d02011-07-06 19:27:11 +00001695 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001696 {
Greg Clayton34132752011-07-06 04:07:21 +00001697 // I have not deref-ed yet, let's do it
1698 // this happens when we are not going through GetValueForVariableExpressionPath
1699 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001700 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001701 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001702 if (error.Fail())
1703 {
1704 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001705 log->Printf("[Debugger::FormatPrompt] ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granatadc940732011-08-23 00:32:52 +00001706 break;
1707 }
Greg Clayton34132752011-07-06 04:07:21 +00001708 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001709 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001710
Enrico Granataa777dc22012-05-08 21:49:57 +00001711 // we do not want to use the summary for a bitfield of type T:n
1712 // if we were originally dealing with just a T - that would get
1713 // us into an endless recursion
1714 if (target->IsBitfield() && was_var_indexed)
1715 {
1716 // TODO: check for a (T:n)-specific summary - we should still obey that
1717 StreamString bitfield_name;
1718 bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), target->GetBitfieldBitSize());
1719 lldb::TypeNameSpecifierImplSP type_sp(new TypeNameSpecifierImpl(bitfield_name.GetData(),false));
1720 if (!DataVisualization::GetSummaryForType(type_sp))
1721 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
1722 }
1723
Enrico Granata85933ed2011-08-18 16:38:26 +00001724 // TODO use flags for these
Greg Clayton57ee3062013-07-11 22:46:58 +00001725 const uint32_t type_info_flags = target->GetClangType().GetTypeInfo(NULL);
1726 bool is_array = (type_info_flags & ClangASTType::eTypeIsArray) != 0;
1727 bool is_pointer = (type_info_flags & ClangASTType::eTypeIsPointer) != 0;
1728 bool is_aggregate = target->GetClangType().IsAggregateType();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001729
Enrico Granata86cc9822012-03-19 22:58:49 +00001730 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 +00001731 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001732 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001733 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001734 log->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001735
Greg Clayton5088c482013-03-25 21:06:13 +00001736 if (target->HasSpecialPrintableRepresentation(val_obj_display, custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001737 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001738 // try to use the special cases
1739 var_success = target->DumpPrintableRepresentation(str_temp,
1740 val_obj_display,
1741 custom_format);
1742 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001743 log->Printf("[Debugger::FormatPrompt] special cases did%s match", var_success ? "" : "n't");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001744
1745 // should not happen
Greg Clayton5088c482013-03-25 21:06:13 +00001746 if (var_success)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001747 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001748 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001749 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001750 }
1751 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001752 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001753 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001754 {
1755 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1756 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001757 else if (is_pointer) // if pointer, value is the address stored
1758 {
Greg Clayton23f59502012-07-17 03:23:13 +00001759 target->DumpPrintableRepresentation (s,
1760 val_obj_display,
1761 custom_format,
1762 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001763 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001764 var_success = true;
1765 break;
1766 }
1767 }
1768
1769 // if directly trying to print ${var}, and this is an aggregate, display a nice
1770 // type @ location message
1771 if (is_aggregate && was_plain_var)
1772 {
1773 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1774 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001775 break;
1776 }
1777
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001778 // 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 +00001779 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001780 {
1781 s << "<invalid use of aggregate type>";
1782 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001783 break;
1784 }
Greg Clayton34132752011-07-06 04:07:21 +00001785
1786 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001787 {
1788 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001789 log->Printf("[Debugger::FormatPrompt] dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001790 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001791 }
Greg Clayton34132752011-07-06 04:07:21 +00001792 else
Enrico Granatae992a082011-07-22 17:03:19 +00001793 {
1794 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001795 log->Printf("[Debugger::FormatPrompt] checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001796 if (!is_array && !is_pointer)
1797 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001798 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001799 log->Printf("[Debugger::FormatPrompt] handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001800 const char* special_directions = NULL;
1801 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001802 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1803 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001804 ConstString additional_data;
1805 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1806 special_directions_writer.Printf("${%svar%s}",
1807 do_deref_pointer ? "*" : "",
1808 additional_data.GetCString());
1809 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001810 }
1811
1812 // let us display items index_lower thru index_higher of this array
1813 s.PutChar('[');
1814 var_success = true;
1815
1816 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001817 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001818
Greg Claytoncc4d0142012-02-17 07:49:44 +00001819 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001820
Greg Clayton34132752011-07-06 04:07:21 +00001821 for (;index_lower<=index_higher;index_lower++)
1822 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001823 ValueObject* item = ExpandIndexedExpression (target,
1824 index_lower,
1825 exe_ctx->GetFramePtr(),
1826 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001827
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001828 if (!item)
1829 {
Enrico Granatae992a082011-07-22 17:03:19 +00001830 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001831 log->Printf("[Debugger::FormatPrompt] ERROR in getting child item at index %" PRId64, index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001832 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001833 else
1834 {
Enrico Granatae992a082011-07-22 17:03:19 +00001835 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +00001836 log->Printf("[Debugger::FormatPrompt] special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001837 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001838
Greg Clayton34132752011-07-06 04:07:21 +00001839 if (!special_directions)
1840 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1841 else
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001842 var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
Greg Clayton34132752011-07-06 04:07:21 +00001843
Enrico Granata22c55d12011-08-12 02:00:06 +00001844 if (--max_num_children == 0)
1845 {
1846 s.PutCString(", ...");
1847 break;
1848 }
1849
Greg Clayton34132752011-07-06 04:07:21 +00001850 if (index_lower < index_higher)
1851 s.PutChar(',');
1852 }
1853 s.PutChar(']');
1854 }
Enrico Granata4becb372011-06-29 22:27:15 +00001855 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001856 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001857 case 'a':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001858 if (IsToken (var_name_begin, "addr}"))
Greg Clayton1b654882010-09-19 02:33:57 +00001859 {
1860 if (addr && addr->IsValid())
1861 {
1862 var_success = true;
1863 format_addr = *addr;
1864 }
1865 }
1866 break;
1867
1868 case 'p':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001869 if (IsToken (var_name_begin, "process."))
Greg Clayton1b654882010-09-19 02:33:57 +00001870 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001871 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001872 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001873 Process *process = exe_ctx->GetProcessPtr();
1874 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001875 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001876 var_name_begin += ::strlen ("process.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001877 if (IsTokenWithFormat (var_name_begin, "id", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001878 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001879 s.Printf(token_format.c_str(), process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001880 var_success = true;
1881 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001882 else if ((IsToken (var_name_begin, "name}")) ||
1883 (IsToken (var_name_begin, "file.basename}")) ||
1884 (IsToken (var_name_begin, "file.fullpath}")))
Greg Claytonc14ee322011-09-22 04:58:26 +00001885 {
1886 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1887 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001888 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001889 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1890 {
1891 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001892 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001893 }
1894 else
1895 {
1896 format_file_spec = exe_module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00001897 var_success = (bool)format_file_spec;
Greg Claytonc14ee322011-09-22 04:58:26 +00001898 }
Greg Clayton1b654882010-09-19 02:33:57 +00001899 }
1900 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001901 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001902 {
1903 var_name_begin += ::strlen("script:");
1904 std::string script_name(var_name_begin,var_name_end);
1905 ScriptInterpreter* script_interpreter = process->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001906 if (RunScriptFormatKeyword (s, script_interpreter, process, script_name))
1907 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001908 }
Greg Clayton1b654882010-09-19 02:33:57 +00001909 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001910 }
Greg Clayton1b654882010-09-19 02:33:57 +00001911 }
1912 break;
1913
1914 case 't':
Michael Sartain0769b2b2013-07-30 16:44:36 +00001915 if (IsToken (var_name_begin, "thread."))
Greg Clayton1b654882010-09-19 02:33:57 +00001916 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001917 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001918 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001919 Thread *thread = exe_ctx->GetThreadPtr();
1920 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001921 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001922 var_name_begin += ::strlen ("thread.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00001923 if (IsTokenWithFormat (var_name_begin, "id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00001924 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001925 s.Printf(token_format.c_str(), thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001926 var_success = true;
1927 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001928 else if (IsTokenWithFormat (var_name_begin, "protocol_id", token_format, "0x%4.4" PRIx64, exe_ctx, sc))
Greg Clayton160c9d82013-05-01 21:54:04 +00001929 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001930 s.Printf(token_format.c_str(), thread->GetProtocolID());
Greg Clayton160c9d82013-05-01 21:54:04 +00001931 var_success = true;
1932 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001933 else if (IsTokenWithFormat (var_name_begin, "index", token_format, "%" PRIu64, exe_ctx, sc))
Greg Claytonc14ee322011-09-22 04:58:26 +00001934 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00001935 s.Printf(token_format.c_str(), (uint64_t)thread->GetIndexID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001936 var_success = true;
1937 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001938 else if (IsToken (var_name_begin, "name}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001939 {
1940 cstr = thread->GetName();
1941 var_success = cstr && cstr[0];
1942 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001943 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001944 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001945 else if (IsToken (var_name_begin, "queue}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001946 {
1947 cstr = thread->GetQueueName();
1948 var_success = cstr && cstr[0];
1949 if (var_success)
1950 s.PutCString(cstr);
1951 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001952 else if (IsToken (var_name_begin, "stop-reason}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00001953 {
1954 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001955 if (stop_info_sp && stop_info_sp->IsValid())
Greg Claytonc14ee322011-09-22 04:58:26 +00001956 {
1957 cstr = stop_info_sp->GetDescription();
1958 if (cstr && cstr[0])
1959 {
1960 s.PutCString(cstr);
1961 var_success = true;
1962 }
Greg Clayton1b654882010-09-19 02:33:57 +00001963 }
1964 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001965 else if (IsToken (var_name_begin, "return-value}"))
Jim Ingham73ca05a2011-12-17 01:35:57 +00001966 {
1967 StopInfoSP stop_info_sp = thread->GetStopInfo ();
Jim Ingham5d88a062012-10-16 00:09:33 +00001968 if (stop_info_sp && stop_info_sp->IsValid())
Jim Ingham73ca05a2011-12-17 01:35:57 +00001969 {
1970 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1971 if (return_valobj_sp)
1972 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +00001973 return_valobj_sp->Dump(s);
Jim Inghamef651602011-12-22 19:12:40 +00001974 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001975 }
1976 }
1977 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001978 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00001979 {
1980 var_name_begin += ::strlen("script:");
1981 std::string script_name(var_name_begin,var_name_end);
1982 ScriptInterpreter* script_interpreter = thread->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00001983 if (RunScriptFormatKeyword (s, script_interpreter, thread, script_name))
1984 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00001985 }
Greg Clayton1b654882010-09-19 02:33:57 +00001986 }
1987 }
1988 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00001989 else if (IsToken (var_name_begin, "target."))
Greg Clayton1b654882010-09-19 02:33:57 +00001990 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001991 // TODO: hookup properties
1992// if (!target_properties_sp)
1993// {
1994// Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1995// if (target)
1996// target_properties_sp = target->GetProperties();
1997// }
1998//
1999// if (target_properties_sp)
2000// {
2001// var_name_begin += ::strlen ("target.");
2002// const char *end_property = strchr(var_name_begin, '}');
2003// if (end_property)
2004// {
2005// ConstString property_name(var_name_begin, end_property - var_name_begin);
2006// std::string property_value (target_properties_sp->GetPropertyValue(property_name));
2007// if (!property_value.empty())
2008// {
2009// s.PutCString (property_value.c_str());
2010// var_success = true;
2011// }
2012// }
2013// }
Greg Clayton0603aa92010-10-04 01:05:56 +00002014 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2015 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00002016 {
Greg Clayton1b654882010-09-19 02:33:57 +00002017 var_name_begin += ::strlen ("target.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002018 if (IsToken (var_name_begin, "arch}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002019 {
2020 ArchSpec arch (target->GetArchitecture ());
2021 if (arch.IsValid())
2022 {
Greg Clayton64195a22011-02-23 00:35:02 +00002023 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00002024 var_success = true;
2025 }
2026 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002027 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002028 {
2029 var_name_begin += ::strlen("script:");
2030 std::string script_name(var_name_begin,var_name_end);
2031 ScriptInterpreter* script_interpreter = target->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002032 if (RunScriptFormatKeyword (s, script_interpreter, target, script_name))
2033 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002034 }
Greg Clayton67cc0632012-08-22 17:17:09 +00002035 }
Greg Clayton1b654882010-09-19 02:33:57 +00002036 }
2037 break;
2038
2039
2040 case 'm':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002041 if (IsToken (var_name_begin, "module."))
Greg Clayton1b654882010-09-19 02:33:57 +00002042 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002043 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00002044 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002045 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00002046 var_name_begin += ::strlen ("module.");
2047
Michael Sartain0769b2b2013-07-30 16:44:36 +00002048 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002049 {
2050 if (module->GetFileSpec())
2051 {
2052 var_name_begin += ::strlen ("file.");
2053
Michael Sartain0769b2b2013-07-30 16:44:36 +00002054 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002055 {
2056 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002057 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002058 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002059 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002060 {
2061 format_file_spec = module->GetFileSpec();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002062 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002063 }
2064 }
2065 }
2066 }
2067 }
2068 break;
2069
2070
2071 case 'f':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002072 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002073 {
2074 if (sc && sc->comp_unit != NULL)
2075 {
2076 var_name_begin += ::strlen ("file.");
2077
Michael Sartain0769b2b2013-07-30 16:44:36 +00002078 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002079 {
2080 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002081 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002082 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002083 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002084 {
2085 format_file_spec = *sc->comp_unit;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002086 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002087 }
2088 }
2089 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002090 else if (IsToken (var_name_begin, "frame."))
Greg Clayton1b654882010-09-19 02:33:57 +00002091 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002092 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002093 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002094 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002095 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00002096 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002097 var_name_begin += ::strlen ("frame.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002098 if (IsToken (var_name_begin, "index}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002099 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002100 s.Printf("%u", frame->GetFrameIndex());
2101 var_success = true;
2102 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002103 else if (IsToken (var_name_begin, "pc}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002104 {
2105 reg_kind = eRegisterKindGeneric;
2106 reg_num = LLDB_REGNUM_GENERIC_PC;
2107 var_success = true;
2108 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002109 else if (IsToken (var_name_begin, "sp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002110 {
2111 reg_kind = eRegisterKindGeneric;
2112 reg_num = LLDB_REGNUM_GENERIC_SP;
2113 var_success = true;
2114 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002115 else if (IsToken (var_name_begin, "fp}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002116 {
2117 reg_kind = eRegisterKindGeneric;
2118 reg_num = LLDB_REGNUM_GENERIC_FP;
2119 var_success = true;
2120 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002121 else if (IsToken (var_name_begin, "flags}"))
Greg Claytonc14ee322011-09-22 04:58:26 +00002122 {
2123 reg_kind = eRegisterKindGeneric;
2124 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
2125 var_success = true;
2126 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002127 else if (IsToken (var_name_begin, "reg."))
Greg Claytonc14ee322011-09-22 04:58:26 +00002128 {
2129 reg_ctx = frame->GetRegisterContext().get();
2130 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00002131 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002132 var_name_begin += ::strlen ("reg.");
2133 if (var_name_begin < var_name_end)
2134 {
2135 std::string reg_name (var_name_begin, var_name_end);
2136 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
2137 if (reg_info)
2138 var_success = true;
2139 }
Greg Clayton1b654882010-09-19 02:33:57 +00002140 }
2141 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002142 else if (IsToken (var_name_begin, "script:"))
Enrico Granataaad8e482013-06-20 23:40:21 +00002143 {
2144 var_name_begin += ::strlen("script:");
2145 std::string script_name(var_name_begin,var_name_end);
2146 ScriptInterpreter* script_interpreter = frame->GetThread()->GetProcess()->GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Michael Sartain0769b2b2013-07-30 16:44:36 +00002147 if (RunScriptFormatKeyword (s, script_interpreter, frame, script_name))
2148 var_success = true;
Enrico Granataaad8e482013-06-20 23:40:21 +00002149 }
Greg Clayton1b654882010-09-19 02:33:57 +00002150 }
2151 }
2152 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002153 else if (IsToken (var_name_begin, "function."))
Greg Clayton1b654882010-09-19 02:33:57 +00002154 {
2155 if (sc && (sc->function != NULL || sc->symbol != NULL))
2156 {
2157 var_name_begin += ::strlen ("function.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002158 if (IsToken (var_name_begin, "id}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002159 {
2160 if (sc->function)
Daniel Malead01b2952012-11-29 21:49:15 +00002161 s.Printf("function{0x%8.8" PRIx64 "}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00002162 else
2163 s.Printf("symbol[%u]", sc->symbol->GetID());
2164
2165 var_success = true;
2166 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002167 else if (IsToken (var_name_begin, "name}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002168 {
2169 if (sc->function)
2170 cstr = sc->function->GetName().AsCString (NULL);
2171 else if (sc->symbol)
2172 cstr = sc->symbol->GetName().AsCString (NULL);
2173 if (cstr)
2174 {
2175 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00002176
2177 if (sc->block)
2178 {
2179 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2180 if (inline_block)
2181 {
2182 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
2183 if (inline_info)
2184 {
2185 s.PutCString(" [inlined] ");
2186 inline_info->GetName().Dump(&s);
2187 }
2188 }
2189 }
Greg Clayton1b654882010-09-19 02:33:57 +00002190 var_success = true;
2191 }
2192 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002193 else if (IsToken (var_name_begin, "name-with-args}"))
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002194 {
2195 // Print the function name with arguments in it
2196
2197 if (sc->function)
2198 {
2199 var_success = true;
2200 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
2201 cstr = sc->function->GetName().AsCString (NULL);
2202 if (cstr)
2203 {
2204 const InlineFunctionInfo *inline_info = NULL;
2205 VariableListSP variable_list_sp;
2206 bool get_function_vars = true;
2207 if (sc->block)
2208 {
2209 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2210
2211 if (inline_block)
2212 {
2213 get_function_vars = false;
2214 inline_info = sc->block->GetInlinedFunctionInfo();
2215 if (inline_info)
2216 variable_list_sp = inline_block->GetBlockVariableList (true);
2217 }
2218 }
2219
2220 if (get_function_vars)
2221 {
2222 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
2223 }
2224
2225 if (inline_info)
2226 {
2227 s.PutCString (cstr);
2228 s.PutCString (" [inlined] ");
2229 cstr = inline_info->GetName().GetCString();
2230 }
2231
2232 VariableList args;
2233 if (variable_list_sp)
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002234 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument, args);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002235 if (args.GetSize() > 0)
2236 {
2237 const char *open_paren = strchr (cstr, '(');
2238 const char *close_paren = NULL;
2239 if (open_paren)
Greg Clayton855958c2013-03-26 01:45:43 +00002240 {
Michael Sartain0769b2b2013-07-30 16:44:36 +00002241 if (IsToken (open_paren, "(anonymous namespace)"))
Greg Clayton855958c2013-03-26 01:45:43 +00002242 {
2243 open_paren = strchr (open_paren + strlen("(anonymous namespace)"), '(');
2244 if (open_paren)
2245 close_paren = strchr (open_paren, ')');
2246 }
2247 else
2248 close_paren = strchr (open_paren, ')');
2249 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002250
2251 if (open_paren)
2252 s.Write(cstr, open_paren - cstr + 1);
2253 else
2254 {
2255 s.PutCString (cstr);
2256 s.PutChar ('(');
2257 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00002258 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002259 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
2260 {
2261 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
2262 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
2263 const char *var_name = var_value_sp->GetName().GetCString();
2264 const char *var_value = var_value_sp->GetValueAsCString();
Greg Clayton3b188b12012-12-10 22:26:34 +00002265 if (arg_idx > 0)
2266 s.PutCString (", ");
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002267 if (var_value_sp->GetError().Success())
Enrico Granatacc7f9bf2013-05-08 20:27:37 +00002268 {
2269 if (var_value)
2270 s.Printf ("%s=%s", var_name, var_value);
2271 else
2272 s.Printf ("%s=%s at %s", var_name, var_value_sp->GetTypeName().GetCString(), var_value_sp->GetLocationAsCString());
2273 }
Greg Clayton3b188b12012-12-10 22:26:34 +00002274 else
2275 s.Printf ("%s=<unavailable>", var_name);
Greg Clayton6d3dbf52012-01-13 08:39:16 +00002276 }
2277
2278 if (close_paren)
2279 s.PutCString (close_paren);
2280 else
2281 s.PutChar(')');
2282
2283 }
2284 else
2285 {
2286 s.PutCString(cstr);
2287 }
2288 }
2289 }
2290 else if (sc->symbol)
2291 {
2292 cstr = sc->symbol->GetName().AsCString (NULL);
2293 if (cstr)
2294 {
2295 s.PutCString(cstr);
2296 var_success = true;
2297 }
2298 }
2299 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002300 else if (IsToken (var_name_begin, "addr-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002301 {
2302 var_success = addr != NULL;
2303 if (var_success)
2304 {
2305 format_addr = *addr;
2306 calculate_format_addr_function_offset = true;
2307 }
2308 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002309 else if (IsToken (var_name_begin, "line-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002310 {
2311 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2312 if (var_success)
2313 {
2314 format_addr = sc->line_entry.range.GetBaseAddress();
2315 calculate_format_addr_function_offset = true;
2316 }
2317 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002318 else if (IsToken (var_name_begin, "pc-offset}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002319 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002320 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002321 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002322 if (var_success)
2323 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002324 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002325 calculate_format_addr_function_offset = true;
2326 }
2327 }
2328 }
2329 }
2330 break;
2331
2332 case 'l':
Michael Sartain0769b2b2013-07-30 16:44:36 +00002333 if (IsToken (var_name_begin, "line."))
Greg Clayton1b654882010-09-19 02:33:57 +00002334 {
2335 if (sc && sc->line_entry.IsValid())
2336 {
2337 var_name_begin += ::strlen ("line.");
Michael Sartain0769b2b2013-07-30 16:44:36 +00002338 if (IsToken (var_name_begin, "file."))
Greg Clayton1b654882010-09-19 02:33:57 +00002339 {
2340 var_name_begin += ::strlen ("file.");
2341
Michael Sartain0769b2b2013-07-30 16:44:36 +00002342 if (IsToken (var_name_begin, "basename}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002343 {
2344 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002345 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002346 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002347 else if (IsToken (var_name_begin, "fullpath}"))
Greg Clayton1b654882010-09-19 02:33:57 +00002348 {
2349 format_file_spec = sc->line_entry.file;
Sean Callanan9076c0f2013-10-04 21:35:29 +00002350 var_success = (bool)format_file_spec;
Greg Clayton1b654882010-09-19 02:33:57 +00002351 }
2352 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002353 else if (IsTokenWithFormat (var_name_begin, "number", token_format, "%" PRIu64, exe_ctx, sc))
Greg Clayton1b654882010-09-19 02:33:57 +00002354 {
2355 var_success = true;
Michael Sartain0769b2b2013-07-30 16:44:36 +00002356 s.Printf(token_format.c_str(), (uint64_t)sc->line_entry.line);
Greg Clayton1b654882010-09-19 02:33:57 +00002357 }
Michael Sartain0769b2b2013-07-30 16:44:36 +00002358 else if ((IsToken (var_name_begin, "start-addr}")) ||
2359 (IsToken (var_name_begin, "end-addr}")))
Greg Clayton1b654882010-09-19 02:33:57 +00002360 {
2361 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2362 if (var_success)
2363 {
2364 format_addr = sc->line_entry.range.GetBaseAddress();
2365 if (var_name_begin[0] == 'e')
2366 format_addr.Slide (sc->line_entry.range.GetByteSize());
2367 }
2368 }
2369 }
2370 }
2371 break;
2372 }
2373
2374 if (var_success)
2375 {
2376 // If format addr is valid, then we need to print an address
2377 if (reg_num != LLDB_INVALID_REGNUM)
2378 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002379 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002380 // We have a register value to display...
2381 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2382 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002383 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002384 }
2385 else
2386 {
2387 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002388 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002389
2390 if (reg_ctx)
2391 {
2392 if (reg_kind != kNumRegisterKinds)
2393 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2394 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2395 var_success = reg_info != NULL;
2396 }
2397 }
2398 }
2399
2400 if (reg_info != NULL)
2401 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002402 RegisterValue reg_value;
2403 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2404 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002405 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002406 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002407 }
2408 }
2409
2410 if (format_file_spec)
2411 {
2412 s << format_file_spec;
2413 }
2414
2415 // If format addr is valid, then we need to print an address
2416 if (format_addr.IsValid())
2417 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002418 var_success = false;
2419
Greg Clayton1b654882010-09-19 02:33:57 +00002420 if (calculate_format_addr_function_offset)
2421 {
2422 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002423
Greg Clayton0603aa92010-10-04 01:05:56 +00002424 if (sc)
2425 {
2426 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002427 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002428 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002429 if (sc->block)
2430 {
2431 // Check to make sure we aren't in an inline
2432 // function. If we are, use the inline block
2433 // range that contains "format_addr" since
2434 // blocks can be discontiguous.
2435 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2436 AddressRange inline_range;
2437 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2438 func_addr = inline_range.GetBaseAddress();
2439 }
2440 }
Greg Claytone7612132012-03-07 21:03:09 +00002441 else if (sc->symbol && sc->symbol->ValueIsAddress())
2442 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002443 }
2444
2445 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002446 {
2447 if (func_addr.GetSection() == format_addr.GetSection())
2448 {
2449 addr_t func_file_addr = func_addr.GetFileAddress();
2450 addr_t addr_file_addr = format_addr.GetFileAddress();
2451 if (addr_file_addr > func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002452 s.Printf(" + %" PRIu64, addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002453 else if (addr_file_addr < func_file_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002454 s.Printf(" - %" PRIu64, func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002455 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002456 }
2457 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002458 {
2459 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2460 if (target)
2461 {
2462 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2463 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2464 if (addr_load_addr > func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002465 s.Printf(" + %" PRIu64, addr_load_addr - func_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002466 else if (addr_load_addr < func_load_addr)
Daniel Malead01b2952012-11-29 21:49:15 +00002467 s.Printf(" - %" PRIu64, func_load_addr - addr_load_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002468 var_success = true;
2469 }
2470 }
Greg Clayton1b654882010-09-19 02:33:57 +00002471 }
2472 }
2473 else
2474 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002475 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002476 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002477 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2478 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002479 if (vaddr == LLDB_INVALID_ADDRESS)
2480 vaddr = format_addr.GetFileAddress ();
2481
2482 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002483 {
Greg Clayton514487e2011-02-15 21:59:32 +00002484 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002485 if (addr_width == 0)
2486 addr_width = 16;
Daniel Malead01b2952012-11-29 21:49:15 +00002487 s.Printf("0x%*.*" PRIx64, addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002488 var_success = true;
2489 }
Greg Clayton1b654882010-09-19 02:33:57 +00002490 }
2491 }
2492 }
2493
2494 if (var_success == false)
2495 success = false;
2496 }
2497 p = var_name_end;
2498 }
2499 else
2500 break;
2501 }
2502 else
2503 {
2504 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2505 s.PutChar(*p);
2506 }
2507 }
2508 else if (*p == '\\')
2509 {
2510 ++p; // skip the slash
2511 switch (*p)
2512 {
2513 case 'a': s.PutChar ('\a'); break;
2514 case 'b': s.PutChar ('\b'); break;
2515 case 'f': s.PutChar ('\f'); break;
2516 case 'n': s.PutChar ('\n'); break;
2517 case 'r': s.PutChar ('\r'); break;
2518 case 't': s.PutChar ('\t'); break;
2519 case 'v': s.PutChar ('\v'); break;
2520 case '\'': s.PutChar ('\''); break;
2521 case '\\': s.PutChar ('\\'); break;
2522 case '0':
2523 // 1 to 3 octal chars
2524 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002525 // Make a string that can hold onto the initial zero char,
2526 // up to 3 octal digits, and a terminating NULL.
2527 char oct_str[5] = { 0, 0, 0, 0, 0 };
2528
2529 int i;
2530 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2531 oct_str[i] = p[i];
2532
2533 // We don't want to consume the last octal character since
2534 // the main for loop will do this for us, so we advance p by
2535 // one less than i (even if i is zero)
2536 p += i - 1;
2537 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2538 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002539 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002540 s.PutChar((char)octal_value);
Greg Clayton1b654882010-09-19 02:33:57 +00002541 }
Greg Clayton1b654882010-09-19 02:33:57 +00002542 }
2543 break;
2544
2545 case 'x':
2546 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002547 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002548 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002549 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002550
Greg Clayton0603aa92010-10-04 01:05:56 +00002551 // Make a string that can hold onto two hex chars plus a
2552 // NULL terminator
2553 char hex_str[3] = { 0,0,0 };
2554 hex_str[0] = *p;
2555 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002556 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002557 ++p; // Skip the first of the two hex chars
2558 hex_str[1] = *p;
2559 }
2560
2561 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2562 if (hex_value <= UINT8_MAX)
Greg Claytonc7bece562013-01-25 18:06:21 +00002563 s.PutChar ((char)hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002564 }
2565 else
2566 {
2567 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002568 }
2569 break;
2570
2571 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002572 // Just desensitize any other character by just printing what
2573 // came after the '\'
2574 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002575 break;
2576
2577 }
2578
2579 }
2580 }
2581 if (end)
2582 *end = p;
2583 return success;
2584}
2585
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002586bool
2587Debugger::FormatPrompt
2588(
2589 const char *format,
2590 const SymbolContext *sc,
2591 const ExecutionContext *exe_ctx,
2592 const Address *addr,
2593 Stream &s,
2594 ValueObject* valobj
2595)
2596{
2597 bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
2598 std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
2599 if (format_str.length())
2600 format = format_str.c_str();
2601 return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
2602}
2603
Jim Ingham228063c2012-02-21 02:23:08 +00002604void
2605Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2606{
Jim Ingham4f02b222012-02-22 22:49:20 +00002607 // For simplicity's sake, I am not going to deal with how to close down any
2608 // open logging streams, I just redirect everything from here on out to the
2609 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002610 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2611}
2612
2613bool
2614Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2615{
2616 Log::Callbacks log_callbacks;
2617
2618 StreamSP log_stream_sp;
Sean Callanan9a028512012-08-09 00:50:26 +00002619 if (m_log_callback_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002620 {
2621 log_stream_sp = m_log_callback_stream_sp;
2622 // For now when using the callback mode you always get thread & timestamp.
2623 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2624 }
2625 else if (log_file == NULL || *log_file == '\0')
2626 {
2627 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2628 }
2629 else
2630 {
2631 LogStreamMap::iterator pos = m_log_streams.find(log_file);
Greg Claytonc1b2ccf2013-01-08 00:01:36 +00002632 if (pos != m_log_streams.end())
2633 log_stream_sp = pos->second.lock();
2634 if (!log_stream_sp)
Jim Ingham228063c2012-02-21 02:23:08 +00002635 {
2636 log_stream_sp.reset (new StreamFile (log_file));
2637 m_log_streams[log_file] = log_stream_sp;
2638 }
Jim Ingham228063c2012-02-21 02:23:08 +00002639 }
2640 assert (log_stream_sp.get());
2641
2642 if (log_options == 0)
2643 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2644
Greg Clayton57abc5d2013-05-10 21:47:16 +00002645 if (Log::GetLogChannelCallbacks (ConstString(channel), log_callbacks))
Jim Ingham228063c2012-02-21 02:23:08 +00002646 {
2647 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2648 return true;
2649 }
2650 else
2651 {
2652 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2653 if (log_channel_sp)
2654 {
2655 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2656 {
2657 return true;
2658 }
2659 else
2660 {
2661 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2662 return false;
2663 }
2664 }
2665 else
2666 {
2667 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2668 return false;
2669 }
2670 }
2671 return false;
2672}
2673
Greg Clayton9585fbf2013-03-19 00:20:55 +00002674SourceManager &
2675Debugger::GetSourceManager ()
2676{
2677 if (m_source_manager_ap.get() == NULL)
2678 m_source_manager_ap.reset (new SourceManager (shared_from_this()));
2679 return *m_source_manager_ap;
2680}
2681
2682