blob: 219fbfa8985489720cff356a2eb40ff9390953c7 [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 Granataf4efecd2011-07-12 22:56:10 +0000697// FIXME this should eventually be replaced by proper use of LLDB logging facilities
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000698//#define VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata9fc19442011-07-06 02:13:41 +0000699#ifdef VERBOSE_FORMATPROMPT_OUTPUT
700#define IFERROR_PRINT_IT if (error.Fail()) \
701{ \
702 printf("ERROR: %s\n",error.AsCString("unknown")); \
703 break; \
704}
705#else // IFERROR_PRINT_IT
706#define IFERROR_PRINT_IT if (error.Fail()) \
707break;
708#endif // IFERROR_PRINT_IT
709
710static bool
711ScanFormatDescriptor(const char* var_name_begin,
712 const char* var_name_end,
713 const char** var_name_final,
714 const char** percent_position,
715 lldb::Format* custom_format,
716 ValueObject::ValueObjectRepresentationStyle* val_obj_display)
717{
718 *percent_position = ::strchr(var_name_begin,'%');
Greg Clayton34132752011-07-06 04:07:21 +0000719 if (!*percent_position || *percent_position > var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +0000720 *var_name_final = var_name_end;
721 else
722 {
723 *var_name_final = *percent_position;
724 char* format_name = new char[var_name_end-*var_name_final]; format_name[var_name_end-*var_name_final-1] = '\0';
725 memcpy(format_name, *var_name_final+1, var_name_end-*var_name_final-1);
726 if ( !FormatManager::GetFormatFromCString(format_name,
727 true,
728 *custom_format) )
729 {
730 // if this is an @ sign, print ObjC description
Greg Clayton34132752011-07-06 04:07:21 +0000731 if (*format_name == '@')
Enrico Granata9fc19442011-07-06 02:13:41 +0000732 *val_obj_display = ValueObject::eDisplayLanguageSpecific;
733 // if this is a V, print the value using the default format
Greg Clayton34132752011-07-06 04:07:21 +0000734 if (*format_name == 'V')
Enrico Granata9fc19442011-07-06 02:13:41 +0000735 *val_obj_display = ValueObject::eDisplayValue;
Enrico Granatad55546b2011-07-22 00:16:08 +0000736 // if this is an L, print the location of the value
Enrico Granataf2bbf712011-07-15 02:26:42 +0000737 if (*format_name == 'L')
738 *val_obj_display = ValueObject::eDisplayLocation;
Enrico Granatad55546b2011-07-22 00:16:08 +0000739 // if this is an S, print the summary after all
740 if (*format_name == 'S')
741 *val_obj_display = ValueObject::eDisplaySummary;
Enrico Granata9fc19442011-07-06 02:13:41 +0000742 }
743 // a good custom format tells us to print the value using it
744 else
745 *val_obj_display = ValueObject::eDisplayValue;
746 delete format_name;
747 }
748 return true;
749}
750
751static bool
752ScanBracketedRange(const char* var_name_begin,
753 const char* var_name_end,
754 const char* var_name_final,
755 const char** open_bracket_position,
756 const char** separator_position,
757 const char** close_bracket_position,
758 const char** var_name_final_if_array_range,
759 int64_t* index_lower,
760 int64_t* index_higher)
761{
762 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +0000763 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +0000764 {
765 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
766 *close_bracket_position = ::strchr(*open_bracket_position,']');
767 // as usual, we assume that [] will come before %
768 //printf("trying to expand a []\n");
769 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +0000770 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +0000771 {
772 *index_lower = 0;
773 }
774 else if (*separator_position == NULL || *separator_position > var_name_end)
775 {
776 char *end = NULL;
777 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
778 *index_higher = *index_lower;
779 //printf("got to read low=%d high same\n",bitfield_lower);
780 }
Greg Clayton34132752011-07-06 04:07:21 +0000781 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +0000782 {
783 char *end = NULL;
784 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
785 *index_higher = ::strtoul (*separator_position+1, &end, 0);
786 //printf("got to read low=%d high=%d\n",bitfield_lower,bitfield_higher);
787 }
788 else
789 return false;
790 if (*index_lower > *index_higher && *index_higher > 0)
791 {
792 int temp = *index_lower;
793 *index_lower = *index_higher;
794 *index_higher = temp;
795 }
796 }
797 return true;
798}
799
800
801static ValueObjectSP
802ExpandExpressionPath(ValueObject* vobj,
803 StackFrame* frame,
804 bool* do_deref_pointer,
805 const char* var_name_begin,
806 const char* var_name_final,
807 Error& error)
808{
809
810 StreamString sstring;
811 VariableSP var_sp;
812
Greg Clayton34132752011-07-06 04:07:21 +0000813 if (*do_deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +0000814 sstring.PutChar('*');
Greg Clayton34132752011-07-06 04:07:21 +0000815 else if (vobj->IsDereferenceOfParent() && ClangASTContext::IsPointerType(vobj->GetParent()->GetClangType()) && !vobj->IsArrayItemForPointer())
Enrico Granata9fc19442011-07-06 02:13:41 +0000816 {
817 sstring.PutChar('*');
818 *do_deref_pointer = true;
819 }
820
821 vobj->GetExpressionPath(sstring, true, ValueObject::eHonorPointers);
822#ifdef VERBOSE_FORMATPROMPT_OUTPUT
823 printf("name to expand in phase 0: %s\n",sstring.GetData());
824#endif //VERBOSE_FORMATPROMPT_OUTPUT
825 sstring.PutRawBytes(var_name_begin+3, var_name_final-var_name_begin-3);
826#ifdef VERBOSE_FORMATPROMPT_OUTPUT
827 printf("name to expand in phase 1: %s\n",sstring.GetData());
828#endif //VERBOSE_FORMATPROMPT_OUTPUT
829 std::string name = std::string(sstring.GetData());
830 ValueObjectSP target = frame->GetValueForVariableExpressionPath (name.c_str(),
831 eNoDynamicValues,
832 0,
833 var_sp,
834 error);
835 return target;
836}
837
838static ValueObjectSP
839ExpandIndexedExpression(ValueObject* vobj,
840 uint32_t index,
841 StackFrame* frame,
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000842 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +0000843{
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000844 const char* ptr_deref_format = "[%d]";
845 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
846 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
847#ifdef VERBOSE_FORMATPROMPT_OUTPUT
848 printf("name to deref: %s\n",ptr_deref_buffer.get());
849#endif //VERBOSE_FORMATPROMPT_OUTPUT
850 const char* first_unparsed;
851 ValueObject::GetValueForExpressionPathOptions options;
852 ValueObject::ExpressionPathEndResultType final_value_type;
853 ValueObject::ExpressionPathScanEndReason reason_to_stop;
854 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eDereference : ValueObject::eNothing);
855 ValueObjectSP item = vobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
856 &first_unparsed,
857 &reason_to_stop,
858 &final_value_type,
859 options,
860 &what_next);
861 if (!item)
862 {
863#ifdef VERBOSE_FORMATPROMPT_OUTPUT
864 printf("ERROR: unparsed portion = %s, why stopping = %d,"
865 " final_value_type %d\n",
866 first_unparsed, reason_to_stop, final_value_type);
867#endif //VERBOSE_FORMATPROMPT_OUTPUT
868 }
869#ifdef VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata9fc19442011-07-06 02:13:41 +0000870 else
871 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000872 printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
873 " final_value_type %d\n",
874 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +0000875 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000876#endif //VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata9fc19442011-07-06 02:13:41 +0000877 return item;
878}
879
Greg Clayton1b654882010-09-19 02:33:57 +0000880bool
881Debugger::FormatPrompt
882(
883 const char *format,
884 const SymbolContext *sc,
885 const ExecutionContext *exe_ctx,
886 const Address *addr,
887 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +0000888 const char **end,
889 ValueObject* vobj
Greg Clayton1b654882010-09-19 02:33:57 +0000890)
891{
Enrico Granata4becb372011-06-29 22:27:15 +0000892 ValueObject* realvobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +0000893 bool success = true;
894 const char *p;
895 for (p = format; *p != '\0'; ++p)
896 {
Greg Clayton34132752011-07-06 04:07:21 +0000897 if (realvobj)
Enrico Granata4becb372011-06-29 22:27:15 +0000898 {
899 vobj = realvobj;
900 realvobj = NULL;
901 }
Greg Clayton1b654882010-09-19 02:33:57 +0000902 size_t non_special_chars = ::strcspn (p, "${}\\");
903 if (non_special_chars > 0)
904 {
905 if (success)
906 s.Write (p, non_special_chars);
907 p += non_special_chars;
908 }
909
910 if (*p == '\0')
911 {
912 break;
913 }
914 else if (*p == '{')
915 {
916 // Start a new scope that must have everything it needs if it is to
917 // to make it into the final output stream "s". If you want to make
918 // a format that only prints out the function or symbol name if there
919 // is one in the symbol context you can use:
920 // "{function =${function.name}}"
921 // The first '{' starts a new scope that end with the matching '}' at
922 // the end of the string. The contents "function =${function.name}"
923 // will then be evaluated and only be output if there is a function
924 // or symbol with a valid name.
925 StreamString sub_strm;
926
927 ++p; // Skip the '{'
928
Enrico Granata4becb372011-06-29 22:27:15 +0000929 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, vobj))
Greg Clayton1b654882010-09-19 02:33:57 +0000930 {
931 // The stream had all it needed
932 s.Write(sub_strm.GetData(), sub_strm.GetSize());
933 }
934 if (*p != '}')
935 {
936 success = false;
937 break;
938 }
939 }
940 else if (*p == '}')
941 {
942 // End of a enclosing scope
943 break;
944 }
945 else if (*p == '$')
946 {
947 // We have a prompt variable to print
948 ++p;
949 if (*p == '{')
950 {
951 ++p;
952 const char *var_name_begin = p;
953 const char *var_name_end = ::strchr (p, '}');
954
955 if (var_name_end && var_name_begin < var_name_end)
956 {
957 // if we have already failed to parse, skip this variable
958 if (success)
959 {
960 const char *cstr = NULL;
961 Address format_addr;
962 bool calculate_format_addr_function_offset = false;
963 // Set reg_kind and reg_num to invalid values
964 RegisterKind reg_kind = kNumRegisterKinds;
965 uint32_t reg_num = LLDB_INVALID_REGNUM;
966 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +0000967 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +0000968 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +0000969 bool do_deref_pointer = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000970 ValueObject::ExpressionPathScanEndReason reason_to_stop;
971 ValueObject::ExpressionPathEndResultType final_value_type;
972
Greg Clayton1b654882010-09-19 02:33:57 +0000973 // Each variable must set success to true below...
974 bool var_success = false;
975 switch (var_name_begin[0])
976 {
Enrico Granata4becb372011-06-29 22:27:15 +0000977 case '*':
978 {
Greg Clayton34132752011-07-06 04:07:21 +0000979 if (!vobj)
980 break;
Enrico Granata9fc19442011-07-06 02:13:41 +0000981 do_deref_pointer = true;
982 var_name_begin++;
Enrico Granata4becb372011-06-29 22:27:15 +0000983 }
Greg Clayton34132752011-07-06 04:07:21 +0000984 // Fall through...
985
Enrico Granata4becb372011-06-29 22:27:15 +0000986 case 'v':
987 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000988 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
989 ValueObject::eDereference : ValueObject::eNothing);
990 ValueObject::GetValueForExpressionPathOptions options;
991 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar();
Greg Clayton34132752011-07-06 04:07:21 +0000992 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eDisplaySummary;
993 ValueObject* target = NULL;
994 lldb::Format custom_format = eFormatInvalid;
995 const char* var_name_final = NULL;
996 const char* var_name_final_if_array_range = NULL;
997 const char* close_bracket_position = NULL;
998 int64_t index_lower = -1;
999 int64_t index_higher = -1;
1000 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001001 const char* first_unparsed;
1002
Greg Clayton34132752011-07-06 04:07:21 +00001003 if (!vobj) break;
1004 // simplest case ${var}, just print vobj's value
1005 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001006 {
Greg Clayton34132752011-07-06 04:07:21 +00001007 target = vobj;
1008 val_obj_display = ValueObject::eDisplayValue;
1009 }
1010 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1011 {
1012 // this is a variable with some custom format applied to it
1013 const char* percent_position;
1014 target = vobj;
1015 val_obj_display = ValueObject::eDisplayValue;
1016 ScanFormatDescriptor (var_name_begin,
1017 var_name_end,
1018 &var_name_final,
1019 &percent_position,
1020 &custom_format,
1021 &val_obj_display);
1022 }
1023 // this is ${var.something} or multiple .something nested
1024 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1025 {
1026
1027 const char* percent_position;
1028 ScanFormatDescriptor (var_name_begin,
1029 var_name_end,
1030 &var_name_final,
1031 &percent_position,
1032 &custom_format,
1033 &val_obj_display);
1034
1035 const char* open_bracket_position;
1036 const char* separator_position;
1037 ScanBracketedRange (var_name_begin,
1038 var_name_end,
1039 var_name_final,
1040 &open_bracket_position,
1041 &separator_position,
1042 &close_bracket_position,
1043 &var_name_final_if_array_range,
1044 &index_lower,
1045 &index_higher);
1046
1047 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001048
1049 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1050 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1051 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1052
1053#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1054 printf("symbol to expand: %s\n",expr_path.get());
1055#endif //VERBOSE_FORMATPROMPT_OUTPUT
1056
1057 target = vobj->GetValueForExpressionPath(expr_path.get(),
1058 &first_unparsed,
1059 &reason_to_stop,
1060 &final_value_type,
1061 options,
1062 &what_next).get();
1063
1064 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001065 {
Greg Clayton34132752011-07-06 04:07:21 +00001066#ifdef VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001067 printf("ERROR: unparsed portion = %s, why stopping = %d,"
1068 " final_value_type %d\n",
1069 first_unparsed, reason_to_stop, final_value_type);
Greg Clayton34132752011-07-06 04:07:21 +00001070#endif //VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001071 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001072 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001073#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1074 else
1075 {
1076 printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1077 " final_value_type %d\n",
1078 first_unparsed, reason_to_stop, final_value_type);
1079 }
1080#endif //VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata4becb372011-06-29 22:27:15 +00001081 }
Greg Clayton34132752011-07-06 04:07:21 +00001082 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001083 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001084
1085 is_array_range = (final_value_type == ValueObject::eBoundedRange ||
1086 final_value_type == ValueObject::eUnboundedRange);
1087
1088 do_deref_pointer = (what_next == ValueObject::eDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001089
Enrico Granataa7187d02011-07-06 19:27:11 +00001090 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001091 {
Greg Clayton34132752011-07-06 04:07:21 +00001092 // I have not deref-ed yet, let's do it
1093 // this happens when we are not going through GetValueForVariableExpressionPath
1094 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001095 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001096 target = target->Dereference(error).get();
Enrico Granata9fc19442011-07-06 02:13:41 +00001097 IFERROR_PRINT_IT
Greg Clayton34132752011-07-06 04:07:21 +00001098 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001099 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001100
1101 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1102 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
1103
1104 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eDisplayValue) // this should be wrong, but there are some exceptions
1105 {
1106#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1107 printf("I am into array || pointer && !range\n");
1108#endif //VERBOSE_FORMATPROMPT_OUTPUT
1109 // try to use the special cases
1110 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1111 if (!var_success)
1112 s << "<invalid, please use [] operator>";
1113#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1114 printf("outcome was : %s\n", var_success ? "good" : "bad");
1115#endif //VERBOSE_FORMATPROMPT_OUTPUT
1116 break;
1117 }
Greg Clayton34132752011-07-06 04:07:21 +00001118
1119 if (!is_array_range)
1120 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1121 else
Enrico Granataf4efecd2011-07-12 22:56:10 +00001122 {
Greg Clayton34132752011-07-06 04:07:21 +00001123 if (!is_array && !is_pointer)
1124 break;
1125
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001126 const char* special_directions = NULL;
1127 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001128 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1129 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001130 ConstString additional_data;
1131 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1132 special_directions_writer.Printf("${%svar%s}",
1133 do_deref_pointer ? "*" : "",
1134 additional_data.GetCString());
1135 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001136 }
1137
1138 // let us display items index_lower thru index_higher of this array
1139 s.PutChar('[');
1140 var_success = true;
1141
1142 if (index_higher < 0)
1143 index_higher = vobj->GetNumChildren() - 1;
1144
1145 for (;index_lower<=index_higher;index_lower++)
1146 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001147 ValueObject* item = ExpandIndexedExpression(target,
Greg Clayton34132752011-07-06 04:07:21 +00001148 index_lower,
1149 exe_ctx->frame,
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001150 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001151
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001152 if (!item)
1153 {
1154#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1155 printf("ERROR\n");
1156#endif //VERBOSE_FORMATPROMPT_OUTPUT
1157 }
1158#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1159 else
1160 {
1161 printf("special_directions: %s\n",special_directions);
1162 }
1163#endif //VERBOSE_FORMATPROMPT_OUTPUT
1164
Greg Clayton34132752011-07-06 04:07:21 +00001165 if (!special_directions)
1166 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1167 else
1168 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1169
1170 if (index_lower < index_higher)
1171 s.PutChar(',');
1172 }
1173 s.PutChar(']');
1174 }
Enrico Granata4becb372011-06-29 22:27:15 +00001175 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001176 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001177 case 'a':
1178 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1179 {
1180 if (addr && addr->IsValid())
1181 {
1182 var_success = true;
1183 format_addr = *addr;
1184 }
1185 }
1186 break;
1187
1188 case 'p':
1189 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1190 {
1191 if (exe_ctx && exe_ctx->process != NULL)
1192 {
1193 var_name_begin += ::strlen ("process.");
1194 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1195 {
1196 s.Printf("%i", exe_ctx->process->GetID());
1197 var_success = true;
1198 }
1199 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1200 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1201 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1202 {
1203 ModuleSP exe_module_sp (exe_ctx->process->GetTarget().GetExecutableModule());
1204 if (exe_module_sp)
1205 {
1206 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1207 {
1208 format_file_spec.GetFilename() = exe_module_sp->GetFileSpec().GetFilename();
1209 var_success = format_file_spec;
1210 }
1211 else
1212 {
1213 format_file_spec = exe_module_sp->GetFileSpec();
1214 var_success = format_file_spec;
1215 }
1216 }
1217 }
1218 }
1219 }
1220 break;
1221
1222 case 't':
1223 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1224 {
1225 if (exe_ctx && exe_ctx->thread)
1226 {
1227 var_name_begin += ::strlen ("thread.");
1228 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1229 {
1230 s.Printf("0x%4.4x", exe_ctx->thread->GetID());
1231 var_success = true;
1232 }
1233 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1234 {
1235 s.Printf("%u", exe_ctx->thread->GetIndexID());
1236 var_success = true;
1237 }
1238 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1239 {
1240 cstr = exe_ctx->thread->GetName();
1241 var_success = cstr && cstr[0];
1242 if (var_success)
1243 s.PutCString(cstr);
1244 }
1245 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1246 {
1247 cstr = exe_ctx->thread->GetQueueName();
1248 var_success = cstr && cstr[0];
1249 if (var_success)
1250 s.PutCString(cstr);
1251 }
1252 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1253 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001254 StopInfoSP stop_info_sp = exe_ctx->thread->GetStopInfo ();
1255 if (stop_info_sp)
Greg Clayton1b654882010-09-19 02:33:57 +00001256 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001257 cstr = stop_info_sp->GetDescription();
Greg Clayton1b654882010-09-19 02:33:57 +00001258 if (cstr && cstr[0])
1259 {
1260 s.PutCString(cstr);
1261 var_success = true;
1262 }
1263 }
1264 }
1265 }
1266 }
1267 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1268 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001269 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1270 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001271 {
Greg Clayton1b654882010-09-19 02:33:57 +00001272 var_name_begin += ::strlen ("target.");
1273 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1274 {
1275 ArchSpec arch (target->GetArchitecture ());
1276 if (arch.IsValid())
1277 {
Greg Clayton64195a22011-02-23 00:35:02 +00001278 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001279 var_success = true;
1280 }
1281 }
1282 }
1283 }
1284 break;
1285
1286
1287 case 'm':
1288 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1289 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001290 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001291 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001292 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001293 var_name_begin += ::strlen ("module.");
1294
1295 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1296 {
1297 if (module->GetFileSpec())
1298 {
1299 var_name_begin += ::strlen ("file.");
1300
1301 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1302 {
1303 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1304 var_success = format_file_spec;
1305 }
1306 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1307 {
1308 format_file_spec = module->GetFileSpec();
1309 var_success = format_file_spec;
1310 }
1311 }
1312 }
1313 }
1314 }
1315 break;
1316
1317
1318 case 'f':
1319 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1320 {
1321 if (sc && sc->comp_unit != NULL)
1322 {
1323 var_name_begin += ::strlen ("file.");
1324
1325 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1326 {
1327 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1328 var_success = format_file_spec;
1329 }
1330 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1331 {
1332 format_file_spec = *sc->comp_unit;
1333 var_success = format_file_spec;
1334 }
1335 }
1336 }
1337 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1338 {
1339 if (exe_ctx && exe_ctx->frame)
1340 {
1341 var_name_begin += ::strlen ("frame.");
1342 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1343 {
1344 s.Printf("%u", exe_ctx->frame->GetFrameIndex());
1345 var_success = true;
1346 }
1347 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1348 {
1349 reg_kind = eRegisterKindGeneric;
1350 reg_num = LLDB_REGNUM_GENERIC_PC;
1351 var_success = true;
1352 }
1353 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1354 {
1355 reg_kind = eRegisterKindGeneric;
1356 reg_num = LLDB_REGNUM_GENERIC_SP;
1357 var_success = true;
1358 }
1359 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1360 {
1361 reg_kind = eRegisterKindGeneric;
1362 reg_num = LLDB_REGNUM_GENERIC_FP;
1363 var_success = true;
1364 }
1365 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
1366 {
1367 reg_kind = eRegisterKindGeneric;
1368 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
1369 var_success = true;
1370 }
1371 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
1372 {
Greg Clayton5ccbd292011-01-06 22:15:06 +00001373 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001374 if (reg_ctx)
1375 {
1376 var_name_begin += ::strlen ("reg.");
1377 if (var_name_begin < var_name_end)
1378 {
1379 std::string reg_name (var_name_begin, var_name_end);
1380 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
1381 if (reg_info)
1382 var_success = true;
1383 }
1384 }
1385 }
1386 }
1387 }
1388 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
1389 {
1390 if (sc && (sc->function != NULL || sc->symbol != NULL))
1391 {
1392 var_name_begin += ::strlen ("function.");
1393 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1394 {
1395 if (sc->function)
1396 s.Printf("function{0x%8.8x}", sc->function->GetID());
1397 else
1398 s.Printf("symbol[%u]", sc->symbol->GetID());
1399
1400 var_success = true;
1401 }
1402 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1403 {
1404 if (sc->function)
1405 cstr = sc->function->GetName().AsCString (NULL);
1406 else if (sc->symbol)
1407 cstr = sc->symbol->GetName().AsCString (NULL);
1408 if (cstr)
1409 {
1410 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00001411
1412 if (sc->block)
1413 {
1414 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1415 if (inline_block)
1416 {
1417 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
1418 if (inline_info)
1419 {
1420 s.PutCString(" [inlined] ");
1421 inline_info->GetName().Dump(&s);
1422 }
1423 }
1424 }
Greg Clayton1b654882010-09-19 02:33:57 +00001425 var_success = true;
1426 }
1427 }
1428 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
1429 {
1430 var_success = addr != NULL;
1431 if (var_success)
1432 {
1433 format_addr = *addr;
1434 calculate_format_addr_function_offset = true;
1435 }
1436 }
1437 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
1438 {
1439 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
1440 if (var_success)
1441 {
1442 format_addr = sc->line_entry.range.GetBaseAddress();
1443 calculate_format_addr_function_offset = true;
1444 }
1445 }
1446 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
1447 {
1448 var_success = exe_ctx->frame;
1449 if (var_success)
1450 {
1451 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1452 calculate_format_addr_function_offset = true;
1453 }
1454 }
1455 }
1456 }
1457 break;
1458
1459 case 'l':
1460 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
1461 {
1462 if (sc && sc->line_entry.IsValid())
1463 {
1464 var_name_begin += ::strlen ("line.");
1465 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1466 {
1467 var_name_begin += ::strlen ("file.");
1468
1469 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1470 {
1471 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
1472 var_success = format_file_spec;
1473 }
1474 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1475 {
1476 format_file_spec = sc->line_entry.file;
1477 var_success = format_file_spec;
1478 }
1479 }
1480 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
1481 {
1482 var_success = true;
1483 s.Printf("%u", sc->line_entry.line);
1484 }
1485 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
1486 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
1487 {
1488 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
1489 if (var_success)
1490 {
1491 format_addr = sc->line_entry.range.GetBaseAddress();
1492 if (var_name_begin[0] == 'e')
1493 format_addr.Slide (sc->line_entry.range.GetByteSize());
1494 }
1495 }
1496 }
1497 }
1498 break;
1499 }
1500
1501 if (var_success)
1502 {
1503 // If format addr is valid, then we need to print an address
1504 if (reg_num != LLDB_INVALID_REGNUM)
1505 {
1506 // We have a register value to display...
1507 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
1508 {
1509 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1510 }
1511 else
1512 {
1513 if (reg_ctx == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +00001514 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001515
1516 if (reg_ctx)
1517 {
1518 if (reg_kind != kNumRegisterKinds)
1519 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
1520 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
1521 var_success = reg_info != NULL;
1522 }
1523 }
1524 }
1525
1526 if (reg_info != NULL)
1527 {
Greg Clayton7349bd92011-05-09 20:18:18 +00001528 RegisterValue reg_value;
1529 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
1530 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001531 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00001532 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00001533 }
1534 }
1535
1536 if (format_file_spec)
1537 {
1538 s << format_file_spec;
1539 }
1540
1541 // If format addr is valid, then we need to print an address
1542 if (format_addr.IsValid())
1543 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001544 var_success = false;
1545
Greg Clayton1b654882010-09-19 02:33:57 +00001546 if (calculate_format_addr_function_offset)
1547 {
1548 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00001549
Greg Clayton0603aa92010-10-04 01:05:56 +00001550 if (sc)
1551 {
1552 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00001553 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001554 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00001555 if (sc->block)
1556 {
1557 // Check to make sure we aren't in an inline
1558 // function. If we are, use the inline block
1559 // range that contains "format_addr" since
1560 // blocks can be discontiguous.
1561 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1562 AddressRange inline_range;
1563 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
1564 func_addr = inline_range.GetBaseAddress();
1565 }
1566 }
Greg Clayton0603aa92010-10-04 01:05:56 +00001567 else if (sc->symbol && sc->symbol->GetAddressRangePtr())
1568 func_addr = sc->symbol->GetAddressRangePtr()->GetBaseAddress();
1569 }
1570
1571 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00001572 {
1573 if (func_addr.GetSection() == format_addr.GetSection())
1574 {
1575 addr_t func_file_addr = func_addr.GetFileAddress();
1576 addr_t addr_file_addr = format_addr.GetFileAddress();
1577 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001578 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00001579 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001580 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001581 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00001582 }
1583 else
Greg Clayton0603aa92010-10-04 01:05:56 +00001584 {
1585 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1586 if (target)
1587 {
1588 addr_t func_load_addr = func_addr.GetLoadAddress (target);
1589 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
1590 if (addr_load_addr > func_load_addr)
1591 s.Printf(" + %llu", addr_load_addr - func_load_addr);
1592 else if (addr_load_addr < func_load_addr)
1593 s.Printf(" - %llu", func_load_addr - addr_load_addr);
1594 var_success = true;
1595 }
1596 }
Greg Clayton1b654882010-09-19 02:33:57 +00001597 }
1598 }
1599 else
1600 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001601 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00001602 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00001603 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
1604 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00001605 if (vaddr == LLDB_INVALID_ADDRESS)
1606 vaddr = format_addr.GetFileAddress ();
1607
1608 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00001609 {
Greg Clayton514487e2011-02-15 21:59:32 +00001610 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00001611 if (addr_width == 0)
1612 addr_width = 16;
1613 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001614 var_success = true;
1615 }
Greg Clayton1b654882010-09-19 02:33:57 +00001616 }
1617 }
1618 }
1619
1620 if (var_success == false)
1621 success = false;
1622 }
1623 p = var_name_end;
1624 }
1625 else
1626 break;
1627 }
1628 else
1629 {
1630 // We got a dollar sign with no '{' after it, it must just be a dollar sign
1631 s.PutChar(*p);
1632 }
1633 }
1634 else if (*p == '\\')
1635 {
1636 ++p; // skip the slash
1637 switch (*p)
1638 {
1639 case 'a': s.PutChar ('\a'); break;
1640 case 'b': s.PutChar ('\b'); break;
1641 case 'f': s.PutChar ('\f'); break;
1642 case 'n': s.PutChar ('\n'); break;
1643 case 'r': s.PutChar ('\r'); break;
1644 case 't': s.PutChar ('\t'); break;
1645 case 'v': s.PutChar ('\v'); break;
1646 case '\'': s.PutChar ('\''); break;
1647 case '\\': s.PutChar ('\\'); break;
1648 case '0':
1649 // 1 to 3 octal chars
1650 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001651 // Make a string that can hold onto the initial zero char,
1652 // up to 3 octal digits, and a terminating NULL.
1653 char oct_str[5] = { 0, 0, 0, 0, 0 };
1654
1655 int i;
1656 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
1657 oct_str[i] = p[i];
1658
1659 // We don't want to consume the last octal character since
1660 // the main for loop will do this for us, so we advance p by
1661 // one less than i (even if i is zero)
1662 p += i - 1;
1663 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
1664 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001665 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001666 char octal_char = octal_value;
1667 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00001668 }
Greg Clayton1b654882010-09-19 02:33:57 +00001669 }
1670 break;
1671
1672 case 'x':
1673 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00001674 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001675 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001676 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00001677
Greg Clayton0603aa92010-10-04 01:05:56 +00001678 // Make a string that can hold onto two hex chars plus a
1679 // NULL terminator
1680 char hex_str[3] = { 0,0,0 };
1681 hex_str[0] = *p;
1682 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001683 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001684 ++p; // Skip the first of the two hex chars
1685 hex_str[1] = *p;
1686 }
1687
1688 unsigned long hex_value = strtoul (hex_str, NULL, 16);
1689 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001690 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00001691 }
1692 else
1693 {
1694 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00001695 }
1696 break;
1697
1698 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00001699 // Just desensitize any other character by just printing what
1700 // came after the '\'
1701 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00001702 break;
1703
1704 }
1705
1706 }
1707 }
1708 if (end)
1709 *end = p;
1710 return success;
1711}
1712
Greg Clayton4a33d312011-06-23 17:59:56 +00001713
1714static FormatManager&
1715GetFormatManager() {
1716 static FormatManager g_format_manager;
1717 return g_format_manager;
1718}
1719
1720bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001721Debugger::Formatting::ValueFormats::Get(ValueObject& vobj, ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001722{
Enrico Granata4becb372011-06-29 22:27:15 +00001723 return GetFormatManager().Value().Get(vobj,entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001724}
1725
1726void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001727Debugger::Formatting::ValueFormats::Add(const ConstString &type, const ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001728{
Enrico Granata4becb372011-06-29 22:27:15 +00001729 GetFormatManager().Value().Add(type.AsCString(),entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001730}
1731
1732bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001733Debugger::Formatting::ValueFormats::Delete(const ConstString &type)
Greg Clayton4a33d312011-06-23 17:59:56 +00001734{
Enrico Granata4becb372011-06-29 22:27:15 +00001735 return GetFormatManager().Value().Delete(type.AsCString());
Greg Clayton4a33d312011-06-23 17:59:56 +00001736}
1737
1738void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001739Debugger::Formatting::ValueFormats::Clear()
Greg Clayton4a33d312011-06-23 17:59:56 +00001740{
Enrico Granata4becb372011-06-29 22:27:15 +00001741 GetFormatManager().Value().Clear();
1742}
1743
1744void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001745Debugger::Formatting::ValueFormats::LoopThrough(ValueFormat::ValueCallback callback, void* callback_baton)
Enrico Granata4becb372011-06-29 22:27:15 +00001746{
1747 GetFormatManager().Value().LoopThrough(callback, callback_baton);
1748}
1749
1750uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001751Debugger::Formatting::ValueFormats::GetCurrentRevision()
Enrico Granata4becb372011-06-29 22:27:15 +00001752{
1753 return GetFormatManager().GetCurrentRevision();
1754}
1755
Enrico Granata0a3958e2011-07-02 00:25:22 +00001756uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001757Debugger::Formatting::ValueFormats::GetCount()
Enrico Granata0a3958e2011-07-02 00:25:22 +00001758{
1759 return GetFormatManager().Value().GetCount();
1760}
Enrico Granata4becb372011-06-29 22:27:15 +00001761
Enrico Granata4becb372011-06-29 22:27:15 +00001762bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001763Debugger::Formatting::GetSummaryFormat(ValueObject& vobj,
1764 lldb::SummaryFormatSP& entry)
Enrico Granata4becb372011-06-29 22:27:15 +00001765{
Enrico Granata1490c6f2011-07-19 02:34:21 +00001766 return GetFormatManager().Get(vobj, entry);
1767}
Enrico Granatad55546b2011-07-22 00:16:08 +00001768bool
1769Debugger::Formatting::GetSyntheticFilter(ValueObject& vobj,
1770 lldb::SyntheticFilterSP& entry)
1771{
1772 return GetFormatManager().Get(vobj, entry);
1773}
Enrico Granata1490c6f2011-07-19 02:34:21 +00001774
1775bool
1776Debugger::Formatting::Categories::Get(const ConstString &category, lldb::FormatCategorySP &entry)
1777{
1778 entry = GetFormatManager().Category(category.GetCString());
1779 return true;
Enrico Granata4becb372011-06-29 22:27:15 +00001780}
1781
1782void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001783Debugger::Formatting::Categories::Add(const ConstString &category)
Enrico Granata4becb372011-06-29 22:27:15 +00001784{
Enrico Granata1490c6f2011-07-19 02:34:21 +00001785 GetFormatManager().Category(category.GetCString());
1786}
1787
1788bool
1789Debugger::Formatting::Categories::Delete(const ConstString &category)
1790{
1791 GetFormatManager().DisableCategory(category.GetCString());
1792 return GetFormatManager().Categories().Delete(category.GetCString());
Enrico Granata4becb372011-06-29 22:27:15 +00001793}
1794
1795void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001796Debugger::Formatting::Categories::Clear()
Enrico Granata4becb372011-06-29 22:27:15 +00001797{
Enrico Granata1490c6f2011-07-19 02:34:21 +00001798 GetFormatManager().Categories().Clear();
1799}
1800
1801void
1802Debugger::Formatting::Categories::Clear(ConstString &category)
1803{
Enrico Granatad55546b2011-07-22 00:16:08 +00001804 GetFormatManager().Category(category.GetCString())->ClearSummaries();
Enrico Granata1490c6f2011-07-19 02:34:21 +00001805}
1806
1807void
1808Debugger::Formatting::Categories::Enable(ConstString& category)
1809{
1810 if (GetFormatManager().Category(category.GetCString())->IsEnabled() == false)
Enrico Granata1490c6f2011-07-19 02:34:21 +00001811 GetFormatManager().EnableCategory(category.GetCString());
Enrico Granata1490c6f2011-07-19 02:34:21 +00001812 else
1813 {
Enrico Granata1490c6f2011-07-19 02:34:21 +00001814 GetFormatManager().DisableCategory(category.GetCString());
Enrico Granata1490c6f2011-07-19 02:34:21 +00001815 GetFormatManager().EnableCategory(category.GetCString());
1816 }
1817}
1818
1819void
1820Debugger::Formatting::Categories::Disable(ConstString& category)
1821{
1822 if (GetFormatManager().Category(category.GetCString())->IsEnabled() == true)
Enrico Granata1490c6f2011-07-19 02:34:21 +00001823 GetFormatManager().DisableCategory(category.GetCString());
Enrico Granata1490c6f2011-07-19 02:34:21 +00001824}
1825
1826void
1827Debugger::Formatting::Categories::LoopThrough(FormatManager::CategoryCallback callback, void* callback_baton)
1828{
1829 GetFormatManager().LoopThroughCategories(callback, callback_baton);
Enrico Granata4becb372011-06-29 22:27:15 +00001830}
1831
1832uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001833Debugger::Formatting::Categories::GetCurrentRevision()
Enrico Granata4becb372011-06-29 22:27:15 +00001834{
1835 return GetFormatManager().GetCurrentRevision();
Greg Clayton4a33d312011-06-23 17:59:56 +00001836}
1837
Enrico Granata0a3958e2011-07-02 00:25:22 +00001838uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001839Debugger::Formatting::Categories::GetCount()
Enrico Granata0a3958e2011-07-02 00:25:22 +00001840{
Enrico Granata1490c6f2011-07-19 02:34:21 +00001841 return GetFormatManager().Categories().GetCount();
Enrico Granata0a3958e2011-07-02 00:25:22 +00001842}
1843
1844bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001845Debugger::Formatting::NamedSummaryFormats::Get(const ConstString &type, SummaryFormat::SharedPointer &entry)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001846{
1847 return GetFormatManager().NamedSummary().Get(type.AsCString(),entry);
1848}
1849
1850void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001851Debugger::Formatting::NamedSummaryFormats::Add(const ConstString &type, const SummaryFormat::SharedPointer &entry)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001852{
1853 GetFormatManager().NamedSummary().Add(type.AsCString(),entry);
1854}
1855
1856bool
Enrico Granata1490c6f2011-07-19 02:34:21 +00001857Debugger::Formatting::NamedSummaryFormats::Delete(const ConstString &type)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001858{
1859 return GetFormatManager().NamedSummary().Delete(type.AsCString());
1860}
1861
1862void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001863Debugger::Formatting::NamedSummaryFormats::Clear()
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001864{
1865 GetFormatManager().NamedSummary().Clear();
1866}
1867
1868void
Enrico Granata1490c6f2011-07-19 02:34:21 +00001869Debugger::Formatting::NamedSummaryFormats::LoopThrough(SummaryFormat::SummaryCallback callback, void* callback_baton)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001870{
1871 GetFormatManager().NamedSummary().LoopThrough(callback, callback_baton);
1872}
1873
1874uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001875Debugger::Formatting::NamedSummaryFormats::GetCurrentRevision()
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001876{
1877 return GetFormatManager().GetCurrentRevision();
1878}
1879
1880uint32_t
Enrico Granata1490c6f2011-07-19 02:34:21 +00001881Debugger::Formatting::NamedSummaryFormats::GetCount()
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001882{
1883 return GetFormatManager().NamedSummary().GetCount();
1884}
1885
Greg Clayton1b654882010-09-19 02:33:57 +00001886#pragma mark Debugger::SettingsController
1887
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001888//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00001889// class Debugger::SettingsController
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001890//--------------------------------------------------
1891
Greg Clayton1b654882010-09-19 02:33:57 +00001892Debugger::SettingsController::SettingsController () :
Caroline Tice101c7c22010-09-09 06:25:08 +00001893 UserSettingsController ("", lldb::UserSettingsControllerSP())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001894{
Caroline Tice91123da2010-09-08 17:48:55 +00001895 m_default_settings.reset (new DebuggerInstanceSettings (*this, false,
1896 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001897}
1898
Greg Clayton1b654882010-09-19 02:33:57 +00001899Debugger::SettingsController::~SettingsController ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001900{
1901}
1902
1903
1904lldb::InstanceSettingsSP
Greg Clayton1b654882010-09-19 02:33:57 +00001905Debugger::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001906{
Greg Claytondbe54502010-11-19 03:46:01 +00001907 DebuggerInstanceSettings *new_settings = new DebuggerInstanceSettings (*GetSettingsController(),
Caroline Tice91123da2010-09-08 17:48:55 +00001908 false, instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001909 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1910 return new_settings_sp;
1911}
1912
Greg Clayton1b654882010-09-19 02:33:57 +00001913#pragma mark DebuggerInstanceSettings
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001914//--------------------------------------------------
1915// class DebuggerInstanceSettings
1916//--------------------------------------------------
1917
Greg Claytona7015092010-09-18 01:14:36 +00001918DebuggerInstanceSettings::DebuggerInstanceSettings
1919(
1920 UserSettingsController &owner,
1921 bool live_instance,
1922 const char *name
1923) :
Greg Clayton85851dd2010-12-04 00:10:17 +00001924 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytona7015092010-09-18 01:14:36 +00001925 m_term_width (80),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001926 m_prompt (),
Greg Clayton0603aa92010-10-04 01:05:56 +00001927 m_frame_format (),
1928 m_thread_format (),
Caroline Ticedaccaa92010-09-20 20:44:43 +00001929 m_script_lang (),
Jim Ingham3bcdb292010-10-04 22:44:14 +00001930 m_use_external_editor (false),
1931 m_auto_confirm_on (false)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001932{
Caroline Ticef20e8232010-09-09 18:26:37 +00001933 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1934 // until the vtables for DebuggerInstanceSettings are properly set up, i.e. AFTER all the initializers.
1935 // 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 +00001936 // The same is true of CreateInstanceName().
1937
1938 if (GetInstanceName() == InstanceSettings::InvalidName())
1939 {
1940 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1941 m_owner.RegisterInstanceSettings (this);
1942 }
Caroline Ticef20e8232010-09-09 18:26:37 +00001943
1944 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001945 {
1946 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1947 CopyInstanceSettings (pending_settings, false);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001948 }
1949}
1950
1951DebuggerInstanceSettings::DebuggerInstanceSettings (const DebuggerInstanceSettings &rhs) :
Greg Clayton99d0faf2010-11-18 23:32:35 +00001952 InstanceSettings (*Debugger::GetSettingsController(), CreateInstanceName ().AsCString()),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001953 m_prompt (rhs.m_prompt),
Greg Clayton0603aa92010-10-04 01:05:56 +00001954 m_frame_format (rhs.m_frame_format),
1955 m_thread_format (rhs.m_thread_format),
Caroline Ticedaccaa92010-09-20 20:44:43 +00001956 m_script_lang (rhs.m_script_lang),
Jim Ingham3bcdb292010-10-04 22:44:14 +00001957 m_use_external_editor (rhs.m_use_external_editor),
1958 m_auto_confirm_on(rhs.m_auto_confirm_on)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001959{
1960 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1961 CopyInstanceSettings (pending_settings, false);
1962 m_owner.RemovePendingSettings (m_instance_name);
1963}
1964
1965DebuggerInstanceSettings::~DebuggerInstanceSettings ()
1966{
1967}
1968
1969DebuggerInstanceSettings&
1970DebuggerInstanceSettings::operator= (const DebuggerInstanceSettings &rhs)
1971{
1972 if (this != &rhs)
1973 {
Greg Clayton1b654882010-09-19 02:33:57 +00001974 m_term_width = rhs.m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001975 m_prompt = rhs.m_prompt;
Greg Clayton0603aa92010-10-04 01:05:56 +00001976 m_frame_format = rhs.m_frame_format;
1977 m_thread_format = rhs.m_thread_format;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001978 m_script_lang = rhs.m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00001979 m_use_external_editor = rhs.m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00001980 m_auto_confirm_on = rhs.m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001981 }
1982
1983 return *this;
1984}
1985
Greg Clayton1b654882010-09-19 02:33:57 +00001986bool
1987DebuggerInstanceSettings::ValidTermWidthValue (const char *value, Error err)
1988{
1989 bool valid = false;
1990
1991 // Verify we have a value string.
1992 if (value == NULL || value[0] == '\0')
1993 {
1994 err.SetErrorString ("Missing value. Can't set terminal width without a value.\n");
1995 }
1996 else
1997 {
1998 char *end = NULL;
1999 const uint32_t width = ::strtoul (value, &end, 0);
2000
Johnny Chenea9fc182010-09-20 16:36:43 +00002001 if (end && end[0] == '\0')
Greg Clayton1b654882010-09-19 02:33:57 +00002002 {
Johnny Chen433d7742010-09-20 17:04:41 +00002003 if (width >= 10 && width <= 1024)
Greg Clayton1b654882010-09-19 02:33:57 +00002004 valid = true;
2005 else
2006 err.SetErrorString ("Invalid term-width value; value must be between 10 and 1024.\n");
2007 }
2008 else
2009 err.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string.\n", value);
2010 }
2011
2012 return valid;
2013}
2014
2015
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002016void
2017DebuggerInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2018 const char *index_value,
2019 const char *value,
2020 const ConstString &instance_name,
2021 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:54 +00002022 VarSetOperationType op,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002023 Error &err,
2024 bool pending)
2025{
Greg Clayton0603aa92010-10-04 01:05:56 +00002026
2027 if (var_name == TermWidthVarName())
2028 {
2029 if (ValidTermWidthValue (value, err))
2030 {
2031 m_term_width = ::strtoul (value, NULL, 0);
2032 }
2033 }
2034 else if (var_name == PromptVarName())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002035 {
Caroline Tice101c7c22010-09-09 06:25:08 +00002036 UserSettingsController::UpdateStringVariable (op, m_prompt, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002037 if (!pending)
2038 {
Caroline Tice49e27372010-09-07 18:35:40 +00002039 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2040 // strip off the brackets before passing it to BroadcastPromptChange.
2041
2042 std::string tmp_instance_name (instance_name.AsCString());
2043 if ((tmp_instance_name[0] == '[')
2044 && (tmp_instance_name[instance_name.GetLength() - 1] == ']'))
2045 tmp_instance_name = tmp_instance_name.substr (1, instance_name.GetLength() - 2);
2046 ConstString new_name (tmp_instance_name.c_str());
2047
2048 BroadcastPromptChange (new_name, m_prompt.c_str());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002049 }
2050 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002051 else if (var_name == GetFrameFormatName())
2052 {
2053 UserSettingsController::UpdateStringVariable (op, m_frame_format, value, err);
2054 }
2055 else if (var_name == GetThreadFormatName())
2056 {
2057 UserSettingsController::UpdateStringVariable (op, m_thread_format, value, err);
2058 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002059 else if (var_name == ScriptLangVarName())
2060 {
2061 bool success;
2062 m_script_lang = Args::StringToScriptLanguage (value, eScriptLanguageDefault,
2063 &success);
2064 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002065 else if (var_name == UseExternalEditorVarName ())
2066 {
Greg Clayton385aa282011-04-22 03:55:06 +00002067 UserSettingsController::UpdateBooleanVariable (op, m_use_external_editor, value, false, err);
Caroline Ticedaccaa92010-09-20 20:44:43 +00002068 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002069 else if (var_name == AutoConfirmName ())
2070 {
Greg Clayton385aa282011-04-22 03:55:06 +00002071 UserSettingsController::UpdateBooleanVariable (op, m_auto_confirm_on, value, false, err);
Jim Ingham3bcdb292010-10-04 22:44:14 +00002072 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002073}
2074
Caroline Tice12cecd72010-09-20 21:37:42 +00002075bool
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002076DebuggerInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2077 const ConstString &var_name,
Caroline Ticedaccaa92010-09-20 20:44:43 +00002078 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42 +00002079 Error *err)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002080{
2081 if (var_name == PromptVarName())
2082 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002083 value.AppendString (m_prompt.c_str(), m_prompt.size());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002084
2085 }
2086 else if (var_name == ScriptLangVarName())
2087 {
2088 value.AppendString (ScriptInterpreter::LanguageToString (m_script_lang).c_str());
2089 }
Caroline Tice101c7c22010-09-09 06:25:08 +00002090 else if (var_name == TermWidthVarName())
2091 {
2092 StreamString width_str;
2093 width_str.Printf ("%d", m_term_width);
2094 value.AppendString (width_str.GetData());
2095 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002096 else if (var_name == GetFrameFormatName ())
2097 {
2098 value.AppendString(m_frame_format.c_str(), m_frame_format.size());
2099 }
2100 else if (var_name == GetThreadFormatName ())
2101 {
2102 value.AppendString(m_thread_format.c_str(), m_thread_format.size());
2103 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002104 else if (var_name == UseExternalEditorVarName())
2105 {
2106 if (m_use_external_editor)
2107 value.AppendString ("true");
2108 else
2109 value.AppendString ("false");
2110 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002111 else if (var_name == AutoConfirmName())
2112 {
2113 if (m_auto_confirm_on)
2114 value.AppendString ("true");
2115 else
2116 value.AppendString ("false");
2117 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002118 else
Caroline Tice12cecd72010-09-20 21:37:42 +00002119 {
2120 if (err)
2121 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2122 return false;
2123 }
2124 return true;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002125}
2126
2127void
2128DebuggerInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
2129 bool pending)
2130{
2131 if (new_settings.get() == NULL)
2132 return;
2133
2134 DebuggerInstanceSettings *new_debugger_settings = (DebuggerInstanceSettings *) new_settings.get();
2135
2136 m_prompt = new_debugger_settings->m_prompt;
2137 if (!pending)
Caroline Tice49e27372010-09-07 18:35:40 +00002138 {
2139 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2140 // strip off the brackets before passing it to BroadcastPromptChange.
2141
2142 std::string tmp_instance_name (m_instance_name.AsCString());
2143 if ((tmp_instance_name[0] == '[')
2144 && (tmp_instance_name[m_instance_name.GetLength() - 1] == ']'))
2145 tmp_instance_name = tmp_instance_name.substr (1, m_instance_name.GetLength() - 2);
2146 ConstString new_name (tmp_instance_name.c_str());
2147
2148 BroadcastPromptChange (new_name, m_prompt.c_str());
2149 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002150 m_frame_format = new_debugger_settings->m_frame_format;
2151 m_thread_format = new_debugger_settings->m_thread_format;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002152 m_term_width = new_debugger_settings->m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002153 m_script_lang = new_debugger_settings->m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002154 m_use_external_editor = new_debugger_settings->m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002155 m_auto_confirm_on = new_debugger_settings->m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002156}
2157
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002158
2159bool
2160DebuggerInstanceSettings::BroadcastPromptChange (const ConstString &instance_name, const char *new_prompt)
2161{
2162 std::string tmp_prompt;
2163
2164 if (new_prompt != NULL)
2165 {
2166 tmp_prompt = new_prompt ;
2167 int len = tmp_prompt.size();
2168 if (len > 1
2169 && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"')
2170 && (tmp_prompt[len-1] == tmp_prompt[0]))
2171 {
2172 tmp_prompt = tmp_prompt.substr(1,len-2);
2173 }
2174 len = tmp_prompt.size();
2175 if (tmp_prompt[len-1] != ' ')
2176 tmp_prompt.append(" ");
2177 }
2178 EventSP new_event_sp;
2179 new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt,
2180 new EventDataBytes (tmp_prompt.c_str())));
2181
2182 if (instance_name.GetLength() != 0)
2183 {
2184 // Set prompt for a particular instance.
2185 Debugger *dbg = Debugger::FindDebuggerWithInstanceName (instance_name).get();
2186 if (dbg != NULL)
2187 {
2188 dbg->GetCommandInterpreter().BroadcastEvent (new_event_sp);
2189 }
2190 }
2191
2192 return true;
2193}
2194
2195const ConstString
2196DebuggerInstanceSettings::CreateInstanceName ()
2197{
2198 static int instance_count = 1;
2199 StreamString sstr;
2200
2201 sstr.Printf ("debugger_%d", instance_count);
2202 ++instance_count;
2203
2204 const ConstString ret_val (sstr.GetData());
2205
2206 return ret_val;
2207}
2208
2209const ConstString &
2210DebuggerInstanceSettings::PromptVarName ()
2211{
2212 static ConstString prompt_var_name ("prompt");
2213
2214 return prompt_var_name;
2215}
2216
2217const ConstString &
Greg Clayton0603aa92010-10-04 01:05:56 +00002218DebuggerInstanceSettings::GetFrameFormatName ()
2219{
2220 static ConstString prompt_var_name ("frame-format");
2221
2222 return prompt_var_name;
2223}
2224
2225const ConstString &
2226DebuggerInstanceSettings::GetThreadFormatName ()
2227{
2228 static ConstString prompt_var_name ("thread-format");
2229
2230 return prompt_var_name;
2231}
2232
2233const ConstString &
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002234DebuggerInstanceSettings::ScriptLangVarName ()
2235{
2236 static ConstString script_lang_var_name ("script-lang");
2237
2238 return script_lang_var_name;
2239}
2240
Caroline Tice101c7c22010-09-09 06:25:08 +00002241const ConstString &
2242DebuggerInstanceSettings::TermWidthVarName ()
2243{
2244 static ConstString term_width_var_name ("term-width");
2245
2246 return term_width_var_name;
2247}
2248
Caroline Ticedaccaa92010-09-20 20:44:43 +00002249const ConstString &
2250DebuggerInstanceSettings::UseExternalEditorVarName ()
2251{
2252 static ConstString use_external_editor_var_name ("use-external-editor");
2253
2254 return use_external_editor_var_name;
2255}
2256
Jim Ingham3bcdb292010-10-04 22:44:14 +00002257const ConstString &
2258DebuggerInstanceSettings::AutoConfirmName ()
2259{
2260 static ConstString use_external_editor_var_name ("auto-confirm");
2261
2262 return use_external_editor_var_name;
2263}
2264
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002265//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00002266// SettingsController Variable Tables
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002267//--------------------------------------------------
2268
2269
2270SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002271Debugger::SettingsController::global_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002272{
2273 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
Caroline Tice101c7c22010-09-09 06:25:08 +00002274 // The Debugger level global table should always be empty; all Debugger settable variables should be instance
2275 // variables.
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002276 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2277};
2278
Greg Claytonbb562b12010-10-04 02:44:26 +00002279#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name}${function.pc-offset}}}"
Greg Clayton0603aa92010-10-04 01:05:56 +00002280#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002281
Greg Clayton0603aa92010-10-04 01:05:56 +00002282#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2283 "{, ${frame.pc}}"\
2284 MODULE_WITH_FUNC\
Greg Claytoncf4b9072010-10-04 17:04:23 +00002285 FILE_AND_LINE\
Greg Clayton0603aa92010-10-04 01:05:56 +00002286 "{, stop reason = ${thread.stop-reason}}"\
Greg Clayton0603aa92010-10-04 01:05:56 +00002287 "\\n"
2288
Greg Clayton315d2ca2010-11-02 01:53:21 +00002289//#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2290// "{, ${frame.pc}}"\
2291// MODULE_WITH_FUNC\
2292// FILE_AND_LINE\
2293// "{, stop reason = ${thread.stop-reason}}"\
2294// "{, name = ${thread.name}}"\
2295// "{, queue = ${thread.queue}}"\
2296// "\\n"
2297
Greg Clayton0603aa92010-10-04 01:05:56 +00002298#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
2299 MODULE_WITH_FUNC\
2300 FILE_AND_LINE\
2301 "\\n"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002302
2303SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002304Debugger::SettingsController::instance_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002305{
Greg Clayton0603aa92010-10-04 01:05:56 +00002306// NAME Setting variable type Default Enum Init'd Hidden Help
2307// ======================= ======================= ====================== ==== ====== ====== ======================
Greg Clayton0603aa92010-10-04 01:05:56 +00002308{ "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 +00002309{ "prompt", eSetVarTypeString, "(lldb) ", NULL, false, false, "The debugger command line prompt displayed for the user." },
Jim Ingham3bcdb292010-10-04 22:44:14 +00002310{ "script-lang", eSetVarTypeString, "python", NULL, false, false, "The script language to be used for evaluating user-written scripts." },
2311{ "term-width", eSetVarTypeInt, "80" , NULL, false, false, "The maximum number of columns to use for displaying text." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002312{ "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 +00002313{ "use-external-editor", eSetVarTypeBoolean, "false", NULL, false, false, "Whether to use an external editor or not." },
2314{ "auto-confirm", eSetVarTypeBoolean, "false", NULL, false, false, "If true all confirmation prompts will receive their default reply." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002315{ NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002316};