blob: 2c36dbb8c66449bfe18bf1c19a6ae9295524f088 [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;
736 }
737 // a good custom format tells us to print the value using it
738 else
739 *val_obj_display = ValueObject::eDisplayValue;
740 delete format_name;
741 }
742 return true;
743}
744
745static bool
746ScanBracketedRange(const char* var_name_begin,
747 const char* var_name_end,
748 const char* var_name_final,
749 const char** open_bracket_position,
750 const char** separator_position,
751 const char** close_bracket_position,
752 const char** var_name_final_if_array_range,
753 int64_t* index_lower,
754 int64_t* index_higher)
755{
756 *open_bracket_position = ::strchr(var_name_begin,'[');
Greg Clayton34132752011-07-06 04:07:21 +0000757 if (*open_bracket_position && *open_bracket_position < var_name_final)
Enrico Granata9fc19442011-07-06 02:13:41 +0000758 {
759 *separator_position = ::strchr(*open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
760 *close_bracket_position = ::strchr(*open_bracket_position,']');
761 // as usual, we assume that [] will come before %
762 //printf("trying to expand a []\n");
763 *var_name_final_if_array_range = *open_bracket_position;
Greg Clayton34132752011-07-06 04:07:21 +0000764 if (*close_bracket_position - *open_bracket_position == 1)
Enrico Granata9fc19442011-07-06 02:13:41 +0000765 {
766 *index_lower = 0;
767 }
768 else if (*separator_position == NULL || *separator_position > var_name_end)
769 {
770 char *end = NULL;
771 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
772 *index_higher = *index_lower;
773 //printf("got to read low=%d high same\n",bitfield_lower);
774 }
Greg Clayton34132752011-07-06 04:07:21 +0000775 else if (*close_bracket_position && *close_bracket_position < var_name_end)
Enrico Granata9fc19442011-07-06 02:13:41 +0000776 {
777 char *end = NULL;
778 *index_lower = ::strtoul (*open_bracket_position+1, &end, 0);
779 *index_higher = ::strtoul (*separator_position+1, &end, 0);
780 //printf("got to read low=%d high=%d\n",bitfield_lower,bitfield_higher);
781 }
782 else
783 return false;
784 if (*index_lower > *index_higher && *index_higher > 0)
785 {
786 int temp = *index_lower;
787 *index_lower = *index_higher;
788 *index_higher = temp;
789 }
790 }
791 return true;
792}
793
794
795static ValueObjectSP
796ExpandExpressionPath(ValueObject* vobj,
797 StackFrame* frame,
798 bool* do_deref_pointer,
799 const char* var_name_begin,
800 const char* var_name_final,
801 Error& error)
802{
803
804 StreamString sstring;
805 VariableSP var_sp;
806
Greg Clayton34132752011-07-06 04:07:21 +0000807 if (*do_deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +0000808 sstring.PutChar('*');
Greg Clayton34132752011-07-06 04:07:21 +0000809 else if (vobj->IsDereferenceOfParent() && ClangASTContext::IsPointerType(vobj->GetParent()->GetClangType()) && !vobj->IsArrayItemForPointer())
Enrico Granata9fc19442011-07-06 02:13:41 +0000810 {
811 sstring.PutChar('*');
812 *do_deref_pointer = true;
813 }
814
815 vobj->GetExpressionPath(sstring, true, ValueObject::eHonorPointers);
816#ifdef VERBOSE_FORMATPROMPT_OUTPUT
817 printf("name to expand in phase 0: %s\n",sstring.GetData());
818#endif //VERBOSE_FORMATPROMPT_OUTPUT
819 sstring.PutRawBytes(var_name_begin+3, var_name_final-var_name_begin-3);
820#ifdef VERBOSE_FORMATPROMPT_OUTPUT
821 printf("name to expand in phase 1: %s\n",sstring.GetData());
822#endif //VERBOSE_FORMATPROMPT_OUTPUT
823 std::string name = std::string(sstring.GetData());
824 ValueObjectSP target = frame->GetValueForVariableExpressionPath (name.c_str(),
825 eNoDynamicValues,
826 0,
827 var_sp,
828 error);
829 return target;
830}
831
832static ValueObjectSP
833ExpandIndexedExpression(ValueObject* vobj,
834 uint32_t index,
835 StackFrame* frame,
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000836 bool deref_pointer)
Enrico Granata9fc19442011-07-06 02:13:41 +0000837{
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000838 const char* ptr_deref_format = "[%d]";
839 std::auto_ptr<char> ptr_deref_buffer(new char[10]);
840 ::sprintf(ptr_deref_buffer.get(), ptr_deref_format, index);
841#ifdef VERBOSE_FORMATPROMPT_OUTPUT
842 printf("name to deref: %s\n",ptr_deref_buffer.get());
843#endif //VERBOSE_FORMATPROMPT_OUTPUT
844 const char* first_unparsed;
845 ValueObject::GetValueForExpressionPathOptions options;
846 ValueObject::ExpressionPathEndResultType final_value_type;
847 ValueObject::ExpressionPathScanEndReason reason_to_stop;
848 ValueObject::ExpressionPathAftermath what_next = (deref_pointer ? ValueObject::eDereference : ValueObject::eNothing);
849 ValueObjectSP item = vobj->GetValueForExpressionPath (ptr_deref_buffer.get(),
850 &first_unparsed,
851 &reason_to_stop,
852 &final_value_type,
853 options,
854 &what_next);
855 if (!item)
856 {
857#ifdef VERBOSE_FORMATPROMPT_OUTPUT
858 printf("ERROR: unparsed portion = %s, why stopping = %d,"
859 " final_value_type %d\n",
860 first_unparsed, reason_to_stop, final_value_type);
861#endif //VERBOSE_FORMATPROMPT_OUTPUT
862 }
863#ifdef VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata9fc19442011-07-06 02:13:41 +0000864 else
865 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000866 printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
867 " final_value_type %d\n",
868 first_unparsed, reason_to_stop, final_value_type);
Enrico Granata9fc19442011-07-06 02:13:41 +0000869 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000870#endif //VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata9fc19442011-07-06 02:13:41 +0000871 return item;
872}
873
Greg Clayton1b654882010-09-19 02:33:57 +0000874bool
875Debugger::FormatPrompt
876(
877 const char *format,
878 const SymbolContext *sc,
879 const ExecutionContext *exe_ctx,
880 const Address *addr,
881 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +0000882 const char **end,
883 ValueObject* vobj
Greg Clayton1b654882010-09-19 02:33:57 +0000884)
885{
Enrico Granata4becb372011-06-29 22:27:15 +0000886 ValueObject* realvobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +0000887 bool success = true;
888 const char *p;
889 for (p = format; *p != '\0'; ++p)
890 {
Greg Clayton34132752011-07-06 04:07:21 +0000891 if (realvobj)
Enrico Granata4becb372011-06-29 22:27:15 +0000892 {
893 vobj = realvobj;
894 realvobj = NULL;
895 }
Greg Clayton1b654882010-09-19 02:33:57 +0000896 size_t non_special_chars = ::strcspn (p, "${}\\");
897 if (non_special_chars > 0)
898 {
899 if (success)
900 s.Write (p, non_special_chars);
901 p += non_special_chars;
902 }
903
904 if (*p == '\0')
905 {
906 break;
907 }
908 else if (*p == '{')
909 {
910 // Start a new scope that must have everything it needs if it is to
911 // to make it into the final output stream "s". If you want to make
912 // a format that only prints out the function or symbol name if there
913 // is one in the symbol context you can use:
914 // "{function =${function.name}}"
915 // The first '{' starts a new scope that end with the matching '}' at
916 // the end of the string. The contents "function =${function.name}"
917 // will then be evaluated and only be output if there is a function
918 // or symbol with a valid name.
919 StreamString sub_strm;
920
921 ++p; // Skip the '{'
922
Enrico Granata4becb372011-06-29 22:27:15 +0000923 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, vobj))
Greg Clayton1b654882010-09-19 02:33:57 +0000924 {
925 // The stream had all it needed
926 s.Write(sub_strm.GetData(), sub_strm.GetSize());
927 }
928 if (*p != '}')
929 {
930 success = false;
931 break;
932 }
933 }
934 else if (*p == '}')
935 {
936 // End of a enclosing scope
937 break;
938 }
939 else if (*p == '$')
940 {
941 // We have a prompt variable to print
942 ++p;
943 if (*p == '{')
944 {
945 ++p;
946 const char *var_name_begin = p;
947 const char *var_name_end = ::strchr (p, '}');
948
949 if (var_name_end && var_name_begin < var_name_end)
950 {
951 // if we have already failed to parse, skip this variable
952 if (success)
953 {
954 const char *cstr = NULL;
955 Address format_addr;
956 bool calculate_format_addr_function_offset = false;
957 // Set reg_kind and reg_num to invalid values
958 RegisterKind reg_kind = kNumRegisterKinds;
959 uint32_t reg_num = LLDB_INVALID_REGNUM;
960 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +0000961 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +0000962 RegisterContext *reg_ctx = NULL;
Enrico Granata9fc19442011-07-06 02:13:41 +0000963 bool do_deref_pointer = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000964 ValueObject::ExpressionPathScanEndReason reason_to_stop;
965 ValueObject::ExpressionPathEndResultType final_value_type;
966
Greg Clayton1b654882010-09-19 02:33:57 +0000967 // Each variable must set success to true below...
968 bool var_success = false;
969 switch (var_name_begin[0])
970 {
Enrico Granata4becb372011-06-29 22:27:15 +0000971 case '*':
972 {
Greg Clayton34132752011-07-06 04:07:21 +0000973 if (!vobj)
974 break;
Enrico Granata9fc19442011-07-06 02:13:41 +0000975 do_deref_pointer = true;
976 var_name_begin++;
Enrico Granata4becb372011-06-29 22:27:15 +0000977 }
Greg Clayton34132752011-07-06 04:07:21 +0000978 // Fall through...
979
Enrico Granata4becb372011-06-29 22:27:15 +0000980 case 'v':
981 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000982 ValueObject::ExpressionPathAftermath what_next = (do_deref_pointer ?
983 ValueObject::eDereference : ValueObject::eNothing);
984 ValueObject::GetValueForExpressionPathOptions options;
985 options.DontCheckDotVsArrowSyntax().DoAllowBitfieldSyntax().DoAllowFragileIVar();
Greg Clayton34132752011-07-06 04:07:21 +0000986 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eDisplaySummary;
987 ValueObject* target = NULL;
988 lldb::Format custom_format = eFormatInvalid;
989 const char* var_name_final = NULL;
990 const char* var_name_final_if_array_range = NULL;
991 const char* close_bracket_position = NULL;
992 int64_t index_lower = -1;
993 int64_t index_higher = -1;
994 bool is_array_range = false;
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000995 const char* first_unparsed;
996
Greg Clayton34132752011-07-06 04:07:21 +0000997 if (!vobj) break;
998 // simplest case ${var}, just print vobj's value
999 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata4becb372011-06-29 22:27:15 +00001000 {
Greg Clayton34132752011-07-06 04:07:21 +00001001 target = vobj;
1002 val_obj_display = ValueObject::eDisplayValue;
1003 }
1004 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1005 {
1006 // this is a variable with some custom format applied to it
1007 const char* percent_position;
1008 target = vobj;
1009 val_obj_display = ValueObject::eDisplayValue;
1010 ScanFormatDescriptor (var_name_begin,
1011 var_name_end,
1012 &var_name_final,
1013 &percent_position,
1014 &custom_format,
1015 &val_obj_display);
1016 }
1017 // this is ${var.something} or multiple .something nested
1018 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1019 {
1020
1021 const char* percent_position;
1022 ScanFormatDescriptor (var_name_begin,
1023 var_name_end,
1024 &var_name_final,
1025 &percent_position,
1026 &custom_format,
1027 &val_obj_display);
1028
1029 const char* open_bracket_position;
1030 const char* separator_position;
1031 ScanBracketedRange (var_name_begin,
1032 var_name_end,
1033 var_name_final,
1034 &open_bracket_position,
1035 &separator_position,
1036 &close_bracket_position,
1037 &var_name_final_if_array_range,
1038 &index_lower,
1039 &index_higher);
1040
1041 Error error;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001042
1043 std::auto_ptr<char> expr_path(new char[var_name_final-var_name_begin-1]);
1044 ::memset(expr_path.get(), 0, var_name_final-var_name_begin-1);
1045 memcpy(expr_path.get(), var_name_begin+3,var_name_final-var_name_begin-3);
1046
1047#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1048 printf("symbol to expand: %s\n",expr_path.get());
1049#endif //VERBOSE_FORMATPROMPT_OUTPUT
1050
1051 target = vobj->GetValueForExpressionPath(expr_path.get(),
1052 &first_unparsed,
1053 &reason_to_stop,
1054 &final_value_type,
1055 options,
1056 &what_next).get();
1057
1058 if (!target)
Enrico Granata9fc19442011-07-06 02:13:41 +00001059 {
Greg Clayton34132752011-07-06 04:07:21 +00001060#ifdef VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001061 printf("ERROR: unparsed portion = %s, why stopping = %d,"
1062 " final_value_type %d\n",
1063 first_unparsed, reason_to_stop, final_value_type);
Greg Clayton34132752011-07-06 04:07:21 +00001064#endif //VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001065 break;
Enrico Granata9fc19442011-07-06 02:13:41 +00001066 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001067#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1068 else
1069 {
1070 printf("ALL RIGHT: unparsed portion = %s, why stopping = %d,"
1071 " final_value_type %d\n",
1072 first_unparsed, reason_to_stop, final_value_type);
1073 }
1074#endif //VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata4becb372011-06-29 22:27:15 +00001075 }
Greg Clayton34132752011-07-06 04:07:21 +00001076 else
Enrico Granata9fc19442011-07-06 02:13:41 +00001077 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001078
1079 is_array_range = (final_value_type == ValueObject::eBoundedRange ||
1080 final_value_type == ValueObject::eUnboundedRange);
1081
1082 do_deref_pointer = (what_next == ValueObject::eDereference);
Enrico Granata9fc19442011-07-06 02:13:41 +00001083
Enrico Granataa7187d02011-07-06 19:27:11 +00001084 if (do_deref_pointer && !is_array_range)
Enrico Granata9fc19442011-07-06 02:13:41 +00001085 {
Greg Clayton34132752011-07-06 04:07:21 +00001086 // I have not deref-ed yet, let's do it
1087 // this happens when we are not going through GetValueForVariableExpressionPath
1088 // to get to the target ValueObject
Enrico Granata9fc19442011-07-06 02:13:41 +00001089 Error error;
Greg Clayton34132752011-07-06 04:07:21 +00001090 target = target->Dereference(error).get();
Enrico Granata9fc19442011-07-06 02:13:41 +00001091 IFERROR_PRINT_IT
Greg Clayton34132752011-07-06 04:07:21 +00001092 do_deref_pointer = false;
Enrico Granata9fc19442011-07-06 02:13:41 +00001093 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001094
1095 bool is_array = ClangASTContext::IsArrayType(target->GetClangType());
1096 bool is_pointer = ClangASTContext::IsPointerType(target->GetClangType());
1097
1098 if ((is_array || is_pointer) && (!is_array_range) && val_obj_display == ValueObject::eDisplayValue) // this should be wrong, but there are some exceptions
1099 {
1100#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1101 printf("I am into array || pointer && !range\n");
1102#endif //VERBOSE_FORMATPROMPT_OUTPUT
1103 // try to use the special cases
1104 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1105 if (!var_success)
1106 s << "<invalid, please use [] operator>";
1107#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1108 printf("outcome was : %s\n", var_success ? "good" : "bad");
1109#endif //VERBOSE_FORMATPROMPT_OUTPUT
1110 break;
1111 }
Greg Clayton34132752011-07-06 04:07:21 +00001112
1113 if (!is_array_range)
1114 var_success = target->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1115 else
Enrico Granataf4efecd2011-07-12 22:56:10 +00001116 {
Greg Clayton34132752011-07-06 04:07:21 +00001117 if (!is_array && !is_pointer)
1118 break;
1119
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001120 const char* special_directions = NULL;
1121 StreamString special_directions_writer;
Greg Clayton34132752011-07-06 04:07:21 +00001122 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
1123 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001124 ConstString additional_data;
1125 additional_data.SetCStringWithLength(close_bracket_position+1, var_name_end-close_bracket_position-1);
1126 special_directions_writer.Printf("${%svar%s}",
1127 do_deref_pointer ? "*" : "",
1128 additional_data.GetCString());
1129 special_directions = special_directions_writer.GetData();
Greg Clayton34132752011-07-06 04:07:21 +00001130 }
1131
1132 // let us display items index_lower thru index_higher of this array
1133 s.PutChar('[');
1134 var_success = true;
1135
1136 if (index_higher < 0)
1137 index_higher = vobj->GetNumChildren() - 1;
1138
1139 for (;index_lower<=index_higher;index_lower++)
1140 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001141 ValueObject* item = ExpandIndexedExpression(target,
Greg Clayton34132752011-07-06 04:07:21 +00001142 index_lower,
1143 exe_ctx->frame,
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001144 false).get();
Greg Clayton34132752011-07-06 04:07:21 +00001145
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001146 if (!item)
1147 {
1148#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1149 printf("ERROR\n");
1150#endif //VERBOSE_FORMATPROMPT_OUTPUT
1151 }
1152#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1153 else
1154 {
1155 printf("special_directions: %s\n",special_directions);
1156 }
1157#endif //VERBOSE_FORMATPROMPT_OUTPUT
1158
Greg Clayton34132752011-07-06 04:07:21 +00001159 if (!special_directions)
1160 var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
1161 else
1162 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
1163
1164 if (index_lower < index_higher)
1165 s.PutChar(',');
1166 }
1167 s.PutChar(']');
1168 }
Enrico Granata4becb372011-06-29 22:27:15 +00001169 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001170 break;
Greg Clayton1b654882010-09-19 02:33:57 +00001171 case 'a':
1172 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1173 {
1174 if (addr && addr->IsValid())
1175 {
1176 var_success = true;
1177 format_addr = *addr;
1178 }
1179 }
1180 break;
1181
1182 case 'p':
1183 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1184 {
1185 if (exe_ctx && exe_ctx->process != NULL)
1186 {
1187 var_name_begin += ::strlen ("process.");
1188 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1189 {
1190 s.Printf("%i", exe_ctx->process->GetID());
1191 var_success = true;
1192 }
1193 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1194 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1195 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1196 {
1197 ModuleSP exe_module_sp (exe_ctx->process->GetTarget().GetExecutableModule());
1198 if (exe_module_sp)
1199 {
1200 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1201 {
1202 format_file_spec.GetFilename() = exe_module_sp->GetFileSpec().GetFilename();
1203 var_success = format_file_spec;
1204 }
1205 else
1206 {
1207 format_file_spec = exe_module_sp->GetFileSpec();
1208 var_success = format_file_spec;
1209 }
1210 }
1211 }
1212 }
1213 }
1214 break;
1215
1216 case 't':
1217 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1218 {
1219 if (exe_ctx && exe_ctx->thread)
1220 {
1221 var_name_begin += ::strlen ("thread.");
1222 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1223 {
1224 s.Printf("0x%4.4x", exe_ctx->thread->GetID());
1225 var_success = true;
1226 }
1227 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1228 {
1229 s.Printf("%u", exe_ctx->thread->GetIndexID());
1230 var_success = true;
1231 }
1232 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1233 {
1234 cstr = exe_ctx->thread->GetName();
1235 var_success = cstr && cstr[0];
1236 if (var_success)
1237 s.PutCString(cstr);
1238 }
1239 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1240 {
1241 cstr = exe_ctx->thread->GetQueueName();
1242 var_success = cstr && cstr[0];
1243 if (var_success)
1244 s.PutCString(cstr);
1245 }
1246 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1247 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001248 StopInfoSP stop_info_sp = exe_ctx->thread->GetStopInfo ();
1249 if (stop_info_sp)
Greg Clayton1b654882010-09-19 02:33:57 +00001250 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001251 cstr = stop_info_sp->GetDescription();
Greg Clayton1b654882010-09-19 02:33:57 +00001252 if (cstr && cstr[0])
1253 {
1254 s.PutCString(cstr);
1255 var_success = true;
1256 }
1257 }
1258 }
1259 }
1260 }
1261 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1262 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001263 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1264 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001265 {
Greg Clayton1b654882010-09-19 02:33:57 +00001266 var_name_begin += ::strlen ("target.");
1267 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1268 {
1269 ArchSpec arch (target->GetArchitecture ());
1270 if (arch.IsValid())
1271 {
Greg Clayton64195a22011-02-23 00:35:02 +00001272 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001273 var_success = true;
1274 }
1275 }
1276 }
1277 }
1278 break;
1279
1280
1281 case 'm':
1282 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1283 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001284 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001285 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001286 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001287 var_name_begin += ::strlen ("module.");
1288
1289 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1290 {
1291 if (module->GetFileSpec())
1292 {
1293 var_name_begin += ::strlen ("file.");
1294
1295 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1296 {
1297 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1298 var_success = format_file_spec;
1299 }
1300 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1301 {
1302 format_file_spec = module->GetFileSpec();
1303 var_success = format_file_spec;
1304 }
1305 }
1306 }
1307 }
1308 }
1309 break;
1310
1311
1312 case 'f':
1313 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1314 {
1315 if (sc && sc->comp_unit != NULL)
1316 {
1317 var_name_begin += ::strlen ("file.");
1318
1319 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1320 {
1321 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1322 var_success = format_file_spec;
1323 }
1324 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1325 {
1326 format_file_spec = *sc->comp_unit;
1327 var_success = format_file_spec;
1328 }
1329 }
1330 }
1331 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1332 {
1333 if (exe_ctx && exe_ctx->frame)
1334 {
1335 var_name_begin += ::strlen ("frame.");
1336 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1337 {
1338 s.Printf("%u", exe_ctx->frame->GetFrameIndex());
1339 var_success = true;
1340 }
1341 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1342 {
1343 reg_kind = eRegisterKindGeneric;
1344 reg_num = LLDB_REGNUM_GENERIC_PC;
1345 var_success = true;
1346 }
1347 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1348 {
1349 reg_kind = eRegisterKindGeneric;
1350 reg_num = LLDB_REGNUM_GENERIC_SP;
1351 var_success = true;
1352 }
1353 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1354 {
1355 reg_kind = eRegisterKindGeneric;
1356 reg_num = LLDB_REGNUM_GENERIC_FP;
1357 var_success = true;
1358 }
1359 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
1360 {
1361 reg_kind = eRegisterKindGeneric;
1362 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
1363 var_success = true;
1364 }
1365 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
1366 {
Greg Clayton5ccbd292011-01-06 22:15:06 +00001367 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001368 if (reg_ctx)
1369 {
1370 var_name_begin += ::strlen ("reg.");
1371 if (var_name_begin < var_name_end)
1372 {
1373 std::string reg_name (var_name_begin, var_name_end);
1374 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
1375 if (reg_info)
1376 var_success = true;
1377 }
1378 }
1379 }
1380 }
1381 }
1382 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
1383 {
1384 if (sc && (sc->function != NULL || sc->symbol != NULL))
1385 {
1386 var_name_begin += ::strlen ("function.");
1387 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1388 {
1389 if (sc->function)
1390 s.Printf("function{0x%8.8x}", sc->function->GetID());
1391 else
1392 s.Printf("symbol[%u]", sc->symbol->GetID());
1393
1394 var_success = true;
1395 }
1396 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1397 {
1398 if (sc->function)
1399 cstr = sc->function->GetName().AsCString (NULL);
1400 else if (sc->symbol)
1401 cstr = sc->symbol->GetName().AsCString (NULL);
1402 if (cstr)
1403 {
1404 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00001405
1406 if (sc->block)
1407 {
1408 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1409 if (inline_block)
1410 {
1411 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
1412 if (inline_info)
1413 {
1414 s.PutCString(" [inlined] ");
1415 inline_info->GetName().Dump(&s);
1416 }
1417 }
1418 }
Greg Clayton1b654882010-09-19 02:33:57 +00001419 var_success = true;
1420 }
1421 }
1422 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
1423 {
1424 var_success = addr != NULL;
1425 if (var_success)
1426 {
1427 format_addr = *addr;
1428 calculate_format_addr_function_offset = true;
1429 }
1430 }
1431 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
1432 {
1433 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
1434 if (var_success)
1435 {
1436 format_addr = sc->line_entry.range.GetBaseAddress();
1437 calculate_format_addr_function_offset = true;
1438 }
1439 }
1440 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
1441 {
1442 var_success = exe_ctx->frame;
1443 if (var_success)
1444 {
1445 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1446 calculate_format_addr_function_offset = true;
1447 }
1448 }
1449 }
1450 }
1451 break;
1452
1453 case 'l':
1454 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
1455 {
1456 if (sc && sc->line_entry.IsValid())
1457 {
1458 var_name_begin += ::strlen ("line.");
1459 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1460 {
1461 var_name_begin += ::strlen ("file.");
1462
1463 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1464 {
1465 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
1466 var_success = format_file_spec;
1467 }
1468 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1469 {
1470 format_file_spec = sc->line_entry.file;
1471 var_success = format_file_spec;
1472 }
1473 }
1474 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
1475 {
1476 var_success = true;
1477 s.Printf("%u", sc->line_entry.line);
1478 }
1479 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
1480 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
1481 {
1482 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
1483 if (var_success)
1484 {
1485 format_addr = sc->line_entry.range.GetBaseAddress();
1486 if (var_name_begin[0] == 'e')
1487 format_addr.Slide (sc->line_entry.range.GetByteSize());
1488 }
1489 }
1490 }
1491 }
1492 break;
1493 }
1494
1495 if (var_success)
1496 {
1497 // If format addr is valid, then we need to print an address
1498 if (reg_num != LLDB_INVALID_REGNUM)
1499 {
1500 // We have a register value to display...
1501 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
1502 {
1503 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1504 }
1505 else
1506 {
1507 if (reg_ctx == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +00001508 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001509
1510 if (reg_ctx)
1511 {
1512 if (reg_kind != kNumRegisterKinds)
1513 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
1514 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
1515 var_success = reg_info != NULL;
1516 }
1517 }
1518 }
1519
1520 if (reg_info != NULL)
1521 {
Greg Clayton7349bd92011-05-09 20:18:18 +00001522 RegisterValue reg_value;
1523 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
1524 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001525 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00001526 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00001527 }
1528 }
1529
1530 if (format_file_spec)
1531 {
1532 s << format_file_spec;
1533 }
1534
1535 // If format addr is valid, then we need to print an address
1536 if (format_addr.IsValid())
1537 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001538 var_success = false;
1539
Greg Clayton1b654882010-09-19 02:33:57 +00001540 if (calculate_format_addr_function_offset)
1541 {
1542 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00001543
Greg Clayton0603aa92010-10-04 01:05:56 +00001544 if (sc)
1545 {
1546 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00001547 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001548 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00001549 if (sc->block)
1550 {
1551 // Check to make sure we aren't in an inline
1552 // function. If we are, use the inline block
1553 // range that contains "format_addr" since
1554 // blocks can be discontiguous.
1555 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1556 AddressRange inline_range;
1557 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
1558 func_addr = inline_range.GetBaseAddress();
1559 }
1560 }
Greg Clayton0603aa92010-10-04 01:05:56 +00001561 else if (sc->symbol && sc->symbol->GetAddressRangePtr())
1562 func_addr = sc->symbol->GetAddressRangePtr()->GetBaseAddress();
1563 }
1564
1565 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00001566 {
1567 if (func_addr.GetSection() == format_addr.GetSection())
1568 {
1569 addr_t func_file_addr = func_addr.GetFileAddress();
1570 addr_t addr_file_addr = format_addr.GetFileAddress();
1571 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001572 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00001573 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001574 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001575 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00001576 }
1577 else
Greg Clayton0603aa92010-10-04 01:05:56 +00001578 {
1579 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1580 if (target)
1581 {
1582 addr_t func_load_addr = func_addr.GetLoadAddress (target);
1583 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
1584 if (addr_load_addr > func_load_addr)
1585 s.Printf(" + %llu", addr_load_addr - func_load_addr);
1586 else if (addr_load_addr < func_load_addr)
1587 s.Printf(" - %llu", func_load_addr - addr_load_addr);
1588 var_success = true;
1589 }
1590 }
Greg Clayton1b654882010-09-19 02:33:57 +00001591 }
1592 }
1593 else
1594 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001595 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00001596 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00001597 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
1598 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00001599 if (vaddr == LLDB_INVALID_ADDRESS)
1600 vaddr = format_addr.GetFileAddress ();
1601
1602 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00001603 {
Greg Clayton514487e2011-02-15 21:59:32 +00001604 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00001605 if (addr_width == 0)
1606 addr_width = 16;
1607 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001608 var_success = true;
1609 }
Greg Clayton1b654882010-09-19 02:33:57 +00001610 }
1611 }
1612 }
1613
1614 if (var_success == false)
1615 success = false;
1616 }
1617 p = var_name_end;
1618 }
1619 else
1620 break;
1621 }
1622 else
1623 {
1624 // We got a dollar sign with no '{' after it, it must just be a dollar sign
1625 s.PutChar(*p);
1626 }
1627 }
1628 else if (*p == '\\')
1629 {
1630 ++p; // skip the slash
1631 switch (*p)
1632 {
1633 case 'a': s.PutChar ('\a'); break;
1634 case 'b': s.PutChar ('\b'); break;
1635 case 'f': s.PutChar ('\f'); break;
1636 case 'n': s.PutChar ('\n'); break;
1637 case 'r': s.PutChar ('\r'); break;
1638 case 't': s.PutChar ('\t'); break;
1639 case 'v': s.PutChar ('\v'); break;
1640 case '\'': s.PutChar ('\''); break;
1641 case '\\': s.PutChar ('\\'); break;
1642 case '0':
1643 // 1 to 3 octal chars
1644 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001645 // Make a string that can hold onto the initial zero char,
1646 // up to 3 octal digits, and a terminating NULL.
1647 char oct_str[5] = { 0, 0, 0, 0, 0 };
1648
1649 int i;
1650 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
1651 oct_str[i] = p[i];
1652
1653 // We don't want to consume the last octal character since
1654 // the main for loop will do this for us, so we advance p by
1655 // one less than i (even if i is zero)
1656 p += i - 1;
1657 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
1658 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001659 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001660 char octal_char = octal_value;
1661 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00001662 }
Greg Clayton1b654882010-09-19 02:33:57 +00001663 }
1664 break;
1665
1666 case 'x':
1667 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00001668 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001669 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001670 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00001671
Greg Clayton0603aa92010-10-04 01:05:56 +00001672 // Make a string that can hold onto two hex chars plus a
1673 // NULL terminator
1674 char hex_str[3] = { 0,0,0 };
1675 hex_str[0] = *p;
1676 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001677 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001678 ++p; // Skip the first of the two hex chars
1679 hex_str[1] = *p;
1680 }
1681
1682 unsigned long hex_value = strtoul (hex_str, NULL, 16);
1683 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001684 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00001685 }
1686 else
1687 {
1688 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00001689 }
1690 break;
1691
1692 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00001693 // Just desensitize any other character by just printing what
1694 // came after the '\'
1695 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00001696 break;
1697
1698 }
1699
1700 }
1701 }
1702 if (end)
1703 *end = p;
1704 return success;
1705}
1706
Greg Clayton4a33d312011-06-23 17:59:56 +00001707
1708static FormatManager&
1709GetFormatManager() {
1710 static FormatManager g_format_manager;
1711 return g_format_manager;
1712}
1713
1714bool
Enrico Granata4becb372011-06-29 22:27:15 +00001715Debugger::ValueFormats::Get(ValueObject& vobj, ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001716{
Enrico Granata4becb372011-06-29 22:27:15 +00001717 return GetFormatManager().Value().Get(vobj,entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001718}
1719
1720void
Enrico Granata4becb372011-06-29 22:27:15 +00001721Debugger::ValueFormats::Add(const ConstString &type, const ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001722{
Enrico Granata4becb372011-06-29 22:27:15 +00001723 GetFormatManager().Value().Add(type.AsCString(),entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001724}
1725
1726bool
Enrico Granata4becb372011-06-29 22:27:15 +00001727Debugger::ValueFormats::Delete(const ConstString &type)
Greg Clayton4a33d312011-06-23 17:59:56 +00001728{
Enrico Granata4becb372011-06-29 22:27:15 +00001729 return GetFormatManager().Value().Delete(type.AsCString());
Greg Clayton4a33d312011-06-23 17:59:56 +00001730}
1731
1732void
Enrico Granata4becb372011-06-29 22:27:15 +00001733Debugger::ValueFormats::Clear()
Greg Clayton4a33d312011-06-23 17:59:56 +00001734{
Enrico Granata4becb372011-06-29 22:27:15 +00001735 GetFormatManager().Value().Clear();
1736}
1737
1738void
Enrico Granata0a3958e2011-07-02 00:25:22 +00001739Debugger::ValueFormats::LoopThrough(ValueFormat::ValueCallback callback, void* callback_baton)
Enrico Granata4becb372011-06-29 22:27:15 +00001740{
1741 GetFormatManager().Value().LoopThrough(callback, callback_baton);
1742}
1743
1744uint32_t
1745Debugger::ValueFormats::GetCurrentRevision()
1746{
1747 return GetFormatManager().GetCurrentRevision();
1748}
1749
Enrico Granata0a3958e2011-07-02 00:25:22 +00001750uint32_t
1751Debugger::ValueFormats::GetCount()
1752{
1753 return GetFormatManager().Value().GetCount();
1754}
Enrico Granata4becb372011-06-29 22:27:15 +00001755
1756bool
1757Debugger::SummaryFormats::Get(ValueObject& vobj, SummaryFormat::SharedPointer &entry)
1758{
1759 return GetFormatManager().Summary().Get(vobj,entry);
1760}
1761
1762void
1763Debugger::SummaryFormats::Add(const ConstString &type, const SummaryFormat::SharedPointer &entry)
1764{
1765 GetFormatManager().Summary().Add(type.AsCString(),entry);
1766}
1767
1768bool
1769Debugger::SummaryFormats::Delete(const ConstString &type)
1770{
1771 return GetFormatManager().Summary().Delete(type.AsCString());
1772}
1773
1774void
1775Debugger::SummaryFormats::Clear()
1776{
1777 GetFormatManager().Summary().Clear();
1778}
1779
1780void
Enrico Granata0a3958e2011-07-02 00:25:22 +00001781Debugger::SummaryFormats::LoopThrough(SummaryFormat::SummaryCallback callback, void* callback_baton)
Enrico Granata4becb372011-06-29 22:27:15 +00001782{
1783 GetFormatManager().Summary().LoopThrough(callback, callback_baton);
1784}
1785
1786uint32_t
1787Debugger::SummaryFormats::GetCurrentRevision()
1788{
1789 return GetFormatManager().GetCurrentRevision();
Greg Clayton4a33d312011-06-23 17:59:56 +00001790}
1791
Enrico Granata0a3958e2011-07-02 00:25:22 +00001792uint32_t
1793Debugger::SummaryFormats::GetCount()
1794{
1795 return GetFormatManager().Summary().GetCount();
1796}
1797
1798bool
1799Debugger::RegexSummaryFormats::Get(ValueObject& vobj, SummaryFormat::SharedPointer &entry)
1800{
1801 return GetFormatManager().RegexSummary().Get(vobj,entry);
1802}
1803
1804void
1805Debugger::RegexSummaryFormats::Add(const lldb::RegularExpressionSP &type, const SummaryFormat::SharedPointer &entry)
1806{
1807 GetFormatManager().RegexSummary().Add(type,entry);
1808}
1809
1810bool
1811Debugger::RegexSummaryFormats::Delete(const ConstString &type)
1812{
1813 return GetFormatManager().RegexSummary().Delete(type.AsCString());
1814}
1815
1816void
1817Debugger::RegexSummaryFormats::Clear()
1818{
1819 GetFormatManager().RegexSummary().Clear();
1820}
1821
1822void
1823Debugger::RegexSummaryFormats::LoopThrough(SummaryFormat::RegexSummaryCallback callback, void* callback_baton)
1824{
1825 GetFormatManager().RegexSummary().LoopThrough(callback, callback_baton);
1826}
1827
1828uint32_t
1829Debugger::RegexSummaryFormats::GetCurrentRevision()
1830{
1831 return GetFormatManager().GetCurrentRevision();
1832}
1833
1834uint32_t
1835Debugger::RegexSummaryFormats::GetCount()
1836{
1837 return GetFormatManager().RegexSummary().GetCount();
1838}
1839
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001840bool
1841Debugger::NamedSummaryFormats::Get(const ConstString &type, SummaryFormat::SharedPointer &entry)
1842{
1843 return GetFormatManager().NamedSummary().Get(type.AsCString(),entry);
1844}
1845
1846void
1847Debugger::NamedSummaryFormats::Add(const ConstString &type, const SummaryFormat::SharedPointer &entry)
1848{
1849 GetFormatManager().NamedSummary().Add(type.AsCString(),entry);
1850}
1851
1852bool
1853Debugger::NamedSummaryFormats::Delete(const ConstString &type)
1854{
1855 return GetFormatManager().NamedSummary().Delete(type.AsCString());
1856}
1857
1858void
1859Debugger::NamedSummaryFormats::Clear()
1860{
1861 GetFormatManager().NamedSummary().Clear();
1862}
1863
1864void
1865Debugger::NamedSummaryFormats::LoopThrough(SummaryFormat::SummaryCallback callback, void* callback_baton)
1866{
1867 GetFormatManager().NamedSummary().LoopThrough(callback, callback_baton);
1868}
1869
1870uint32_t
1871Debugger::NamedSummaryFormats::GetCurrentRevision()
1872{
1873 return GetFormatManager().GetCurrentRevision();
1874}
1875
1876uint32_t
1877Debugger::NamedSummaryFormats::GetCount()
1878{
1879 return GetFormatManager().NamedSummary().GetCount();
1880}
1881
Greg Clayton1b654882010-09-19 02:33:57 +00001882#pragma mark Debugger::SettingsController
1883
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001884//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00001885// class Debugger::SettingsController
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001886//--------------------------------------------------
1887
Greg Clayton1b654882010-09-19 02:33:57 +00001888Debugger::SettingsController::SettingsController () :
Caroline Tice101c7c22010-09-09 06:25:08 +00001889 UserSettingsController ("", lldb::UserSettingsControllerSP())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001890{
Caroline Tice91123da2010-09-08 17:48:55 +00001891 m_default_settings.reset (new DebuggerInstanceSettings (*this, false,
1892 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001893}
1894
Greg Clayton1b654882010-09-19 02:33:57 +00001895Debugger::SettingsController::~SettingsController ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001896{
1897}
1898
1899
1900lldb::InstanceSettingsSP
Greg Clayton1b654882010-09-19 02:33:57 +00001901Debugger::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001902{
Greg Claytondbe54502010-11-19 03:46:01 +00001903 DebuggerInstanceSettings *new_settings = new DebuggerInstanceSettings (*GetSettingsController(),
Caroline Tice91123da2010-09-08 17:48:55 +00001904 false, instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001905 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1906 return new_settings_sp;
1907}
1908
Greg Clayton1b654882010-09-19 02:33:57 +00001909#pragma mark DebuggerInstanceSettings
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001910//--------------------------------------------------
1911// class DebuggerInstanceSettings
1912//--------------------------------------------------
1913
Greg Claytona7015092010-09-18 01:14:36 +00001914DebuggerInstanceSettings::DebuggerInstanceSettings
1915(
1916 UserSettingsController &owner,
1917 bool live_instance,
1918 const char *name
1919) :
Greg Clayton85851dd2010-12-04 00:10:17 +00001920 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytona7015092010-09-18 01:14:36 +00001921 m_term_width (80),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001922 m_prompt (),
Greg Clayton0603aa92010-10-04 01:05:56 +00001923 m_frame_format (),
1924 m_thread_format (),
Caroline Ticedaccaa92010-09-20 20:44:43 +00001925 m_script_lang (),
Jim Ingham3bcdb292010-10-04 22:44:14 +00001926 m_use_external_editor (false),
1927 m_auto_confirm_on (false)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001928{
Caroline Ticef20e8232010-09-09 18:26:37 +00001929 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1930 // until the vtables for DebuggerInstanceSettings are properly set up, i.e. AFTER all the initializers.
1931 // 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 +00001932 // The same is true of CreateInstanceName().
1933
1934 if (GetInstanceName() == InstanceSettings::InvalidName())
1935 {
1936 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1937 m_owner.RegisterInstanceSettings (this);
1938 }
Caroline Ticef20e8232010-09-09 18:26:37 +00001939
1940 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001941 {
1942 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1943 CopyInstanceSettings (pending_settings, false);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001944 }
1945}
1946
1947DebuggerInstanceSettings::DebuggerInstanceSettings (const DebuggerInstanceSettings &rhs) :
Greg Clayton99d0faf2010-11-18 23:32:35 +00001948 InstanceSettings (*Debugger::GetSettingsController(), CreateInstanceName ().AsCString()),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001949 m_prompt (rhs.m_prompt),
Greg Clayton0603aa92010-10-04 01:05:56 +00001950 m_frame_format (rhs.m_frame_format),
1951 m_thread_format (rhs.m_thread_format),
Caroline Ticedaccaa92010-09-20 20:44:43 +00001952 m_script_lang (rhs.m_script_lang),
Jim Ingham3bcdb292010-10-04 22:44:14 +00001953 m_use_external_editor (rhs.m_use_external_editor),
1954 m_auto_confirm_on(rhs.m_auto_confirm_on)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001955{
1956 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1957 CopyInstanceSettings (pending_settings, false);
1958 m_owner.RemovePendingSettings (m_instance_name);
1959}
1960
1961DebuggerInstanceSettings::~DebuggerInstanceSettings ()
1962{
1963}
1964
1965DebuggerInstanceSettings&
1966DebuggerInstanceSettings::operator= (const DebuggerInstanceSettings &rhs)
1967{
1968 if (this != &rhs)
1969 {
Greg Clayton1b654882010-09-19 02:33:57 +00001970 m_term_width = rhs.m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001971 m_prompt = rhs.m_prompt;
Greg Clayton0603aa92010-10-04 01:05:56 +00001972 m_frame_format = rhs.m_frame_format;
1973 m_thread_format = rhs.m_thread_format;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001974 m_script_lang = rhs.m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00001975 m_use_external_editor = rhs.m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00001976 m_auto_confirm_on = rhs.m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001977 }
1978
1979 return *this;
1980}
1981
Greg Clayton1b654882010-09-19 02:33:57 +00001982bool
1983DebuggerInstanceSettings::ValidTermWidthValue (const char *value, Error err)
1984{
1985 bool valid = false;
1986
1987 // Verify we have a value string.
1988 if (value == NULL || value[0] == '\0')
1989 {
1990 err.SetErrorString ("Missing value. Can't set terminal width without a value.\n");
1991 }
1992 else
1993 {
1994 char *end = NULL;
1995 const uint32_t width = ::strtoul (value, &end, 0);
1996
Johnny Chenea9fc182010-09-20 16:36:43 +00001997 if (end && end[0] == '\0')
Greg Clayton1b654882010-09-19 02:33:57 +00001998 {
Johnny Chen433d7742010-09-20 17:04:41 +00001999 if (width >= 10 && width <= 1024)
Greg Clayton1b654882010-09-19 02:33:57 +00002000 valid = true;
2001 else
2002 err.SetErrorString ("Invalid term-width value; value must be between 10 and 1024.\n");
2003 }
2004 else
2005 err.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string.\n", value);
2006 }
2007
2008 return valid;
2009}
2010
2011
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002012void
2013DebuggerInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2014 const char *index_value,
2015 const char *value,
2016 const ConstString &instance_name,
2017 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:54 +00002018 VarSetOperationType op,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002019 Error &err,
2020 bool pending)
2021{
Greg Clayton0603aa92010-10-04 01:05:56 +00002022
2023 if (var_name == TermWidthVarName())
2024 {
2025 if (ValidTermWidthValue (value, err))
2026 {
2027 m_term_width = ::strtoul (value, NULL, 0);
2028 }
2029 }
2030 else if (var_name == PromptVarName())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002031 {
Caroline Tice101c7c22010-09-09 06:25:08 +00002032 UserSettingsController::UpdateStringVariable (op, m_prompt, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002033 if (!pending)
2034 {
Caroline Tice49e27372010-09-07 18:35:40 +00002035 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2036 // strip off the brackets before passing it to BroadcastPromptChange.
2037
2038 std::string tmp_instance_name (instance_name.AsCString());
2039 if ((tmp_instance_name[0] == '[')
2040 && (tmp_instance_name[instance_name.GetLength() - 1] == ']'))
2041 tmp_instance_name = tmp_instance_name.substr (1, instance_name.GetLength() - 2);
2042 ConstString new_name (tmp_instance_name.c_str());
2043
2044 BroadcastPromptChange (new_name, m_prompt.c_str());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002045 }
2046 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002047 else if (var_name == GetFrameFormatName())
2048 {
2049 UserSettingsController::UpdateStringVariable (op, m_frame_format, value, err);
2050 }
2051 else if (var_name == GetThreadFormatName())
2052 {
2053 UserSettingsController::UpdateStringVariable (op, m_thread_format, value, err);
2054 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002055 else if (var_name == ScriptLangVarName())
2056 {
2057 bool success;
2058 m_script_lang = Args::StringToScriptLanguage (value, eScriptLanguageDefault,
2059 &success);
2060 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002061 else if (var_name == UseExternalEditorVarName ())
2062 {
Greg Clayton385aa282011-04-22 03:55:06 +00002063 UserSettingsController::UpdateBooleanVariable (op, m_use_external_editor, value, false, err);
Caroline Ticedaccaa92010-09-20 20:44:43 +00002064 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002065 else if (var_name == AutoConfirmName ())
2066 {
Greg Clayton385aa282011-04-22 03:55:06 +00002067 UserSettingsController::UpdateBooleanVariable (op, m_auto_confirm_on, value, false, err);
Jim Ingham3bcdb292010-10-04 22:44:14 +00002068 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002069}
2070
Caroline Tice12cecd72010-09-20 21:37:42 +00002071bool
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002072DebuggerInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2073 const ConstString &var_name,
Caroline Ticedaccaa92010-09-20 20:44:43 +00002074 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42 +00002075 Error *err)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002076{
2077 if (var_name == PromptVarName())
2078 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002079 value.AppendString (m_prompt.c_str(), m_prompt.size());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002080
2081 }
2082 else if (var_name == ScriptLangVarName())
2083 {
2084 value.AppendString (ScriptInterpreter::LanguageToString (m_script_lang).c_str());
2085 }
Caroline Tice101c7c22010-09-09 06:25:08 +00002086 else if (var_name == TermWidthVarName())
2087 {
2088 StreamString width_str;
2089 width_str.Printf ("%d", m_term_width);
2090 value.AppendString (width_str.GetData());
2091 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002092 else if (var_name == GetFrameFormatName ())
2093 {
2094 value.AppendString(m_frame_format.c_str(), m_frame_format.size());
2095 }
2096 else if (var_name == GetThreadFormatName ())
2097 {
2098 value.AppendString(m_thread_format.c_str(), m_thread_format.size());
2099 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002100 else if (var_name == UseExternalEditorVarName())
2101 {
2102 if (m_use_external_editor)
2103 value.AppendString ("true");
2104 else
2105 value.AppendString ("false");
2106 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002107 else if (var_name == AutoConfirmName())
2108 {
2109 if (m_auto_confirm_on)
2110 value.AppendString ("true");
2111 else
2112 value.AppendString ("false");
2113 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002114 else
Caroline Tice12cecd72010-09-20 21:37:42 +00002115 {
2116 if (err)
2117 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2118 return false;
2119 }
2120 return true;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002121}
2122
2123void
2124DebuggerInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
2125 bool pending)
2126{
2127 if (new_settings.get() == NULL)
2128 return;
2129
2130 DebuggerInstanceSettings *new_debugger_settings = (DebuggerInstanceSettings *) new_settings.get();
2131
2132 m_prompt = new_debugger_settings->m_prompt;
2133 if (!pending)
Caroline Tice49e27372010-09-07 18:35:40 +00002134 {
2135 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2136 // strip off the brackets before passing it to BroadcastPromptChange.
2137
2138 std::string tmp_instance_name (m_instance_name.AsCString());
2139 if ((tmp_instance_name[0] == '[')
2140 && (tmp_instance_name[m_instance_name.GetLength() - 1] == ']'))
2141 tmp_instance_name = tmp_instance_name.substr (1, m_instance_name.GetLength() - 2);
2142 ConstString new_name (tmp_instance_name.c_str());
2143
2144 BroadcastPromptChange (new_name, m_prompt.c_str());
2145 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002146 m_frame_format = new_debugger_settings->m_frame_format;
2147 m_thread_format = new_debugger_settings->m_thread_format;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002148 m_term_width = new_debugger_settings->m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002149 m_script_lang = new_debugger_settings->m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002150 m_use_external_editor = new_debugger_settings->m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002151 m_auto_confirm_on = new_debugger_settings->m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002152}
2153
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002154
2155bool
2156DebuggerInstanceSettings::BroadcastPromptChange (const ConstString &instance_name, const char *new_prompt)
2157{
2158 std::string tmp_prompt;
2159
2160 if (new_prompt != NULL)
2161 {
2162 tmp_prompt = new_prompt ;
2163 int len = tmp_prompt.size();
2164 if (len > 1
2165 && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"')
2166 && (tmp_prompt[len-1] == tmp_prompt[0]))
2167 {
2168 tmp_prompt = tmp_prompt.substr(1,len-2);
2169 }
2170 len = tmp_prompt.size();
2171 if (tmp_prompt[len-1] != ' ')
2172 tmp_prompt.append(" ");
2173 }
2174 EventSP new_event_sp;
2175 new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt,
2176 new EventDataBytes (tmp_prompt.c_str())));
2177
2178 if (instance_name.GetLength() != 0)
2179 {
2180 // Set prompt for a particular instance.
2181 Debugger *dbg = Debugger::FindDebuggerWithInstanceName (instance_name).get();
2182 if (dbg != NULL)
2183 {
2184 dbg->GetCommandInterpreter().BroadcastEvent (new_event_sp);
2185 }
2186 }
2187
2188 return true;
2189}
2190
2191const ConstString
2192DebuggerInstanceSettings::CreateInstanceName ()
2193{
2194 static int instance_count = 1;
2195 StreamString sstr;
2196
2197 sstr.Printf ("debugger_%d", instance_count);
2198 ++instance_count;
2199
2200 const ConstString ret_val (sstr.GetData());
2201
2202 return ret_val;
2203}
2204
2205const ConstString &
2206DebuggerInstanceSettings::PromptVarName ()
2207{
2208 static ConstString prompt_var_name ("prompt");
2209
2210 return prompt_var_name;
2211}
2212
2213const ConstString &
Greg Clayton0603aa92010-10-04 01:05:56 +00002214DebuggerInstanceSettings::GetFrameFormatName ()
2215{
2216 static ConstString prompt_var_name ("frame-format");
2217
2218 return prompt_var_name;
2219}
2220
2221const ConstString &
2222DebuggerInstanceSettings::GetThreadFormatName ()
2223{
2224 static ConstString prompt_var_name ("thread-format");
2225
2226 return prompt_var_name;
2227}
2228
2229const ConstString &
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002230DebuggerInstanceSettings::ScriptLangVarName ()
2231{
2232 static ConstString script_lang_var_name ("script-lang");
2233
2234 return script_lang_var_name;
2235}
2236
Caroline Tice101c7c22010-09-09 06:25:08 +00002237const ConstString &
2238DebuggerInstanceSettings::TermWidthVarName ()
2239{
2240 static ConstString term_width_var_name ("term-width");
2241
2242 return term_width_var_name;
2243}
2244
Caroline Ticedaccaa92010-09-20 20:44:43 +00002245const ConstString &
2246DebuggerInstanceSettings::UseExternalEditorVarName ()
2247{
2248 static ConstString use_external_editor_var_name ("use-external-editor");
2249
2250 return use_external_editor_var_name;
2251}
2252
Jim Ingham3bcdb292010-10-04 22:44:14 +00002253const ConstString &
2254DebuggerInstanceSettings::AutoConfirmName ()
2255{
2256 static ConstString use_external_editor_var_name ("auto-confirm");
2257
2258 return use_external_editor_var_name;
2259}
2260
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002261//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00002262// SettingsController Variable Tables
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002263//--------------------------------------------------
2264
2265
2266SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002267Debugger::SettingsController::global_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002268{
2269 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
Caroline Tice101c7c22010-09-09 06:25:08 +00002270 // The Debugger level global table should always be empty; all Debugger settable variables should be instance
2271 // variables.
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002272 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2273};
2274
Greg Claytonbb562b12010-10-04 02:44:26 +00002275#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name}${function.pc-offset}}}"
Greg Clayton0603aa92010-10-04 01:05:56 +00002276#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002277
Greg Clayton0603aa92010-10-04 01:05:56 +00002278#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2279 "{, ${frame.pc}}"\
2280 MODULE_WITH_FUNC\
Greg Claytoncf4b9072010-10-04 17:04:23 +00002281 FILE_AND_LINE\
Greg Clayton0603aa92010-10-04 01:05:56 +00002282 "{, stop reason = ${thread.stop-reason}}"\
Greg Clayton0603aa92010-10-04 01:05:56 +00002283 "\\n"
2284
Greg Clayton315d2ca2010-11-02 01:53:21 +00002285//#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2286// "{, ${frame.pc}}"\
2287// MODULE_WITH_FUNC\
2288// FILE_AND_LINE\
2289// "{, stop reason = ${thread.stop-reason}}"\
2290// "{, name = ${thread.name}}"\
2291// "{, queue = ${thread.queue}}"\
2292// "\\n"
2293
Greg Clayton0603aa92010-10-04 01:05:56 +00002294#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
2295 MODULE_WITH_FUNC\
2296 FILE_AND_LINE\
2297 "\\n"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002298
2299SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002300Debugger::SettingsController::instance_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002301{
Greg Clayton0603aa92010-10-04 01:05:56 +00002302// NAME Setting variable type Default Enum Init'd Hidden Help
2303// ======================= ======================= ====================== ==== ====== ====== ======================
Greg Clayton0603aa92010-10-04 01:05:56 +00002304{ "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 +00002305{ "prompt", eSetVarTypeString, "(lldb) ", NULL, false, false, "The debugger command line prompt displayed for the user." },
Jim Ingham3bcdb292010-10-04 22:44:14 +00002306{ "script-lang", eSetVarTypeString, "python", NULL, false, false, "The script language to be used for evaluating user-written scripts." },
2307{ "term-width", eSetVarTypeInt, "80" , NULL, false, false, "The maximum number of columns to use for displaying text." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002308{ "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 +00002309{ "use-external-editor", eSetVarTypeBoolean, "false", NULL, false, false, "Whether to use an external editor or not." },
2310{ "auto-confirm", eSetVarTypeBoolean, "false", NULL, false, false, "If true all confirmation prompts will receive their default reply." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002311{ NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002312};