blob: 1ba125ca653d73385107aef4515083b1e0a00e3d [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"
20#include "lldb/Core/Event.h"
21#include "lldb/Core/Log.h"
22#include "lldb/Core/Timer.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Host/Host.h"
25#include "lldb/lldb-private-log.h"
26#include "lldb/Symbol/ObjectFile.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Core/Debugger.h"
29
30using namespace lldb;
31using namespace lldb_private;
32
33//----------------------------------------------------------------------
34// Target constructor
35//----------------------------------------------------------------------
Greg Clayton63094e02010-06-23 01:19:29 +000036Target::Target(Debugger &debugger) :
Chris Lattner24943d22010-06-08 16:52:24 +000037 Broadcaster("Target"),
Greg Clayton63094e02010-06-23 01:19:29 +000038 m_debugger (debugger),
Chris Lattner24943d22010-06-08 16:52:24 +000039 m_images(),
40 m_breakpoint_list (false),
41 m_internal_breakpoint_list (true),
42 m_process_sp(),
43 m_triple(),
44 m_search_filter_sp(),
45 m_image_search_paths (ImageSearchPathsChanged, this),
46 m_scratch_ast_context_ap(NULL)
47{
48 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
49 if (log)
50 log->Printf ("%p Target::Target()", this);
51}
52
53//----------------------------------------------------------------------
54// Destructor
55//----------------------------------------------------------------------
56Target::~Target()
57{
58 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT);
59 if (log)
60 log->Printf ("%p Target::~Target()", this);
61 DeleteCurrentProcess ();
62}
63
64void
65Target::Dump (Stream *s)
66{
67 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
68 s->Indent();
69 s->PutCString("Target\n");
70 s->IndentMore();
71 m_images.Dump(s);
72 m_breakpoint_list.Dump(s);
73 m_internal_breakpoint_list.Dump(s);
74// if (m_process_sp.get())
75// m_process_sp->Dump(s);
76 s->IndentLess();
77}
78
79void
80Target::DeleteCurrentProcess ()
81{
82 if (m_process_sp.get())
83 {
84 if (m_process_sp->IsAlive())
85 m_process_sp->Destroy();
86 else
87 m_process_sp->Finalize();
88
89 // Do any cleanup of the target we need to do between process instances.
90 // NB It is better to do this before destroying the process in case the
91 // clean up needs some help from the process.
92 m_breakpoint_list.ClearAllBreakpointSites();
93 m_internal_breakpoint_list.ClearAllBreakpointSites();
94 m_process_sp.reset();
95 }
96}
97
98const lldb::ProcessSP &
99Target::CreateProcess (Listener &listener, const char *plugin_name)
100{
101 DeleteCurrentProcess ();
102 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
103 return m_process_sp;
104}
105
106const lldb::ProcessSP &
107Target::GetProcessSP () const
108{
109 return m_process_sp;
110}
111
112lldb::TargetSP
113Target::GetSP()
114{
Greg Clayton63094e02010-06-23 01:19:29 +0000115 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000116}
117
118BreakpointList &
119Target::GetBreakpointList(bool internal)
120{
121 if (internal)
122 return m_internal_breakpoint_list;
123 else
124 return m_breakpoint_list;
125}
126
127const BreakpointList &
128Target::GetBreakpointList(bool internal) const
129{
130 if (internal)
131 return m_internal_breakpoint_list;
132 else
133 return m_breakpoint_list;
134}
135
136BreakpointSP
137Target::GetBreakpointByID (break_id_t break_id)
138{
139 BreakpointSP bp_sp;
140
141 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
142 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
143 else
144 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
145
146 return bp_sp;
147}
148
149BreakpointSP
150Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
151{
152 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
153 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
154 return CreateBreakpoint (filter_sp, resolver_sp, internal);
155}
156
157
158BreakpointSP
159Target::CreateBreakpoint (lldb::addr_t load_addr, bool internal)
160{
161 BreakpointSP bp_sp;
162 Address so_addr;
163 // Attempt to resolve our load address if possible, though it is ok if
164 // it doesn't resolve to section/offset.
165
166 Process *process = GetProcessSP().get();
167 if (process && process->ResolveLoadAddress(load_addr, so_addr))
168 bp_sp = CreateBreakpoint(so_addr, internal);
169 return bp_sp;
170}
171
172BreakpointSP
173Target::CreateBreakpoint (Address &addr, bool internal)
174{
175 TargetSP target_sp = this->GetSP();
176 SearchFilterSP filter_sp(new SearchFilter (target_sp));
177 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
178 return CreateBreakpoint (filter_sp, resolver_sp, internal);
179}
180
181BreakpointSP
182Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, bool internal)
183{
184 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
185 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name));
186 return CreateBreakpoint (filter_sp, resolver_sp, internal);
187}
188
189
190SearchFilterSP
191Target::GetSearchFilterForModule (const FileSpec *containingModule)
192{
193 SearchFilterSP filter_sp;
194 lldb::TargetSP target_sp = this->GetSP();
195 if (containingModule != NULL)
196 {
197 // TODO: We should look into sharing module based search filters
198 // across many breakpoints like we do for the simple target based one
199 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
200 }
201 else
202 {
203 if (m_search_filter_sp.get() == NULL)
204 m_search_filter_sp.reset (new SearchFilter (target_sp));
205 filter_sp = m_search_filter_sp;
206 }
207 return filter_sp;
208}
209
210BreakpointSP
211Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
212{
213 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
214 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
215
216 return CreateBreakpoint (filter_sp, resolver_sp, internal);
217}
218
219BreakpointSP
220Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
221{
222 BreakpointSP bp_sp;
223 if (filter_sp && resolver_sp)
224 {
225 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
226 resolver_sp->SetBreakpoint (bp_sp.get());
227
228 if (internal)
229 m_internal_breakpoint_list.Add (bp_sp);
230 else
231 m_breakpoint_list.Add (bp_sp);
232
233 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
234 if (log)
235 {
236 StreamString s;
237 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
238 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
239 }
240
241 // Broadcast the breakpoint creation event.
242 if (!internal && EventTypeHasListeners(eBroadcastBitBreakpointChanged))
243 {
244 BroadcastEvent (eBroadcastBitBreakpointChanged,
245 new Breakpoint::BreakpointEventData (Breakpoint::BreakpointEventData::eBreakpointAdded, bp_sp));
246 }
247
248 bp_sp->ResolveBreakpoint();
249 }
250 return bp_sp;
251}
252
253void
254Target::RemoveAllBreakpoints (bool internal_also)
255{
256 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
257 if (log)
258 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
259
260 m_breakpoint_list.RemoveAll();
261 if (internal_also)
262 m_internal_breakpoint_list.RemoveAll();
263}
264
265void
266Target::DisableAllBreakpoints (bool internal_also)
267{
268 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
269 if (log)
270 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
271
272 m_breakpoint_list.SetEnabledAll (false);
273 if (internal_also)
274 m_internal_breakpoint_list.SetEnabledAll (false);
275}
276
277void
278Target::EnableAllBreakpoints (bool internal_also)
279{
280 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
281 if (log)
282 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
283
284 m_breakpoint_list.SetEnabledAll (true);
285 if (internal_also)
286 m_internal_breakpoint_list.SetEnabledAll (true);
287}
288
289bool
290Target::RemoveBreakpointByID (break_id_t break_id)
291{
292 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
293 if (log)
294 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
295
296 if (DisableBreakpointByID (break_id))
297 {
298 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
299 m_internal_breakpoint_list.Remove(break_id);
300 else
301 m_breakpoint_list.Remove(break_id);
302 return true;
303 }
304 return false;
305}
306
307bool
308Target::DisableBreakpointByID (break_id_t break_id)
309{
310 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
311 if (log)
312 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
313
314 BreakpointSP bp_sp;
315
316 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
317 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
318 else
319 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
320 if (bp_sp)
321 {
322 bp_sp->SetEnabled (false);
323 return true;
324 }
325 return false;
326}
327
328bool
329Target::EnableBreakpointByID (break_id_t break_id)
330{
331 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS);
332 if (log)
333 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
334 __FUNCTION__,
335 break_id,
336 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
337
338 BreakpointSP bp_sp;
339
340 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
341 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
342 else
343 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
344
345 if (bp_sp)
346 {
347 bp_sp->SetEnabled (true);
348 return true;
349 }
350 return false;
351}
352
353ModuleSP
354Target::GetExecutableModule ()
355{
356 ModuleSP executable_sp;
357 if (m_images.GetSize() > 0)
358 executable_sp = m_images.GetModuleAtIndex(0);
359 return executable_sp;
360}
361
362void
363Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
364{
365 m_images.Clear();
366 m_scratch_ast_context_ap.reset();
367
368 if (executable_sp.get())
369 {
370 Timer scoped_timer (__PRETTY_FUNCTION__,
371 "Target::SetExecutableModule (executable = '%s/%s')",
372 executable_sp->GetFileSpec().GetDirectory().AsCString(),
373 executable_sp->GetFileSpec().GetFilename().AsCString());
374
375 m_images.Append(executable_sp); // The first image is our exectuable file
376
377 ArchSpec exe_arch = executable_sp->GetArchitecture();
378 FileSpecList dependent_files;
379 ObjectFile * executable_objfile = executable_sp->GetObjectFile();
380 if (executable_objfile == NULL)
381 {
382
383 FileSpec bundle_executable(executable_sp->GetFileSpec());
384 if (Host::ResolveExecutableInBundle (&bundle_executable))
385 {
386 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
387 exe_arch));
388 SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
389 if (bundle_exe_module_sp->GetObjectFile() != NULL)
390 executable_sp = bundle_exe_module_sp;
391 return;
392 }
393 }
394
395 if (executable_objfile)
396 {
397 executable_objfile->GetDependentModules(dependent_files);
398 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
399 {
400 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
401 exe_arch));
402 if (image_module_sp.get())
403 {
404 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
405 ObjectFile *objfile = image_module_sp->GetObjectFile();
406 if (objfile)
407 objfile->GetDependentModules(dependent_files);
408 }
409 }
410 }
411
412 // Now see if we know the target triple, and if so, create our scratch AST context:
413 ConstString target_triple;
414 if (GetTargetTriple(target_triple))
415 {
416 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString()));
417 }
418 }
419}
420
421
422ModuleList&
423Target::GetImages ()
424{
425 return m_images;
426}
427
428ArchSpec
429Target::GetArchitecture () const
430{
431 ArchSpec arch;
432 if (m_images.GetSize() > 0)
433 {
434 Module *exe_module = m_images.GetModulePointerAtIndex(0);
435 if (exe_module)
436 arch = exe_module->GetArchitecture();
437 }
438 return arch;
439}
440
441
442
443bool
444Target::GetTargetTriple(ConstString &triple)
445{
446 triple.Clear();
447
448 if (m_triple)
449 {
450 triple = m_triple;
451 }
452 else
453 {
454 Module *exe_module = GetExecutableModule().get();
455 if (exe_module)
456 {
457 ObjectFile *objfile = exe_module->GetObjectFile();
458 if (objfile)
459 {
460 objfile->GetTargetTriple(m_triple);
461 triple = m_triple;
462 }
463 }
464 }
465 return !triple.IsEmpty();
466}
467
468void
469Target::ModuleAdded (ModuleSP &module_sp)
470{
471 // A module is being added to this target for the first time
472 ModuleList module_list;
473 module_list.Append(module_sp);
474 ModulesDidLoad (module_list);
475}
476
477void
478Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
479{
480 // A module is being added to this target for the first time
481 ModuleList module_list;
482 module_list.Append (old_module_sp);
483 ModulesDidUnload (module_list);
484 module_list.Clear ();
485 module_list.Append (new_module_sp);
486 ModulesDidLoad (module_list);
487}
488
489void
490Target::ModulesDidLoad (ModuleList &module_list)
491{
492 m_breakpoint_list.UpdateBreakpoints (module_list, true);
493 // TODO: make event data that packages up the module_list
494 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
495}
496
497void
498Target::ModulesDidUnload (ModuleList &module_list)
499{
500 m_breakpoint_list.UpdateBreakpoints (module_list, false);
501 // TODO: make event data that packages up the module_list
502 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
503}
504
505size_t
506Target::ReadMemory
507(
508 lldb::AddressType addr_type,
509 lldb::addr_t addr,
510 void *dst,
511 size_t dst_len,
512 Error &error,
513 ObjectFile* objfile
514)
515{
516 size_t bytes_read = 0;
517 error.Clear();
518 switch (addr_type)
519 {
520 case eAddressTypeFile:
521 if (objfile)
522 {
523 if (m_process_sp.get())
524 {
525 // If we have an execution context with a process, lets try and
526 // resolve the file address in "objfile" and read it from the
527 // process
528 Address so_addr(addr, objfile->GetSectionList());
529 lldb::addr_t load_addr = so_addr.GetLoadAddress(m_process_sp.get());
530 if (load_addr == LLDB_INVALID_ADDRESS)
531 {
532 if (objfile->GetFileSpec())
533 error.SetErrorStringWithFormat("0x%llx can't be resolved, %s in not currently loaded.\n", addr, objfile->GetFileSpec().GetFilename().AsCString());
534 else
535 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", addr, objfile->GetFileSpec().GetFilename().AsCString());
536 }
537 else
538 {
539 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
540 if (bytes_read != dst_len)
541 {
542 if (error.Success())
543 {
544 if (bytes_read == 0)
545 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
546 else
547 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
548 }
549 }
550 }
551 }
552 else
553 {
554 // Try and read the file based address from the object file's
555 // section data.
556 }
557 }
558 break;
559
560 case eAddressTypeLoad:
561 if (m_process_sp.get())
562 {
563 bytes_read = m_process_sp->ReadMemory(addr, dst, dst_len, error);
564 if (bytes_read != dst_len)
565 {
566 if (error.Success())
567 {
568 if (bytes_read == 0)
569 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", addr);
570 else
571 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, addr);
572 }
573 }
574 }
575 else
576 error.SetErrorStringWithFormat("Need valid process to read load address.\n");
577 break;
578
579 case eAddressTypeHost:
580 // The address is an address in this process, so just copy it
581 ::memcpy (dst, (uint8_t*)NULL + addr, dst_len);
582 break;
583
584 default:
585 error.SetErrorStringWithFormat ("Unsupported lldb::AddressType value (%i).\n", addr_type);
586 break;
587 }
588 return bytes_read;
589}
590
591
592ModuleSP
593Target::GetSharedModule
594(
595 const FileSpec& file_spec,
596 const ArchSpec& arch,
597 const UUID *uuid_ptr,
598 const ConstString *object_name,
599 off_t object_offset,
600 Error *error_ptr
601)
602{
603 // Don't pass in the UUID so we can tell if we have a stale value in our list
604 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
605 bool did_create_module = false;
606 ModuleSP module_sp;
607
608 // If there are image search path entries, try to use them first to acquire a suitable image.
609
610 Error error;
611
612 if (m_image_search_paths.GetSize())
613 {
614 FileSpec transformed_spec;
615 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
616 {
617 transformed_spec.GetFilename() = file_spec.GetFilename();
618 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
619 }
620 }
621
622 // If a module hasn't been found yet, use the unmodified path.
623
624 if (!module_sp)
625 {
626 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
627 }
628
629 if (module_sp)
630 {
631 m_images.Append (module_sp);
632 if (did_create_module)
633 {
634 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
635 ModuleUpdated(old_module_sp, module_sp);
636 else
637 ModuleAdded(module_sp);
638 }
639 }
640 if (error_ptr)
641 *error_ptr = error;
642 return module_sp;
643}
644
645
646Target *
647Target::CalculateTarget ()
648{
649 return this;
650}
651
652Process *
653Target::CalculateProcess ()
654{
655 return NULL;
656}
657
658Thread *
659Target::CalculateThread ()
660{
661 return NULL;
662}
663
664StackFrame *
665Target::CalculateStackFrame ()
666{
667 return NULL;
668}
669
670void
671Target::Calculate (ExecutionContext &exe_ctx)
672{
673 exe_ctx.target = this;
674 exe_ctx.process = NULL; // Do NOT fill in process...
675 exe_ctx.thread = NULL;
676 exe_ctx.frame = NULL;
677}
678
679PathMappingList &
680Target::GetImageSearchPathList ()
681{
682 return m_image_search_paths;
683}
684
685void
686Target::ImageSearchPathsChanged
687(
688 const PathMappingList &path_list,
689 void *baton
690)
691{
692 Target *target = (Target *)baton;
693 if (target->m_images.GetSize() > 1)
694 {
695 ModuleSP exe_module_sp (target->GetExecutableModule());
696 if (exe_module_sp)
697 {
698 target->m_images.Clear();
699 target->SetExecutableModule (exe_module_sp, true);
700 }
701 }
702}
703
704ClangASTContext *
705Target::GetScratchClangASTContext()
706{
707 return m_scratch_ast_context_ap.get();
708}