blob: f979c8d5877e106274d626e571ca982277c9f540 [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
Greg Clayton4a33d312011-06-23 17:59:56 +000010#include "lldb/Core/Debugger.h"
11
12#include <map>
13
Enrico Granata4becb372011-06-29 22:27:15 +000014#include "clang/AST/DeclCXX.h"
15#include "clang/AST/Type.h"
16
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/lldb-private.h"
18#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000019#include "lldb/Core/FormatManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/InputReader.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000021#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000023#include "lldb/Core/StreamAsynchronousIO.h"
Greg Clayton1b654882010-09-19 02:33:57 +000024#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000026#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000027#include "lldb/Core/ValueObjectVariable.h"
Greg Claytona3406612011-02-07 23:24:47 +000028#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000029#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000030#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/TargetList.h"
32#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000033#include "lldb/Target/RegisterContext.h"
34#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000036#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
Greg Clayton1b654882010-09-19 02:33:57 +000042static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000043static lldb::user_id_t g_unique_id = 1;
44
Greg Clayton1b654882010-09-19 02:33:57 +000045#pragma mark Static Functions
46
47static Mutex &
48GetDebuggerListMutex ()
49{
50 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
51 return g_mutex;
52}
53
54typedef std::vector<DebuggerSP> DebuggerList;
55
56static DebuggerList &
57GetDebuggerList()
58{
59 // hide the static debugger list inside a singleton accessor to avoid
60 // global init contructors
61 static DebuggerList g_list;
62 return g_list;
63}
64
65
Greg Claytone372b982011-11-21 21:44:34 +000066static const ConstString &
67PromptVarName ()
68{
69 static ConstString g_const_string ("prompt");
70 return g_const_string;
71}
72
73static const ConstString &
74GetFrameFormatName ()
75{
76 static ConstString g_const_string ("frame-format");
77 return g_const_string;
78}
79
80static const ConstString &
81GetThreadFormatName ()
82{
83 static ConstString g_const_string ("thread-format");
84 return g_const_string;
85}
86
87static const ConstString &
88ScriptLangVarName ()
89{
90 static ConstString g_const_string ("script-lang");
91 return g_const_string;
92}
93
94static const ConstString &
95TermWidthVarName ()
96{
97 static ConstString g_const_string ("term-width");
98 return g_const_string;
99}
100
101static const ConstString &
102UseExternalEditorVarName ()
103{
104 static ConstString g_const_string ("use-external-editor");
105 return g_const_string;
106}
107
108static const ConstString &
109AutoConfirmName ()
110{
111 static ConstString g_const_string ("auto-confirm");
112 return g_const_string;
113}
114
115static const ConstString &
116StopSourceContextBeforeName ()
117{
118 static ConstString g_const_string ("stop-line-count-before");
119 return g_const_string;
120}
121
122static const ConstString &
123StopSourceContextAfterName ()
124{
125 static ConstString g_const_string ("stop-line-count-after");
126 return g_const_string;
127}
128
129static const ConstString &
130StopDisassemblyCountName ()
131{
132 static ConstString g_const_string ("stop-disassembly-count");
133 return g_const_string;
134}
135
136static const ConstString &
137StopDisassemblyDisplayName ()
138{
139 static ConstString g_const_string ("stop-disassembly-display");
140 return g_const_string;
141}
142
143OptionEnumValueElement
144DebuggerInstanceSettings::g_show_disassembly_enum_values[] =
145{
146 { eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
147 { eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
148 { eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
149 { 0, NULL, NULL }
150};
151
152
153
Greg Clayton1b654882010-09-19 02:33:57 +0000154#pragma mark Debugger
155
Greg Clayton99d0faf2010-11-18 23:32:35 +0000156UserSettingsControllerSP &
157Debugger::GetSettingsController ()
158{
159 static UserSettingsControllerSP g_settings_controller;
160 return g_settings_controller;
161}
162
Caroline Tice2f88aad2011-01-14 00:29:16 +0000163int
164Debugger::TestDebuggerRefCount ()
165{
166 return g_shared_debugger_refcount;
167}
168
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169void
170Debugger::Initialize ()
171{
Greg Clayton66111032010-06-23 01:19:29 +0000172 if (g_shared_debugger_refcount == 0)
Greg Clayton99d0faf2010-11-18 23:32:35 +0000173 {
Greg Claytondbe54502010-11-19 03:46:01 +0000174 lldb_private::Initialize();
Greg Clayton99d0faf2010-11-18 23:32:35 +0000175 }
Greg Clayton66111032010-06-23 01:19:29 +0000176 g_shared_debugger_refcount++;
Greg Clayton99d0faf2010-11-18 23:32:35 +0000177
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178}
179
180void
181Debugger::Terminate ()
182{
Greg Clayton66111032010-06-23 01:19:29 +0000183 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184 {
Greg Clayton66111032010-06-23 01:19:29 +0000185 g_shared_debugger_refcount--;
186 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 {
Greg Claytondbe54502010-11-19 03:46:01 +0000188 lldb_private::WillTerminate();
189 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000190
191 // Clear our master list of debugger objects
192 Mutex::Locker locker (GetDebuggerListMutex ());
193 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 }
196}
197
Caroline Tice20bd37f2011-03-10 22:14:10 +0000198void
199Debugger::SettingsInitialize ()
200{
201 static bool g_initialized = false;
202
203 if (!g_initialized)
204 {
205 g_initialized = true;
206 UserSettingsControllerSP &usc = GetSettingsController();
207 usc.reset (new SettingsController);
208 UserSettingsController::InitializeSettingsController (usc,
209 SettingsController::global_settings_table,
210 SettingsController::instance_settings_table);
211 // Now call SettingsInitialize for each settings 'child' of Debugger
212 Target::SettingsInitialize ();
213 }
214}
215
216void
217Debugger::SettingsTerminate ()
218{
219
220 // Must call SettingsTerminate() for each settings 'child' of Debugger, before terminating the Debugger's
221 // Settings.
222
223 Target::SettingsTerminate ();
224
225 // Now terminate the Debugger Settings.
226
227 UserSettingsControllerSP &usc = GetSettingsController();
228 UserSettingsController::FinalizeSettingsController (usc);
229 usc.reset();
230}
231
Greg Clayton66111032010-06-23 01:19:29 +0000232DebuggerSP
233Debugger::CreateInstance ()
234{
235 DebuggerSP debugger_sp (new Debugger);
236 // Scope for locker
237 {
238 Mutex::Locker locker (GetDebuggerListMutex ());
239 GetDebuggerList().push_back(debugger_sp);
240 }
241 return debugger_sp;
242}
243
Caroline Ticee02657b2011-01-22 01:02:07 +0000244void
Greg Clayton4d122c42011-09-17 08:33:22 +0000245Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000246{
247 if (debugger_sp.get() == NULL)
248 return;
249
Jim Ingham8314c522011-09-15 21:36:42 +0000250 debugger_sp->Clear();
251
Caroline Ticee02657b2011-01-22 01:02:07 +0000252 Mutex::Locker locker (GetDebuggerListMutex ());
253 DebuggerList &debugger_list = GetDebuggerList ();
254 DebuggerList::iterator pos, end = debugger_list.end();
255 for (pos = debugger_list.begin (); pos != end; ++pos)
256 {
257 if ((*pos).get() == debugger_sp.get())
258 {
259 debugger_list.erase (pos);
260 return;
261 }
262 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000263}
264
Greg Clayton4d122c42011-09-17 08:33:22 +0000265DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000266Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
267{
Greg Clayton4d122c42011-09-17 08:33:22 +0000268 DebuggerSP debugger_sp;
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000269
270 Mutex::Locker locker (GetDebuggerListMutex ());
271 DebuggerList &debugger_list = GetDebuggerList();
272 DebuggerList::iterator pos, end = debugger_list.end();
273
274 for (pos = debugger_list.begin(); pos != end; ++pos)
275 {
276 if ((*pos).get()->m_instance_name == instance_name)
277 {
278 debugger_sp = *pos;
279 break;
280 }
281 }
282 return debugger_sp;
283}
Greg Clayton66111032010-06-23 01:19:29 +0000284
285TargetSP
286Debugger::FindTargetWithProcessID (lldb::pid_t pid)
287{
Greg Clayton4d122c42011-09-17 08:33:22 +0000288 TargetSP target_sp;
Greg Clayton66111032010-06-23 01:19:29 +0000289 Mutex::Locker locker (GetDebuggerListMutex ());
290 DebuggerList &debugger_list = GetDebuggerList();
291 DebuggerList::iterator pos, end = debugger_list.end();
292 for (pos = debugger_list.begin(); pos != end; ++pos)
293 {
294 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
295 if (target_sp)
296 break;
297 }
298 return target_sp;
299}
300
Greg Claytone4e45922011-11-16 05:37:56 +0000301TargetSP
302Debugger::FindTargetWithProcess (Process *process)
303{
304 TargetSP target_sp;
305 Mutex::Locker locker (GetDebuggerListMutex ());
306 DebuggerList &debugger_list = GetDebuggerList();
307 DebuggerList::iterator pos, end = debugger_list.end();
308 for (pos = debugger_list.begin(); pos != end; ++pos)
309 {
310 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
311 if (target_sp)
312 break;
313 }
314 return target_sp;
315}
316
Greg Clayton66111032010-06-23 01:19:29 +0000317
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318Debugger::Debugger () :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000319 UserID (g_unique_id++),
Greg Claytondbe54502010-11-19 03:46:01 +0000320 DebuggerInstanceSettings (*GetSettingsController()),
Greg Claytond46c87a2010-12-04 02:39:47 +0000321 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 m_input_file (),
323 m_output_file (),
324 m_error_file (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 m_target_list (),
Greg Claytonded470d2011-03-19 01:12:21 +0000326 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 m_listener ("lldb.Debugger"),
Jim Inghame37d6052011-09-13 00:29:56 +0000328 m_source_manager(*this),
329 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000330 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000331 m_input_reader_stack (),
Greg Clayton4957bf62010-09-30 21:49:03 +0000332 m_input_reader_data ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333{
Greg Clayton66111032010-06-23 01:19:29 +0000334 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000335 // Always add our default platform to the platform list
336 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
337 assert (default_platform_sp.get());
338 m_platform_list.Append (default_platform_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339}
340
341Debugger::~Debugger ()
342{
Jim Ingham8314c522011-09-15 21:36:42 +0000343 Clear();
344}
345
346void
347Debugger::Clear()
348{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000349 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000350 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000351 int num_targets = m_target_list.GetNumTargets();
352 for (int i = 0; i < num_targets; i++)
353 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000354 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
355 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000356 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000357 ProcessSP process_sp (target_sp->GetProcessSP());
358 if (process_sp)
359 {
360 if (process_sp->GetShouldDetach())
361 process_sp->Detach();
362 }
363 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000364 }
Greg Clayton66111032010-06-23 01:19:29 +0000365 }
366 DisconnectInput();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367
Jim Ingham8314c522011-09-15 21:36:42 +0000368}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369
370bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000371Debugger::GetCloseInputOnEOF () const
372{
373 return m_input_comm.GetCloseOnEOF();
374}
375
376void
377Debugger::SetCloseInputOnEOF (bool b)
378{
379 m_input_comm.SetCloseOnEOF(b);
380}
381
382bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383Debugger::GetAsyncExecution ()
384{
Greg Clayton66111032010-06-23 01:19:29 +0000385 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386}
387
388void
389Debugger::SetAsyncExecution (bool async_execution)
390{
Greg Clayton66111032010-06-23 01:19:29 +0000391 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392}
393
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394
395void
396Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
397{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000398 File &in_file = GetInputFile();
399 in_file.SetStream (fh, tranfer_ownership);
400 if (in_file.IsValid() == false)
401 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402
403 // Disconnect from any old connection if we had one
404 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000405 // Pass false as the second argument to ConnectionFileDescriptor below because
406 // our "in_file" above will already take ownership if requested and we don't
407 // want to objects trying to own and close a file descriptor.
408 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
410
411 Error error;
412 if (m_input_comm.StartReadThread (&error) == false)
413 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000414 File &err_file = GetErrorFile();
415
416 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
417 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419}
420
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421void
422Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
423{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000424 File &out_file = GetOutputFile();
425 out_file.SetStream (fh, tranfer_ownership);
426 if (out_file.IsValid() == false)
427 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000428
429 GetCommandInterpreter().GetScriptInterpreter()->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430}
431
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432void
433Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
434{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000435 File &err_file = GetErrorFile();
436 err_file.SetStream (fh, tranfer_ownership);
437 if (err_file.IsValid() == false)
438 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439}
440
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000442Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443{
444 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000445 TargetSP target_sp(GetSelectedTarget());
446 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447
448 if (target_sp)
449 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000450 ProcessSP process_sp (target_sp->GetProcessSP());
451 exe_ctx.SetProcessSP (process_sp);
452 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000454 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
455 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000457 exe_ctx.SetThreadSP (thread_sp);
458 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
459 if (exe_ctx.GetFramePtr() == NULL)
460 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 }
462 }
463 }
464 return exe_ctx;
465
466}
467
Caroline Ticeb44880c2011-02-10 01:15:13 +0000468InputReaderSP
469Debugger::GetCurrentInputReader ()
470{
471 InputReaderSP reader_sp;
472
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000473 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000474 {
475 // Clear any finished readers from the stack
476 while (CheckIfTopInputReaderIsDone()) ;
477
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000478 if (!m_input_reader_stack.IsEmpty())
479 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000480 }
481
482 return reader_sp;
483}
484
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485void
486Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
487{
Caroline Ticeefed6132010-11-19 20:47:54 +0000488 if (bytes_len > 0)
489 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
490 else
491 ((Debugger *)baton)->DispatchInputEndOfFile ();
492}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493
494
495void
496Debugger::DispatchInput (const char *bytes, size_t bytes_len)
497{
Caroline Ticeefed6132010-11-19 20:47:54 +0000498 if (bytes == NULL || bytes_len == 0)
499 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500
501 WriteToDefaultReader (bytes, bytes_len);
502}
503
504void
Caroline Ticeefed6132010-11-19 20:47:54 +0000505Debugger::DispatchInputInterrupt ()
506{
507 m_input_reader_data.clear();
508
Caroline Ticeb44880c2011-02-10 01:15:13 +0000509 InputReaderSP reader_sp (GetCurrentInputReader ());
510 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000511 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000512 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000513
Caroline Ticeb44880c2011-02-10 01:15:13 +0000514 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000515 while (CheckIfTopInputReaderIsDone ()) ;
516 }
517}
518
519void
520Debugger::DispatchInputEndOfFile ()
521{
522 m_input_reader_data.clear();
523
Caroline Ticeb44880c2011-02-10 01:15:13 +0000524 InputReaderSP reader_sp (GetCurrentInputReader ());
525 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000526 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000527 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000528
Caroline Ticeb44880c2011-02-10 01:15:13 +0000529 // 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 +0000530 while (CheckIfTopInputReaderIsDone ()) ;
531 }
532}
533
534void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000535Debugger::CleanUpInputReaders ()
536{
537 m_input_reader_data.clear();
538
Caroline Ticeb44880c2011-02-10 01:15:13 +0000539 // 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 +0000540 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000541 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000542 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000543 if (reader_sp)
544 {
545 reader_sp->Notify (eInputReaderEndOfFile);
546 reader_sp->SetIsDone (true);
547 }
548 }
549}
550
551void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000552Debugger::NotifyTopInputReader (InputReaderAction notification)
553{
554 InputReaderSP reader_sp (GetCurrentInputReader());
555 if (reader_sp)
556 {
557 reader_sp->Notify (notification);
558
559 // Flush out any input readers that are done.
560 while (CheckIfTopInputReaderIsDone ())
561 /* Do nothing. */;
562 }
563}
564
Caroline Tice9088b062011-05-09 23:06:58 +0000565bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000566Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000567{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000568 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000569
Caroline Ticed61c10b2011-06-16 16:27:19 +0000570 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000571}
572
573
Caroline Tice969ed3d2011-05-02 20:41:46 +0000574void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
576{
577 if (bytes && bytes_len)
578 m_input_reader_data.append (bytes, bytes_len);
579
580 if (m_input_reader_data.empty())
581 return;
582
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000583 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000586 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 if (!reader_sp)
588 break;
589
Greg Clayton471b31c2010-07-20 22:52:08 +0000590 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591 m_input_reader_data.size());
592 if (bytes_handled)
593 {
594 m_input_reader_data.erase (0, bytes_handled);
595 }
596 else
597 {
598 // No bytes were handled, we might not have reached our
599 // granularity, just return and wait for more data
600 break;
601 }
602 }
603
Caroline Ticeb44880c2011-02-10 01:15:13 +0000604 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 while (CheckIfTopInputReaderIsDone ())
606 /* Do nothing. */;
607
608}
609
610void
611Debugger::PushInputReader (const InputReaderSP& reader_sp)
612{
613 if (!reader_sp)
614 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000615
616 // Deactivate the old top reader
617 InputReaderSP top_reader_sp (GetCurrentInputReader ());
618
619 if (top_reader_sp)
620 top_reader_sp->Notify (eInputReaderDeactivate);
621
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000622 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 reader_sp->Notify (eInputReaderActivate);
624 ActivateInputReader (reader_sp);
625}
626
627bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000628Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629{
630 bool result = false;
631
632 // The reader on the stop of the stack is done, so let the next
633 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000634 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000636 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000637 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638
639 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
640 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000641 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642 reader_sp->Notify (eInputReaderDeactivate);
643 reader_sp->Notify (eInputReaderDone);
644 result = true;
645
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000646 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000648 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649 if (reader_sp)
650 {
651 ActivateInputReader (reader_sp);
652 reader_sp->Notify (eInputReaderReactivate);
653 }
654 }
655 }
656 }
657 return result;
658}
659
660bool
661Debugger::CheckIfTopInputReaderIsDone ()
662{
663 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000664 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000666 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000667 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668
669 if (reader_sp && reader_sp->IsDone())
670 {
671 result = true;
672 PopInputReader (reader_sp);
673 }
674 }
675 return result;
676}
677
678void
679Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
680{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000681 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000683 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000685 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000686
687 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688
Greg Claytona3406612011-02-07 23:24:47 +0000689 switch (reader_sp->GetGranularity())
690 {
691 case eInputReaderGranularityByte:
692 case eInputReaderGranularityWord:
693 tty.SetCanonical (false);
694 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695
Greg Claytona3406612011-02-07 23:24:47 +0000696 case eInputReaderGranularityLine:
697 case eInputReaderGranularityAll:
698 tty.SetCanonical (true);
699 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700
Greg Claytona3406612011-02-07 23:24:47 +0000701 default:
702 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 }
704 }
705}
Greg Clayton66111032010-06-23 01:19:29 +0000706
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000707StreamSP
708Debugger::GetAsyncOutputStream ()
709{
710 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
711 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
712}
713
714StreamSP
715Debugger::GetAsyncErrorStream ()
716{
717 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
718 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
719}
720
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000721DebuggerSP
722Debugger::FindDebuggerWithID (lldb::user_id_t id)
723{
Greg Clayton4d122c42011-09-17 08:33:22 +0000724 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000725
726 Mutex::Locker locker (GetDebuggerListMutex ());
727 DebuggerList &debugger_list = GetDebuggerList();
728 DebuggerList::iterator pos, end = debugger_list.end();
729 for (pos = debugger_list.begin(); pos != end; ++pos)
730 {
731 if ((*pos).get()->GetID() == id)
732 {
733 debugger_sp = *pos;
734 break;
735 }
736 }
737 return debugger_sp;
738}
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000739
Greg Clayton1b654882010-09-19 02:33:57 +0000740static void
741TestPromptFormats (StackFrame *frame)
742{
743 if (frame == NULL)
744 return;
745
746 StreamString s;
747 const char *prompt_format =
748 "{addr = '${addr}'\n}"
749 "{process.id = '${process.id}'\n}"
750 "{process.name = '${process.name}'\n}"
751 "{process.file.basename = '${process.file.basename}'\n}"
752 "{process.file.fullpath = '${process.file.fullpath}'\n}"
753 "{thread.id = '${thread.id}'\n}"
754 "{thread.index = '${thread.index}'\n}"
755 "{thread.name = '${thread.name}'\n}"
756 "{thread.queue = '${thread.queue}'\n}"
757 "{thread.stop-reason = '${thread.stop-reason}'\n}"
758 "{target.arch = '${target.arch}'\n}"
759 "{module.file.basename = '${module.file.basename}'\n}"
760 "{module.file.fullpath = '${module.file.fullpath}'\n}"
761 "{file.basename = '${file.basename}'\n}"
762 "{file.fullpath = '${file.fullpath}'\n}"
763 "{frame.index = '${frame.index}'\n}"
764 "{frame.pc = '${frame.pc}'\n}"
765 "{frame.sp = '${frame.sp}'\n}"
766 "{frame.fp = '${frame.fp}'\n}"
767 "{frame.flags = '${frame.flags}'\n}"
768 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
769 "{frame.reg.rip = '${frame.reg.rip}'\n}"
770 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
771 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
772 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
773 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
774 "{frame.reg.carp = '${frame.reg.carp}'\n}"
775 "{function.id = '${function.id}'\n}"
776 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +0000777 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +0000778 "{function.addr-offset = '${function.addr-offset}'\n}"
779 "{function.line-offset = '${function.line-offset}'\n}"
780 "{function.pc-offset = '${function.pc-offset}'\n}"
781 "{line.file.basename = '${line.file.basename}'\n}"
782 "{line.file.fullpath = '${line.file.fullpath}'\n}"
783 "{line.number = '${line.number}'\n}"
784 "{line.start-addr = '${line.start-addr}'\n}"
785 "{line.end-addr = '${line.end-addr}'\n}"
786;
787
788 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
789 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +0000790 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +0000791 const char *end = NULL;
792 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
793 {
794 printf("%s\n", s.GetData());
795 }
796 else
797 {
798 printf ("error: at '%s'\n", end);
799 printf ("what we got: %s\n", s.GetData());
800 }
801}
802
Enrico Granata9fc19442011-07-06 02:13:41 +0000803static bool
Enrico Granatadc940732011-08-23 00:32:52 +0000804ScanFormatDescriptor (const char* var_name_begin,
805 const char* var_name_end,
806 const char** var_name_final,
807 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +0000808 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +0000809 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +0000810{
Enrico Granatae992a082011-07-22 17:03:19 +0000811 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000812 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +0000813 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +0000814 {
815 if (log)
816 log->Printf("no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +0000817 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +0000818 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000819 else
820 {
821 *var_name_final = *percent_position;
822 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
823 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +0000824 if (log)
825 log->Printf("parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000826 if ( !FormatManager::GetFormatFromCString(format_name,
827 true,
828 *custom_format) )
829 {
Enrico Granatae992a082011-07-22 17:03:19 +0000830 if (log)
831 log->Printf("%s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000832 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +0000833 if (*format_name == '@')
Enrico Granata9fc19442011-07-06 02:13:41 +0000834 *val_obj_display = ValueObject::eDisplayLanguageSpecific;
835 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +0000836 else if (*format_name == 'V')
Enrico Granata9fc19442011-07-06 02:13:41 +0000837 *val_obj_display = ValueObject::eDisplayValue;
Enrico Granatad55546b2011-07-22 00:16:08 +0000838 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +0000839 else if (*format_name == 'L')
Enrico Granataf2bbf712011-07-15 02:26:42 +0000840 *val_obj_display = ValueObject::eDisplayLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +0000841 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +0000842 else if (*format_name == 'S')
Enrico Granatad55546b2011-07-22 00:16:08 +0000843 *val_obj_display = ValueObject::eDisplaySummary;
Enrico Granata5dfd49c2011-08-04 02:34:29 +0000844 else if (*format_name == '#')
845 *val_obj_display = ValueObject::eDisplayChildrenCount;
Enrico Granatad64d0bc2011-08-19 21:13:46 +0000846 else if (*format_name == 'T')
847 *val_obj_display = ValueObject::eDisplayType;
Enrico Granatae992a082011-07-22 17:03:19 +0000848 else if (log)
849 log->Printf("%s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000850 }
851 // a good custom format tells us to print the value using it
852 else
Enrico Granatae992a082011-07-22 17:03:19 +0000853 {
854 if (log)
855 log->Printf("will display value for this VO");
Enrico Granata9fc19442011-07-06 02:13:41 +0000856 *val_obj_display = ValueObject::eDisplayValue;
Enrico Granatae992a082011-07-22 17:03:19 +0000857 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000858 delete format_name;
859 }
Enrico Granatae992a082011-07-22 17:03:19 +0000860 if (log)
861 log->Printf("final format description outcome: custom_format = %d, val_obj_display = %d",
862 *custom_format,
863 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +0000864 return true;
865}
866
867static bool
Enrico Granatadc940732011-08-23 00:32:52 +0000868ScanBracketedRange (const char* var_name_begin,
869 const char* var_name_end,
870 const char* var_name_final,
871 const char** open_bracket_position,
872 const char** separator_position,
873 const char** close_bracket_position,
874 const char** var_name_final_if_array_range,
875 int64_t* index_lower,
876 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +0000877{
Enrico Granatae992a082011-07-22 17:03:19 +0000878 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000879 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +0000880 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +0000881 {
882 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
883 *close_bracket_position = ::strchr(*open_bracket_position,']');
884 // as usual, we assume that [] will come before %
885 //printf("trying to expand a []\n");
886 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +0000887 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +0000888 {
Enrico Granatae992a082011-07-22 17:03:19 +0000889 if (log)
890 log->Printf("[] detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +0000891 *index_lower = 0;
892 }
893 else if (*separator_position == NULL || *separator_position > var_name_end)
894 {
895 char *end = NULL;
896 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
897 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +0000898 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000899 log->Printf("[%lld] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +0000900 }
Greg Clayton34132752011-07-06 04:07:21 +0000901 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +0000902 {
903 char *end = NULL;
904 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
905 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +0000906 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000907 log->Printf("[%lld-%lld] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +0000908 }
909 else
Enrico Granatae992a082011-07-22 17:03:19 +0000910 {
911 if (log)
912 log->Printf("expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +0000913 return false;
Enrico Granatae992a082011-07-22 17:03:19 +0000914 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000915 if (*index_lower > *index_higher && *index_higher > 0)
916 {
Enrico Granatae992a082011-07-22 17:03:19 +0000917 if (log)
918 log->Printf("swapping indices");
Enrico Granata9fc19442011-07-06 02:13:41 +0000919 int temp = *index_lower;
920 *index_lower = *index_higher;
921 *index_higher = temp;
922 }
923 }
Enrico Granatae992a082011-07-22 17:03:19 +0000924 else if (log)
925 log->Printf("no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +0000926 return true;
927}
928
929
930static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +0000931ExpandExpressionPath (ValueObject* valobj,
932 StackFrame* frame,
933 bool* do_deref_pointer,
934 const char* var_name_begin,
935 const char* var_name_final,
936 Error& error)
Enrico Granata9fc19442011-07-06 02:13:41 +0000937{
Enrico Granatae992a082011-07-22 17:03:19 +0000938 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000939 StreamString sstring;
940 VariableSP var_sp;
941
Greg Clayton34132752011-07-06 04:07:21 +0000942 if (*do_deref_pointer)
Enrico Granatae992a082011-07-22 17:03:19 +0000943 {
944 if (log)
945 log->Printf("been told to deref_pointer by caller");
Enrico Granata9fc19442011-07-06 02:13:41 +0000946 sstring.PutChar('*');
Enrico Granatae992a082011-07-22 17:03:19 +0000947 }
Enrico Granatac482a192011-08-17 22:13:59 +0000948 else if (valobj->IsDereferenceOfParent() && ClangASTContext::IsPointerType(valobj->GetParent()->GetClangType()) && !valobj->IsArrayItemForPointer())
Enrico Granata9fc19442011-07-06 02:13:41 +0000949 {
Enrico Granatae992a082011-07-22 17:03:19 +0000950 if (log)
951 log->Printf("decided to deref_pointer myself");
Enrico Granata9fc19442011-07-06 02:13:41 +0000952 sstring.PutChar('*');
953 *do_deref_pointer = true;
954 }
955
Enrico Granatac482a192011-08-17 22:13:59 +0000956 valobj->GetExpressionPath(sstring, true, ValueObject::eHonorPointers);
Enrico Granatae992a082011-07-22 17:03:19 +0000957 if (log)
958 log->Printf("expression path to expand in phase 0: %s",sstring.GetData());
Enrico Granata9fc19442011-07-06 02:13:41 +0000959 sstring.PutRawBytes(var_name_begin+3, var_name_final-var_name_begin-3);
Enrico Granatae992a082011-07-22 17:03:19 +0000960 if (log)
961 log->Printf("expression path to expand in phase 1: %s",sstring.GetData());
Enrico Granata9fc19442011-07-06 02:13:41 +0000962 std::string name = std::string(sstring.GetData());
963 ValueObjectSP target = frame->GetValueForVariableExpressionPath (name.c_str(),
964 eNoDynamicValues,
965 0,
966 var_sp,
967 error);
968 return target;
969}
970
971static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +0000972ExpandIndexedExpression (ValueObject* valobj,
973 uint32_t index,
974 StackFrame* frame,
975 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +0000976{
Enrico Granatae992a082011-07-22 17:03:19 +0000977 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000978 const char* ptr_deref_format = "[%d]";
979 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
980 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +0000981 if (log)
982 log->Printf("name to deref: %s",ptr_deref_buffer.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000983 const char* first_unparsed;
984 ValueObject::GetValueForExpressionPathOptions options;
985 ValueObject::ExpressionPathEndResultType final_value_type;
986 ValueObject::ExpressionPathScanEndReason reason_to_stop;
987 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eDereference : ValueObject::eNothing);
Enrico Granatac482a192011-08-17 22:13:59 +0000988 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000989 &first_unparsed,
990 &reason_to_stop,
991 &final_value_type,
992 options,
993 &what_next);
994 if (!item)
995 {
Enrico Granatae992a082011-07-22 17:03:19 +0000996 if (log)
997 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
998 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000999 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001000 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001001 else
1002 {
Enrico Granatae992a082011-07-22 17:03:19 +00001003 if (log)
1004 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1005 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001006 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001007 }
1008 return item;
1009}
1010
Greg Clayton1b654882010-09-19 02:33:57 +00001011bool
1012Debugger::FormatPrompt
1013(
1014 const char *format,
1015 const SymbolContext *sc,
1016 const ExecutionContext *exe_ctx,
1017 const Address *addr,
1018 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001019 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001020 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001021)
1022{
Enrico Granatac482a192011-08-17 22:13:59 +00001023 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001024 bool success = true;
1025 const char *p;
Enrico Granatae992a082011-07-22 17:03:19 +00001026 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001027 for (p = format; *p != '\0'; ++p)
1028 {
Enrico Granatac482a192011-08-17 22:13:59 +00001029 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001030 {
Enrico Granatac482a192011-08-17 22:13:59 +00001031 valobj = realvalobj;
1032 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001033 }
Greg Clayton1b654882010-09-19 02:33:57 +00001034 size_t non_special_chars = ::strcspn (p, "${}\\");
1035 if (non_special_chars > 0)
1036 {
1037 if (success)
1038 s.Write (p, non_special_chars);
1039 p += non_special_chars;
1040 }
1041
1042 if (*p == '\0')
1043 {
1044 break;
1045 }
1046 else if (*p == '{')
1047 {
1048 // Start a new scope that must have everything it needs if it is to
1049 // to make it into the final output stream "s". If you want to make
1050 // a format that only prints out the function or symbol name if there
1051 // is one in the symbol context you can use:
1052 // "{function =${function.name}}"
1053 // The first '{' starts a new scope that end with the matching '}' at
1054 // the end of the string. The contents "function =${function.name}"
1055 // will then be evaluated and only be output if there is a function
1056 // or symbol with a valid name.
1057 StreamString sub_strm;
1058
1059 ++p; // Skip the '{'
1060
Enrico Granatac482a192011-08-17 22:13:59 +00001061 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001062 {
1063 // The stream had all it needed
1064 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1065 }
1066 if (*p != '}')
1067 {
1068 success = false;
1069 break;
1070 }
1071 }
1072 else if (*p == '}')
1073 {
1074 // End of a enclosing scope
1075 break;
1076 }
1077 else if (*p == '$')
1078 {
1079 // We have a prompt variable to print
1080 ++p;
1081 if (*p == '{')
1082 {
1083 ++p;
1084 const char *var_name_begin = p;
1085 const char *var_name_end = ::strchr (p, '}');
1086
1087 if (var_name_end && var_name_begin < var_name_end)
1088 {
1089 // if we have already failed to parse, skip this variable
1090 if (success)
1091 {
1092 const char *cstr = NULL;
1093 Address format_addr;
1094 bool calculate_format_addr_function_offset = false;
1095 // Set reg_kind and reg_num to invalid values
1096 RegisterKind reg_kind = kNumRegisterKinds;
1097 uint32_t reg_num = LLDB_INVALID_REGNUM;
1098 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001099 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001100 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001101 bool do_deref_pointer = false;
Enrico Granatae992a082011-07-22 17:03:19 +00001102 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eEndOfString;
1103 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::ePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001104
Greg Clayton1b654882010-09-19 02:33:57 +00001105 // Each variable must set success to true below...
1106 bool var_success = false;
1107 switch (var_name_begin[0])
1108 {
Enrico Granata4becb372011-06-29 22:27:15 +00001109 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001110 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001111 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001112 {
Enrico Granatac482a192011-08-17 22:13:59 +00001113 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001114 break;
1115
Enrico Granatac3e320a2011-08-02 17:27:39 +00001116 if (log)
1117 log->Printf("initial string: %s",var_name_begin);
1118
Enrico Granata6f3533f2011-07-29 19:53:35 +00001119 // check for *var and *svar
1120 if (*var_name_begin == '*')
1121 {
1122 do_deref_pointer = true;
1123 var_name_begin++;
1124 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001125
1126 if (log)
1127 log->Printf("initial string: %s",var_name_begin);
1128
Enrico Granata6f3533f2011-07-29 19:53:35 +00001129 if (*var_name_begin == 's')
1130 {
Greg Clayton4d122c42011-09-17 08:33:22 +00001131 valobj = valobj->GetSyntheticValue(eUseSyntheticFilter).get();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001132 var_name_begin++;
1133 }
1134
Enrico Granatac3e320a2011-08-02 17:27:39 +00001135 if (log)
1136 log->Printf("initial string: %s",var_name_begin);
1137
Enrico Granata6f3533f2011-07-29 19:53:35 +00001138 // should be a 'v' by now
1139 if (*var_name_begin != 'v')
1140 break;
1141
Enrico Granatac3e320a2011-08-02 17:27:39 +00001142 if (log)
1143 log->Printf("initial string: %s",var_name_begin);
1144
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001145 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
1146 ValueObject::eDereference : ValueObject::eNothing);
1147 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001148 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Greg Clayton34132752011-07-06 04:07:21 +00001149 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eDisplaySummary;
1150 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001151 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001152 const char* var_name_final = NULL;
1153 const char* var_name_final_if_array_range = NULL;
1154 const char* close_bracket_position = NULL;
1155 int64_t index_lower = -1;
1156 int64_t index_higher = -1;
1157 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001158 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001159 bool was_plain_var = false;
1160 bool was_var_format = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001161
Enrico Granatac482a192011-08-17 22:13:59 +00001162 if (!valobj) break;
1163 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001164 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001165 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001166 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001167 target = valobj;
Greg Clayton34132752011-07-06 04:07:21 +00001168 val_obj_display = ValueObject::eDisplayValue;
1169 }
1170 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1171 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001172 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001173 // this is a variable with some custom format applied to it
1174 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001175 target = valobj;
Greg Clayton34132752011-07-06 04:07:21 +00001176 val_obj_display = ValueObject::eDisplayValue;
1177 ScanFormatDescriptor (var_name_begin,
1178 var_name_end,
1179 &var_name_final,
1180 &percent_position,
1181 &custom_format,
1182 &val_obj_display);
1183 }
1184 // this is ${var.something} or multiple .something nested
1185 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1186 {
1187
1188 const char* percent_position;
1189 ScanFormatDescriptor (var_name_begin,
1190 var_name_end,
1191 &var_name_final,
1192 &percent_position,
1193 &custom_format,
1194 &val_obj_display);
1195
1196 const char* open_bracket_position;
1197 const char* separator_position;
1198 ScanBracketedRange (var_name_begin,
1199 var_name_end,
1200 var_name_final,
1201 &open_bracket_position,
1202 &separator_position,
1203 &close_bracket_position,
1204 &var_name_final_if_array_range,
1205 &index_lower,
1206 &index_higher);
1207
1208 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001209
1210 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1211 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1212 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1213
Enrico Granatae992a082011-07-22 17:03:19 +00001214 if (log)
1215 log->Printf("symbol to expand: %s",expr_path.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001216
Enrico Granatac482a192011-08-17 22:13:59 +00001217 target = valobj->GetValueForExpressionPath(expr_path.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001218 &first_unparsed,
1219 &reason_to_stop,
1220 &final_value_type,
1221 options,
1222 &what_next).get();
1223
1224 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001225 {
Enrico Granatae992a082011-07-22 17:03:19 +00001226 if (log)
1227 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1228 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001229 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001230 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001231 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001232 else
1233 {
Enrico Granatae992a082011-07-22 17:03:19 +00001234 if (log)
1235 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1236 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001237 first_unparsed, reason_to_stop, final_value_type);
1238 }
Enrico Granata4becb372011-06-29 22:27:15 +00001239 }
Greg Clayton34132752011-07-06 04:07:21 +00001240 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001241 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001242
1243 is_array_range = (final_value_type == ValueObject::eBoundedRange ||
1244 final_value_type == ValueObject::eUnboundedRange);
1245
1246 do_deref_pointer = (what_next == ValueObject::eDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001247
Enrico Granataa7187d02011-07-06 19:27:11 +00001248 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001249 {
Greg Clayton34132752011-07-06 04:07:21 +00001250 // I have not deref-ed yet, let's do it
1251 // this happens when we are not going through GetValueForVariableExpressionPath
1252 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001253 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001254 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001255 if (error.Fail())
1256 {
1257 if (log)
1258 log->Printf("ERROR: %s\n", error.AsCString("unknown")); \
1259 break;
1260 }
Greg Clayton34132752011-07-06 04:07:21 +00001261 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001262 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001263
Enrico Granata85933ed2011-08-18 16:38:26 +00001264 // TODO use flags for these
Enrico Granataf4efecd2011-07-12 22:56:10 +00001265 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1266 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001267 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001268
1269 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eDisplayValue) // this should be wrong, but there are some exceptions
1270 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001271 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001272 if (log)
1273 log->Printf("I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001274
1275 if (target->HasSpecialCasesForPrintableRepresentation(val_obj_display,
1276 custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001277 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001278 // try to use the special cases
1279 var_success = target->DumpPrintableRepresentation(str_temp,
1280 val_obj_display,
1281 custom_format);
1282 if (log)
1283 log->Printf("special cases did%s match", var_success ? "" : "n't");
1284
1285 // should not happen
1286 if (!var_success)
1287 s << "<invalid usage of pointer value as object>";
1288 else
1289 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001290 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001291 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001292 }
1293 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001294 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001295 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001296 {
1297 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1298 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001299 else if (is_pointer) // if pointer, value is the address stored
1300 {
1301 var_success = target->GetPrintableRepresentation(s,
1302 val_obj_display,
1303 custom_format);
1304 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001305 else
1306 {
1307 s << "<invalid usage of pointer value as object>";
1308 }
1309 var_success = true;
1310 break;
1311 }
1312 }
1313
1314 // if directly trying to print ${var}, and this is an aggregate, display a nice
1315 // type @ location message
1316 if (is_aggregate && was_plain_var)
1317 {
1318 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1319 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001320 break;
1321 }
1322
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001323 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it
1324 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eDisplayValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001325 {
1326 s << "<invalid use of aggregate type>";
1327 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001328 break;
1329 }
Greg Clayton34132752011-07-06 04:07:21 +00001330
1331 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001332 {
1333 if (log)
1334 log->Printf("dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001335 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001336 }
Greg Clayton34132752011-07-06 04:07:21 +00001337 else
Enrico Granatae992a082011-07-22 17:03:19 +00001338 {
1339 if (log)
1340 log->Printf("checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001341 if (!is_array && !is_pointer)
1342 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001343 if (log)
1344 log->Printf("handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001345 const char* special_directions = NULL;
1346 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001347 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1348 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001349 ConstString additional_data;
1350 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1351 special_directions_writer.Printf("${%svar%s}",
1352 do_deref_pointer ? "*" : "",
1353 additional_data.GetCString());
1354 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001355 }
1356
1357 // let us display items index_lower thru index_higher of this array
1358 s.PutChar('[');
1359 var_success = true;
1360
1361 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001362 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001363
Enrico Granata22c55d12011-08-12 02:00:06 +00001364 uint32_t max_num_children = target->GetUpdatePoint().GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
1365
Greg Clayton34132752011-07-06 04:07:21 +00001366 for (;index_lower<=index_higher;index_lower++)
1367 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001368 ValueObject* item = ExpandIndexedExpression (target,
1369 index_lower,
1370 exe_ctx->GetFramePtr(),
1371 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001372
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001373 if (!item)
1374 {
Enrico Granatae992a082011-07-22 17:03:19 +00001375 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001376 log->Printf("ERROR in getting child item at index %lld", index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001377 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001378 else
1379 {
Enrico Granatae992a082011-07-22 17:03:19 +00001380 if (log)
1381 log->Printf("special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001382 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001383
Greg Clayton34132752011-07-06 04:07:21 +00001384 if (!special_directions)
1385 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1386 else
1387 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1388
Enrico Granata22c55d12011-08-12 02:00:06 +00001389 if (--max_num_children == 0)
1390 {
1391 s.PutCString(", ...");
1392 break;
1393 }
1394
Greg Clayton34132752011-07-06 04:07:21 +00001395 if (index_lower < index_higher)
1396 s.PutChar(',');
1397 }
1398 s.PutChar(']');
1399 }
Enrico Granata4becb372011-06-29 22:27:15 +00001400 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001401 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001402 case 'a':
1403 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1404 {
1405 if (addr && addr->IsValid())
1406 {
1407 var_success = true;
1408 format_addr = *addr;
1409 }
1410 }
Greg Clayton5a314712011-10-14 07:41:33 +00001411 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1412 {
1413 var_success = true;
1414 var_name_begin += strlen("ansi."); // Skip the "ansi."
1415 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1416 {
1417 var_name_begin += strlen("fg."); // Skip the "fg."
1418 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1419 {
1420 s.Printf ("%s%s%s",
1421 lldb_utility::ansi::k_escape_start,
1422 lldb_utility::ansi::k_fg_black,
1423 lldb_utility::ansi::k_escape_end);
1424 }
1425 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1426 {
1427 s.Printf ("%s%s%s",
1428 lldb_utility::ansi::k_escape_start,
1429 lldb_utility::ansi::k_fg_red,
1430 lldb_utility::ansi::k_escape_end);
1431 }
1432 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1433 {
1434 s.Printf ("%s%s%s",
1435 lldb_utility::ansi::k_escape_start,
1436 lldb_utility::ansi::k_fg_green,
1437 lldb_utility::ansi::k_escape_end);
1438 }
1439 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1440 {
1441 s.Printf ("%s%s%s",
1442 lldb_utility::ansi::k_escape_start,
1443 lldb_utility::ansi::k_fg_yellow,
1444 lldb_utility::ansi::k_escape_end);
1445 }
1446 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1447 {
1448 s.Printf ("%s%s%s",
1449 lldb_utility::ansi::k_escape_start,
1450 lldb_utility::ansi::k_fg_blue,
1451 lldb_utility::ansi::k_escape_end);
1452 }
1453 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1454 {
1455 s.Printf ("%s%s%s",
1456 lldb_utility::ansi::k_escape_start,
1457 lldb_utility::ansi::k_fg_purple,
1458 lldb_utility::ansi::k_escape_end);
1459 }
1460 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1461 {
1462 s.Printf ("%s%s%s",
1463 lldb_utility::ansi::k_escape_start,
1464 lldb_utility::ansi::k_fg_cyan,
1465 lldb_utility::ansi::k_escape_end);
1466 }
1467 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1468 {
1469 s.Printf ("%s%s%s",
1470 lldb_utility::ansi::k_escape_start,
1471 lldb_utility::ansi::k_fg_white,
1472 lldb_utility::ansi::k_escape_end);
1473 }
1474 else
1475 {
1476 var_success = false;
1477 }
1478 }
1479 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1480 {
1481 var_name_begin += strlen("bg."); // Skip the "bg."
1482 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1483 {
1484 s.Printf ("%s%s%s",
1485 lldb_utility::ansi::k_escape_start,
1486 lldb_utility::ansi::k_bg_black,
1487 lldb_utility::ansi::k_escape_end);
1488 }
1489 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1490 {
1491 s.Printf ("%s%s%s",
1492 lldb_utility::ansi::k_escape_start,
1493 lldb_utility::ansi::k_bg_red,
1494 lldb_utility::ansi::k_escape_end);
1495 }
1496 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1497 {
1498 s.Printf ("%s%s%s",
1499 lldb_utility::ansi::k_escape_start,
1500 lldb_utility::ansi::k_bg_green,
1501 lldb_utility::ansi::k_escape_end);
1502 }
1503 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1504 {
1505 s.Printf ("%s%s%s",
1506 lldb_utility::ansi::k_escape_start,
1507 lldb_utility::ansi::k_bg_yellow,
1508 lldb_utility::ansi::k_escape_end);
1509 }
1510 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1511 {
1512 s.Printf ("%s%s%s",
1513 lldb_utility::ansi::k_escape_start,
1514 lldb_utility::ansi::k_bg_blue,
1515 lldb_utility::ansi::k_escape_end);
1516 }
1517 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1518 {
1519 s.Printf ("%s%s%s",
1520 lldb_utility::ansi::k_escape_start,
1521 lldb_utility::ansi::k_bg_purple,
1522 lldb_utility::ansi::k_escape_end);
1523 }
1524 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1525 {
1526 s.Printf ("%s%s%s",
1527 lldb_utility::ansi::k_escape_start,
1528 lldb_utility::ansi::k_bg_cyan,
1529 lldb_utility::ansi::k_escape_end);
1530 }
1531 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1532 {
1533 s.Printf ("%s%s%s",
1534 lldb_utility::ansi::k_escape_start,
1535 lldb_utility::ansi::k_bg_white,
1536 lldb_utility::ansi::k_escape_end);
1537 }
1538 else
1539 {
1540 var_success = false;
1541 }
1542 }
1543 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1544 {
1545 s.Printf ("%s%s%s",
1546 lldb_utility::ansi::k_escape_start,
1547 lldb_utility::ansi::k_ctrl_normal,
1548 lldb_utility::ansi::k_escape_end);
1549 }
1550 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1551 {
1552 s.Printf ("%s%s%s",
1553 lldb_utility::ansi::k_escape_start,
1554 lldb_utility::ansi::k_ctrl_bold,
1555 lldb_utility::ansi::k_escape_end);
1556 }
1557 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1558 {
1559 s.Printf ("%s%s%s",
1560 lldb_utility::ansi::k_escape_start,
1561 lldb_utility::ansi::k_ctrl_faint,
1562 lldb_utility::ansi::k_escape_end);
1563 }
1564 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1565 {
1566 s.Printf ("%s%s%s",
1567 lldb_utility::ansi::k_escape_start,
1568 lldb_utility::ansi::k_ctrl_italic,
1569 lldb_utility::ansi::k_escape_end);
1570 }
1571 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1572 {
1573 s.Printf ("%s%s%s",
1574 lldb_utility::ansi::k_escape_start,
1575 lldb_utility::ansi::k_ctrl_underline,
1576 lldb_utility::ansi::k_escape_end);
1577 }
1578 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1579 {
1580 s.Printf ("%s%s%s",
1581 lldb_utility::ansi::k_escape_start,
1582 lldb_utility::ansi::k_ctrl_slow_blink,
1583 lldb_utility::ansi::k_escape_end);
1584 }
1585 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1586 {
1587 s.Printf ("%s%s%s",
1588 lldb_utility::ansi::k_escape_start,
1589 lldb_utility::ansi::k_ctrl_fast_blink,
1590 lldb_utility::ansi::k_escape_end);
1591 }
1592 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1593 {
1594 s.Printf ("%s%s%s",
1595 lldb_utility::ansi::k_escape_start,
1596 lldb_utility::ansi::k_ctrl_negative,
1597 lldb_utility::ansi::k_escape_end);
1598 }
1599 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1600 {
1601 s.Printf ("%s%s%s",
1602 lldb_utility::ansi::k_escape_start,
1603 lldb_utility::ansi::k_ctrl_conceal,
1604 lldb_utility::ansi::k_escape_end);
1605
1606 }
1607 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1608 {
1609 s.Printf ("%s%s%s",
1610 lldb_utility::ansi::k_escape_start,
1611 lldb_utility::ansi::k_ctrl_crossed_out,
1612 lldb_utility::ansi::k_escape_end);
1613 }
1614 else
1615 {
1616 var_success = false;
1617 }
1618 }
Greg Clayton1b654882010-09-19 02:33:57 +00001619 break;
1620
1621 case 'p':
1622 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1623 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001624 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001625 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001626 Process *process = exe_ctx->GetProcessPtr();
1627 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001628 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001629 var_name_begin += ::strlen ("process.");
1630 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001631 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001632 s.Printf("%llu", process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001633 var_success = true;
1634 }
1635 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1636 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1637 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1638 {
1639 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1640 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001641 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001642 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1643 {
1644 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1645 var_success = format_file_spec;
1646 }
1647 else
1648 {
1649 format_file_spec = exe_module->GetFileSpec();
1650 var_success = format_file_spec;
1651 }
Greg Clayton1b654882010-09-19 02:33:57 +00001652 }
1653 }
1654 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001655 }
Greg Clayton1b654882010-09-19 02:33:57 +00001656 }
1657 break;
1658
1659 case 't':
1660 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1661 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001662 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001663 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001664 Thread *thread = exe_ctx->GetThreadPtr();
1665 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001666 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001667 var_name_begin += ::strlen ("thread.");
1668 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001669 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001670 s.Printf("0x%4.4llx", thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001671 var_success = true;
1672 }
1673 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1674 {
1675 s.Printf("%u", thread->GetIndexID());
1676 var_success = true;
1677 }
1678 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1679 {
1680 cstr = thread->GetName();
1681 var_success = cstr && cstr[0];
1682 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001683 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001684 }
1685 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1686 {
1687 cstr = thread->GetQueueName();
1688 var_success = cstr && cstr[0];
1689 if (var_success)
1690 s.PutCString(cstr);
1691 }
1692 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1693 {
1694 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1695 if (stop_info_sp)
1696 {
1697 cstr = stop_info_sp->GetDescription();
1698 if (cstr && cstr[0])
1699 {
1700 s.PutCString(cstr);
1701 var_success = true;
1702 }
Greg Clayton1b654882010-09-19 02:33:57 +00001703 }
1704 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001705 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1706 {
1707 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1708 if (stop_info_sp)
1709 {
1710 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1711 if (return_valobj_sp)
1712 {
Jim Inghamef651602011-12-22 19:12:40 +00001713 ValueObject::DumpValueObjectOptions dump_options;
1714 ValueObject::DumpValueObject (s, return_valobj_sp.get(), dump_options);
1715 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001716 }
1717 }
1718 }
Greg Clayton1b654882010-09-19 02:33:57 +00001719 }
1720 }
1721 }
1722 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1723 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001724 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1725 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001726 {
Greg Clayton1b654882010-09-19 02:33:57 +00001727 var_name_begin += ::strlen ("target.");
1728 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1729 {
1730 ArchSpec arch (target->GetArchitecture ());
1731 if (arch.IsValid())
1732 {
Greg Clayton64195a22011-02-23 00:35:02 +00001733 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001734 var_success = true;
1735 }
1736 }
1737 }
1738 }
1739 break;
1740
1741
1742 case 'm':
1743 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1744 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001745 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001746 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001747 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001748 var_name_begin += ::strlen ("module.");
1749
1750 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1751 {
1752 if (module->GetFileSpec())
1753 {
1754 var_name_begin += ::strlen ("file.");
1755
1756 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1757 {
1758 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1759 var_success = format_file_spec;
1760 }
1761 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1762 {
1763 format_file_spec = module->GetFileSpec();
1764 var_success = format_file_spec;
1765 }
1766 }
1767 }
1768 }
1769 }
1770 break;
1771
1772
1773 case 'f':
1774 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1775 {
1776 if (sc && sc->comp_unit != NULL)
1777 {
1778 var_name_begin += ::strlen ("file.");
1779
1780 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1781 {
1782 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1783 var_success = format_file_spec;
1784 }
1785 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1786 {
1787 format_file_spec = *sc->comp_unit;
1788 var_success = format_file_spec;
1789 }
1790 }
1791 }
1792 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1793 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001794 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001795 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001796 StackFrame *frame = exe_ctx->GetFramePtr();
1797 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001798 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001799 var_name_begin += ::strlen ("frame.");
1800 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001801 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001802 s.Printf("%u", frame->GetFrameIndex());
1803 var_success = true;
1804 }
1805 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1806 {
1807 reg_kind = eRegisterKindGeneric;
1808 reg_num = LLDB_REGNUM_GENERIC_PC;
1809 var_success = true;
1810 }
1811 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1812 {
1813 reg_kind = eRegisterKindGeneric;
1814 reg_num = LLDB_REGNUM_GENERIC_SP;
1815 var_success = true;
1816 }
1817 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1818 {
1819 reg_kind = eRegisterKindGeneric;
1820 reg_num = LLDB_REGNUM_GENERIC_FP;
1821 var_success = true;
1822 }
1823 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
1824 {
1825 reg_kind = eRegisterKindGeneric;
1826 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
1827 var_success = true;
1828 }
1829 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
1830 {
1831 reg_ctx = frame->GetRegisterContext().get();
1832 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001833 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001834 var_name_begin += ::strlen ("reg.");
1835 if (var_name_begin < var_name_end)
1836 {
1837 std::string reg_name (var_name_begin, var_name_end);
1838 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
1839 if (reg_info)
1840 var_success = true;
1841 }
Greg Clayton1b654882010-09-19 02:33:57 +00001842 }
1843 }
1844 }
1845 }
1846 }
1847 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
1848 {
1849 if (sc && (sc->function != NULL || sc->symbol != NULL))
1850 {
1851 var_name_begin += ::strlen ("function.");
1852 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1853 {
1854 if (sc->function)
Greg Clayton81c22f62011-10-19 18:09:39 +00001855 s.Printf("function{0x%8.8llx}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00001856 else
1857 s.Printf("symbol[%u]", sc->symbol->GetID());
1858
1859 var_success = true;
1860 }
1861 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1862 {
1863 if (sc->function)
1864 cstr = sc->function->GetName().AsCString (NULL);
1865 else if (sc->symbol)
1866 cstr = sc->symbol->GetName().AsCString (NULL);
1867 if (cstr)
1868 {
1869 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00001870
1871 if (sc->block)
1872 {
1873 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1874 if (inline_block)
1875 {
1876 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
1877 if (inline_info)
1878 {
1879 s.PutCString(" [inlined] ");
1880 inline_info->GetName().Dump(&s);
1881 }
1882 }
1883 }
Greg Clayton1b654882010-09-19 02:33:57 +00001884 var_success = true;
1885 }
1886 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00001887 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
1888 {
1889 // Print the function name with arguments in it
1890
1891 if (sc->function)
1892 {
1893 var_success = true;
1894 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
1895 cstr = sc->function->GetName().AsCString (NULL);
1896 if (cstr)
1897 {
1898 const InlineFunctionInfo *inline_info = NULL;
1899 VariableListSP variable_list_sp;
1900 bool get_function_vars = true;
1901 if (sc->block)
1902 {
1903 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1904
1905 if (inline_block)
1906 {
1907 get_function_vars = false;
1908 inline_info = sc->block->GetInlinedFunctionInfo();
1909 if (inline_info)
1910 variable_list_sp = inline_block->GetBlockVariableList (true);
1911 }
1912 }
1913
1914 if (get_function_vars)
1915 {
1916 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
1917 }
1918
1919 if (inline_info)
1920 {
1921 s.PutCString (cstr);
1922 s.PutCString (" [inlined] ");
1923 cstr = inline_info->GetName().GetCString();
1924 }
1925
1926 VariableList args;
1927 if (variable_list_sp)
1928 {
1929 const size_t num_variables = variable_list_sp->GetSize();
1930 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
1931 {
1932 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
1933 if (var_sp->GetScope() == eValueTypeVariableArgument)
1934 args.AddVariable (var_sp);
1935 }
1936
1937 }
1938 if (args.GetSize() > 0)
1939 {
1940 const char *open_paren = strchr (cstr, '(');
1941 const char *close_paren = NULL;
1942 if (open_paren)
1943 close_paren = strchr (open_paren, ')');
1944
1945 if (open_paren)
1946 s.Write(cstr, open_paren - cstr + 1);
1947 else
1948 {
1949 s.PutCString (cstr);
1950 s.PutChar ('(');
1951 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00001952 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00001953 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
1954 {
1955 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
1956 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
1957 const char *var_name = var_value_sp->GetName().GetCString();
1958 const char *var_value = var_value_sp->GetValueAsCString();
1959 if (var_value_sp->GetError().Success())
1960 {
1961 if (arg_idx > 0)
1962 s.PutCString (", ");
1963 s.Printf ("%s=%s", var_name, var_value);
1964 }
1965 }
1966
1967 if (close_paren)
1968 s.PutCString (close_paren);
1969 else
1970 s.PutChar(')');
1971
1972 }
1973 else
1974 {
1975 s.PutCString(cstr);
1976 }
1977 }
1978 }
1979 else if (sc->symbol)
1980 {
1981 cstr = sc->symbol->GetName().AsCString (NULL);
1982 if (cstr)
1983 {
1984 s.PutCString(cstr);
1985 var_success = true;
1986 }
1987 }
1988 }
Greg Clayton1b654882010-09-19 02:33:57 +00001989 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
1990 {
1991 var_success = addr != NULL;
1992 if (var_success)
1993 {
1994 format_addr = *addr;
1995 calculate_format_addr_function_offset = true;
1996 }
1997 }
1998 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
1999 {
2000 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2001 if (var_success)
2002 {
2003 format_addr = sc->line_entry.range.GetBaseAddress();
2004 calculate_format_addr_function_offset = true;
2005 }
2006 }
2007 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2008 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002009 StackFrame *frame = exe_ctx->GetFramePtr();
2010 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002011 if (var_success)
2012 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002013 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002014 calculate_format_addr_function_offset = true;
2015 }
2016 }
2017 }
2018 }
2019 break;
2020
2021 case 'l':
2022 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2023 {
2024 if (sc && sc->line_entry.IsValid())
2025 {
2026 var_name_begin += ::strlen ("line.");
2027 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2028 {
2029 var_name_begin += ::strlen ("file.");
2030
2031 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2032 {
2033 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2034 var_success = format_file_spec;
2035 }
2036 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2037 {
2038 format_file_spec = sc->line_entry.file;
2039 var_success = format_file_spec;
2040 }
2041 }
2042 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2043 {
2044 var_success = true;
2045 s.Printf("%u", sc->line_entry.line);
2046 }
2047 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2048 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2049 {
2050 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2051 if (var_success)
2052 {
2053 format_addr = sc->line_entry.range.GetBaseAddress();
2054 if (var_name_begin[0] == 'e')
2055 format_addr.Slide (sc->line_entry.range.GetByteSize());
2056 }
2057 }
2058 }
2059 }
2060 break;
2061 }
2062
2063 if (var_success)
2064 {
2065 // If format addr is valid, then we need to print an address
2066 if (reg_num != LLDB_INVALID_REGNUM)
2067 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002068 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002069 // We have a register value to display...
2070 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2071 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002072 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002073 }
2074 else
2075 {
2076 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002077 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002078
2079 if (reg_ctx)
2080 {
2081 if (reg_kind != kNumRegisterKinds)
2082 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2083 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2084 var_success = reg_info != NULL;
2085 }
2086 }
2087 }
2088
2089 if (reg_info != NULL)
2090 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002091 RegisterValue reg_value;
2092 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2093 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002094 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002095 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002096 }
2097 }
2098
2099 if (format_file_spec)
2100 {
2101 s << format_file_spec;
2102 }
2103
2104 // If format addr is valid, then we need to print an address
2105 if (format_addr.IsValid())
2106 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002107 var_success = false;
2108
Greg Clayton1b654882010-09-19 02:33:57 +00002109 if (calculate_format_addr_function_offset)
2110 {
2111 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002112
Greg Clayton0603aa92010-10-04 01:05:56 +00002113 if (sc)
2114 {
2115 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002116 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002117 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002118 if (sc->block)
2119 {
2120 // Check to make sure we aren't in an inline
2121 // function. If we are, use the inline block
2122 // range that contains "format_addr" since
2123 // blocks can be discontiguous.
2124 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2125 AddressRange inline_range;
2126 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2127 func_addr = inline_range.GetBaseAddress();
2128 }
2129 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002130 else if (sc->symbol && sc->symbol->GetAddressRangePtr())
2131 func_addr = sc->symbol->GetAddressRangePtr()->GetBaseAddress();
2132 }
2133
2134 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002135 {
2136 if (func_addr.GetSection() == format_addr.GetSection())
2137 {
2138 addr_t func_file_addr = func_addr.GetFileAddress();
2139 addr_t addr_file_addr = format_addr.GetFileAddress();
2140 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002141 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002142 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002143 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002144 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002145 }
2146 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002147 {
2148 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2149 if (target)
2150 {
2151 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2152 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2153 if (addr_load_addr > func_load_addr)
2154 s.Printf(" + %llu", addr_load_addr - func_load_addr);
2155 else if (addr_load_addr < func_load_addr)
2156 s.Printf(" - %llu", func_load_addr - addr_load_addr);
2157 var_success = true;
2158 }
2159 }
Greg Clayton1b654882010-09-19 02:33:57 +00002160 }
2161 }
2162 else
2163 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002164 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002165 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002166 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2167 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002168 if (vaddr == LLDB_INVALID_ADDRESS)
2169 vaddr = format_addr.GetFileAddress ();
2170
2171 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002172 {
Greg Clayton514487e2011-02-15 21:59:32 +00002173 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002174 if (addr_width == 0)
2175 addr_width = 16;
2176 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002177 var_success = true;
2178 }
Greg Clayton1b654882010-09-19 02:33:57 +00002179 }
2180 }
2181 }
2182
2183 if (var_success == false)
2184 success = false;
2185 }
2186 p = var_name_end;
2187 }
2188 else
2189 break;
2190 }
2191 else
2192 {
2193 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2194 s.PutChar(*p);
2195 }
2196 }
2197 else if (*p == '\\')
2198 {
2199 ++p; // skip the slash
2200 switch (*p)
2201 {
2202 case 'a': s.PutChar ('\a'); break;
2203 case 'b': s.PutChar ('\b'); break;
2204 case 'f': s.PutChar ('\f'); break;
2205 case 'n': s.PutChar ('\n'); break;
2206 case 'r': s.PutChar ('\r'); break;
2207 case 't': s.PutChar ('\t'); break;
2208 case 'v': s.PutChar ('\v'); break;
2209 case '\'': s.PutChar ('\''); break;
2210 case '\\': s.PutChar ('\\'); break;
2211 case '0':
2212 // 1 to 3 octal chars
2213 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002214 // Make a string that can hold onto the initial zero char,
2215 // up to 3 octal digits, and a terminating NULL.
2216 char oct_str[5] = { 0, 0, 0, 0, 0 };
2217
2218 int i;
2219 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2220 oct_str[i] = p[i];
2221
2222 // We don't want to consume the last octal character since
2223 // the main for loop will do this for us, so we advance p by
2224 // one less than i (even if i is zero)
2225 p += i - 1;
2226 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2227 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002228 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002229 char octal_char = octal_value;
2230 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00002231 }
Greg Clayton1b654882010-09-19 02:33:57 +00002232 }
2233 break;
2234
2235 case 'x':
2236 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002237 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002238 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002239 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002240
Greg Clayton0603aa92010-10-04 01:05:56 +00002241 // Make a string that can hold onto two hex chars plus a
2242 // NULL terminator
2243 char hex_str[3] = { 0,0,0 };
2244 hex_str[0] = *p;
2245 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002246 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002247 ++p; // Skip the first of the two hex chars
2248 hex_str[1] = *p;
2249 }
2250
2251 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2252 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002253 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002254 }
2255 else
2256 {
2257 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002258 }
2259 break;
2260
2261 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002262 // Just desensitize any other character by just printing what
2263 // came after the '\'
2264 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002265 break;
2266
2267 }
2268
2269 }
2270 }
2271 if (end)
2272 *end = p;
2273 return success;
2274}
2275
2276#pragma mark Debugger::SettingsController
2277
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002278//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00002279// class Debugger::SettingsController
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002280//--------------------------------------------------
2281
Greg Clayton1b654882010-09-19 02:33:57 +00002282Debugger::SettingsController::SettingsController () :
Greg Clayton4d122c42011-09-17 08:33:22 +00002283 UserSettingsController ("", UserSettingsControllerSP())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002284{
Caroline Tice91123da2010-09-08 17:48:55 +00002285 m_default_settings.reset (new DebuggerInstanceSettings (*this, false,
2286 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002287}
2288
Greg Clayton1b654882010-09-19 02:33:57 +00002289Debugger::SettingsController::~SettingsController ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002290{
2291}
2292
2293
Greg Clayton4d122c42011-09-17 08:33:22 +00002294InstanceSettingsSP
Greg Clayton1b654882010-09-19 02:33:57 +00002295Debugger::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002296{
Greg Claytondbe54502010-11-19 03:46:01 +00002297 DebuggerInstanceSettings *new_settings = new DebuggerInstanceSettings (*GetSettingsController(),
Caroline Tice91123da2010-09-08 17:48:55 +00002298 false, instance_name);
Greg Clayton4d122c42011-09-17 08:33:22 +00002299 InstanceSettingsSP new_settings_sp (new_settings);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002300 return new_settings_sp;
2301}
2302
Greg Clayton1b654882010-09-19 02:33:57 +00002303#pragma mark DebuggerInstanceSettings
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002304//--------------------------------------------------
2305// class DebuggerInstanceSettings
2306//--------------------------------------------------
2307
Greg Claytona7015092010-09-18 01:14:36 +00002308DebuggerInstanceSettings::DebuggerInstanceSettings
2309(
2310 UserSettingsController &owner,
2311 bool live_instance,
2312 const char *name
2313) :
Greg Clayton85851dd2010-12-04 00:10:17 +00002314 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytona7015092010-09-18 01:14:36 +00002315 m_term_width (80),
Greg Claytone372b982011-11-21 21:44:34 +00002316 m_stop_source_before_count (3),
2317 m_stop_source_after_count (3),
2318 m_stop_disassembly_count (4),
2319 m_stop_disassembly_display (eStopDisassemblyTypeNoSource),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002320 m_prompt (),
Greg Clayton0603aa92010-10-04 01:05:56 +00002321 m_frame_format (),
2322 m_thread_format (),
Caroline Ticedaccaa92010-09-20 20:44:43 +00002323 m_script_lang (),
Jim Ingham3bcdb292010-10-04 22:44:14 +00002324 m_use_external_editor (false),
2325 m_auto_confirm_on (false)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002326{
Caroline Ticef20e8232010-09-09 18:26:37 +00002327 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2328 // until the vtables for DebuggerInstanceSettings are properly set up, i.e. AFTER all the initializers.
2329 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
Caroline Tice9e41c152010-09-16 19:05:55 +00002330 // The same is true of CreateInstanceName().
2331
2332 if (GetInstanceName() == InstanceSettings::InvalidName())
2333 {
2334 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2335 m_owner.RegisterInstanceSettings (this);
2336 }
Caroline Ticef20e8232010-09-09 18:26:37 +00002337
2338 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002339 {
Greg Clayton4d122c42011-09-17 08:33:22 +00002340 const InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002341 CopyInstanceSettings (pending_settings, false);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002342 }
2343}
2344
2345DebuggerInstanceSettings::DebuggerInstanceSettings (const DebuggerInstanceSettings &rhs) :
Greg Clayton99d0faf2010-11-18 23:32:35 +00002346 InstanceSettings (*Debugger::GetSettingsController(), CreateInstanceName ().AsCString()),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002347 m_prompt (rhs.m_prompt),
Greg Clayton0603aa92010-10-04 01:05:56 +00002348 m_frame_format (rhs.m_frame_format),
2349 m_thread_format (rhs.m_thread_format),
Caroline Ticedaccaa92010-09-20 20:44:43 +00002350 m_script_lang (rhs.m_script_lang),
Jim Ingham3bcdb292010-10-04 22:44:14 +00002351 m_use_external_editor (rhs.m_use_external_editor),
2352 m_auto_confirm_on(rhs.m_auto_confirm_on)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002353{
Greg Clayton4d122c42011-09-17 08:33:22 +00002354 const InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002355 CopyInstanceSettings (pending_settings, false);
2356 m_owner.RemovePendingSettings (m_instance_name);
2357}
2358
2359DebuggerInstanceSettings::~DebuggerInstanceSettings ()
2360{
2361}
2362
2363DebuggerInstanceSettings&
2364DebuggerInstanceSettings::operator= (const DebuggerInstanceSettings &rhs)
2365{
2366 if (this != &rhs)
2367 {
Greg Clayton1b654882010-09-19 02:33:57 +00002368 m_term_width = rhs.m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002369 m_prompt = rhs.m_prompt;
Greg Clayton0603aa92010-10-04 01:05:56 +00002370 m_frame_format = rhs.m_frame_format;
2371 m_thread_format = rhs.m_thread_format;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002372 m_script_lang = rhs.m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002373 m_use_external_editor = rhs.m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002374 m_auto_confirm_on = rhs.m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002375 }
2376
2377 return *this;
2378}
2379
Greg Clayton1b654882010-09-19 02:33:57 +00002380bool
2381DebuggerInstanceSettings::ValidTermWidthValue (const char *value, Error err)
2382{
2383 bool valid = false;
2384
2385 // Verify we have a value string.
2386 if (value == NULL || value[0] == '\0')
2387 {
Greg Clayton86edbf42011-10-26 00:56:27 +00002388 err.SetErrorString ("missing value, can't set terminal width without a value");
Greg Clayton1b654882010-09-19 02:33:57 +00002389 }
2390 else
2391 {
2392 char *end = NULL;
2393 const uint32_t width = ::strtoul (value, &end, 0);
2394
Johnny Chenea9fc182010-09-20 16:36:43 +00002395 if (end && end[0] == '\0')
Greg Clayton1b654882010-09-19 02:33:57 +00002396 {
Johnny Chen433d7742010-09-20 17:04:41 +00002397 if (width >= 10 && width <= 1024)
Greg Clayton1b654882010-09-19 02:33:57 +00002398 valid = true;
2399 else
Greg Clayton86edbf42011-10-26 00:56:27 +00002400 err.SetErrorString ("invalid term-width value; value must be between 10 and 1024");
Greg Clayton1b654882010-09-19 02:33:57 +00002401 }
2402 else
Greg Clayton86edbf42011-10-26 00:56:27 +00002403 err.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string", value);
Greg Clayton1b654882010-09-19 02:33:57 +00002404 }
2405
2406 return valid;
2407}
2408
2409
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002410void
2411DebuggerInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2412 const char *index_value,
2413 const char *value,
2414 const ConstString &instance_name,
2415 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:54 +00002416 VarSetOperationType op,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002417 Error &err,
2418 bool pending)
2419{
Greg Clayton0603aa92010-10-04 01:05:56 +00002420
2421 if (var_name == TermWidthVarName())
2422 {
2423 if (ValidTermWidthValue (value, err))
2424 {
2425 m_term_width = ::strtoul (value, NULL, 0);
2426 }
2427 }
2428 else if (var_name == PromptVarName())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002429 {
Caroline Tice101c7c22010-09-09 06:25:08 +00002430 UserSettingsController::UpdateStringVariable (op, m_prompt, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002431 if (!pending)
2432 {
Caroline Tice49e27372010-09-07 18:35:40 +00002433 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2434 // strip off the brackets before passing it to BroadcastPromptChange.
2435
2436 std::string tmp_instance_name (instance_name.AsCString());
2437 if ((tmp_instance_name[0] == '[')
2438 && (tmp_instance_name[instance_name.GetLength() - 1] == ']'))
2439 tmp_instance_name = tmp_instance_name.substr (1, instance_name.GetLength() - 2);
2440 ConstString new_name (tmp_instance_name.c_str());
2441
2442 BroadcastPromptChange (new_name, m_prompt.c_str());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002443 }
2444 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002445 else if (var_name == GetFrameFormatName())
2446 {
2447 UserSettingsController::UpdateStringVariable (op, m_frame_format, value, err);
2448 }
2449 else if (var_name == GetThreadFormatName())
2450 {
2451 UserSettingsController::UpdateStringVariable (op, m_thread_format, value, err);
2452 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002453 else if (var_name == ScriptLangVarName())
2454 {
2455 bool success;
2456 m_script_lang = Args::StringToScriptLanguage (value, eScriptLanguageDefault,
2457 &success);
2458 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002459 else if (var_name == UseExternalEditorVarName ())
2460 {
Greg Clayton385aa282011-04-22 03:55:06 +00002461 UserSettingsController::UpdateBooleanVariable (op, m_use_external_editor, value, false, err);
Caroline Ticedaccaa92010-09-20 20:44:43 +00002462 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002463 else if (var_name == AutoConfirmName ())
2464 {
Greg Clayton385aa282011-04-22 03:55:06 +00002465 UserSettingsController::UpdateBooleanVariable (op, m_auto_confirm_on, value, false, err);
Jim Ingham3bcdb292010-10-04 22:44:14 +00002466 }
Greg Claytone372b982011-11-21 21:44:34 +00002467 else if (var_name == StopSourceContextBeforeName ())
2468 {
2469 uint32_t new_value = Args::StringToUInt32(value, UINT32_MAX, 10, NULL);
2470 if (new_value != UINT32_MAX)
2471 m_stop_source_before_count = new_value;
2472 else
2473 err.SetErrorStringWithFormat("invalid unsigned string value '%s' for the '%s' setting", value, StopSourceContextAfterName ().GetCString());
2474 }
2475 else if (var_name == StopSourceContextAfterName ())
2476 {
2477 uint32_t new_value = Args::StringToUInt32(value, UINT32_MAX, 10, NULL);
2478 if (new_value != UINT32_MAX)
2479 m_stop_source_after_count = new_value;
2480 else
2481 err.SetErrorStringWithFormat("invalid unsigned string value '%s' for the '%s' setting", value, StopSourceContextBeforeName ().GetCString());
2482 }
2483 else if (var_name == StopDisassemblyCountName ())
2484 {
2485 uint32_t new_value = Args::StringToUInt32(value, UINT32_MAX, 10, NULL);
2486 if (new_value != UINT32_MAX)
2487 m_stop_disassembly_count = new_value;
2488 else
2489 err.SetErrorStringWithFormat("invalid unsigned string value '%s' for the '%s' setting", value, StopDisassemblyCountName ().GetCString());
2490 }
2491 else if (var_name == StopDisassemblyDisplayName ())
2492 {
2493 int new_value;
2494 UserSettingsController::UpdateEnumVariable (g_show_disassembly_enum_values, &new_value, value, err);
2495 if (err.Success())
2496 m_stop_disassembly_display = (StopDisassemblyType)new_value;
2497 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002498}
2499
Caroline Tice12cecd72010-09-20 21:37:42 +00002500bool
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002501DebuggerInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2502 const ConstString &var_name,
Caroline Ticedaccaa92010-09-20 20:44:43 +00002503 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42 +00002504 Error *err)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002505{
2506 if (var_name == PromptVarName())
2507 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002508 value.AppendString (m_prompt.c_str(), m_prompt.size());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002509
2510 }
2511 else if (var_name == ScriptLangVarName())
2512 {
2513 value.AppendString (ScriptInterpreter::LanguageToString (m_script_lang).c_str());
2514 }
Caroline Tice101c7c22010-09-09 06:25:08 +00002515 else if (var_name == TermWidthVarName())
2516 {
2517 StreamString width_str;
Greg Claytone372b982011-11-21 21:44:34 +00002518 width_str.Printf ("%u", m_term_width);
Caroline Tice101c7c22010-09-09 06:25:08 +00002519 value.AppendString (width_str.GetData());
2520 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002521 else if (var_name == GetFrameFormatName ())
2522 {
2523 value.AppendString(m_frame_format.c_str(), m_frame_format.size());
2524 }
2525 else if (var_name == GetThreadFormatName ())
2526 {
2527 value.AppendString(m_thread_format.c_str(), m_thread_format.size());
2528 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002529 else if (var_name == UseExternalEditorVarName())
2530 {
2531 if (m_use_external_editor)
2532 value.AppendString ("true");
2533 else
2534 value.AppendString ("false");
2535 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002536 else if (var_name == AutoConfirmName())
2537 {
2538 if (m_auto_confirm_on)
2539 value.AppendString ("true");
2540 else
2541 value.AppendString ("false");
2542 }
Greg Claytone372b982011-11-21 21:44:34 +00002543 else if (var_name == StopSourceContextAfterName ())
2544 {
2545 StreamString strm;
2546 strm.Printf ("%u", m_stop_source_before_count);
2547 value.AppendString (strm.GetData());
2548 }
2549 else if (var_name == StopSourceContextBeforeName ())
2550 {
2551 StreamString strm;
2552 strm.Printf ("%u", m_stop_source_after_count);
2553 value.AppendString (strm.GetData());
2554 }
2555 else if (var_name == StopDisassemblyCountName ())
2556 {
2557 StreamString strm;
2558 strm.Printf ("%u", m_stop_disassembly_count);
2559 value.AppendString (strm.GetData());
2560 }
2561 else if (var_name == StopDisassemblyDisplayName ())
2562 {
2563 if (m_stop_disassembly_display >= eStopDisassemblyTypeNever && m_stop_disassembly_display <= eStopDisassemblyTypeAlways)
2564 value.AppendString (g_show_disassembly_enum_values[m_stop_disassembly_display].string_value);
2565 else
2566 value.AppendString ("<invalid>");
2567 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002568 else
Caroline Tice12cecd72010-09-20 21:37:42 +00002569 {
2570 if (err)
2571 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2572 return false;
2573 }
2574 return true;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002575}
2576
2577void
Greg Clayton4d122c42011-09-17 08:33:22 +00002578DebuggerInstanceSettings::CopyInstanceSettings (const InstanceSettingsSP &new_settings,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002579 bool pending)
2580{
2581 if (new_settings.get() == NULL)
2582 return;
2583
2584 DebuggerInstanceSettings *new_debugger_settings = (DebuggerInstanceSettings *) new_settings.get();
2585
2586 m_prompt = new_debugger_settings->m_prompt;
2587 if (!pending)
Caroline Tice49e27372010-09-07 18:35:40 +00002588 {
2589 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2590 // strip off the brackets before passing it to BroadcastPromptChange.
2591
2592 std::string tmp_instance_name (m_instance_name.AsCString());
2593 if ((tmp_instance_name[0] == '[')
2594 && (tmp_instance_name[m_instance_name.GetLength() - 1] == ']'))
2595 tmp_instance_name = tmp_instance_name.substr (1, m_instance_name.GetLength() - 2);
2596 ConstString new_name (tmp_instance_name.c_str());
2597
2598 BroadcastPromptChange (new_name, m_prompt.c_str());
2599 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002600 m_frame_format = new_debugger_settings->m_frame_format;
2601 m_thread_format = new_debugger_settings->m_thread_format;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002602 m_term_width = new_debugger_settings->m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002603 m_script_lang = new_debugger_settings->m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002604 m_use_external_editor = new_debugger_settings->m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002605 m_auto_confirm_on = new_debugger_settings->m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002606}
2607
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002608
2609bool
2610DebuggerInstanceSettings::BroadcastPromptChange (const ConstString &instance_name, const char *new_prompt)
2611{
2612 std::string tmp_prompt;
2613
2614 if (new_prompt != NULL)
2615 {
2616 tmp_prompt = new_prompt ;
2617 int len = tmp_prompt.size();
2618 if (len > 1
2619 && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"')
2620 && (tmp_prompt[len-1] == tmp_prompt[0]))
2621 {
2622 tmp_prompt = tmp_prompt.substr(1,len-2);
2623 }
2624 len = tmp_prompt.size();
2625 if (tmp_prompt[len-1] != ' ')
2626 tmp_prompt.append(" ");
2627 }
2628 EventSP new_event_sp;
2629 new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt,
2630 new EventDataBytes (tmp_prompt.c_str())));
2631
2632 if (instance_name.GetLength() != 0)
2633 {
2634 // Set prompt for a particular instance.
2635 Debugger *dbg = Debugger::FindDebuggerWithInstanceName (instance_name).get();
2636 if (dbg != NULL)
2637 {
2638 dbg->GetCommandInterpreter().BroadcastEvent (new_event_sp);
2639 }
2640 }
2641
2642 return true;
2643}
2644
2645const ConstString
2646DebuggerInstanceSettings::CreateInstanceName ()
2647{
2648 static int instance_count = 1;
2649 StreamString sstr;
2650
2651 sstr.Printf ("debugger_%d", instance_count);
2652 ++instance_count;
2653
2654 const ConstString ret_val (sstr.GetData());
2655
2656 return ret_val;
2657}
2658
Jim Ingham3bcdb292010-10-04 22:44:14 +00002659
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002660//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00002661// SettingsController Variable Tables
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002662//--------------------------------------------------
2663
2664
2665SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002666Debugger::SettingsController::global_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002667{
2668 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
Caroline Tice101c7c22010-09-09 06:25:08 +00002669 // The Debugger level global table should always be empty; all Debugger settable variables should be instance
2670 // variables.
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002671 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2672};
2673
Greg Claytonbb562b12010-10-04 02:44:26 +00002674#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name}${function.pc-offset}}}"
Greg Clayton0603aa92010-10-04 01:05:56 +00002675#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002676
Greg Clayton0603aa92010-10-04 01:05:56 +00002677#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2678 "{, ${frame.pc}}"\
2679 MODULE_WITH_FUNC\
Greg Claytoncf4b9072010-10-04 17:04:23 +00002680 FILE_AND_LINE\
Greg Clayton0603aa92010-10-04 01:05:56 +00002681 "{, stop reason = ${thread.stop-reason}}"\
Jim Inghamef651602011-12-22 19:12:40 +00002682 "{\\nReturn value: ${thread.return-value}}"\
Greg Clayton0603aa92010-10-04 01:05:56 +00002683 "\\n"
2684
Greg Clayton315d2ca2010-11-02 01:53:21 +00002685//#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2686// "{, ${frame.pc}}"\
2687// MODULE_WITH_FUNC\
2688// FILE_AND_LINE\
2689// "{, stop reason = ${thread.stop-reason}}"\
2690// "{, name = ${thread.name}}"\
2691// "{, queue = ${thread.queue}}"\
2692// "\\n"
2693
Greg Clayton0603aa92010-10-04 01:05:56 +00002694#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
2695 MODULE_WITH_FUNC\
2696 FILE_AND_LINE\
2697 "\\n"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002698
2699SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002700Debugger::SettingsController::instance_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002701{
Greg Clayton0603aa92010-10-04 01:05:56 +00002702// NAME Setting variable type Default Enum Init'd Hidden Help
2703// ======================= ======================= ====================== ==== ====== ====== ======================
Greg Clayton0603aa92010-10-04 01:05:56 +00002704{ "frame-format", eSetVarTypeString, DEFAULT_FRAME_FORMAT, NULL, false, false, "The default frame format string to use when displaying thread information." },
Greg Clayton54180392010-10-22 21:15:00 +00002705{ "prompt", eSetVarTypeString, "(lldb) ", NULL, false, false, "The debugger command line prompt displayed for the user." },
Jim Ingham3bcdb292010-10-04 22:44:14 +00002706{ "script-lang", eSetVarTypeString, "python", NULL, false, false, "The script language to be used for evaluating user-written scripts." },
2707{ "term-width", eSetVarTypeInt, "80" , NULL, false, false, "The maximum number of columns to use for displaying text." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002708{ "thread-format", eSetVarTypeString, DEFAULT_THREAD_FORMAT, NULL, false, false, "The default thread format string to use when displaying thread information." },
Greg Claytone372b982011-11-21 21:44:34 +00002709{ "use-external-editor", eSetVarTypeBoolean, "false", NULL, false, false, "Whether to use an external editor or not." },
2710{ "auto-confirm", eSetVarTypeBoolean, "false", NULL, false, false, "If true all confirmation prompts will receive their default reply." },
2711{ "stop-line-count-before",eSetVarTypeInt, "3", NULL, false, false, "The number of sources lines to display that come before the current source line when displaying a stopped context." },
2712{ "stop-line-count-after", eSetVarTypeInt, "3", NULL, false, false, "The number of sources lines to display that come after the current source line when displaying a stopped context." },
2713{ "stop-disassembly-count", eSetVarTypeInt, "0", NULL, false, false, "The number of disassembly lines to show when displaying a stopped context." },
2714{ "stop-disassembly-display", eSetVarTypeEnum, "no-source", g_show_disassembly_enum_values, false, false, "Control when to display disassembly when displaying a stopped context." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002715{ NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002716};