blob: a217ddb36ec41797093251cd314d59a09263f31f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Debugger.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton4a33d312011-06-23 17:59:56 +000010#include "lldb/Core/Debugger.h"
11
12#include <map>
13
Enrico Granata4becb372011-06-29 22:27:15 +000014#include "clang/AST/DeclCXX.h"
15#include "clang/AST/Type.h"
16
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/lldb-private.h"
18#include "lldb/Core/ConnectionFileDescriptor.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000019#include "lldb/Core/FormatManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/InputReader.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000021#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/State.h"
Jim Ingham5b52f0c2011-06-02 23:58:26 +000023#include "lldb/Core/StreamAsynchronousIO.h"
Greg Clayton1b654882010-09-19 02:33:57 +000024#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
Enrico Granata4becb372011-06-29 22:27:15 +000026#include "lldb/Core/ValueObject.h"
Greg Claytona3406612011-02-07 23:24:47 +000027#include "lldb/Host/Terminal.h"
Greg Clayton66111032010-06-23 01:19:29 +000028#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/TargetList.h"
30#include "lldb/Target/Process.h"
Greg Clayton1b654882010-09-19 02:33:57 +000031#include "lldb/Target/RegisterContext.h"
32#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Target/Thread.h"
34
35
36using namespace lldb;
37using namespace lldb_private;
38
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
Greg Clayton1b654882010-09-19 02:33:57 +000040static uint32_t g_shared_debugger_refcount = 0;
Caroline Ticeebc1bb22010-06-30 16:22:25 +000041static lldb::user_id_t g_unique_id = 1;
42
Greg Clayton1b654882010-09-19 02:33:57 +000043#pragma mark Static Functions
44
45static Mutex &
46GetDebuggerListMutex ()
47{
48 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
49 return g_mutex;
50}
51
52typedef std::vector<DebuggerSP> DebuggerList;
53
54static DebuggerList &
55GetDebuggerList()
56{
57 // hide the static debugger list inside a singleton accessor to avoid
58 // global init contructors
59 static DebuggerList g_list;
60 return g_list;
61}
62
63
64#pragma mark Debugger
65
Greg Clayton99d0faf2010-11-18 23:32:35 +000066UserSettingsControllerSP &
67Debugger::GetSettingsController ()
68{
69 static UserSettingsControllerSP g_settings_controller;
70 return g_settings_controller;
71}
72
Caroline Tice2f88aad2011-01-14 00:29:16 +000073int
74Debugger::TestDebuggerRefCount ()
75{
76 return g_shared_debugger_refcount;
77}
78
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079void
80Debugger::Initialize ()
81{
Greg Clayton66111032010-06-23 01:19:29 +000082 if (g_shared_debugger_refcount == 0)
Greg Clayton99d0faf2010-11-18 23:32:35 +000083 {
Greg Claytondbe54502010-11-19 03:46:01 +000084 lldb_private::Initialize();
Greg Clayton99d0faf2010-11-18 23:32:35 +000085 }
Greg Clayton66111032010-06-23 01:19:29 +000086 g_shared_debugger_refcount++;
Greg Clayton99d0faf2010-11-18 23:32:35 +000087
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088}
89
90void
91Debugger::Terminate ()
92{
Greg Clayton66111032010-06-23 01:19:29 +000093 if (g_shared_debugger_refcount > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094 {
Greg Clayton66111032010-06-23 01:19:29 +000095 g_shared_debugger_refcount--;
96 if (g_shared_debugger_refcount == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097 {
Greg Claytondbe54502010-11-19 03:46:01 +000098 lldb_private::WillTerminate();
99 lldb_private::Terminate();
Caroline Tice6760a512011-01-17 21:55:19 +0000100
101 // Clear our master list of debugger objects
102 Mutex::Locker locker (GetDebuggerListMutex ());
103 GetDebuggerList().clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 }
106}
107
Caroline Tice20bd37f2011-03-10 22:14:10 +0000108void
109Debugger::SettingsInitialize ()
110{
111 static bool g_initialized = false;
112
113 if (!g_initialized)
114 {
115 g_initialized = true;
116 UserSettingsControllerSP &usc = GetSettingsController();
117 usc.reset (new SettingsController);
118 UserSettingsController::InitializeSettingsController (usc,
119 SettingsController::global_settings_table,
120 SettingsController::instance_settings_table);
121 // Now call SettingsInitialize for each settings 'child' of Debugger
122 Target::SettingsInitialize ();
123 }
124}
125
126void
127Debugger::SettingsTerminate ()
128{
129
130 // Must call SettingsTerminate() for each settings 'child' of Debugger, before terminating the Debugger's
131 // Settings.
132
133 Target::SettingsTerminate ();
134
135 // Now terminate the Debugger Settings.
136
137 UserSettingsControllerSP &usc = GetSettingsController();
138 UserSettingsController::FinalizeSettingsController (usc);
139 usc.reset();
140}
141
Greg Clayton66111032010-06-23 01:19:29 +0000142DebuggerSP
143Debugger::CreateInstance ()
144{
145 DebuggerSP debugger_sp (new Debugger);
146 // Scope for locker
147 {
148 Mutex::Locker locker (GetDebuggerListMutex ());
149 GetDebuggerList().push_back(debugger_sp);
150 }
151 return debugger_sp;
152}
153
Caroline Ticee02657b2011-01-22 01:02:07 +0000154void
155Debugger::Destroy (lldb::DebuggerSP &debugger_sp)
156{
157 if (debugger_sp.get() == NULL)
158 return;
159
160 Mutex::Locker locker (GetDebuggerListMutex ());
161 DebuggerList &debugger_list = GetDebuggerList ();
162 DebuggerList::iterator pos, end = debugger_list.end();
163 for (pos = debugger_list.begin (); pos != end; ++pos)
164 {
165 if ((*pos).get() == debugger_sp.get())
166 {
167 debugger_list.erase (pos);
168 return;
169 }
170 }
171
172}
173
Greg Clayton66111032010-06-23 01:19:29 +0000174lldb::DebuggerSP
175Debugger::GetSP ()
176{
177 lldb::DebuggerSP debugger_sp;
178
179 Mutex::Locker locker (GetDebuggerListMutex ());
180 DebuggerList &debugger_list = GetDebuggerList();
181 DebuggerList::iterator pos, end = debugger_list.end();
182 for (pos = debugger_list.begin(); pos != end; ++pos)
183 {
184 if ((*pos).get() == this)
185 {
186 debugger_sp = *pos;
187 break;
188 }
189 }
190 return debugger_sp;
191}
192
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000193lldb::DebuggerSP
194Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
195{
196 lldb::DebuggerSP debugger_sp;
197
198 Mutex::Locker locker (GetDebuggerListMutex ());
199 DebuggerList &debugger_list = GetDebuggerList();
200 DebuggerList::iterator pos, end = debugger_list.end();
201
202 for (pos = debugger_list.begin(); pos != end; ++pos)
203 {
204 if ((*pos).get()->m_instance_name == instance_name)
205 {
206 debugger_sp = *pos;
207 break;
208 }
209 }
210 return debugger_sp;
211}
Greg Clayton66111032010-06-23 01:19:29 +0000212
213TargetSP
214Debugger::FindTargetWithProcessID (lldb::pid_t pid)
215{
216 lldb::TargetSP target_sp;
217 Mutex::Locker locker (GetDebuggerListMutex ());
218 DebuggerList &debugger_list = GetDebuggerList();
219 DebuggerList::iterator pos, end = debugger_list.end();
220 for (pos = debugger_list.begin(); pos != end; ++pos)
221 {
222 target_sp = (*pos)->GetTargetList().FindTargetWithProcessID (pid);
223 if (target_sp)
224 break;
225 }
226 return target_sp;
227}
228
229
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230Debugger::Debugger () :
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000231 UserID (g_unique_id++),
Greg Claytondbe54502010-11-19 03:46:01 +0000232 DebuggerInstanceSettings (*GetSettingsController()),
Greg Claytond46c87a2010-12-04 02:39:47 +0000233 m_input_comm("debugger.input"),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 m_input_file (),
235 m_output_file (),
236 m_error_file (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 m_target_list (),
Greg Claytonded470d2011-03-19 01:12:21 +0000238 m_platform_list (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 m_listener ("lldb.Debugger"),
240 m_source_manager (),
Greg Clayton66111032010-06-23 01:19:29 +0000241 m_command_interpreter_ap (new CommandInterpreter (*this, eScriptLanguageDefault, false)),
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000242 m_input_reader_stack (),
Greg Clayton4957bf62010-09-30 21:49:03 +0000243 m_input_reader_data ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244{
Greg Clayton66111032010-06-23 01:19:29 +0000245 m_command_interpreter_ap->Initialize ();
Greg Claytonded470d2011-03-19 01:12:21 +0000246 // Always add our default platform to the platform list
247 PlatformSP default_platform_sp (Platform::GetDefaultPlatform());
248 assert (default_platform_sp.get());
249 m_platform_list.Append (default_platform_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250}
251
252Debugger::~Debugger ()
253{
Caroline Tice3d6086f2010-12-20 18:35:50 +0000254 CleanUpInputReaders();
Greg Clayton66111032010-06-23 01:19:29 +0000255 int num_targets = m_target_list.GetNumTargets();
256 for (int i = 0; i < num_targets; i++)
257 {
258 ProcessSP process_sp (m_target_list.GetTargetAtIndex (i)->GetProcessSP());
259 if (process_sp)
260 process_sp->Destroy();
261 }
262 DisconnectInput();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263}
264
265
266bool
Greg Claytonfc3f0272011-05-29 04:06:55 +0000267Debugger::GetCloseInputOnEOF () const
268{
269 return m_input_comm.GetCloseOnEOF();
270}
271
272void
273Debugger::SetCloseInputOnEOF (bool b)
274{
275 m_input_comm.SetCloseOnEOF(b);
276}
277
278bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279Debugger::GetAsyncExecution ()
280{
Greg Clayton66111032010-06-23 01:19:29 +0000281 return !m_command_interpreter_ap->GetSynchronous();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282}
283
284void
285Debugger::SetAsyncExecution (bool async_execution)
286{
Greg Clayton66111032010-06-23 01:19:29 +0000287 m_command_interpreter_ap->SetSynchronous (!async_execution);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288}
289
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290
291void
292Debugger::SetInputFileHandle (FILE *fh, bool tranfer_ownership)
293{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000294 File &in_file = GetInputFile();
295 in_file.SetStream (fh, tranfer_ownership);
296 if (in_file.IsValid() == false)
297 in_file.SetStream (stdin, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298
299 // Disconnect from any old connection if we had one
300 m_input_comm.Disconnect ();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000301 m_input_comm.SetConnection (new ConnectionFileDescriptor (in_file.GetDescriptor(), true));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 m_input_comm.SetReadThreadBytesReceivedCallback (Debugger::DispatchInputCallback, this);
303
304 Error error;
305 if (m_input_comm.StartReadThread (&error) == false)
306 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000307 File &err_file = GetErrorFile();
308
309 err_file.Printf ("error: failed to main input read thread: %s", error.AsCString() ? error.AsCString() : "unkown error");
310 exit(1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312}
313
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314void
315Debugger::SetOutputFileHandle (FILE *fh, bool tranfer_ownership)
316{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000317 File &out_file = GetOutputFile();
318 out_file.SetStream (fh, tranfer_ownership);
319 if (out_file.IsValid() == false)
320 out_file.SetStream (stdout, false);
Caroline Tice2f88aad2011-01-14 00:29:16 +0000321
322 GetCommandInterpreter().GetScriptInterpreter()->ResetOutputFileHandle (fh);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323}
324
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325void
326Debugger::SetErrorFileHandle (FILE *fh, bool tranfer_ownership)
327{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000328 File &err_file = GetErrorFile();
329 err_file.SetStream (fh, tranfer_ownership);
330 if (err_file.IsValid() == false)
331 err_file.SetStream (stderr, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332}
333
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334ExecutionContext
Jim Ingham2976d002010-08-26 21:32:51 +0000335Debugger::GetSelectedExecutionContext ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336{
337 ExecutionContext exe_ctx;
338 exe_ctx.Clear();
339
Jim Ingham2976d002010-08-26 21:32:51 +0000340 lldb::TargetSP target_sp = GetSelectedTarget();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341 exe_ctx.target = target_sp.get();
342
343 if (target_sp)
344 {
345 exe_ctx.process = target_sp->GetProcessSP().get();
346 if (exe_ctx.process && exe_ctx.process->IsRunning() == false)
347 {
Jim Ingham2976d002010-08-26 21:32:51 +0000348 exe_ctx.thread = exe_ctx.process->GetThreadList().GetSelectedThread().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 if (exe_ctx.thread == NULL)
350 exe_ctx.thread = exe_ctx.process->GetThreadList().GetThreadAtIndex(0).get();
351 if (exe_ctx.thread)
352 {
Jim Ingham2976d002010-08-26 21:32:51 +0000353 exe_ctx.frame = exe_ctx.thread->GetSelectedFrame().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 if (exe_ctx.frame == NULL)
355 exe_ctx.frame = exe_ctx.thread->GetStackFrameAtIndex (0).get();
356 }
357 }
358 }
359 return exe_ctx;
360
361}
362
Caroline Ticeb44880c2011-02-10 01:15:13 +0000363InputReaderSP
364Debugger::GetCurrentInputReader ()
365{
366 InputReaderSP reader_sp;
367
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000368 if (!m_input_reader_stack.IsEmpty())
Caroline Ticeb44880c2011-02-10 01:15:13 +0000369 {
370 // Clear any finished readers from the stack
371 while (CheckIfTopInputReaderIsDone()) ;
372
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000373 if (!m_input_reader_stack.IsEmpty())
374 reader_sp = m_input_reader_stack.Top();
Caroline Ticeb44880c2011-02-10 01:15:13 +0000375 }
376
377 return reader_sp;
378}
379
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380void
381Debugger::DispatchInputCallback (void *baton, const void *bytes, size_t bytes_len)
382{
Caroline Ticeefed6132010-11-19 20:47:54 +0000383 if (bytes_len > 0)
384 ((Debugger *)baton)->DispatchInput ((char *)bytes, bytes_len);
385 else
386 ((Debugger *)baton)->DispatchInputEndOfFile ();
387}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388
389
390void
391Debugger::DispatchInput (const char *bytes, size_t bytes_len)
392{
Caroline Ticeefed6132010-11-19 20:47:54 +0000393 if (bytes == NULL || bytes_len == 0)
394 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395
396 WriteToDefaultReader (bytes, bytes_len);
397}
398
399void
Caroline Ticeefed6132010-11-19 20:47:54 +0000400Debugger::DispatchInputInterrupt ()
401{
402 m_input_reader_data.clear();
403
Caroline Ticeb44880c2011-02-10 01:15:13 +0000404 InputReaderSP reader_sp (GetCurrentInputReader ());
405 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000406 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000407 reader_sp->Notify (eInputReaderInterrupt);
Caroline Ticeefed6132010-11-19 20:47:54 +0000408
Caroline Ticeb44880c2011-02-10 01:15:13 +0000409 // If notifying the reader of the interrupt finished the reader, we should pop it off the stack.
Caroline Ticeefed6132010-11-19 20:47:54 +0000410 while (CheckIfTopInputReaderIsDone ()) ;
411 }
412}
413
414void
415Debugger::DispatchInputEndOfFile ()
416{
417 m_input_reader_data.clear();
418
Caroline Ticeb44880c2011-02-10 01:15:13 +0000419 InputReaderSP reader_sp (GetCurrentInputReader ());
420 if (reader_sp)
Caroline Ticeefed6132010-11-19 20:47:54 +0000421 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000422 reader_sp->Notify (eInputReaderEndOfFile);
Caroline Ticeefed6132010-11-19 20:47:54 +0000423
Caroline Ticeb44880c2011-02-10 01:15:13 +0000424 // 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 +0000425 while (CheckIfTopInputReaderIsDone ()) ;
426 }
427}
428
429void
Caroline Tice3d6086f2010-12-20 18:35:50 +0000430Debugger::CleanUpInputReaders ()
431{
432 m_input_reader_data.clear();
433
Caroline Ticeb44880c2011-02-10 01:15:13 +0000434 // 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 +0000435 while (m_input_reader_stack.GetSize() > 1)
Caroline Tice3d6086f2010-12-20 18:35:50 +0000436 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000437 InputReaderSP reader_sp (GetCurrentInputReader ());
Caroline Tice3d6086f2010-12-20 18:35:50 +0000438 if (reader_sp)
439 {
440 reader_sp->Notify (eInputReaderEndOfFile);
441 reader_sp->SetIsDone (true);
442 }
443 }
444}
445
446void
Caroline Tice969ed3d2011-05-02 20:41:46 +0000447Debugger::NotifyTopInputReader (InputReaderAction notification)
448{
449 InputReaderSP reader_sp (GetCurrentInputReader());
450 if (reader_sp)
451 {
452 reader_sp->Notify (notification);
453
454 // Flush out any input readers that are done.
455 while (CheckIfTopInputReaderIsDone ())
456 /* Do nothing. */;
457 }
458}
459
Caroline Tice9088b062011-05-09 23:06:58 +0000460bool
461Debugger::InputReaderIsTopReader (const lldb::InputReaderSP& reader_sp)
462{
Caroline Ticed61c10b2011-06-16 16:27:19 +0000463 InputReaderSP top_reader_sp (GetCurrentInputReader());
Caroline Tice9088b062011-05-09 23:06:58 +0000464
Caroline Ticed61c10b2011-06-16 16:27:19 +0000465 return (reader_sp.get() == top_reader_sp.get());
Caroline Tice9088b062011-05-09 23:06:58 +0000466}
467
468
Caroline Tice969ed3d2011-05-02 20:41:46 +0000469void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470Debugger::WriteToDefaultReader (const char *bytes, size_t bytes_len)
471{
472 if (bytes && bytes_len)
473 m_input_reader_data.append (bytes, bytes_len);
474
475 if (m_input_reader_data.empty())
476 return;
477
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000478 while (!m_input_reader_stack.IsEmpty() && !m_input_reader_data.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 // Get the input reader from the top of the stack
Caroline Ticeb44880c2011-02-10 01:15:13 +0000481 InputReaderSP reader_sp (GetCurrentInputReader ());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 if (!reader_sp)
483 break;
484
Greg Clayton471b31c2010-07-20 22:52:08 +0000485 size_t bytes_handled = reader_sp->HandleRawBytes (m_input_reader_data.c_str(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486 m_input_reader_data.size());
487 if (bytes_handled)
488 {
489 m_input_reader_data.erase (0, bytes_handled);
490 }
491 else
492 {
493 // No bytes were handled, we might not have reached our
494 // granularity, just return and wait for more data
495 break;
496 }
497 }
498
Caroline Ticeb44880c2011-02-10 01:15:13 +0000499 // Flush out any input readers that are done.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 while (CheckIfTopInputReaderIsDone ())
501 /* Do nothing. */;
502
503}
504
505void
506Debugger::PushInputReader (const InputReaderSP& reader_sp)
507{
508 if (!reader_sp)
509 return;
Caroline Ticeb44880c2011-02-10 01:15:13 +0000510
511 // Deactivate the old top reader
512 InputReaderSP top_reader_sp (GetCurrentInputReader ());
513
514 if (top_reader_sp)
515 top_reader_sp->Notify (eInputReaderDeactivate);
516
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000517 m_input_reader_stack.Push (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518 reader_sp->Notify (eInputReaderActivate);
519 ActivateInputReader (reader_sp);
520}
521
522bool
523Debugger::PopInputReader (const lldb::InputReaderSP& pop_reader_sp)
524{
525 bool result = false;
526
527 // The reader on the stop of the stack is done, so let the next
528 // read on the stack referesh its prompt and if there is one...
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000529 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000531 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000532 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533
534 if (!pop_reader_sp || pop_reader_sp.get() == reader_sp.get())
535 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000536 m_input_reader_stack.Pop ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537 reader_sp->Notify (eInputReaderDeactivate);
538 reader_sp->Notify (eInputReaderDone);
539 result = true;
540
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000541 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 {
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000543 reader_sp = m_input_reader_stack.Top();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544 if (reader_sp)
545 {
546 ActivateInputReader (reader_sp);
547 reader_sp->Notify (eInputReaderReactivate);
548 }
549 }
550 }
551 }
552 return result;
553}
554
555bool
556Debugger::CheckIfTopInputReaderIsDone ()
557{
558 bool result = false;
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000559 if (!m_input_reader_stack.IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 {
Caroline Ticeb44880c2011-02-10 01:15:13 +0000561 // Cannot call GetCurrentInputReader here, as that would cause an infinite loop.
Caroline Ticed5a0a01b2011-06-02 19:18:55 +0000562 InputReaderSP reader_sp(m_input_reader_stack.Top());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563
564 if (reader_sp && reader_sp->IsDone())
565 {
566 result = true;
567 PopInputReader (reader_sp);
568 }
569 }
570 return result;
571}
572
573void
574Debugger::ActivateInputReader (const InputReaderSP &reader_sp)
575{
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000576 int input_fd = m_input_file.GetFile().GetDescriptor();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000578 if (input_fd >= 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000580 Terminal tty(input_fd);
Greg Claytona3406612011-02-07 23:24:47 +0000581
582 tty.SetEcho(reader_sp->GetEcho());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583
Greg Claytona3406612011-02-07 23:24:47 +0000584 switch (reader_sp->GetGranularity())
585 {
586 case eInputReaderGranularityByte:
587 case eInputReaderGranularityWord:
588 tty.SetCanonical (false);
589 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590
Greg Claytona3406612011-02-07 23:24:47 +0000591 case eInputReaderGranularityLine:
592 case eInputReaderGranularityAll:
593 tty.SetCanonical (true);
594 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595
Greg Claytona3406612011-02-07 23:24:47 +0000596 default:
597 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 }
599 }
600}
Greg Clayton66111032010-06-23 01:19:29 +0000601
Jim Ingham5b52f0c2011-06-02 23:58:26 +0000602StreamSP
603Debugger::GetAsyncOutputStream ()
604{
605 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
606 CommandInterpreter::eBroadcastBitAsynchronousOutputData));
607}
608
609StreamSP
610Debugger::GetAsyncErrorStream ()
611{
612 return StreamSP (new StreamAsynchronousIO (GetCommandInterpreter(),
613 CommandInterpreter::eBroadcastBitAsynchronousErrorData));
614}
615
Caroline Ticeebc1bb22010-06-30 16:22:25 +0000616DebuggerSP
617Debugger::FindDebuggerWithID (lldb::user_id_t id)
618{
619 lldb::DebuggerSP debugger_sp;
620
621 Mutex::Locker locker (GetDebuggerListMutex ());
622 DebuggerList &debugger_list = GetDebuggerList();
623 DebuggerList::iterator pos, end = debugger_list.end();
624 for (pos = debugger_list.begin(); pos != end; ++pos)
625 {
626 if ((*pos).get()->GetID() == id)
627 {
628 debugger_sp = *pos;
629 break;
630 }
631 }
632 return debugger_sp;
633}
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000634
Greg Clayton1b654882010-09-19 02:33:57 +0000635static void
636TestPromptFormats (StackFrame *frame)
637{
638 if (frame == NULL)
639 return;
640
641 StreamString s;
642 const char *prompt_format =
643 "{addr = '${addr}'\n}"
644 "{process.id = '${process.id}'\n}"
645 "{process.name = '${process.name}'\n}"
646 "{process.file.basename = '${process.file.basename}'\n}"
647 "{process.file.fullpath = '${process.file.fullpath}'\n}"
648 "{thread.id = '${thread.id}'\n}"
649 "{thread.index = '${thread.index}'\n}"
650 "{thread.name = '${thread.name}'\n}"
651 "{thread.queue = '${thread.queue}'\n}"
652 "{thread.stop-reason = '${thread.stop-reason}'\n}"
653 "{target.arch = '${target.arch}'\n}"
654 "{module.file.basename = '${module.file.basename}'\n}"
655 "{module.file.fullpath = '${module.file.fullpath}'\n}"
656 "{file.basename = '${file.basename}'\n}"
657 "{file.fullpath = '${file.fullpath}'\n}"
658 "{frame.index = '${frame.index}'\n}"
659 "{frame.pc = '${frame.pc}'\n}"
660 "{frame.sp = '${frame.sp}'\n}"
661 "{frame.fp = '${frame.fp}'\n}"
662 "{frame.flags = '${frame.flags}'\n}"
663 "{frame.reg.rdi = '${frame.reg.rdi}'\n}"
664 "{frame.reg.rip = '${frame.reg.rip}'\n}"
665 "{frame.reg.rsp = '${frame.reg.rsp}'\n}"
666 "{frame.reg.rbp = '${frame.reg.rbp}'\n}"
667 "{frame.reg.rflags = '${frame.reg.rflags}'\n}"
668 "{frame.reg.xmm0 = '${frame.reg.xmm0}'\n}"
669 "{frame.reg.carp = '${frame.reg.carp}'\n}"
670 "{function.id = '${function.id}'\n}"
671 "{function.name = '${function.name}'\n}"
672 "{function.addr-offset = '${function.addr-offset}'\n}"
673 "{function.line-offset = '${function.line-offset}'\n}"
674 "{function.pc-offset = '${function.pc-offset}'\n}"
675 "{line.file.basename = '${line.file.basename}'\n}"
676 "{line.file.fullpath = '${line.file.fullpath}'\n}"
677 "{line.number = '${line.number}'\n}"
678 "{line.start-addr = '${line.start-addr}'\n}"
679 "{line.end-addr = '${line.end-addr}'\n}"
680;
681
682 SymbolContext sc (frame->GetSymbolContext(eSymbolContextEverything));
683 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +0000684 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton1b654882010-09-19 02:33:57 +0000685 const char *end = NULL;
686 if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
687 {
688 printf("%s\n", s.GetData());
689 }
690 else
691 {
692 printf ("error: at '%s'\n", end);
693 printf ("what we got: %s\n", s.GetData());
694 }
695}
696
Enrico Granata9fc19442011-07-06 02:13:41 +0000697#define IFERROR_PRINT_IT if (error.Fail()) \
698{ \
Enrico Granatae992a082011-07-22 17:03:19 +0000699 if (log) \
700 log->Printf("ERROR: %s\n", error.AsCString("unknown")); \
Enrico Granata9fc19442011-07-06 02:13:41 +0000701 break; \
702}
Enrico Granata9fc19442011-07-06 02:13:41 +0000703
704static bool
705ScanFormatDescriptor(const char* var_name_begin,
706 const char* var_name_end,
707 const char** var_name_final,
708 const char** percent_position,
709 lldb::Format* custom_format,
710 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
711{
Enrico Granatae992a082011-07-22 17:03:19 +0000712 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000713 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +0000714 if (!*percent_position || *percent_position > var_name_end)
Enrico Granatae992a082011-07-22 17:03:19 +0000715 {
716 if (log)
717 log->Printf("no format descriptor in string, skipping");
Enrico Granata9fc19442011-07-06 02:13:41 +0000718 *var_name_final = var_name_end;
Enrico Granatae992a082011-07-22 17:03:19 +0000719 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000720 else
721 {
722 *var_name_final = *percent_position;
723 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
724 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
Enrico Granatae992a082011-07-22 17:03:19 +0000725 if (log)
726 log->Printf("parsing %s as a format descriptor", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000727 if ( !FormatManager::GetFormatFromCString(format_name,
728 true,
729 *custom_format) )
730 {
Enrico Granatae992a082011-07-22 17:03:19 +0000731 if (log)
732 log->Printf("%s is an unknown format", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000733 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +0000734 if (*format_name == '@')
Enrico Granata9fc19442011-07-06 02:13:41 +0000735 *val_obj_display = ValueObject::eDisplayLanguageSpecific;
736 // if this is a V, print the value using the default format
Enrico Granatae992a082011-07-22 17:03:19 +0000737 else if (*format_name == 'V')
Enrico Granata9fc19442011-07-06 02:13:41 +0000738 *val_obj_display = ValueObject::eDisplayValue;
Enrico Granatad55546b2011-07-22 00:16:08 +0000739 // if this is an L, print the location of the value
Enrico Granatae992a082011-07-22 17:03:19 +0000740 else if (*format_name == 'L')
Enrico Granataf2bbf712011-07-15 02:26:42 +0000741 *val_obj_display = ValueObject::eDisplayLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +0000742 // if this is an S, print the summary after all
Enrico Granatae992a082011-07-22 17:03:19 +0000743 else if (*format_name == 'S')
Enrico Granatad55546b2011-07-22 00:16:08 +0000744 *val_obj_display = ValueObject::eDisplaySummary;
Enrico Granatae992a082011-07-22 17:03:19 +0000745 else if (log)
746 log->Printf("%s is an error, leaving the previous value alone", format_name);
Enrico Granata9fc19442011-07-06 02:13:41 +0000747 }
748 // a good custom format tells us to print the value using it
749 else
Enrico Granatae992a082011-07-22 17:03:19 +0000750 {
751 if (log)
752 log->Printf("will display value for this VO");
Enrico Granata9fc19442011-07-06 02:13:41 +0000753 *val_obj_display = ValueObject::eDisplayValue;
Enrico Granatae992a082011-07-22 17:03:19 +0000754 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000755 delete format_name;
756 }
Enrico Granatae992a082011-07-22 17:03:19 +0000757 if (log)
758 log->Printf("final format description outcome: custom_format = %d, val_obj_display = %d",
759 *custom_format,
760 *val_obj_display);
Enrico Granata9fc19442011-07-06 02:13:41 +0000761 return true;
762}
763
764static bool
765ScanBracketedRange(const char* var_name_begin,
766 const char* var_name_end,
767 const char* var_name_final,
768 const char** open_bracket_position,
769 const char** separator_position,
770 const char** close_bracket_position,
771 const char** var_name_final_if_array_range,
772 int64_t* index_lower,
773 int64_t* index_higher)
774{
Enrico Granatae992a082011-07-22 17:03:19 +0000775 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000776 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +0000777 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +0000778 {
779 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
780 *close_bracket_position = ::strchr(*open_bracket_position,']');
781 // as usual, we assume that [] will come before %
782 //printf("trying to expand a []\n");
783 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +0000784 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +0000785 {
Enrico Granatae992a082011-07-22 17:03:19 +0000786 if (log)
787 log->Printf("[] detected.. going from 0 to end of data");
Enrico Granata9fc19442011-07-06 02:13:41 +0000788 *index_lower = 0;
789 }
790 else if (*separator_position == NULL || *separator_position > var_name_end)
791 {
792 char *end = NULL;
793 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
794 *index_higher = *index_lower;
Enrico Granatae992a082011-07-22 17:03:19 +0000795 if (log)
796 log->Printf("[%d] detected, high index is same",index_lower);
Enrico Granata9fc19442011-07-06 02:13:41 +0000797 }
Greg Clayton34132752011-07-06 04:07:21 +0000798 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +0000799 {
800 char *end = NULL;
801 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
802 *index_higher = ::strtoul (*separator_position+1, &end, 0);
Enrico Granatae992a082011-07-22 17:03:19 +0000803 if (log)
804 log->Printf("[%d-%d] detected",index_lower,index_higher);
Enrico Granata9fc19442011-07-06 02:13:41 +0000805 }
806 else
Enrico Granatae992a082011-07-22 17:03:19 +0000807 {
808 if (log)
809 log->Printf("expression is erroneous, cannot extract indices out of it");
Enrico Granata9fc19442011-07-06 02:13:41 +0000810 return false;
Enrico Granatae992a082011-07-22 17:03:19 +0000811 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000812 if (*index_lower > *index_higher && *index_higher > 0)
813 {
Enrico Granatae992a082011-07-22 17:03:19 +0000814 if (log)
815 log->Printf("swapping indices");
Enrico Granata9fc19442011-07-06 02:13:41 +0000816 int temp = *index_lower;
817 *index_lower = *index_higher;
818 *index_higher = temp;
819 }
820 }
Enrico Granatae992a082011-07-22 17:03:19 +0000821 else if (log)
822 log->Printf("no bracketed range, skipping entirely");
Enrico Granata9fc19442011-07-06 02:13:41 +0000823 return true;
824}
825
826
827static ValueObjectSP
828ExpandExpressionPath(ValueObject* vobj,
829 StackFrame* frame,
830 bool* do_deref_pointer,
831 const char* var_name_begin,
832 const char* var_name_final,
833 Error& error)
834{
Enrico Granatae992a082011-07-22 17:03:19 +0000835 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata9fc19442011-07-06 02:13:41 +0000836 StreamString sstring;
837 VariableSP var_sp;
838
Greg Clayton34132752011-07-06 04:07:21 +0000839 if (*do_deref_pointer)
Enrico Granatae992a082011-07-22 17:03:19 +0000840 {
841 if (log)
842 log->Printf("been told to deref_pointer by caller");
Enrico Granata9fc19442011-07-06 02:13:41 +0000843 sstring.PutChar('*');
Enrico Granatae992a082011-07-22 17:03:19 +0000844 }
Greg Clayton34132752011-07-06 04:07:21 +0000845 else if (vobj->IsDereferenceOfParent() && ClangASTContext::IsPointerType(vobj->GetParent()->GetClangType()) && !vobj->IsArrayItemForPointer())
Enrico Granata9fc19442011-07-06 02:13:41 +0000846 {
Enrico Granatae992a082011-07-22 17:03:19 +0000847 if (log)
848 log->Printf("decided to deref_pointer myself");
Enrico Granata9fc19442011-07-06 02:13:41 +0000849 sstring.PutChar('*');
850 *do_deref_pointer = true;
851 }
852
853 vobj->GetExpressionPath(sstring, true, ValueObject::eHonorPointers);
Enrico Granatae992a082011-07-22 17:03:19 +0000854 if (log)
855 log->Printf("expression path to expand in phase 0: %s",sstring.GetData());
Enrico Granata9fc19442011-07-06 02:13:41 +0000856 sstring.PutRawBytes(var_name_begin+3, var_name_final-var_name_begin-3);
Enrico Granatae992a082011-07-22 17:03:19 +0000857 if (log)
858 log->Printf("expression path to expand in phase 1: %s",sstring.GetData());
Enrico Granata9fc19442011-07-06 02:13:41 +0000859 std::string name = std::string(sstring.GetData());
860 ValueObjectSP target = frame->GetValueForVariableExpressionPath (name.c_str(),
861 eNoDynamicValues,
862 0,
863 var_sp,
864 error);
865 return target;
866}
867
868static ValueObjectSP
869ExpandIndexedExpression(ValueObject* vobj,
870 uint32_t index,
871 StackFrame* frame,
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000872 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +0000873{
Enrico Granatae992a082011-07-22 17:03:19 +0000874 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000875 const char* ptr_deref_format = "[%d]";
876 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
877 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
Enrico Granatae992a082011-07-22 17:03:19 +0000878 if (log)
879 log->Printf("name to deref: %s",ptr_deref_buffer.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000880 const char* first_unparsed;
881 ValueObject::GetValueForExpressionPathOptions options;
882 ValueObject::ExpressionPathEndResultType final_value_type;
883 ValueObject::ExpressionPathScanEndReason reason_to_stop;
884 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eDereference : ValueObject::eNothing);
885 ValueObjectSP item = vobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
886 &first_unparsed,
887 &reason_to_stop,
888 &final_value_type,
889 options,
890 &what_next);
891 if (!item)
892 {
Enrico Granatae992a082011-07-22 17:03:19 +0000893 if (log)
894 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
895 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000896 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000897 }
Enrico Granata9fc19442011-07-06 02:13:41 +0000898 else
899 {
Enrico Granatae992a082011-07-22 17:03:19 +0000900 if (log)
901 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
902 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000903 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +0000904 }
905 return item;
906}
907
Greg Clayton1b654882010-09-19 02:33:57 +0000908bool
909Debugger::FormatPrompt
910(
911 const char *format,
912 const SymbolContext *sc,
913 const ExecutionContext *exe_ctx,
914 const Address *addr,
915 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +0000916 const char **end,
917 ValueObject* vobj
Greg Clayton1b654882010-09-19 02:33:57 +0000918)
919{
Enrico Granata4becb372011-06-29 22:27:15 +0000920 ValueObject* realvobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +0000921 bool success = true;
922 const char *p;
Enrico Granatae992a082011-07-22 17:03:19 +0000923 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Greg Clayton1b654882010-09-19 02:33:57 +0000924 for (p = format; *p != '\0'; ++p)
925 {
Greg Clayton34132752011-07-06 04:07:21 +0000926 if (realvobj)
Enrico Granata4becb372011-06-29 22:27:15 +0000927 {
928 vobj = realvobj;
929 realvobj = NULL;
930 }
Greg Clayton1b654882010-09-19 02:33:57 +0000931 size_t non_special_chars = ::strcspn (p, "${}\\");
932 if (non_special_chars > 0)
933 {
934 if (success)
935 s.Write (p, non_special_chars);
936 p += non_special_chars;
937 }
938
939 if (*p == '\0')
940 {
941 break;
942 }
943 else if (*p == '{')
944 {
945 // Start a new scope that must have everything it needs if it is to
946 // to make it into the final output stream "s". If you want to make
947 // a format that only prints out the function or symbol name if there
948 // is one in the symbol context you can use:
949 // "{function =${function.name}}"
950 // The first '{' starts a new scope that end with the matching '}' at
951 // the end of the string. The contents "function =${function.name}"
952 // will then be evaluated and only be output if there is a function
953 // or symbol with a valid name.
954 StreamString sub_strm;
955
956 ++p; // Skip the '{'
957
Enrico Granata4becb372011-06-29 22:27:15 +0000958 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, vobj))
Greg Clayton1b654882010-09-19 02:33:57 +0000959 {
960 // The stream had all it needed
961 s.Write(sub_strm.GetData(), sub_strm.GetSize());
962 }
963 if (*p != '}')
964 {
965 success = false;
966 break;
967 }
968 }
969 else if (*p == '}')
970 {
971 // End of a enclosing scope
972 break;
973 }
974 else if (*p == '$')
975 {
976 // We have a prompt variable to print
977 ++p;
978 if (*p == '{')
979 {
980 ++p;
981 const char *var_name_begin = p;
982 const char *var_name_end = ::strchr (p, '}');
983
984 if (var_name_end && var_name_begin < var_name_end)
985 {
986 // if we have already failed to parse, skip this variable
987 if (success)
988 {
989 const char *cstr = NULL;
990 Address format_addr;
991 bool calculate_format_addr_function_offset = false;
992 // Set reg_kind and reg_num to invalid values
993 RegisterKind reg_kind = kNumRegisterKinds;
994 uint32_t reg_num = LLDB_INVALID_REGNUM;
995 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +0000996 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +0000997 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +0000998 bool do_deref_pointer = false;
Enrico Granatae992a082011-07-22 17:03:19 +0000999 ValueObject::ExpressionPathScanEndReason reason_to_stop = ValueObject::eEndOfString;
1000 ValueObject::ExpressionPathEndResultType final_value_type = ValueObject::ePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001001
Greg Clayton1b654882010-09-19 02:33:57 +00001002 // Each variable must set success to true below...
1003 bool var_success = false;
1004 switch (var_name_begin[0])
1005 {
Enrico Granata4becb372011-06-29 22:27:15 +00001006 case '*':
Enrico Granata4becb372011-06-29 22:27:15 +00001007 case 'v':
Enrico Granata6f3533f2011-07-29 19:53:35 +00001008 case 's':
Enrico Granata4becb372011-06-29 22:27:15 +00001009 {
Enrico Granata6f3533f2011-07-29 19:53:35 +00001010 if (!vobj)
1011 break;
1012
1013 // check for *var and *svar
1014 if (*var_name_begin == '*')
1015 {
1016 do_deref_pointer = true;
1017 var_name_begin++;
1018 }
1019 if (*var_name_begin == 's')
1020 {
1021 vobj = vobj->GetSyntheticValue(lldb::eUseSyntheticFilter).get();
1022 var_name_begin++;
1023 }
1024
1025 // should be a 'v' by now
1026 if (*var_name_begin != 'v')
1027 break;
1028
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001029 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
1030 ValueObject::eDereference : ValueObject::eNothing);
1031 ValueObject::GetValueForExpressionPathOptions options;
1032 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar();
Greg Clayton34132752011-07-06 04:07:21 +00001033 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eDisplaySummary;
1034 ValueObject* target = NULL;
1035 lldb::Format custom_format = eFormatInvalid;
1036 const char* var_name_final = NULL;
1037 const char* var_name_final_if_array_range = NULL;
1038 const char* close_bracket_position = NULL;
1039 int64_t index_lower = -1;
1040 int64_t index_higher = -1;
1041 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001042 const char* first_unparsed;
1043
Greg Clayton34132752011-07-06 04:07:21 +00001044 if (!vobj) break;
1045 // simplest case ${var}, just print vobj's value
1046 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001047 {
Greg Clayton34132752011-07-06 04:07:21 +00001048 target = vobj;
1049 val_obj_display = ValueObject::eDisplayValue;
1050 }
1051 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1052 {
1053 // this is a variable with some custom format applied to it
1054 const char* percent_position;
1055 target = vobj;
1056 val_obj_display = ValueObject::eDisplayValue;
1057 ScanFormatDescriptor (var_name_begin,
1058 var_name_end,
1059 &var_name_final,
1060 &percent_position,
1061 &custom_format,
1062 &val_obj_display);
1063 }
1064 // this is ${var.something} or multiple .something nested
1065 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1066 {
1067
1068 const char* percent_position;
1069 ScanFormatDescriptor (var_name_begin,
1070 var_name_end,
1071 &var_name_final,
1072 &percent_position,
1073 &custom_format,
1074 &val_obj_display);
1075
1076 const char* open_bracket_position;
1077 const char* separator_position;
1078 ScanBracketedRange (var_name_begin,
1079 var_name_end,
1080 var_name_final,
1081 &open_bracket_position,
1082 &separator_position,
1083 &close_bracket_position,
1084 &var_name_final_if_array_range,
1085 &index_lower,
1086 &index_higher);
1087
1088 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001089
1090 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1091 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1092 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1093
Enrico Granatae992a082011-07-22 17:03:19 +00001094 if (log)
1095 log->Printf("symbol to expand: %s",expr_path.get());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001096
1097 target = vobj->GetValueForExpressionPath(expr_path.get(),
1098 &first_unparsed,
1099 &reason_to_stop,
1100 &final_value_type,
1101 options,
1102 &what_next).get();
1103
1104 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001105 {
Enrico Granatae992a082011-07-22 17:03:19 +00001106 if (log)
1107 log->Printf("ERROR: unparsed portion = %s, why stopping = %d,"
1108 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001109 first_unparsed, reason_to_stop, final_value_type);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001110 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001111 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001112 else
1113 {
Enrico Granatae992a082011-07-22 17:03:19 +00001114 if (log)
1115 log->Printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1116 " final_value_type %d",
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001117 first_unparsed, reason_to_stop, final_value_type);
1118 }
Enrico Granata4becb372011-06-29 22:27:15 +00001119 }
Greg Clayton34132752011-07-06 04:07:21 +00001120 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001121 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001122
1123 is_array_range = (final_value_type == ValueObject::eBoundedRange ||
1124 final_value_type == ValueObject::eUnboundedRange);
1125
1126 do_deref_pointer = (what_next == ValueObject::eDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001127
Enrico Granataa7187d02011-07-06 19:27:11 +00001128 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001129 {
Greg Clayton34132752011-07-06 04:07:21 +00001130 // I have not deref-ed yet, let's do it
1131 // this happens when we are not going through GetValueForVariableExpressionPath
1132 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001133 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001134 target = target->Dereference(error).get();
Enrico Granata9fc19442011-07-06 02:13:41 +00001135 IFERROR_PRINT_IT
Greg Clayton34132752011-07-06 04:07:21 +00001136 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001137 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001138
1139 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1140 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
1141
1142 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eDisplayValue) // this should be wrong, but there are some exceptions
1143 {
Enrico Granatae992a082011-07-22 17:03:19 +00001144 if (log)
1145 log->Printf("I am into array || pointer && !range");
Enrico Granataf4efecd2011-07-12 22:56:10 +00001146 // try to use the special cases
1147 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1148 if (!var_success)
1149 s << "<invalid, please use [] operator>";
Enrico Granatae992a082011-07-22 17:03:19 +00001150 if (log)
1151 log->Printf("special cases did%s match", var_success ? "" : "n't");
Enrico Granataf4efecd2011-07-12 22:56:10 +00001152 break;
1153 }
Greg Clayton34132752011-07-06 04:07:21 +00001154
1155 if (!is_array_range)
Enrico Granatae992a082011-07-22 17:03:19 +00001156 {
1157 if (log)
1158 log->Printf("dumping ordinary printable output");
Greg Clayton34132752011-07-06 04:07:21 +00001159 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
Enrico Granatae992a082011-07-22 17:03:19 +00001160 }
Greg Clayton34132752011-07-06 04:07:21 +00001161 else
Enrico Granatae992a082011-07-22 17:03:19 +00001162 {
1163 if (log)
1164 log->Printf("checking if I can handle as array");
Greg Clayton34132752011-07-06 04:07:21 +00001165 if (!is_array && !is_pointer)
1166 break;
Enrico Granatae992a082011-07-22 17:03:19 +00001167 if (log)
1168 log->Printf("handle as array");
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001169 const char* special_directions = NULL;
1170 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001171 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1172 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001173 ConstString additional_data;
1174 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1175 special_directions_writer.Printf("${%svar%s}",
1176 do_deref_pointer ? "*" : "",
1177 additional_data.GetCString());
1178 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001179 }
1180
1181 // let us display items index_lower thru index_higher of this array
1182 s.PutChar('[');
1183 var_success = true;
1184
1185 if (index_higher < 0)
1186 index_higher = vobj->GetNumChildren() - 1;
1187
1188 for (;index_lower<=index_higher;index_lower++)
1189 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001190 ValueObject* item = ExpandIndexedExpression(target,
Greg Clayton34132752011-07-06 04:07:21 +00001191 index_lower,
1192 exe_ctx->frame,
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001193 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001194
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001195 if (!item)
1196 {
Enrico Granatae992a082011-07-22 17:03:19 +00001197 if (log)
1198 log->Printf("ERROR in getting child item at index %d", index_lower);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001199 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001200 else
1201 {
Enrico Granatae992a082011-07-22 17:03:19 +00001202 if (log)
1203 log->Printf("special_directions for child item: %s",special_directions);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001204 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001205
Greg Clayton34132752011-07-06 04:07:21 +00001206 if (!special_directions)
1207 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1208 else
1209 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1210
1211 if (index_lower < index_higher)
1212 s.PutChar(',');
1213 }
1214 s.PutChar(']');
1215 }
Enrico Granata4becb372011-06-29 22:27:15 +00001216 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001217 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001218 case 'a':
1219 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1220 {
1221 if (addr && addr->IsValid())
1222 {
1223 var_success = true;
1224 format_addr = *addr;
1225 }
1226 }
1227 break;
1228
1229 case 'p':
1230 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1231 {
1232 if (exe_ctx && exe_ctx->process != NULL)
1233 {
1234 var_name_begin += ::strlen ("process.");
1235 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1236 {
1237 s.Printf("%i", exe_ctx->process->GetID());
1238 var_success = true;
1239 }
1240 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1241 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1242 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1243 {
1244 ModuleSP exe_module_sp (exe_ctx->process->GetTarget().GetExecutableModule());
1245 if (exe_module_sp)
1246 {
1247 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1248 {
1249 format_file_spec.GetFilename() = exe_module_sp->GetFileSpec().GetFilename();
1250 var_success = format_file_spec;
1251 }
1252 else
1253 {
1254 format_file_spec = exe_module_sp->GetFileSpec();
1255 var_success = format_file_spec;
1256 }
1257 }
1258 }
1259 }
1260 }
1261 break;
1262
1263 case 't':
1264 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1265 {
1266 if (exe_ctx && exe_ctx->thread)
1267 {
1268 var_name_begin += ::strlen ("thread.");
1269 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1270 {
1271 s.Printf("0x%4.4x", exe_ctx->thread->GetID());
1272 var_success = true;
1273 }
1274 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1275 {
1276 s.Printf("%u", exe_ctx->thread->GetIndexID());
1277 var_success = true;
1278 }
1279 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1280 {
1281 cstr = exe_ctx->thread->GetName();
1282 var_success = cstr && cstr[0];
1283 if (var_success)
1284 s.PutCString(cstr);
1285 }
1286 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1287 {
1288 cstr = exe_ctx->thread->GetQueueName();
1289 var_success = cstr && cstr[0];
1290 if (var_success)
1291 s.PutCString(cstr);
1292 }
1293 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1294 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001295 StopInfoSP stop_info_sp = exe_ctx->thread->GetStopInfo ();
1296 if (stop_info_sp)
Greg Clayton1b654882010-09-19 02:33:57 +00001297 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001298 cstr = stop_info_sp->GetDescription();
Greg Clayton1b654882010-09-19 02:33:57 +00001299 if (cstr && cstr[0])
1300 {
1301 s.PutCString(cstr);
1302 var_success = true;
1303 }
1304 }
1305 }
1306 }
1307 }
1308 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1309 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001310 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1311 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001312 {
Greg Clayton1b654882010-09-19 02:33:57 +00001313 var_name_begin += ::strlen ("target.");
1314 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1315 {
1316 ArchSpec arch (target->GetArchitecture ());
1317 if (arch.IsValid())
1318 {
Greg Clayton64195a22011-02-23 00:35:02 +00001319 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001320 var_success = true;
1321 }
1322 }
1323 }
1324 }
1325 break;
1326
1327
1328 case 'm':
1329 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1330 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001331 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001332 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001333 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001334 var_name_begin += ::strlen ("module.");
1335
1336 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1337 {
1338 if (module->GetFileSpec())
1339 {
1340 var_name_begin += ::strlen ("file.");
1341
1342 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1343 {
1344 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1345 var_success = format_file_spec;
1346 }
1347 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1348 {
1349 format_file_spec = module->GetFileSpec();
1350 var_success = format_file_spec;
1351 }
1352 }
1353 }
1354 }
1355 }
1356 break;
1357
1358
1359 case 'f':
1360 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1361 {
1362 if (sc && sc->comp_unit != NULL)
1363 {
1364 var_name_begin += ::strlen ("file.");
1365
1366 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1367 {
1368 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1369 var_success = format_file_spec;
1370 }
1371 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1372 {
1373 format_file_spec = *sc->comp_unit;
1374 var_success = format_file_spec;
1375 }
1376 }
1377 }
1378 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1379 {
1380 if (exe_ctx && exe_ctx->frame)
1381 {
1382 var_name_begin += ::strlen ("frame.");
1383 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1384 {
1385 s.Printf("%u", exe_ctx->frame->GetFrameIndex());
1386 var_success = true;
1387 }
1388 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1389 {
1390 reg_kind = eRegisterKindGeneric;
1391 reg_num = LLDB_REGNUM_GENERIC_PC;
1392 var_success = true;
1393 }
1394 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1395 {
1396 reg_kind = eRegisterKindGeneric;
1397 reg_num = LLDB_REGNUM_GENERIC_SP;
1398 var_success = true;
1399 }
1400 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1401 {
1402 reg_kind = eRegisterKindGeneric;
1403 reg_num = LLDB_REGNUM_GENERIC_FP;
1404 var_success = true;
1405 }
1406 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
1407 {
1408 reg_kind = eRegisterKindGeneric;
1409 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
1410 var_success = true;
1411 }
1412 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
1413 {
Greg Clayton5ccbd292011-01-06 22:15:06 +00001414 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001415 if (reg_ctx)
1416 {
1417 var_name_begin += ::strlen ("reg.");
1418 if (var_name_begin < var_name_end)
1419 {
1420 std::string reg_name (var_name_begin, var_name_end);
1421 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
1422 if (reg_info)
1423 var_success = true;
1424 }
1425 }
1426 }
1427 }
1428 }
1429 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
1430 {
1431 if (sc && (sc->function != NULL || sc->symbol != NULL))
1432 {
1433 var_name_begin += ::strlen ("function.");
1434 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1435 {
1436 if (sc->function)
1437 s.Printf("function{0x%8.8x}", sc->function->GetID());
1438 else
1439 s.Printf("symbol[%u]", sc->symbol->GetID());
1440
1441 var_success = true;
1442 }
1443 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1444 {
1445 if (sc->function)
1446 cstr = sc->function->GetName().AsCString (NULL);
1447 else if (sc->symbol)
1448 cstr = sc->symbol->GetName().AsCString (NULL);
1449 if (cstr)
1450 {
1451 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00001452
1453 if (sc->block)
1454 {
1455 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1456 if (inline_block)
1457 {
1458 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
1459 if (inline_info)
1460 {
1461 s.PutCString(" [inlined] ");
1462 inline_info->GetName().Dump(&s);
1463 }
1464 }
1465 }
Greg Clayton1b654882010-09-19 02:33:57 +00001466 var_success = true;
1467 }
1468 }
1469 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
1470 {
1471 var_success = addr != NULL;
1472 if (var_success)
1473 {
1474 format_addr = *addr;
1475 calculate_format_addr_function_offset = true;
1476 }
1477 }
1478 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
1479 {
1480 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
1481 if (var_success)
1482 {
1483 format_addr = sc->line_entry.range.GetBaseAddress();
1484 calculate_format_addr_function_offset = true;
1485 }
1486 }
1487 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
1488 {
1489 var_success = exe_ctx->frame;
1490 if (var_success)
1491 {
1492 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1493 calculate_format_addr_function_offset = true;
1494 }
1495 }
1496 }
1497 }
1498 break;
1499
1500 case 'l':
1501 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
1502 {
1503 if (sc && sc->line_entry.IsValid())
1504 {
1505 var_name_begin += ::strlen ("line.");
1506 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1507 {
1508 var_name_begin += ::strlen ("file.");
1509
1510 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1511 {
1512 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
1513 var_success = format_file_spec;
1514 }
1515 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1516 {
1517 format_file_spec = sc->line_entry.file;
1518 var_success = format_file_spec;
1519 }
1520 }
1521 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
1522 {
1523 var_success = true;
1524 s.Printf("%u", sc->line_entry.line);
1525 }
1526 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
1527 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
1528 {
1529 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
1530 if (var_success)
1531 {
1532 format_addr = sc->line_entry.range.GetBaseAddress();
1533 if (var_name_begin[0] == 'e')
1534 format_addr.Slide (sc->line_entry.range.GetByteSize());
1535 }
1536 }
1537 }
1538 }
1539 break;
1540 }
1541
1542 if (var_success)
1543 {
1544 // If format addr is valid, then we need to print an address
1545 if (reg_num != LLDB_INVALID_REGNUM)
1546 {
1547 // We have a register value to display...
1548 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
1549 {
1550 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1551 }
1552 else
1553 {
1554 if (reg_ctx == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +00001555 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001556
1557 if (reg_ctx)
1558 {
1559 if (reg_kind != kNumRegisterKinds)
1560 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
1561 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
1562 var_success = reg_info != NULL;
1563 }
1564 }
1565 }
1566
1567 if (reg_info != NULL)
1568 {
Greg Clayton7349bd92011-05-09 20:18:18 +00001569 RegisterValue reg_value;
1570 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
1571 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001572 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00001573 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00001574 }
1575 }
1576
1577 if (format_file_spec)
1578 {
1579 s << format_file_spec;
1580 }
1581
1582 // If format addr is valid, then we need to print an address
1583 if (format_addr.IsValid())
1584 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001585 var_success = false;
1586
Greg Clayton1b654882010-09-19 02:33:57 +00001587 if (calculate_format_addr_function_offset)
1588 {
1589 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00001590
Greg Clayton0603aa92010-10-04 01:05:56 +00001591 if (sc)
1592 {
1593 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00001594 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001595 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00001596 if (sc->block)
1597 {
1598 // Check to make sure we aren't in an inline
1599 // function. If we are, use the inline block
1600 // range that contains "format_addr" since
1601 // blocks can be discontiguous.
1602 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1603 AddressRange inline_range;
1604 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
1605 func_addr = inline_range.GetBaseAddress();
1606 }
1607 }
Greg Clayton0603aa92010-10-04 01:05:56 +00001608 else if (sc->symbol && sc->symbol->GetAddressRangePtr())
1609 func_addr = sc->symbol->GetAddressRangePtr()->GetBaseAddress();
1610 }
1611
1612 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00001613 {
1614 if (func_addr.GetSection() == format_addr.GetSection())
1615 {
1616 addr_t func_file_addr = func_addr.GetFileAddress();
1617 addr_t addr_file_addr = format_addr.GetFileAddress();
1618 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001619 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00001620 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001621 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001622 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00001623 }
1624 else
Greg Clayton0603aa92010-10-04 01:05:56 +00001625 {
1626 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1627 if (target)
1628 {
1629 addr_t func_load_addr = func_addr.GetLoadAddress (target);
1630 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
1631 if (addr_load_addr > func_load_addr)
1632 s.Printf(" + %llu", addr_load_addr - func_load_addr);
1633 else if (addr_load_addr < func_load_addr)
1634 s.Printf(" - %llu", func_load_addr - addr_load_addr);
1635 var_success = true;
1636 }
1637 }
Greg Clayton1b654882010-09-19 02:33:57 +00001638 }
1639 }
1640 else
1641 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001642 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00001643 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00001644 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
1645 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00001646 if (vaddr == LLDB_INVALID_ADDRESS)
1647 vaddr = format_addr.GetFileAddress ();
1648
1649 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00001650 {
Greg Clayton514487e2011-02-15 21:59:32 +00001651 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00001652 if (addr_width == 0)
1653 addr_width = 16;
1654 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001655 var_success = true;
1656 }
Greg Clayton1b654882010-09-19 02:33:57 +00001657 }
1658 }
1659 }
1660
1661 if (var_success == false)
1662 success = false;
1663 }
1664 p = var_name_end;
1665 }
1666 else
1667 break;
1668 }
1669 else
1670 {
1671 // We got a dollar sign with no '{' after it, it must just be a dollar sign
1672 s.PutChar(*p);
1673 }
1674 }
1675 else if (*p == '\\')
1676 {
1677 ++p; // skip the slash
1678 switch (*p)
1679 {
1680 case 'a': s.PutChar ('\a'); break;
1681 case 'b': s.PutChar ('\b'); break;
1682 case 'f': s.PutChar ('\f'); break;
1683 case 'n': s.PutChar ('\n'); break;
1684 case 'r': s.PutChar ('\r'); break;
1685 case 't': s.PutChar ('\t'); break;
1686 case 'v': s.PutChar ('\v'); break;
1687 case '\'': s.PutChar ('\''); break;
1688 case '\\': s.PutChar ('\\'); break;
1689 case '0':
1690 // 1 to 3 octal chars
1691 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001692 // Make a string that can hold onto the initial zero char,
1693 // up to 3 octal digits, and a terminating NULL.
1694 char oct_str[5] = { 0, 0, 0, 0, 0 };
1695
1696 int i;
1697 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
1698 oct_str[i] = p[i];
1699
1700 // We don't want to consume the last octal character since
1701 // the main for loop will do this for us, so we advance p by
1702 // one less than i (even if i is zero)
1703 p += i - 1;
1704 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
1705 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001706 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001707 char octal_char = octal_value;
1708 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00001709 }
Greg Clayton1b654882010-09-19 02:33:57 +00001710 }
1711 break;
1712
1713 case 'x':
1714 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00001715 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001716 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001717 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00001718
Greg Clayton0603aa92010-10-04 01:05:56 +00001719 // Make a string that can hold onto two hex chars plus a
1720 // NULL terminator
1721 char hex_str[3] = { 0,0,0 };
1722 hex_str[0] = *p;
1723 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001724 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001725 ++p; // Skip the first of the two hex chars
1726 hex_str[1] = *p;
1727 }
1728
1729 unsigned long hex_value = strtoul (hex_str, NULL, 16);
1730 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001731 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00001732 }
1733 else
1734 {
1735 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00001736 }
1737 break;
1738
1739 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00001740 // Just desensitize any other character by just printing what
1741 // came after the '\'
1742 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00001743 break;
1744
1745 }
1746
1747 }
1748 }
1749 if (end)
1750 *end = p;
1751 return success;
1752}
1753
Greg Clayton4a33d312011-06-23 17:59:56 +00001754
1755static FormatManager&
1756GetFormatManager() {
1757 static FormatManager g_format_manager;
1758 return g_format_manager;
1759}
1760
Enrico Granataa37a0652011-07-24 00:14:56 +00001761void
1762Debugger::Formatting::ForceUpdate()
1763{
1764 GetFormatManager().Changed();
1765}
1766
Greg Clayton4a33d312011-06-23 17:59:56 +00001767bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001768Debugger::Formatting::ValueFormats::Get(ValueObject& vobj, ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001769{
Enrico Granata4becb372011-06-29 22:27:15 +00001770 return GetFormatManager().Value().Get(vobj,entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001771}
1772
1773void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001774Debugger::Formatting::ValueFormats::Add(const ConstString &type, const ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001775{
Enrico Granata4becb372011-06-29 22:27:15 +00001776 GetFormatManager().Value().Add(type.AsCString(),entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001777}
1778
1779bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001780Debugger::Formatting::ValueFormats::Delete(const ConstString &type)
Greg Clayton4a33d312011-06-23 17:59:56 +00001781{
Enrico Granata4becb372011-06-29 22:27:15 +00001782 return GetFormatManager().Value().Delete(type.AsCString());
Greg Clayton4a33d312011-06-23 17:59:56 +00001783}
1784
1785void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001786Debugger::Formatting::ValueFormats::Clear()
Greg Clayton4a33d312011-06-23 17:59:56 +00001787{
Enrico Granata4becb372011-06-29 22:27:15 +00001788 GetFormatManager().Value().Clear();
1789}
1790
1791void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001792Debugger::Formatting::ValueFormats::LoopThrough(ValueFormat::ValueCallback callback, void* callback_baton)
Enrico Granata4becb372011-06-29 22:27:15 +00001793{
1794 GetFormatManager().Value().LoopThrough(callback, callback_baton);
1795}
1796
1797uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001798Debugger::Formatting::ValueFormats::GetCurrentRevision()
Enrico Granata4becb372011-06-29 22:27:15 +00001799{
1800 return GetFormatManager().GetCurrentRevision();
1801}
1802
Enrico Granata0a3958e2011-07-02 00:25:22 +00001803uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001804Debugger::Formatting::ValueFormats::GetCount()
Enrico Granata0a3958e2011-07-02 00:25:22 +00001805{
1806 return GetFormatManager().Value().GetCount();
1807}
Enrico Granata4becb372011-06-29 22:27:15 +00001808
Enrico Granata4becb372011-06-29 22:27:15 +00001809bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001810Debugger::Formatting::GetSummaryFormat(ValueObject& vobj,
1811 lldb::SummaryFormatSP& entry)
Enrico Granata4becb372011-06-29 22:27:15 +00001812{
Enrico Granata1490c6f2011-07-19 02:34:21 +00001813 return GetFormatManager().Get(vobj, entry);
1814}
Enrico Granatad55546b2011-07-22 00:16:08 +00001815bool
1816Debugger::Formatting::GetSyntheticFilter(ValueObject& vobj,
Enrico Granataa37a0652011-07-24 00:14:56 +00001817 lldb::SyntheticChildrenSP& entry)
Enrico Granatad55546b2011-07-22 00:16:08 +00001818{
1819 return GetFormatManager().Get(vobj, entry);
1820}
Enrico Granata1490c6f2011-07-19 02:34:21 +00001821
1822bool
1823Debugger::Formatting::Categories::Get(const ConstString &category, lldb::FormatCategorySP &entry)
1824{
1825 entry = GetFormatManager().Category(category.GetCString());
1826 return true;
Enrico Granata4becb372011-06-29 22:27:15 +00001827}
1828
1829void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001830Debugger::Formatting::Categories::Add(const ConstString &category)
Enrico Granata4becb372011-06-29 22:27:15 +00001831{
Enrico Granata1490c6f2011-07-19 02:34:21 +00001832 GetFormatManager().Category(category.GetCString());
1833}
1834
1835bool
1836Debugger::Formatting::Categories::Delete(const ConstString &category)
1837{
1838 GetFormatManager().DisableCategory(category.GetCString());
1839 return GetFormatManager().Categories().Delete(category.GetCString());
Enrico Granata4becb372011-06-29 22:27:15 +00001840}
1841
1842void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001843Debugger::Formatting::Categories::Clear()
Enrico Granata4becb372011-06-29 22:27:15 +00001844{
Enrico Granata1490c6f2011-07-19 02:34:21 +00001845 GetFormatManager().Categories().Clear();
1846}
1847
1848void
1849Debugger::Formatting::Categories::Clear(ConstString &category)
1850{
Enrico Granatad55546b2011-07-22 00:16:08 +00001851 GetFormatManager().Category(category.GetCString())->ClearSummaries();
Enrico Granata1490c6f2011-07-19 02:34:21 +00001852}
1853
1854void
1855Debugger::Formatting::Categories::Enable(ConstString& category)
1856{
1857 if (GetFormatManager().Category(category.GetCString())->IsEnabled() == false)
Enrico Granata1490c6f2011-07-19 02:34:21 +00001858 GetFormatManager().EnableCategory(category.GetCString());
Enrico Granata1490c6f2011-07-19 02:34:21 +00001859 else
1860 {
Enrico Granata1490c6f2011-07-19 02:34:21 +00001861 GetFormatManager().DisableCategory(category.GetCString());
Enrico Granata1490c6f2011-07-19 02:34:21 +00001862 GetFormatManager().EnableCategory(category.GetCString());
1863 }
1864}
1865
1866void
1867Debugger::Formatting::Categories::Disable(ConstString& category)
1868{
1869 if (GetFormatManager().Category(category.GetCString())->IsEnabled() == true)
Enrico Granata1490c6f2011-07-19 02:34:21 +00001870 GetFormatManager().DisableCategory(category.GetCString());
Enrico Granata1490c6f2011-07-19 02:34:21 +00001871}
1872
1873void
1874Debugger::Formatting::Categories::LoopThrough(FormatManager::CategoryCallback callback, void* callback_baton)
1875{
1876 GetFormatManager().LoopThroughCategories(callback, callback_baton);
Enrico Granata4becb372011-06-29 22:27:15 +00001877}
1878
1879uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001880Debugger::Formatting::Categories::GetCurrentRevision()
Enrico Granata4becb372011-06-29 22:27:15 +00001881{
1882 return GetFormatManager().GetCurrentRevision();
Greg Clayton4a33d312011-06-23 17:59:56 +00001883}
1884
Enrico Granata0a3958e2011-07-02 00:25:22 +00001885uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001886Debugger::Formatting::Categories::GetCount()
Enrico Granata0a3958e2011-07-02 00:25:22 +00001887{
Enrico Granata1490c6f2011-07-19 02:34:21 +00001888 return GetFormatManager().Categories().GetCount();
Enrico Granata0a3958e2011-07-02 00:25:22 +00001889}
1890
1891bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001892Debugger::Formatting::NamedSummaryFormats::Get(const ConstString &type, SummaryFormat::SharedPointer &entry)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001893{
1894 return GetFormatManager().NamedSummary().Get(type.AsCString(),entry);
1895}
1896
1897void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001898Debugger::Formatting::NamedSummaryFormats::Add(const ConstString &type, const SummaryFormat::SharedPointer &entry)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001899{
1900 GetFormatManager().NamedSummary().Add(type.AsCString(),entry);
1901}
1902
1903bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001904Debugger::Formatting::NamedSummaryFormats::Delete(const ConstString &type)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001905{
1906 return GetFormatManager().NamedSummary().Delete(type.AsCString());
1907}
1908
1909void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001910Debugger::Formatting::NamedSummaryFormats::Clear()
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001911{
1912 GetFormatManager().NamedSummary().Clear();
1913}
1914
1915void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001916Debugger::Formatting::NamedSummaryFormats::LoopThrough(SummaryFormat::SummaryCallback callback, void* callback_baton)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001917{
1918 GetFormatManager().NamedSummary().LoopThrough(callback, callback_baton);
1919}
1920
1921uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001922Debugger::Formatting::NamedSummaryFormats::GetCurrentRevision()
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001923{
1924 return GetFormatManager().GetCurrentRevision();
1925}
1926
1927uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001928Debugger::Formatting::NamedSummaryFormats::GetCount()
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001929{
1930 return GetFormatManager().NamedSummary().GetCount();
1931}
1932
Greg Clayton1b654882010-09-19 02:33:57 +00001933#pragma mark Debugger::SettingsController
1934
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001935//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00001936// class Debugger::SettingsController
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001937//--------------------------------------------------
1938
Greg Clayton1b654882010-09-19 02:33:57 +00001939Debugger::SettingsController::SettingsController () :
Caroline Tice101c7c22010-09-09 06:25:08 +00001940 UserSettingsController ("", lldb::UserSettingsControllerSP())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001941{
Caroline Tice91123da2010-09-08 17:48:55 +00001942 m_default_settings.reset (new DebuggerInstanceSettings (*this, false,
1943 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001944}
1945
Greg Clayton1b654882010-09-19 02:33:57 +00001946Debugger::SettingsController::~SettingsController ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001947{
1948}
1949
1950
1951lldb::InstanceSettingsSP
Greg Clayton1b654882010-09-19 02:33:57 +00001952Debugger::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001953{
Greg Claytondbe54502010-11-19 03:46:01 +00001954 DebuggerInstanceSettings *new_settings = new DebuggerInstanceSettings (*GetSettingsController(),
Caroline Tice91123da2010-09-08 17:48:55 +00001955 false, instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001956 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1957 return new_settings_sp;
1958}
1959
Greg Clayton1b654882010-09-19 02:33:57 +00001960#pragma mark DebuggerInstanceSettings
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001961//--------------------------------------------------
1962// class DebuggerInstanceSettings
1963//--------------------------------------------------
1964
Greg Claytona7015092010-09-18 01:14:36 +00001965DebuggerInstanceSettings::DebuggerInstanceSettings
1966(
1967 UserSettingsController &owner,
1968 bool live_instance,
1969 const char *name
1970) :
Greg Clayton85851dd2010-12-04 00:10:17 +00001971 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytona7015092010-09-18 01:14:36 +00001972 m_term_width (80),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001973 m_prompt (),
Greg Clayton0603aa92010-10-04 01:05:56 +00001974 m_frame_format (),
1975 m_thread_format (),
Caroline Ticedaccaa92010-09-20 20:44:43 +00001976 m_script_lang (),
Jim Ingham3bcdb292010-10-04 22:44:14 +00001977 m_use_external_editor (false),
1978 m_auto_confirm_on (false)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001979{
Caroline Ticef20e8232010-09-09 18:26:37 +00001980 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1981 // until the vtables for DebuggerInstanceSettings are properly set up, i.e. AFTER all the initializers.
1982 // 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 +00001983 // The same is true of CreateInstanceName().
1984
1985 if (GetInstanceName() == InstanceSettings::InvalidName())
1986 {
1987 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1988 m_owner.RegisterInstanceSettings (this);
1989 }
Caroline Ticef20e8232010-09-09 18:26:37 +00001990
1991 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001992 {
1993 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1994 CopyInstanceSettings (pending_settings, false);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001995 }
1996}
1997
1998DebuggerInstanceSettings::DebuggerInstanceSettings (const DebuggerInstanceSettings &rhs) :
Greg Clayton99d0faf2010-11-18 23:32:35 +00001999 InstanceSettings (*Debugger::GetSettingsController(), CreateInstanceName ().AsCString()),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002000 m_prompt (rhs.m_prompt),
Greg Clayton0603aa92010-10-04 01:05:56 +00002001 m_frame_format (rhs.m_frame_format),
2002 m_thread_format (rhs.m_thread_format),
Caroline Ticedaccaa92010-09-20 20:44:43 +00002003 m_script_lang (rhs.m_script_lang),
Jim Ingham3bcdb292010-10-04 22:44:14 +00002004 m_use_external_editor (rhs.m_use_external_editor),
2005 m_auto_confirm_on(rhs.m_auto_confirm_on)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002006{
2007 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2008 CopyInstanceSettings (pending_settings, false);
2009 m_owner.RemovePendingSettings (m_instance_name);
2010}
2011
2012DebuggerInstanceSettings::~DebuggerInstanceSettings ()
2013{
2014}
2015
2016DebuggerInstanceSettings&
2017DebuggerInstanceSettings::operator= (const DebuggerInstanceSettings &rhs)
2018{
2019 if (this != &rhs)
2020 {
Greg Clayton1b654882010-09-19 02:33:57 +00002021 m_term_width = rhs.m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002022 m_prompt = rhs.m_prompt;
Greg Clayton0603aa92010-10-04 01:05:56 +00002023 m_frame_format = rhs.m_frame_format;
2024 m_thread_format = rhs.m_thread_format;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002025 m_script_lang = rhs.m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002026 m_use_external_editor = rhs.m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002027 m_auto_confirm_on = rhs.m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002028 }
2029
2030 return *this;
2031}
2032
Greg Clayton1b654882010-09-19 02:33:57 +00002033bool
2034DebuggerInstanceSettings::ValidTermWidthValue (const char *value, Error err)
2035{
2036 bool valid = false;
2037
2038 // Verify we have a value string.
2039 if (value == NULL || value[0] == '\0')
2040 {
2041 err.SetErrorString ("Missing value. Can't set terminal width without a value.\n");
2042 }
2043 else
2044 {
2045 char *end = NULL;
2046 const uint32_t width = ::strtoul (value, &end, 0);
2047
Johnny Chenea9fc182010-09-20 16:36:43 +00002048 if (end && end[0] == '\0')
Greg Clayton1b654882010-09-19 02:33:57 +00002049 {
Johnny Chen433d7742010-09-20 17:04:41 +00002050 if (width >= 10 && width <= 1024)
Greg Clayton1b654882010-09-19 02:33:57 +00002051 valid = true;
2052 else
2053 err.SetErrorString ("Invalid term-width value; value must be between 10 and 1024.\n");
2054 }
2055 else
2056 err.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string.\n", value);
2057 }
2058
2059 return valid;
2060}
2061
2062
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002063void
2064DebuggerInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2065 const char *index_value,
2066 const char *value,
2067 const ConstString &instance_name,
2068 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:54 +00002069 VarSetOperationType op,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002070 Error &err,
2071 bool pending)
2072{
Greg Clayton0603aa92010-10-04 01:05:56 +00002073
2074 if (var_name == TermWidthVarName())
2075 {
2076 if (ValidTermWidthValue (value, err))
2077 {
2078 m_term_width = ::strtoul (value, NULL, 0);
2079 }
2080 }
2081 else if (var_name == PromptVarName())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002082 {
Caroline Tice101c7c22010-09-09 06:25:08 +00002083 UserSettingsController::UpdateStringVariable (op, m_prompt, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002084 if (!pending)
2085 {
Caroline Tice49e27372010-09-07 18:35:40 +00002086 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2087 // strip off the brackets before passing it to BroadcastPromptChange.
2088
2089 std::string tmp_instance_name (instance_name.AsCString());
2090 if ((tmp_instance_name[0] == '[')
2091 && (tmp_instance_name[instance_name.GetLength() - 1] == ']'))
2092 tmp_instance_name = tmp_instance_name.substr (1, instance_name.GetLength() - 2);
2093 ConstString new_name (tmp_instance_name.c_str());
2094
2095 BroadcastPromptChange (new_name, m_prompt.c_str());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002096 }
2097 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002098 else if (var_name == GetFrameFormatName())
2099 {
2100 UserSettingsController::UpdateStringVariable (op, m_frame_format, value, err);
2101 }
2102 else if (var_name == GetThreadFormatName())
2103 {
2104 UserSettingsController::UpdateStringVariable (op, m_thread_format, value, err);
2105 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002106 else if (var_name == ScriptLangVarName())
2107 {
2108 bool success;
2109 m_script_lang = Args::StringToScriptLanguage (value, eScriptLanguageDefault,
2110 &success);
2111 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002112 else if (var_name == UseExternalEditorVarName ())
2113 {
Greg Clayton385aa282011-04-22 03:55:06 +00002114 UserSettingsController::UpdateBooleanVariable (op, m_use_external_editor, value, false, err);
Caroline Ticedaccaa92010-09-20 20:44:43 +00002115 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002116 else if (var_name == AutoConfirmName ())
2117 {
Greg Clayton385aa282011-04-22 03:55:06 +00002118 UserSettingsController::UpdateBooleanVariable (op, m_auto_confirm_on, value, false, err);
Jim Ingham3bcdb292010-10-04 22:44:14 +00002119 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002120}
2121
Caroline Tice12cecd72010-09-20 21:37:42 +00002122bool
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002123DebuggerInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2124 const ConstString &var_name,
Caroline Ticedaccaa92010-09-20 20:44:43 +00002125 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42 +00002126 Error *err)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002127{
2128 if (var_name == PromptVarName())
2129 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002130 value.AppendString (m_prompt.c_str(), m_prompt.size());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002131
2132 }
2133 else if (var_name == ScriptLangVarName())
2134 {
2135 value.AppendString (ScriptInterpreter::LanguageToString (m_script_lang).c_str());
2136 }
Caroline Tice101c7c22010-09-09 06:25:08 +00002137 else if (var_name == TermWidthVarName())
2138 {
2139 StreamString width_str;
2140 width_str.Printf ("%d", m_term_width);
2141 value.AppendString (width_str.GetData());
2142 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002143 else if (var_name == GetFrameFormatName ())
2144 {
2145 value.AppendString(m_frame_format.c_str(), m_frame_format.size());
2146 }
2147 else if (var_name == GetThreadFormatName ())
2148 {
2149 value.AppendString(m_thread_format.c_str(), m_thread_format.size());
2150 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002151 else if (var_name == UseExternalEditorVarName())
2152 {
2153 if (m_use_external_editor)
2154 value.AppendString ("true");
2155 else
2156 value.AppendString ("false");
2157 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002158 else if (var_name == AutoConfirmName())
2159 {
2160 if (m_auto_confirm_on)
2161 value.AppendString ("true");
2162 else
2163 value.AppendString ("false");
2164 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002165 else
Caroline Tice12cecd72010-09-20 21:37:42 +00002166 {
2167 if (err)
2168 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2169 return false;
2170 }
2171 return true;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002172}
2173
2174void
2175DebuggerInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
2176 bool pending)
2177{
2178 if (new_settings.get() == NULL)
2179 return;
2180
2181 DebuggerInstanceSettings *new_debugger_settings = (DebuggerInstanceSettings *) new_settings.get();
2182
2183 m_prompt = new_debugger_settings->m_prompt;
2184 if (!pending)
Caroline Tice49e27372010-09-07 18:35:40 +00002185 {
2186 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2187 // strip off the brackets before passing it to BroadcastPromptChange.
2188
2189 std::string tmp_instance_name (m_instance_name.AsCString());
2190 if ((tmp_instance_name[0] == '[')
2191 && (tmp_instance_name[m_instance_name.GetLength() - 1] == ']'))
2192 tmp_instance_name = tmp_instance_name.substr (1, m_instance_name.GetLength() - 2);
2193 ConstString new_name (tmp_instance_name.c_str());
2194
2195 BroadcastPromptChange (new_name, m_prompt.c_str());
2196 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002197 m_frame_format = new_debugger_settings->m_frame_format;
2198 m_thread_format = new_debugger_settings->m_thread_format;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002199 m_term_width = new_debugger_settings->m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002200 m_script_lang = new_debugger_settings->m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002201 m_use_external_editor = new_debugger_settings->m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002202 m_auto_confirm_on = new_debugger_settings->m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002203}
2204
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002205
2206bool
2207DebuggerInstanceSettings::BroadcastPromptChange (const ConstString &instance_name, const char *new_prompt)
2208{
2209 std::string tmp_prompt;
2210
2211 if (new_prompt != NULL)
2212 {
2213 tmp_prompt = new_prompt ;
2214 int len = tmp_prompt.size();
2215 if (len > 1
2216 && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"')
2217 && (tmp_prompt[len-1] == tmp_prompt[0]))
2218 {
2219 tmp_prompt = tmp_prompt.substr(1,len-2);
2220 }
2221 len = tmp_prompt.size();
2222 if (tmp_prompt[len-1] != ' ')
2223 tmp_prompt.append(" ");
2224 }
2225 EventSP new_event_sp;
2226 new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt,
2227 new EventDataBytes (tmp_prompt.c_str())));
2228
2229 if (instance_name.GetLength() != 0)
2230 {
2231 // Set prompt for a particular instance.
2232 Debugger *dbg = Debugger::FindDebuggerWithInstanceName (instance_name).get();
2233 if (dbg != NULL)
2234 {
2235 dbg->GetCommandInterpreter().BroadcastEvent (new_event_sp);
2236 }
2237 }
2238
2239 return true;
2240}
2241
2242const ConstString
2243DebuggerInstanceSettings::CreateInstanceName ()
2244{
2245 static int instance_count = 1;
2246 StreamString sstr;
2247
2248 sstr.Printf ("debugger_%d", instance_count);
2249 ++instance_count;
2250
2251 const ConstString ret_val (sstr.GetData());
2252
2253 return ret_val;
2254}
2255
2256const ConstString &
2257DebuggerInstanceSettings::PromptVarName ()
2258{
2259 static ConstString prompt_var_name ("prompt");
2260
2261 return prompt_var_name;
2262}
2263
2264const ConstString &
Greg Clayton0603aa92010-10-04 01:05:56 +00002265DebuggerInstanceSettings::GetFrameFormatName ()
2266{
2267 static ConstString prompt_var_name ("frame-format");
2268
2269 return prompt_var_name;
2270}
2271
2272const ConstString &
2273DebuggerInstanceSettings::GetThreadFormatName ()
2274{
2275 static ConstString prompt_var_name ("thread-format");
2276
2277 return prompt_var_name;
2278}
2279
2280const ConstString &
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002281DebuggerInstanceSettings::ScriptLangVarName ()
2282{
2283 static ConstString script_lang_var_name ("script-lang");
2284
2285 return script_lang_var_name;
2286}
2287
Caroline Tice101c7c22010-09-09 06:25:08 +00002288const ConstString &
2289DebuggerInstanceSettings::TermWidthVarName ()
2290{
2291 static ConstString term_width_var_name ("term-width");
2292
2293 return term_width_var_name;
2294}
2295
Caroline Ticedaccaa92010-09-20 20:44:43 +00002296const ConstString &
2297DebuggerInstanceSettings::UseExternalEditorVarName ()
2298{
2299 static ConstString use_external_editor_var_name ("use-external-editor");
2300
2301 return use_external_editor_var_name;
2302}
2303
Jim Ingham3bcdb292010-10-04 22:44:14 +00002304const ConstString &
2305DebuggerInstanceSettings::AutoConfirmName ()
2306{
2307 static ConstString use_external_editor_var_name ("auto-confirm");
2308
2309 return use_external_editor_var_name;
2310}
2311
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002312//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00002313// SettingsController Variable Tables
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002314//--------------------------------------------------
2315
2316
2317SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002318Debugger::SettingsController::global_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002319{
2320 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
Caroline Tice101c7c22010-09-09 06:25:08 +00002321 // The Debugger level global table should always be empty; all Debugger settable variables should be instance
2322 // variables.
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002323 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2324};
2325
Greg Claytonbb562b12010-10-04 02:44:26 +00002326#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name}${function.pc-offset}}}"
Greg Clayton0603aa92010-10-04 01:05:56 +00002327#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002328
Greg Clayton0603aa92010-10-04 01:05:56 +00002329#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2330 "{, ${frame.pc}}"\
2331 MODULE_WITH_FUNC\
Greg Claytoncf4b9072010-10-04 17:04:23 +00002332 FILE_AND_LINE\
Greg Clayton0603aa92010-10-04 01:05:56 +00002333 "{, stop reason = ${thread.stop-reason}}"\
Greg Clayton0603aa92010-10-04 01:05:56 +00002334 "\\n"
2335
Greg Clayton315d2ca2010-11-02 01:53:21 +00002336//#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2337// "{, ${frame.pc}}"\
2338// MODULE_WITH_FUNC\
2339// FILE_AND_LINE\
2340// "{, stop reason = ${thread.stop-reason}}"\
2341// "{, name = ${thread.name}}"\
2342// "{, queue = ${thread.queue}}"\
2343// "\\n"
2344
Greg Clayton0603aa92010-10-04 01:05:56 +00002345#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
2346 MODULE_WITH_FUNC\
2347 FILE_AND_LINE\
2348 "\\n"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002349
2350SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002351Debugger::SettingsController::instance_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002352{
Greg Clayton0603aa92010-10-04 01:05:56 +00002353// NAME Setting variable type Default Enum Init'd Hidden Help
2354// ======================= ======================= ====================== ==== ====== ====== ======================
Greg Clayton0603aa92010-10-04 01:05:56 +00002355{ "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 +00002356{ "prompt", eSetVarTypeString, "(lldb) ", NULL, false, false, "The debugger command line prompt displayed for the user." },
Jim Ingham3bcdb292010-10-04 22:44:14 +00002357{ "script-lang", eSetVarTypeString, "python", NULL, false, false, "The script language to be used for evaluating user-written scripts." },
2358{ "term-width", eSetVarTypeInt, "80" , NULL, false, false, "The maximum number of columns to use for displaying text." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002359{ "thread-format", eSetVarTypeString, DEFAULT_THREAD_FORMAT, NULL, false, false, "The default thread format string to use when displaying thread information." },
Jim Ingham06e827c2010-11-11 19:26:09 +00002360{ "use-external-editor", eSetVarTypeBoolean, "false", NULL, false, false, "Whether to use an external editor or not." },
2361{ "auto-confirm", eSetVarTypeBoolean, "false", NULL, false, false, "If true all confirmation prompts will receive their default reply." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002362{ NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002363};