blob: d7f1460e7fbb4b52e8a325ec1bd329a167a449e8 [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"
Jim Ingham228063c2012-02-21 02:23:08 +000024#include "lldb/Core/StreamCallback.h"
Greg Clayton1b654882010-09-19 02:33:57 +000025#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000027#include "lldb/Core/ValueObject.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000028#include "lldb/Core/ValueObjectVariable.h"
Greg Claytona3406612011-02-07 23:24:47 +000029#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000030#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton6d3dbf52012-01-13 08:39:16 +000031#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/TargetList.h"
33#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000034#include "lldb/Target/RegisterContext.h"
35#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/Thread.h"
Greg Clayton5a314712011-10-14 07:41:33 +000037#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038
39using namespace lldb;
40using namespace lldb_private;
41
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042
Greg Clayton1b654882010-09-19 02:33:57 +000043static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000044static lldb::user_id_t g_unique_id = 1;
45
Greg Clayton1b654882010-09-19 02:33:57 +000046#pragma mark Static Functions
47
48static Mutex &
49GetDebuggerListMutex ()
50{
51 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
52 return g_mutex;
53}
54
55typedef std::vector<DebuggerSP> DebuggerList;
56
57static DebuggerList &
58GetDebuggerList()
59{
60 // hide the static debugger list inside a singleton accessor to avoid
61 // global init contructors
62 static DebuggerList g_list;
63 return g_list;
64}
65
66
Greg Claytone372b982011-11-21 21:44:34 +000067static const ConstString &
68PromptVarName ()
69{
70 static ConstString g_const_string ("prompt");
71 return g_const_string;
72}
73
74static const ConstString &
75GetFrameFormatName ()
76{
77 static ConstString g_const_string ("frame-format");
78 return g_const_string;
79}
80
81static const ConstString &
82GetThreadFormatName ()
83{
84 static ConstString g_const_string ("thread-format");
85 return g_const_string;
86}
87
88static const ConstString &
89ScriptLangVarName ()
90{
91 static ConstString g_const_string ("script-lang");
92 return g_const_string;
93}
94
95static const ConstString &
96TermWidthVarName ()
97{
98 static ConstString g_const_string ("term-width");
99 return g_const_string;
100}
101
102static const ConstString &
103UseExternalEditorVarName ()
104{
105 static ConstString g_const_string ("use-external-editor");
106 return g_const_string;
107}
108
109static const ConstString &
110AutoConfirmName ()
111{
112 static ConstString g_const_string ("auto-confirm");
113 return g_const_string;
114}
115
116static const ConstString &
117StopSourceContextBeforeName ()
118{
119 static ConstString g_const_string ("stop-line-count-before");
120 return g_const_string;
121}
122
123static const ConstString &
124StopSourceContextAfterName ()
125{
126 static ConstString g_const_string ("stop-line-count-after");
127 return g_const_string;
128}
129
130static const ConstString &
131StopDisassemblyCountName ()
132{
133 static ConstString g_const_string ("stop-disassembly-count");
134 return g_const_string;
135}
136
137static const ConstString &
138StopDisassemblyDisplayName ()
139{
140 static ConstString g_const_string ("stop-disassembly-display");
141 return g_const_string;
142}
143
144OptionEnumValueElement
145DebuggerInstanceSettings::g_show_disassembly_enum_values[] =
146{
147 { eStopDisassemblyTypeNever, "never", "Never show disassembly when displaying a stop context."},
148 { eStopDisassemblyTypeNoSource, "no-source", "Show disassembly when there is no source information, or the source file is missing when displaying a stop context."},
149 { eStopDisassemblyTypeAlways, "always", "Always show disassembly when displaying a stop context."},
150 { 0, NULL, NULL }
151};
152
153
154
Greg Clayton1b654882010-09-19 02:33:57 +0000155#pragma mark Debugger
156
Greg Clayton99d0faf2010-11-18 23:32:35 +0000157UserSettingsControllerSP &
158Debugger::GetSettingsController ()
159{
Greg Claytonb9556ac2012-01-30 07:41:31 +0000160 static UserSettingsControllerSP g_settings_controller_sp;
161 if (!g_settings_controller_sp)
162 {
163 g_settings_controller_sp.reset (new Debugger::SettingsController);
164
165 // The first shared pointer to Debugger::SettingsController in
166 // g_settings_controller_sp must be fully created above so that
167 // the DebuggerInstanceSettings can use a weak_ptr to refer back
168 // to the master setttings controller
169 InstanceSettingsSP default_instance_settings_sp (new DebuggerInstanceSettings (g_settings_controller_sp,
170 false,
171 InstanceSettings::GetDefaultName().AsCString()));
172 g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
173 }
174 return g_settings_controller_sp;
Greg Clayton99d0faf2010-11-18 23:32:35 +0000175}
176
Caroline Tice2f88aad2011-01-14 00:29:16 +0000177int
178Debugger::TestDebuggerRefCount ()
179{
180 return g_shared_debugger_refcount;
181}
182
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183void
184Debugger::Initialize ()
185{
Greg Clayton66111032010-06-23 01:19:29 +0000186 if (g_shared_debugger_refcount == 0)
Greg Clayton99d0faf2010-11-18 23:32:35 +0000187 {
Greg Claytondbe54502010-11-19 03:46:01 +0000188 lldb_private::Initialize();
Greg Clayton99d0faf2010-11-18 23:32:35 +0000189 }
Greg Clayton66111032010-06-23 01:19:29 +0000190 g_shared_debugger_refcount++;
Greg Clayton99d0faf2010-11-18 23:32:35 +0000191
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192}
193
194void
195Debugger::Terminate ()
196{
Greg Clayton66111032010-06-23 01:19:29 +0000197 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198 {
Greg Clayton66111032010-06-23 01:19:29 +0000199 g_shared_debugger_refcount--;
200 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 {
Greg Claytondbe54502010-11-19 03:46:01 +0000202 lldb_private::WillTerminate();
203 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000204
205 // Clear our master list of debugger objects
206 Mutex::Locker locker (GetDebuggerListMutex ());
207 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 }
210}
211
Caroline Tice20bd37f2011-03-10 22:14:10 +0000212void
213Debugger::SettingsInitialize ()
214{
215 static bool g_initialized = false;
216
217 if (!g_initialized)
218 {
219 g_initialized = true;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000220 UserSettingsController::InitializeSettingsController (GetSettingsController(),
Caroline Tice20bd37f2011-03-10 22:14:10 +0000221 SettingsController::global_settings_table,
222 SettingsController::instance_settings_table);
223 // Now call SettingsInitialize for each settings 'child' of Debugger
224 Target::SettingsInitialize ();
225 }
226}
227
228void
229Debugger::SettingsTerminate ()
230{
231
232 // Must call SettingsTerminate() for each settings 'child' of Debugger, before terminating the Debugger's
233 // Settings.
234
235 Target::SettingsTerminate ();
236
237 // Now terminate the Debugger Settings.
238
239 UserSettingsControllerSP &usc = GetSettingsController();
240 UserSettingsController::FinalizeSettingsController (usc);
241 usc.reset();
242}
243
Greg Clayton66111032010-06-23 01:19:29 +0000244DebuggerSP
Jim Ingham228063c2012-02-21 02:23:08 +0000245Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
Greg Clayton66111032010-06-23 01:19:29 +0000246{
Jim Ingham228063c2012-02-21 02:23:08 +0000247 DebuggerSP debugger_sp (new Debugger(log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000248 // Scope for locker
249 {
250 Mutex::Locker locker (GetDebuggerListMutex ());
251 GetDebuggerList().push_back(debugger_sp);
252 }
253 return debugger_sp;
254}
255
Caroline Ticee02657b2011-01-22 01:02:07 +0000256void
Greg Clayton4d122c42011-09-17 08:33:22 +0000257Debugger::Destroy (DebuggerSP &debugger_sp)
Caroline Ticee02657b2011-01-22 01:02:07 +0000258{
259 if (debugger_sp.get() == NULL)
260 return;
261
Jim Ingham8314c522011-09-15 21:36:42 +0000262 debugger_sp->Clear();
263
Caroline Ticee02657b2011-01-22 01:02:07 +0000264 Mutex::Locker locker (GetDebuggerListMutex ());
265 DebuggerList &debugger_list = GetDebuggerList ();
266 DebuggerList::iterator pos, end = debugger_list.end();
267 for (pos = debugger_list.begin (); pos != end; ++pos)
268 {
269 if ((*pos).get() == debugger_sp.get())
270 {
271 debugger_list.erase (pos);
272 return;
273 }
274 }
Caroline Ticee02657b2011-01-22 01:02:07 +0000275}
276
Greg Clayton4d122c42011-09-17 08:33:22 +0000277DebuggerSP
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000278Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
279{
Greg Clayton4d122c42011-09-17 08:33:22 +0000280 DebuggerSP debugger_sp;
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000281
282 Mutex::Locker locker (GetDebuggerListMutex ());
283 DebuggerList &debugger_list = GetDebuggerList();
284 DebuggerList::iterator pos, end = debugger_list.end();
285
286 for (pos = debugger_list.begin(); pos != end; ++pos)
287 {
288 if ((*pos).get()->m_instance_name == instance_name)
289 {
290 debugger_sp = *pos;
291 break;
292 }
293 }
294 return debugger_sp;
295}
Greg Clayton66111032010-06-23 01:19:29 +0000296
297TargetSP
298Debugger::FindTargetWithProcessID (lldb::pid_t pid)
299{
Greg Clayton4d122c42011-09-17 08:33:22 +0000300 TargetSP target_sp;
Greg Clayton66111032010-06-23 01:19:29 +0000301 Mutex::Locker locker (GetDebuggerListMutex ());
302 DebuggerList &debugger_list = GetDebuggerList();
303 DebuggerList::iterator pos, end = debugger_list.end();
304 for (pos = debugger_list.begin(); pos != end; ++pos)
305 {
306 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
307 if (target_sp)
308 break;
309 }
310 return target_sp;
311}
312
Greg Claytone4e45922011-11-16 05:37:56 +0000313TargetSP
314Debugger::FindTargetWithProcess (Process *process)
315{
316 TargetSP target_sp;
317 Mutex::Locker locker (GetDebuggerListMutex ());
318 DebuggerList &debugger_list = GetDebuggerList();
319 DebuggerList::iterator pos, end = debugger_list.end();
320 for (pos = debugger_list.begin(); pos != end; ++pos)
321 {
322 target_sp = (*pos)->GetTargetList().FindTargetWithProcess (process);
323 if (target_sp)
324 break;
325 }
326 return target_sp;
327}
328
Greg Clayton66111032010-06-23 01:19:29 +0000329
Jim Ingham228063c2012-02-21 02:23:08 +0000330Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000331 UserID (g_unique_id++),
Greg Claytonb9556ac2012-01-30 07:41:31 +0000332 DebuggerInstanceSettings (GetSettingsController()),
Greg Claytond46c87a2010-12-04 02:39:47 +0000333 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 m_input_file (),
335 m_output_file (),
336 m_error_file (),
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000337 m_target_list (*this),
Greg Claytonded470d2011-03-19 01:12:21 +0000338 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 m_listener ("lldb.Debugger"),
Jim Inghame37d6052011-09-13 00:29:56 +0000340 m_source_manager(*this),
341 m_source_file_cache(),
Greg Clayton66111032010-06-23 01:19:29 +0000342 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000343 m_input_reader_stack (),
Greg Clayton4957bf62010-09-30 21:49:03 +0000344 m_input_reader_data ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345{
Jim Ingham228063c2012-02-21 02:23:08 +0000346 if (log_callback)
347 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
Greg Clayton66111032010-06-23 01:19:29 +0000348 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000349 // Always add our default platform to the platform list
350 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
351 assert (default_platform_sp.get());
352 m_platform_list.Append (default_platform_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353}
354
355Debugger::~Debugger ()
356{
Jim Ingham8314c522011-09-15 21:36:42 +0000357 Clear();
358}
359
360void
361Debugger::Clear()
362{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000363 CleanUpInputReaders();
Greg Clayton1ed54f52011-10-01 00:45:15 +0000364 m_listener.Clear();
Greg Clayton66111032010-06-23 01:19:29 +0000365 int num_targets = m_target_list.GetNumTargets();
366 for (int i = 0; i < num_targets; i++)
367 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000368 TargetSP target_sp (m_target_list.GetTargetAtIndex (i));
369 if (target_sp)
Jim Ingham8314c522011-09-15 21:36:42 +0000370 {
Greg Claytonccbc08e2012-01-14 17:04:19 +0000371 ProcessSP process_sp (target_sp->GetProcessSP());
372 if (process_sp)
373 {
374 if (process_sp->GetShouldDetach())
375 process_sp->Detach();
376 }
377 target_sp->Destroy();
Jim Ingham8314c522011-09-15 21:36:42 +0000378 }
Greg Clayton66111032010-06-23 01:19:29 +0000379 }
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000380 BroadcasterManager::Clear ();
Greg Clayton66111032010-06-23 01:19:29 +0000381 DisconnectInput();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382
Jim Ingham8314c522011-09-15 21:36:42 +0000383}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384
385bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000386Debugger::GetCloseInputOnEOF () const
387{
388 return m_input_comm.GetCloseOnEOF();
389}
390
391void
392Debugger::SetCloseInputOnEOF (bool b)
393{
394 m_input_comm.SetCloseOnEOF(b);
395}
396
397bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398Debugger::GetAsyncExecution ()
399{
Greg Clayton66111032010-06-23 01:19:29 +0000400 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401}
402
403void
404Debugger::SetAsyncExecution (bool async_execution)
405{
Greg Clayton66111032010-06-23 01:19:29 +0000406 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407}
408
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409
410void
411Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
412{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000413 File &in_file = GetInputFile();
414 in_file.SetStream (fh, tranfer_ownership);
415 if (in_file.IsValid() == false)
416 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417
418 // Disconnect from any old connection if we had one
419 m_input_comm.Disconnect ();
Greg Clayton32720b52012-01-14 20:47:38 +0000420 // Pass false as the second argument to ConnectionFileDescriptor below because
421 // our "in_file" above will already take ownership if requested and we don't
422 // want to objects trying to own and close a file descriptor.
423 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
425
426 Error error;
427 if (m_input_comm.StartReadThread (&error) == false)
428 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000429 File &err_file = GetErrorFile();
430
431 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
432 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434}
435
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436void
437Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
438{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000439 File &out_file = GetOutputFile();
440 out_file.SetStream (fh, tranfer_ownership);
441 if (out_file.IsValid() == false)
442 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000443
444 GetCommandInterpreter().GetScriptInterpreter()->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445}
446
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447void
448Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
449{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000450 File &err_file = GetErrorFile();
451 err_file.SetStream (fh, tranfer_ownership);
452 if (err_file.IsValid() == false)
453 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454}
455
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000457Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458{
459 ExecutionContext exe_ctx;
Greg Claytonc14ee322011-09-22 04:58:26 +0000460 TargetSP target_sp(GetSelectedTarget());
461 exe_ctx.SetTargetSP (target_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462
463 if (target_sp)
464 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000465 ProcessSP process_sp (target_sp->GetProcessSP());
466 exe_ctx.SetProcessSP (process_sp);
467 if (process_sp && process_sp->IsRunning() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000469 ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread());
470 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000472 exe_ctx.SetThreadSP (thread_sp);
473 exe_ctx.SetFrameSP (thread_sp->GetSelectedFrame());
474 if (exe_ctx.GetFramePtr() == NULL)
475 exe_ctx.SetFrameSP (thread_sp->GetStackFrameAtIndex (0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 }
477 }
478 }
479 return exe_ctx;
480
481}
482
Caroline Ticeb44880c2011-02-10 01:15:13 +0000483InputReaderSP
484Debugger::GetCurrentInputReader ()
485{
486 InputReaderSP reader_sp;
487
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000488 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000489 {
490 // Clear any finished readers from the stack
491 while (CheckIfTopInputReaderIsDone()) ;
492
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000493 if (!m_input_reader_stack.IsEmpty())
494 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000495 }
496
497 return reader_sp;
498}
499
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500void
501Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
502{
Caroline Ticeefed6132010-11-19 20:47:54 +0000503 if (bytes_len > 0)
504 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
505 else
506 ((Debugger *)baton)->DispatchInputEndOfFile ();
507}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508
509
510void
511Debugger::DispatchInput (const char *bytes, size_t bytes_len)
512{
Caroline Ticeefed6132010-11-19 20:47:54 +0000513 if (bytes == NULL || bytes_len == 0)
514 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515
516 WriteToDefaultReader (bytes, bytes_len);
517}
518
519void
Caroline Ticeefed6132010-11-19 20:47:54 +0000520Debugger::DispatchInputInterrupt ()
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 (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000528
Caroline Ticeb44880c2011-02-10 01:15:13 +0000529 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000530 while (CheckIfTopInputReaderIsDone ()) ;
531 }
532}
533
534void
535Debugger::DispatchInputEndOfFile ()
536{
537 m_input_reader_data.clear();
538
Caroline Ticeb44880c2011-02-10 01:15:13 +0000539 InputReaderSP reader_sp (GetCurrentInputReader ());
540 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000541 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000542 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000543
Caroline Ticeb44880c2011-02-10 01:15:13 +0000544 // 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 +0000545 while (CheckIfTopInputReaderIsDone ()) ;
546 }
547}
548
549void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000550Debugger::CleanUpInputReaders ()
551{
552 m_input_reader_data.clear();
553
Caroline Ticeb44880c2011-02-10 01:15:13 +0000554 // 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 +0000555 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000556 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000557 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000558 if (reader_sp)
559 {
560 reader_sp->Notify (eInputReaderEndOfFile);
561 reader_sp->SetIsDone (true);
562 }
563 }
564}
565
566void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000567Debugger::NotifyTopInputReader (InputReaderAction notification)
568{
569 InputReaderSP reader_sp (GetCurrentInputReader());
570 if (reader_sp)
571 {
572 reader_sp->Notify (notification);
573
574 // Flush out any input readers that are done.
575 while (CheckIfTopInputReaderIsDone ())
576 /* Do nothing. */;
577 }
578}
579
Caroline Tice9088b062011-05-09 23:06:58 +0000580bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000581Debugger::InputReaderIsTopReader (const InputReaderSP& reader_sp)
Caroline Tice9088b062011-05-09 23:06:58 +0000582{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000583 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000584
Caroline Ticed61c10b2011-06-16 16:27:19 +0000585 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000586}
587
588
Caroline Tice969ed3d2011-05-02 20:41:46 +0000589void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
591{
592 if (bytes && bytes_len)
593 m_input_reader_data.append (bytes, bytes_len);
594
595 if (m_input_reader_data.empty())
596 return;
597
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000598 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000601 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 if (!reader_sp)
603 break;
604
Greg Clayton471b31c2010-07-20 22:52:08 +0000605 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 m_input_reader_data.size());
607 if (bytes_handled)
608 {
609 m_input_reader_data.erase (0, bytes_handled);
610 }
611 else
612 {
613 // No bytes were handled, we might not have reached our
614 // granularity, just return and wait for more data
615 break;
616 }
617 }
618
Caroline Ticeb44880c2011-02-10 01:15:13 +0000619 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 while (CheckIfTopInputReaderIsDone ())
621 /* Do nothing. */;
622
623}
624
625void
626Debugger::PushInputReader (const InputReaderSP& reader_sp)
627{
628 if (!reader_sp)
629 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000630
631 // Deactivate the old top reader
632 InputReaderSP top_reader_sp (GetCurrentInputReader ());
633
634 if (top_reader_sp)
635 top_reader_sp->Notify (eInputReaderDeactivate);
636
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000637 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 reader_sp->Notify (eInputReaderActivate);
639 ActivateInputReader (reader_sp);
640}
641
642bool
Greg Clayton4d122c42011-09-17 08:33:22 +0000643Debugger::PopInputReader (const InputReaderSP& pop_reader_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644{
645 bool result = false;
646
647 // The reader on the stop of the stack is done, so let the next
648 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000649 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000651 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000652 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653
654 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
655 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000656 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 reader_sp->Notify (eInputReaderDeactivate);
658 reader_sp->Notify (eInputReaderDone);
659 result = true;
660
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000661 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000663 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 if (reader_sp)
665 {
666 ActivateInputReader (reader_sp);
667 reader_sp->Notify (eInputReaderReactivate);
668 }
669 }
670 }
671 }
672 return result;
673}
674
675bool
676Debugger::CheckIfTopInputReaderIsDone ()
677{
678 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000679 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000681 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000682 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683
684 if (reader_sp && reader_sp->IsDone())
685 {
686 result = true;
687 PopInputReader (reader_sp);
688 }
689 }
690 return result;
691}
692
693void
694Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
695{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000696 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000698 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000700 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000701
702 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703
Greg Claytona3406612011-02-07 23:24:47 +0000704 switch (reader_sp->GetGranularity())
705 {
706 case eInputReaderGranularityByte:
707 case eInputReaderGranularityWord:
708 tty.SetCanonical (false);
709 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710
Greg Claytona3406612011-02-07 23:24:47 +0000711 case eInputReaderGranularityLine:
712 case eInputReaderGranularityAll:
713 tty.SetCanonical (true);
714 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715
Greg Claytona3406612011-02-07 23:24:47 +0000716 default:
717 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 }
719 }
720}
Greg Clayton66111032010-06-23 01:19:29 +0000721
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000722StreamSP
723Debugger::GetAsyncOutputStream ()
724{
725 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
726 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
727}
728
729StreamSP
730Debugger::GetAsyncErrorStream ()
731{
732 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
733 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
734}
735
Enrico Granata061858c2012-02-15 02:34:21 +0000736uint32_t
737Debugger::GetNumDebuggers()
738{
739 Mutex::Locker locker (GetDebuggerListMutex ());
740 return GetDebuggerList().size();
741}
742
743lldb::DebuggerSP
744Debugger::GetDebuggerAtIndex (uint32_t index)
745{
746 DebuggerSP debugger_sp;
747
748 Mutex::Locker locker (GetDebuggerListMutex ());
749 DebuggerList &debugger_list = GetDebuggerList();
750
751 if (index < debugger_list.size())
752 debugger_sp = debugger_list[index];
753
754 return debugger_sp;
755}
756
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000757DebuggerSP
758Debugger::FindDebuggerWithID (lldb::user_id_t id)
759{
Greg Clayton4d122c42011-09-17 08:33:22 +0000760 DebuggerSP debugger_sp;
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000761
762 Mutex::Locker locker (GetDebuggerListMutex ());
763 DebuggerList &debugger_list = GetDebuggerList();
764 DebuggerList::iterator pos, end = debugger_list.end();
765 for (pos = debugger_list.begin(); pos != end; ++pos)
766 {
767 if ((*pos).get()->GetID() == id)
768 {
769 debugger_sp = *pos;
770 break;
771 }
772 }
773 return debugger_sp;
774}
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000775
Greg Clayton1b654882010-09-19 02:33:57 +0000776static void
777TestPromptFormats (StackFrame *frame)
778{
779 if (frame == NULL)
780 return;
781
782 StreamString s;
783 const char *prompt_format =
784 "{addr = '${addr}'\n}"
785 "{process.id = '${process.id}'\n}"
786 "{process.name = '${process.name}'\n}"
787 "{process.file.basename = '${process.file.basename}'\n}"
788 "{process.file.fullpath = '${process.file.fullpath}'\n}"
789 "{thread.id = '${thread.id}'\n}"
790 "{thread.index = '${thread.index}'\n}"
791 "{thread.name = '${thread.name}'\n}"
792 "{thread.queue = '${thread.queue}'\n}"
793 "{thread.stop-reason = '${thread.stop-reason}'\n}"
794 "{target.arch = '${target.arch}'\n}"
795 "{module.file.basename = '${module.file.basename}'\n}"
796 "{module.file.fullpath = '${module.file.fullpath}'\n}"
797 "{file.basename = '${file.basename}'\n}"
798 "{file.fullpath = '${file.fullpath}'\n}"
799 "{frame.index = '${frame.index}'\n}"
800 "{frame.pc = '${frame.pc}'\n}"
801 "{frame.sp = '${frame.sp}'\n}"
802 "{frame.fp = '${frame.fp}'\n}"
803 "{frame.flags = '${frame.flags}'\n}"
804 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
805 "{frame.reg.rip = '${frame.reg.rip}'\n}"
806 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
807 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
808 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
809 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
810 "{frame.reg.carp = '${frame.reg.carp}'\n}"
811 "{function.id = '${function.id}'\n}"
812 "{function.name = '${function.name}'\n}"
Greg Claytonccbc08e2012-01-14 17:04:19 +0000813 "{function.name-with-args = '${function.name-with-args}'\n}"
Greg Clayton1b654882010-09-19 02:33:57 +0000814 "{function.addr-offset = '${function.addr-offset}'\n}"
815 "{function.line-offset = '${function.line-offset}'\n}"
816 "{function.pc-offset = '${function.pc-offset}'\n}"
817 "{line.file.basename = '${line.file.basename}'\n}"
818 "{line.file.fullpath = '${line.file.fullpath}'\n}"
819 "{line.number = '${line.number}'\n}"
820 "{line.start-addr = '${line.start-addr}'\n}"
821 "{line.end-addr = '${line.end-addr}'\n}"
822;
823
824 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
825 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +0000826 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +0000827 const char *end = NULL;
828 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
829 {
830 printf("%s\n", s.GetData());
831 }
832 else
833 {
834 printf ("error: at '%s'\n", end);
835 printf ("what we got: %s\n", s.GetData());
836 }
837}
838
Enrico Granata9fc19442011-07-06 02:13:41 +0000839static bool
Enrico Granatadc940732011-08-23 00:32:52 +0000840ScanFormatDescriptor (const char* var_name_begin,
841 const char* var_name_end,
842 const char** var_name_final,
843 const char** percent_position,
Greg Clayton4d122c42011-09-17 08:33:22 +0000844 Format* custom_format,
Enrico Granatadc940732011-08-23 00:32:52 +0000845 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
Enrico Granata9fc19442011-07-06 02:13:41 +0000846{
Enrico Granatae992a082011-07-22 17:03:19 +0000847 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000848 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +0000849 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +0000850 {
851 if (log)
852 log->Printf("no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +0000853 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +0000854 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000855 else
856 {
857 *var_name_final = *percent_position;
858 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
859 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +0000860 if (log)
861 log->Printf("parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000862 if ( !FormatManager::GetFormatFromCString(format_name,
863 true,
864 *custom_format) )
865 {
Enrico Granatae992a082011-07-22 17:03:19 +0000866 if (log)
867 log->Printf("%s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000868 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +0000869 if (*format_name == '@')
Enrico Granata86cc9822012-03-19 22:58:49 +0000870 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLanguageSpecific;
Enrico Granata9fc19442011-07-06 02:13:41 +0000871 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +0000872 else if (*format_name == 'V')
Enrico Granata86cc9822012-03-19 22:58:49 +0000873 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatad55546b2011-07-22 00:16:08 +0000874 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +0000875 else if (*format_name == 'L')
Enrico Granata86cc9822012-03-19 22:58:49 +0000876 *val_obj_display = ValueObject::eValueObjectRepresentationStyleLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +0000877 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +0000878 else if (*format_name == 'S')
Enrico Granata86cc9822012-03-19 22:58:49 +0000879 *val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Enrico Granata5dfd49c2011-08-04 02:34:29 +0000880 else if (*format_name == '#')
Enrico Granata86cc9822012-03-19 22:58:49 +0000881 *val_obj_display = ValueObject::eValueObjectRepresentationStyleChildrenCount;
Enrico Granatad64d0bc2011-08-19 21:13:46 +0000882 else if (*format_name == 'T')
Enrico Granata86cc9822012-03-19 22:58:49 +0000883 *val_obj_display = ValueObject::eValueObjectRepresentationStyleType;
Enrico Granatae992a082011-07-22 17:03:19 +0000884 else if (log)
885 log->Printf("%s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000886 }
887 // a good custom format tells us to print the value using it
888 else
Enrico Granatae992a082011-07-22 17:03:19 +0000889 {
890 if (log)
891 log->Printf("will display value for this VO");
Enrico Granata86cc9822012-03-19 22:58:49 +0000892 *val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Enrico Granatae992a082011-07-22 17:03:19 +0000893 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000894 delete format_name;
895 }
Enrico Granatae992a082011-07-22 17:03:19 +0000896 if (log)
897 log->Printf("final format description outcome: custom_format = %d, val_obj_display = %d",
898 *custom_format,
899 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +0000900 return true;
901}
902
903static bool
Enrico Granatadc940732011-08-23 00:32:52 +0000904ScanBracketedRange (const char* var_name_begin,
905 const char* var_name_end,
906 const char* var_name_final,
907 const char** open_bracket_position,
908 const char** separator_position,
909 const char** close_bracket_position,
910 const char** var_name_final_if_array_range,
911 int64_t* index_lower,
912 int64_t* index_higher)
Enrico Granata9fc19442011-07-06 02:13:41 +0000913{
Enrico Granatae992a082011-07-22 17:03:19 +0000914 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000915 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +0000916 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +0000917 {
918 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
919 *close_bracket_position = ::strchr(*open_bracket_position,']');
920 // as usual, we assume that [] will come before %
921 //printf("trying to expand a []\n");
922 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +0000923 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +0000924 {
Enrico Granatae992a082011-07-22 17:03:19 +0000925 if (log)
926 log->Printf("[] detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +0000927 *index_lower = 0;
928 }
929 else if (*separator_position == NULL || *separator_position > var_name_end)
930 {
931 char *end = NULL;
932 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
933 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +0000934 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000935 log->Printf("[%lld] detected, high index is same", *index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +0000936 }
Greg Clayton34132752011-07-06 04:07:21 +0000937 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +0000938 {
939 char *end = NULL;
940 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
941 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +0000942 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000943 log->Printf("[%lld-%lld] detected", *index_lower, *index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +0000944 }
945 else
Enrico Granatae992a082011-07-22 17:03:19 +0000946 {
947 if (log)
948 log->Printf("expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +0000949 return false;
Enrico Granatae992a082011-07-22 17:03:19 +0000950 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000951 if (*index_lower > *index_higher && *index_higher > 0)
952 {
Enrico Granatae992a082011-07-22 17:03:19 +0000953 if (log)
954 log->Printf("swapping indices");
Enrico Granata9fc19442011-07-06 02:13:41 +0000955 int temp = *index_lower;
956 *index_lower = *index_higher;
957 *index_higher = temp;
958 }
959 }
Enrico Granatae992a082011-07-22 17:03:19 +0000960 else if (log)
961 log->Printf("no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +0000962 return true;
963}
964
965
966static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +0000967ExpandExpressionPath (ValueObject* valobj,
968 StackFrame* frame,
969 bool* do_deref_pointer,
970 const char* var_name_begin,
971 const char* var_name_final,
972 Error& error)
Enrico Granata9fc19442011-07-06 02:13:41 +0000973{
Enrico Granatae992a082011-07-22 17:03:19 +0000974 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000975 StreamString sstring;
976 VariableSP var_sp;
977
Greg Clayton34132752011-07-06 04:07:21 +0000978 if (*do_deref_pointer)
Enrico Granatae992a082011-07-22 17:03:19 +0000979 {
980 if (log)
981 log->Printf("been told to deref_pointer by caller");
Enrico Granata9fc19442011-07-06 02:13:41 +0000982 sstring.PutChar('*');
Enrico Granatae992a082011-07-22 17:03:19 +0000983 }
Enrico Granatac482a192011-08-17 22:13:59 +0000984 else if (valobj->IsDereferenceOfParent() && ClangASTContext::IsPointerType(valobj->GetParent()->GetClangType()) && !valobj->IsArrayItemForPointer())
Enrico Granata9fc19442011-07-06 02:13:41 +0000985 {
Enrico Granatae992a082011-07-22 17:03:19 +0000986 if (log)
987 log->Printf("decided to deref_pointer myself");
Enrico Granata9fc19442011-07-06 02:13:41 +0000988 sstring.PutChar('*');
989 *do_deref_pointer = true;
990 }
991
Enrico Granata86cc9822012-03-19 22:58:49 +0000992 valobj->GetExpressionPath(sstring, true, ValueObject::eGetExpressionPathFormatHonorPointers);
Enrico Granatae992a082011-07-22 17:03:19 +0000993 if (log)
994 log->Printf("expression path to expand in phase 0: %s",sstring.GetData());
Enrico Granata9fc19442011-07-06 02:13:41 +0000995 sstring.PutRawBytes(var_name_begin+3, var_name_final-var_name_begin-3);
Enrico Granatae992a082011-07-22 17:03:19 +0000996 if (log)
997 log->Printf("expression path to expand in phase 1: %s",sstring.GetData());
Enrico Granata9fc19442011-07-06 02:13:41 +0000998 std::string name = std::string(sstring.GetData());
999 ValueObjectSP target = frame->GetValueForVariableExpressionPath (name.c_str(),
1000 eNoDynamicValues,
1001 0,
1002 var_sp,
1003 error);
1004 return target;
1005}
1006
1007static ValueObjectSP
Enrico Granatadc940732011-08-23 00:32:52 +00001008ExpandIndexedExpression (ValueObject* valobj,
1009 uint32_t index,
1010 StackFrame* frame,
1011 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +00001012{
Enrico Granatae992a082011-07-22 17:03:19 +00001013 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001014 const char* ptr_deref_format = "[%d]";
1015 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
1016 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +00001017 if (log)
1018 log->Printf("name to deref: %s",ptr_deref_buffer.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001019 const char* first_unparsed;
1020 ValueObject::GetValueForExpressionPathOptions options;
1021 ValueObject::ExpressionPathEndResultType final_value_type;
1022 ValueObject::ExpressionPathScanEndReason reason_to_stop;
Enrico Granata86cc9822012-03-19 22:58:49 +00001023 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatac482a192011-08-17 22:13:59 +00001024 ValueObjectSP item = valobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001025 &first_unparsed,
1026 &reason_to_stop,
1027 &final_value_type,
1028 options,
1029 &what_next);
1030 if (!item)
1031 {
Enrico Granatae992a082011-07-22 17:03:19 +00001032 if (log)
1033 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1034 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001035 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001036 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001037 else
1038 {
Enrico Granatae992a082011-07-22 17:03:19 +00001039 if (log)
1040 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1041 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001042 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +00001043 }
1044 return item;
1045}
1046
Greg Clayton1b654882010-09-19 02:33:57 +00001047bool
1048Debugger::FormatPrompt
1049(
1050 const char *format,
1051 const SymbolContext *sc,
1052 const ExecutionContext *exe_ctx,
1053 const Address *addr,
1054 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +00001055 const char **end,
Enrico Granatac482a192011-08-17 22:13:59 +00001056 ValueObject* valobj
Greg Clayton1b654882010-09-19 02:33:57 +00001057)
1058{
Enrico Granatac482a192011-08-17 22:13:59 +00001059 ValueObject* realvalobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +00001060 bool success = true;
1061 const char *p;
Enrico Granatae992a082011-07-22 17:03:19 +00001062 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +00001063 for (p = format; *p != '\0'; ++p)
1064 {
Enrico Granatac482a192011-08-17 22:13:59 +00001065 if (realvalobj)
Enrico Granata4becb372011-06-29 22:27:15 +00001066 {
Enrico Granatac482a192011-08-17 22:13:59 +00001067 valobj = realvalobj;
1068 realvalobj = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001069 }
Greg Clayton1b654882010-09-19 02:33:57 +00001070 size_t non_special_chars = ::strcspn (p, "${}\\");
1071 if (non_special_chars > 0)
1072 {
1073 if (success)
1074 s.Write (p, non_special_chars);
1075 p += non_special_chars;
1076 }
1077
1078 if (*p == '\0')
1079 {
1080 break;
1081 }
1082 else if (*p == '{')
1083 {
1084 // Start a new scope that must have everything it needs if it is to
1085 // to make it into the final output stream "s". If you want to make
1086 // a format that only prints out the function or symbol name if there
1087 // is one in the symbol context you can use:
1088 // "{function =${function.name}}"
1089 // The first '{' starts a new scope that end with the matching '}' at
1090 // the end of the string. The contents "function =${function.name}"
1091 // will then be evaluated and only be output if there is a function
1092 // or symbol with a valid name.
1093 StreamString sub_strm;
1094
1095 ++p; // Skip the '{'
1096
Enrico Granatac482a192011-08-17 22:13:59 +00001097 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
Greg Clayton1b654882010-09-19 02:33:57 +00001098 {
1099 // The stream had all it needed
1100 s.Write(sub_strm.GetData(), sub_strm.GetSize());
1101 }
1102 if (*p != '}')
1103 {
1104 success = false;
1105 break;
1106 }
1107 }
1108 else if (*p == '}')
1109 {
1110 // End of a enclosing scope
1111 break;
1112 }
1113 else if (*p == '$')
1114 {
1115 // We have a prompt variable to print
1116 ++p;
1117 if (*p == '{')
1118 {
1119 ++p;
1120 const char *var_name_begin = p;
1121 const char *var_name_end = ::strchr (p, '}');
1122
1123 if (var_name_end && var_name_begin < var_name_end)
1124 {
1125 // if we have already failed to parse, skip this variable
1126 if (success)
1127 {
1128 const char *cstr = NULL;
1129 Address format_addr;
1130 bool calculate_format_addr_function_offset = false;
1131 // Set reg_kind and reg_num to invalid values
1132 RegisterKind reg_kind = kNumRegisterKinds;
1133 uint32_t reg_num = LLDB_INVALID_REGNUM;
1134 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +00001135 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00001136 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +00001137 bool do_deref_pointer = false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001138 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
1139 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001140
Greg Clayton1b654882010-09-19 02:33:57 +00001141 // Each variable must set success to true below...
1142 bool var_success = false;
1143 switch (var_name_begin[0])
1144 {
Enrico Granata4becb372011-06-29 22:27:15 +00001145 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001146 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001147 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001148 {
Enrico Granatac482a192011-08-17 22:13:59 +00001149 if (!valobj)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001150 break;
1151
Enrico Granatac3e320a2011-08-02 17:27:39 +00001152 if (log)
1153 log->Printf("initial string: %s",var_name_begin);
1154
Enrico Granata6f3533f2011-07-29 19:53:35 +00001155 // check for *var and *svar
1156 if (*var_name_begin == '*')
1157 {
1158 do_deref_pointer = true;
1159 var_name_begin++;
1160 }
Enrico Granatac3e320a2011-08-02 17:27:39 +00001161
1162 if (log)
1163 log->Printf("initial string: %s",var_name_begin);
1164
Enrico Granata6f3533f2011-07-29 19:53:35 +00001165 if (*var_name_begin == 's')
1166 {
Enrico Granatac5bc4122012-03-27 02:35:13 +00001167 if (!valobj->IsSynthetic())
1168 valobj = valobj->GetSyntheticValue().get();
Enrico Granata86cc9822012-03-19 22:58:49 +00001169 if (!valobj)
1170 break;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001171 var_name_begin++;
1172 }
1173
Enrico Granatac3e320a2011-08-02 17:27:39 +00001174 if (log)
1175 log->Printf("initial string: %s",var_name_begin);
1176
Enrico Granata6f3533f2011-07-29 19:53:35 +00001177 // should be a 'v' by now
1178 if (*var_name_begin != 'v')
1179 break;
1180
Enrico Granatac3e320a2011-08-02 17:27:39 +00001181 if (log)
1182 log->Printf("initial string: %s",var_name_begin);
1183
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001184 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
Enrico Granata86cc9822012-03-19 22:58:49 +00001185 ValueObject::eExpressionPathAftermathDereference : ValueObject::eExpressionPathAftermathNothing);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001186 ValueObject::GetValueForExpressionPathOptions options;
Enrico Granata8c9d3562011-08-11 17:08:01 +00001187 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar().DoAllowSyntheticChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001188 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eValueObjectRepresentationStyleSummary;
Greg Clayton34132752011-07-06 04:07:21 +00001189 ValueObject* target = NULL;
Greg Clayton4d122c42011-09-17 08:33:22 +00001190 Format custom_format = eFormatInvalid;
Greg Clayton34132752011-07-06 04:07:21 +00001191 const char* var_name_final = NULL;
1192 const char* var_name_final_if_array_range = NULL;
1193 const char* close_bracket_position = NULL;
1194 int64_t index_lower = -1;
1195 int64_t index_higher = -1;
1196 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001197 const char* first_unparsed;
Enrico Granata85933ed2011-08-18 16:38:26 +00001198 bool was_plain_var = false;
1199 bool was_var_format = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001200
Enrico Granatac482a192011-08-17 22:13:59 +00001201 if (!valobj) break;
1202 // simplest case ${var}, just print valobj's value
Greg Clayton34132752011-07-06 04:07:21 +00001203 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001204 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001205 was_plain_var = true;
Enrico Granatac482a192011-08-17 22:13:59 +00001206 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001207 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001208 }
1209 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1210 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001211 was_var_format = true;
Greg Clayton34132752011-07-06 04:07:21 +00001212 // this is a variable with some custom format applied to it
1213 const char* percent_position;
Enrico Granatac482a192011-08-17 22:13:59 +00001214 target = valobj;
Enrico Granata86cc9822012-03-19 22:58:49 +00001215 val_obj_display = ValueObject::eValueObjectRepresentationStyleValue;
Greg Clayton34132752011-07-06 04:07:21 +00001216 ScanFormatDescriptor (var_name_begin,
1217 var_name_end,
1218 &var_name_final,
1219 &percent_position,
1220 &custom_format,
1221 &val_obj_display);
1222 }
1223 // this is ${var.something} or multiple .something nested
1224 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1225 {
1226
1227 const char* percent_position;
1228 ScanFormatDescriptor (var_name_begin,
1229 var_name_end,
1230 &var_name_final,
1231 &percent_position,
1232 &custom_format,
1233 &val_obj_display);
1234
1235 const char* open_bracket_position;
1236 const char* separator_position;
1237 ScanBracketedRange (var_name_begin,
1238 var_name_end,
1239 var_name_final,
1240 &open_bracket_position,
1241 &separator_position,
1242 &close_bracket_position,
1243 &var_name_final_if_array_range,
1244 &index_lower,
1245 &index_higher);
1246
1247 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001248
1249 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1250 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1251 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1252
Enrico Granatae992a082011-07-22 17:03:19 +00001253 if (log)
1254 log->Printf("symbol to expand: %s",expr_path.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001255
Enrico Granatac482a192011-08-17 22:13:59 +00001256 target = valobj->GetValueForExpressionPath(expr_path.get(),
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001257 &first_unparsed,
1258 &reason_to_stop,
1259 &final_value_type,
1260 options,
1261 &what_next).get();
1262
1263 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001264 {
Enrico Granatae992a082011-07-22 17:03:19 +00001265 if (log)
1266 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1267 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001268 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001269 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001270 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001271 else
1272 {
Enrico Granatae992a082011-07-22 17:03:19 +00001273 if (log)
1274 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1275 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001276 first_unparsed, reason_to_stop, final_value_type);
1277 }
Enrico Granata4becb372011-06-29 22:27:15 +00001278 }
Greg Clayton34132752011-07-06 04:07:21 +00001279 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001280 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001281
Enrico Granata86cc9822012-03-19 22:58:49 +00001282 is_array_range = (final_value_type == ValueObject::eExpressionPathEndResultTypeBoundedRange ||
1283 final_value_type == ValueObject::eExpressionPathEndResultTypeUnboundedRange);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001284
Enrico Granata86cc9822012-03-19 22:58:49 +00001285 do_deref_pointer = (what_next == ValueObject::eExpressionPathAftermathDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001286
Enrico Granataa7187d02011-07-06 19:27:11 +00001287 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001288 {
Greg Clayton34132752011-07-06 04:07:21 +00001289 // I have not deref-ed yet, let's do it
1290 // this happens when we are not going through GetValueForVariableExpressionPath
1291 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001292 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001293 target = target->Dereference(error).get();
Enrico Granatadc940732011-08-23 00:32:52 +00001294 if (error.Fail())
1295 {
1296 if (log)
1297 log->Printf("ERROR: %s\n", error.AsCString("unknown")); \
1298 break;
1299 }
Greg Clayton34132752011-07-06 04:07:21 +00001300 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001301 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001302
Enrico Granata85933ed2011-08-18 16:38:26 +00001303 // TODO use flags for these
Enrico Granataf4efecd2011-07-12 22:56:10 +00001304 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1305 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
Enrico Granata85933ed2011-08-18 16:38:26 +00001306 bool is_aggregate = ClangASTContext::IsAggregateType(target->GetClangType());
Enrico Granataf4efecd2011-07-12 22:56:10 +00001307
Enrico Granata86cc9822012-03-19 22:58:49 +00001308 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) // this should be wrong, but there are some exceptions
Enrico Granataf4efecd2011-07-12 22:56:10 +00001309 {
Enrico Granata85933ed2011-08-18 16:38:26 +00001310 StreamString str_temp;
Enrico Granatae992a082011-07-22 17:03:19 +00001311 if (log)
1312 log->Printf("I am into array || pointer && !range");
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001313
Enrico Granata86cc9822012-03-19 22:58:49 +00001314 if (target->HasSpecialPrintableRepresentation(val_obj_display,
1315 custom_format))
Enrico Granata85933ed2011-08-18 16:38:26 +00001316 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001317 // try to use the special cases
1318 var_success = target->DumpPrintableRepresentation(str_temp,
1319 val_obj_display,
1320 custom_format);
1321 if (log)
1322 log->Printf("special cases did%s match", var_success ? "" : "n't");
1323
1324 // should not happen
1325 if (!var_success)
1326 s << "<invalid usage of pointer value as object>";
1327 else
1328 s << str_temp.GetData();
Enrico Granata85933ed2011-08-18 16:38:26 +00001329 var_success = true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001330 break;
Enrico Granata85933ed2011-08-18 16:38:26 +00001331 }
1332 else
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001333 {
Enrico Granata88da35f2011-08-23 21:26:09 +00001334 if (was_plain_var) // if ${var}
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001335 {
1336 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1337 }
Enrico Granata88da35f2011-08-23 21:26:09 +00001338 else if (is_pointer) // if pointer, value is the address stored
1339 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001340 var_success = target->DumpPrintableRepresentation(s,
Enrico Granata88da35f2011-08-23 21:26:09 +00001341 val_obj_display,
Enrico Granata86cc9822012-03-19 22:58:49 +00001342 custom_format,
1343 ValueObject::ePrintableRepresentationSpecialCasesDisable);
Enrico Granata88da35f2011-08-23 21:26:09 +00001344 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001345 else
1346 {
1347 s << "<invalid usage of pointer value as object>";
1348 }
1349 var_success = true;
1350 break;
1351 }
1352 }
1353
1354 // if directly trying to print ${var}, and this is an aggregate, display a nice
1355 // type @ location message
1356 if (is_aggregate && was_plain_var)
1357 {
1358 s << target->GetTypeName() << " @ " << target->GetLocationAsCString();
1359 var_success = true;
Enrico Granata85933ed2011-08-18 16:38:26 +00001360 break;
1361 }
1362
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001363 // if directly trying to print ${var%V}, and this is an aggregate, do not let the user do it
Enrico Granata86cc9822012-03-19 22:58:49 +00001364 if (is_aggregate && ((was_var_format && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)))
Enrico Granata85933ed2011-08-18 16:38:26 +00001365 {
1366 s << "<invalid use of aggregate type>";
1367 var_success = true;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001368 break;
1369 }
Greg Clayton34132752011-07-06 04:07:21 +00001370
1371 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001372 {
1373 if (log)
1374 log->Printf("dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001375 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001376 }
Greg Clayton34132752011-07-06 04:07:21 +00001377 else
Enrico Granatae992a082011-07-22 17:03:19 +00001378 {
1379 if (log)
1380 log->Printf("checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001381 if (!is_array && !is_pointer)
1382 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001383 if (log)
1384 log->Printf("handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001385 const char* special_directions = NULL;
1386 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001387 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1388 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001389 ConstString additional_data;
1390 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1391 special_directions_writer.Printf("${%svar%s}",
1392 do_deref_pointer ? "*" : "",
1393 additional_data.GetCString());
1394 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001395 }
1396
1397 // let us display items index_lower thru index_higher of this array
1398 s.PutChar('[');
1399 var_success = true;
1400
1401 if (index_higher < 0)
Enrico Granatac482a192011-08-17 22:13:59 +00001402 index_higher = valobj->GetNumChildren() - 1;
Greg Clayton34132752011-07-06 04:07:21 +00001403
Greg Claytoncc4d0142012-02-17 07:49:44 +00001404 uint32_t max_num_children = target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00001405
Greg Clayton34132752011-07-06 04:07:21 +00001406 for (;index_lower<=index_higher;index_lower++)
1407 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001408 ValueObject* item = ExpandIndexedExpression (target,
1409 index_lower,
1410 exe_ctx->GetFramePtr(),
1411 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001412
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001413 if (!item)
1414 {
Enrico Granatae992a082011-07-22 17:03:19 +00001415 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +00001416 log->Printf("ERROR in getting child item at index %lld", index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001417 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001418 else
1419 {
Enrico Granatae992a082011-07-22 17:03:19 +00001420 if (log)
1421 log->Printf("special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001422 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001423
Greg Clayton34132752011-07-06 04:07:21 +00001424 if (!special_directions)
1425 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1426 else
1427 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1428
Enrico Granata22c55d12011-08-12 02:00:06 +00001429 if (--max_num_children == 0)
1430 {
1431 s.PutCString(", ...");
1432 break;
1433 }
1434
Greg Clayton34132752011-07-06 04:07:21 +00001435 if (index_lower < index_higher)
1436 s.PutChar(',');
1437 }
1438 s.PutChar(']');
1439 }
Enrico Granata4becb372011-06-29 22:27:15 +00001440 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001441 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001442 case 'a':
1443 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1444 {
1445 if (addr && addr->IsValid())
1446 {
1447 var_success = true;
1448 format_addr = *addr;
1449 }
1450 }
Greg Clayton5a314712011-10-14 07:41:33 +00001451 else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
1452 {
1453 var_success = true;
1454 var_name_begin += strlen("ansi."); // Skip the "ansi."
1455 if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
1456 {
1457 var_name_begin += strlen("fg."); // Skip the "fg."
1458 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1459 {
1460 s.Printf ("%s%s%s",
1461 lldb_utility::ansi::k_escape_start,
1462 lldb_utility::ansi::k_fg_black,
1463 lldb_utility::ansi::k_escape_end);
1464 }
1465 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1466 {
1467 s.Printf ("%s%s%s",
1468 lldb_utility::ansi::k_escape_start,
1469 lldb_utility::ansi::k_fg_red,
1470 lldb_utility::ansi::k_escape_end);
1471 }
1472 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1473 {
1474 s.Printf ("%s%s%s",
1475 lldb_utility::ansi::k_escape_start,
1476 lldb_utility::ansi::k_fg_green,
1477 lldb_utility::ansi::k_escape_end);
1478 }
1479 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1480 {
1481 s.Printf ("%s%s%s",
1482 lldb_utility::ansi::k_escape_start,
1483 lldb_utility::ansi::k_fg_yellow,
1484 lldb_utility::ansi::k_escape_end);
1485 }
1486 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1487 {
1488 s.Printf ("%s%s%s",
1489 lldb_utility::ansi::k_escape_start,
1490 lldb_utility::ansi::k_fg_blue,
1491 lldb_utility::ansi::k_escape_end);
1492 }
1493 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1494 {
1495 s.Printf ("%s%s%s",
1496 lldb_utility::ansi::k_escape_start,
1497 lldb_utility::ansi::k_fg_purple,
1498 lldb_utility::ansi::k_escape_end);
1499 }
1500 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1501 {
1502 s.Printf ("%s%s%s",
1503 lldb_utility::ansi::k_escape_start,
1504 lldb_utility::ansi::k_fg_cyan,
1505 lldb_utility::ansi::k_escape_end);
1506 }
1507 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1508 {
1509 s.Printf ("%s%s%s",
1510 lldb_utility::ansi::k_escape_start,
1511 lldb_utility::ansi::k_fg_white,
1512 lldb_utility::ansi::k_escape_end);
1513 }
1514 else
1515 {
1516 var_success = false;
1517 }
1518 }
1519 else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
1520 {
1521 var_name_begin += strlen("bg."); // Skip the "bg."
1522 if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
1523 {
1524 s.Printf ("%s%s%s",
1525 lldb_utility::ansi::k_escape_start,
1526 lldb_utility::ansi::k_bg_black,
1527 lldb_utility::ansi::k_escape_end);
1528 }
1529 else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
1530 {
1531 s.Printf ("%s%s%s",
1532 lldb_utility::ansi::k_escape_start,
1533 lldb_utility::ansi::k_bg_red,
1534 lldb_utility::ansi::k_escape_end);
1535 }
1536 else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
1537 {
1538 s.Printf ("%s%s%s",
1539 lldb_utility::ansi::k_escape_start,
1540 lldb_utility::ansi::k_bg_green,
1541 lldb_utility::ansi::k_escape_end);
1542 }
1543 else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
1544 {
1545 s.Printf ("%s%s%s",
1546 lldb_utility::ansi::k_escape_start,
1547 lldb_utility::ansi::k_bg_yellow,
1548 lldb_utility::ansi::k_escape_end);
1549 }
1550 else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
1551 {
1552 s.Printf ("%s%s%s",
1553 lldb_utility::ansi::k_escape_start,
1554 lldb_utility::ansi::k_bg_blue,
1555 lldb_utility::ansi::k_escape_end);
1556 }
1557 else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
1558 {
1559 s.Printf ("%s%s%s",
1560 lldb_utility::ansi::k_escape_start,
1561 lldb_utility::ansi::k_bg_purple,
1562 lldb_utility::ansi::k_escape_end);
1563 }
1564 else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
1565 {
1566 s.Printf ("%s%s%s",
1567 lldb_utility::ansi::k_escape_start,
1568 lldb_utility::ansi::k_bg_cyan,
1569 lldb_utility::ansi::k_escape_end);
1570 }
1571 else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
1572 {
1573 s.Printf ("%s%s%s",
1574 lldb_utility::ansi::k_escape_start,
1575 lldb_utility::ansi::k_bg_white,
1576 lldb_utility::ansi::k_escape_end);
1577 }
1578 else
1579 {
1580 var_success = false;
1581 }
1582 }
1583 else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
1584 {
1585 s.Printf ("%s%s%s",
1586 lldb_utility::ansi::k_escape_start,
1587 lldb_utility::ansi::k_ctrl_normal,
1588 lldb_utility::ansi::k_escape_end);
1589 }
1590 else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
1591 {
1592 s.Printf ("%s%s%s",
1593 lldb_utility::ansi::k_escape_start,
1594 lldb_utility::ansi::k_ctrl_bold,
1595 lldb_utility::ansi::k_escape_end);
1596 }
1597 else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
1598 {
1599 s.Printf ("%s%s%s",
1600 lldb_utility::ansi::k_escape_start,
1601 lldb_utility::ansi::k_ctrl_faint,
1602 lldb_utility::ansi::k_escape_end);
1603 }
1604 else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
1605 {
1606 s.Printf ("%s%s%s",
1607 lldb_utility::ansi::k_escape_start,
1608 lldb_utility::ansi::k_ctrl_italic,
1609 lldb_utility::ansi::k_escape_end);
1610 }
1611 else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
1612 {
1613 s.Printf ("%s%s%s",
1614 lldb_utility::ansi::k_escape_start,
1615 lldb_utility::ansi::k_ctrl_underline,
1616 lldb_utility::ansi::k_escape_end);
1617 }
1618 else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
1619 {
1620 s.Printf ("%s%s%s",
1621 lldb_utility::ansi::k_escape_start,
1622 lldb_utility::ansi::k_ctrl_slow_blink,
1623 lldb_utility::ansi::k_escape_end);
1624 }
1625 else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
1626 {
1627 s.Printf ("%s%s%s",
1628 lldb_utility::ansi::k_escape_start,
1629 lldb_utility::ansi::k_ctrl_fast_blink,
1630 lldb_utility::ansi::k_escape_end);
1631 }
1632 else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
1633 {
1634 s.Printf ("%s%s%s",
1635 lldb_utility::ansi::k_escape_start,
1636 lldb_utility::ansi::k_ctrl_negative,
1637 lldb_utility::ansi::k_escape_end);
1638 }
1639 else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
1640 {
1641 s.Printf ("%s%s%s",
1642 lldb_utility::ansi::k_escape_start,
1643 lldb_utility::ansi::k_ctrl_conceal,
1644 lldb_utility::ansi::k_escape_end);
1645
1646 }
1647 else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
1648 {
1649 s.Printf ("%s%s%s",
1650 lldb_utility::ansi::k_escape_start,
1651 lldb_utility::ansi::k_ctrl_crossed_out,
1652 lldb_utility::ansi::k_escape_end);
1653 }
1654 else
1655 {
1656 var_success = false;
1657 }
1658 }
Greg Clayton1b654882010-09-19 02:33:57 +00001659 break;
1660
1661 case 'p':
1662 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1663 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001664 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001665 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001666 Process *process = exe_ctx->GetProcessPtr();
1667 if (process)
Greg Clayton1b654882010-09-19 02:33:57 +00001668 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001669 var_name_begin += ::strlen ("process.");
1670 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001671 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001672 s.Printf("%llu", process->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001673 var_success = true;
1674 }
1675 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1676 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1677 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1678 {
1679 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
1680 if (exe_module)
Greg Clayton1b654882010-09-19 02:33:57 +00001681 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001682 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1683 {
1684 format_file_spec.GetFilename() = exe_module->GetFileSpec().GetFilename();
1685 var_success = format_file_spec;
1686 }
1687 else
1688 {
1689 format_file_spec = exe_module->GetFileSpec();
1690 var_success = format_file_spec;
1691 }
Greg Clayton1b654882010-09-19 02:33:57 +00001692 }
1693 }
1694 }
Greg Claytonc14ee322011-09-22 04:58:26 +00001695 }
Greg Clayton1b654882010-09-19 02:33:57 +00001696 }
1697 break;
1698
1699 case 't':
1700 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1701 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001702 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001703 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001704 Thread *thread = exe_ctx->GetThreadPtr();
1705 if (thread)
Greg Clayton1b654882010-09-19 02:33:57 +00001706 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001707 var_name_begin += ::strlen ("thread.");
1708 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001709 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001710 s.Printf("0x%4.4llx", thread->GetID());
Greg Claytonc14ee322011-09-22 04:58:26 +00001711 var_success = true;
1712 }
1713 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1714 {
1715 s.Printf("%u", thread->GetIndexID());
1716 var_success = true;
1717 }
1718 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1719 {
1720 cstr = thread->GetName();
1721 var_success = cstr && cstr[0];
1722 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001723 s.PutCString(cstr);
Greg Claytonc14ee322011-09-22 04:58:26 +00001724 }
1725 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1726 {
1727 cstr = thread->GetQueueName();
1728 var_success = cstr && cstr[0];
1729 if (var_success)
1730 s.PutCString(cstr);
1731 }
1732 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1733 {
1734 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1735 if (stop_info_sp)
1736 {
1737 cstr = stop_info_sp->GetDescription();
1738 if (cstr && cstr[0])
1739 {
1740 s.PutCString(cstr);
1741 var_success = true;
1742 }
Greg Clayton1b654882010-09-19 02:33:57 +00001743 }
1744 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001745 else if (::strncmp (var_name_begin, "return-value}", strlen("return-value}")) == 0)
1746 {
1747 StopInfoSP stop_info_sp = thread->GetStopInfo ();
1748 if (stop_info_sp)
1749 {
1750 ValueObjectSP return_valobj_sp = StopInfo::GetReturnValueObject (stop_info_sp);
1751 if (return_valobj_sp)
1752 {
Jim Inghamef651602011-12-22 19:12:40 +00001753 ValueObject::DumpValueObjectOptions dump_options;
1754 ValueObject::DumpValueObject (s, return_valobj_sp.get(), dump_options);
1755 var_success = true;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001756 }
1757 }
1758 }
Greg Clayton1b654882010-09-19 02:33:57 +00001759 }
1760 }
1761 }
1762 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1763 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001764 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1765 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001766 {
Greg Clayton1b654882010-09-19 02:33:57 +00001767 var_name_begin += ::strlen ("target.");
1768 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1769 {
1770 ArchSpec arch (target->GetArchitecture ());
1771 if (arch.IsValid())
1772 {
Greg Clayton64195a22011-02-23 00:35:02 +00001773 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001774 var_success = true;
1775 }
1776 }
1777 }
1778 }
1779 break;
1780
1781
1782 case 'm':
1783 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1784 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001785 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001786 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001787 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001788 var_name_begin += ::strlen ("module.");
1789
1790 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1791 {
1792 if (module->GetFileSpec())
1793 {
1794 var_name_begin += ::strlen ("file.");
1795
1796 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1797 {
1798 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1799 var_success = format_file_spec;
1800 }
1801 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1802 {
1803 format_file_spec = module->GetFileSpec();
1804 var_success = format_file_spec;
1805 }
1806 }
1807 }
1808 }
1809 }
1810 break;
1811
1812
1813 case 'f':
1814 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1815 {
1816 if (sc && sc->comp_unit != NULL)
1817 {
1818 var_name_begin += ::strlen ("file.");
1819
1820 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1821 {
1822 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1823 var_success = format_file_spec;
1824 }
1825 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1826 {
1827 format_file_spec = *sc->comp_unit;
1828 var_success = format_file_spec;
1829 }
1830 }
1831 }
1832 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1833 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001834 if (exe_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001835 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001836 StackFrame *frame = exe_ctx->GetFramePtr();
1837 if (frame)
Greg Clayton1b654882010-09-19 02:33:57 +00001838 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001839 var_name_begin += ::strlen ("frame.");
1840 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
Greg Clayton1b654882010-09-19 02:33:57 +00001841 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001842 s.Printf("%u", frame->GetFrameIndex());
1843 var_success = true;
1844 }
1845 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1846 {
1847 reg_kind = eRegisterKindGeneric;
1848 reg_num = LLDB_REGNUM_GENERIC_PC;
1849 var_success = true;
1850 }
1851 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1852 {
1853 reg_kind = eRegisterKindGeneric;
1854 reg_num = LLDB_REGNUM_GENERIC_SP;
1855 var_success = true;
1856 }
1857 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1858 {
1859 reg_kind = eRegisterKindGeneric;
1860 reg_num = LLDB_REGNUM_GENERIC_FP;
1861 var_success = true;
1862 }
1863 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
1864 {
1865 reg_kind = eRegisterKindGeneric;
1866 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
1867 var_success = true;
1868 }
1869 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
1870 {
1871 reg_ctx = frame->GetRegisterContext().get();
1872 if (reg_ctx)
Greg Clayton1b654882010-09-19 02:33:57 +00001873 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001874 var_name_begin += ::strlen ("reg.");
1875 if (var_name_begin < var_name_end)
1876 {
1877 std::string reg_name (var_name_begin, var_name_end);
1878 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
1879 if (reg_info)
1880 var_success = true;
1881 }
Greg Clayton1b654882010-09-19 02:33:57 +00001882 }
1883 }
1884 }
1885 }
1886 }
1887 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
1888 {
1889 if (sc && (sc->function != NULL || sc->symbol != NULL))
1890 {
1891 var_name_begin += ::strlen ("function.");
1892 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1893 {
1894 if (sc->function)
Greg Clayton81c22f62011-10-19 18:09:39 +00001895 s.Printf("function{0x%8.8llx}", sc->function->GetID());
Greg Clayton1b654882010-09-19 02:33:57 +00001896 else
1897 s.Printf("symbol[%u]", sc->symbol->GetID());
1898
1899 var_success = true;
1900 }
1901 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1902 {
1903 if (sc->function)
1904 cstr = sc->function->GetName().AsCString (NULL);
1905 else if (sc->symbol)
1906 cstr = sc->symbol->GetName().AsCString (NULL);
1907 if (cstr)
1908 {
1909 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00001910
1911 if (sc->block)
1912 {
1913 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1914 if (inline_block)
1915 {
1916 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
1917 if (inline_info)
1918 {
1919 s.PutCString(" [inlined] ");
1920 inline_info->GetName().Dump(&s);
1921 }
1922 }
1923 }
Greg Clayton1b654882010-09-19 02:33:57 +00001924 var_success = true;
1925 }
1926 }
Greg Clayton6d3dbf52012-01-13 08:39:16 +00001927 else if (::strncmp (var_name_begin, "name-with-args}", strlen("name-with-args}")) == 0)
1928 {
1929 // Print the function name with arguments in it
1930
1931 if (sc->function)
1932 {
1933 var_success = true;
1934 ExecutionContextScope *exe_scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
1935 cstr = sc->function->GetName().AsCString (NULL);
1936 if (cstr)
1937 {
1938 const InlineFunctionInfo *inline_info = NULL;
1939 VariableListSP variable_list_sp;
1940 bool get_function_vars = true;
1941 if (sc->block)
1942 {
1943 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1944
1945 if (inline_block)
1946 {
1947 get_function_vars = false;
1948 inline_info = sc->block->GetInlinedFunctionInfo();
1949 if (inline_info)
1950 variable_list_sp = inline_block->GetBlockVariableList (true);
1951 }
1952 }
1953
1954 if (get_function_vars)
1955 {
1956 variable_list_sp = sc->function->GetBlock(true).GetBlockVariableList (true);
1957 }
1958
1959 if (inline_info)
1960 {
1961 s.PutCString (cstr);
1962 s.PutCString (" [inlined] ");
1963 cstr = inline_info->GetName().GetCString();
1964 }
1965
1966 VariableList args;
1967 if (variable_list_sp)
1968 {
1969 const size_t num_variables = variable_list_sp->GetSize();
1970 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
1971 {
1972 VariableSP var_sp (variable_list_sp->GetVariableAtIndex(var_idx));
1973 if (var_sp->GetScope() == eValueTypeVariableArgument)
1974 args.AddVariable (var_sp);
1975 }
1976
1977 }
1978 if (args.GetSize() > 0)
1979 {
1980 const char *open_paren = strchr (cstr, '(');
1981 const char *close_paren = NULL;
1982 if (open_paren)
1983 close_paren = strchr (open_paren, ')');
1984
1985 if (open_paren)
1986 s.Write(cstr, open_paren - cstr + 1);
1987 else
1988 {
1989 s.PutCString (cstr);
1990 s.PutChar ('(');
1991 }
Greg Clayton5b6889b2012-01-18 21:56:18 +00001992 const size_t num_args = args.GetSize();
Greg Clayton6d3dbf52012-01-13 08:39:16 +00001993 for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx)
1994 {
1995 VariableSP var_sp (args.GetVariableAtIndex (arg_idx));
1996 ValueObjectSP var_value_sp (ValueObjectVariable::Create (exe_scope, var_sp));
1997 const char *var_name = var_value_sp->GetName().GetCString();
1998 const char *var_value = var_value_sp->GetValueAsCString();
1999 if (var_value_sp->GetError().Success())
2000 {
2001 if (arg_idx > 0)
2002 s.PutCString (", ");
2003 s.Printf ("%s=%s", var_name, var_value);
2004 }
2005 }
2006
2007 if (close_paren)
2008 s.PutCString (close_paren);
2009 else
2010 s.PutChar(')');
2011
2012 }
2013 else
2014 {
2015 s.PutCString(cstr);
2016 }
2017 }
2018 }
2019 else if (sc->symbol)
2020 {
2021 cstr = sc->symbol->GetName().AsCString (NULL);
2022 if (cstr)
2023 {
2024 s.PutCString(cstr);
2025 var_success = true;
2026 }
2027 }
2028 }
Greg Clayton1b654882010-09-19 02:33:57 +00002029 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
2030 {
2031 var_success = addr != NULL;
2032 if (var_success)
2033 {
2034 format_addr = *addr;
2035 calculate_format_addr_function_offset = true;
2036 }
2037 }
2038 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
2039 {
2040 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
2041 if (var_success)
2042 {
2043 format_addr = sc->line_entry.range.GetBaseAddress();
2044 calculate_format_addr_function_offset = true;
2045 }
2046 }
2047 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
2048 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002049 StackFrame *frame = exe_ctx->GetFramePtr();
2050 var_success = frame != NULL;
Greg Clayton1b654882010-09-19 02:33:57 +00002051 if (var_success)
2052 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002053 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002054 calculate_format_addr_function_offset = true;
2055 }
2056 }
2057 }
2058 }
2059 break;
2060
2061 case 'l':
2062 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
2063 {
2064 if (sc && sc->line_entry.IsValid())
2065 {
2066 var_name_begin += ::strlen ("line.");
2067 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
2068 {
2069 var_name_begin += ::strlen ("file.");
2070
2071 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
2072 {
2073 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
2074 var_success = format_file_spec;
2075 }
2076 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
2077 {
2078 format_file_spec = sc->line_entry.file;
2079 var_success = format_file_spec;
2080 }
2081 }
2082 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
2083 {
2084 var_success = true;
2085 s.Printf("%u", sc->line_entry.line);
2086 }
2087 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
2088 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
2089 {
2090 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
2091 if (var_success)
2092 {
2093 format_addr = sc->line_entry.range.GetBaseAddress();
2094 if (var_name_begin[0] == 'e')
2095 format_addr.Slide (sc->line_entry.range.GetByteSize());
2096 }
2097 }
2098 }
2099 }
2100 break;
2101 }
2102
2103 if (var_success)
2104 {
2105 // If format addr is valid, then we need to print an address
2106 if (reg_num != LLDB_INVALID_REGNUM)
2107 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002108 StackFrame *frame = exe_ctx->GetFramePtr();
Greg Clayton1b654882010-09-19 02:33:57 +00002109 // We have a register value to display...
2110 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
2111 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002112 format_addr = frame->GetFrameCodeAddress();
Greg Clayton1b654882010-09-19 02:33:57 +00002113 }
2114 else
2115 {
2116 if (reg_ctx == NULL)
Greg Claytonc14ee322011-09-22 04:58:26 +00002117 reg_ctx = frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00002118
2119 if (reg_ctx)
2120 {
2121 if (reg_kind != kNumRegisterKinds)
2122 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
2123 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
2124 var_success = reg_info != NULL;
2125 }
2126 }
2127 }
2128
2129 if (reg_info != NULL)
2130 {
Greg Clayton7349bd92011-05-09 20:18:18 +00002131 RegisterValue reg_value;
2132 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
2133 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00002134 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00002135 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00002136 }
2137 }
2138
2139 if (format_file_spec)
2140 {
2141 s << format_file_spec;
2142 }
2143
2144 // If format addr is valid, then we need to print an address
2145 if (format_addr.IsValid())
2146 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002147 var_success = false;
2148
Greg Clayton1b654882010-09-19 02:33:57 +00002149 if (calculate_format_addr_function_offset)
2150 {
2151 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00002152
Greg Clayton0603aa92010-10-04 01:05:56 +00002153 if (sc)
2154 {
2155 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00002156 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002157 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00002158 if (sc->block)
2159 {
2160 // Check to make sure we aren't in an inline
2161 // function. If we are, use the inline block
2162 // range that contains "format_addr" since
2163 // blocks can be discontiguous.
2164 Block *inline_block = sc->block->GetContainingInlinedBlock ();
2165 AddressRange inline_range;
2166 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
2167 func_addr = inline_range.GetBaseAddress();
2168 }
2169 }
Greg Claytone7612132012-03-07 21:03:09 +00002170 else if (sc->symbol && sc->symbol->ValueIsAddress())
2171 func_addr = sc->symbol->GetAddress();
Greg Clayton0603aa92010-10-04 01:05:56 +00002172 }
2173
2174 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00002175 {
2176 if (func_addr.GetSection() == format_addr.GetSection())
2177 {
2178 addr_t func_file_addr = func_addr.GetFileAddress();
2179 addr_t addr_file_addr = format_addr.GetFileAddress();
2180 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002181 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00002182 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00002183 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002184 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00002185 }
2186 else
Greg Clayton0603aa92010-10-04 01:05:56 +00002187 {
2188 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
2189 if (target)
2190 {
2191 addr_t func_load_addr = func_addr.GetLoadAddress (target);
2192 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
2193 if (addr_load_addr > func_load_addr)
2194 s.Printf(" + %llu", addr_load_addr - func_load_addr);
2195 else if (addr_load_addr < func_load_addr)
2196 s.Printf(" - %llu", func_load_addr - addr_load_addr);
2197 var_success = true;
2198 }
2199 }
Greg Clayton1b654882010-09-19 02:33:57 +00002200 }
2201 }
2202 else
2203 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002204 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00002205 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00002206 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
2207 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00002208 if (vaddr == LLDB_INVALID_ADDRESS)
2209 vaddr = format_addr.GetFileAddress ();
2210
2211 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00002212 {
Greg Clayton514487e2011-02-15 21:59:32 +00002213 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00002214 if (addr_width == 0)
2215 addr_width = 16;
2216 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00002217 var_success = true;
2218 }
Greg Clayton1b654882010-09-19 02:33:57 +00002219 }
2220 }
2221 }
2222
2223 if (var_success == false)
2224 success = false;
2225 }
2226 p = var_name_end;
2227 }
2228 else
2229 break;
2230 }
2231 else
2232 {
2233 // We got a dollar sign with no '{' after it, it must just be a dollar sign
2234 s.PutChar(*p);
2235 }
2236 }
2237 else if (*p == '\\')
2238 {
2239 ++p; // skip the slash
2240 switch (*p)
2241 {
2242 case 'a': s.PutChar ('\a'); break;
2243 case 'b': s.PutChar ('\b'); break;
2244 case 'f': s.PutChar ('\f'); break;
2245 case 'n': s.PutChar ('\n'); break;
2246 case 'r': s.PutChar ('\r'); break;
2247 case 't': s.PutChar ('\t'); break;
2248 case 'v': s.PutChar ('\v'); break;
2249 case '\'': s.PutChar ('\''); break;
2250 case '\\': s.PutChar ('\\'); break;
2251 case '0':
2252 // 1 to 3 octal chars
2253 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002254 // Make a string that can hold onto the initial zero char,
2255 // up to 3 octal digits, and a terminating NULL.
2256 char oct_str[5] = { 0, 0, 0, 0, 0 };
2257
2258 int i;
2259 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
2260 oct_str[i] = p[i];
2261
2262 // We don't want to consume the last octal character since
2263 // the main for loop will do this for us, so we advance p by
2264 // one less than i (even if i is zero)
2265 p += i - 1;
2266 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
2267 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002268 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002269 char octal_char = octal_value;
2270 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00002271 }
Greg Clayton1b654882010-09-19 02:33:57 +00002272 }
2273 break;
2274
2275 case 'x':
2276 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00002277 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002278 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002279 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00002280
Greg Clayton0603aa92010-10-04 01:05:56 +00002281 // Make a string that can hold onto two hex chars plus a
2282 // NULL terminator
2283 char hex_str[3] = { 0,0,0 };
2284 hex_str[0] = *p;
2285 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00002286 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002287 ++p; // Skip the first of the two hex chars
2288 hex_str[1] = *p;
2289 }
2290
2291 unsigned long hex_value = strtoul (hex_str, NULL, 16);
2292 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00002293 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00002294 }
2295 else
2296 {
2297 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00002298 }
2299 break;
2300
2301 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00002302 // Just desensitize any other character by just printing what
2303 // came after the '\'
2304 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00002305 break;
2306
2307 }
2308
2309 }
2310 }
2311 if (end)
2312 *end = p;
2313 return success;
2314}
2315
Jim Ingham228063c2012-02-21 02:23:08 +00002316void
2317Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
2318{
Jim Ingham4f02b222012-02-22 22:49:20 +00002319 // For simplicity's sake, I am not going to deal with how to close down any
2320 // open logging streams, I just redirect everything from here on out to the
2321 // callback.
Jim Ingham228063c2012-02-21 02:23:08 +00002322 m_log_callback_stream_sp.reset (new StreamCallback (log_callback, baton));
2323}
2324
2325bool
2326Debugger::EnableLog (const char *channel, const char **categories, const char *log_file, uint32_t log_options, Stream &error_stream)
2327{
2328 Log::Callbacks log_callbacks;
2329
2330 StreamSP log_stream_sp;
2331 if (m_log_callback_stream_sp != NULL)
2332 {
2333 log_stream_sp = m_log_callback_stream_sp;
2334 // For now when using the callback mode you always get thread & timestamp.
2335 log_options |= LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
2336 }
2337 else if (log_file == NULL || *log_file == '\0')
2338 {
2339 log_stream_sp.reset(new StreamFile(GetOutputFile().GetDescriptor(), false));
2340 }
2341 else
2342 {
2343 LogStreamMap::iterator pos = m_log_streams.find(log_file);
2344 if (pos == m_log_streams.end())
2345 {
2346 log_stream_sp.reset (new StreamFile (log_file));
2347 m_log_streams[log_file] = log_stream_sp;
2348 }
2349 else
2350 log_stream_sp = pos->second;
2351 }
2352 assert (log_stream_sp.get());
2353
2354 if (log_options == 0)
2355 log_options = LLDB_LOG_OPTION_PREPEND_THREAD_NAME | LLDB_LOG_OPTION_THREADSAFE;
2356
2357 if (Log::GetLogChannelCallbacks (channel, log_callbacks))
2358 {
2359 log_callbacks.enable (log_stream_sp, log_options, categories, &error_stream);
2360 return true;
2361 }
2362 else
2363 {
2364 LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel));
2365 if (log_channel_sp)
2366 {
2367 if (log_channel_sp->Enable (log_stream_sp, log_options, &error_stream, categories))
2368 {
2369 return true;
2370 }
2371 else
2372 {
2373 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2374 return false;
2375 }
2376 }
2377 else
2378 {
2379 error_stream.Printf ("Invalid log channel '%s'.\n", channel);
2380 return false;
2381 }
2382 }
2383 return false;
2384}
2385
Greg Clayton1b654882010-09-19 02:33:57 +00002386#pragma mark Debugger::SettingsController
2387
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002388//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00002389// class Debugger::SettingsController
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002390//--------------------------------------------------
2391
Greg Clayton1b654882010-09-19 02:33:57 +00002392Debugger::SettingsController::SettingsController () :
Greg Clayton4d122c42011-09-17 08:33:22 +00002393 UserSettingsController ("", UserSettingsControllerSP())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002394{
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002395}
2396
Greg Clayton1b654882010-09-19 02:33:57 +00002397Debugger::SettingsController::~SettingsController ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002398{
2399}
2400
2401
Greg Clayton4d122c42011-09-17 08:33:22 +00002402InstanceSettingsSP
Greg Clayton1b654882010-09-19 02:33:57 +00002403Debugger::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002404{
Greg Claytonb9556ac2012-01-30 07:41:31 +00002405 InstanceSettingsSP new_settings_sp (new DebuggerInstanceSettings (GetSettingsController(),
2406 false,
2407 instance_name));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002408 return new_settings_sp;
2409}
2410
Greg Clayton1b654882010-09-19 02:33:57 +00002411#pragma mark DebuggerInstanceSettings
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002412//--------------------------------------------------
2413// class DebuggerInstanceSettings
2414//--------------------------------------------------
2415
Greg Claytona7015092010-09-18 01:14:36 +00002416DebuggerInstanceSettings::DebuggerInstanceSettings
2417(
Greg Claytonb9556ac2012-01-30 07:41:31 +00002418 const UserSettingsControllerSP &m_owner_sp,
Greg Claytona7015092010-09-18 01:14:36 +00002419 bool live_instance,
2420 const char *name
2421) :
Greg Claytonb9556ac2012-01-30 07:41:31 +00002422 InstanceSettings (m_owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytona7015092010-09-18 01:14:36 +00002423 m_term_width (80),
Greg Claytone372b982011-11-21 21:44:34 +00002424 m_stop_source_before_count (3),
2425 m_stop_source_after_count (3),
2426 m_stop_disassembly_count (4),
2427 m_stop_disassembly_display (eStopDisassemblyTypeNoSource),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002428 m_prompt (),
Greg Clayton0603aa92010-10-04 01:05:56 +00002429 m_frame_format (),
2430 m_thread_format (),
Caroline Ticedaccaa92010-09-20 20:44:43 +00002431 m_script_lang (),
Jim Ingham3bcdb292010-10-04 22:44:14 +00002432 m_use_external_editor (false),
2433 m_auto_confirm_on (false)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002434{
Caroline Ticef20e8232010-09-09 18:26:37 +00002435 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2436 // until the vtables for DebuggerInstanceSettings are properly set up, i.e. AFTER all the initializers.
2437 // 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 +00002438 // The same is true of CreateInstanceName().
2439
2440 if (GetInstanceName() == InstanceSettings::InvalidName())
2441 {
2442 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
Greg Claytonb9556ac2012-01-30 07:41:31 +00002443 m_owner_sp->RegisterInstanceSettings (this);
Caroline Tice9e41c152010-09-16 19:05:55 +00002444 }
Caroline Ticef20e8232010-09-09 18:26:37 +00002445
2446 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002447 {
Greg Claytonb9556ac2012-01-30 07:41:31 +00002448 const InstanceSettingsSP &pending_settings = m_owner_sp->FindPendingSettings (m_instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002449 CopyInstanceSettings (pending_settings, false);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002450 }
2451}
2452
2453DebuggerInstanceSettings::DebuggerInstanceSettings (const DebuggerInstanceSettings &rhs) :
Greg Claytonb9556ac2012-01-30 07:41:31 +00002454 InstanceSettings (Debugger::GetSettingsController(), CreateInstanceName ().AsCString()),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002455 m_prompt (rhs.m_prompt),
Greg Clayton0603aa92010-10-04 01:05:56 +00002456 m_frame_format (rhs.m_frame_format),
2457 m_thread_format (rhs.m_thread_format),
Caroline Ticedaccaa92010-09-20 20:44:43 +00002458 m_script_lang (rhs.m_script_lang),
Jim Ingham3bcdb292010-10-04 22:44:14 +00002459 m_use_external_editor (rhs.m_use_external_editor),
2460 m_auto_confirm_on(rhs.m_auto_confirm_on)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002461{
Greg Claytonb9556ac2012-01-30 07:41:31 +00002462 UserSettingsControllerSP owner_sp (m_owner_wp.lock());
2463 if (owner_sp)
2464 {
2465 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name), false);
2466 owner_sp->RemovePendingSettings (m_instance_name);
2467 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002468}
2469
2470DebuggerInstanceSettings::~DebuggerInstanceSettings ()
2471{
2472}
2473
2474DebuggerInstanceSettings&
2475DebuggerInstanceSettings::operator= (const DebuggerInstanceSettings &rhs)
2476{
2477 if (this != &rhs)
2478 {
Greg Clayton1b654882010-09-19 02:33:57 +00002479 m_term_width = rhs.m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002480 m_prompt = rhs.m_prompt;
Greg Clayton0603aa92010-10-04 01:05:56 +00002481 m_frame_format = rhs.m_frame_format;
2482 m_thread_format = rhs.m_thread_format;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002483 m_script_lang = rhs.m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002484 m_use_external_editor = rhs.m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002485 m_auto_confirm_on = rhs.m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002486 }
2487
2488 return *this;
2489}
2490
Greg Clayton1b654882010-09-19 02:33:57 +00002491bool
2492DebuggerInstanceSettings::ValidTermWidthValue (const char *value, Error err)
2493{
2494 bool valid = false;
2495
2496 // Verify we have a value string.
2497 if (value == NULL || value[0] == '\0')
2498 {
Greg Clayton86edbf42011-10-26 00:56:27 +00002499 err.SetErrorString ("missing value, can't set terminal width without a value");
Greg Clayton1b654882010-09-19 02:33:57 +00002500 }
2501 else
2502 {
2503 char *end = NULL;
2504 const uint32_t width = ::strtoul (value, &end, 0);
2505
Johnny Chenea9fc182010-09-20 16:36:43 +00002506 if (end && end[0] == '\0')
Greg Clayton1b654882010-09-19 02:33:57 +00002507 {
Johnny Chen433d7742010-09-20 17:04:41 +00002508 if (width >= 10 && width <= 1024)
Greg Clayton1b654882010-09-19 02:33:57 +00002509 valid = true;
2510 else
Greg Clayton86edbf42011-10-26 00:56:27 +00002511 err.SetErrorString ("invalid term-width value; value must be between 10 and 1024");
Greg Clayton1b654882010-09-19 02:33:57 +00002512 }
2513 else
Greg Clayton86edbf42011-10-26 00:56:27 +00002514 err.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string", value);
Greg Clayton1b654882010-09-19 02:33:57 +00002515 }
2516
2517 return valid;
2518}
2519
2520
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002521void
2522DebuggerInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2523 const char *index_value,
2524 const char *value,
2525 const ConstString &instance_name,
2526 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:54 +00002527 VarSetOperationType op,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002528 Error &err,
2529 bool pending)
2530{
Greg Clayton0603aa92010-10-04 01:05:56 +00002531
2532 if (var_name == TermWidthVarName())
2533 {
2534 if (ValidTermWidthValue (value, err))
2535 {
2536 m_term_width = ::strtoul (value, NULL, 0);
2537 }
2538 }
2539 else if (var_name == PromptVarName())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002540 {
Caroline Tice101c7c22010-09-09 06:25:08 +00002541 UserSettingsController::UpdateStringVariable (op, m_prompt, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002542 if (!pending)
2543 {
Caroline Tice49e27372010-09-07 18:35:40 +00002544 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2545 // strip off the brackets before passing it to BroadcastPromptChange.
2546
2547 std::string tmp_instance_name (instance_name.AsCString());
2548 if ((tmp_instance_name[0] == '[')
2549 && (tmp_instance_name[instance_name.GetLength() - 1] == ']'))
2550 tmp_instance_name = tmp_instance_name.substr (1, instance_name.GetLength() - 2);
2551 ConstString new_name (tmp_instance_name.c_str());
2552
2553 BroadcastPromptChange (new_name, m_prompt.c_str());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002554 }
2555 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002556 else if (var_name == GetFrameFormatName())
2557 {
2558 UserSettingsController::UpdateStringVariable (op, m_frame_format, value, err);
2559 }
2560 else if (var_name == GetThreadFormatName())
2561 {
2562 UserSettingsController::UpdateStringVariable (op, m_thread_format, value, err);
2563 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002564 else if (var_name == ScriptLangVarName())
2565 {
2566 bool success;
2567 m_script_lang = Args::StringToScriptLanguage (value, eScriptLanguageDefault,
2568 &success);
2569 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002570 else if (var_name == UseExternalEditorVarName ())
2571 {
Greg Clayton385aa282011-04-22 03:55:06 +00002572 UserSettingsController::UpdateBooleanVariable (op, m_use_external_editor, value, false, err);
Caroline Ticedaccaa92010-09-20 20:44:43 +00002573 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002574 else if (var_name == AutoConfirmName ())
2575 {
Greg Clayton385aa282011-04-22 03:55:06 +00002576 UserSettingsController::UpdateBooleanVariable (op, m_auto_confirm_on, value, false, err);
Jim Ingham3bcdb292010-10-04 22:44:14 +00002577 }
Greg Claytone372b982011-11-21 21:44:34 +00002578 else if (var_name == StopSourceContextBeforeName ())
2579 {
2580 uint32_t new_value = Args::StringToUInt32(value, UINT32_MAX, 10, NULL);
2581 if (new_value != UINT32_MAX)
2582 m_stop_source_before_count = new_value;
2583 else
2584 err.SetErrorStringWithFormat("invalid unsigned string value '%s' for the '%s' setting", value, StopSourceContextAfterName ().GetCString());
2585 }
2586 else if (var_name == StopSourceContextAfterName ())
2587 {
2588 uint32_t new_value = Args::StringToUInt32(value, UINT32_MAX, 10, NULL);
2589 if (new_value != UINT32_MAX)
2590 m_stop_source_after_count = new_value;
2591 else
2592 err.SetErrorStringWithFormat("invalid unsigned string value '%s' for the '%s' setting", value, StopSourceContextBeforeName ().GetCString());
2593 }
2594 else if (var_name == StopDisassemblyCountName ())
2595 {
2596 uint32_t new_value = Args::StringToUInt32(value, UINT32_MAX, 10, NULL);
2597 if (new_value != UINT32_MAX)
2598 m_stop_disassembly_count = new_value;
2599 else
2600 err.SetErrorStringWithFormat("invalid unsigned string value '%s' for the '%s' setting", value, StopDisassemblyCountName ().GetCString());
2601 }
2602 else if (var_name == StopDisassemblyDisplayName ())
2603 {
2604 int new_value;
2605 UserSettingsController::UpdateEnumVariable (g_show_disassembly_enum_values, &new_value, value, err);
2606 if (err.Success())
2607 m_stop_disassembly_display = (StopDisassemblyType)new_value;
2608 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002609}
2610
Caroline Tice12cecd72010-09-20 21:37:42 +00002611bool
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002612DebuggerInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2613 const ConstString &var_name,
Caroline Ticedaccaa92010-09-20 20:44:43 +00002614 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42 +00002615 Error *err)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002616{
2617 if (var_name == PromptVarName())
2618 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002619 value.AppendString (m_prompt.c_str(), m_prompt.size());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002620
2621 }
2622 else if (var_name == ScriptLangVarName())
2623 {
2624 value.AppendString (ScriptInterpreter::LanguageToString (m_script_lang).c_str());
2625 }
Caroline Tice101c7c22010-09-09 06:25:08 +00002626 else if (var_name == TermWidthVarName())
2627 {
2628 StreamString width_str;
Greg Claytone372b982011-11-21 21:44:34 +00002629 width_str.Printf ("%u", m_term_width);
Caroline Tice101c7c22010-09-09 06:25:08 +00002630 value.AppendString (width_str.GetData());
2631 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002632 else if (var_name == GetFrameFormatName ())
2633 {
2634 value.AppendString(m_frame_format.c_str(), m_frame_format.size());
2635 }
2636 else if (var_name == GetThreadFormatName ())
2637 {
2638 value.AppendString(m_thread_format.c_str(), m_thread_format.size());
2639 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002640 else if (var_name == UseExternalEditorVarName())
2641 {
2642 if (m_use_external_editor)
2643 value.AppendString ("true");
2644 else
2645 value.AppendString ("false");
2646 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002647 else if (var_name == AutoConfirmName())
2648 {
2649 if (m_auto_confirm_on)
2650 value.AppendString ("true");
2651 else
2652 value.AppendString ("false");
2653 }
Greg Claytone372b982011-11-21 21:44:34 +00002654 else if (var_name == StopSourceContextAfterName ())
2655 {
2656 StreamString strm;
2657 strm.Printf ("%u", m_stop_source_before_count);
2658 value.AppendString (strm.GetData());
2659 }
2660 else if (var_name == StopSourceContextBeforeName ())
2661 {
2662 StreamString strm;
2663 strm.Printf ("%u", m_stop_source_after_count);
2664 value.AppendString (strm.GetData());
2665 }
2666 else if (var_name == StopDisassemblyCountName ())
2667 {
2668 StreamString strm;
2669 strm.Printf ("%u", m_stop_disassembly_count);
2670 value.AppendString (strm.GetData());
2671 }
2672 else if (var_name == StopDisassemblyDisplayName ())
2673 {
2674 if (m_stop_disassembly_display >= eStopDisassemblyTypeNever && m_stop_disassembly_display <= eStopDisassemblyTypeAlways)
2675 value.AppendString (g_show_disassembly_enum_values[m_stop_disassembly_display].string_value);
2676 else
2677 value.AppendString ("<invalid>");
2678 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002679 else
Caroline Tice12cecd72010-09-20 21:37:42 +00002680 {
2681 if (err)
2682 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2683 return false;
2684 }
2685 return true;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002686}
2687
2688void
Greg Clayton4d122c42011-09-17 08:33:22 +00002689DebuggerInstanceSettings::CopyInstanceSettings (const InstanceSettingsSP &new_settings,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002690 bool pending)
2691{
2692 if (new_settings.get() == NULL)
2693 return;
2694
2695 DebuggerInstanceSettings *new_debugger_settings = (DebuggerInstanceSettings *) new_settings.get();
2696
2697 m_prompt = new_debugger_settings->m_prompt;
2698 if (!pending)
Caroline Tice49e27372010-09-07 18:35:40 +00002699 {
2700 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2701 // strip off the brackets before passing it to BroadcastPromptChange.
2702
2703 std::string tmp_instance_name (m_instance_name.AsCString());
2704 if ((tmp_instance_name[0] == '[')
2705 && (tmp_instance_name[m_instance_name.GetLength() - 1] == ']'))
2706 tmp_instance_name = tmp_instance_name.substr (1, m_instance_name.GetLength() - 2);
2707 ConstString new_name (tmp_instance_name.c_str());
2708
2709 BroadcastPromptChange (new_name, m_prompt.c_str());
2710 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002711 m_frame_format = new_debugger_settings->m_frame_format;
2712 m_thread_format = new_debugger_settings->m_thread_format;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002713 m_term_width = new_debugger_settings->m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002714 m_script_lang = new_debugger_settings->m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002715 m_use_external_editor = new_debugger_settings->m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002716 m_auto_confirm_on = new_debugger_settings->m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002717}
2718
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002719
2720bool
2721DebuggerInstanceSettings::BroadcastPromptChange (const ConstString &instance_name, const char *new_prompt)
2722{
2723 std::string tmp_prompt;
2724
2725 if (new_prompt != NULL)
2726 {
2727 tmp_prompt = new_prompt ;
2728 int len = tmp_prompt.size();
2729 if (len > 1
2730 && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"')
2731 && (tmp_prompt[len-1] == tmp_prompt[0]))
2732 {
2733 tmp_prompt = tmp_prompt.substr(1,len-2);
2734 }
2735 len = tmp_prompt.size();
2736 if (tmp_prompt[len-1] != ' ')
2737 tmp_prompt.append(" ");
2738 }
2739 EventSP new_event_sp;
2740 new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt,
2741 new EventDataBytes (tmp_prompt.c_str())));
2742
2743 if (instance_name.GetLength() != 0)
2744 {
2745 // Set prompt for a particular instance.
2746 Debugger *dbg = Debugger::FindDebuggerWithInstanceName (instance_name).get();
2747 if (dbg != NULL)
2748 {
2749 dbg->GetCommandInterpreter().BroadcastEvent (new_event_sp);
2750 }
2751 }
2752
2753 return true;
2754}
2755
2756const ConstString
2757DebuggerInstanceSettings::CreateInstanceName ()
2758{
2759 static int instance_count = 1;
2760 StreamString sstr;
2761
2762 sstr.Printf ("debugger_%d", instance_count);
2763 ++instance_count;
2764
2765 const ConstString ret_val (sstr.GetData());
2766
2767 return ret_val;
2768}
2769
Jim Ingham3bcdb292010-10-04 22:44:14 +00002770
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002771//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00002772// SettingsController Variable Tables
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002773//--------------------------------------------------
2774
2775
2776SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002777Debugger::SettingsController::global_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002778{
2779 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
Caroline Tice101c7c22010-09-09 06:25:08 +00002780 // The Debugger level global table should always be empty; all Debugger settable variables should be instance
2781 // variables.
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002782 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2783};
2784
Greg Claytonbb562b12010-10-04 02:44:26 +00002785#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name}${function.pc-offset}}}"
Greg Clayton0603aa92010-10-04 01:05:56 +00002786#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002787
Greg Clayton0603aa92010-10-04 01:05:56 +00002788#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2789 "{, ${frame.pc}}"\
2790 MODULE_WITH_FUNC\
Greg Claytoncf4b9072010-10-04 17:04:23 +00002791 FILE_AND_LINE\
Greg Clayton0603aa92010-10-04 01:05:56 +00002792 "{, stop reason = ${thread.stop-reason}}"\
Jim Inghamef651602011-12-22 19:12:40 +00002793 "{\\nReturn value: ${thread.return-value}}"\
Greg Clayton0603aa92010-10-04 01:05:56 +00002794 "\\n"
2795
Greg Clayton315d2ca2010-11-02 01:53:21 +00002796//#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2797// "{, ${frame.pc}}"\
2798// MODULE_WITH_FUNC\
2799// FILE_AND_LINE\
2800// "{, stop reason = ${thread.stop-reason}}"\
2801// "{, name = ${thread.name}}"\
2802// "{, queue = ${thread.queue}}"\
2803// "\\n"
2804
Greg Clayton0603aa92010-10-04 01:05:56 +00002805#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
2806 MODULE_WITH_FUNC\
2807 FILE_AND_LINE\
2808 "\\n"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002809
2810SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002811Debugger::SettingsController::instance_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002812{
Greg Clayton0603aa92010-10-04 01:05:56 +00002813// NAME Setting variable type Default Enum Init'd Hidden Help
2814// ======================= ======================= ====================== ==== ====== ====== ======================
Greg Clayton0603aa92010-10-04 01:05:56 +00002815{ "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 +00002816{ "prompt", eSetVarTypeString, "(lldb) ", NULL, false, false, "The debugger command line prompt displayed for the user." },
Jim Ingham3bcdb292010-10-04 22:44:14 +00002817{ "script-lang", eSetVarTypeString, "python", NULL, false, false, "The script language to be used for evaluating user-written scripts." },
2818{ "term-width", eSetVarTypeInt, "80" , NULL, false, false, "The maximum number of columns to use for displaying text." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002819{ "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 +00002820{ "use-external-editor", eSetVarTypeBoolean, "false", NULL, false, false, "Whether to use an external editor or not." },
2821{ "auto-confirm", eSetVarTypeBoolean, "false", NULL, false, false, "If true all confirmation prompts will receive their default reply." },
2822{ "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." },
2823{ "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." },
2824{ "stop-disassembly-count", eSetVarTypeInt, "0", NULL, false, false, "The number of disassembly lines to show when displaying a stopped context." },
2825{ "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 +00002826{ NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002827};