blob: da42e47c08d925ae020c1da23d56772b3782de85 [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"
Sean Callanan77e93942010-10-29 00:29:03 +000020#include "lldb/Core/DataBufferMemoryMap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
23#include "lldb/Core/Timer.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/Host/Host.h"
26#include "lldb/lldb-private-log.h"
27#include "lldb/Symbol/ObjectFile.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Core/Debugger.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34//----------------------------------------------------------------------
35// Target constructor
36//----------------------------------------------------------------------
Greg Clayton63094e02010-06-23 01:19:29 +000037Target::Target(Debugger &debugger) :
Greg Clayton49ce6822010-10-31 03:01:06 +000038 Broadcaster("lldb.target"),
Caroline Tice5bc8c972010-09-20 20:44:43 +000039 TargetInstanceSettings (*(Target::GetSettingsController().get())),
Greg Clayton63094e02010-06-23 01:19:29 +000040 m_debugger (debugger),
Chris Lattner24943d22010-06-08 16:52:24 +000041 m_images(),
Greg Claytoneea26402010-09-14 23:36:40 +000042 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000043 m_breakpoint_list (false),
44 m_internal_breakpoint_list (true),
45 m_process_sp(),
46 m_triple(),
47 m_search_filter_sp(),
48 m_image_search_paths (ImageSearchPathsChanged, this),
49 m_scratch_ast_context_ap(NULL)
50{
Greg Clayton49ce6822010-10-31 03:01:06 +000051 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
52 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
53 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
54
Greg Claytone005f2c2010-11-06 01:53:30 +000055 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000056 if (log)
57 log->Printf ("%p Target::Target()", this);
58}
59
60//----------------------------------------------------------------------
61// Destructor
62//----------------------------------------------------------------------
63Target::~Target()
64{
Greg Claytone005f2c2010-11-06 01:53:30 +000065 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000066 if (log)
67 log->Printf ("%p Target::~Target()", this);
68 DeleteCurrentProcess ();
69}
70
71void
Caroline Tice7826c882010-10-26 03:11:13 +000072Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000073{
Greg Clayton3fed8b92010-10-08 00:21:05 +000074// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000075 if (description_level != lldb::eDescriptionLevelBrief)
76 {
77 s->Indent();
78 s->PutCString("Target\n");
79 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000080 m_images.Dump(s);
81 m_breakpoint_list.Dump(s);
82 m_internal_breakpoint_list.Dump(s);
83 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +000084 }
85 else
86 {
Greg Claytona66ba462010-10-30 04:51:46 +000087 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Tice7826c882010-10-26 03:11:13 +000088 }
Chris Lattner24943d22010-06-08 16:52:24 +000089}
90
91void
92Target::DeleteCurrentProcess ()
93{
94 if (m_process_sp.get())
95 {
Greg Clayton49480b12010-09-14 23:52:43 +000096 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000097 if (m_process_sp->IsAlive())
98 m_process_sp->Destroy();
99 else
100 m_process_sp->Finalize();
101
102 // Do any cleanup of the target we need to do between process instances.
103 // NB It is better to do this before destroying the process in case the
104 // clean up needs some help from the process.
105 m_breakpoint_list.ClearAllBreakpointSites();
106 m_internal_breakpoint_list.ClearAllBreakpointSites();
107 m_process_sp.reset();
108 }
109}
110
111const lldb::ProcessSP &
112Target::CreateProcess (Listener &listener, const char *plugin_name)
113{
114 DeleteCurrentProcess ();
115 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
116 return m_process_sp;
117}
118
119const lldb::ProcessSP &
120Target::GetProcessSP () const
121{
122 return m_process_sp;
123}
124
125lldb::TargetSP
126Target::GetSP()
127{
Greg Clayton63094e02010-06-23 01:19:29 +0000128 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000129}
130
131BreakpointList &
132Target::GetBreakpointList(bool internal)
133{
134 if (internal)
135 return m_internal_breakpoint_list;
136 else
137 return m_breakpoint_list;
138}
139
140const BreakpointList &
141Target::GetBreakpointList(bool internal) const
142{
143 if (internal)
144 return m_internal_breakpoint_list;
145 else
146 return m_breakpoint_list;
147}
148
149BreakpointSP
150Target::GetBreakpointByID (break_id_t break_id)
151{
152 BreakpointSP bp_sp;
153
154 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
155 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
156 else
157 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
158
159 return bp_sp;
160}
161
162BreakpointSP
163Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
164{
165 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
166 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
167 return CreateBreakpoint (filter_sp, resolver_sp, internal);
168}
169
170
171BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000172Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000173{
Chris Lattner24943d22010-06-08 16:52:24 +0000174 Address so_addr;
175 // Attempt to resolve our load address if possible, though it is ok if
176 // it doesn't resolve to section/offset.
177
Greg Clayton33ed1702010-08-24 00:45:41 +0000178 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000179 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000180 if (!so_addr.IsValid())
181 {
182 // The address didn't resolve, so just set this as an absolute address
183 so_addr.SetOffset (addr);
184 }
185 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000186 return bp_sp;
187}
188
189BreakpointSP
190Target::CreateBreakpoint (Address &addr, bool internal)
191{
192 TargetSP target_sp = this->GetSP();
193 SearchFilterSP filter_sp(new SearchFilter (target_sp));
194 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
195 return CreateBreakpoint (filter_sp, resolver_sp, internal);
196}
197
198BreakpointSP
Greg Clayton12bec712010-06-28 21:30:43 +0000199Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000200{
Greg Clayton12bec712010-06-28 21:30:43 +0000201 BreakpointSP bp_sp;
202 if (func_name)
203 {
204 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
205 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
206 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
207 }
208 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000209}
210
211
212SearchFilterSP
213Target::GetSearchFilterForModule (const FileSpec *containingModule)
214{
215 SearchFilterSP filter_sp;
216 lldb::TargetSP target_sp = this->GetSP();
217 if (containingModule != NULL)
218 {
219 // TODO: We should look into sharing module based search filters
220 // across many breakpoints like we do for the simple target based one
221 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
222 }
223 else
224 {
225 if (m_search_filter_sp.get() == NULL)
226 m_search_filter_sp.reset (new SearchFilter (target_sp));
227 filter_sp = m_search_filter_sp;
228 }
229 return filter_sp;
230}
231
232BreakpointSP
233Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
234{
235 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
236 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
237
238 return CreateBreakpoint (filter_sp, resolver_sp, internal);
239}
240
241BreakpointSP
242Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
243{
244 BreakpointSP bp_sp;
245 if (filter_sp && resolver_sp)
246 {
247 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
248 resolver_sp->SetBreakpoint (bp_sp.get());
249
250 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000251 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000252 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000253 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000254
Greg Claytone005f2c2010-11-06 01:53:30 +0000255 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000256 if (log)
257 {
258 StreamString s;
259 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
260 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
261 }
262
Chris Lattner24943d22010-06-08 16:52:24 +0000263 bp_sp->ResolveBreakpoint();
264 }
Jim Inghamd1686902010-10-14 23:45:03 +0000265
266 if (!internal && bp_sp)
267 {
268 m_last_created_breakpoint = bp_sp;
269 }
270
Chris Lattner24943d22010-06-08 16:52:24 +0000271 return bp_sp;
272}
273
274void
275Target::RemoveAllBreakpoints (bool internal_also)
276{
Greg Claytone005f2c2010-11-06 01:53:30 +0000277 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000278 if (log)
279 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
280
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000281 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000282 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000283 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000284
285 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000286}
287
288void
289Target::DisableAllBreakpoints (bool internal_also)
290{
Greg Claytone005f2c2010-11-06 01:53:30 +0000291 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000292 if (log)
293 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
294
295 m_breakpoint_list.SetEnabledAll (false);
296 if (internal_also)
297 m_internal_breakpoint_list.SetEnabledAll (false);
298}
299
300void
301Target::EnableAllBreakpoints (bool internal_also)
302{
Greg Claytone005f2c2010-11-06 01:53:30 +0000303 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000304 if (log)
305 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
306
307 m_breakpoint_list.SetEnabledAll (true);
308 if (internal_also)
309 m_internal_breakpoint_list.SetEnabledAll (true);
310}
311
312bool
313Target::RemoveBreakpointByID (break_id_t break_id)
314{
Greg Claytone005f2c2010-11-06 01:53:30 +0000315 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000316 if (log)
317 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
318
319 if (DisableBreakpointByID (break_id))
320 {
321 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000322 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000323 else
Jim Inghamd1686902010-10-14 23:45:03 +0000324 {
325 if (m_last_created_breakpoint->GetID() == break_id)
326 m_last_created_breakpoint.reset();
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000327 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000328 }
Chris Lattner24943d22010-06-08 16:52:24 +0000329 return true;
330 }
331 return false;
332}
333
334bool
335Target::DisableBreakpointByID (break_id_t break_id)
336{
Greg Claytone005f2c2010-11-06 01:53:30 +0000337 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000338 if (log)
339 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
340
341 BreakpointSP bp_sp;
342
343 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
344 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
345 else
346 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
347 if (bp_sp)
348 {
349 bp_sp->SetEnabled (false);
350 return true;
351 }
352 return false;
353}
354
355bool
356Target::EnableBreakpointByID (break_id_t break_id)
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 (break_id = %i, internal = %s)\n",
361 __FUNCTION__,
362 break_id,
363 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
364
365 BreakpointSP bp_sp;
366
367 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
368 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
369 else
370 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
371
372 if (bp_sp)
373 {
374 bp_sp->SetEnabled (true);
375 return true;
376 }
377 return false;
378}
379
380ModuleSP
381Target::GetExecutableModule ()
382{
383 ModuleSP executable_sp;
384 if (m_images.GetSize() > 0)
385 executable_sp = m_images.GetModuleAtIndex(0);
386 return executable_sp;
387}
388
389void
390Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
391{
392 m_images.Clear();
393 m_scratch_ast_context_ap.reset();
394
395 if (executable_sp.get())
396 {
397 Timer scoped_timer (__PRETTY_FUNCTION__,
398 "Target::SetExecutableModule (executable = '%s/%s')",
399 executable_sp->GetFileSpec().GetDirectory().AsCString(),
400 executable_sp->GetFileSpec().GetFilename().AsCString());
401
402 m_images.Append(executable_sp); // The first image is our exectuable file
403
404 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000405 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
406 if (!m_arch_spec.IsValid())
407 m_arch_spec = exe_arch;
408
Chris Lattner24943d22010-06-08 16:52:24 +0000409 FileSpecList dependent_files;
410 ObjectFile * executable_objfile = executable_sp->GetObjectFile();
411 if (executable_objfile == NULL)
412 {
413
414 FileSpec bundle_executable(executable_sp->GetFileSpec());
Greg Clayton24b48ff2010-10-17 22:03:32 +0000415 if (Host::ResolveExecutableInBundle (bundle_executable))
Chris Lattner24943d22010-06-08 16:52:24 +0000416 {
417 ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
418 exe_arch));
419 SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
420 if (bundle_exe_module_sp->GetObjectFile() != NULL)
421 executable_sp = bundle_exe_module_sp;
422 return;
423 }
424 }
425
426 if (executable_objfile)
427 {
428 executable_objfile->GetDependentModules(dependent_files);
429 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
430 {
431 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
432 exe_arch));
433 if (image_module_sp.get())
434 {
435 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
436 ObjectFile *objfile = image_module_sp->GetObjectFile();
437 if (objfile)
438 objfile->GetDependentModules(dependent_files);
439 }
440 }
441 }
442
443 // Now see if we know the target triple, and if so, create our scratch AST context:
444 ConstString target_triple;
445 if (GetTargetTriple(target_triple))
446 {
447 m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString()));
448 }
449 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000450
451 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000452}
453
454
455ModuleList&
456Target::GetImages ()
457{
458 return m_images;
459}
460
461ArchSpec
462Target::GetArchitecture () const
463{
Jim Ingham7508e732010-08-09 23:31:02 +0000464 return m_arch_spec;
Chris Lattner24943d22010-06-08 16:52:24 +0000465}
466
Jim Ingham7508e732010-08-09 23:31:02 +0000467bool
468Target::SetArchitecture (const ArchSpec &arch_spec)
469{
470 if (m_arch_spec == arch_spec)
471 {
472 // If we're setting the architecture to our current architecture, we
473 // don't need to do anything.
474 return true;
475 }
476 else if (!m_arch_spec.IsValid())
477 {
478 // If we haven't got a valid arch spec, then we just need to set it.
479 m_arch_spec = arch_spec;
480 return true;
481 }
482 else
483 {
484 // If we have an executable file, try to reset the executable to the desired architecture
485 m_arch_spec = arch_spec;
486 ModuleSP executable_sp = GetExecutableModule ();
487 m_images.Clear();
488 m_scratch_ast_context_ap.reset();
489 m_triple.Clear();
490 // Need to do something about unsetting breakpoints.
491
492 if (executable_sp)
493 {
494 FileSpec exec_file_spec = executable_sp->GetFileSpec();
495 Error error = ModuleList::GetSharedModule(exec_file_spec,
496 arch_spec,
497 NULL,
498 NULL,
499 0,
500 executable_sp,
501 NULL,
502 NULL);
503
504 if (!error.Fail() && executable_sp)
505 {
506 SetExecutableModule (executable_sp, true);
507 return true;
508 }
509 else
510 {
511 return false;
512 }
513 }
514 else
515 {
516 return false;
517 }
518 }
519}
Chris Lattner24943d22010-06-08 16:52:24 +0000520
521bool
522Target::GetTargetTriple(ConstString &triple)
523{
524 triple.Clear();
525
526 if (m_triple)
527 {
528 triple = m_triple;
529 }
530 else
531 {
532 Module *exe_module = GetExecutableModule().get();
533 if (exe_module)
534 {
535 ObjectFile *objfile = exe_module->GetObjectFile();
536 if (objfile)
537 {
538 objfile->GetTargetTriple(m_triple);
539 triple = m_triple;
540 }
541 }
542 }
543 return !triple.IsEmpty();
544}
545
546void
547Target::ModuleAdded (ModuleSP &module_sp)
548{
549 // A module is being added to this target for the first time
550 ModuleList module_list;
551 module_list.Append(module_sp);
552 ModulesDidLoad (module_list);
553}
554
555void
556Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
557{
558 // A module is being added to this target for the first time
559 ModuleList module_list;
560 module_list.Append (old_module_sp);
561 ModulesDidUnload (module_list);
562 module_list.Clear ();
563 module_list.Append (new_module_sp);
564 ModulesDidLoad (module_list);
565}
566
567void
568Target::ModulesDidLoad (ModuleList &module_list)
569{
570 m_breakpoint_list.UpdateBreakpoints (module_list, true);
571 // TODO: make event data that packages up the module_list
572 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
573}
574
575void
576Target::ModulesDidUnload (ModuleList &module_list)
577{
578 m_breakpoint_list.UpdateBreakpoints (module_list, false);
579 // TODO: make event data that packages up the module_list
580 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
581}
582
583size_t
Greg Clayton2cf6e9e2010-06-30 23:04:24 +0000584Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000585{
Chris Lattner24943d22010-06-08 16:52:24 +0000586 error.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000587
Greg Clayton70436352010-06-30 23:03:03 +0000588 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
589
590 Address resolved_addr(addr);
591 if (!resolved_addr.IsSectionOffset())
592 {
593 if (process_is_valid)
Chris Lattner24943d22010-06-08 16:52:24 +0000594 {
Greg Claytoneea26402010-09-14 23:36:40 +0000595 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000596 }
597 else
598 {
599 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
600 }
601 }
602
603
604 if (process_is_valid)
605 {
Greg Claytoneea26402010-09-14 23:36:40 +0000606 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000607 if (load_addr == LLDB_INVALID_ADDRESS)
608 {
609 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
610 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
611 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
612 resolved_addr.GetFileAddress());
613 else
614 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
615 }
616 else
617 {
618 size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000619 if (bytes_read != dst_len)
620 {
621 if (error.Success())
622 {
623 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000624 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000625 else
Greg Clayton70436352010-06-30 23:03:03 +0000626 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 +0000627 }
628 }
Greg Clayton70436352010-06-30 23:03:03 +0000629 if (bytes_read)
630 return bytes_read;
631 // If the address is not section offset we have an address that
632 // doesn't resolve to any address in any currently loaded shared
633 // libaries and we failed to read memory so there isn't anything
634 // more we can do. If it is section offset, we might be able to
635 // read cached memory from the object file.
636 if (!resolved_addr.IsSectionOffset())
637 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000638 }
Chris Lattner24943d22010-06-08 16:52:24 +0000639 }
Greg Clayton70436352010-06-30 23:03:03 +0000640
641 const Section *section = resolved_addr.GetSection();
642 if (section && section->GetModule())
643 {
644 ObjectFile *objfile = section->GetModule()->GetObjectFile();
645 return section->ReadSectionDataFromObjectFile (objfile,
646 resolved_addr.GetOffset(),
647 dst,
648 dst_len);
649 }
650 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000651}
652
653
654ModuleSP
655Target::GetSharedModule
656(
657 const FileSpec& file_spec,
658 const ArchSpec& arch,
659 const UUID *uuid_ptr,
660 const ConstString *object_name,
661 off_t object_offset,
662 Error *error_ptr
663)
664{
665 // Don't pass in the UUID so we can tell if we have a stale value in our list
666 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
667 bool did_create_module = false;
668 ModuleSP module_sp;
669
670 // If there are image search path entries, try to use them first to acquire a suitable image.
671
672 Error error;
673
674 if (m_image_search_paths.GetSize())
675 {
676 FileSpec transformed_spec;
677 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
678 {
679 transformed_spec.GetFilename() = file_spec.GetFilename();
680 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
681 }
682 }
683
684 // If a module hasn't been found yet, use the unmodified path.
685
686 if (!module_sp)
687 {
688 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
689 }
690
691 if (module_sp)
692 {
693 m_images.Append (module_sp);
694 if (did_create_module)
695 {
696 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
697 ModuleUpdated(old_module_sp, module_sp);
698 else
699 ModuleAdded(module_sp);
700 }
701 }
702 if (error_ptr)
703 *error_ptr = error;
704 return module_sp;
705}
706
707
708Target *
709Target::CalculateTarget ()
710{
711 return this;
712}
713
714Process *
715Target::CalculateProcess ()
716{
717 return NULL;
718}
719
720Thread *
721Target::CalculateThread ()
722{
723 return NULL;
724}
725
726StackFrame *
727Target::CalculateStackFrame ()
728{
729 return NULL;
730}
731
732void
Greg Claytona830adb2010-10-04 01:05:56 +0000733Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000734{
735 exe_ctx.target = this;
736 exe_ctx.process = NULL; // Do NOT fill in process...
737 exe_ctx.thread = NULL;
738 exe_ctx.frame = NULL;
739}
740
741PathMappingList &
742Target::GetImageSearchPathList ()
743{
744 return m_image_search_paths;
745}
746
747void
748Target::ImageSearchPathsChanged
749(
750 const PathMappingList &path_list,
751 void *baton
752)
753{
754 Target *target = (Target *)baton;
755 if (target->m_images.GetSize() > 1)
756 {
757 ModuleSP exe_module_sp (target->GetExecutableModule());
758 if (exe_module_sp)
759 {
760 target->m_images.Clear();
761 target->SetExecutableModule (exe_module_sp, true);
762 }
763 }
764}
765
766ClangASTContext *
767Target::GetScratchClangASTContext()
768{
769 return m_scratch_ast_context_ap.get();
770}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000771
Greg Clayton990de7b2010-11-18 23:32:35 +0000772void
773Target::Initialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000774{
Greg Clayton990de7b2010-11-18 23:32:35 +0000775 UserSettingsControllerSP &usc = GetSettingsController();
776 usc.reset (new SettingsController);
777 UserSettingsController::InitializeSettingsController (usc,
778 SettingsController::global_settings_table,
779 SettingsController::instance_settings_table);
780}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000781
Greg Clayton990de7b2010-11-18 23:32:35 +0000782void
783Target::Terminate ()
784{
785 UserSettingsControllerSP &usc = GetSettingsController();
786 UserSettingsController::FinalizeSettingsController (usc);
787 usc.reset();
788}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000789
Greg Clayton990de7b2010-11-18 23:32:35 +0000790UserSettingsControllerSP &
791Target::GetSettingsController ()
792{
793 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000794 return g_settings_controller;
795}
796
797ArchSpec
798Target::GetDefaultArchitecture ()
799{
800 lldb::UserSettingsControllerSP settings_controller = Target::GetSettingsController();
801 lldb::SettableVariableType var_type;
802 Error err;
803 StringList result = settings_controller->GetVariable ("target.default-arch", var_type, "[]", err);
804
805 const char *default_name = "";
806 if (result.GetSize() == 1 && err.Success())
807 default_name = result.GetStringAtIndex (0);
808
809 ArchSpec default_arch (default_name);
810 return default_arch;
811}
812
813void
814Target::SetDefaultArchitecture (ArchSpec new_arch)
815{
816 if (new_arch.IsValid())
817 Target::GetSettingsController ()->SetVariable ("target.default-arch", new_arch.AsCString(),
818 lldb::eVarSetOperationAssign, false, "[]");
819}
820
Greg Claytona830adb2010-10-04 01:05:56 +0000821Target *
822Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
823{
824 // The target can either exist in the "process" of ExecutionContext, or in
825 // the "target_sp" member of SymbolContext. This accessor helper function
826 // will get the target from one of these locations.
827
828 Target *target = NULL;
829 if (sc_ptr != NULL)
830 target = sc_ptr->target_sp.get();
831 if (target == NULL)
832 {
833 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
834 target = &exe_ctx_ptr->process->GetTarget();
835 }
836 return target;
837}
838
839
Caroline Tice1ebef442010-09-27 00:30:10 +0000840void
841Target::UpdateInstanceName ()
842{
843 StreamString sstr;
844
845 ModuleSP module_sp = GetExecutableModule();
846 if (module_sp)
847 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000848 sstr.Printf ("%s_%s",
849 module_sp->GetFileSpec().GetFilename().AsCString(),
Caroline Tice1ebef442010-09-27 00:30:10 +0000850 module_sp->GetArchitecture().AsCString());
Greg Claytonbf6e2102010-10-27 02:06:37 +0000851 Target::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
852 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +0000853 }
854}
855
Sean Callanan77e93942010-10-29 00:29:03 +0000856const char *
857Target::GetExpressionPrefixContentsAsCString ()
858{
859 return m_expr_prefix_contents.c_str();
860}
861
Caroline Tice5bc8c972010-09-20 20:44:43 +0000862//--------------------------------------------------------------
863// class Target::SettingsController
864//--------------------------------------------------------------
865
866Target::SettingsController::SettingsController () :
867 UserSettingsController ("target", Debugger::GetSettingsController()),
868 m_default_architecture ()
869{
870 m_default_settings.reset (new TargetInstanceSettings (*this, false,
871 InstanceSettings::GetDefaultName().AsCString()));
872}
873
874Target::SettingsController::~SettingsController ()
875{
876}
877
878lldb::InstanceSettingsSP
879Target::SettingsController::CreateInstanceSettings (const char *instance_name)
880{
881 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*(Target::GetSettingsController().get()),
882 false, instance_name);
883 lldb::InstanceSettingsSP new_settings_sp (new_settings);
884 return new_settings_sp;
885}
886
887const ConstString &
888Target::SettingsController::DefArchVarName ()
889{
890 static ConstString def_arch_var_name ("default-arch");
891
892 return def_arch_var_name;
893}
894
895bool
896Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
897 const char *index_value,
898 const char *value,
899 const SettingEntry &entry,
900 const lldb::VarSetOperationType op,
901 Error&err)
902{
903 if (var_name == DefArchVarName())
904 {
905 ArchSpec tmp_spec (value);
906 if (tmp_spec.IsValid())
907 m_default_architecture = tmp_spec;
908 else
909 err.SetErrorStringWithFormat ("'%s' is not a valid architecture.", value);
910 }
911 return true;
912}
913
914
915bool
916Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
917 StringList &value,
918 Error &err)
919{
920 if (var_name == DefArchVarName())
921 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000922 // If the arch is invalid (the default), don't show a string for it
923 if (m_default_architecture.IsValid())
924 value.AppendString (m_default_architecture.AsCString());
Caroline Tice5bc8c972010-09-20 20:44:43 +0000925 return true;
926 }
927 else
928 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
929
930 return false;
931}
932
933//--------------------------------------------------------------
934// class TargetInstanceSettings
935//--------------------------------------------------------------
936
937TargetInstanceSettings::TargetInstanceSettings (UserSettingsController &owner, bool live_instance,
938 const char *name) :
939 InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance)
940{
941 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
942 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
943 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
944 // This is true for CreateInstanceName() too.
945
946 if (GetInstanceName () == InstanceSettings::InvalidName())
947 {
948 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
949 m_owner.RegisterInstanceSettings (this);
950 }
951
952 if (live_instance)
953 {
954 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
955 CopyInstanceSettings (pending_settings,false);
956 //m_owner.RemovePendingSettings (m_instance_name);
957 }
958}
959
960TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
961 InstanceSettings (*(Target::GetSettingsController().get()), CreateInstanceName().AsCString())
962{
963 if (m_instance_name != InstanceSettings::GetDefaultName())
964 {
965 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
966 CopyInstanceSettings (pending_settings,false);
967 //m_owner.RemovePendingSettings (m_instance_name);
968 }
969}
970
971TargetInstanceSettings::~TargetInstanceSettings ()
972{
973}
974
975TargetInstanceSettings&
976TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
977{
978 if (this != &rhs)
979 {
980 }
981
982 return *this;
983}
984
Sean Callanan77e93942010-10-29 00:29:03 +0000985#define EXPR_PREFIX_STRING "expr-prefix"
Caroline Tice5bc8c972010-09-20 20:44:43 +0000986
987void
988TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
989 const char *index_value,
990 const char *value,
991 const ConstString &instance_name,
992 const SettingEntry &entry,
993 lldb::VarSetOperationType op,
994 Error &err,
995 bool pending)
996{
Sean Callanan77e93942010-10-29 00:29:03 +0000997 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
998
999 if (var_name == expr_prefix_str)
1000 {
1001 switch (op)
1002 {
1003 default:
1004 err.SetErrorToGenericError ();
1005 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1006 return;
1007 case lldb::eVarSetOperationAssign:
1008 {
1009 FileSpec file_spec(value, true);
1010
1011 if (!file_spec.Exists())
1012 {
1013 err.SetErrorToGenericError ();
1014 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1015 return;
1016 }
1017
1018 DataBufferMemoryMap buf;
1019
1020 if (!buf.MemoryMapFromFileSpec(&file_spec) &&
1021 buf.GetError().Fail())
1022 {
1023 err.SetErrorToGenericError ();
1024 err.SetErrorStringWithFormat ("Couldn't read from %s: %s\n", value, buf.GetError().AsCString());
1025 return;
1026 }
1027
1028 m_expr_prefix_path = value;
1029 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(buf.GetBytes()), buf.GetByteSize());
1030 }
1031 return;
1032 case lldb::eVarSetOperationAppend:
1033 err.SetErrorToGenericError ();
1034 err.SetErrorString ("Cannot append to a path.\n");
1035 return;
1036 case lldb::eVarSetOperationClear:
1037 m_expr_prefix_path.clear ();
1038 m_expr_prefix_contents.clear ();
1039 return;
1040 }
1041 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001042}
1043
1044void
1045TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
Sean Callanan77e93942010-10-29 00:29:03 +00001046 bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001047{
Sean Callanan77e93942010-10-29 00:29:03 +00001048 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1049
1050 if (!new_settings_ptr)
1051 return;
1052
1053 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1054 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001055}
1056
Caroline Ticebcb5b452010-09-20 21:37:42 +00001057bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001058TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1059 const ConstString &var_name,
1060 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001061 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001062{
Sean Callanan77e93942010-10-29 00:29:03 +00001063 static ConstString expr_prefix_str (EXPR_PREFIX_STRING);
1064
1065 if (var_name == expr_prefix_str)
1066 {
1067 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1068 }
1069 else
1070 {
1071 if (err)
1072 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1073 return false;
1074 }
1075
1076 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001077}
1078
1079const ConstString
1080TargetInstanceSettings::CreateInstanceName ()
1081{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001082 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001083 static int instance_count = 1;
1084
Caroline Tice5bc8c972010-09-20 20:44:43 +00001085 sstr.Printf ("target_%d", instance_count);
1086 ++instance_count;
1087
1088 const ConstString ret_val (sstr.GetData());
1089 return ret_val;
1090}
1091
1092//--------------------------------------------------
1093// Target::SettingsController Variable Tables
1094//--------------------------------------------------
1095
1096SettingEntry
1097Target::SettingsController::global_settings_table[] =
1098{
Sean Callanan77e93942010-10-29 00:29:03 +00001099 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1100 { "default-arch", eSetVarTypeString, NULL, NULL, false, false, "Default architecture to choose, when there's a choice." },
Caroline Tice5bc8c972010-09-20 20:44:43 +00001101 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1102};
1103
1104SettingEntry
1105Target::SettingsController::instance_settings_table[] =
1106{
Sean Callanan77e93942010-10-29 00:29:03 +00001107 //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"},
1108 { EXPR_PREFIX_STRING, eSetVarTypeString, NULL, NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
Caroline Tice5bc8c972010-09-20 20:44:43 +00001109 { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL }
1110};