blob: fadeafdf8131aef14c77b3b58e4ed50ca6f2e185 [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;
794 var_name_begin++;
795 lldb::clang_type_t pointer_clang_type = vobj->GetClangType();
796 clang_type_t elem_or_pointee_clang_type;
797 const Flags type_flags (ClangASTContext::GetTypeInfo (pointer_clang_type,
798 vobj->GetClangAST(),
799 &elem_or_pointee_clang_type));
800 if (type_flags.Test (ClangASTContext::eTypeIsPointer))
801 {
802 if (ClangASTContext::IsCharType (elem_or_pointee_clang_type))
803 {
804 StreamString sstr;
805 ExecutionContextScope *exe_scope = vobj->GetExecutionContextScope();
806 Process *process = exe_scope->CalculateProcess();
807 if(!process) break;
808 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
809 AddressType cstr_address_type = eAddressTypeInvalid;
810 DataExtractor data;
811 size_t bytes_read = 0;
812 std::vector<char> data_buffer;
813 Error error;
814 cstr_address = vobj->GetPointerValue (cstr_address_type, true);
815 {
816 const size_t k_max_buf_size = 256;
817 data_buffer.resize (k_max_buf_size + 1);
818 // NULL terminate in case we don't get the entire C string
819 data_buffer.back() = '\0';
820
821 sstr << '"';
822
823 data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
824 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
825 {
826 size_t len = strlen(&data_buffer.front());
827 if (len == 0)
828 break;
829 if (len > bytes_read)
830 len = bytes_read;
831
832 data.Dump (&sstr,
833 0, // Start offset in "data"
834 eFormatCharArray, // Print as characters
835 1, // Size of item (1 byte for a char!)
836 len, // How many bytes to print?
837 UINT32_MAX, // num per line
838 LLDB_INVALID_ADDRESS,// base address
839 0, // bitfield bit size
840 0); // bitfield bit offset
841
842 if (len < k_max_buf_size)
843 break;
844 cstr_address += k_max_buf_size;
845 }
846 sstr << '"';
847 s.PutCString(sstr.GetData());
848 var_success = true;
849 break;
850 }
851 }
852 else /*if (ClangASTContext::IsAggregateType (elem_or_pointee_clang_type)) or this is some other pointer type*/
853 {
854 Error error;
855 realvobj = vobj;
856 vobj = vobj->Dereference(error).get();
857 if(!vobj || error.Fail())
858 break;
859 }
860 }
861 else
862 break;
863 }
864 case 'v':
865 {
866 const char* targetvalue;
867 bool use_summary = false;
868 ValueObject* target;
869 lldb::Format custom_format = eFormatInvalid;
870 int bitfield_lower = -1;
871 int bitfield_higher = -1;
872 if (!vobj) break;
873 // simplest case ${var}, just print vobj's value
874 if (::strncmp (var_name_begin, "var}", strlen("var}")) == 0)
875 target = vobj;
876 else if (::strncmp(var_name_begin,"var%",strlen("var%")) == 0)
877 {
878 // this is a variable with some custom format applied to it
879 const char* var_name_final;
880 target = vobj;
881 {
882 const char* percent_position = ::strchr(var_name_begin,'%'); // TODO: make this a constant
883 //if(!percent_position || percent_position > var_name_end)
884 // var_name_final = var_name_end;
885 //else
886 //{
887 var_name_final = percent_position;
888 char* format_name = new char[var_name_end-var_name_final]; format_name[var_name_end-var_name_final-1] = '\0';
889 memcpy(format_name, var_name_final+1, var_name_end-var_name_final-1);
890 FormatManager::GetFormatFromCString(format_name,
891 true,
892 custom_format); // if this fails, custom_format is reset to invalid
893 delete format_name;
894 //}
895 }
896 }
897 else if (::strncmp(var_name_begin,"var[",strlen("var[")) == 0)
898 {
899 // this is a bitfield variable
900 const char *var_name_final;
901 target = vobj;
902
903 {
904 const char* percent_position = ::strchr(var_name_begin,'%');
905 if(!percent_position || percent_position > var_name_end)
906 var_name_final = var_name_end;
907 else
908 {
909 var_name_final = percent_position;
910 char* format_name = new char[var_name_end-var_name_final]; format_name[var_name_end-var_name_final-1] = '\0';
911 memcpy(format_name, var_name_final+1, var_name_end-var_name_final-1);
912 FormatManager::GetFormatFromCString(format_name,
913 true,
914 custom_format); // if this fails, custom_format is reset to invalid
915 delete format_name;
916 }
917 }
918
919 {
920 // code here might be simpler than in the case below
921 const char* open_bracket_position = ::strchr(var_name_begin,'[');
922 if(open_bracket_position && open_bracket_position < var_name_final)
923 {
924 char* separator_position = ::strchr(open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
925 char* close_bracket_position = ::strchr(open_bracket_position,']');
926 // as usual, we assume that [] will come before %
927 //printf("trying to expand a []\n");
928 var_name_final = open_bracket_position;
929 if (separator_position == NULL || separator_position > var_name_end)
930 {
931 char *end = NULL;
932 bitfield_lower = ::strtoul (open_bracket_position+1, &end, 0);
933 bitfield_higher = bitfield_lower;
934 //printf("got to read low=%d high same\n",bitfield_lower);
935 }
936 else if(close_bracket_position && close_bracket_position < var_name_end)
937 {
938 char *end = NULL;
939 bitfield_lower = ::strtoul (open_bracket_position+1, &end, 0);
940 bitfield_higher = ::strtoul (separator_position+1, &end, 0);
941 //printf("got to read low=%d high=%d\n",bitfield_lower,bitfield_higher);
942 }
943 else
944 break;
945 if(bitfield_lower > bitfield_higher)
946 break;
947 }
948 }
949 }
950 // this is ${var.something} or multiple .something nested
951 else if (::strncmp (var_name_begin, "var", strlen("var")) == 0)
952 {
953 // check for custom format string
954
955 // we need this because we might have ${var.something%format}. in this case var_name_end
956 // still points to the closing }, but we must extract the variable name only up to
957 // before the %. var_name_final will point to that % sign position
958 const char* var_name_final;
959
960 {
961 const char* percent_position = ::strchr(var_name_begin,'%');
962 if(!percent_position || percent_position > var_name_end)
963 var_name_final = var_name_end;
964 else
965 {
966 var_name_final = percent_position;
967 char* format_name = new char[var_name_end-var_name_final]; format_name[var_name_end-var_name_final-1] = '\0';
968 memcpy(format_name, var_name_final+1, var_name_end-var_name_final-1);
969 FormatManager::GetFormatFromCString(format_name,
970 true,
971 custom_format); // if this fails, custom_format is reset to invalid
972 delete format_name;
973 }
974 }
975
976 {
977 const char* open_bracket_position = ::strchr(var_name_begin,'[');
978 if(open_bracket_position && open_bracket_position < var_name_final)
979 {
980 char* separator_position = ::strchr(open_bracket_position,'-'); // might be NULL if this is a simple var[N] bitfield
981 char* close_bracket_position = ::strchr(open_bracket_position,']');
982 // as usual, we assume that [] will come before %
983 //printf("trying to expand a []\n");
984 var_name_final = open_bracket_position;
985 if (separator_position == NULL || separator_position > var_name_end)
986 {
987 char *end = NULL;
988 bitfield_lower = ::strtoul (open_bracket_position+1, &end, 0);
989 bitfield_higher = bitfield_lower;
990 //printf("got to read low=%d high same\n",bitfield_lower);
991 }
992 else if(close_bracket_position && close_bracket_position < var_name_end)
993 {
994 char *end = NULL;
995 bitfield_lower = ::strtoul (open_bracket_position+1, &end, 0);
996 bitfield_higher = ::strtoul (separator_position+1, &end, 0);
997 //printf("got to read low=%d high=%d\n",bitfield_lower,bitfield_higher);
998 }
999 else
1000 break;
1001 if(bitfield_lower > bitfield_higher)
1002 break;
1003 //*((char*)open_bracket_position) = '\0';
1004 //printf("variable name is %s\n",var_name_begin);
1005 //*((char*)open_bracket_position) = '[';
1006 }
1007 }
1008
1009 Error error;
1010 lldb::VariableSP var_sp;
1011 StreamString sstring;
1012 vobj->GetExpressionPath(sstring, true, ValueObject::eHonorPointers);
1013 //printf("name to expand in phase 0: %s\n",sstring.GetData());
1014 sstring.PutRawBytes(var_name_begin+3, var_name_final-var_name_begin-3);
1015 //printf("name to expand in phase 1: %s\n",sstring.GetData());
1016 std::string name = std::string(sstring.GetData());
1017 target = exe_ctx->frame->GetValueForVariableExpressionPath (name.c_str(),
1018 eNoDynamicValues,
1019 0,
1020 var_sp,
1021 error).get();
1022 if (error.Fail())
1023 {
1024 //printf("ERROR: %s\n",error.AsCString("unknown"));
1025 break;
1026 }
1027 }
1028 else
1029 break;
1030 if(*(var_name_end+1)=='s')
1031 {
1032 use_summary = true;
1033 var_name_end++;
1034 }
1035 if (bitfield_lower >= 0)
1036 {
1037 //printf("trying to print a []\n");
1038 // format this as a bitfield
1039 DataExtractor extractor = target->GetDataExtractor();
1040 uint32_t item_byte_size = ClangASTType::GetTypeByteSize(target->GetClangAST(), target->GetClangType());
1041 if(custom_format == eFormatInvalid)
1042 custom_format = eFormatHex;
1043 var_success =
1044 extractor.Dump(&s, 0, custom_format, item_byte_size, 1, 1, LLDB_INVALID_ADDRESS, bitfield_higher-bitfield_lower+1, bitfield_lower) > 0;
1045 //printf("var_success = %s\n",var_success ? "true" : "false");
1046 }
1047 else
1048 {
1049 //printf("here I come 1\n");
1050 // format this as usual
1051 if(custom_format != eFormatInvalid)
1052 target->SetFormat(custom_format);
1053 //printf("here I come 2\n");
1054 if(!use_summary)
1055 targetvalue = target->GetValueAsCString();
1056 else
1057 targetvalue = target->GetSummaryAsCString();
1058 //printf("here I come 3\n");
1059 if(targetvalue)
1060 s.PutCString(targetvalue);
1061 var_success = targetvalue;
1062 //printf("here I come 4 : %s\n",var_success ? "good" : "bad");
1063 if(custom_format != eFormatInvalid)
1064 target->SetFormat(eFormatDefault);
1065 //printf("here I come 5\n");
1066 }
1067 break;
1068 }
Greg Clayton1b654882010-09-19 02:33:57 +00001069 case 'a':
1070 if (::strncmp (var_name_begin, "addr}", strlen("addr}")) == 0)
1071 {
1072 if (addr && addr->IsValid())
1073 {
1074 var_success = true;
1075 format_addr = *addr;
1076 }
1077 }
1078 break;
1079
1080 case 'p':
1081 if (::strncmp (var_name_begin, "process.", strlen("process.")) == 0)
1082 {
1083 if (exe_ctx && exe_ctx->process != NULL)
1084 {
1085 var_name_begin += ::strlen ("process.");
1086 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1087 {
1088 s.Printf("%i", exe_ctx->process->GetID());
1089 var_success = true;
1090 }
1091 else if ((::strncmp (var_name_begin, "name}", strlen("name}")) == 0) ||
1092 (::strncmp (var_name_begin, "file.basename}", strlen("file.basename}")) == 0) ||
1093 (::strncmp (var_name_begin, "file.fullpath}", strlen("file.fullpath}")) == 0))
1094 {
1095 ModuleSP exe_module_sp (exe_ctx->process->GetTarget().GetExecutableModule());
1096 if (exe_module_sp)
1097 {
1098 if (var_name_begin[0] == 'n' || var_name_begin[5] == 'f')
1099 {
1100 format_file_spec.GetFilename() = exe_module_sp->GetFileSpec().GetFilename();
1101 var_success = format_file_spec;
1102 }
1103 else
1104 {
1105 format_file_spec = exe_module_sp->GetFileSpec();
1106 var_success = format_file_spec;
1107 }
1108 }
1109 }
1110 }
1111 }
1112 break;
1113
1114 case 't':
1115 if (::strncmp (var_name_begin, "thread.", strlen("thread.")) == 0)
1116 {
1117 if (exe_ctx && exe_ctx->thread)
1118 {
1119 var_name_begin += ::strlen ("thread.");
1120 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1121 {
1122 s.Printf("0x%4.4x", exe_ctx->thread->GetID());
1123 var_success = true;
1124 }
1125 else if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1126 {
1127 s.Printf("%u", exe_ctx->thread->GetIndexID());
1128 var_success = true;
1129 }
1130 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1131 {
1132 cstr = exe_ctx->thread->GetName();
1133 var_success = cstr && cstr[0];
1134 if (var_success)
1135 s.PutCString(cstr);
1136 }
1137 else if (::strncmp (var_name_begin, "queue}", strlen("queue}")) == 0)
1138 {
1139 cstr = exe_ctx->thread->GetQueueName();
1140 var_success = cstr && cstr[0];
1141 if (var_success)
1142 s.PutCString(cstr);
1143 }
1144 else if (::strncmp (var_name_begin, "stop-reason}", strlen("stop-reason}")) == 0)
1145 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001146 StopInfoSP stop_info_sp = exe_ctx->thread->GetStopInfo ();
1147 if (stop_info_sp)
Greg Clayton1b654882010-09-19 02:33:57 +00001148 {
Jim Inghamb15bfc72010-10-20 00:39:53 +00001149 cstr = stop_info_sp->GetDescription();
Greg Clayton1b654882010-09-19 02:33:57 +00001150 if (cstr && cstr[0])
1151 {
1152 s.PutCString(cstr);
1153 var_success = true;
1154 }
1155 }
1156 }
1157 }
1158 }
1159 else if (::strncmp (var_name_begin, "target.", strlen("target.")) == 0)
1160 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001161 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1162 if (target)
Greg Clayton1b654882010-09-19 02:33:57 +00001163 {
Greg Clayton1b654882010-09-19 02:33:57 +00001164 var_name_begin += ::strlen ("target.");
1165 if (::strncmp (var_name_begin, "arch}", strlen("arch}")) == 0)
1166 {
1167 ArchSpec arch (target->GetArchitecture ());
1168 if (arch.IsValid())
1169 {
Greg Clayton64195a22011-02-23 00:35:02 +00001170 s.PutCString (arch.GetArchitectureName());
Greg Clayton1b654882010-09-19 02:33:57 +00001171 var_success = true;
1172 }
1173 }
1174 }
1175 }
1176 break;
1177
1178
1179 case 'm':
1180 if (::strncmp (var_name_begin, "module.", strlen("module.")) == 0)
1181 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001182 if (sc && sc->module_sp.get())
Greg Clayton1b654882010-09-19 02:33:57 +00001183 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001184 Module *module = sc->module_sp.get();
Greg Clayton1b654882010-09-19 02:33:57 +00001185 var_name_begin += ::strlen ("module.");
1186
1187 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1188 {
1189 if (module->GetFileSpec())
1190 {
1191 var_name_begin += ::strlen ("file.");
1192
1193 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1194 {
1195 format_file_spec.GetFilename() = module->GetFileSpec().GetFilename();
1196 var_success = format_file_spec;
1197 }
1198 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1199 {
1200 format_file_spec = module->GetFileSpec();
1201 var_success = format_file_spec;
1202 }
1203 }
1204 }
1205 }
1206 }
1207 break;
1208
1209
1210 case 'f':
1211 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1212 {
1213 if (sc && sc->comp_unit != NULL)
1214 {
1215 var_name_begin += ::strlen ("file.");
1216
1217 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1218 {
1219 format_file_spec.GetFilename() = sc->comp_unit->GetFilename();
1220 var_success = format_file_spec;
1221 }
1222 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1223 {
1224 format_file_spec = *sc->comp_unit;
1225 var_success = format_file_spec;
1226 }
1227 }
1228 }
1229 else if (::strncmp (var_name_begin, "frame.", strlen("frame.")) == 0)
1230 {
1231 if (exe_ctx && exe_ctx->frame)
1232 {
1233 var_name_begin += ::strlen ("frame.");
1234 if (::strncmp (var_name_begin, "index}", strlen("index}")) == 0)
1235 {
1236 s.Printf("%u", exe_ctx->frame->GetFrameIndex());
1237 var_success = true;
1238 }
1239 else if (::strncmp (var_name_begin, "pc}", strlen("pc}")) == 0)
1240 {
1241 reg_kind = eRegisterKindGeneric;
1242 reg_num = LLDB_REGNUM_GENERIC_PC;
1243 var_success = true;
1244 }
1245 else if (::strncmp (var_name_begin, "sp}", strlen("sp}")) == 0)
1246 {
1247 reg_kind = eRegisterKindGeneric;
1248 reg_num = LLDB_REGNUM_GENERIC_SP;
1249 var_success = true;
1250 }
1251 else if (::strncmp (var_name_begin, "fp}", strlen("fp}")) == 0)
1252 {
1253 reg_kind = eRegisterKindGeneric;
1254 reg_num = LLDB_REGNUM_GENERIC_FP;
1255 var_success = true;
1256 }
1257 else if (::strncmp (var_name_begin, "flags}", strlen("flags}")) == 0)
1258 {
1259 reg_kind = eRegisterKindGeneric;
1260 reg_num = LLDB_REGNUM_GENERIC_FLAGS;
1261 var_success = true;
1262 }
1263 else if (::strncmp (var_name_begin, "reg.", strlen ("reg.")) == 0)
1264 {
Greg Clayton5ccbd292011-01-06 22:15:06 +00001265 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001266 if (reg_ctx)
1267 {
1268 var_name_begin += ::strlen ("reg.");
1269 if (var_name_begin < var_name_end)
1270 {
1271 std::string reg_name (var_name_begin, var_name_end);
1272 reg_info = reg_ctx->GetRegisterInfoByName (reg_name.c_str());
1273 if (reg_info)
1274 var_success = true;
1275 }
1276 }
1277 }
1278 }
1279 }
1280 else if (::strncmp (var_name_begin, "function.", strlen("function.")) == 0)
1281 {
1282 if (sc && (sc->function != NULL || sc->symbol != NULL))
1283 {
1284 var_name_begin += ::strlen ("function.");
1285 if (::strncmp (var_name_begin, "id}", strlen("id}")) == 0)
1286 {
1287 if (sc->function)
1288 s.Printf("function{0x%8.8x}", sc->function->GetID());
1289 else
1290 s.Printf("symbol[%u]", sc->symbol->GetID());
1291
1292 var_success = true;
1293 }
1294 else if (::strncmp (var_name_begin, "name}", strlen("name}")) == 0)
1295 {
1296 if (sc->function)
1297 cstr = sc->function->GetName().AsCString (NULL);
1298 else if (sc->symbol)
1299 cstr = sc->symbol->GetName().AsCString (NULL);
1300 if (cstr)
1301 {
1302 s.PutCString(cstr);
Greg Clayton0d9c9932010-10-04 17:26:49 +00001303
1304 if (sc->block)
1305 {
1306 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1307 if (inline_block)
1308 {
1309 const InlineFunctionInfo *inline_info = sc->block->GetInlinedFunctionInfo();
1310 if (inline_info)
1311 {
1312 s.PutCString(" [inlined] ");
1313 inline_info->GetName().Dump(&s);
1314 }
1315 }
1316 }
Greg Clayton1b654882010-09-19 02:33:57 +00001317 var_success = true;
1318 }
1319 }
1320 else if (::strncmp (var_name_begin, "addr-offset}", strlen("addr-offset}")) == 0)
1321 {
1322 var_success = addr != NULL;
1323 if (var_success)
1324 {
1325 format_addr = *addr;
1326 calculate_format_addr_function_offset = true;
1327 }
1328 }
1329 else if (::strncmp (var_name_begin, "line-offset}", strlen("line-offset}")) == 0)
1330 {
1331 var_success = sc->line_entry.range.GetBaseAddress().IsValid();
1332 if (var_success)
1333 {
1334 format_addr = sc->line_entry.range.GetBaseAddress();
1335 calculate_format_addr_function_offset = true;
1336 }
1337 }
1338 else if (::strncmp (var_name_begin, "pc-offset}", strlen("pc-offset}")) == 0)
1339 {
1340 var_success = exe_ctx->frame;
1341 if (var_success)
1342 {
1343 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1344 calculate_format_addr_function_offset = true;
1345 }
1346 }
1347 }
1348 }
1349 break;
1350
1351 case 'l':
1352 if (::strncmp (var_name_begin, "line.", strlen("line.")) == 0)
1353 {
1354 if (sc && sc->line_entry.IsValid())
1355 {
1356 var_name_begin += ::strlen ("line.");
1357 if (::strncmp (var_name_begin, "file.", strlen("file.")) == 0)
1358 {
1359 var_name_begin += ::strlen ("file.");
1360
1361 if (::strncmp (var_name_begin, "basename}", strlen("basename}")) == 0)
1362 {
1363 format_file_spec.GetFilename() = sc->line_entry.file.GetFilename();
1364 var_success = format_file_spec;
1365 }
1366 else if (::strncmp (var_name_begin, "fullpath}", strlen("fullpath}")) == 0)
1367 {
1368 format_file_spec = sc->line_entry.file;
1369 var_success = format_file_spec;
1370 }
1371 }
1372 else if (::strncmp (var_name_begin, "number}", strlen("number}")) == 0)
1373 {
1374 var_success = true;
1375 s.Printf("%u", sc->line_entry.line);
1376 }
1377 else if ((::strncmp (var_name_begin, "start-addr}", strlen("start-addr}")) == 0) ||
1378 (::strncmp (var_name_begin, "end-addr}", strlen("end-addr}")) == 0))
1379 {
1380 var_success = sc && sc->line_entry.range.GetBaseAddress().IsValid();
1381 if (var_success)
1382 {
1383 format_addr = sc->line_entry.range.GetBaseAddress();
1384 if (var_name_begin[0] == 'e')
1385 format_addr.Slide (sc->line_entry.range.GetByteSize());
1386 }
1387 }
1388 }
1389 }
1390 break;
1391 }
1392
1393 if (var_success)
1394 {
1395 // If format addr is valid, then we need to print an address
1396 if (reg_num != LLDB_INVALID_REGNUM)
1397 {
1398 // We have a register value to display...
1399 if (reg_num == LLDB_REGNUM_GENERIC_PC && reg_kind == eRegisterKindGeneric)
1400 {
1401 format_addr = exe_ctx->frame->GetFrameCodeAddress();
1402 }
1403 else
1404 {
1405 if (reg_ctx == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +00001406 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Greg Clayton1b654882010-09-19 02:33:57 +00001407
1408 if (reg_ctx)
1409 {
1410 if (reg_kind != kNumRegisterKinds)
1411 reg_num = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
1412 reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_num);
1413 var_success = reg_info != NULL;
1414 }
1415 }
1416 }
1417
1418 if (reg_info != NULL)
1419 {
Greg Clayton7349bd92011-05-09 20:18:18 +00001420 RegisterValue reg_value;
1421 var_success = reg_ctx->ReadRegister (reg_info, reg_value);
1422 if (var_success)
Greg Clayton1b654882010-09-19 02:33:57 +00001423 {
Greg Clayton9a8fa912011-05-15 04:12:07 +00001424 reg_value.Dump(&s, reg_info, false, false, eFormatDefault);
Greg Clayton1b654882010-09-19 02:33:57 +00001425 }
1426 }
1427
1428 if (format_file_spec)
1429 {
1430 s << format_file_spec;
1431 }
1432
1433 // If format addr is valid, then we need to print an address
1434 if (format_addr.IsValid())
1435 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001436 var_success = false;
1437
Greg Clayton1b654882010-09-19 02:33:57 +00001438 if (calculate_format_addr_function_offset)
1439 {
1440 Address func_addr;
Greg Clayton1b654882010-09-19 02:33:57 +00001441
Greg Clayton0603aa92010-10-04 01:05:56 +00001442 if (sc)
1443 {
1444 if (sc->function)
Greg Clayton0d9c9932010-10-04 17:26:49 +00001445 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001446 func_addr = sc->function->GetAddressRange().GetBaseAddress();
Greg Clayton0d9c9932010-10-04 17:26:49 +00001447 if (sc->block)
1448 {
1449 // Check to make sure we aren't in an inline
1450 // function. If we are, use the inline block
1451 // range that contains "format_addr" since
1452 // blocks can be discontiguous.
1453 Block *inline_block = sc->block->GetContainingInlinedBlock ();
1454 AddressRange inline_range;
1455 if (inline_block && inline_block->GetRangeContainingAddress (format_addr, inline_range))
1456 func_addr = inline_range.GetBaseAddress();
1457 }
1458 }
Greg Clayton0603aa92010-10-04 01:05:56 +00001459 else if (sc->symbol && sc->symbol->GetAddressRangePtr())
1460 func_addr = sc->symbol->GetAddressRangePtr()->GetBaseAddress();
1461 }
1462
1463 if (func_addr.IsValid())
Greg Clayton1b654882010-09-19 02:33:57 +00001464 {
1465 if (func_addr.GetSection() == format_addr.GetSection())
1466 {
1467 addr_t func_file_addr = func_addr.GetFileAddress();
1468 addr_t addr_file_addr = format_addr.GetFileAddress();
1469 if (addr_file_addr > func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001470 s.Printf(" + %llu", addr_file_addr - func_file_addr);
Greg Clayton1b654882010-09-19 02:33:57 +00001471 else if (addr_file_addr < func_file_addr)
Greg Clayton1b654882010-09-19 02:33:57 +00001472 s.Printf(" - %llu", func_file_addr - addr_file_addr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001473 var_success = true;
Greg Clayton1b654882010-09-19 02:33:57 +00001474 }
1475 else
Greg Clayton0603aa92010-10-04 01:05:56 +00001476 {
1477 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
1478 if (target)
1479 {
1480 addr_t func_load_addr = func_addr.GetLoadAddress (target);
1481 addr_t addr_load_addr = format_addr.GetLoadAddress (target);
1482 if (addr_load_addr > func_load_addr)
1483 s.Printf(" + %llu", addr_load_addr - func_load_addr);
1484 else if (addr_load_addr < func_load_addr)
1485 s.Printf(" - %llu", func_load_addr - addr_load_addr);
1486 var_success = true;
1487 }
1488 }
Greg Clayton1b654882010-09-19 02:33:57 +00001489 }
1490 }
1491 else
1492 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001493 Target *target = Target::GetTargetFromContexts (exe_ctx, sc);
Greg Clayton1b654882010-09-19 02:33:57 +00001494 addr_t vaddr = LLDB_INVALID_ADDRESS;
Greg Clayton0603aa92010-10-04 01:05:56 +00001495 if (exe_ctx && !target->GetSectionLoadList().IsEmpty())
1496 vaddr = format_addr.GetLoadAddress (target);
Greg Clayton1b654882010-09-19 02:33:57 +00001497 if (vaddr == LLDB_INVALID_ADDRESS)
1498 vaddr = format_addr.GetFileAddress ();
1499
1500 if (vaddr != LLDB_INVALID_ADDRESS)
Greg Clayton0603aa92010-10-04 01:05:56 +00001501 {
Greg Clayton514487e2011-02-15 21:59:32 +00001502 int addr_width = target->GetArchitecture().GetAddressByteSize() * 2;
Greg Clayton35f1a0d2010-11-19 04:16:11 +00001503 if (addr_width == 0)
1504 addr_width = 16;
1505 s.Printf("0x%*.*llx", addr_width, addr_width, vaddr);
Greg Clayton0603aa92010-10-04 01:05:56 +00001506 var_success = true;
1507 }
Greg Clayton1b654882010-09-19 02:33:57 +00001508 }
1509 }
1510 }
1511
1512 if (var_success == false)
1513 success = false;
1514 }
1515 p = var_name_end;
1516 }
1517 else
1518 break;
1519 }
1520 else
1521 {
1522 // We got a dollar sign with no '{' after it, it must just be a dollar sign
1523 s.PutChar(*p);
1524 }
1525 }
1526 else if (*p == '\\')
1527 {
1528 ++p; // skip the slash
1529 switch (*p)
1530 {
1531 case 'a': s.PutChar ('\a'); break;
1532 case 'b': s.PutChar ('\b'); break;
1533 case 'f': s.PutChar ('\f'); break;
1534 case 'n': s.PutChar ('\n'); break;
1535 case 'r': s.PutChar ('\r'); break;
1536 case 't': s.PutChar ('\t'); break;
1537 case 'v': s.PutChar ('\v'); break;
1538 case '\'': s.PutChar ('\''); break;
1539 case '\\': s.PutChar ('\\'); break;
1540 case '0':
1541 // 1 to 3 octal chars
1542 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001543 // Make a string that can hold onto the initial zero char,
1544 // up to 3 octal digits, and a terminating NULL.
1545 char oct_str[5] = { 0, 0, 0, 0, 0 };
1546
1547 int i;
1548 for (i=0; (p[i] >= '0' && p[i] <= '7') && i<4; ++i)
1549 oct_str[i] = p[i];
1550
1551 // We don't want to consume the last octal character since
1552 // the main for loop will do this for us, so we advance p by
1553 // one less than i (even if i is zero)
1554 p += i - 1;
1555 unsigned long octal_value = ::strtoul (oct_str, NULL, 8);
1556 if (octal_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001557 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001558 char octal_char = octal_value;
1559 s.Write (&octal_char, 1);
Greg Clayton1b654882010-09-19 02:33:57 +00001560 }
Greg Clayton1b654882010-09-19 02:33:57 +00001561 }
1562 break;
1563
1564 case 'x':
1565 // hex number in the format
Greg Clayton0603aa92010-10-04 01:05:56 +00001566 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001567 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001568 ++p; // Skip the 'x'
Greg Clayton1b654882010-09-19 02:33:57 +00001569
Greg Clayton0603aa92010-10-04 01:05:56 +00001570 // Make a string that can hold onto two hex chars plus a
1571 // NULL terminator
1572 char hex_str[3] = { 0,0,0 };
1573 hex_str[0] = *p;
1574 if (isxdigit(p[1]))
Greg Clayton1b654882010-09-19 02:33:57 +00001575 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001576 ++p; // Skip the first of the two hex chars
1577 hex_str[1] = *p;
1578 }
1579
1580 unsigned long hex_value = strtoul (hex_str, NULL, 16);
1581 if (hex_value <= UINT8_MAX)
Greg Clayton1b654882010-09-19 02:33:57 +00001582 s.PutChar (hex_value);
Greg Clayton0603aa92010-10-04 01:05:56 +00001583 }
1584 else
1585 {
1586 s.PutChar('x');
Greg Clayton1b654882010-09-19 02:33:57 +00001587 }
1588 break;
1589
1590 default:
Greg Clayton0603aa92010-10-04 01:05:56 +00001591 // Just desensitize any other character by just printing what
1592 // came after the '\'
1593 s << *p;
Greg Clayton1b654882010-09-19 02:33:57 +00001594 break;
1595
1596 }
1597
1598 }
1599 }
1600 if (end)
1601 *end = p;
1602 return success;
1603}
1604
Greg Clayton4a33d312011-06-23 17:59:56 +00001605
1606static FormatManager&
1607GetFormatManager() {
1608 static FormatManager g_format_manager;
1609 return g_format_manager;
1610}
1611
1612bool
Enrico Granata4becb372011-06-29 22:27:15 +00001613Debugger::ValueFormats::Get(ValueObject& vobj, ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001614{
Enrico Granata4becb372011-06-29 22:27:15 +00001615 return GetFormatManager().Value().Get(vobj,entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001616}
1617
1618void
Enrico Granata4becb372011-06-29 22:27:15 +00001619Debugger::ValueFormats::Add(const ConstString &type, const ValueFormat::SharedPointer &entry)
Greg Clayton4a33d312011-06-23 17:59:56 +00001620{
Enrico Granata4becb372011-06-29 22:27:15 +00001621 GetFormatManager().Value().Add(type.AsCString(),entry);
Greg Clayton4a33d312011-06-23 17:59:56 +00001622}
1623
1624bool
Enrico Granata4becb372011-06-29 22:27:15 +00001625Debugger::ValueFormats::Delete(const ConstString &type)
Greg Clayton4a33d312011-06-23 17:59:56 +00001626{
Enrico Granata4becb372011-06-29 22:27:15 +00001627 return GetFormatManager().Value().Delete(type.AsCString());
Greg Clayton4a33d312011-06-23 17:59:56 +00001628}
1629
1630void
Enrico Granata4becb372011-06-29 22:27:15 +00001631Debugger::ValueFormats::Clear()
Greg Clayton4a33d312011-06-23 17:59:56 +00001632{
Enrico Granata4becb372011-06-29 22:27:15 +00001633 GetFormatManager().Value().Clear();
1634}
1635
1636void
1637Debugger::ValueFormats::LoopThrough(FormatManager::ValueCallback callback, void* callback_baton)
1638{
1639 GetFormatManager().Value().LoopThrough(callback, callback_baton);
1640}
1641
1642uint32_t
1643Debugger::ValueFormats::GetCurrentRevision()
1644{
1645 return GetFormatManager().GetCurrentRevision();
1646}
1647
1648
1649bool
1650Debugger::SummaryFormats::Get(ValueObject& vobj, SummaryFormat::SharedPointer &entry)
1651{
1652 return GetFormatManager().Summary().Get(vobj,entry);
1653}
1654
1655void
1656Debugger::SummaryFormats::Add(const ConstString &type, const SummaryFormat::SharedPointer &entry)
1657{
1658 GetFormatManager().Summary().Add(type.AsCString(),entry);
1659}
1660
1661bool
1662Debugger::SummaryFormats::Delete(const ConstString &type)
1663{
1664 return GetFormatManager().Summary().Delete(type.AsCString());
1665}
1666
1667void
1668Debugger::SummaryFormats::Clear()
1669{
1670 GetFormatManager().Summary().Clear();
1671}
1672
1673void
1674Debugger::SummaryFormats::LoopThrough(FormatManager::SummaryCallback callback, void* callback_baton)
1675{
1676 GetFormatManager().Summary().LoopThrough(callback, callback_baton);
1677}
1678
1679uint32_t
1680Debugger::SummaryFormats::GetCurrentRevision()
1681{
1682 return GetFormatManager().GetCurrentRevision();
Greg Clayton4a33d312011-06-23 17:59:56 +00001683}
1684
Greg Clayton1b654882010-09-19 02:33:57 +00001685#pragma mark Debugger::SettingsController
1686
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001687//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00001688// class Debugger::SettingsController
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001689//--------------------------------------------------
1690
Greg Clayton1b654882010-09-19 02:33:57 +00001691Debugger::SettingsController::SettingsController () :
Caroline Tice101c7c22010-09-09 06:25:08 +00001692 UserSettingsController ("", lldb::UserSettingsControllerSP())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001693{
Caroline Tice91123da2010-09-08 17:48:55 +00001694 m_default_settings.reset (new DebuggerInstanceSettings (*this, false,
1695 InstanceSettings::GetDefaultName().AsCString()));
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001696}
1697
Greg Clayton1b654882010-09-19 02:33:57 +00001698Debugger::SettingsController::~SettingsController ()
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001699{
1700}
1701
1702
1703lldb::InstanceSettingsSP
Greg Clayton1b654882010-09-19 02:33:57 +00001704Debugger::SettingsController::CreateInstanceSettings (const char *instance_name)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001705{
Greg Claytondbe54502010-11-19 03:46:01 +00001706 DebuggerInstanceSettings *new_settings = new DebuggerInstanceSettings (*GetSettingsController(),
Caroline Tice91123da2010-09-08 17:48:55 +00001707 false, instance_name);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001708 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1709 return new_settings_sp;
1710}
1711
Greg Clayton1b654882010-09-19 02:33:57 +00001712#pragma mark DebuggerInstanceSettings
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001713//--------------------------------------------------
1714// class DebuggerInstanceSettings
1715//--------------------------------------------------
1716
Greg Claytona7015092010-09-18 01:14:36 +00001717DebuggerInstanceSettings::DebuggerInstanceSettings
1718(
1719 UserSettingsController &owner,
1720 bool live_instance,
1721 const char *name
1722) :
Greg Clayton85851dd2010-12-04 00:10:17 +00001723 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytona7015092010-09-18 01:14:36 +00001724 m_term_width (80),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001725 m_prompt (),
Greg Clayton0603aa92010-10-04 01:05:56 +00001726 m_frame_format (),
1727 m_thread_format (),
Caroline Ticedaccaa92010-09-20 20:44:43 +00001728 m_script_lang (),
Jim Ingham3bcdb292010-10-04 22:44:14 +00001729 m_use_external_editor (false),
1730 m_auto_confirm_on (false)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001731{
Caroline Ticef20e8232010-09-09 18:26:37 +00001732 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1733 // until the vtables for DebuggerInstanceSettings are properly set up, i.e. AFTER all the initializers.
1734 // 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 +00001735 // The same is true of CreateInstanceName().
1736
1737 if (GetInstanceName() == InstanceSettings::InvalidName())
1738 {
1739 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1740 m_owner.RegisterInstanceSettings (this);
1741 }
Caroline Ticef20e8232010-09-09 18:26:37 +00001742
1743 if (live_instance)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001744 {
1745 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1746 CopyInstanceSettings (pending_settings, false);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001747 }
1748}
1749
1750DebuggerInstanceSettings::DebuggerInstanceSettings (const DebuggerInstanceSettings &rhs) :
Greg Clayton99d0faf2010-11-18 23:32:35 +00001751 InstanceSettings (*Debugger::GetSettingsController(), CreateInstanceName ().AsCString()),
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001752 m_prompt (rhs.m_prompt),
Greg Clayton0603aa92010-10-04 01:05:56 +00001753 m_frame_format (rhs.m_frame_format),
1754 m_thread_format (rhs.m_thread_format),
Caroline Ticedaccaa92010-09-20 20:44:43 +00001755 m_script_lang (rhs.m_script_lang),
Jim Ingham3bcdb292010-10-04 22:44:14 +00001756 m_use_external_editor (rhs.m_use_external_editor),
1757 m_auto_confirm_on(rhs.m_auto_confirm_on)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001758{
1759 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1760 CopyInstanceSettings (pending_settings, false);
1761 m_owner.RemovePendingSettings (m_instance_name);
1762}
1763
1764DebuggerInstanceSettings::~DebuggerInstanceSettings ()
1765{
1766}
1767
1768DebuggerInstanceSettings&
1769DebuggerInstanceSettings::operator= (const DebuggerInstanceSettings &rhs)
1770{
1771 if (this != &rhs)
1772 {
Greg Clayton1b654882010-09-19 02:33:57 +00001773 m_term_width = rhs.m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001774 m_prompt = rhs.m_prompt;
Greg Clayton0603aa92010-10-04 01:05:56 +00001775 m_frame_format = rhs.m_frame_format;
1776 m_thread_format = rhs.m_thread_format;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001777 m_script_lang = rhs.m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00001778 m_use_external_editor = rhs.m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00001779 m_auto_confirm_on = rhs.m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001780 }
1781
1782 return *this;
1783}
1784
Greg Clayton1b654882010-09-19 02:33:57 +00001785bool
1786DebuggerInstanceSettings::ValidTermWidthValue (const char *value, Error err)
1787{
1788 bool valid = false;
1789
1790 // Verify we have a value string.
1791 if (value == NULL || value[0] == '\0')
1792 {
1793 err.SetErrorString ("Missing value. Can't set terminal width without a value.\n");
1794 }
1795 else
1796 {
1797 char *end = NULL;
1798 const uint32_t width = ::strtoul (value, &end, 0);
1799
Johnny Chenea9fc182010-09-20 16:36:43 +00001800 if (end && end[0] == '\0')
Greg Clayton1b654882010-09-19 02:33:57 +00001801 {
Johnny Chen433d7742010-09-20 17:04:41 +00001802 if (width >= 10 && width <= 1024)
Greg Clayton1b654882010-09-19 02:33:57 +00001803 valid = true;
1804 else
1805 err.SetErrorString ("Invalid term-width value; value must be between 10 and 1024.\n");
1806 }
1807 else
1808 err.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string.\n", value);
1809 }
1810
1811 return valid;
1812}
1813
1814
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001815void
1816DebuggerInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1817 const char *index_value,
1818 const char *value,
1819 const ConstString &instance_name,
1820 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:54 +00001821 VarSetOperationType op,
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001822 Error &err,
1823 bool pending)
1824{
Greg Clayton0603aa92010-10-04 01:05:56 +00001825
1826 if (var_name == TermWidthVarName())
1827 {
1828 if (ValidTermWidthValue (value, err))
1829 {
1830 m_term_width = ::strtoul (value, NULL, 0);
1831 }
1832 }
1833 else if (var_name == PromptVarName())
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001834 {
Caroline Tice101c7c22010-09-09 06:25:08 +00001835 UserSettingsController::UpdateStringVariable (op, m_prompt, value, err);
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001836 if (!pending)
1837 {
Caroline Tice49e27372010-09-07 18:35:40 +00001838 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
1839 // strip off the brackets before passing it to BroadcastPromptChange.
1840
1841 std::string tmp_instance_name (instance_name.AsCString());
1842 if ((tmp_instance_name[0] == '[')
1843 && (tmp_instance_name[instance_name.GetLength() - 1] == ']'))
1844 tmp_instance_name = tmp_instance_name.substr (1, instance_name.GetLength() - 2);
1845 ConstString new_name (tmp_instance_name.c_str());
1846
1847 BroadcastPromptChange (new_name, m_prompt.c_str());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001848 }
1849 }
Greg Clayton0603aa92010-10-04 01:05:56 +00001850 else if (var_name == GetFrameFormatName())
1851 {
1852 UserSettingsController::UpdateStringVariable (op, m_frame_format, value, err);
1853 }
1854 else if (var_name == GetThreadFormatName())
1855 {
1856 UserSettingsController::UpdateStringVariable (op, m_thread_format, value, err);
1857 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001858 else if (var_name == ScriptLangVarName())
1859 {
1860 bool success;
1861 m_script_lang = Args::StringToScriptLanguage (value, eScriptLanguageDefault,
1862 &success);
1863 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001864 else if (var_name == UseExternalEditorVarName ())
1865 {
Greg Clayton385aa282011-04-22 03:55:06 +00001866 UserSettingsController::UpdateBooleanVariable (op, m_use_external_editor, value, false, err);
Caroline Ticedaccaa92010-09-20 20:44:43 +00001867 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00001868 else if (var_name == AutoConfirmName ())
1869 {
Greg Clayton385aa282011-04-22 03:55:06 +00001870 UserSettingsController::UpdateBooleanVariable (op, m_auto_confirm_on, value, false, err);
Jim Ingham3bcdb292010-10-04 22:44:14 +00001871 }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001872}
1873
Caroline Tice12cecd72010-09-20 21:37:42 +00001874bool
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001875DebuggerInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1876 const ConstString &var_name,
Caroline Ticedaccaa92010-09-20 20:44:43 +00001877 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:42 +00001878 Error *err)
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001879{
1880 if (var_name == PromptVarName())
1881 {
Greg Clayton0603aa92010-10-04 01:05:56 +00001882 value.AppendString (m_prompt.c_str(), m_prompt.size());
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001883
1884 }
1885 else if (var_name == ScriptLangVarName())
1886 {
1887 value.AppendString (ScriptInterpreter::LanguageToString (m_script_lang).c_str());
1888 }
Caroline Tice101c7c22010-09-09 06:25:08 +00001889 else if (var_name == TermWidthVarName())
1890 {
1891 StreamString width_str;
1892 width_str.Printf ("%d", m_term_width);
1893 value.AppendString (width_str.GetData());
1894 }
Greg Clayton0603aa92010-10-04 01:05:56 +00001895 else if (var_name == GetFrameFormatName ())
1896 {
1897 value.AppendString(m_frame_format.c_str(), m_frame_format.size());
1898 }
1899 else if (var_name == GetThreadFormatName ())
1900 {
1901 value.AppendString(m_thread_format.c_str(), m_thread_format.size());
1902 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001903 else if (var_name == UseExternalEditorVarName())
1904 {
1905 if (m_use_external_editor)
1906 value.AppendString ("true");
1907 else
1908 value.AppendString ("false");
1909 }
Jim Ingham3bcdb292010-10-04 22:44:14 +00001910 else if (var_name == AutoConfirmName())
1911 {
1912 if (m_auto_confirm_on)
1913 value.AppendString ("true");
1914 else
1915 value.AppendString ("false");
1916 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001917 else
Caroline Tice12cecd72010-09-20 21:37:42 +00001918 {
1919 if (err)
1920 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1921 return false;
1922 }
1923 return true;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001924}
1925
1926void
1927DebuggerInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
1928 bool pending)
1929{
1930 if (new_settings.get() == NULL)
1931 return;
1932
1933 DebuggerInstanceSettings *new_debugger_settings = (DebuggerInstanceSettings *) new_settings.get();
1934
1935 m_prompt = new_debugger_settings->m_prompt;
1936 if (!pending)
Caroline Tice49e27372010-09-07 18:35:40 +00001937 {
1938 // 'instance_name' is actually (probably) in the form '[<instance_name>]'; if so, we need to
1939 // strip off the brackets before passing it to BroadcastPromptChange.
1940
1941 std::string tmp_instance_name (m_instance_name.AsCString());
1942 if ((tmp_instance_name[0] == '[')
1943 && (tmp_instance_name[m_instance_name.GetLength() - 1] == ']'))
1944 tmp_instance_name = tmp_instance_name.substr (1, m_instance_name.GetLength() - 2);
1945 ConstString new_name (tmp_instance_name.c_str());
1946
1947 BroadcastPromptChange (new_name, m_prompt.c_str());
1948 }
Greg Clayton0603aa92010-10-04 01:05:56 +00001949 m_frame_format = new_debugger_settings->m_frame_format;
1950 m_thread_format = new_debugger_settings->m_thread_format;
Caroline Ticedaccaa92010-09-20 20:44:43 +00001951 m_term_width = new_debugger_settings->m_term_width;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001952 m_script_lang = new_debugger_settings->m_script_lang;
Caroline Ticedaccaa92010-09-20 20:44:43 +00001953 m_use_external_editor = new_debugger_settings->m_use_external_editor;
Jim Ingham3bcdb292010-10-04 22:44:14 +00001954 m_auto_confirm_on = new_debugger_settings->m_auto_confirm_on;
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001955}
1956
Caroline Tice3df9a8d2010-09-04 00:03:46 +00001957
1958bool
1959DebuggerInstanceSettings::BroadcastPromptChange (const ConstString &instance_name, const char *new_prompt)
1960{
1961 std::string tmp_prompt;
1962
1963 if (new_prompt != NULL)
1964 {
1965 tmp_prompt = new_prompt ;
1966 int len = tmp_prompt.size();
1967 if (len > 1
1968 && (tmp_prompt[0] == '\'' || tmp_prompt[0] == '"')
1969 && (tmp_prompt[len-1] == tmp_prompt[0]))
1970 {
1971 tmp_prompt = tmp_prompt.substr(1,len-2);
1972 }
1973 len = tmp_prompt.size();
1974 if (tmp_prompt[len-1] != ' ')
1975 tmp_prompt.append(" ");
1976 }
1977 EventSP new_event_sp;
1978 new_event_sp.reset (new Event(CommandInterpreter::eBroadcastBitResetPrompt,
1979 new EventDataBytes (tmp_prompt.c_str())));
1980
1981 if (instance_name.GetLength() != 0)
1982 {
1983 // Set prompt for a particular instance.
1984 Debugger *dbg = Debugger::FindDebuggerWithInstanceName (instance_name).get();
1985 if (dbg != NULL)
1986 {
1987 dbg->GetCommandInterpreter().BroadcastEvent (new_event_sp);
1988 }
1989 }
1990
1991 return true;
1992}
1993
1994const ConstString
1995DebuggerInstanceSettings::CreateInstanceName ()
1996{
1997 static int instance_count = 1;
1998 StreamString sstr;
1999
2000 sstr.Printf ("debugger_%d", instance_count);
2001 ++instance_count;
2002
2003 const ConstString ret_val (sstr.GetData());
2004
2005 return ret_val;
2006}
2007
2008const ConstString &
2009DebuggerInstanceSettings::PromptVarName ()
2010{
2011 static ConstString prompt_var_name ("prompt");
2012
2013 return prompt_var_name;
2014}
2015
2016const ConstString &
Greg Clayton0603aa92010-10-04 01:05:56 +00002017DebuggerInstanceSettings::GetFrameFormatName ()
2018{
2019 static ConstString prompt_var_name ("frame-format");
2020
2021 return prompt_var_name;
2022}
2023
2024const ConstString &
2025DebuggerInstanceSettings::GetThreadFormatName ()
2026{
2027 static ConstString prompt_var_name ("thread-format");
2028
2029 return prompt_var_name;
2030}
2031
2032const ConstString &
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002033DebuggerInstanceSettings::ScriptLangVarName ()
2034{
2035 static ConstString script_lang_var_name ("script-lang");
2036
2037 return script_lang_var_name;
2038}
2039
Caroline Tice101c7c22010-09-09 06:25:08 +00002040const ConstString &
2041DebuggerInstanceSettings::TermWidthVarName ()
2042{
2043 static ConstString term_width_var_name ("term-width");
2044
2045 return term_width_var_name;
2046}
2047
Caroline Ticedaccaa92010-09-20 20:44:43 +00002048const ConstString &
2049DebuggerInstanceSettings::UseExternalEditorVarName ()
2050{
2051 static ConstString use_external_editor_var_name ("use-external-editor");
2052
2053 return use_external_editor_var_name;
2054}
2055
Jim Ingham3bcdb292010-10-04 22:44:14 +00002056const ConstString &
2057DebuggerInstanceSettings::AutoConfirmName ()
2058{
2059 static ConstString use_external_editor_var_name ("auto-confirm");
2060
2061 return use_external_editor_var_name;
2062}
2063
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002064//--------------------------------------------------
Greg Clayton1b654882010-09-19 02:33:57 +00002065// SettingsController Variable Tables
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002066//--------------------------------------------------
2067
2068
2069SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002070Debugger::SettingsController::global_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002071{
2072 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
Caroline Tice101c7c22010-09-09 06:25:08 +00002073 // The Debugger level global table should always be empty; all Debugger settable variables should be instance
2074 // variables.
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002075 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
2076};
2077
Greg Claytonbb562b12010-10-04 02:44:26 +00002078#define MODULE_WITH_FUNC "{ ${module.file.basename}{`${function.name}${function.pc-offset}}}"
Greg Clayton0603aa92010-10-04 01:05:56 +00002079#define FILE_AND_LINE "{ at ${line.file.basename}:${line.number}}"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002080
Greg Clayton0603aa92010-10-04 01:05:56 +00002081#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2082 "{, ${frame.pc}}"\
2083 MODULE_WITH_FUNC\
Greg Claytoncf4b9072010-10-04 17:04:23 +00002084 FILE_AND_LINE\
Greg Clayton0603aa92010-10-04 01:05:56 +00002085 "{, stop reason = ${thread.stop-reason}}"\
Greg Clayton0603aa92010-10-04 01:05:56 +00002086 "\\n"
2087
Greg Clayton315d2ca2010-11-02 01:53:21 +00002088//#define DEFAULT_THREAD_FORMAT "thread #${thread.index}: tid = ${thread.id}"\
2089// "{, ${frame.pc}}"\
2090// MODULE_WITH_FUNC\
2091// FILE_AND_LINE\
2092// "{, stop reason = ${thread.stop-reason}}"\
2093// "{, name = ${thread.name}}"\
2094// "{, queue = ${thread.queue}}"\
2095// "\\n"
2096
Greg Clayton0603aa92010-10-04 01:05:56 +00002097#define DEFAULT_FRAME_FORMAT "frame #${frame.index}: ${frame.pc}"\
2098 MODULE_WITH_FUNC\
2099 FILE_AND_LINE\
2100 "\\n"
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002101
2102SettingEntry
Greg Clayton1b654882010-09-19 02:33:57 +00002103Debugger::SettingsController::instance_settings_table[] =
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002104{
Greg Clayton0603aa92010-10-04 01:05:56 +00002105// NAME Setting variable type Default Enum Init'd Hidden Help
2106// ======================= ======================= ====================== ==== ====== ====== ======================
Greg Clayton0603aa92010-10-04 01:05:56 +00002107{ "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 +00002108{ "prompt", eSetVarTypeString, "(lldb) ", NULL, false, false, "The debugger command line prompt displayed for the user." },
Jim Ingham3bcdb292010-10-04 22:44:14 +00002109{ "script-lang", eSetVarTypeString, "python", NULL, false, false, "The script language to be used for evaluating user-written scripts." },
2110{ "term-width", eSetVarTypeInt, "80" , NULL, false, false, "The maximum number of columns to use for displaying text." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002111{ "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 +00002112{ "use-external-editor", eSetVarTypeBoolean, "false", NULL, false, false, "Whether to use an external editor or not." },
2113{ "auto-confirm", eSetVarTypeBoolean, "false", NULL, false, false, "If true all confirmation prompts will receive their default reply." },
Greg Clayton0603aa92010-10-04 01:05:56 +00002114{ NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL }
Caroline Tice3df9a8d2010-09-04 00:03:46 +00002115};