blob: 39ac87abc625bafd21cad3cf8154ad9c61c6430b [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
697bool
698Debugger::FormatPrompt
699(
700 const char *format,
701 const SymbolContext *sc,
702 const ExecutionContext *exe_ctx,
703 const Address *addr,
704 Stream &s,
Enrico Granata4becb372011-06-29 22:27:15 +0000705 const char **end,
706 ValueObject* vobj
Greg Clayton1b654882010-09-19 02:33:57 +0000707)
708{
Enrico Granata4becb372011-06-29 22:27:15 +0000709 ValueObject* realvobj = NULL; // makes it super-easy to parse pointers
Greg Clayton1b654882010-09-19 02:33:57 +0000710 bool success = true;
711 const char *p;
712 for (p = format; *p != '\0'; ++p)
713 {
Enrico Granata4becb372011-06-29 22:27:15 +0000714 if(realvobj)
715 {
716 vobj = realvobj;
717 realvobj = NULL;
718 }
Greg Clayton1b654882010-09-19 02:33:57 +0000719 size_t non_special_chars = ::strcspn (p, "${}\\");
720 if (non_special_chars > 0)
721 {
722 if (success)
723 s.Write (p, non_special_chars);
724 p += non_special_chars;
725 }
726
727 if (*p == '\0')
728 {
729 break;
730 }
731 else if (*p == '{')
732 {
733 // Start a new scope that must have everything it needs if it is to
734 // to make it into the final output stream "s". If you want to make
735 // a format that only prints out the function or symbol name if there
736 // is one in the symbol context you can use:
737 // "{function =${function.name}}"
738 // The first '{' starts a new scope that end with the matching '}' at
739 // the end of the string. The contents "function =${function.name}"
740 // will then be evaluated and only be output if there is a function
741 // or symbol with a valid name.
742 StreamString sub_strm;
743
744 ++p; // Skip the '{'
745
Enrico Granata4becb372011-06-29 22:27:15 +0000746 if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, vobj))
Greg Clayton1b654882010-09-19 02:33:57 +0000747 {
748 // The stream had all it needed
749 s.Write(sub_strm.GetData(), sub_strm.GetSize());
750 }
751 if (*p != '}')
752 {
753 success = false;
754 break;
755 }
756 }
757 else if (*p == '}')
758 {
759 // End of a enclosing scope
760 break;
761 }
762 else if (*p == '$')
763 {
764 // We have a prompt variable to print
765 ++p;
766 if (*p == '{')
767 {
768 ++p;
769 const char *var_name_begin = p;
770 const char *var_name_end = ::strchr (p, '}');
771
772 if (var_name_end && var_name_begin < var_name_end)
773 {
774 // if we have already failed to parse, skip this variable
775 if (success)
776 {
777 const char *cstr = NULL;
778 Address format_addr;
779 bool calculate_format_addr_function_offset = false;
780 // Set reg_kind and reg_num to invalid values
781 RegisterKind reg_kind = kNumRegisterKinds;
782 uint32_t reg_num = LLDB_INVALID_REGNUM;
783 FileSpec format_file_spec;
Greg Claytone0d378b2011-03-24 21:19:54 +0000784 const RegisterInfo *reg_info = NULL;
Greg Clayton1b654882010-09-19 02:33:57 +0000785 RegisterContext *reg_ctx = NULL;
786
787 // Each variable must set success to true below...
788 bool var_success = false;
789 switch (var_name_begin[0])
790 {
Enrico Granata4becb372011-06-29 22:27:15 +0000791 case '*':
792 {
793 if (!vobj) break;
Enrico Granata4becb372011-06-29 22:27:15 +0000794 lldb::clang_type_t pointer_clang_type = vobj->GetClangType();
795 clang_type_t elem_or_pointee_clang_type;
796 const Flags type_flags (ClangASTContext::GetTypeInfo (pointer_clang_type,
797 vobj->GetClangAST(),
798 &elem_or_pointee_clang_type));
Enrico Granata0a3958e2011-07-02 00:25:22 +0000799 bool is_pointer = type_flags.Test (ClangASTContext::eTypeIsPointer),
800 is_array = type_flags.Test (ClangASTContext::eTypeIsArray);
801 if ( is_array ||
802 ( is_pointer && ::strchr(var_name_begin,'[') && ::strchr(var_name_begin,'[') < var_name_end )
803 )
Enrico Granata4becb372011-06-29 22:27:15 +0000804 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000805 const char* var_name_final;
806 char* close_bracket_position = NULL;
807 const char* percent_position = NULL;
808 const char* targetvalue;
809 lldb::Format custom_format = eFormatInvalid;
810 int index_lower = -1;
811 int index_higher = -1;
812 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eDisplaySummary;
813 {
814 percent_position = ::strchr(var_name_begin,'%');
815 if(!percent_position || percent_position > var_name_end)
816 var_name_final = var_name_end;
817 else
818 {
819 var_name_final = percent_position;
820 char* format_name = new char[var_name_end-var_name_final]; format_name[var_name_end-var_name_final-1] = '\0';
821 memcpy(format_name, var_name_final+1, var_name_end-var_name_final-1);
822 if ( !FormatManager::GetFormatFromCString(format_name,
823 true,
824 custom_format) )
825 {
826 // if this is an @ sign, print ObjC description
827 if(*format_name == '@')
828 val_obj_display = ValueObject::eDisplayLanguageSpecific;
829 // if this is a V, print the value using the default format
830 if(*format_name == 'V')
831 val_obj_display = ValueObject::eDisplayValue;
832 }
833 // a good custom format tells us to print the value using it
834 else
835 val_obj_display = ValueObject::eDisplayValue;
836 }
837 }
838
839 {
840 const char* open_bracket_position = ::strchr(var_name_begin,'[');
841 if(open_bracket_position && open_bracket_position < var_name_final)
842 {
843 // TODO: pick a way to say "all entries". this will make more sense once
844 // regex typenames are in place. now, you need to be size-aware anyways
845 char* separator_position = ::strchr(open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
846 close_bracket_position = ::strchr(open_bracket_position,']');
847 // as usual, we assume that [] will come before %
848 //printf("trying to expand a []\n");
849 var_name_final = open_bracket_position;
850 if(close_bracket_position - open_bracket_position == 1)
851 {
852 if(is_array)
853 {
854 index_lower = 0;
855 index_higher = vobj->GetNumChildren() - 1;
856 }
857 else
858 break; // cannot auto-determine size for pointers
859 }
860 else if (separator_position == NULL || separator_position > var_name_end)
861 {
862 char *end = NULL;
863 index_lower = ::strtoul (open_bracket_position+1, &end, 0);
864 index_higher = index_lower;
865 //printf("got to read low=%d high same\n",bitfield_lower);
866 }
867 else if(close_bracket_position && close_bracket_position < var_name_end)
868 {
869 char *end = NULL;
870 index_lower = ::strtoul (open_bracket_position+1, &end, 0);
871 index_higher = ::strtoul (separator_position+1, &end, 0);
872 //printf("got to read low=%d high=%d\n",bitfield_lower,bitfield_higher);
873 }
874 else
875 break;
876 if (index_lower > index_higher)
877 {
878 int temp = index_lower;
879 index_lower = index_higher;
880 index_higher = temp;
881 }
882 //*((char*)open_bracket_position) = '\0';
883 //printf("variable name is %s\n",var_name_begin);
884 //*((char*)open_bracket_position) = '[';
885 }
886 }
887
888 // if you just type a range, lldb will do the "right thing" in picking
889 // a reasonable display for the array entries. you can override this by
890 // giving other input (e.g. ${*var[1-3].member1%uint8_t[]}) and they
891 // will be honored
892 char* special_directions = NULL;
893 if (close_bracket_position && (var_name_end-close_bracket_position > 1))
894 {
895 int base_len = var_name_end-close_bracket_position;
896 special_directions = new char[8+base_len];
897 special_directions[0] = '$';
898 special_directions[1] = '{';
899 special_directions[2] = 'v';
900 special_directions[3] = 'a';
901 special_directions[4] = 'r';
902 memcpy(special_directions+5, close_bracket_position+1, base_len);
903 special_directions[base_len+7] = '\0';
904 printf("%s\n",special_directions);
905 }
906
907 // let us display items index_lower thru index_higher of this array
908 s.PutChar('[');
909 var_success = true;
910 const char* expr_path = NULL;
911 const char* ptr_deref_format = "%s[%d]";
912 char* ptr_deref_buffer = new char[1024];
913 StreamString expr_path_string;
914
915 if(is_pointer)
916 {
917 vobj->GetExpressionPath(expr_path_string, true, ValueObject::eHonorPointers);
918 expr_path = expr_path_string.GetData();
919 }
920
921 for(;index_lower<=index_higher;index_lower++)
922 {
923 ValueObject* item;
924
925 if(is_array)
926 item = vobj->GetChildAtIndex(index_lower, true).get();
927 else
928 {
929#ifdef VERBOSE_FORMATPROMPT_OUTPUT
930 printf("name to deref in phase 0: %s\n",expr_path);
931#endif //VERBOSE_FORMATPROMPT_OUTPUT
932 ::sprintf(ptr_deref_buffer, ptr_deref_format, expr_path, index_lower);
933#ifdef VERBOSE_FORMATPROMPT_OUTPUT
934 printf("name to deref in phase 1: %s\n",ptr_deref_buffer);
935#endif //VERBOSE_FORMATPROMPT_OUTPUT
936 lldb::VariableSP var_sp;
937 Error error;
938 item = exe_ctx->frame->GetValueForVariableExpressionPath (ptr_deref_buffer,
939 eNoDynamicValues,
940 0,
941 var_sp,
942 error).get();
943 if (error.Fail())
944 {
945#ifdef VERBOSE_FORMATPROMPT_OUTPUT
946 printf("ERROR: %s\n",error.AsCString("unknown"));
947#endif //VERBOSE_FORMATPROMPT_OUTPUT
948 break;
949 }
950 }
951
952 if (!special_directions)
953 {
954 targetvalue = item->GetPrintableRepresentation(val_obj_display, custom_format);
955 if(targetvalue)
956 s.PutCString(targetvalue);
957 var_success &= (targetvalue != NULL);
958 if(custom_format != eFormatInvalid)
959 item->SetFormat(eFormatDefault);
960 }
961 else
962 {
963 var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
964 }
965
966 if(index_lower < index_higher)
967 s.PutChar(',');
968 }
969 s.PutChar(']');
970 break;
971
972 }
973 else if (is_pointer)
974 {
975 var_name_begin++;
976 uint32_t offset = 0;
977 DataExtractor read_for_null = vobj->GetDataExtractor();
978 if (read_for_null.GetPointer(&offset) == 0)
979 break;
980 if (ClangASTContext::IsAggregateType (elem_or_pointee_clang_type) )
981 {
982 Error error;
983 realvobj = vobj;
984 vobj = vobj->Dereference(error).get();
985 if(!vobj || error.Fail())
986 break;
987 }
988 else if (ClangASTContext::IsCharType (elem_or_pointee_clang_type))
Enrico Granata4becb372011-06-29 22:27:15 +0000989 {
990 StreamString sstr;
991 ExecutionContextScope *exe_scope = vobj->GetExecutionContextScope();
992 Process *process = exe_scope->CalculateProcess();
993 if(!process) break;
994 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
995 AddressType cstr_address_type = eAddressTypeInvalid;
996 DataExtractor data;
997 size_t bytes_read = 0;
998 std::vector<char> data_buffer;
999 Error error;
1000 cstr_address = vobj->GetPointerValue (cstr_address_type, true);
1001 {
1002 const size_t k_max_buf_size = 256;
1003 data_buffer.resize (k_max_buf_size + 1);
1004 // NULL terminate in case we don't get the entire C string
1005 data_buffer.back() = '\0';
1006
1007 sstr << '"';
1008
1009 data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
1010 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
1011 {
1012 size_t len = strlen(&data_buffer.front());
1013 if (len == 0)
1014 break;
1015 if (len > bytes_read)
1016 len = bytes_read;
1017
1018 data.Dump (&sstr,
1019 0, // Start offset in "data"
1020 eFormatCharArray, // Print as characters
1021 1, // Size of item (1 byte for a char!)
1022 len, // How many bytes to print?
1023 UINT32_MAX, // num per line
1024 LLDB_INVALID_ADDRESS,// base address
1025 0, // bitfield bit size
1026 0); // bitfield bit offset
1027
1028 if (len < k_max_buf_size)
1029 break;
1030 cstr_address += k_max_buf_size;
1031 }
1032 sstr << '"';
1033 s.PutCString(sstr.GetData());
1034 var_success = true;
1035 break;
1036 }
1037 }
Enrico Granata0a3958e2011-07-02 00:25:22 +00001038 else /*some other pointer type*/
Enrico Granata4becb372011-06-29 22:27:15 +00001039 {
1040 Error error;
1041 realvobj = vobj;
1042 vobj = vobj->Dereference(error).get();
1043 if(!vobj || error.Fail())
1044 break;
1045 }
1046 }
1047 else
1048 break;
1049 }
1050 case 'v':
1051 {
1052 const char* targetvalue;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001053 ValueObject::ValueObjectRepresentationStyle val_obj_display = ValueObject::eDisplaySummary;
Enrico Granata4becb372011-06-29 22:27:15 +00001054 ValueObject* target;
1055 lldb::Format custom_format = eFormatInvalid;
1056 int bitfield_lower = -1;
1057 int bitfield_higher = -1;
1058 if (!vobj) break;
1059 // simplest case ${var}, just print vobj's value
1060 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
Enrico Granata0a3958e2011-07-02 00:25:22 +00001061 {
Enrico Granata4becb372011-06-29 22:27:15 +00001062 target = vobj;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001063 val_obj_display = ValueObject::eDisplayValue;
1064 }
Enrico Granata4becb372011-06-29 22:27:15 +00001065 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
1066 {
1067 // this is a variable with some custom format applied to it
1068 const char* var_name_final;
1069 target = vobj;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001070 val_obj_display = ValueObject::eDisplayValue;
Enrico Granata4becb372011-06-29 22:27:15 +00001071 {
1072 const char* percent_position = ::strchr(var_name_begin,'%'); // TODO: make this a constant
1073 //if(!percent_position || percent_position > var_name_end)
1074 // var_name_final = var_name_end;
1075 //else
1076 //{
1077 var_name_final = percent_position;
1078 char* format_name = new char[var_name_end-var_name_final]; format_name[var_name_end-var_name_final-1] = '\0';
1079 memcpy(format_name, var_name_final+1, var_name_end-var_name_final-1);
Enrico Granata0a3958e2011-07-02 00:25:22 +00001080 if ( !FormatManager::GetFormatFromCString(format_name,
Enrico Granata4becb372011-06-29 22:27:15 +00001081 true,
Enrico Granata0a3958e2011-07-02 00:25:22 +00001082 custom_format) )
1083 {
1084 // if this is an @ sign, print ObjC description
1085 if(*format_name == '@')
1086 val_obj_display = ValueObject::eDisplayLanguageSpecific;
1087 }
Enrico Granata4becb372011-06-29 22:27:15 +00001088 delete format_name;
1089 //}
1090 }
1091 }
1092 else if (::strncmp(var_name_begin,"var[",strlen("var[")) == 0)
1093 {
1094 // this is a bitfield variable
1095 const char *var_name_final;
1096 target = vobj;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001097 val_obj_display = ValueObject::eDisplayValue;
Enrico Granata4becb372011-06-29 22:27:15 +00001098 {
1099 const char* percent_position = ::strchr(var_name_begin,'%');
1100 if(!percent_position || percent_position > var_name_end)
1101 var_name_final = var_name_end;
1102 else
1103 {
1104 var_name_final = percent_position;
1105 char* format_name = new char[var_name_end-var_name_final]; format_name[var_name_end-var_name_final-1] = '\0';
1106 memcpy(format_name, var_name_final+1, var_name_end-var_name_final-1);
Enrico Granata0a3958e2011-07-02 00:25:22 +00001107 if ( !FormatManager::GetFormatFromCString(format_name,
1108 true,
1109 custom_format) )
1110 {
1111 delete format_name;
1112 break;
1113 }
1114 else
1115 delete format_name;
Enrico Granata4becb372011-06-29 22:27:15 +00001116 }
1117 }
1118
1119 {
1120 // code here might be simpler than in the case below
1121 const char* open_bracket_position = ::strchr(var_name_begin,'[');
1122 if(open_bracket_position && open_bracket_position < var_name_final)
1123 {
1124 char* separator_position = ::strchr(open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1125 char* close_bracket_position = ::strchr(open_bracket_position,']');
1126 // as usual, we assume that [] will come before %
1127 //printf("trying to expand a []\n");
1128 var_name_final = open_bracket_position;
1129 if (separator_position == NULL || separator_position > var_name_end)
1130 {
1131 char *end = NULL;
1132 bitfield_lower = ::strtoul (open_bracket_position+1, &end, 0);
1133 bitfield_higher = bitfield_lower;
1134 //printf("got to read low=%d high same\n",bitfield_lower);
1135 }
1136 else if(close_bracket_position && close_bracket_position < var_name_end)
1137 {
1138 char *end = NULL;
1139 bitfield_lower = ::strtoul (open_bracket_position+1, &end, 0);
1140 bitfield_higher = ::strtoul (separator_position+1, &end, 0);
1141 //printf("got to read low=%d high=%d\n",bitfield_lower,bitfield_higher);
1142 }
1143 else
1144 break;
1145 if(bitfield_lower > bitfield_higher)
Enrico Granata0a3958e2011-07-02 00:25:22 +00001146 {
1147 int temp = bitfield_lower;
1148 bitfield_lower = bitfield_higher;
1149 bitfield_higher = temp;
1150 }
Enrico Granata4becb372011-06-29 22:27:15 +00001151 }
1152 }
1153 }
1154 // this is ${var.something} or multiple .something nested
1155 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
1156 {
1157 // check for custom format string
1158
1159 // we need this because we might have ${var.something%format}. in this case var_name_end
1160 // still points to the closing }, but we must extract the variable name only up to
1161 // before the %. var_name_final will point to that % sign position
1162 const char* var_name_final;
1163
1164 {
1165 const char* percent_position = ::strchr(var_name_begin,'%');
1166 if(!percent_position || percent_position > var_name_end)
1167 var_name_final = var_name_end;
1168 else
1169 {
1170 var_name_final = percent_position;
1171 char* format_name = new char[var_name_end-var_name_final]; format_name[var_name_end-var_name_final-1] = '\0';
1172 memcpy(format_name, var_name_final+1, var_name_end-var_name_final-1);
Enrico Granata0a3958e2011-07-02 00:25:22 +00001173 if ( !FormatManager::GetFormatFromCString(format_name,
1174 true,
1175 custom_format) )
1176 {
1177 // if this is an @ sign, print ObjC description
1178 if(*format_name == '@')
1179 val_obj_display = ValueObject::eDisplayLanguageSpecific;
1180 // if this is a V, print the value using the default format
1181 if(*format_name == 'V')
1182 val_obj_display = ValueObject::eDisplayValue;
1183 }
1184 // a good custom format tells us to print the value using it
1185 else
1186 val_obj_display = ValueObject::eDisplayValue;
Enrico Granata4becb372011-06-29 22:27:15 +00001187 }
1188 }
1189
1190 {
1191 const char* open_bracket_position = ::strchr(var_name_begin,'[');
1192 if(open_bracket_position && open_bracket_position < var_name_final)
1193 {
1194 char* separator_position = ::strchr(open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
1195 char* close_bracket_position = ::strchr(open_bracket_position,']');
1196 // as usual, we assume that [] will come before %
1197 //printf("trying to expand a []\n");
1198 var_name_final = open_bracket_position;
1199 if (separator_position == NULL || separator_position > var_name_end)
1200 {
1201 char *end = NULL;
1202 bitfield_lower = ::strtoul (open_bracket_position+1, &end, 0);
1203 bitfield_higher = bitfield_lower;
1204 //printf("got to read low=%d high same\n",bitfield_lower);
1205 }
1206 else if(close_bracket_position && close_bracket_position < var_name_end)
1207 {
1208 char *end = NULL;
1209 bitfield_lower = ::strtoul (open_bracket_position+1, &end, 0);
1210 bitfield_higher = ::strtoul (separator_position+1, &end, 0);
1211 //printf("got to read low=%d high=%d\n",bitfield_lower,bitfield_higher);
1212 }
1213 else
1214 break;
1215 if(bitfield_lower > bitfield_higher)
Enrico Granata0a3958e2011-07-02 00:25:22 +00001216 {
1217 int temp = bitfield_lower;
1218 bitfield_lower = bitfield_higher;
1219 bitfield_higher = temp;
1220 }
Enrico Granata4becb372011-06-29 22:27:15 +00001221 //*((char*)open_bracket_position) = '\0';
1222 //printf("variable name is %s\n",var_name_begin);
1223 //*((char*)open_bracket_position) = '[';
1224 }
1225 }
1226
1227 Error error;
1228 lldb::VariableSP var_sp;
1229 StreamString sstring;
1230 vobj->GetExpressionPath(sstring, true, ValueObject::eHonorPointers);
Enrico Granata0a3958e2011-07-02 00:25:22 +00001231#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1232 printf("name to expand in phase 0: %s\n",sstring.GetData());
1233#endif //VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata4becb372011-06-29 22:27:15 +00001234 sstring.PutRawBytes(var_name_begin+3, var_name_final-var_name_begin-3);
Enrico Granata0a3958e2011-07-02 00:25:22 +00001235#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1236 printf("name to expand in phase 1: %s\n",sstring.GetData());
1237#endif //VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata4becb372011-06-29 22:27:15 +00001238 std::string name = std::string(sstring.GetData());
1239 target = exe_ctx->frame->GetValueForVariableExpressionPath (name.c_str(),
1240 eNoDynamicValues,
1241 0,
1242 var_sp,
1243 error).get();
1244 if (error.Fail())
1245 {
Enrico Granata0a3958e2011-07-02 00:25:22 +00001246#ifdef VERBOSE_FORMATPROMPT_OUTPUT
1247 printf("ERROR: %s\n",error.AsCString("unknown"));
1248#endif //VERBOSE_FORMATPROMPT_OUTPUT
Enrico Granata4becb372011-06-29 22:27:15 +00001249 break;
1250 }
1251 }
1252 else
1253 break;
Enrico Granata4becb372011-06-29 22:27:15 +00001254 if (bitfield_lower >= 0)
1255 {
1256 //printf("trying to print a []\n");
1257 // format this as a bitfield
1258 DataExtractor extractor = target->GetDataExtractor();
1259 uint32_t item_byte_size = ClangASTType::GetTypeByteSize(target->GetClangAST(), target->GetClangType());
1260 if(custom_format == eFormatInvalid)
1261 custom_format = eFormatHex;
1262 var_success =
1263 extractor.Dump(&s, 0, custom_format, item_byte_size, 1, 1, LLDB_INVALID_ADDRESS, bitfield_higher-bitfield_lower+1, bitfield_lower) > 0;
1264 //printf("var_success = %s\n",var_success ? "true" : "false");
1265 }
1266 else
1267 {
Enrico Granata4becb372011-06-29 22:27:15 +00001268 // format this as usual
Enrico Granata0a3958e2011-07-02 00:25:22 +00001269 targetvalue = target->GetPrintableRepresentation(val_obj_display, custom_format);
Enrico Granata4becb372011-06-29 22:27:15 +00001270 if(targetvalue)
1271 s.PutCString(targetvalue);
1272 var_success = targetvalue;
1273 //printf("here I come 4 : %s\n",var_success ? "good" : "bad");
1274 if(custom_format != eFormatInvalid)
1275 target->SetFormat(eFormatDefault);
1276 //printf("here I come 5\n");
1277 }
1278 break;
1279 }
Greg Clayton1b654882010-09-19 02:33:57 +00001280 case 'a':
1281 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1282 {
1283 if (addr && addr->IsValid())
1284 {
1285 var_success = true;
1286 format_addr = *addr;
1287 }
1288 }
1289 break;
1290
1291 case 'p':
1292 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1293 {
1294 if (exe_ctx && exe_ctx->process != NULL)
1295 {
1296 var_name_begin += ::strlen ("process.");
1297 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1298 {
1299 s.Printf("%i", exe_ctx->process->GetID());
1300 var_success = true;
1301 }
1302 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1303 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1304 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1305 {
1306 ModuleSP exe_module_sp (exe_ctx->process->GetTarget().GetExecutableModule());
1307 if (exe_module_sp)
1308 {
1309 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1310 {
1311 format_file_spec.GetFilename() = exe_module_sp->GetFileSpec().GetFilename();
1312 var_success = format_file_spec;
1313 }
1314 else
1315 {
1316 format_file_spec = exe_module_sp->GetFileSpec();
1317 var_success = format_file_spec;
1318 }
1319 }
1320 }
1321 }
1322 }
1323 break;
1324
1325 case 't':
1326 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1327 {
1328 if (exe_ctx && exe_ctx->thread)
1329 {
1330 var_name_begin += ::strlen ("thread.");
1331 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1332 {
1333 s.Printf("0x%4.4x", exe_ctx->thread->GetID());
1334 var_success = true;
1335 }
1336 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1337 {
1338 s.Printf("%u", exe_ctx->thread->GetIndexID());
1339 var_success = true;
1340 }
1341 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1342 {
1343 cstr = exe_ctx->thread->GetName();
1344 var_success = cstr && cstr[0];
1345 if (var_success)
1346 s.PutCString(cstr);
1347 }
1348 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1349 {
1350 cstr = exe_ctx->thread->GetQueueName();
1351 var_success = cstr && cstr[0];
1352 if (var_success)
1353 s.PutCString(cstr);
1354 }
1355 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1356 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001357 StopInfoSP stop_info_sp = exe_ctx->thread->GetStopInfo ();
1358 if (stop_info_sp)
Greg Clayton1b654882010-09-19 02:33:57 +00001359 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001360 cstr = stop_info_sp->GetDescription();
Greg Clayton1b654882010-09-19 02:33:57 +00001361 if (cstr && cstr[0])
1362 {
1363 s.PutCString(cstr);
1364 var_success = true;
1365 }
1366 }
1367 }
1368 }
1369 }
1370 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1371 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001372 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1373 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001374 {
Greg Clayton1b654882010-09-19 02:33:57 +00001375 var_name_begin += ::strlen ("target.");
1376 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1377 {
1378 ArchSpec arch (target->GetArchitecture ());
1379 if (arch.IsValid())
1380 {
Greg Clayton64195a22011-02-23 00:35:02 +00001381 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001382 var_success = true;
1383 }
1384 }
1385 }
1386 }
1387 break;
1388
1389
1390 case 'm':
1391 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1392 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001393 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001394 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001395 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001396 var_name_begin += ::strlen ("module.");
1397
1398 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1399 {
1400 if (module->GetFileSpec())
1401 {
1402 var_name_begin += ::strlen ("file.");
1403
1404 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1405 {
1406 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1407 var_success = format_file_spec;
1408 }
1409 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1410 {
1411 format_file_spec = module->GetFileSpec();
1412 var_success = format_file_spec;
1413 }
1414 }
1415 }
1416 }
1417 }
1418 break;
1419
1420
1421 case 'f':
1422 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1423 {
1424 if (sc && sc->comp_unit != NULL)
1425 {
1426 var_name_begin += ::strlen ("file.");
1427
1428 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1429 {
1430 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1431 var_success = format_file_spec;
1432 }
1433 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1434 {
1435 format_file_spec = *sc->comp_unit;
1436 var_success = format_file_spec;
1437 }
1438 }
1439 }
1440 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1441 {
1442 if (exe_ctx && exe_ctx->frame)
1443 {
1444 var_name_begin += ::strlen ("frame.");
1445 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1446 {
1447 s.Printf("%u", exe_ctx->frame->GetFrameIndex());
1448 var_success = true;
1449 }
1450 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1451 {
1452 reg_kind = eRegisterKindGeneric;
1453 reg_num = LLDB_REGNUM_GENERIC_PC;
1454 var_success = true;
1455 }
1456 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1457 {
1458 reg_kind = eRegisterKindGeneric;
1459 reg_num = LLDB_REGNUM_GENERIC_SP;
1460 var_success = true;
1461 }
1462 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1463 {
1464 reg_kind = eRegisterKindGeneric;
1465 reg_num = LLDB_REGNUM_GENERIC_FP;
1466 var_success = true;
1467 }
1468 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
1469 {
1470 reg_kind = eRegisterKindGeneric;
1471 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
1472 var_success = true;
1473 }
1474 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
1475 {
Greg Clayton5ccbd292011-01-06 22:15:06 +00001476 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001477 if (reg_ctx)
1478 {
1479 var_name_begin += ::strlen ("reg.");
1480 if (var_name_begin < var_name_end)
1481 {
1482 std::string reg_name (var_name_begin, var_name_end);
1483 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
1484 if (reg_info)
1485 var_success = true;
1486 }
1487 }
1488 }
1489 }
1490 }
1491 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
1492 {
1493 if (sc && (sc->function != NULL || sc->symbol != NULL))
1494 {
1495 var_name_begin += ::strlen ("function.");
1496 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1497 {
1498 if (sc->function)
1499 s.Printf("function{0x%8.8x}", sc->function->GetID());
1500 else
1501 s.Printf("symbol[%u]", sc->symbol->GetID());
1502
1503 var_success = true;
1504 }
1505 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1506 {
1507 if (sc->function)
1508 cstr = sc->function->GetName().AsCString (NULL);
1509 else if (sc->symbol)
1510 cstr = sc->symbol->GetName().AsCString (NULL);
1511 if (cstr)
1512 {
1513 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00001514
1515 if (sc->block)
1516 {
1517 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1518 if (inline_block)
1519 {
1520 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
1521 if (inline_info)
1522 {
1523 s.PutCString(" [inlined] ");
1524 inline_info->GetName().Dump(&s);
1525 }
1526 }
1527 }
Greg Clayton1b654882010-09-19 02:33:57 +00001528 var_success = true;
1529 }
1530 }
1531 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
1532 {
1533 var_success = addr != NULL;
1534 if (var_success)
1535 {
1536 format_addr = *addr;
1537 calculate_format_addr_function_offset = true;
1538 }
1539 }
1540 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
1541 {
1542 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
1543 if (var_success)
1544 {
1545 format_addr = sc->line_entry.range.GetBaseAddress();
1546 calculate_format_addr_function_offset = true;
1547 }
1548 }
1549 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
1550 {
1551 var_success = exe_ctx->frame;
1552 if (var_success)
1553 {
1554 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1555 calculate_format_addr_function_offset = true;
1556 }
1557 }
1558 }
1559 }
1560 break;
1561
1562 case 'l':
1563 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
1564 {
1565 if (sc && sc->line_entry.IsValid())
1566 {
1567 var_name_begin += ::strlen ("line.");
1568 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1569 {
1570 var_name_begin += ::strlen ("file.");
1571
1572 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1573 {
1574 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
1575 var_success = format_file_spec;
1576 }
1577 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1578 {
1579 format_file_spec = sc->line_entry.file;
1580 var_success = format_file_spec;
1581 }
1582 }
1583 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
1584 {
1585 var_success = true;
1586 s.Printf("%u", sc->line_entry.line);
1587 }
1588 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
1589 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
1590 {
1591 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
1592 if (var_success)
1593 {
1594 format_addr = sc->line_entry.range.GetBaseAddress();
1595 if (var_name_begin[0] == 'e')
1596 format_addr.Slide (sc->line_entry.range.GetByteSize());
1597 }
1598 }
1599 }
1600 }
1601 break;
1602 }
1603
1604 if (var_success)
1605 {
1606 // If format addr is valid, then we need to print an address
1607 if (reg_num != LLDB_INVALID_REGNUM)
1608 {
1609 // We have a register value to display...
1610 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
1611 {
1612 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1613 }
1614 else
1615 {
1616 if (reg_ctx == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +00001617 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001618
1619 if (reg_ctx)
1620 {
1621 if (reg_kind != kNumRegisterKinds)
1622 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
1623 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
1624 var_success = reg_info != NULL;
1625 }
1626 }
1627 }
1628
1629 if (reg_info != NULL)
1630 {
Greg Clayton7349bd92011-05-09 20:18:18 +00001631 RegisterValue reg_value;
1632 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
1633 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001634 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00001635 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00001636 }
1637 }
1638
1639 if (format_file_spec)
1640 {
1641 s << format_file_spec;
1642 }
1643
1644 // If format addr is valid, then we need to print an address
1645 if (format_addr.IsValid())
1646 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001647 var_success = false;
1648
Greg Clayton1b654882010-09-19 02:33:57 +00001649 if (calculate_format_addr_function_offset)
1650 {
1651 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00001652
Greg Clayton0603aa92010-10-04 01:05:56 +00001653 if (sc)
1654 {
1655 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00001656 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001657 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00001658 if (sc->block)
1659 {
1660 // Check to make sure we aren't in an inline
1661 // function. If we are, use the inline block
1662 // range that contains "format_addr" since
1663 // blocks can be discontiguous.
1664 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1665 AddressRange inline_range;
1666 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
1667 func_addr = inline_range.GetBaseAddress();
1668 }
1669 }
Greg Clayton0603aa92010-10-04 01:05:56 +00001670 else if (sc->symbol && sc->symbol->GetAddressRangePtr())
1671 func_addr = sc->symbol->GetAddressRangePtr()->GetBaseAddress();
1672 }
1673
1674 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00001675 {
1676 if (func_addr.GetSection() == format_addr.GetSection())
1677 {
1678 addr_t func_file_addr = func_addr.GetFileAddress();
1679 addr_t addr_file_addr = format_addr.GetFileAddress();
1680 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001681 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00001682 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001683 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001684 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00001685 }
1686 else
Greg Clayton0603aa92010-10-04 01:05:56 +00001687 {
1688 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1689 if (target)
1690 {
1691 addr_t func_load_addr = func_addr.GetLoadAddress (target);
1692 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
1693 if (addr_load_addr > func_load_addr)
1694 s.Printf(" + %llu", addr_load_addr - func_load_addr);
1695 else if (addr_load_addr < func_load_addr)
1696 s.Printf(" - %llu", func_load_addr - addr_load_addr);
1697 var_success = true;
1698 }
1699 }
Greg Clayton1b654882010-09-19 02:33:57 +00001700 }
1701 }
1702 else
1703 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001704 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00001705 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00001706 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
1707 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00001708 if (vaddr == LLDB_INVALID_ADDRESS)
1709 vaddr = format_addr.GetFileAddress ();
1710
1711 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00001712 {
Greg Clayton514487e2011-02-15 21:59:32 +00001713 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00001714 if (addr_width == 0)
1715 addr_width = 16;
1716 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001717 var_success = true;
1718 }
Greg Clayton1b654882010-09-19 02:33:57 +00001719 }
1720 }
1721 }
1722
1723 if (var_success == false)
1724 success = false;
1725 }
1726 p = var_name_end;
1727 }
1728 else
1729 break;
1730 }
1731 else
1732 {
1733 // We got a dollar sign with no '{' after it, it must just be a dollar sign
1734 s.PutChar(*p);
1735 }
1736 }
1737 else if (*p == '\\')
1738 {
1739 ++p; // skip the slash
1740 switch (*p)
1741 {
1742 case 'a': s.PutChar ('\a'); break;
1743 case 'b': s.PutChar ('\b'); break;
1744 case 'f': s.PutChar ('\f'); break;
1745 case 'n': s.PutChar ('\n'); break;
1746 case 'r': s.PutChar ('\r'); break;
1747 case 't': s.PutChar ('\t'); break;
1748 case 'v': s.PutChar ('\v'); break;
1749 case '\'': s.PutChar ('\''); break;
1750 case '\\': s.PutChar ('\\'); break;
1751 case '0':
1752 // 1 to 3 octal chars
1753 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001754 // Make a string that can hold onto the initial zero char,
1755 // up to 3 octal digits, and a terminating NULL.
1756 char oct_str[5] = { 0, 0, 0, 0, 0 };
1757
1758 int i;
1759 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
1760 oct_str[i] = p[i];
1761
1762 // We don't want to consume the last octal character since
1763 // the main for loop will do this for us, so we advance p by
1764 // one less than i (even if i is zero)
1765 p += i - 1;
1766 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
1767 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001768 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001769 char octal_char = octal_value;
1770 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00001771 }
Greg Clayton1b654882010-09-19 02:33:57 +00001772 }
1773 break;
1774
1775 case 'x':
1776 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00001777 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001778 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001779 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00001780
Greg Clayton0603aa92010-10-04 01:05:56 +00001781 // Make a string that can hold onto two hex chars plus a
1782 // NULL terminator
1783 char hex_str[3] = { 0,0,0 };
1784 hex_str[0] = *p;
1785 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001786 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001787 ++p; // Skip the first of the two hex chars
1788 hex_str[1] = *p;
1789 }
1790
1791 unsigned long hex_value = strtoul (hex_str, NULL, 16);
1792 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001793 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00001794 }
1795 else
1796 {
1797 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00001798 }
1799 break;
1800
1801 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00001802 // Just desensitize any other character by just printing what
1803 // came after the '\'
1804 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00001805 break;
1806
1807 }
1808
1809 }
1810 }
1811 if (end)
1812 *end = p;
1813 return success;
1814}
1815
Greg Clayton4a33d312011-06-23 17:59:56 +00001816
1817static FormatManager&
1818GetFormatManager() {
1819 static FormatManager g_format_manager;
1820 return g_format_manager;
1821}
1822
1823bool
Enrico Granata4becb372011-06-29 22:27:15 +00001824Debugger::ValueFormats::Get(ValueObject& vobj, ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001825{
Enrico Granata4becb372011-06-29 22:27:15 +00001826 return GetFormatManager().Value().Get(vobj,entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001827}
1828
1829void
Enrico Granata4becb372011-06-29 22:27:15 +00001830Debugger::ValueFormats::Add(const ConstString &type, const ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001831{
Enrico Granata4becb372011-06-29 22:27:15 +00001832 GetFormatManager().Value().Add(type.AsCString(),entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001833}
1834
1835bool
Enrico Granata4becb372011-06-29 22:27:15 +00001836Debugger::ValueFormats::Delete(const ConstString &type)
Greg Clayton4a33d312011-06-23 17:59:56 +00001837{
Enrico Granata4becb372011-06-29 22:27:15 +00001838 return GetFormatManager().Value().Delete(type.AsCString());
Greg Clayton4a33d312011-06-23 17:59:56 +00001839}
1840
1841void
Enrico Granata4becb372011-06-29 22:27:15 +00001842Debugger::ValueFormats::Clear()
Greg Clayton4a33d312011-06-23 17:59:56 +00001843{
Enrico Granata4becb372011-06-29 22:27:15 +00001844 GetFormatManager().Value().Clear();
1845}
1846
1847void
Enrico Granata0a3958e2011-07-02 00:25:22 +00001848Debugger::ValueFormats::LoopThrough(ValueFormat::ValueCallback callback, void* callback_baton)
Enrico Granata4becb372011-06-29 22:27:15 +00001849{
1850 GetFormatManager().Value().LoopThrough(callback, callback_baton);
1851}
1852
1853uint32_t
1854Debugger::ValueFormats::GetCurrentRevision()
1855{
1856 return GetFormatManager().GetCurrentRevision();
1857}
1858
Enrico Granata0a3958e2011-07-02 00:25:22 +00001859uint32_t
1860Debugger::ValueFormats::GetCount()
1861{
1862 return GetFormatManager().Value().GetCount();
1863}
Enrico Granata4becb372011-06-29 22:27:15 +00001864
1865bool
1866Debugger::SummaryFormats::Get(ValueObject& vobj, SummaryFormat::SharedPointer &entry)
1867{
1868 return GetFormatManager().Summary().Get(vobj,entry);
1869}
1870
1871void
1872Debugger::SummaryFormats::Add(const ConstString &type, const SummaryFormat::SharedPointer &entry)
1873{
1874 GetFormatManager().Summary().Add(type.AsCString(),entry);
1875}
1876
1877bool
1878Debugger::SummaryFormats::Delete(const ConstString &type)
1879{
1880 return GetFormatManager().Summary().Delete(type.AsCString());
1881}
1882
1883void
1884Debugger::SummaryFormats::Clear()
1885{
1886 GetFormatManager().Summary().Clear();
1887}
1888
1889void
Enrico Granata0a3958e2011-07-02 00:25:22 +00001890Debugger::SummaryFormats::LoopThrough(SummaryFormat::SummaryCallback callback, void* callback_baton)
Enrico Granata4becb372011-06-29 22:27:15 +00001891{
1892 GetFormatManager().Summary().LoopThrough(callback, callback_baton);
1893}
1894
1895uint32_t
1896Debugger::SummaryFormats::GetCurrentRevision()
1897{
1898 return GetFormatManager().GetCurrentRevision();
Greg Clayton4a33d312011-06-23 17:59:56 +00001899}
1900
Enrico Granata0a3958e2011-07-02 00:25:22 +00001901uint32_t
1902Debugger::SummaryFormats::GetCount()
1903{
1904 return GetFormatManager().Summary().GetCount();
1905}
1906
1907bool
1908Debugger::RegexSummaryFormats::Get(ValueObject& vobj, SummaryFormat::SharedPointer &entry)
1909{
1910 return GetFormatManager().RegexSummary().Get(vobj,entry);
1911}
1912
1913void
1914Debugger::RegexSummaryFormats::Add(const lldb::RegularExpressionSP &type, const SummaryFormat::SharedPointer &entry)
1915{
1916 GetFormatManager().RegexSummary().Add(type,entry);
1917}
1918
1919bool
1920Debugger::RegexSummaryFormats::Delete(const ConstString &type)
1921{
1922 return GetFormatManager().RegexSummary().Delete(type.AsCString());
1923}
1924
1925void
1926Debugger::RegexSummaryFormats::Clear()
1927{
1928 GetFormatManager().RegexSummary().Clear();
1929}
1930
1931void
1932Debugger::RegexSummaryFormats::LoopThrough(SummaryFormat::RegexSummaryCallback callback, void* callback_baton)
1933{
1934 GetFormatManager().RegexSummary().LoopThrough(callback, callback_baton);
1935}
1936
1937uint32_t
1938Debugger::RegexSummaryFormats::GetCurrentRevision()
1939{
1940 return GetFormatManager().GetCurrentRevision();
1941}
1942
1943uint32_t
1944Debugger::RegexSummaryFormats::GetCount()
1945{
1946 return GetFormatManager().RegexSummary().GetCount();
1947}
1948
Greg Clayton1b654882010-09-19 02:33:57 +00001949#pragma mark Debugger::SettingsController
1950
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001951//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00001952// class Debugger::SettingsController
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001953//--------------------------------------------------
1954
Greg Clayton1b654882010-09-19 02:33:57 +00001955Debugger::SettingsController::SettingsController () :
Caroline Tice101c7c22010-09-09 06:25:08 +00001956 UserSettingsController ("", lldb::UserSettingsControllerSP())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001957{
Caroline Tice91123da2010-09-08 17:48:55 +00001958 m_default_settings.reset (new DebuggerInstanceSettings (*this, false,
1959 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001960}
1961
Greg Clayton1b654882010-09-19 02:33:57 +00001962Debugger::SettingsController::~SettingsController ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001963{
1964}
1965
1966
1967lldb::InstanceSettingsSP
Greg Clayton1b654882010-09-19 02:33:57 +00001968Debugger::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001969{
Greg Claytondbe54502010-11-19 03:46:01 +00001970 DebuggerInstanceSettings *new_settings = new DebuggerInstanceSettings (*GetSettingsController(),
Caroline Tice91123da2010-09-08 17:48:55 +00001971 false, instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001972 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1973 return new_settings_sp;
1974}
1975
Greg Clayton1b654882010-09-19 02:33:57 +00001976#pragma mark DebuggerInstanceSettings
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001977//--------------------------------------------------
1978// class DebuggerInstanceSettings
1979//--------------------------------------------------
1980
Greg Claytona7015092010-09-18 01:14:36 +00001981DebuggerInstanceSettings::DebuggerInstanceSettings
1982(
1983 UserSettingsController &owner,
1984 bool live_instance,
1985 const char *name
1986) :
Greg Clayton85851dd2010-12-04 00:10:17 +00001987 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytona7015092010-09-18 01:14:36 +00001988 m_term_width (80),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001989 m_prompt (),
Greg Clayton0603aa92010-10-04 01:05:56 +00001990 m_frame_format (),
1991 m_thread_format (),
Caroline Ticedaccaa92010-09-20 20:44:43 +00001992 m_script_lang (),
Jim Ingham3bcdb292010-10-04 22:44:14 +00001993 m_use_external_editor (false),
1994 m_auto_confirm_on (false)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001995{
Caroline Ticef20e8232010-09-09 18:26:37 +00001996 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1997 // until the vtables for DebuggerInstanceSettings are properly set up, i.e. AFTER all the initializers.
1998 // 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 +00001999 // The same is true of CreateInstanceName().
2000
2001 if (GetInstanceName() == InstanceSettings::InvalidName())
2002 {
2003 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2004 m_owner.RegisterInstanceSettings (this);
2005 }
Caroline Ticef20e8232010-09-09 18:26:37 +00002006
2007 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002008 {
2009 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2010 CopyInstanceSettings (pending_settings, false);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002011 }
2012}
2013
2014DebuggerInstanceSettings::DebuggerInstanceSettings (const DebuggerInstanceSettings &rhs) :
Greg Clayton99d0faf2010-11-18 23:32:35 +00002015 InstanceSettings (*Debugger::GetSettingsController(), CreateInstanceName ().AsCString()),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002016 m_prompt (rhs.m_prompt),
Greg Clayton0603aa92010-10-04 01:05:56 +00002017 m_frame_format (rhs.m_frame_format),
2018 m_thread_format (rhs.m_thread_format),
Caroline Ticedaccaa92010-09-20 20:44:43 +00002019 m_script_lang (rhs.m_script_lang),
Jim Ingham3bcdb292010-10-04 22:44:14 +00002020 m_use_external_editor (rhs.m_use_external_editor),
2021 m_auto_confirm_on(rhs.m_auto_confirm_on)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002022{
2023 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2024 CopyInstanceSettings (pending_settings, false);
2025 m_owner.RemovePendingSettings (m_instance_name);
2026}
2027
2028DebuggerInstanceSettings::~DebuggerInstanceSettings ()
2029{
2030}
2031
2032DebuggerInstanceSettings&
2033DebuggerInstanceSettings::operator= (const DebuggerInstanceSettings &rhs)
2034{
2035 if (this != &rhs)
2036 {
Greg Clayton1b654882010-09-19 02:33:57 +00002037 m_term_width = rhs.m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002038 m_prompt = rhs.m_prompt;
Greg Clayton0603aa92010-10-04 01:05:56 +00002039 m_frame_format = rhs.m_frame_format;
2040 m_thread_format = rhs.m_thread_format;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002041 m_script_lang = rhs.m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002042 m_use_external_editor = rhs.m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002043 m_auto_confirm_on = rhs.m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002044 }
2045
2046 return *this;
2047}
2048
Greg Clayton1b654882010-09-19 02:33:57 +00002049bool
2050DebuggerInstanceSettings::ValidTermWidthValue (const char *value, Error err)
2051{
2052 bool valid = false;
2053
2054 // Verify we have a value string.
2055 if (value == NULL || value[0] == '\0')
2056 {
2057 err.SetErrorString ("Missing value. Can't set terminal width without a value.\n");
2058 }
2059 else
2060 {
2061 char *end = NULL;
2062 const uint32_t width = ::strtoul (value, &end, 0);
2063
Johnny Chenea9fc182010-09-20 16:36:43 +00002064 if (end && end[0] == '\0')
Greg Clayton1b654882010-09-19 02:33:57 +00002065 {
Johnny Chen433d7742010-09-20 17:04:41 +00002066 if (width >= 10 && width <= 1024)
Greg Clayton1b654882010-09-19 02:33:57 +00002067 valid = true;
2068 else
2069 err.SetErrorString ("Invalid term-width value; value must be between 10 and 1024.\n");
2070 }
2071 else
2072 err.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string.\n", value);
2073 }
2074
2075 return valid;
2076}
2077
2078
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002079void
2080DebuggerInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2081 const char *index_value,
2082 const char *value,
2083 const ConstString &instance_name,
2084 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:54 +00002085 VarSetOperationType op,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002086 Error &err,
2087 bool pending)
2088{
Greg Clayton0603aa92010-10-04 01:05:56 +00002089
2090 if (var_name == TermWidthVarName())
2091 {
2092 if (ValidTermWidthValue (value, err))
2093 {
2094 m_term_width = ::strtoul (value, NULL, 0);
2095 }
2096 }
2097 else if (var_name == PromptVarName())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002098 {
Caroline Tice101c7c22010-09-09 06:25:08 +00002099 UserSettingsController::UpdateStringVariable (op, m_prompt, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002100 if (!pending)
2101 {
Caroline Tice49e27372010-09-07 18:35:40 +00002102 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2103 // strip off the brackets before passing it to BroadcastPromptChange.
2104
2105 std::string tmp_instance_name (instance_name.AsCString());
2106 if ((tmp_instance_name[0] == '[')
2107 && (tmp_instance_name[instance_name.GetLength() - 1] == ']'))
2108 tmp_instance_name = tmp_instance_name.substr (1, instance_name.GetLength() - 2);
2109 ConstString new_name (tmp_instance_name.c_str());
2110
2111 BroadcastPromptChange (new_name, m_prompt.c_str());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002112 }
2113 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002114 else if (var_name == GetFrameFormatName())
2115 {
2116 UserSettingsController::UpdateStringVariable (op, m_frame_format, value, err);
2117 }
2118 else if (var_name == GetThreadFormatName())
2119 {
2120 UserSettingsController::UpdateStringVariable (op, m_thread_format, value, err);
2121 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002122 else if (var_name == ScriptLangVarName())
2123 {
2124 bool success;
2125 m_script_lang = Args::StringToScriptLanguage (value, eScriptLanguageDefault,
2126 &success);
2127 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002128 else if (var_name == UseExternalEditorVarName ())
2129 {
Greg Clayton385aa282011-04-22 03:55:06 +00002130 UserSettingsController::UpdateBooleanVariable (op, m_use_external_editor, value, false, err);
Caroline Ticedaccaa92010-09-20 20:44:43 +00002131 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002132 else if (var_name == AutoConfirmName ())
2133 {
Greg Clayton385aa282011-04-22 03:55:06 +00002134 UserSettingsController::UpdateBooleanVariable (op, m_auto_confirm_on, value, false, err);
Jim Ingham3bcdb292010-10-04 22:44:14 +00002135 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002136}
2137
Caroline Tice12cecd72010-09-20 21:37:42 +00002138bool
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002139DebuggerInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2140 const ConstString &var_name,
Caroline Ticedaccaa92010-09-20 20:44:43 +00002141 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42 +00002142 Error *err)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002143{
2144 if (var_name == PromptVarName())
2145 {
Greg Clayton0603aa92010-10-04 01:05:56 +00002146 value.AppendString (m_prompt.c_str(), m_prompt.size());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002147
2148 }
2149 else if (var_name == ScriptLangVarName())
2150 {
2151 value.AppendString (ScriptInterpreter::LanguageToString (m_script_lang).c_str());
2152 }
Caroline Tice101c7c22010-09-09 06:25:08 +00002153 else if (var_name == TermWidthVarName())
2154 {
2155 StreamString width_str;
2156 width_str.Printf ("%d", m_term_width);
2157 value.AppendString (width_str.GetData());
2158 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002159 else if (var_name == GetFrameFormatName ())
2160 {
2161 value.AppendString(m_frame_format.c_str(), m_frame_format.size());
2162 }
2163 else if (var_name == GetThreadFormatName ())
2164 {
2165 value.AppendString(m_thread_format.c_str(), m_thread_format.size());
2166 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002167 else if (var_name == UseExternalEditorVarName())
2168 {
2169 if (m_use_external_editor)
2170 value.AppendString ("true");
2171 else
2172 value.AppendString ("false");
2173 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00002174 else if (var_name == AutoConfirmName())
2175 {
2176 if (m_auto_confirm_on)
2177 value.AppendString ("true");
2178 else
2179 value.AppendString ("false");
2180 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002181 else
Caroline Tice12cecd72010-09-20 21:37:42 +00002182 {
2183 if (err)
2184 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2185 return false;
2186 }
2187 return true;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002188}
2189
2190void
2191DebuggerInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
2192 bool pending)
2193{
2194 if (new_settings.get() == NULL)
2195 return;
2196
2197 DebuggerInstanceSettings *new_debugger_settings = (DebuggerInstanceSettings *) new_settings.get();
2198
2199 m_prompt = new_debugger_settings->m_prompt;
2200 if (!pending)
Caroline Tice49e27372010-09-07 18:35:40 +00002201 {
2202 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
2203 // strip off the brackets before passing it to BroadcastPromptChange.
2204
2205 std::string tmp_instance_name (m_instance_name.AsCString());
2206 if ((tmp_instance_name[0] == '[')
2207 && (tmp_instance_name[m_instance_name.GetLength() - 1] == ']'))
2208 tmp_instance_name = tmp_instance_name.substr (1, m_instance_name.GetLength() - 2);
2209 ConstString new_name (tmp_instance_name.c_str());
2210
2211 BroadcastPromptChange (new_name, m_prompt.c_str());
2212 }
Greg Clayton0603aa92010-10-04 01:05:56 +00002213 m_frame_format = new_debugger_settings->m_frame_format;
2214 m_thread_format = new_debugger_settings->m_thread_format;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002215 m_term_width = new_debugger_settings->m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002216 m_script_lang = new_debugger_settings->m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002217 m_use_external_editor = new_debugger_settings->m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00002218 m_auto_confirm_on = new_debugger_settings->m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002219}
2220
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002221
2222bool
2223DebuggerInstanceSettings::BroadcastPromptChange (const ConstString &instance_name, const char *new_prompt)
2224{
2225 std::string tmp_prompt;
2226
2227 if (new_prompt != NULL)
2228 {
2229 tmp_prompt = new_prompt ;
2230 int len = tmp_prompt.size();
2231 if (len > 1
2232 && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"')
2233 && (tmp_prompt[len-1] == tmp_prompt[0]))
2234 {
2235 tmp_prompt = tmp_prompt.substr(1,len-2);
2236 }
2237 len = tmp_prompt.size();
2238 if (tmp_prompt[len-1] != ' ')
2239 tmp_prompt.append(" ");
2240 }
2241 EventSP new_event_sp;
2242 new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt,
2243 new EventDataBytes (tmp_prompt.c_str())));
2244
2245 if (instance_name.GetLength() != 0)
2246 {
2247 // Set prompt for a particular instance.
2248 Debugger *dbg = Debugger::FindDebuggerWithInstanceName (instance_name).get();
2249 if (dbg != NULL)
2250 {
2251 dbg->GetCommandInterpreter().BroadcastEvent (new_event_sp);
2252 }
2253 }
2254
2255 return true;
2256}
2257
2258const ConstString
2259DebuggerInstanceSettings::CreateInstanceName ()
2260{
2261 static int instance_count = 1;
2262 StreamString sstr;
2263
2264 sstr.Printf ("debugger_%d", instance_count);
2265 ++instance_count;
2266
2267 const ConstString ret_val (sstr.GetData());
2268
2269 return ret_val;
2270}
2271
2272const ConstString &
2273DebuggerInstanceSettings::PromptVarName ()
2274{
2275 static ConstString prompt_var_name ("prompt");
2276
2277 return prompt_var_name;
2278}
2279
2280const ConstString &
Greg Clayton0603aa92010-10-04 01:05:56 +00002281DebuggerInstanceSettings::GetFrameFormatName ()
2282{
2283 static ConstString prompt_var_name ("frame-format");
2284
2285 return prompt_var_name;
2286}
2287
2288const ConstString &
2289DebuggerInstanceSettings::GetThreadFormatName ()
2290{
2291 static ConstString prompt_var_name ("thread-format");
2292
2293 return prompt_var_name;
2294}
2295
2296const ConstString &
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002297DebuggerInstanceSettings::ScriptLangVarName ()
2298{
2299 static ConstString script_lang_var_name ("script-lang");
2300
2301 return script_lang_var_name;
2302}
2303
Caroline Tice101c7c22010-09-09 06:25:08 +00002304const ConstString &
2305DebuggerInstanceSettings::TermWidthVarName ()
2306{
2307 static ConstString term_width_var_name ("term-width");
2308
2309 return term_width_var_name;
2310}
2311
Caroline Ticedaccaa92010-09-20 20:44:43 +00002312const ConstString &
2313DebuggerInstanceSettings::UseExternalEditorVarName ()
2314{
2315 static ConstString use_external_editor_var_name ("use-external-editor");
2316
2317 return use_external_editor_var_name;
2318}
2319
Jim Ingham3bcdb292010-10-04 22:44:14 +00002320const ConstString &
2321DebuggerInstanceSettings::AutoConfirmName ()
2322{
2323 static ConstString use_external_editor_var_name ("auto-confirm");
2324
2325 return use_external_editor_var_name;
2326}
2327
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002328//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00002329// SettingsController Variable Tables
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002330//--------------------------------------------------
2331
2332
2333SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002334Debugger::SettingsController::global_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002335{
2336 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
Caroline Tice101c7c22010-09-09 06:25:08 +00002337 // The Debugger level global table should always be empty; all Debugger settable variables should be instance
2338 // variables.
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002339 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2340};
2341
Greg Claytonbb562b12010-10-04 02:44:26 +00002342#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name}${function.pc-offset}}}"
Greg Clayton0603aa92010-10-04 01:05:56 +00002343#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002344
Greg Clayton0603aa92010-10-04 01:05:56 +00002345#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2346 "{, ${frame.pc}}"\
2347 MODULE_WITH_FUNC\
Greg Claytoncf4b9072010-10-04 17:04:23 +00002348 FILE_AND_LINE\
Greg Clayton0603aa92010-10-04 01:05:56 +00002349 "{, stop reason = ${thread.stop-reason}}"\
Greg Clayton0603aa92010-10-04 01:05:56 +00002350 "\\n"
2351
Greg Clayton315d2ca2010-11-02 01:53:21 +00002352//#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2353// "{, ${frame.pc}}"\
2354// MODULE_WITH_FUNC\
2355// FILE_AND_LINE\
2356// "{, stop reason = ${thread.stop-reason}}"\
2357// "{, name = ${thread.name}}"\
2358// "{, queue = ${thread.queue}}"\
2359// "\\n"
2360
Greg Clayton0603aa92010-10-04 01:05:56 +00002361#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
2362 MODULE_WITH_FUNC\
2363 FILE_AND_LINE\
2364 "\\n"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002365
2366SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002367Debugger::SettingsController::instance_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002368{
Greg Clayton0603aa92010-10-04 01:05:56 +00002369// NAME Setting variable type Default Enum Init'd Hidden Help
2370// ======================= ======================= ====================== ==== ====== ====== ======================
Greg Clayton0603aa92010-10-04 01:05:56 +00002371{ "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 +00002372{ "prompt", eSetVarTypeString, "(lldb) ", NULL, false, false, "The debugger command line prompt displayed for the user." },
Jim Ingham3bcdb292010-10-04 22:44:14 +00002373{ "script-lang", eSetVarTypeString, "python", NULL, false, false, "The script language to be used for evaluating user-written scripts." },
2374{ "term-width", eSetVarTypeInt, "80" , NULL, false, false, "The maximum number of columns to use for displaying text." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002375{ "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 +00002376{ "use-external-editor", eSetVarTypeBoolean, "false", NULL, false, false, "Whether to use an external editor or not." },
2377{ "auto-confirm", eSetVarTypeBoolean, "false", NULL, false, false, "If true all confirmation prompts will receive their default reply." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002378{ NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002379};