blob: 3d8849fe9ecc8e294a3d0b8b25de879949c474b1 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton427f2902010-12-14 02:59:59 +000020#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000024#include "lldb/Core/Timer.h"
25#include "lldb/Core/ValueObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000027#include "lldb/Interpreter/CommandInterpreter.h"
28#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/lldb-private-log.h"
30#include "lldb/Symbol/ObjectFile.h"
31#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000032#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000033#include "lldb/Target/Thread.h"
34#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035
36using namespace lldb;
37using namespace lldb_private;
38
39//----------------------------------------------------------------------
40// Target constructor
41//----------------------------------------------------------------------
Greg Clayton63094e02010-06-23 01:19:29 +000042Target::Target(Debugger &debugger) :
Greg Clayton49ce6822010-10-31 03:01:06 +000043 Broadcaster("lldb.target"),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000044 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000045 m_debugger (debugger),
Greg Claytonbdcda462010-12-20 20:49:23 +000046 m_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000047 m_images(),
Greg Claytoneea26402010-09-14 23:36:40 +000048 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000049 m_breakpoint_list (false),
50 m_internal_breakpoint_list (true),
51 m_process_sp(),
Chris Lattner24943d22010-06-08 16:52:24 +000052 m_search_filter_sp(),
53 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000054 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000055 m_persistent_variables (),
56 m_stop_hook_next_id(0)
Chris Lattner24943d22010-06-08 16:52:24 +000057{
Greg Clayton49ce6822010-10-31 03:01:06 +000058 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
59 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
60 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
61
Greg Claytone005f2c2010-11-06 01:53:30 +000062 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000063 if (log)
64 log->Printf ("%p Target::Target()", this);
65}
66
67//----------------------------------------------------------------------
68// Destructor
69//----------------------------------------------------------------------
70Target::~Target()
71{
Greg Claytone005f2c2010-11-06 01:53:30 +000072 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000073 if (log)
74 log->Printf ("%p Target::~Target()", this);
75 DeleteCurrentProcess ();
76}
77
78void
Caroline Tice7826c882010-10-26 03:11:13 +000079Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000080{
Greg Clayton3fed8b92010-10-08 00:21:05 +000081// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000082 if (description_level != lldb::eDescriptionLevelBrief)
83 {
84 s->Indent();
85 s->PutCString("Target\n");
86 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000087 m_images.Dump(s);
88 m_breakpoint_list.Dump(s);
89 m_internal_breakpoint_list.Dump(s);
90 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +000091 }
92 else
93 {
Greg Claytona66ba462010-10-30 04:51:46 +000094 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Tice7826c882010-10-26 03:11:13 +000095 }
Chris Lattner24943d22010-06-08 16:52:24 +000096}
97
98void
99Target::DeleteCurrentProcess ()
100{
101 if (m_process_sp.get())
102 {
Greg Clayton49480b12010-09-14 23:52:43 +0000103 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000104 if (m_process_sp->IsAlive())
105 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000106
107 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000108
109 // Do any cleanup of the target we need to do between process instances.
110 // NB It is better to do this before destroying the process in case the
111 // clean up needs some help from the process.
112 m_breakpoint_list.ClearAllBreakpointSites();
113 m_internal_breakpoint_list.ClearAllBreakpointSites();
114 m_process_sp.reset();
115 }
116}
117
118const lldb::ProcessSP &
119Target::CreateProcess (Listener &listener, const char *plugin_name)
120{
121 DeleteCurrentProcess ();
122 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
123 return m_process_sp;
124}
125
126const lldb::ProcessSP &
127Target::GetProcessSP () const
128{
129 return m_process_sp;
130}
131
132lldb::TargetSP
133Target::GetSP()
134{
Greg Clayton63094e02010-06-23 01:19:29 +0000135 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000136}
137
138BreakpointList &
139Target::GetBreakpointList(bool internal)
140{
141 if (internal)
142 return m_internal_breakpoint_list;
143 else
144 return m_breakpoint_list;
145}
146
147const BreakpointList &
148Target::GetBreakpointList(bool internal) const
149{
150 if (internal)
151 return m_internal_breakpoint_list;
152 else
153 return m_breakpoint_list;
154}
155
156BreakpointSP
157Target::GetBreakpointByID (break_id_t break_id)
158{
159 BreakpointSP bp_sp;
160
161 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
162 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
163 else
164 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
165
166 return bp_sp;
167}
168
169BreakpointSP
170Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
171{
172 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
173 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
174 return CreateBreakpoint (filter_sp, resolver_sp, internal);
175}
176
177
178BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000179Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000180{
Chris Lattner24943d22010-06-08 16:52:24 +0000181 Address so_addr;
182 // Attempt to resolve our load address if possible, though it is ok if
183 // it doesn't resolve to section/offset.
184
Greg Clayton33ed1702010-08-24 00:45:41 +0000185 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000186 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000187 if (!so_addr.IsValid())
188 {
189 // The address didn't resolve, so just set this as an absolute address
190 so_addr.SetOffset (addr);
191 }
192 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000193 return bp_sp;
194}
195
196BreakpointSP
197Target::CreateBreakpoint (Address &addr, bool internal)
198{
199 TargetSP target_sp = this->GetSP();
200 SearchFilterSP filter_sp(new SearchFilter (target_sp));
201 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
202 return CreateBreakpoint (filter_sp, resolver_sp, internal);
203}
204
205BreakpointSP
Greg Clayton12bec712010-06-28 21:30:43 +0000206Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000207{
Greg Clayton12bec712010-06-28 21:30:43 +0000208 BreakpointSP bp_sp;
209 if (func_name)
210 {
211 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
212 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
213 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
214 }
215 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000216}
217
218
219SearchFilterSP
220Target::GetSearchFilterForModule (const FileSpec *containingModule)
221{
222 SearchFilterSP filter_sp;
223 lldb::TargetSP target_sp = this->GetSP();
224 if (containingModule != NULL)
225 {
226 // TODO: We should look into sharing module based search filters
227 // across many breakpoints like we do for the simple target based one
228 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
229 }
230 else
231 {
232 if (m_search_filter_sp.get() == NULL)
233 m_search_filter_sp.reset (new SearchFilter (target_sp));
234 filter_sp = m_search_filter_sp;
235 }
236 return filter_sp;
237}
238
239BreakpointSP
240Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
241{
242 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
243 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
244
245 return CreateBreakpoint (filter_sp, resolver_sp, internal);
246}
247
248BreakpointSP
249Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
250{
251 BreakpointSP bp_sp;
252 if (filter_sp && resolver_sp)
253 {
254 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
255 resolver_sp->SetBreakpoint (bp_sp.get());
256
257 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000258 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000259 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000260 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000261
Greg Claytone005f2c2010-11-06 01:53:30 +0000262 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000263 if (log)
264 {
265 StreamString s;
266 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
267 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
268 }
269
Chris Lattner24943d22010-06-08 16:52:24 +0000270 bp_sp->ResolveBreakpoint();
271 }
Jim Inghamd1686902010-10-14 23:45:03 +0000272
273 if (!internal && bp_sp)
274 {
275 m_last_created_breakpoint = bp_sp;
276 }
277
Chris Lattner24943d22010-06-08 16:52:24 +0000278 return bp_sp;
279}
280
281void
282Target::RemoveAllBreakpoints (bool internal_also)
283{
Greg Claytone005f2c2010-11-06 01:53:30 +0000284 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000285 if (log)
286 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
287
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000288 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000289 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000290 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000291
292 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000293}
294
295void
296Target::DisableAllBreakpoints (bool internal_also)
297{
Greg Claytone005f2c2010-11-06 01:53:30 +0000298 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000299 if (log)
300 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
301
302 m_breakpoint_list.SetEnabledAll (false);
303 if (internal_also)
304 m_internal_breakpoint_list.SetEnabledAll (false);
305}
306
307void
308Target::EnableAllBreakpoints (bool internal_also)
309{
Greg Claytone005f2c2010-11-06 01:53:30 +0000310 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000311 if (log)
312 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
313
314 m_breakpoint_list.SetEnabledAll (true);
315 if (internal_also)
316 m_internal_breakpoint_list.SetEnabledAll (true);
317}
318
319bool
320Target::RemoveBreakpointByID (break_id_t break_id)
321{
Greg Claytone005f2c2010-11-06 01:53:30 +0000322 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000323 if (log)
324 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
325
326 if (DisableBreakpointByID (break_id))
327 {
328 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000329 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000330 else
Jim Inghamd1686902010-10-14 23:45:03 +0000331 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000332 if (m_last_created_breakpoint)
333 {
334 if (m_last_created_breakpoint->GetID() == break_id)
335 m_last_created_breakpoint.reset();
336 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000337 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000338 }
Chris Lattner24943d22010-06-08 16:52:24 +0000339 return true;
340 }
341 return false;
342}
343
344bool
345Target::DisableBreakpointByID (break_id_t break_id)
346{
Greg Claytone005f2c2010-11-06 01:53:30 +0000347 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000348 if (log)
349 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
350
351 BreakpointSP bp_sp;
352
353 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
354 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
355 else
356 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
357 if (bp_sp)
358 {
359 bp_sp->SetEnabled (false);
360 return true;
361 }
362 return false;
363}
364
365bool
366Target::EnableBreakpointByID (break_id_t break_id)
367{
Greg Claytone005f2c2010-11-06 01:53:30 +0000368 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000369 if (log)
370 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
371 __FUNCTION__,
372 break_id,
373 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
374
375 BreakpointSP bp_sp;
376
377 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
378 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
379 else
380 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
381
382 if (bp_sp)
383 {
384 bp_sp->SetEnabled (true);
385 return true;
386 }
387 return false;
388}
389
390ModuleSP
391Target::GetExecutableModule ()
392{
393 ModuleSP executable_sp;
394 if (m_images.GetSize() > 0)
395 executable_sp = m_images.GetModuleAtIndex(0);
396 return executable_sp;
397}
398
399void
400Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
401{
402 m_images.Clear();
403 m_scratch_ast_context_ap.reset();
404
405 if (executable_sp.get())
406 {
407 Timer scoped_timer (__PRETTY_FUNCTION__,
408 "Target::SetExecutableModule (executable = '%s/%s')",
409 executable_sp->GetFileSpec().GetDirectory().AsCString(),
410 executable_sp->GetFileSpec().GetFilename().AsCString());
411
412 m_images.Append(executable_sp); // The first image is our exectuable file
413
414 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000415 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
416 if (!m_arch_spec.IsValid())
417 m_arch_spec = exe_arch;
418
Chris Lattner24943d22010-06-08 16:52:24 +0000419 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000420 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
421 assert (executable_objfile);
422 // TODO: remote assertion above after verifying that it doesn't fire off
423 // after the platform changes. The platform is what should be selecting
424 // the right slice of an executable file, and it also should be the one
425 // to resolve any executables in their bundles.
426// if (executable_objfile == NULL)
427// {
428//
429// FileSpec bundle_executable(executable_sp->GetFileSpec());
430// if (Host::ResolveExecutableInBundle (bundle_executable))
431// {
432// ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable,
433// exe_arch));
434// SetExecutableModule (bundle_exe_module_sp, get_dependent_files);
435// if (bundle_exe_module_sp->GetObjectFile() != NULL)
436// executable_sp = bundle_exe_module_sp;
437// return;
438// }
439// }
Chris Lattner24943d22010-06-08 16:52:24 +0000440
441 if (executable_objfile)
442 {
443 executable_objfile->GetDependentModules(dependent_files);
444 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
445 {
446 ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i),
447 exe_arch));
448 if (image_module_sp.get())
449 {
450 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
451 ObjectFile *objfile = image_module_sp->GetObjectFile();
452 if (objfile)
453 objfile->GetDependentModules(dependent_files);
454 }
455 }
456 }
457
458 // Now see if we know the target triple, and if so, create our scratch AST context:
Greg Clayton395fc332011-02-15 21:59:32 +0000459 if (m_arch_spec.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000460 {
Greg Clayton395fc332011-02-15 21:59:32 +0000461 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch_spec.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000462 }
463 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000464
465 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000466}
467
468
Jim Ingham7508e732010-08-09 23:31:02 +0000469bool
470Target::SetArchitecture (const ArchSpec &arch_spec)
471{
472 if (m_arch_spec == arch_spec)
473 {
474 // If we're setting the architecture to our current architecture, we
475 // don't need to do anything.
476 return true;
477 }
478 else if (!m_arch_spec.IsValid())
479 {
480 // If we haven't got a valid arch spec, then we just need to set it.
481 m_arch_spec = arch_spec;
482 return true;
483 }
484 else
485 {
486 // If we have an executable file, try to reset the executable to the desired architecture
487 m_arch_spec = arch_spec;
488 ModuleSP executable_sp = GetExecutableModule ();
489 m_images.Clear();
490 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000491 // Need to do something about unsetting breakpoints.
492
493 if (executable_sp)
494 {
495 FileSpec exec_file_spec = executable_sp->GetFileSpec();
496 Error error = ModuleList::GetSharedModule(exec_file_spec,
497 arch_spec,
498 NULL,
499 NULL,
500 0,
501 executable_sp,
502 NULL,
503 NULL);
504
505 if (!error.Fail() && executable_sp)
506 {
507 SetExecutableModule (executable_sp, true);
508 return true;
509 }
510 else
511 {
512 return false;
513 }
514 }
515 else
516 {
517 return false;
518 }
519 }
520}
Chris Lattner24943d22010-06-08 16:52:24 +0000521
Chris Lattner24943d22010-06-08 16:52:24 +0000522void
523Target::ModuleAdded (ModuleSP &module_sp)
524{
525 // A module is being added to this target for the first time
526 ModuleList module_list;
527 module_list.Append(module_sp);
528 ModulesDidLoad (module_list);
529}
530
531void
532Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
533{
534 // A module is being added to this target for the first time
535 ModuleList module_list;
536 module_list.Append (old_module_sp);
537 ModulesDidUnload (module_list);
538 module_list.Clear ();
539 module_list.Append (new_module_sp);
540 ModulesDidLoad (module_list);
541}
542
543void
544Target::ModulesDidLoad (ModuleList &module_list)
545{
546 m_breakpoint_list.UpdateBreakpoints (module_list, true);
547 // TODO: make event data that packages up the module_list
548 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
549}
550
551void
552Target::ModulesDidUnload (ModuleList &module_list)
553{
554 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000555
556 // Remove the images from the target image list
557 m_images.Remove(module_list);
558
Chris Lattner24943d22010-06-08 16:52:24 +0000559 // TODO: make event data that packages up the module_list
560 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
561}
562
563size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000564Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
565{
566 const Section *section = addr.GetSection();
567 if (section && section->GetModule())
568 {
569 ObjectFile *objfile = section->GetModule()->GetObjectFile();
570 if (objfile)
571 {
572 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
573 addr.GetOffset(),
574 dst,
575 dst_len);
576 if (bytes_read > 0)
577 return bytes_read;
578 else
579 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
580 }
581 else
582 {
583 error.SetErrorString("address isn't from a object file");
584 }
585 }
586 else
587 {
588 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
589 }
590 return 0;
591}
592
593size_t
594Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000595{
Chris Lattner24943d22010-06-08 16:52:24 +0000596 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000597
Greg Clayton70436352010-06-30 23:03:03 +0000598 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
599
Greg Clayton26100dc2011-01-07 01:57:07 +0000600 size_t bytes_read = 0;
Greg Clayton70436352010-06-30 23:03:03 +0000601 Address resolved_addr(addr);
602 if (!resolved_addr.IsSectionOffset())
603 {
604 if (process_is_valid)
Chris Lattner24943d22010-06-08 16:52:24 +0000605 {
Greg Claytoneea26402010-09-14 23:36:40 +0000606 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000607 }
608 else
609 {
610 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
611 }
612 }
613
Greg Clayton26100dc2011-01-07 01:57:07 +0000614 if (prefer_file_cache)
615 {
616 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
617 if (bytes_read > 0)
618 return bytes_read;
619 }
Greg Clayton70436352010-06-30 23:03:03 +0000620
621 if (process_is_valid)
622 {
Greg Claytoneea26402010-09-14 23:36:40 +0000623 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000624 if (load_addr == LLDB_INVALID_ADDRESS)
625 {
626 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
627 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
628 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
629 resolved_addr.GetFileAddress());
630 else
631 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
632 }
633 else
634 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000635 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000636 if (bytes_read != dst_len)
637 {
638 if (error.Success())
639 {
640 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000641 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000642 else
Greg Clayton70436352010-06-30 23:03:03 +0000643 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 +0000644 }
645 }
Greg Clayton70436352010-06-30 23:03:03 +0000646 if (bytes_read)
647 return bytes_read;
648 // If the address is not section offset we have an address that
649 // doesn't resolve to any address in any currently loaded shared
650 // libaries and we failed to read memory so there isn't anything
651 // more we can do. If it is section offset, we might be able to
652 // read cached memory from the object file.
653 if (!resolved_addr.IsSectionOffset())
654 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000655 }
Chris Lattner24943d22010-06-08 16:52:24 +0000656 }
Greg Clayton70436352010-06-30 23:03:03 +0000657
Greg Clayton26100dc2011-01-07 01:57:07 +0000658 if (!prefer_file_cache)
Greg Clayton70436352010-06-30 23:03:03 +0000659 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000660 // If we didn't already try and read from the object file cache, then
661 // try it after failing to read from the process.
662 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000663 }
664 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000665}
666
667
668ModuleSP
669Target::GetSharedModule
670(
671 const FileSpec& file_spec,
672 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000673 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000674 const ConstString *object_name,
675 off_t object_offset,
676 Error *error_ptr
677)
678{
679 // Don't pass in the UUID so we can tell if we have a stale value in our list
680 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
681 bool did_create_module = false;
682 ModuleSP module_sp;
683
684 // If there are image search path entries, try to use them first to acquire a suitable image.
685
686 Error error;
687
688 if (m_image_search_paths.GetSize())
689 {
690 FileSpec transformed_spec;
691 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
692 {
693 transformed_spec.GetFilename() = file_spec.GetFilename();
694 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
695 }
696 }
697
698 // If a module hasn't been found yet, use the unmodified path.
699
700 if (!module_sp)
701 {
702 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
703 }
704
705 if (module_sp)
706 {
707 m_images.Append (module_sp);
708 if (did_create_module)
709 {
710 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
711 ModuleUpdated(old_module_sp, module_sp);
712 else
713 ModuleAdded(module_sp);
714 }
715 }
716 if (error_ptr)
717 *error_ptr = error;
718 return module_sp;
719}
720
721
722Target *
723Target::CalculateTarget ()
724{
725 return this;
726}
727
728Process *
729Target::CalculateProcess ()
730{
731 return NULL;
732}
733
734Thread *
735Target::CalculateThread ()
736{
737 return NULL;
738}
739
740StackFrame *
741Target::CalculateStackFrame ()
742{
743 return NULL;
744}
745
746void
Greg Claytona830adb2010-10-04 01:05:56 +0000747Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000748{
749 exe_ctx.target = this;
750 exe_ctx.process = NULL; // Do NOT fill in process...
751 exe_ctx.thread = NULL;
752 exe_ctx.frame = NULL;
753}
754
755PathMappingList &
756Target::GetImageSearchPathList ()
757{
758 return m_image_search_paths;
759}
760
761void
762Target::ImageSearchPathsChanged
763(
764 const PathMappingList &path_list,
765 void *baton
766)
767{
768 Target *target = (Target *)baton;
769 if (target->m_images.GetSize() > 1)
770 {
771 ModuleSP exe_module_sp (target->GetExecutableModule());
772 if (exe_module_sp)
773 {
774 target->m_images.Clear();
775 target->SetExecutableModule (exe_module_sp, true);
776 }
777 }
778}
779
780ClangASTContext *
781Target::GetScratchClangASTContext()
782{
783 return m_scratch_ast_context_ap.get();
784}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000785
Greg Clayton990de7b2010-11-18 23:32:35 +0000786void
Caroline Tice2a456812011-03-10 22:14:10 +0000787Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000788{
Greg Clayton990de7b2010-11-18 23:32:35 +0000789 UserSettingsControllerSP &usc = GetSettingsController();
790 usc.reset (new SettingsController);
791 UserSettingsController::InitializeSettingsController (usc,
792 SettingsController::global_settings_table,
793 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000794
795 // Now call SettingsInitialize() on each 'child' setting of Target
796 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000797}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000798
Greg Clayton990de7b2010-11-18 23:32:35 +0000799void
Caroline Tice2a456812011-03-10 22:14:10 +0000800Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000801{
Caroline Tice2a456812011-03-10 22:14:10 +0000802
803 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
804
805 Process::SettingsTerminate ();
806
807 // Now terminate Target Settings.
808
Greg Clayton990de7b2010-11-18 23:32:35 +0000809 UserSettingsControllerSP &usc = GetSettingsController();
810 UserSettingsController::FinalizeSettingsController (usc);
811 usc.reset();
812}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000813
Greg Clayton990de7b2010-11-18 23:32:35 +0000814UserSettingsControllerSP &
815Target::GetSettingsController ()
816{
817 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000818 return g_settings_controller;
819}
820
821ArchSpec
822Target::GetDefaultArchitecture ()
823{
Greg Clayton940b1032011-02-23 00:35:02 +0000824 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
825
826 if (settings_controller_sp)
827 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
828 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +0000829}
830
831void
Greg Clayton940b1032011-02-23 00:35:02 +0000832Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +0000833{
Greg Clayton940b1032011-02-23 00:35:02 +0000834 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
835
836 if (settings_controller_sp)
837 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000838}
839
Greg Claytona830adb2010-10-04 01:05:56 +0000840Target *
841Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
842{
843 // The target can either exist in the "process" of ExecutionContext, or in
844 // the "target_sp" member of SymbolContext. This accessor helper function
845 // will get the target from one of these locations.
846
847 Target *target = NULL;
848 if (sc_ptr != NULL)
849 target = sc_ptr->target_sp.get();
850 if (target == NULL)
851 {
852 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
853 target = &exe_ctx_ptr->process->GetTarget();
854 }
855 return target;
856}
857
858
Caroline Tice1ebef442010-09-27 00:30:10 +0000859void
860Target::UpdateInstanceName ()
861{
862 StreamString sstr;
863
864 ModuleSP module_sp = GetExecutableModule();
865 if (module_sp)
866 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000867 sstr.Printf ("%s_%s",
868 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton940b1032011-02-23 00:35:02 +0000869 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000870 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
871 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +0000872 }
873}
874
Sean Callanan77e93942010-10-29 00:29:03 +0000875const char *
876Target::GetExpressionPrefixContentsAsCString ()
877{
878 return m_expr_prefix_contents.c_str();
879}
880
Greg Clayton427f2902010-12-14 02:59:59 +0000881ExecutionResults
882Target::EvaluateExpression
883(
884 const char *expr_cstr,
885 StackFrame *frame,
886 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +0000887 bool keep_in_memory,
Greg Clayton427f2902010-12-14 02:59:59 +0000888 lldb::ValueObjectSP &result_valobj_sp
889)
890{
891 ExecutionResults execution_results = eExecutionSetupError;
892
893 result_valobj_sp.reset();
894
895 ExecutionContext exe_ctx;
896 if (frame)
897 {
898 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +0000899 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +0000900 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
901 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
902 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton427f2902010-12-14 02:59:59 +0000903 }
904 else if (m_process_sp)
905 {
906 m_process_sp->CalculateExecutionContext(exe_ctx);
907 }
908 else
909 {
910 CalculateExecutionContext(exe_ctx);
911 }
912
913 if (result_valobj_sp)
914 {
915 execution_results = eExecutionCompleted;
916 // We got a result from the frame variable expression path above...
917 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
918
919 lldb::ValueObjectSP const_valobj_sp;
920
921 // Check in case our value is already a constant value
922 if (result_valobj_sp->GetIsConstant())
923 {
924 const_valobj_sp = result_valobj_sp;
925 const_valobj_sp->SetName (persistent_variable_name);
926 }
927 else
928 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(),
929 persistent_variable_name);
930
Sean Callanan6a925532011-01-13 08:53:35 +0000931 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
932
Greg Clayton427f2902010-12-14 02:59:59 +0000933 result_valobj_sp = const_valobj_sp;
934
Sean Callanan6a925532011-01-13 08:53:35 +0000935 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
936 assert (clang_expr_variable_sp.get());
937
938 // Set flags and live data as appropriate
939
940 const Value &result_value = live_valobj_sp->GetValue();
941
942 switch (result_value.GetValueType())
943 {
944 case Value::eValueTypeHostAddress:
945 case Value::eValueTypeFileAddress:
946 // we don't do anything with these for now
947 break;
948 case Value::eValueTypeScalar:
949 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
950 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
951 break;
952 case Value::eValueTypeLoadAddress:
953 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
954 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
955 break;
956 }
Greg Clayton427f2902010-12-14 02:59:59 +0000957 }
958 else
959 {
960 // Make sure we aren't just trying to see the value of a persistent
961 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +0000962 lldb::ClangExpressionVariableSP persistent_var_sp;
963 // Only check for persistent variables the expression starts with a '$'
964 if (expr_cstr[0] == '$')
965 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
966
Greg Clayton427f2902010-12-14 02:59:59 +0000967 if (persistent_var_sp)
968 {
969 result_valobj_sp = persistent_var_sp->GetValueObject ();
970 execution_results = eExecutionCompleted;
971 }
972 else
973 {
974 const char *prefix = GetExpressionPrefixContentsAsCString();
975
976 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +0000977 unwind_on_error,
978 keep_in_memory,
Greg Clayton427f2902010-12-14 02:59:59 +0000979 expr_cstr,
980 prefix,
981 result_valobj_sp);
982 }
983 }
984 return execution_results;
985}
986
Jim Inghamd60d94a2011-03-11 03:53:59 +0000987lldb::user_id_t
988Target::AddStopHook (Target::StopHookSP &new_hook_sp)
989{
990 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
991 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
992 m_stop_hooks[new_uid] = new_hook_sp;
993 return new_uid;
994}
995
996bool
997Target::RemoveStopHookByID (lldb::user_id_t user_id)
998{
999 size_t num_removed;
1000 num_removed = m_stop_hooks.erase (user_id);
1001 if (num_removed == 0)
1002 return false;
1003 else
1004 return true;
1005}
1006
1007void
1008Target::RemoveAllStopHooks ()
1009{
1010 m_stop_hooks.clear();
1011}
1012
1013Target::StopHookSP
1014Target::GetStopHookByID (lldb::user_id_t user_id)
1015{
1016 StopHookSP found_hook;
1017
1018 StopHookCollection::iterator specified_hook_iter;
1019 specified_hook_iter = m_stop_hooks.find (user_id);
1020 if (specified_hook_iter != m_stop_hooks.end())
1021 found_hook = (*specified_hook_iter).second;
1022 return found_hook;
1023}
1024
1025bool
1026Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1027{
1028 StopHookCollection::iterator specified_hook_iter;
1029 specified_hook_iter = m_stop_hooks.find (user_id);
1030 if (specified_hook_iter == m_stop_hooks.end())
1031 return false;
1032
1033 (*specified_hook_iter).second->SetIsActive (active_state);
1034 return true;
1035}
1036
1037void
1038Target::SetAllStopHooksActiveState (bool active_state)
1039{
1040 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1041 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1042 {
1043 (*pos).second->SetIsActive (active_state);
1044 }
1045}
1046
1047void
1048Target::RunStopHooks ()
1049{
1050 if (!m_process_sp)
1051 return;
1052
1053 if (m_stop_hooks.empty())
1054 return;
1055
1056 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1057
1058 // If there aren't any active stop hooks, don't bother either:
1059 bool any_active_hooks = false;
1060 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1061 {
1062 if ((*pos).second->IsActive())
1063 {
1064 any_active_hooks = true;
1065 break;
1066 }
1067 }
1068 if (!any_active_hooks)
1069 return;
1070
1071 CommandReturnObject result;
1072
1073 std::vector<ExecutionContext> exc_ctx_with_reasons;
1074 std::vector<SymbolContext> sym_ctx_with_reasons;
1075
1076 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1077 size_t num_threads = cur_threadlist.GetSize();
1078 for (size_t i = 0; i < num_threads; i++)
1079 {
1080 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1081 if (cur_thread_sp->ThreadStoppedForAReason())
1082 {
1083 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1084 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1085 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1086 }
1087 }
1088
1089 // If no threads stopped for a reason, don't run the stop-hooks.
1090 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1091 if (num_exe_ctx == 0)
1092 return;
1093
1094 result.SetImmediateOutputFile (m_debugger.GetOutputFile().GetStream());
1095 result.SetImmediateErrorFile (m_debugger.GetErrorFile().GetStream());
1096
1097 bool keep_going = true;
1098 bool hooks_ran = false;
1099 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1100 {
1101 // result.Clear();
1102 StopHookSP cur_hook_sp = (*pos).second;
1103 if (!cur_hook_sp->IsActive())
1104 continue;
1105
1106 bool any_thread_matched = false;
1107 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1108 {
1109 if ((cur_hook_sp->GetSpecifier () == NULL
1110 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1111 && (cur_hook_sp->GetThreadSpecifier() == NULL
1112 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1113 {
1114 if (!hooks_ran)
1115 {
1116 result.AppendMessage("\n** Stop Hooks **\n");
1117 hooks_ran = true;
1118 }
1119 if (!any_thread_matched)
1120 {
1121 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1122 any_thread_matched = true;
1123 }
1124
1125 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
1126
1127 bool stop_on_continue = true;
1128 bool stop_on_error = true;
1129 bool echo_commands = false;
1130 bool print_results = true;
1131 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
1132 &exc_ctx_with_reasons[i],
1133 stop_on_continue,
1134 stop_on_error,
1135 echo_commands,
1136 print_results,
1137 result);
1138
1139 // If the command started the target going again, we should bag out of
1140 // running the stop hooks.
1141 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
1142 || (result.GetStatus() == eReturnStatusSuccessContinuingResult))
1143 {
1144 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1145 keep_going = false;
1146 }
1147 }
1148 }
1149 }
1150 if (hooks_ran)
1151 result.AppendMessage ("\n** End Stop Hooks **\n");
1152}
1153
1154//--------------------------------------------------------------
1155// class Target::StopHook
1156//--------------------------------------------------------------
1157
1158
1159Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1160 UserID (uid),
1161 m_target_sp (target_sp),
1162 m_active (true),
1163 m_commands (),
1164 m_specifier_sp (),
1165 m_thread_spec_ap(NULL)
1166{
1167}
1168
1169Target::StopHook::StopHook (const StopHook &rhs) :
1170 UserID (rhs.GetID()),
1171 m_target_sp (rhs.m_target_sp),
1172 m_commands (rhs.m_commands),
1173 m_specifier_sp (rhs.m_specifier_sp),
1174 m_active (rhs.m_active),
1175 m_thread_spec_ap (NULL)
1176{
1177 if (rhs.m_thread_spec_ap.get() != NULL)
1178 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1179}
1180
1181
1182Target::StopHook::~StopHook ()
1183{
1184}
1185
1186void
1187Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1188{
1189 m_thread_spec_ap.reset (specifier);
1190}
1191
1192
1193void
1194Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1195{
1196 int indent_level = s->GetIndentLevel();
1197
1198 s->SetIndentLevel(indent_level + 2);
1199
1200 s->Printf ("Hook: %d\n", GetID());
1201 if (m_active)
1202 s->Indent ("State: enabled\n");
1203 else
1204 s->Indent ("State: disabled\n");
1205
1206 if (m_specifier_sp)
1207 {
1208 s->Indent();
1209 s->PutCString ("Specifier:\n");
1210 s->SetIndentLevel (indent_level + 4);
1211 m_specifier_sp->GetDescription (s, level);
1212 s->SetIndentLevel (indent_level + 2);
1213 }
1214
1215 if (m_thread_spec_ap.get() != NULL)
1216 {
1217 StreamString tmp;
1218 s->Indent("Thread:\n");
1219 m_thread_spec_ap->GetDescription (&tmp, level);
1220 s->SetIndentLevel (indent_level + 4);
1221 s->Indent (tmp.GetData());
1222 s->PutCString ("\n");
1223 s->SetIndentLevel (indent_level + 2);
1224 }
1225
1226 s->Indent ("Commands: \n");
1227 s->SetIndentLevel (indent_level + 4);
1228 uint32_t num_commands = m_commands.GetSize();
1229 for (uint32_t i = 0; i < num_commands; i++)
1230 {
1231 s->Indent(m_commands.GetStringAtIndex(i));
1232 s->PutCString ("\n");
1233 }
1234 s->SetIndentLevel (indent_level);
1235}
1236
1237
Caroline Tice5bc8c972010-09-20 20:44:43 +00001238//--------------------------------------------------------------
1239// class Target::SettingsController
1240//--------------------------------------------------------------
1241
1242Target::SettingsController::SettingsController () :
1243 UserSettingsController ("target", Debugger::GetSettingsController()),
1244 m_default_architecture ()
1245{
1246 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1247 InstanceSettings::GetDefaultName().AsCString()));
1248}
1249
1250Target::SettingsController::~SettingsController ()
1251{
1252}
1253
1254lldb::InstanceSettingsSP
1255Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1256{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001257 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1258 false,
1259 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001260 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1261 return new_settings_sp;
1262}
1263
Caroline Tice5bc8c972010-09-20 20:44:43 +00001264
Greg Claytond284b662011-02-18 01:44:25 +00001265#define TSC_DEFAULT_ARCH "default-arch"
1266#define TSC_EXPR_PREFIX "expr-prefix"
1267#define TSC_EXEC_LEVEL "execution-level"
1268#define TSC_EXEC_MODE "execution-mode"
1269#define TSC_EXEC_OS_TYPE "execution-os-type"
1270
1271
1272static const ConstString &
1273GetSettingNameForDefaultArch ()
1274{
1275 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1276
1277 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001278}
1279
Greg Claytond284b662011-02-18 01:44:25 +00001280static const ConstString &
1281GetSettingNameForExpressionPrefix ()
1282{
1283 static ConstString g_const_string (TSC_EXPR_PREFIX);
1284 return g_const_string;
1285}
1286
1287static const ConstString &
1288GetSettingNameForExecutionLevel ()
1289{
1290 static ConstString g_const_string (TSC_EXEC_LEVEL);
1291 return g_const_string;
1292}
1293
1294static const ConstString &
1295GetSettingNameForExecutionMode ()
1296{
1297 static ConstString g_const_string (TSC_EXEC_MODE);
1298 return g_const_string;
1299}
1300
1301static const ConstString &
1302GetSettingNameForExecutionOSType ()
1303{
1304 static ConstString g_const_string (TSC_EXEC_OS_TYPE);
1305 return g_const_string;
1306}
1307
1308
Caroline Tice5bc8c972010-09-20 20:44:43 +00001309bool
1310Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1311 const char *index_value,
1312 const char *value,
1313 const SettingEntry &entry,
1314 const lldb::VarSetOperationType op,
1315 Error&err)
1316{
Greg Claytond284b662011-02-18 01:44:25 +00001317 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001318 {
Greg Clayton940b1032011-02-23 00:35:02 +00001319 m_default_architecture.SetTriple (value);
1320 if (!m_default_architecture.IsValid())
1321 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001322 }
1323 return true;
1324}
1325
1326
1327bool
1328Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1329 StringList &value,
1330 Error &err)
1331{
Greg Claytond284b662011-02-18 01:44:25 +00001332 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001333 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001334 // If the arch is invalid (the default), don't show a string for it
1335 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001336 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001337 return true;
1338 }
1339 else
1340 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1341
1342 return false;
1343}
1344
1345//--------------------------------------------------------------
1346// class TargetInstanceSettings
1347//--------------------------------------------------------------
1348
Greg Clayton638351a2010-12-04 00:10:17 +00001349TargetInstanceSettings::TargetInstanceSettings
1350(
1351 UserSettingsController &owner,
1352 bool live_instance,
1353 const char *name
1354) :
Greg Claytond284b662011-02-18 01:44:25 +00001355 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1356 m_expr_prefix_path (),
1357 m_expr_prefix_contents (),
1358 m_execution_level (eExecutionLevelAuto),
1359 m_execution_mode (eExecutionModeAuto),
1360 m_execution_os_type (eExecutionOSTypeAuto)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001361{
1362 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1363 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1364 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1365 // This is true for CreateInstanceName() too.
1366
1367 if (GetInstanceName () == InstanceSettings::InvalidName())
1368 {
1369 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1370 m_owner.RegisterInstanceSettings (this);
1371 }
1372
1373 if (live_instance)
1374 {
1375 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1376 CopyInstanceSettings (pending_settings,false);
1377 //m_owner.RemovePendingSettings (m_instance_name);
1378 }
1379}
1380
1381TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001382 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001383{
1384 if (m_instance_name != InstanceSettings::GetDefaultName())
1385 {
1386 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1387 CopyInstanceSettings (pending_settings,false);
1388 //m_owner.RemovePendingSettings (m_instance_name);
1389 }
1390}
1391
1392TargetInstanceSettings::~TargetInstanceSettings ()
1393{
1394}
1395
1396TargetInstanceSettings&
1397TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1398{
1399 if (this != &rhs)
1400 {
1401 }
1402
1403 return *this;
1404}
1405
Caroline Tice5bc8c972010-09-20 20:44:43 +00001406void
1407TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1408 const char *index_value,
1409 const char *value,
1410 const ConstString &instance_name,
1411 const SettingEntry &entry,
1412 lldb::VarSetOperationType op,
1413 Error &err,
1414 bool pending)
1415{
Greg Claytond284b662011-02-18 01:44:25 +00001416 int new_enum = -1;
1417
1418 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001419 {
1420 switch (op)
1421 {
1422 default:
1423 err.SetErrorToGenericError ();
1424 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1425 return;
1426 case lldb::eVarSetOperationAssign:
1427 {
1428 FileSpec file_spec(value, true);
1429
1430 if (!file_spec.Exists())
1431 {
1432 err.SetErrorToGenericError ();
1433 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1434 return;
1435 }
1436
Greg Clayton9f527042011-02-03 17:47:47 +00001437 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan77e93942010-10-29 00:29:03 +00001438
Greg Clayton9f527042011-02-03 17:47:47 +00001439 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan77e93942010-10-29 00:29:03 +00001440 {
1441 err.SetErrorToGenericError ();
Greg Clayton9f527042011-02-03 17:47:47 +00001442 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan77e93942010-10-29 00:29:03 +00001443 return;
1444 }
1445
1446 m_expr_prefix_path = value;
Greg Clayton9f527042011-02-03 17:47:47 +00001447 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan77e93942010-10-29 00:29:03 +00001448 }
1449 return;
1450 case lldb::eVarSetOperationAppend:
1451 err.SetErrorToGenericError ();
1452 err.SetErrorString ("Cannot append to a path.\n");
1453 return;
1454 case lldb::eVarSetOperationClear:
1455 m_expr_prefix_path.clear ();
1456 m_expr_prefix_contents.clear ();
1457 return;
1458 }
1459 }
Greg Claytond284b662011-02-18 01:44:25 +00001460 else if (var_name == GetSettingNameForExecutionLevel ())
1461 {
1462 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1463 if (err.Success())
1464 m_execution_level = (ExecutionLevel)new_enum;
1465 }
1466 else if (var_name == GetSettingNameForExecutionMode ())
1467 {
1468 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1469 if (err.Success())
1470 m_execution_mode = (ExecutionMode)new_enum;
1471 }
1472 else if (var_name == GetSettingNameForExecutionOSType ())
1473 {
1474 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1475 if (err.Success())
1476 m_execution_os_type = (ExecutionOSType)new_enum;
1477 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001478}
1479
1480void
Greg Claytond284b662011-02-18 01:44:25 +00001481TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001482{
Sean Callanan77e93942010-10-29 00:29:03 +00001483 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1484
1485 if (!new_settings_ptr)
1486 return;
1487
Greg Claytond284b662011-02-18 01:44:25 +00001488 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1489 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
1490 m_execution_level = new_settings_ptr->m_execution_level;
1491 m_execution_mode = new_settings_ptr->m_execution_mode;
1492 m_execution_os_type = new_settings_ptr->m_execution_os_type;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001493}
1494
Caroline Ticebcb5b452010-09-20 21:37:42 +00001495bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001496TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1497 const ConstString &var_name,
1498 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001499 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001500{
Greg Claytond284b662011-02-18 01:44:25 +00001501 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001502 {
1503 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1504 }
Greg Claytond284b662011-02-18 01:44:25 +00001505 else if (var_name == GetSettingNameForExecutionLevel ())
1506 {
1507 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_level));
1508 }
1509 else if (var_name == GetSettingNameForExecutionMode ())
1510 {
1511 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_mode));
1512 }
1513 else if (var_name == GetSettingNameForExecutionOSType ())
1514 {
1515 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_os_type));
1516 }
Sean Callanan77e93942010-10-29 00:29:03 +00001517 else
1518 {
1519 if (err)
1520 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1521 return false;
1522 }
1523
1524 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001525}
1526
1527const ConstString
1528TargetInstanceSettings::CreateInstanceName ()
1529{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001530 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001531 static int instance_count = 1;
1532
Caroline Tice5bc8c972010-09-20 20:44:43 +00001533 sstr.Printf ("target_%d", instance_count);
1534 ++instance_count;
1535
1536 const ConstString ret_val (sstr.GetData());
1537 return ret_val;
1538}
1539
1540//--------------------------------------------------
1541// Target::SettingsController Variable Tables
1542//--------------------------------------------------
1543
1544SettingEntry
1545Target::SettingsController::global_settings_table[] =
1546{
Greg Claytond284b662011-02-18 01:44:25 +00001547 // var-name var-type default enum init'd hidden help-text
1548 // ================= ================== =========== ==== ====== ====== =========================================================================
1549 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1550 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1551};
1552
1553static lldb::OptionEnumValueElement
1554g_execution_level_enums[] =
1555{
1556 { eExecutionLevelAuto , "auto" , "Automatically detect the execution level (user/kernel)." },
1557 { eExecutionLevelKernel , "kernel" , "Treat executables as kernel executables." },
1558 { eExecutionLevelUser , "user" , "Treat executables as user space executables." },
1559 { 0 , NULL , NULL }
1560};
1561
1562static lldb::OptionEnumValueElement
1563g_execution_mode_enums[] =
1564{
1565 { eExecutionModeAuto , "auto" , "Automatically detect the execution mode (static/dynamic)." },
1566 { eExecutionModeStatic , "static" , "All executables are static and addresses at the virtual addresses in the object files." },
1567 { eExecutionModeDynamic , "dynamic" , "Executables and shared libraries are dynamically loaded.." },
1568 { 0 , NULL , NULL }
1569};
1570
1571static lldb::OptionEnumValueElement
1572g_execution_os_enums[] =
1573{
1574 { eExecutionOSTypeAuto , "auto" , "Automatically detect the execution OS (none/halted/live)." },
1575 { eExecutionOSTypeNone , "none" , "There is no operating system available (no processes or threads)." },
1576 { eExecutionOSTypeHalted, "halted" , "There is an operating system, but it is halted when the debugger is halted. "
1577 "Processes and threads must be discovered by accessing symbols and reading "
1578 "memory." },
1579 { eExecutionOSTypeLive , "live" , "There is a live operating system with debug services that can be used to "
1580 "get processes, threads and theirs states." },
1581 { 0, NULL, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001582};
1583
1584SettingEntry
1585Target::SettingsController::instance_settings_table[] =
1586{
Greg Claytond284b662011-02-18 01:44:25 +00001587 // var-name var-type default enum-table init'd hidden help-text
1588 // ================= ================== =========== ========================= ====== ====== =========================================================================
1589 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL , false, false, "Path to a file containing expressions to be prepended to all expressions." },
1590 { TSC_EXEC_LEVEL , eSetVarTypeEnum , "auto" , g_execution_level_enums , false, false, "Sets the execution level for a target." },
1591 { TSC_EXEC_MODE , eSetVarTypeEnum , "auto" , g_execution_mode_enums , false, false, "Sets the execution mode for a target." },
1592 { TSC_EXEC_OS_TYPE , eSetVarTypeEnum , "auto" , g_execution_os_enums , false, false, "Sets the execution OS for a target." },
1593 { NULL , eSetVarTypeNone , NULL , NULL , false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001594};