blob: 36058e9cc5f0e143b5680b53420e15668d11764e [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- 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"
Sean Callanan322f5292010-10-29 00:29:0320#include "lldb/Core/DataBufferMemoryMap.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5921#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2422#include "lldb/Core/Event.h"
23#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:2424#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5925#include "lldb/Core/Timer.h"
26#include "lldb/Core/ValueObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2427#include "lldb/Host/Host.h"
28#include "lldb/lldb-private-log.h"
29#include "lldb/Symbol/ObjectFile.h"
30#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5931#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:2432
33using namespace lldb;
34using namespace lldb_private;
35
36//----------------------------------------------------------------------
37// Target constructor
38//----------------------------------------------------------------------
Greg Clayton66111032010-06-23 01:19:2939Target::Target(Debugger &debugger) :
Greg Claytoncfd1ace2010-10-31 03:01:0640 Broadcaster("lldb.target"),
Greg Claytondbe54502010-11-19 03:46:0141 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2942 m_debugger (debugger),
Greg Claytonaf67cec2010-12-20 20:49:2343 m_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:2444 m_images(),
Greg Claytonf5e56de2010-09-14 23:36:4045 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2446 m_breakpoint_list (false),
47 m_internal_breakpoint_list (true),
48 m_process_sp(),
49 m_triple(),
50 m_search_filter_sp(),
51 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5952 m_scratch_ast_context_ap (NULL),
53 m_persistent_variables ()
Chris Lattner30fdc8d2010-06-08 16:52:2454{
Greg Claytoncfd1ace2010-10-31 03:01:0655 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
56 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
57 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
58
Greg Clayton2d4edfb2010-11-06 01:53:3059 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2460 if (log)
61 log->Printf ("%p Target::Target()", this);
62}
63
64//----------------------------------------------------------------------
65// Destructor
66//----------------------------------------------------------------------
67Target::~Target()
68{
Greg Clayton2d4edfb2010-11-06 01:53:3069 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2470 if (log)
71 log->Printf ("%p Target::~Target()", this);
72 DeleteCurrentProcess ();
73}
74
75void
Caroline Ticeceb6b132010-10-26 03:11:1376Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2477{
Greg Clayton89411422010-10-08 00:21:0578// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1379 if (description_level != lldb::eDescriptionLevelBrief)
80 {
81 s->Indent();
82 s->PutCString("Target\n");
83 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:3584 m_images.Dump(s);
85 m_breakpoint_list.Dump(s);
86 m_internal_breakpoint_list.Dump(s);
87 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:1388 }
89 else
90 {
Greg Clayton48381312010-10-30 04:51:4691 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Ticeceb6b132010-10-26 03:11:1392 }
Chris Lattner30fdc8d2010-06-08 16:52:2493}
94
95void
96Target::DeleteCurrentProcess ()
97{
98 if (m_process_sp.get())
99 {
Greg Clayton17f69202010-09-14 23:52:43100 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24101 if (m_process_sp->IsAlive())
102 m_process_sp->Destroy();
103 else
104 m_process_sp->Finalize();
105
106 // Do any cleanup of the target we need to do between process instances.
107 // NB It is better to do this before destroying the process in case the
108 // clean up needs some help from the process.
109 m_breakpoint_list.ClearAllBreakpointSites();
110 m_internal_breakpoint_list.ClearAllBreakpointSites();
111 m_process_sp.reset();
112 }
113}
114
115const lldb::ProcessSP &
116Target::CreateProcess (Listener &listener, const char *plugin_name)
117{
118 DeleteCurrentProcess ();
119 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
120 return m_process_sp;
121}
122
123const lldb::ProcessSP &
124Target::GetProcessSP () const
125{
126 return m_process_sp;
127}
128
129lldb::TargetSP
130Target::GetSP()
131{
Greg Clayton66111032010-06-23 01:19:29132 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24133}
134
135BreakpointList &
136Target::GetBreakpointList(bool internal)
137{
138 if (internal)
139 return m_internal_breakpoint_list;
140 else
141 return m_breakpoint_list;
142}
143
144const BreakpointList &
145Target::GetBreakpointList(bool internal) const
146{
147 if (internal)
148 return m_internal_breakpoint_list;
149 else
150 return m_breakpoint_list;
151}
152
153BreakpointSP
154Target::GetBreakpointByID (break_id_t break_id)
155{
156 BreakpointSP bp_sp;
157
158 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
159 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
160 else
161 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
162
163 return bp_sp;
164}
165
166BreakpointSP
167Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
168{
169 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
170 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
171 return CreateBreakpoint (filter_sp, resolver_sp, internal);
172}
173
174
175BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41176Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24177{
Chris Lattner30fdc8d2010-06-08 16:52:24178 Address so_addr;
179 // Attempt to resolve our load address if possible, though it is ok if
180 // it doesn't resolve to section/offset.
181
Greg Clayton1b72fcb2010-08-24 00:45:41182 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40183 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41184 if (!so_addr.IsValid())
185 {
186 // The address didn't resolve, so just set this as an absolute address
187 so_addr.SetOffset (addr);
188 }
189 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24190 return bp_sp;
191}
192
193BreakpointSP
194Target::CreateBreakpoint (Address &addr, bool internal)
195{
196 TargetSP target_sp = this->GetSP();
197 SearchFilterSP filter_sp(new SearchFilter (target_sp));
198 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
199 return CreateBreakpoint (filter_sp, resolver_sp, internal);
200}
201
202BreakpointSP
Greg Clayton0c5cd902010-06-28 21:30:43203Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24204{
Greg Clayton0c5cd902010-06-28 21:30:43205 BreakpointSP bp_sp;
206 if (func_name)
207 {
208 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
209 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
210 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
211 }
212 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24213}
214
215
216SearchFilterSP
217Target::GetSearchFilterForModule (const FileSpec *containingModule)
218{
219 SearchFilterSP filter_sp;
220 lldb::TargetSP target_sp = this->GetSP();
221 if (containingModule != NULL)
222 {
223 // TODO: We should look into sharing module based search filters
224 // across many breakpoints like we do for the simple target based one
225 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
226 }
227 else
228 {
229 if (m_search_filter_sp.get() == NULL)
230 m_search_filter_sp.reset (new SearchFilter (target_sp));
231 filter_sp = m_search_filter_sp;
232 }
233 return filter_sp;
234}
235
236BreakpointSP
237Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
238{
239 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
240 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
241
242 return CreateBreakpoint (filter_sp, resolver_sp, internal);
243}
244
245BreakpointSP
246Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
247{
248 BreakpointSP bp_sp;
249 if (filter_sp && resolver_sp)
250 {
251 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
252 resolver_sp->SetBreakpoint (bp_sp.get());
253
254 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17255 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24256 else
Greg Clayton9fed0d82010-07-23 23:33:17257 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24258
Greg Clayton2d4edfb2010-11-06 01:53:30259 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24260 if (log)
261 {
262 StreamString s;
263 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
264 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
265 }
266
Chris Lattner30fdc8d2010-06-08 16:52:24267 bp_sp->ResolveBreakpoint();
268 }
Jim Ingham36f3b362010-10-14 23:45:03269
270 if (!internal && bp_sp)
271 {
272 m_last_created_breakpoint = bp_sp;
273 }
274
Chris Lattner30fdc8d2010-06-08 16:52:24275 return bp_sp;
276}
277
278void
279Target::RemoveAllBreakpoints (bool internal_also)
280{
Greg Clayton2d4edfb2010-11-06 01:53:30281 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24282 if (log)
283 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
284
Greg Clayton9fed0d82010-07-23 23:33:17285 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24286 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17287 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03288
289 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24290}
291
292void
293Target::DisableAllBreakpoints (bool internal_also)
294{
Greg Clayton2d4edfb2010-11-06 01:53:30295 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24296 if (log)
297 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
298
299 m_breakpoint_list.SetEnabledAll (false);
300 if (internal_also)
301 m_internal_breakpoint_list.SetEnabledAll (false);
302}
303
304void
305Target::EnableAllBreakpoints (bool internal_also)
306{
Greg Clayton2d4edfb2010-11-06 01:53:30307 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24308 if (log)
309 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
310
311 m_breakpoint_list.SetEnabledAll (true);
312 if (internal_also)
313 m_internal_breakpoint_list.SetEnabledAll (true);
314}
315
316bool
317Target::RemoveBreakpointByID (break_id_t break_id)
318{
Greg Clayton2d4edfb2010-11-06 01:53:30319 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24320 if (log)
321 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
322
323 if (DisableBreakpointByID (break_id))
324 {
325 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17326 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24327 else
Jim Ingham36f3b362010-10-14 23:45:03328 {
329 if (m_last_created_breakpoint->GetID() == break_id)
330 m_last_created_breakpoint.reset();
Greg Clayton9fed0d82010-07-23 23:33:17331 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03332 }
Chris Lattner30fdc8d2010-06-08 16:52:24333 return true;
334 }
335 return false;
336}
337
338bool
339Target::DisableBreakpointByID (break_id_t break_id)
340{
Greg Clayton2d4edfb2010-11-06 01:53:30341 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24342 if (log)
343 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
344
345 BreakpointSP bp_sp;
346
347 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
348 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
349 else
350 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
351 if (bp_sp)
352 {
353 bp_sp->SetEnabled (false);
354 return true;
355 }
356 return false;
357}
358
359bool
360Target::EnableBreakpointByID (break_id_t break_id)
361{
Greg Clayton2d4edfb2010-11-06 01:53:30362 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24363 if (log)
364 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
365 __FUNCTION__,
366 break_id,
367 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
368
369 BreakpointSP bp_sp;
370
371 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
372 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
373 else
374 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
375
376 if (bp_sp)
377 {
378 bp_sp->SetEnabled (true);
379 return true;
380 }
381 return false;
382}
383
384ModuleSP
385Target::GetExecutableModule ()
386{
387 ModuleSP executable_sp;
388 if (m_images.GetSize() > 0)
389 executable_sp = m_images.GetModuleAtIndex(0);
390 return executable_sp;
391}
392
393void
394Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
395{
396 m_images.Clear();
397 m_scratch_ast_context_ap.reset();
398
399 if (executable_sp.get())
400 {
401 Timer scoped_timer (__PRETTY_FUNCTION__,
402 "Target::SetExecutableModule (executable = '%s/%s')",
403 executable_sp->GetFileSpec().GetDirectory().AsCString(),
404 executable_sp->GetFileSpec().GetFilename().AsCString());
405
406 m_images.Append(executable_sp); // The first image is our exectuable file
407
408 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02409 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
410 if (!m_arch_spec.IsValid())
411 m_arch_spec = exe_arch;
412
Chris Lattner30fdc8d2010-06-08 16:52:24413 FileSpecList dependent_files;
414 ObjectFile * executable_objfile = executable_sp->GetObjectFile();
415 if (executable_objfile == NULL)
416 {
417
418 FileSpec bundle_executable(executable_sp->GetFileSpec());
Greg Claytondd36def2010-10-17 22:03:32419 if (Host::ResolveExecutableInBundle (bundle_executable))
Chris Lattner30fdc8d2010-06-08 16:52:24420 {
421 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
422 exe_arch));
423 SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
424 if (bundle_exe_module_sp->GetObjectFile() != NULL)
425 executable_sp = bundle_exe_module_sp;
426 return;
427 }
428 }
429
430 if (executable_objfile)
431 {
432 executable_objfile->GetDependentModules(dependent_files);
433 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
434 {
435 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
436 exe_arch));
437 if (image_module_sp.get())
438 {
439 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
440 ObjectFile *objfile = image_module_sp->GetObjectFile();
441 if (objfile)
442 objfile->GetDependentModules(dependent_files);
443 }
444 }
445 }
446
447 // Now see if we know the target triple, and if so, create our scratch AST context:
448 ConstString target_triple;
449 if (GetTargetTriple(target_triple))
450 {
451 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString()));
452 }
453 }
Caroline Tice1559a462010-09-27 00:30:10454
455 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24456}
457
458
459ModuleList&
460Target::GetImages ()
461{
462 return m_images;
463}
464
465ArchSpec
466Target::GetArchitecture () const
467{
Jim Ingham5aee1622010-08-09 23:31:02468 return m_arch_spec;
Chris Lattner30fdc8d2010-06-08 16:52:24469}
470
Jim Ingham5aee1622010-08-09 23:31:02471bool
472Target::SetArchitecture (const ArchSpec &arch_spec)
473{
474 if (m_arch_spec == arch_spec)
475 {
476 // If we're setting the architecture to our current architecture, we
477 // don't need to do anything.
478 return true;
479 }
480 else if (!m_arch_spec.IsValid())
481 {
482 // If we haven't got a valid arch spec, then we just need to set it.
483 m_arch_spec = arch_spec;
484 return true;
485 }
486 else
487 {
488 // If we have an executable file, try to reset the executable to the desired architecture
489 m_arch_spec = arch_spec;
490 ModuleSP executable_sp = GetExecutableModule ();
491 m_images.Clear();
492 m_scratch_ast_context_ap.reset();
493 m_triple.Clear();
494 // Need to do something about unsetting breakpoints.
495
496 if (executable_sp)
497 {
498 FileSpec exec_file_spec = executable_sp->GetFileSpec();
499 Error error = ModuleList::GetSharedModule(exec_file_spec,
500 arch_spec,
501 NULL,
502 NULL,
503 0,
504 executable_sp,
505 NULL,
506 NULL);
507
508 if (!error.Fail() && executable_sp)
509 {
510 SetExecutableModule (executable_sp, true);
511 return true;
512 }
513 else
514 {
515 return false;
516 }
517 }
518 else
519 {
520 return false;
521 }
522 }
523}
Chris Lattner30fdc8d2010-06-08 16:52:24524
525bool
526Target::GetTargetTriple(ConstString &triple)
527{
528 triple.Clear();
529
530 if (m_triple)
531 {
532 triple = m_triple;
533 }
534 else
535 {
536 Module *exe_module = GetExecutableModule().get();
537 if (exe_module)
538 {
539 ObjectFile *objfile = exe_module->GetObjectFile();
540 if (objfile)
541 {
542 objfile->GetTargetTriple(m_triple);
543 triple = m_triple;
544 }
545 }
546 }
547 return !triple.IsEmpty();
548}
549
550void
551Target::ModuleAdded (ModuleSP &module_sp)
552{
553 // A module is being added to this target for the first time
554 ModuleList module_list;
555 module_list.Append(module_sp);
556 ModulesDidLoad (module_list);
557}
558
559void
560Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
561{
562 // A module is being added to this target for the first time
563 ModuleList module_list;
564 module_list.Append (old_module_sp);
565 ModulesDidUnload (module_list);
566 module_list.Clear ();
567 module_list.Append (new_module_sp);
568 ModulesDidLoad (module_list);
569}
570
571void
572Target::ModulesDidLoad (ModuleList &module_list)
573{
574 m_breakpoint_list.UpdateBreakpoints (module_list, true);
575 // TODO: make event data that packages up the module_list
576 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
577}
578
579void
580Target::ModulesDidUnload (ModuleList &module_list)
581{
582 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26583
584 // Remove the images from the target image list
585 m_images.Remove(module_list);
586
Chris Lattner30fdc8d2010-06-08 16:52:24587 // TODO: make event data that packages up the module_list
588 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
589}
590
591size_t
Greg Clayton35f3dd22010-06-30 23:04:24592Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24593{
Chris Lattner30fdc8d2010-06-08 16:52:24594 error.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24595
Greg Claytondda4f7b2010-06-30 23:03:03596 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
597
598 Address resolved_addr(addr);
599 if (!resolved_addr.IsSectionOffset())
600 {
601 if (process_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24602 {
Greg Claytonf5e56de2010-09-14 23:36:40603 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03604 }
605 else
606 {
607 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
608 }
609 }
610
611
612 if (process_is_valid)
613 {
Greg Claytonf5e56de2010-09-14 23:36:40614 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Claytondda4f7b2010-06-30 23:03:03615 if (load_addr == LLDB_INVALID_ADDRESS)
616 {
617 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
618 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
619 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
620 resolved_addr.GetFileAddress());
621 else
622 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
623 }
624 else
625 {
626 size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24627 if (bytes_read != dst_len)
628 {
629 if (error.Success())
630 {
631 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03632 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24633 else
Greg Claytondda4f7b2010-06-30 23:03:03634 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24635 }
636 }
Greg Claytondda4f7b2010-06-30 23:03:03637 if (bytes_read)
638 return bytes_read;
639 // If the address is not section offset we have an address that
640 // doesn't resolve to any address in any currently loaded shared
641 // libaries and we failed to read memory so there isn't anything
642 // more we can do. If it is section offset, we might be able to
643 // read cached memory from the object file.
644 if (!resolved_addr.IsSectionOffset())
645 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24646 }
Chris Lattner30fdc8d2010-06-08 16:52:24647 }
Greg Claytondda4f7b2010-06-30 23:03:03648
649 const Section *section = resolved_addr.GetSection();
650 if (section && section->GetModule())
651 {
652 ObjectFile *objfile = section->GetModule()->GetObjectFile();
653 return section->ReadSectionDataFromObjectFile (objfile,
654 resolved_addr.GetOffset(),
655 dst,
656 dst_len);
657 }
658 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24659}
660
661
662ModuleSP
663Target::GetSharedModule
664(
665 const FileSpec& file_spec,
666 const ArchSpec& arch,
667 const UUID *uuid_ptr,
668 const ConstString *object_name,
669 off_t object_offset,
670 Error *error_ptr
671)
672{
673 // Don't pass in the UUID so we can tell if we have a stale value in our list
674 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
675 bool did_create_module = false;
676 ModuleSP module_sp;
677
678 // If there are image search path entries, try to use them first to acquire a suitable image.
679
680 Error error;
681
682 if (m_image_search_paths.GetSize())
683 {
684 FileSpec transformed_spec;
685 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
686 {
687 transformed_spec.GetFilename() = file_spec.GetFilename();
688 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
689 }
690 }
691
692 // If a module hasn't been found yet, use the unmodified path.
693
694 if (!module_sp)
695 {
696 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
697 }
698
699 if (module_sp)
700 {
701 m_images.Append (module_sp);
702 if (did_create_module)
703 {
704 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
705 ModuleUpdated(old_module_sp, module_sp);
706 else
707 ModuleAdded(module_sp);
708 }
709 }
710 if (error_ptr)
711 *error_ptr = error;
712 return module_sp;
713}
714
715
716Target *
717Target::CalculateTarget ()
718{
719 return this;
720}
721
722Process *
723Target::CalculateProcess ()
724{
725 return NULL;
726}
727
728Thread *
729Target::CalculateThread ()
730{
731 return NULL;
732}
733
734StackFrame *
735Target::CalculateStackFrame ()
736{
737 return NULL;
738}
739
740void
Greg Clayton0603aa92010-10-04 01:05:56741Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24742{
743 exe_ctx.target = this;
744 exe_ctx.process = NULL; // Do NOT fill in process...
745 exe_ctx.thread = NULL;
746 exe_ctx.frame = NULL;
747}
748
749PathMappingList &
750Target::GetImageSearchPathList ()
751{
752 return m_image_search_paths;
753}
754
755void
756Target::ImageSearchPathsChanged
757(
758 const PathMappingList &path_list,
759 void *baton
760)
761{
762 Target *target = (Target *)baton;
763 if (target->m_images.GetSize() > 1)
764 {
765 ModuleSP exe_module_sp (target->GetExecutableModule());
766 if (exe_module_sp)
767 {
768 target->m_images.Clear();
769 target->SetExecutableModule (exe_module_sp, true);
770 }
771 }
772}
773
774ClangASTContext *
775Target::GetScratchClangASTContext()
776{
777 return m_scratch_ast_context_ap.get();
778}
Caroline Ticedaccaa92010-09-20 20:44:43779
Greg Clayton99d0faf2010-11-18 23:32:35780void
781Target::Initialize ()
Caroline Ticedaccaa92010-09-20 20:44:43782{
Greg Clayton99d0faf2010-11-18 23:32:35783 UserSettingsControllerSP &usc = GetSettingsController();
784 usc.reset (new SettingsController);
785 UserSettingsController::InitializeSettingsController (usc,
786 SettingsController::global_settings_table,
787 SettingsController::instance_settings_table);
788}
Caroline Ticedaccaa92010-09-20 20:44:43789
Greg Clayton99d0faf2010-11-18 23:32:35790void
791Target::Terminate ()
792{
793 UserSettingsControllerSP &usc = GetSettingsController();
794 UserSettingsController::FinalizeSettingsController (usc);
795 usc.reset();
796}
Caroline Ticedaccaa92010-09-20 20:44:43797
Greg Clayton99d0faf2010-11-18 23:32:35798UserSettingsControllerSP &
799Target::GetSettingsController ()
800{
801 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:43802 return g_settings_controller;
803}
804
805ArchSpec
806Target::GetDefaultArchitecture ()
807{
Greg Claytondbe54502010-11-19 03:46:01808 lldb::UserSettingsControllerSP &settings_controller = GetSettingsController();
Caroline Ticedaccaa92010-09-20 20:44:43809 lldb::SettableVariableType var_type;
810 Error err;
811 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err);
812
813 const char *default_name = "";
814 if (result.GetSize() == 1 && err.Success())
815 default_name = result.GetStringAtIndex (0);
816
817 ArchSpec default_arch (default_name);
818 return default_arch;
819}
820
821void
822Target::SetDefaultArchitecture (ArchSpec new_arch)
823{
824 if (new_arch.IsValid())
Greg Claytondbe54502010-11-19 03:46:01825 GetSettingsController ()->SetVariable ("target.default-arch",
826 new_arch.AsCString(),
827 lldb::eVarSetOperationAssign,
828 false,
829 "[]");
Caroline Ticedaccaa92010-09-20 20:44:43830}
831
Greg Clayton0603aa92010-10-04 01:05:56832Target *
833Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
834{
835 // The target can either exist in the "process" of ExecutionContext, or in
836 // the "target_sp" member of SymbolContext. This accessor helper function
837 // will get the target from one of these locations.
838
839 Target *target = NULL;
840 if (sc_ptr != NULL)
841 target = sc_ptr->target_sp.get();
842 if (target == NULL)
843 {
844 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
845 target = &exe_ctx_ptr->process->GetTarget();
846 }
847 return target;
848}
849
850
Caroline Tice1559a462010-09-27 00:30:10851void
852Target::UpdateInstanceName ()
853{
854 StreamString sstr;
855
856 ModuleSP module_sp = GetExecutableModule();
857 if (module_sp)
858 {
Greg Clayton307de252010-10-27 02:06:37859 sstr.Printf ("%s_%s",
860 module_sp->GetFileSpec().GetFilename().AsCString(),
Caroline Tice1559a462010-09-27 00:30:10861 module_sp->GetArchitecture().AsCString());
Greg Claytondbe54502010-11-19 03:46:01862 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
863 sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:10864 }
865}
866
Sean Callanan322f5292010-10-29 00:29:03867const char *
868Target::GetExpressionPrefixContentsAsCString ()
869{
870 return m_expr_prefix_contents.c_str();
871}
872
Greg Clayton8b2fe6d2010-12-14 02:59:59873ExecutionResults
874Target::EvaluateExpression
875(
876 const char *expr_cstr,
877 StackFrame *frame,
878 bool unwind_on_error,
879 lldb::ValueObjectSP &result_valobj_sp
880)
881{
882 ExecutionResults execution_results = eExecutionSetupError;
883
884 result_valobj_sp.reset();
885
886 ExecutionContext exe_ctx;
887 if (frame)
888 {
889 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:08890 Error error;
891 const bool check_ptr_vs_member = true;
892 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, check_ptr_vs_member, error);
Greg Clayton8b2fe6d2010-12-14 02:59:59893 }
894 else if (m_process_sp)
895 {
896 m_process_sp->CalculateExecutionContext(exe_ctx);
897 }
898 else
899 {
900 CalculateExecutionContext(exe_ctx);
901 }
902
903 if (result_valobj_sp)
904 {
905 execution_results = eExecutionCompleted;
906 // We got a result from the frame variable expression path above...
907 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
908
909 lldb::ValueObjectSP const_valobj_sp;
910
911 // Check in case our value is already a constant value
912 if (result_valobj_sp->GetIsConstant())
913 {
914 const_valobj_sp = result_valobj_sp;
915 const_valobj_sp->SetName (persistent_variable_name);
916 }
917 else
918 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(),
919 persistent_variable_name);
920
921 result_valobj_sp = const_valobj_sp;
922
923 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
924 assert (clang_expr_variable_sp.get());
925 }
926 else
927 {
928 // Make sure we aren't just trying to see the value of a persistent
929 // variable (something like "$0")
930 lldb::ClangExpressionVariableSP persistent_var_sp (m_persistent_variables.GetVariable (expr_cstr));
931 if (persistent_var_sp)
932 {
933 result_valobj_sp = persistent_var_sp->GetValueObject ();
934 execution_results = eExecutionCompleted;
935 }
936 else
937 {
938 const char *prefix = GetExpressionPrefixContentsAsCString();
939
940 execution_results = ClangUserExpression::Evaluate (exe_ctx,
941 unwind_on_error,
942 expr_cstr,
943 prefix,
944 result_valobj_sp);
945 }
946 }
947 return execution_results;
948}
949
Caroline Ticedaccaa92010-09-20 20:44:43950//--------------------------------------------------------------
951// class Target::SettingsController
952//--------------------------------------------------------------
953
954Target::SettingsController::SettingsController () :
955 UserSettingsController ("target", Debugger::GetSettingsController()),
956 m_default_architecture ()
957{
958 m_default_settings.reset (new TargetInstanceSettings (*this, false,
959 InstanceSettings::GetDefaultName().AsCString()));
960}
961
962Target::SettingsController::~SettingsController ()
963{
964}
965
966lldb::InstanceSettingsSP
967Target::SettingsController::CreateInstanceSettings (const char *instance_name)
968{
Greg Claytondbe54502010-11-19 03:46:01969 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
970 false,
971 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:43972 lldb::InstanceSettingsSP new_settings_sp (new_settings);
973 return new_settings_sp;
974}
975
976const ConstString &
977Target::SettingsController::DefArchVarName ()
978{
979 static ConstString def_arch_var_name ("default-arch");
980
981 return def_arch_var_name;
982}
983
984bool
985Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
986 const char *index_value,
987 const char *value,
988 const SettingEntry &entry,
989 const lldb::VarSetOperationType op,
990 Error&err)
991{
992 if (var_name == DefArchVarName())
993 {
994 ArchSpec tmp_spec (value);
995 if (tmp_spec.IsValid())
996 m_default_architecture = tmp_spec;
997 else
998 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
999 }
1000 return true;
1001}
1002
1003
1004bool
1005Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1006 StringList &value,
1007 Error &err)
1008{
1009 if (var_name == DefArchVarName())
1010 {
Greg Clayton307de252010-10-27 02:06:371011 // If the arch is invalid (the default), don't show a string for it
1012 if (m_default_architecture.IsValid())
1013 value.AppendString (m_default_architecture.AsCString());
Caroline Ticedaccaa92010-09-20 20:44:431014 return true;
1015 }
1016 else
1017 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1018
1019 return false;
1020}
1021
1022//--------------------------------------------------------------
1023// class TargetInstanceSettings
1024//--------------------------------------------------------------
1025
Greg Clayton85851dd2010-12-04 00:10:171026TargetInstanceSettings::TargetInstanceSettings
1027(
1028 UserSettingsController &owner,
1029 bool live_instance,
1030 const char *name
1031) :
1032 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance)
Caroline Ticedaccaa92010-09-20 20:44:431033{
1034 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1035 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1036 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1037 // This is true for CreateInstanceName() too.
1038
1039 if (GetInstanceName () == InstanceSettings::InvalidName())
1040 {
1041 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1042 m_owner.RegisterInstanceSettings (this);
1043 }
1044
1045 if (live_instance)
1046 {
1047 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1048 CopyInstanceSettings (pending_settings,false);
1049 //m_owner.RemovePendingSettings (m_instance_name);
1050 }
1051}
1052
1053TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytondbe54502010-11-19 03:46:011054 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Ticedaccaa92010-09-20 20:44:431055{
1056 if (m_instance_name != InstanceSettings::GetDefaultName())
1057 {
1058 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1059 CopyInstanceSettings (pending_settings,false);
1060 //m_owner.RemovePendingSettings (m_instance_name);
1061 }
1062}
1063
1064TargetInstanceSettings::~TargetInstanceSettings ()
1065{
1066}
1067
1068TargetInstanceSettings&
1069TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1070{
1071 if (this != &rhs)
1072 {
1073 }
1074
1075 return *this;
1076}
1077
Sean Callanan322f5292010-10-29 00:29:031078#define EXPR_PREFIX_STRING "expr-prefix"
Caroline Ticedaccaa92010-09-20 20:44:431079
1080void
1081TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1082 const char *index_value,
1083 const char *value,
1084 const ConstString &instance_name,
1085 const SettingEntry &entry,
1086 lldb::VarSetOperationType op,
1087 Error &err,
1088 bool pending)
1089{
Sean Callanan322f5292010-10-29 00:29:031090 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
1091
1092 if (var_name == expr_prefix_str)
1093 {
1094 switch (op)
1095 {
1096 default:
1097 err.SetErrorToGenericError ();
1098 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1099 return;
1100 case lldb::eVarSetOperationAssign:
1101 {
1102 FileSpec file_spec(value, true);
1103
1104 if (!file_spec.Exists())
1105 {
1106 err.SetErrorToGenericError ();
1107 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1108 return;
1109 }
1110
1111 DataBufferMemoryMap buf;
1112
1113 if (!buf.MemoryMapFromFileSpec(&file_spec) &&
1114 buf.GetError().Fail())
1115 {
1116 err.SetErrorToGenericError ();
1117 err.SetErrorStringWithFormat ("Couldn't read from %s: %s\n", value, buf.GetError().AsCString());
1118 return;
1119 }
1120
1121 m_expr_prefix_path = value;
1122 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(buf.GetBytes()), buf.GetByteSize());
1123 }
1124 return;
1125 case lldb::eVarSetOperationAppend:
1126 err.SetErrorToGenericError ();
1127 err.SetErrorString ("Cannot append to a path.\n");
1128 return;
1129 case lldb::eVarSetOperationClear:
1130 m_expr_prefix_path.clear ();
1131 m_expr_prefix_contents.clear ();
1132 return;
1133 }
1134 }
Caroline Ticedaccaa92010-09-20 20:44:431135}
1136
1137void
1138TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
Sean Callanan322f5292010-10-29 00:29:031139 bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431140{
Sean Callanan322f5292010-10-29 00:29:031141 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1142
1143 if (!new_settings_ptr)
1144 return;
1145
1146 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1147 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
Caroline Ticedaccaa92010-09-20 20:44:431148}
1149
Caroline Tice12cecd72010-09-20 21:37:421150bool
Caroline Ticedaccaa92010-09-20 20:44:431151TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1152 const ConstString &var_name,
1153 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421154 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431155{
Sean Callanan322f5292010-10-29 00:29:031156 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
1157
1158 if (var_name == expr_prefix_str)
1159 {
1160 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1161 }
1162 else
1163 {
1164 if (err)
1165 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1166 return false;
1167 }
1168
1169 return true;
Caroline Ticedaccaa92010-09-20 20:44:431170}
1171
1172const ConstString
1173TargetInstanceSettings::CreateInstanceName ()
1174{
Caroline Ticedaccaa92010-09-20 20:44:431175 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:101176 static int instance_count = 1;
1177
Caroline Ticedaccaa92010-09-20 20:44:431178 sstr.Printf ("target_%d", instance_count);
1179 ++instance_count;
1180
1181 const ConstString ret_val (sstr.GetData());
1182 return ret_val;
1183}
1184
1185//--------------------------------------------------
1186// Target::SettingsController Variable Tables
1187//--------------------------------------------------
1188
1189SettingEntry
1190Target::SettingsController::global_settings_table[] =
1191{
Sean Callanan322f5292010-10-29 00:29:031192 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1193 { "default-arch", eSetVarTypeString, NULL, NULL, false, false, "Default architecture to choose, when there's a choice." },
Caroline Ticedaccaa92010-09-20 20:44:431194 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1195};
1196
1197SettingEntry
1198Target::SettingsController::instance_settings_table[] =
1199{
Sean Callanan322f5292010-10-29 00:29:031200 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1201 { EXPR_PREFIX_STRING, eSetVarTypeString, NULL, NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
Caroline Ticedaccaa92010-09-20 20:44:431202 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1203};