blob: 600a06bc137dd3961f477ac5a2fa32c2b2d86da9 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.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
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton427f2902010-12-14 02:59:59 +000020#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Caroline Tice4a348082011-05-02 20:41:46 +000023#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000025#include "lldb/Core/Timer.h"
26#include "lldb/Core/ValueObject.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000027#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000029#include "lldb/Interpreter/CommandInterpreter.h"
30#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/lldb-private-log.h"
32#include "lldb/Symbol/ObjectFile.h"
33#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000034#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000035#include "lldb/Target/Thread.h"
36#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
41//----------------------------------------------------------------------
42// Target constructor
43//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000044Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
45 Broadcaster ("lldb.target"),
46 ExecutionContextScope (),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000047 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000048 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000049 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000050 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000051 m_arch (target_arch),
52 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000053 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000054 m_breakpoint_list (false),
55 m_internal_breakpoint_list (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000056 m_process_sp (),
57 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000058 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000059 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000060 m_persistent_variables (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000061 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000062 m_stop_hook_next_id (0),
63 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000064{
Greg Clayton49ce6822010-10-31 03:01:06 +000065 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
66 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
67 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
68
Greg Claytone005f2c2010-11-06 01:53:30 +000069 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000070 if (log)
71 log->Printf ("%p Target::Target()", this);
72}
73
74//----------------------------------------------------------------------
75// Destructor
76//----------------------------------------------------------------------
77Target::~Target()
78{
Greg Claytone005f2c2010-11-06 01:53:30 +000079 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000080 if (log)
81 log->Printf ("%p Target::~Target()", this);
82 DeleteCurrentProcess ();
83}
84
85void
Caroline Tice7826c882010-10-26 03:11:13 +000086Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000087{
Greg Clayton3fed8b92010-10-08 00:21:05 +000088// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000089 if (description_level != lldb::eDescriptionLevelBrief)
90 {
91 s->Indent();
92 s->PutCString("Target\n");
93 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000094 m_images.Dump(s);
95 m_breakpoint_list.Dump(s);
96 m_internal_breakpoint_list.Dump(s);
97 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +000098 }
99 else
100 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000101 Module *exe_module = GetExecutableModulePointer();
102 if (exe_module)
103 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000104 else
105 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000106 }
Chris Lattner24943d22010-06-08 16:52:24 +0000107}
108
109void
110Target::DeleteCurrentProcess ()
111{
112 if (m_process_sp.get())
113 {
Greg Clayton49480b12010-09-14 23:52:43 +0000114 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000115 if (m_process_sp->IsAlive())
116 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000117
118 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000119
120 // Do any cleanup of the target we need to do between process instances.
121 // NB It is better to do this before destroying the process in case the
122 // clean up needs some help from the process.
123 m_breakpoint_list.ClearAllBreakpointSites();
124 m_internal_breakpoint_list.ClearAllBreakpointSites();
125 m_process_sp.reset();
126 }
127}
128
129const lldb::ProcessSP &
130Target::CreateProcess (Listener &listener, const char *plugin_name)
131{
132 DeleteCurrentProcess ();
133 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
134 return m_process_sp;
135}
136
137const lldb::ProcessSP &
138Target::GetProcessSP () const
139{
140 return m_process_sp;
141}
142
143lldb::TargetSP
144Target::GetSP()
145{
Greg Clayton63094e02010-06-23 01:19:29 +0000146 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000147}
148
Greg Clayton153ccd72011-08-10 02:10:13 +0000149void
150Target::Destroy()
151{
152 Mutex::Locker locker (m_mutex);
153 DeleteCurrentProcess ();
154 m_platform_sp.reset();
155 m_arch.Clear();
156 m_images.Clear();
157 m_section_load_list.Clear();
158 const bool notify = false;
159 m_breakpoint_list.RemoveAll(notify);
160 m_internal_breakpoint_list.RemoveAll(notify);
161 m_last_created_breakpoint.reset();
162 m_search_filter_sp.reset();
163 m_image_search_paths.Clear(notify);
164 m_scratch_ast_context_ap.reset();
165 m_persistent_variables.Clear();
166 m_stop_hooks.clear();
167 m_stop_hook_next_id = 0;
168 m_suppress_stop_hooks = false;
169}
170
171
Chris Lattner24943d22010-06-08 16:52:24 +0000172BreakpointList &
173Target::GetBreakpointList(bool internal)
174{
175 if (internal)
176 return m_internal_breakpoint_list;
177 else
178 return m_breakpoint_list;
179}
180
181const BreakpointList &
182Target::GetBreakpointList(bool internal) const
183{
184 if (internal)
185 return m_internal_breakpoint_list;
186 else
187 return m_breakpoint_list;
188}
189
190BreakpointSP
191Target::GetBreakpointByID (break_id_t break_id)
192{
193 BreakpointSP bp_sp;
194
195 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
196 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
197 else
198 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
199
200 return bp_sp;
201}
202
203BreakpointSP
204Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
205{
206 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
207 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
208 return CreateBreakpoint (filter_sp, resolver_sp, internal);
209}
210
211
212BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000213Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000214{
Chris Lattner24943d22010-06-08 16:52:24 +0000215 Address so_addr;
216 // Attempt to resolve our load address if possible, though it is ok if
217 // it doesn't resolve to section/offset.
218
Greg Clayton33ed1702010-08-24 00:45:41 +0000219 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000220 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000221 if (!so_addr.IsValid())
222 {
223 // The address didn't resolve, so just set this as an absolute address
224 so_addr.SetOffset (addr);
225 }
226 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000227 return bp_sp;
228}
229
230BreakpointSP
231Target::CreateBreakpoint (Address &addr, bool internal)
232{
233 TargetSP target_sp = this->GetSP();
234 SearchFilterSP filter_sp(new SearchFilter (target_sp));
235 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
236 return CreateBreakpoint (filter_sp, resolver_sp, internal);
237}
238
239BreakpointSP
Greg Clayton7dd98df2011-07-12 17:06:17 +0000240Target::CreateBreakpoint (const FileSpec *containingModule,
241 const char *func_name,
242 uint32_t func_name_type_mask,
243 bool internal,
244 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000245{
Greg Clayton12bec712010-06-28 21:30:43 +0000246 BreakpointSP bp_sp;
247 if (func_name)
248 {
249 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000250
251 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
252 func_name,
253 func_name_type_mask,
254 Breakpoint::Exact,
255 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000256 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
257 }
258 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000259}
260
261
262SearchFilterSP
263Target::GetSearchFilterForModule (const FileSpec *containingModule)
264{
265 SearchFilterSP filter_sp;
266 lldb::TargetSP target_sp = this->GetSP();
267 if (containingModule != NULL)
268 {
269 // TODO: We should look into sharing module based search filters
270 // across many breakpoints like we do for the simple target based one
271 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
272 }
273 else
274 {
275 if (m_search_filter_sp.get() == NULL)
276 m_search_filter_sp.reset (new SearchFilter (target_sp));
277 filter_sp = m_search_filter_sp;
278 }
279 return filter_sp;
280}
281
282BreakpointSP
Greg Clayton7dd98df2011-07-12 17:06:17 +0000283Target::CreateBreakpoint (const FileSpec *containingModule,
284 RegularExpression &func_regex,
285 bool internal,
286 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000287{
288 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000289 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
290 func_regex,
291 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000292
293 return CreateBreakpoint (filter_sp, resolver_sp, internal);
294}
295
296BreakpointSP
297Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
298{
299 BreakpointSP bp_sp;
300 if (filter_sp && resolver_sp)
301 {
302 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
303 resolver_sp->SetBreakpoint (bp_sp.get());
304
305 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000306 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000307 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000308 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000309
Greg Claytone005f2c2010-11-06 01:53:30 +0000310 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000311 if (log)
312 {
313 StreamString s;
314 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
315 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
316 }
317
Chris Lattner24943d22010-06-08 16:52:24 +0000318 bp_sp->ResolveBreakpoint();
319 }
Jim Inghamd1686902010-10-14 23:45:03 +0000320
321 if (!internal && bp_sp)
322 {
323 m_last_created_breakpoint = bp_sp;
324 }
325
Chris Lattner24943d22010-06-08 16:52:24 +0000326 return bp_sp;
327}
328
329void
330Target::RemoveAllBreakpoints (bool internal_also)
331{
Greg Claytone005f2c2010-11-06 01:53:30 +0000332 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000333 if (log)
334 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
335
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000336 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000337 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000338 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000339
340 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000341}
342
343void
344Target::DisableAllBreakpoints (bool internal_also)
345{
Greg Claytone005f2c2010-11-06 01:53:30 +0000346 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000347 if (log)
348 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
349
350 m_breakpoint_list.SetEnabledAll (false);
351 if (internal_also)
352 m_internal_breakpoint_list.SetEnabledAll (false);
353}
354
355void
356Target::EnableAllBreakpoints (bool internal_also)
357{
Greg Claytone005f2c2010-11-06 01:53:30 +0000358 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000359 if (log)
360 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
361
362 m_breakpoint_list.SetEnabledAll (true);
363 if (internal_also)
364 m_internal_breakpoint_list.SetEnabledAll (true);
365}
366
367bool
368Target::RemoveBreakpointByID (break_id_t break_id)
369{
Greg Claytone005f2c2010-11-06 01:53:30 +0000370 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000371 if (log)
372 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
373
374 if (DisableBreakpointByID (break_id))
375 {
376 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000377 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000378 else
Jim Inghamd1686902010-10-14 23:45:03 +0000379 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000380 if (m_last_created_breakpoint)
381 {
382 if (m_last_created_breakpoint->GetID() == break_id)
383 m_last_created_breakpoint.reset();
384 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000385 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000386 }
Chris Lattner24943d22010-06-08 16:52:24 +0000387 return true;
388 }
389 return false;
390}
391
392bool
393Target::DisableBreakpointByID (break_id_t break_id)
394{
Greg Claytone005f2c2010-11-06 01:53:30 +0000395 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000396 if (log)
397 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
398
399 BreakpointSP bp_sp;
400
401 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
402 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
403 else
404 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
405 if (bp_sp)
406 {
407 bp_sp->SetEnabled (false);
408 return true;
409 }
410 return false;
411}
412
413bool
414Target::EnableBreakpointByID (break_id_t break_id)
415{
Greg Claytone005f2c2010-11-06 01:53:30 +0000416 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000417 if (log)
418 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
419 __FUNCTION__,
420 break_id,
421 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
422
423 BreakpointSP bp_sp;
424
425 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
426 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
427 else
428 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
429
430 if (bp_sp)
431 {
432 bp_sp->SetEnabled (true);
433 return true;
434 }
435 return false;
436}
437
438ModuleSP
439Target::GetExecutableModule ()
440{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000441 return m_images.GetModuleAtIndex(0);
442}
443
444Module*
445Target::GetExecutableModulePointer ()
446{
447 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000448}
449
450void
451Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
452{
453 m_images.Clear();
454 m_scratch_ast_context_ap.reset();
455
456 if (executable_sp.get())
457 {
458 Timer scoped_timer (__PRETTY_FUNCTION__,
459 "Target::SetExecutableModule (executable = '%s/%s')",
460 executable_sp->GetFileSpec().GetDirectory().AsCString(),
461 executable_sp->GetFileSpec().GetFilename().AsCString());
462
463 m_images.Append(executable_sp); // The first image is our exectuable file
464
Jim Ingham7508e732010-08-09 23:31:02 +0000465 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000466 if (!m_arch.IsValid())
467 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000468
Chris Lattner24943d22010-06-08 16:52:24 +0000469 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000470 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000471
472 if (executable_objfile)
473 {
474 executable_objfile->GetDependentModules(dependent_files);
475 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
476 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000477 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
478 FileSpec platform_dependent_file_spec;
479 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000480 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000481 else
482 platform_dependent_file_spec = dependent_file_spec;
483
484 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000485 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000486 if (image_module_sp.get())
487 {
Chris Lattner24943d22010-06-08 16:52:24 +0000488 ObjectFile *objfile = image_module_sp->GetObjectFile();
489 if (objfile)
490 objfile->GetDependentModules(dependent_files);
491 }
492 }
493 }
494
Chris Lattner24943d22010-06-08 16:52:24 +0000495 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000496
497 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000498}
499
500
Jim Ingham7508e732010-08-09 23:31:02 +0000501bool
502Target::SetArchitecture (const ArchSpec &arch_spec)
503{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000504 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000505 {
506 // If we're setting the architecture to our current architecture, we
507 // don't need to do anything.
508 return true;
509 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000510 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000511 {
512 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000513 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000514 return true;
515 }
516 else
517 {
518 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000519 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000520 ModuleSP executable_sp = GetExecutableModule ();
521 m_images.Clear();
522 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000523 // Need to do something about unsetting breakpoints.
524
525 if (executable_sp)
526 {
527 FileSpec exec_file_spec = executable_sp->GetFileSpec();
528 Error error = ModuleList::GetSharedModule(exec_file_spec,
529 arch_spec,
530 NULL,
531 NULL,
532 0,
533 executable_sp,
534 NULL,
535 NULL);
536
537 if (!error.Fail() && executable_sp)
538 {
539 SetExecutableModule (executable_sp, true);
540 return true;
541 }
542 else
543 {
544 return false;
545 }
546 }
547 else
548 {
549 return false;
550 }
551 }
552}
Chris Lattner24943d22010-06-08 16:52:24 +0000553
Chris Lattner24943d22010-06-08 16:52:24 +0000554void
555Target::ModuleAdded (ModuleSP &module_sp)
556{
557 // A module is being added to this target for the first time
558 ModuleList module_list;
559 module_list.Append(module_sp);
560 ModulesDidLoad (module_list);
561}
562
563void
564Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
565{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000566 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000567 ModuleList module_list;
568 module_list.Append (old_module_sp);
569 ModulesDidUnload (module_list);
570 module_list.Clear ();
571 module_list.Append (new_module_sp);
572 ModulesDidLoad (module_list);
573}
574
575void
576Target::ModulesDidLoad (ModuleList &module_list)
577{
578 m_breakpoint_list.UpdateBreakpoints (module_list, true);
579 // TODO: make event data that packages up the module_list
580 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
581}
582
583void
584Target::ModulesDidUnload (ModuleList &module_list)
585{
586 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000587
588 // Remove the images from the target image list
589 m_images.Remove(module_list);
590
Chris Lattner24943d22010-06-08 16:52:24 +0000591 // TODO: make event data that packages up the module_list
592 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
593}
594
595size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000596Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
597{
598 const Section *section = addr.GetSection();
599 if (section && section->GetModule())
600 {
601 ObjectFile *objfile = section->GetModule()->GetObjectFile();
602 if (objfile)
603 {
604 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
605 addr.GetOffset(),
606 dst,
607 dst_len);
608 if (bytes_read > 0)
609 return bytes_read;
610 else
611 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
612 }
613 else
614 {
615 error.SetErrorString("address isn't from a object file");
616 }
617 }
618 else
619 {
620 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
621 }
622 return 0;
623}
624
625size_t
626Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000627{
Chris Lattner24943d22010-06-08 16:52:24 +0000628 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000629
Greg Clayton70436352010-06-30 23:03:03 +0000630 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
631
Greg Clayton26100dc2011-01-07 01:57:07 +0000632 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000633
634 addr_t load_addr = LLDB_INVALID_ADDRESS;
635 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000636 Address resolved_addr;
637 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000638 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000639 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000640 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000641 // No sections are loaded, so we must assume we are not running
642 // yet and anything we are given is a file address.
643 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
644 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000645 }
Greg Clayton70436352010-06-30 23:03:03 +0000646 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000647 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000648 // We have at least one section loaded. This can be becuase
649 // we have manually loaded some sections with "target modules load ..."
650 // or because we have have a live process that has sections loaded
651 // through the dynamic loader
652 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
653 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000654 }
Greg Clayton70436352010-06-30 23:03:03 +0000655 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000656 if (!resolved_addr.IsValid())
657 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000658
Greg Clayton9b82f862011-07-11 05:12:02 +0000659
Greg Clayton26100dc2011-01-07 01:57:07 +0000660 if (prefer_file_cache)
661 {
662 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
663 if (bytes_read > 0)
664 return bytes_read;
665 }
Greg Clayton70436352010-06-30 23:03:03 +0000666
667 if (process_is_valid)
668 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000669 if (load_addr == LLDB_INVALID_ADDRESS)
670 load_addr = resolved_addr.GetLoadAddress (this);
671
Greg Clayton70436352010-06-30 23:03:03 +0000672 if (load_addr == LLDB_INVALID_ADDRESS)
673 {
674 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
675 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
676 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
677 resolved_addr.GetFileAddress());
678 else
679 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
680 }
681 else
682 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000683 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000684 if (bytes_read != dst_len)
685 {
686 if (error.Success())
687 {
688 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000689 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000690 else
Greg Clayton70436352010-06-30 23:03:03 +0000691 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000692 }
693 }
Greg Clayton70436352010-06-30 23:03:03 +0000694 if (bytes_read)
695 return bytes_read;
696 // If the address is not section offset we have an address that
697 // doesn't resolve to any address in any currently loaded shared
698 // libaries and we failed to read memory so there isn't anything
699 // more we can do. If it is section offset, we might be able to
700 // read cached memory from the object file.
701 if (!resolved_addr.IsSectionOffset())
702 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000703 }
Chris Lattner24943d22010-06-08 16:52:24 +0000704 }
Greg Clayton70436352010-06-30 23:03:03 +0000705
Greg Clayton9b82f862011-07-11 05:12:02 +0000706 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000707 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000708 // If we didn't already try and read from the object file cache, then
709 // try it after failing to read from the process.
710 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000711 }
712 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000713}
714
Greg Clayton7dd98df2011-07-12 17:06:17 +0000715size_t
716Target::ReadScalarIntegerFromMemory (const Address& addr,
717 bool prefer_file_cache,
718 uint32_t byte_size,
719 bool is_signed,
720 Scalar &scalar,
721 Error &error)
722{
723 uint64_t uval;
724
725 if (byte_size <= sizeof(uval))
726 {
727 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
728 if (bytes_read == byte_size)
729 {
730 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
731 uint32_t offset = 0;
732 if (byte_size <= 4)
733 scalar = data.GetMaxU32 (&offset, byte_size);
734 else
735 scalar = data.GetMaxU64 (&offset, byte_size);
736
737 if (is_signed)
738 scalar.SignExtend(byte_size * 8);
739 return bytes_read;
740 }
741 }
742 else
743 {
744 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
745 }
746 return 0;
747}
748
749uint64_t
750Target::ReadUnsignedIntegerFromMemory (const Address& addr,
751 bool prefer_file_cache,
752 size_t integer_byte_size,
753 uint64_t fail_value,
754 Error &error)
755{
756 Scalar scalar;
757 if (ReadScalarIntegerFromMemory (addr,
758 prefer_file_cache,
759 integer_byte_size,
760 false,
761 scalar,
762 error))
763 return scalar.ULongLong(fail_value);
764 return fail_value;
765}
766
767bool
768Target::ReadPointerFromMemory (const Address& addr,
769 bool prefer_file_cache,
770 Error &error,
771 Address &pointer_addr)
772{
773 Scalar scalar;
774 if (ReadScalarIntegerFromMemory (addr,
775 prefer_file_cache,
776 m_arch.GetAddressByteSize(),
777 false,
778 scalar,
779 error))
780 {
781 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
782 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
783 {
784 if (m_section_load_list.IsEmpty())
785 {
786 // No sections are loaded, so we must assume we are not running
787 // yet and anything we are given is a file address.
788 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
789 }
790 else
791 {
792 // We have at least one section loaded. This can be becuase
793 // we have manually loaded some sections with "target modules load ..."
794 // or because we have have a live process that has sections loaded
795 // through the dynamic loader
796 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
797 }
798 // We weren't able to resolve the pointer value, so just return
799 // an address with no section
800 if (!pointer_addr.IsValid())
801 pointer_addr.SetOffset (pointer_vm_addr);
802 return true;
803
804 }
805 }
806 return false;
807}
Chris Lattner24943d22010-06-08 16:52:24 +0000808
809ModuleSP
810Target::GetSharedModule
811(
812 const FileSpec& file_spec,
813 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000814 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000815 const ConstString *object_name,
816 off_t object_offset,
817 Error *error_ptr
818)
819{
820 // Don't pass in the UUID so we can tell if we have a stale value in our list
821 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
822 bool did_create_module = false;
823 ModuleSP module_sp;
824
Chris Lattner24943d22010-06-08 16:52:24 +0000825 Error error;
826
Greg Clayton24bc5d92011-03-30 18:16:51 +0000827 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +0000828 if (m_image_search_paths.GetSize())
829 {
830 FileSpec transformed_spec;
831 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
832 {
833 transformed_spec.GetFilename() = file_spec.GetFilename();
834 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
835 }
836 }
837
Greg Clayton24bc5d92011-03-30 18:16:51 +0000838 // The platform is responsible for finding and caching an appropriate
839 // module in the shared module cache.
840 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000841 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000842 FileSpec platform_file_spec;
843 error = m_platform_sp->GetSharedModule (file_spec,
844 arch,
845 uuid_ptr,
846 object_name,
847 object_offset,
848 module_sp,
849 &old_module_sp,
850 &did_create_module);
851 }
852 else
853 {
854 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +0000855 }
856
Greg Clayton24bc5d92011-03-30 18:16:51 +0000857 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +0000858 if (module_sp)
859 {
860 m_images.Append (module_sp);
861 if (did_create_module)
862 {
863 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
864 ModuleUpdated(old_module_sp, module_sp);
865 else
866 ModuleAdded(module_sp);
867 }
868 }
869 if (error_ptr)
870 *error_ptr = error;
871 return module_sp;
872}
873
874
875Target *
876Target::CalculateTarget ()
877{
878 return this;
879}
880
881Process *
882Target::CalculateProcess ()
883{
884 return NULL;
885}
886
887Thread *
888Target::CalculateThread ()
889{
890 return NULL;
891}
892
893StackFrame *
894Target::CalculateStackFrame ()
895{
896 return NULL;
897}
898
899void
Greg Claytona830adb2010-10-04 01:05:56 +0000900Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000901{
902 exe_ctx.target = this;
903 exe_ctx.process = NULL; // Do NOT fill in process...
904 exe_ctx.thread = NULL;
905 exe_ctx.frame = NULL;
906}
907
908PathMappingList &
909Target::GetImageSearchPathList ()
910{
911 return m_image_search_paths;
912}
913
914void
915Target::ImageSearchPathsChanged
916(
917 const PathMappingList &path_list,
918 void *baton
919)
920{
921 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +0000922 ModuleSP exe_module_sp (target->GetExecutableModule());
923 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000924 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000925 target->m_images.Clear();
926 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000927 }
928}
929
930ClangASTContext *
931Target::GetScratchClangASTContext()
932{
Greg Clayton34ce4ea2011-08-03 01:23:55 +0000933 // Now see if we know the target triple, and if so, create our scratch AST context:
934 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
935 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000936 return m_scratch_ast_context_ap.get();
937}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000938
Greg Clayton990de7b2010-11-18 23:32:35 +0000939void
Caroline Tice2a456812011-03-10 22:14:10 +0000940Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000941{
Greg Clayton990de7b2010-11-18 23:32:35 +0000942 UserSettingsControllerSP &usc = GetSettingsController();
943 usc.reset (new SettingsController);
944 UserSettingsController::InitializeSettingsController (usc,
945 SettingsController::global_settings_table,
946 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000947
948 // Now call SettingsInitialize() on each 'child' setting of Target
949 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000950}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000951
Greg Clayton990de7b2010-11-18 23:32:35 +0000952void
Caroline Tice2a456812011-03-10 22:14:10 +0000953Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000954{
Caroline Tice2a456812011-03-10 22:14:10 +0000955
956 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
957
958 Process::SettingsTerminate ();
959
960 // Now terminate Target Settings.
961
Greg Clayton990de7b2010-11-18 23:32:35 +0000962 UserSettingsControllerSP &usc = GetSettingsController();
963 UserSettingsController::FinalizeSettingsController (usc);
964 usc.reset();
965}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000966
Greg Clayton990de7b2010-11-18 23:32:35 +0000967UserSettingsControllerSP &
968Target::GetSettingsController ()
969{
970 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000971 return g_settings_controller;
972}
973
974ArchSpec
975Target::GetDefaultArchitecture ()
976{
Greg Clayton940b1032011-02-23 00:35:02 +0000977 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
978
979 if (settings_controller_sp)
980 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
981 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +0000982}
983
984void
Greg Clayton940b1032011-02-23 00:35:02 +0000985Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +0000986{
Greg Clayton940b1032011-02-23 00:35:02 +0000987 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
988
989 if (settings_controller_sp)
990 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000991}
992
Greg Claytona830adb2010-10-04 01:05:56 +0000993Target *
994Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
995{
996 // The target can either exist in the "process" of ExecutionContext, or in
997 // the "target_sp" member of SymbolContext. This accessor helper function
998 // will get the target from one of these locations.
999
1000 Target *target = NULL;
1001 if (sc_ptr != NULL)
1002 target = sc_ptr->target_sp.get();
1003 if (target == NULL)
1004 {
1005 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
1006 target = &exe_ctx_ptr->process->GetTarget();
1007 }
1008 return target;
1009}
1010
1011
Caroline Tice1ebef442010-09-27 00:30:10 +00001012void
1013Target::UpdateInstanceName ()
1014{
1015 StreamString sstr;
1016
Greg Clayton5beb99d2011-08-11 02:48:45 +00001017 Module *exe_module = GetExecutableModulePointer();
1018 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001019 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001020 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001021 exe_module->GetFileSpec().GetFilename().AsCString(),
1022 exe_module->GetArchitecture().GetArchitectureName());
1023 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001024 }
1025}
1026
Sean Callanan77e93942010-10-29 00:29:03 +00001027const char *
1028Target::GetExpressionPrefixContentsAsCString ()
1029{
Greg Claytonff44ab42011-04-23 02:04:55 +00001030 if (m_expr_prefix_contents_sp)
1031 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1032 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001033}
1034
Greg Clayton427f2902010-12-14 02:59:59 +00001035ExecutionResults
1036Target::EvaluateExpression
1037(
1038 const char *expr_cstr,
1039 StackFrame *frame,
1040 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001041 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001042 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001043 lldb::ValueObjectSP &result_valobj_sp
1044)
1045{
1046 ExecutionResults execution_results = eExecutionSetupError;
1047
1048 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001049
1050 // We shouldn't run stop hooks in expressions.
1051 // Be sure to reset this if you return anywhere within this function.
1052 bool old_suppress_value = m_suppress_stop_hooks;
1053 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001054
1055 ExecutionContext exe_ctx;
1056 if (frame)
1057 {
1058 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001059 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001060 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001061 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1062 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001063 lldb::VariableSP var_sp;
1064 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1065 use_dynamic,
1066 expr_path_options,
1067 var_sp,
1068 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001069 }
1070 else if (m_process_sp)
1071 {
1072 m_process_sp->CalculateExecutionContext(exe_ctx);
1073 }
1074 else
1075 {
1076 CalculateExecutionContext(exe_ctx);
1077 }
1078
1079 if (result_valobj_sp)
1080 {
1081 execution_results = eExecutionCompleted;
1082 // We got a result from the frame variable expression path above...
1083 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1084
1085 lldb::ValueObjectSP const_valobj_sp;
1086
1087 // Check in case our value is already a constant value
1088 if (result_valobj_sp->GetIsConstant())
1089 {
1090 const_valobj_sp = result_valobj_sp;
1091 const_valobj_sp->SetName (persistent_variable_name);
1092 }
1093 else
Jim Inghame41494a2011-04-16 00:01:13 +00001094 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001095 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001096 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001097 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001098 if (dynamic_sp)
1099 result_valobj_sp = dynamic_sp;
1100 }
1101
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001102 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001103 }
Greg Clayton427f2902010-12-14 02:59:59 +00001104
Sean Callanan6a925532011-01-13 08:53:35 +00001105 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1106
Greg Clayton427f2902010-12-14 02:59:59 +00001107 result_valobj_sp = const_valobj_sp;
1108
Sean Callanan6a925532011-01-13 08:53:35 +00001109 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1110 assert (clang_expr_variable_sp.get());
1111
1112 // Set flags and live data as appropriate
1113
1114 const Value &result_value = live_valobj_sp->GetValue();
1115
1116 switch (result_value.GetValueType())
1117 {
1118 case Value::eValueTypeHostAddress:
1119 case Value::eValueTypeFileAddress:
1120 // we don't do anything with these for now
1121 break;
1122 case Value::eValueTypeScalar:
1123 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1124 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1125 break;
1126 case Value::eValueTypeLoadAddress:
1127 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1128 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1129 break;
1130 }
Greg Clayton427f2902010-12-14 02:59:59 +00001131 }
1132 else
1133 {
1134 // Make sure we aren't just trying to see the value of a persistent
1135 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001136 lldb::ClangExpressionVariableSP persistent_var_sp;
1137 // Only check for persistent variables the expression starts with a '$'
1138 if (expr_cstr[0] == '$')
1139 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1140
Greg Clayton427f2902010-12-14 02:59:59 +00001141 if (persistent_var_sp)
1142 {
1143 result_valobj_sp = persistent_var_sp->GetValueObject ();
1144 execution_results = eExecutionCompleted;
1145 }
1146 else
1147 {
1148 const char *prefix = GetExpressionPrefixContentsAsCString();
1149
1150 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +00001151 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001152 expr_cstr,
1153 prefix,
1154 result_valobj_sp);
1155 }
1156 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001157
1158 m_suppress_stop_hooks = old_suppress_value;
1159
Greg Clayton427f2902010-12-14 02:59:59 +00001160 return execution_results;
1161}
1162
Greg Claytonc0fa5332011-05-22 22:46:53 +00001163lldb::addr_t
1164Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1165{
1166 addr_t code_addr = load_addr;
1167 switch (m_arch.GetMachine())
1168 {
1169 case llvm::Triple::arm:
1170 case llvm::Triple::thumb:
1171 switch (addr_class)
1172 {
1173 case eAddressClassData:
1174 case eAddressClassDebug:
1175 return LLDB_INVALID_ADDRESS;
1176
1177 case eAddressClassUnknown:
1178 case eAddressClassInvalid:
1179 case eAddressClassCode:
1180 case eAddressClassCodeAlternateISA:
1181 case eAddressClassRuntime:
1182 // Check if bit zero it no set?
1183 if ((code_addr & 1ull) == 0)
1184 {
1185 // Bit zero isn't set, check if the address is a multiple of 2?
1186 if (code_addr & 2ull)
1187 {
1188 // The address is a multiple of 2 so it must be thumb, set bit zero
1189 code_addr |= 1ull;
1190 }
1191 else if (addr_class == eAddressClassCodeAlternateISA)
1192 {
1193 // We checked the address and the address claims to be the alternate ISA
1194 // which means thumb, so set bit zero.
1195 code_addr |= 1ull;
1196 }
1197 }
1198 break;
1199 }
1200 break;
1201
1202 default:
1203 break;
1204 }
1205 return code_addr;
1206}
1207
1208lldb::addr_t
1209Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1210{
1211 addr_t opcode_addr = load_addr;
1212 switch (m_arch.GetMachine())
1213 {
1214 case llvm::Triple::arm:
1215 case llvm::Triple::thumb:
1216 switch (addr_class)
1217 {
1218 case eAddressClassData:
1219 case eAddressClassDebug:
1220 return LLDB_INVALID_ADDRESS;
1221
1222 case eAddressClassInvalid:
1223 case eAddressClassUnknown:
1224 case eAddressClassCode:
1225 case eAddressClassCodeAlternateISA:
1226 case eAddressClassRuntime:
1227 opcode_addr &= ~(1ull);
1228 break;
1229 }
1230 break;
1231
1232 default:
1233 break;
1234 }
1235 return opcode_addr;
1236}
1237
Jim Inghamd60d94a2011-03-11 03:53:59 +00001238lldb::user_id_t
1239Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1240{
1241 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1242 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1243 m_stop_hooks[new_uid] = new_hook_sp;
1244 return new_uid;
1245}
1246
1247bool
1248Target::RemoveStopHookByID (lldb::user_id_t user_id)
1249{
1250 size_t num_removed;
1251 num_removed = m_stop_hooks.erase (user_id);
1252 if (num_removed == 0)
1253 return false;
1254 else
1255 return true;
1256}
1257
1258void
1259Target::RemoveAllStopHooks ()
1260{
1261 m_stop_hooks.clear();
1262}
1263
1264Target::StopHookSP
1265Target::GetStopHookByID (lldb::user_id_t user_id)
1266{
1267 StopHookSP found_hook;
1268
1269 StopHookCollection::iterator specified_hook_iter;
1270 specified_hook_iter = m_stop_hooks.find (user_id);
1271 if (specified_hook_iter != m_stop_hooks.end())
1272 found_hook = (*specified_hook_iter).second;
1273 return found_hook;
1274}
1275
1276bool
1277Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1278{
1279 StopHookCollection::iterator specified_hook_iter;
1280 specified_hook_iter = m_stop_hooks.find (user_id);
1281 if (specified_hook_iter == m_stop_hooks.end())
1282 return false;
1283
1284 (*specified_hook_iter).second->SetIsActive (active_state);
1285 return true;
1286}
1287
1288void
1289Target::SetAllStopHooksActiveState (bool active_state)
1290{
1291 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1292 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1293 {
1294 (*pos).second->SetIsActive (active_state);
1295 }
1296}
1297
1298void
1299Target::RunStopHooks ()
1300{
Jim Ingham3613ae12011-05-12 02:06:14 +00001301 if (m_suppress_stop_hooks)
1302 return;
1303
Jim Inghamd60d94a2011-03-11 03:53:59 +00001304 if (!m_process_sp)
1305 return;
1306
1307 if (m_stop_hooks.empty())
1308 return;
1309
1310 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1311
1312 // If there aren't any active stop hooks, don't bother either:
1313 bool any_active_hooks = false;
1314 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1315 {
1316 if ((*pos).second->IsActive())
1317 {
1318 any_active_hooks = true;
1319 break;
1320 }
1321 }
1322 if (!any_active_hooks)
1323 return;
1324
1325 CommandReturnObject result;
1326
1327 std::vector<ExecutionContext> exc_ctx_with_reasons;
1328 std::vector<SymbolContext> sym_ctx_with_reasons;
1329
1330 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1331 size_t num_threads = cur_threadlist.GetSize();
1332 for (size_t i = 0; i < num_threads; i++)
1333 {
1334 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1335 if (cur_thread_sp->ThreadStoppedForAReason())
1336 {
1337 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1338 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1339 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1340 }
1341 }
1342
1343 // If no threads stopped for a reason, don't run the stop-hooks.
1344 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1345 if (num_exe_ctx == 0)
1346 return;
1347
Jim Inghame5ed8e92011-06-02 23:58:26 +00001348 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1349 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001350
1351 bool keep_going = true;
1352 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001353 bool print_hook_header;
1354 bool print_thread_header;
1355
1356 if (num_exe_ctx == 1)
1357 print_thread_header = false;
1358 else
1359 print_thread_header = true;
1360
1361 if (m_stop_hooks.size() == 1)
1362 print_hook_header = false;
1363 else
1364 print_hook_header = true;
1365
Jim Inghamd60d94a2011-03-11 03:53:59 +00001366 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1367 {
1368 // result.Clear();
1369 StopHookSP cur_hook_sp = (*pos).second;
1370 if (!cur_hook_sp->IsActive())
1371 continue;
1372
1373 bool any_thread_matched = false;
1374 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1375 {
1376 if ((cur_hook_sp->GetSpecifier () == NULL
1377 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1378 && (cur_hook_sp->GetThreadSpecifier() == NULL
1379 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1380 {
1381 if (!hooks_ran)
1382 {
Jim Inghamc54840c2011-03-22 01:47:27 +00001383 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001384 hooks_ran = true;
1385 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001386 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001387 {
1388 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1389 any_thread_matched = true;
1390 }
1391
Jim Inghamc54840c2011-03-22 01:47:27 +00001392 if (print_thread_header)
1393 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001394
1395 bool stop_on_continue = true;
1396 bool stop_on_error = true;
1397 bool echo_commands = false;
1398 bool print_results = true;
1399 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001400 &exc_ctx_with_reasons[i],
1401 stop_on_continue,
1402 stop_on_error,
1403 echo_commands,
1404 print_results,
1405 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001406
1407 // If the command started the target going again, we should bag out of
1408 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001409 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1410 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001411 {
1412 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1413 keep_going = false;
1414 }
1415 }
1416 }
1417 }
1418 if (hooks_ran)
1419 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice4a348082011-05-02 20:41:46 +00001420
1421 result.GetImmediateOutputStream()->Flush();
1422 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001423}
1424
Greg Claytonbbea1332011-07-08 00:48:09 +00001425bool
1426Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1427{
1428 bool changed = false;
1429 if (module)
1430 {
1431 ObjectFile *object_file = module->GetObjectFile();
1432 if (object_file)
1433 {
1434 SectionList *section_list = object_file->GetSectionList ();
1435 if (section_list)
1436 {
1437 // All sections listed in the dyld image info structure will all
1438 // either be fixed up already, or they will all be off by a single
1439 // slide amount that is determined by finding the first segment
1440 // that is at file offset zero which also has bytes (a file size
1441 // that is greater than zero) in the object file.
1442
1443 // Determine the slide amount (if any)
1444 const size_t num_sections = section_list->GetSize();
1445 size_t sect_idx = 0;
1446 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1447 {
1448 // Iterate through the object file sections to find the
1449 // first section that starts of file offset zero and that
1450 // has bytes in the file...
1451 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1452 if (section)
1453 {
1454 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1455 changed = true;
1456 }
1457 }
1458 }
1459 }
1460 }
1461 return changed;
1462}
1463
1464
Jim Inghamd60d94a2011-03-11 03:53:59 +00001465//--------------------------------------------------------------
1466// class Target::StopHook
1467//--------------------------------------------------------------
1468
1469
1470Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1471 UserID (uid),
1472 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001473 m_commands (),
1474 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001475 m_thread_spec_ap(NULL),
1476 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001477{
1478}
1479
1480Target::StopHook::StopHook (const StopHook &rhs) :
1481 UserID (rhs.GetID()),
1482 m_target_sp (rhs.m_target_sp),
1483 m_commands (rhs.m_commands),
1484 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001485 m_thread_spec_ap (NULL),
1486 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001487{
1488 if (rhs.m_thread_spec_ap.get() != NULL)
1489 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1490}
1491
1492
1493Target::StopHook::~StopHook ()
1494{
1495}
1496
1497void
1498Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1499{
1500 m_thread_spec_ap.reset (specifier);
1501}
1502
1503
1504void
1505Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1506{
1507 int indent_level = s->GetIndentLevel();
1508
1509 s->SetIndentLevel(indent_level + 2);
1510
1511 s->Printf ("Hook: %d\n", GetID());
1512 if (m_active)
1513 s->Indent ("State: enabled\n");
1514 else
1515 s->Indent ("State: disabled\n");
1516
1517 if (m_specifier_sp)
1518 {
1519 s->Indent();
1520 s->PutCString ("Specifier:\n");
1521 s->SetIndentLevel (indent_level + 4);
1522 m_specifier_sp->GetDescription (s, level);
1523 s->SetIndentLevel (indent_level + 2);
1524 }
1525
1526 if (m_thread_spec_ap.get() != NULL)
1527 {
1528 StreamString tmp;
1529 s->Indent("Thread:\n");
1530 m_thread_spec_ap->GetDescription (&tmp, level);
1531 s->SetIndentLevel (indent_level + 4);
1532 s->Indent (tmp.GetData());
1533 s->PutCString ("\n");
1534 s->SetIndentLevel (indent_level + 2);
1535 }
1536
1537 s->Indent ("Commands: \n");
1538 s->SetIndentLevel (indent_level + 4);
1539 uint32_t num_commands = m_commands.GetSize();
1540 for (uint32_t i = 0; i < num_commands; i++)
1541 {
1542 s->Indent(m_commands.GetStringAtIndex(i));
1543 s->PutCString ("\n");
1544 }
1545 s->SetIndentLevel (indent_level);
1546}
1547
1548
Caroline Tice5bc8c972010-09-20 20:44:43 +00001549//--------------------------------------------------------------
1550// class Target::SettingsController
1551//--------------------------------------------------------------
1552
1553Target::SettingsController::SettingsController () :
1554 UserSettingsController ("target", Debugger::GetSettingsController()),
1555 m_default_architecture ()
1556{
1557 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1558 InstanceSettings::GetDefaultName().AsCString()));
1559}
1560
1561Target::SettingsController::~SettingsController ()
1562{
1563}
1564
1565lldb::InstanceSettingsSP
1566Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1567{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001568 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1569 false,
1570 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001571 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1572 return new_settings_sp;
1573}
1574
Caroline Tice5bc8c972010-09-20 20:44:43 +00001575
Jim Inghame41494a2011-04-16 00:01:13 +00001576#define TSC_DEFAULT_ARCH "default-arch"
1577#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001578#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001579#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001580#define TSC_SOURCE_MAP "source-map"
Greg Claytond284b662011-02-18 01:44:25 +00001581
1582
1583static const ConstString &
1584GetSettingNameForDefaultArch ()
1585{
1586 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001587 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001588}
1589
Greg Claytond284b662011-02-18 01:44:25 +00001590static const ConstString &
1591GetSettingNameForExpressionPrefix ()
1592{
1593 static ConstString g_const_string (TSC_EXPR_PREFIX);
1594 return g_const_string;
1595}
1596
1597static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001598GetSettingNameForPreferDynamicValue ()
1599{
1600 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1601 return g_const_string;
1602}
1603
Greg Claytonff44ab42011-04-23 02:04:55 +00001604static const ConstString &
1605GetSettingNameForSourcePathMap ()
1606{
1607 static ConstString g_const_string (TSC_SOURCE_MAP);
1608 return g_const_string;
1609}
Greg Claytond284b662011-02-18 01:44:25 +00001610
Greg Clayton17cd9952011-04-22 03:55:06 +00001611static const ConstString &
1612GetSettingNameForSkipPrologue ()
1613{
1614 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1615 return g_const_string;
1616}
1617
1618
1619
Caroline Tice5bc8c972010-09-20 20:44:43 +00001620bool
1621Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1622 const char *index_value,
1623 const char *value,
1624 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001625 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001626 Error&err)
1627{
Greg Claytond284b662011-02-18 01:44:25 +00001628 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001629 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001630 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001631 if (!m_default_architecture.IsValid())
1632 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001633 }
1634 return true;
1635}
1636
1637
1638bool
1639Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1640 StringList &value,
1641 Error &err)
1642{
Greg Claytond284b662011-02-18 01:44:25 +00001643 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001644 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001645 // If the arch is invalid (the default), don't show a string for it
1646 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001647 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001648 return true;
1649 }
1650 else
1651 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1652
1653 return false;
1654}
1655
1656//--------------------------------------------------------------
1657// class TargetInstanceSettings
1658//--------------------------------------------------------------
1659
Greg Clayton638351a2010-12-04 00:10:17 +00001660TargetInstanceSettings::TargetInstanceSettings
1661(
1662 UserSettingsController &owner,
1663 bool live_instance,
1664 const char *name
1665) :
Greg Claytond284b662011-02-18 01:44:25 +00001666 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001667 m_expr_prefix_file (),
1668 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001669 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001670 m_skip_prologue (true, true),
1671 m_source_map (NULL, NULL)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001672{
1673 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1674 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1675 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1676 // This is true for CreateInstanceName() too.
1677
1678 if (GetInstanceName () == InstanceSettings::InvalidName())
1679 {
1680 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1681 m_owner.RegisterInstanceSettings (this);
1682 }
1683
1684 if (live_instance)
1685 {
1686 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1687 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001688 }
1689}
1690
1691TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001692 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1693 m_expr_prefix_file (rhs.m_expr_prefix_file),
1694 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1695 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1696 m_skip_prologue (rhs.m_skip_prologue),
1697 m_source_map (rhs.m_source_map)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001698{
1699 if (m_instance_name != InstanceSettings::GetDefaultName())
1700 {
1701 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1702 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001703 }
1704}
1705
1706TargetInstanceSettings::~TargetInstanceSettings ()
1707{
1708}
1709
1710TargetInstanceSettings&
1711TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1712{
1713 if (this != &rhs)
1714 {
1715 }
1716
1717 return *this;
1718}
1719
Caroline Tice5bc8c972010-09-20 20:44:43 +00001720void
1721TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1722 const char *index_value,
1723 const char *value,
1724 const ConstString &instance_name,
1725 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001726 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001727 Error &err,
1728 bool pending)
1729{
Greg Claytond284b662011-02-18 01:44:25 +00001730 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001731 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001732 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1733 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00001734 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001735 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00001736 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001737 default:
1738 break;
1739 case eVarSetOperationAssign:
1740 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00001741 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001742 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1743 {
1744 err.SetErrorToGenericError ();
1745 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1746 return;
1747 }
1748
1749 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1750
1751 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1752 {
1753 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1754 m_expr_prefix_contents_sp.reset();
1755 }
Sean Callanan77e93942010-10-29 00:29:03 +00001756 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001757 break;
1758 case eVarSetOperationClear:
1759 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00001760 }
Sean Callanan77e93942010-10-29 00:29:03 +00001761 }
1762 }
Jim Inghame41494a2011-04-16 00:01:13 +00001763 else if (var_name == GetSettingNameForPreferDynamicValue())
1764 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001765 int new_value;
1766 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1767 if (err.Success())
1768 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00001769 }
1770 else if (var_name == GetSettingNameForSkipPrologue())
1771 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001772 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1773 }
1774 else if (var_name == GetSettingNameForSourcePathMap ())
1775 {
1776 switch (op)
1777 {
1778 case eVarSetOperationReplace:
1779 case eVarSetOperationInsertBefore:
1780 case eVarSetOperationInsertAfter:
1781 case eVarSetOperationRemove:
1782 default:
1783 break;
1784 case eVarSetOperationAssign:
1785 m_source_map.Clear(true);
1786 // Fall through to append....
1787 case eVarSetOperationAppend:
1788 {
1789 Args args(value);
1790 const uint32_t argc = args.GetArgumentCount();
1791 if (argc & 1 || argc == 0)
1792 {
1793 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1794 }
1795 else
1796 {
1797 char resolved_new_path[PATH_MAX];
1798 FileSpec file_spec;
1799 const char *old_path;
1800 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1801 {
1802 const char *new_path = args.GetArgumentAtIndex(idx+1);
1803 assert (new_path); // We have an even number of paths, this shouldn't happen!
1804
1805 file_spec.SetFile(new_path, true);
1806 if (file_spec.Exists())
1807 {
1808 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1809 {
1810 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1811 return;
1812 }
1813 }
1814 else
1815 {
1816 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1817 return;
1818 }
1819 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1820 }
1821 }
1822 }
1823 break;
1824
1825 case eVarSetOperationClear:
1826 m_source_map.Clear(true);
1827 break;
1828 }
Jim Inghame41494a2011-04-16 00:01:13 +00001829 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001830}
1831
1832void
Greg Claytond284b662011-02-18 01:44:25 +00001833TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001834{
Sean Callanan77e93942010-10-29 00:29:03 +00001835 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1836
1837 if (!new_settings_ptr)
1838 return;
1839
Greg Claytonff44ab42011-04-23 02:04:55 +00001840 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1841 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1842 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1843 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001844}
1845
Caroline Ticebcb5b452010-09-20 21:37:42 +00001846bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001847TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1848 const ConstString &var_name,
1849 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001850 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001851{
Greg Claytond284b662011-02-18 01:44:25 +00001852 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001853 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001854 char path[PATH_MAX];
1855 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1856 if (path_len > 0)
1857 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00001858 }
Jim Inghame41494a2011-04-16 00:01:13 +00001859 else if (var_name == GetSettingNameForPreferDynamicValue())
1860 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001861 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00001862 }
Greg Clayton17cd9952011-04-22 03:55:06 +00001863 else if (var_name == GetSettingNameForSkipPrologue())
1864 {
1865 if (m_skip_prologue)
1866 value.AppendString ("true");
1867 else
1868 value.AppendString ("false");
1869 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001870 else if (var_name == GetSettingNameForSourcePathMap ())
1871 {
1872 }
Sean Callanan77e93942010-10-29 00:29:03 +00001873 else
1874 {
1875 if (err)
1876 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1877 return false;
1878 }
1879
1880 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001881}
1882
1883const ConstString
1884TargetInstanceSettings::CreateInstanceName ()
1885{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001886 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001887 static int instance_count = 1;
1888
Caroline Tice5bc8c972010-09-20 20:44:43 +00001889 sstr.Printf ("target_%d", instance_count);
1890 ++instance_count;
1891
1892 const ConstString ret_val (sstr.GetData());
1893 return ret_val;
1894}
1895
1896//--------------------------------------------------
1897// Target::SettingsController Variable Tables
1898//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00001899OptionEnumValueElement
1900TargetInstanceSettings::g_dynamic_value_types[] =
1901{
Greg Clayton577fbc32011-05-30 00:39:48 +00001902{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
1903{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
1904{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00001905{ 0, NULL, NULL }
1906};
Caroline Tice5bc8c972010-09-20 20:44:43 +00001907
1908SettingEntry
1909Target::SettingsController::global_settings_table[] =
1910{
Greg Claytond284b662011-02-18 01:44:25 +00001911 // var-name var-type default enum init'd hidden help-text
1912 // ================= ================== =========== ==== ====== ====== =========================================================================
1913 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1914 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1915};
1916
Caroline Tice5bc8c972010-09-20 20:44:43 +00001917SettingEntry
1918Target::SettingsController::instance_settings_table[] =
1919{
Jim Ingham10de7d12011-05-04 03:43:18 +00001920 // var-name var-type default enum init'd hidden help-text
1921 // ================= ================== =============== ======================= ====== ====== =========================================================================
1922 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
Greg Clayton577fbc32011-05-30 00:39:48 +00001923 { TSC_PREFER_DYNAMIC, eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
1924 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
1925 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
Jim Ingham10de7d12011-05-04 03:43:18 +00001926 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001927};