blob: d08a0fe2bd114d5d798e0bd04e3e0edac16de294 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5920#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2421#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:2423#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5924#include "lldb/Core/Timer.h"
25#include "lldb/Core/ValueObject.h"
Greg Claytoneb0103f2011-04-07 22:46:3526#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:2427#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:5928#include "lldb/Interpreter/CommandInterpreter.h"
29#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2430#include "lldb/lldb-private-log.h"
31#include "lldb/Symbol/ObjectFile.h"
32#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5933#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:5934#include "lldb/Target/Thread.h"
35#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:2436
37using namespace lldb;
38using namespace lldb_private;
39
40//----------------------------------------------------------------------
41// Target constructor
42//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:5143Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
44 Broadcaster ("lldb.target"),
45 ExecutionContextScope (),
Greg Claytondbe54502010-11-19 03:46:0146 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2947 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:5148 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:2349 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:5150 m_arch (target_arch),
51 m_images (),
Greg Claytonf5e56de2010-09-14 23:36:4052 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2453 m_breakpoint_list (false),
54 m_internal_breakpoint_list (true),
Greg Clayton32e0a752011-03-30 18:16:5155 m_process_sp (),
56 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:2457 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5958 m_scratch_ast_context_ap (NULL),
Jim Ingham9575d842011-03-11 03:53:5959 m_persistent_variables (),
Greg Clayton32e0a752011-03-30 18:16:5160 m_stop_hooks (),
61 m_stop_hook_next_id (0)
Chris Lattner30fdc8d2010-06-08 16:52:2462{
Greg Claytoncfd1ace2010-10-31 03:01:0663 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
64 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
65 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
66
Greg Clayton2d4edfb2010-11-06 01:53:3067 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2468 if (log)
69 log->Printf ("%p Target::Target()", this);
70}
71
72//----------------------------------------------------------------------
73// Destructor
74//----------------------------------------------------------------------
75Target::~Target()
76{
Greg Clayton2d4edfb2010-11-06 01:53:3077 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2478 if (log)
79 log->Printf ("%p Target::~Target()", this);
80 DeleteCurrentProcess ();
81}
82
83void
Caroline Ticeceb6b132010-10-26 03:11:1384Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2485{
Greg Clayton89411422010-10-08 00:21:0586// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1387 if (description_level != lldb::eDescriptionLevelBrief)
88 {
89 s->Indent();
90 s->PutCString("Target\n");
91 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:3592 m_images.Dump(s);
93 m_breakpoint_list.Dump(s);
94 m_internal_breakpoint_list.Dump(s);
95 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:1396 }
97 else
98 {
Greg Clayton48381312010-10-30 04:51:4699 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Ticeceb6b132010-10-26 03:11:13100 }
Chris Lattner30fdc8d2010-06-08 16:52:24101}
102
103void
104Target::DeleteCurrentProcess ()
105{
106 if (m_process_sp.get())
107 {
Greg Clayton17f69202010-09-14 23:52:43108 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24109 if (m_process_sp->IsAlive())
110 m_process_sp->Destroy();
Jim Inghamd0a3e12b2011-02-16 17:54:55111
112 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24113
114 // Do any cleanup of the target we need to do between process instances.
115 // NB It is better to do this before destroying the process in case the
116 // clean up needs some help from the process.
117 m_breakpoint_list.ClearAllBreakpointSites();
118 m_internal_breakpoint_list.ClearAllBreakpointSites();
119 m_process_sp.reset();
120 }
121}
122
123const lldb::ProcessSP &
124Target::CreateProcess (Listener &listener, const char *plugin_name)
125{
126 DeleteCurrentProcess ();
127 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
128 return m_process_sp;
129}
130
131const lldb::ProcessSP &
132Target::GetProcessSP () const
133{
134 return m_process_sp;
135}
136
137lldb::TargetSP
138Target::GetSP()
139{
Greg Clayton66111032010-06-23 01:19:29140 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24141}
142
143BreakpointList &
144Target::GetBreakpointList(bool internal)
145{
146 if (internal)
147 return m_internal_breakpoint_list;
148 else
149 return m_breakpoint_list;
150}
151
152const BreakpointList &
153Target::GetBreakpointList(bool internal) const
154{
155 if (internal)
156 return m_internal_breakpoint_list;
157 else
158 return m_breakpoint_list;
159}
160
161BreakpointSP
162Target::GetBreakpointByID (break_id_t break_id)
163{
164 BreakpointSP bp_sp;
165
166 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
167 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
168 else
169 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
170
171 return bp_sp;
172}
173
174BreakpointSP
175Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
176{
177 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
178 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
179 return CreateBreakpoint (filter_sp, resolver_sp, internal);
180}
181
182
183BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41184Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24185{
Chris Lattner30fdc8d2010-06-08 16:52:24186 Address so_addr;
187 // Attempt to resolve our load address if possible, though it is ok if
188 // it doesn't resolve to section/offset.
189
Greg Clayton1b72fcb2010-08-24 00:45:41190 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40191 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41192 if (!so_addr.IsValid())
193 {
194 // The address didn't resolve, so just set this as an absolute address
195 so_addr.SetOffset (addr);
196 }
197 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24198 return bp_sp;
199}
200
201BreakpointSP
202Target::CreateBreakpoint (Address &addr, bool internal)
203{
204 TargetSP target_sp = this->GetSP();
205 SearchFilterSP filter_sp(new SearchFilter (target_sp));
206 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
207 return CreateBreakpoint (filter_sp, resolver_sp, internal);
208}
209
210BreakpointSP
Greg Clayton0c5cd902010-06-28 21:30:43211Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24212{
Greg Clayton0c5cd902010-06-28 21:30:43213 BreakpointSP bp_sp;
214 if (func_name)
215 {
216 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
217 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
218 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
219 }
220 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24221}
222
223
224SearchFilterSP
225Target::GetSearchFilterForModule (const FileSpec *containingModule)
226{
227 SearchFilterSP filter_sp;
228 lldb::TargetSP target_sp = this->GetSP();
229 if (containingModule != NULL)
230 {
231 // TODO: We should look into sharing module based search filters
232 // across many breakpoints like we do for the simple target based one
233 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
234 }
235 else
236 {
237 if (m_search_filter_sp.get() == NULL)
238 m_search_filter_sp.reset (new SearchFilter (target_sp));
239 filter_sp = m_search_filter_sp;
240 }
241 return filter_sp;
242}
243
244BreakpointSP
245Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
246{
247 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
248 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
249
250 return CreateBreakpoint (filter_sp, resolver_sp, internal);
251}
252
253BreakpointSP
254Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
255{
256 BreakpointSP bp_sp;
257 if (filter_sp && resolver_sp)
258 {
259 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
260 resolver_sp->SetBreakpoint (bp_sp.get());
261
262 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17263 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24264 else
Greg Clayton9fed0d82010-07-23 23:33:17265 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24266
Greg Clayton2d4edfb2010-11-06 01:53:30267 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24268 if (log)
269 {
270 StreamString s;
271 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
272 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
273 }
274
Chris Lattner30fdc8d2010-06-08 16:52:24275 bp_sp->ResolveBreakpoint();
276 }
Jim Ingham36f3b362010-10-14 23:45:03277
278 if (!internal && bp_sp)
279 {
280 m_last_created_breakpoint = bp_sp;
281 }
282
Chris Lattner30fdc8d2010-06-08 16:52:24283 return bp_sp;
284}
285
286void
287Target::RemoveAllBreakpoints (bool internal_also)
288{
Greg Clayton2d4edfb2010-11-06 01:53:30289 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24290 if (log)
291 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
292
Greg Clayton9fed0d82010-07-23 23:33:17293 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24294 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17295 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03296
297 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24298}
299
300void
301Target::DisableAllBreakpoints (bool internal_also)
302{
Greg Clayton2d4edfb2010-11-06 01:53:30303 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24304 if (log)
305 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
306
307 m_breakpoint_list.SetEnabledAll (false);
308 if (internal_also)
309 m_internal_breakpoint_list.SetEnabledAll (false);
310}
311
312void
313Target::EnableAllBreakpoints (bool internal_also)
314{
Greg Clayton2d4edfb2010-11-06 01:53:30315 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24316 if (log)
317 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
318
319 m_breakpoint_list.SetEnabledAll (true);
320 if (internal_also)
321 m_internal_breakpoint_list.SetEnabledAll (true);
322}
323
324bool
325Target::RemoveBreakpointByID (break_id_t break_id)
326{
Greg Clayton2d4edfb2010-11-06 01:53:30327 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24328 if (log)
329 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
330
331 if (DisableBreakpointByID (break_id))
332 {
333 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17334 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24335 else
Jim Ingham36f3b362010-10-14 23:45:03336 {
Greg Claytonaa1c5872011-01-24 23:35:47337 if (m_last_created_breakpoint)
338 {
339 if (m_last_created_breakpoint->GetID() == break_id)
340 m_last_created_breakpoint.reset();
341 }
Greg Clayton9fed0d82010-07-23 23:33:17342 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03343 }
Chris Lattner30fdc8d2010-06-08 16:52:24344 return true;
345 }
346 return false;
347}
348
349bool
350Target::DisableBreakpointByID (break_id_t break_id)
351{
Greg Clayton2d4edfb2010-11-06 01:53:30352 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24353 if (log)
354 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
355
356 BreakpointSP bp_sp;
357
358 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
359 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
360 else
361 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
362 if (bp_sp)
363 {
364 bp_sp->SetEnabled (false);
365 return true;
366 }
367 return false;
368}
369
370bool
371Target::EnableBreakpointByID (break_id_t break_id)
372{
Greg Clayton2d4edfb2010-11-06 01:53:30373 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24374 if (log)
375 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
376 __FUNCTION__,
377 break_id,
378 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
379
380 BreakpointSP bp_sp;
381
382 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
383 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
384 else
385 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
386
387 if (bp_sp)
388 {
389 bp_sp->SetEnabled (true);
390 return true;
391 }
392 return false;
393}
394
395ModuleSP
396Target::GetExecutableModule ()
397{
398 ModuleSP executable_sp;
399 if (m_images.GetSize() > 0)
400 executable_sp = m_images.GetModuleAtIndex(0);
401 return executable_sp;
402}
403
404void
405Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
406{
407 m_images.Clear();
408 m_scratch_ast_context_ap.reset();
409
410 if (executable_sp.get())
411 {
412 Timer scoped_timer (__PRETTY_FUNCTION__,
413 "Target::SetExecutableModule (executable = '%s/%s')",
414 executable_sp->GetFileSpec().GetDirectory().AsCString(),
415 executable_sp->GetFileSpec().GetFilename().AsCString());
416
417 m_images.Append(executable_sp); // The first image is our exectuable file
418
Jim Ingham5aee1622010-08-09 23:31:02419 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton32e0a752011-03-30 18:16:51420 if (!m_arch.IsValid())
421 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02422
Chris Lattner30fdc8d2010-06-08 16:52:24423 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15424 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24425
426 if (executable_objfile)
427 {
428 executable_objfile->GetDependentModules(dependent_files);
429 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
430 {
Greg Claytonded470d2011-03-19 01:12:21431 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
432 FileSpec platform_dependent_file_spec;
433 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55434 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21435 else
436 platform_dependent_file_spec = dependent_file_spec;
437
438 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton32e0a752011-03-30 18:16:51439 m_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24440 if (image_module_sp.get())
441 {
442 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
443 ObjectFile *objfile = image_module_sp->GetObjectFile();
444 if (objfile)
445 objfile->GetDependentModules(dependent_files);
446 }
447 }
448 }
449
450 // Now see if we know the target triple, and if so, create our scratch AST context:
Greg Clayton32e0a752011-03-30 18:16:51451 if (m_arch.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24452 {
Greg Clayton32e0a752011-03-30 18:16:51453 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:24454 }
455 }
Caroline Tice1559a462010-09-27 00:30:10456
457 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24458}
459
460
Jim Ingham5aee1622010-08-09 23:31:02461bool
462Target::SetArchitecture (const ArchSpec &arch_spec)
463{
Greg Clayton32e0a752011-03-30 18:16:51464 if (m_arch == arch_spec)
Jim Ingham5aee1622010-08-09 23:31:02465 {
466 // If we're setting the architecture to our current architecture, we
467 // don't need to do anything.
468 return true;
469 }
Greg Clayton32e0a752011-03-30 18:16:51470 else if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02471 {
472 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton32e0a752011-03-30 18:16:51473 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02474 return true;
475 }
476 else
477 {
478 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51479 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02480 ModuleSP executable_sp = GetExecutableModule ();
481 m_images.Clear();
482 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02483 // Need to do something about unsetting breakpoints.
484
485 if (executable_sp)
486 {
487 FileSpec exec_file_spec = executable_sp->GetFileSpec();
488 Error error = ModuleList::GetSharedModule(exec_file_spec,
489 arch_spec,
490 NULL,
491 NULL,
492 0,
493 executable_sp,
494 NULL,
495 NULL);
496
497 if (!error.Fail() && executable_sp)
498 {
499 SetExecutableModule (executable_sp, true);
500 return true;
501 }
502 else
503 {
504 return false;
505 }
506 }
507 else
508 {
509 return false;
510 }
511 }
512}
Chris Lattner30fdc8d2010-06-08 16:52:24513
Chris Lattner30fdc8d2010-06-08 16:52:24514void
515Target::ModuleAdded (ModuleSP &module_sp)
516{
517 // A module is being added to this target for the first time
518 ModuleList module_list;
519 module_list.Append(module_sp);
520 ModulesDidLoad (module_list);
521}
522
523void
524Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
525{
526 // A module is being added to this target for the first time
527 ModuleList module_list;
528 module_list.Append (old_module_sp);
529 ModulesDidUnload (module_list);
530 module_list.Clear ();
531 module_list.Append (new_module_sp);
532 ModulesDidLoad (module_list);
533}
534
535void
536Target::ModulesDidLoad (ModuleList &module_list)
537{
538 m_breakpoint_list.UpdateBreakpoints (module_list, true);
539 // TODO: make event data that packages up the module_list
540 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
541}
542
543void
544Target::ModulesDidUnload (ModuleList &module_list)
545{
546 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26547
548 // Remove the images from the target image list
549 m_images.Remove(module_list);
550
Chris Lattner30fdc8d2010-06-08 16:52:24551 // TODO: make event data that packages up the module_list
552 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
553}
554
555size_t
Greg Claytondb598232011-01-07 01:57:07556Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
557{
558 const Section *section = addr.GetSection();
559 if (section && section->GetModule())
560 {
561 ObjectFile *objfile = section->GetModule()->GetObjectFile();
562 if (objfile)
563 {
564 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
565 addr.GetOffset(),
566 dst,
567 dst_len);
568 if (bytes_read > 0)
569 return bytes_read;
570 else
571 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
572 }
573 else
574 {
575 error.SetErrorString("address isn't from a object file");
576 }
577 }
578 else
579 {
580 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
581 }
582 return 0;
583}
584
585size_t
586Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner30fdc8d2010-06-08 16:52:24587{
Chris Lattner30fdc8d2010-06-08 16:52:24588 error.Clear();
Greg Claytondb598232011-01-07 01:57:07589
Greg Claytondda4f7b2010-06-30 23:03:03590 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
591
Greg Claytondb598232011-01-07 01:57:07592 size_t bytes_read = 0;
Greg Clayton357132e2011-03-26 19:14:58593 Address resolved_addr;
594 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03595 {
596 if (process_is_valid)
Greg Claytonf5e56de2010-09-14 23:36:40597 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03598 else
Greg Claytondda4f7b2010-06-30 23:03:03599 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03600 }
Greg Clayton357132e2011-03-26 19:14:58601 if (!resolved_addr.IsValid())
602 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03603
Greg Claytondb598232011-01-07 01:57:07604 if (prefer_file_cache)
605 {
606 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
607 if (bytes_read > 0)
608 return bytes_read;
609 }
Greg Claytondda4f7b2010-06-30 23:03:03610
611 if (process_is_valid)
612 {
Greg Claytonf5e56de2010-09-14 23:36:40613 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Claytondda4f7b2010-06-30 23:03:03614 if (load_addr == LLDB_INVALID_ADDRESS)
615 {
616 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
617 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
618 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
619 resolved_addr.GetFileAddress());
620 else
621 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
622 }
623 else
624 {
Greg Claytondb598232011-01-07 01:57:07625 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24626 if (bytes_read != dst_len)
627 {
628 if (error.Success())
629 {
630 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03631 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24632 else
Greg Claytondda4f7b2010-06-30 23:03:03633 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24634 }
635 }
Greg Claytondda4f7b2010-06-30 23:03:03636 if (bytes_read)
637 return bytes_read;
638 // If the address is not section offset we have an address that
639 // doesn't resolve to any address in any currently loaded shared
640 // libaries and we failed to read memory so there isn't anything
641 // more we can do. If it is section offset, we might be able to
642 // read cached memory from the object file.
643 if (!resolved_addr.IsSectionOffset())
644 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24645 }
Chris Lattner30fdc8d2010-06-08 16:52:24646 }
Greg Claytondda4f7b2010-06-30 23:03:03647
Greg Claytondb598232011-01-07 01:57:07648 if (!prefer_file_cache)
Greg Claytondda4f7b2010-06-30 23:03:03649 {
Greg Claytondb598232011-01-07 01:57:07650 // If we didn't already try and read from the object file cache, then
651 // try it after failing to read from the process.
652 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03653 }
654 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24655}
656
657
658ModuleSP
659Target::GetSharedModule
660(
661 const FileSpec& file_spec,
662 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:10663 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:24664 const ConstString *object_name,
665 off_t object_offset,
666 Error *error_ptr
667)
668{
669 // Don't pass in the UUID so we can tell if we have a stale value in our list
670 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
671 bool did_create_module = false;
672 ModuleSP module_sp;
673
Chris Lattner30fdc8d2010-06-08 16:52:24674 Error error;
675
Greg Clayton32e0a752011-03-30 18:16:51676 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner30fdc8d2010-06-08 16:52:24677 if (m_image_search_paths.GetSize())
678 {
679 FileSpec transformed_spec;
680 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
681 {
682 transformed_spec.GetFilename() = file_spec.GetFilename();
683 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
684 }
685 }
686
Greg Clayton32e0a752011-03-30 18:16:51687 // The platform is responsible for finding and caching an appropriate
688 // module in the shared module cache.
689 if (m_platform_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24690 {
Greg Clayton32e0a752011-03-30 18:16:51691 FileSpec platform_file_spec;
692 error = m_platform_sp->GetSharedModule (file_spec,
693 arch,
694 uuid_ptr,
695 object_name,
696 object_offset,
697 module_sp,
698 &old_module_sp,
699 &did_create_module);
700 }
701 else
702 {
703 error.SetErrorString("no platform is currently set");
Chris Lattner30fdc8d2010-06-08 16:52:24704 }
705
Greg Clayton32e0a752011-03-30 18:16:51706 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner30fdc8d2010-06-08 16:52:24707 if (module_sp)
708 {
709 m_images.Append (module_sp);
710 if (did_create_module)
711 {
712 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
713 ModuleUpdated(old_module_sp, module_sp);
714 else
715 ModuleAdded(module_sp);
716 }
717 }
718 if (error_ptr)
719 *error_ptr = error;
720 return module_sp;
721}
722
723
724Target *
725Target::CalculateTarget ()
726{
727 return this;
728}
729
730Process *
731Target::CalculateProcess ()
732{
733 return NULL;
734}
735
736Thread *
737Target::CalculateThread ()
738{
739 return NULL;
740}
741
742StackFrame *
743Target::CalculateStackFrame ()
744{
745 return NULL;
746}
747
748void
Greg Clayton0603aa92010-10-04 01:05:56749Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24750{
751 exe_ctx.target = this;
752 exe_ctx.process = NULL; // Do NOT fill in process...
753 exe_ctx.thread = NULL;
754 exe_ctx.frame = NULL;
755}
756
757PathMappingList &
758Target::GetImageSearchPathList ()
759{
760 return m_image_search_paths;
761}
762
763void
764Target::ImageSearchPathsChanged
765(
766 const PathMappingList &path_list,
767 void *baton
768)
769{
770 Target *target = (Target *)baton;
771 if (target->m_images.GetSize() > 1)
772 {
773 ModuleSP exe_module_sp (target->GetExecutableModule());
774 if (exe_module_sp)
775 {
776 target->m_images.Clear();
777 target->SetExecutableModule (exe_module_sp, true);
778 }
779 }
780}
781
782ClangASTContext *
783Target::GetScratchClangASTContext()
784{
785 return m_scratch_ast_context_ap.get();
786}
Caroline Ticedaccaa92010-09-20 20:44:43787
Greg Clayton99d0faf2010-11-18 23:32:35788void
Caroline Tice20bd37f2011-03-10 22:14:10789Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43790{
Greg Clayton99d0faf2010-11-18 23:32:35791 UserSettingsControllerSP &usc = GetSettingsController();
792 usc.reset (new SettingsController);
793 UserSettingsController::InitializeSettingsController (usc,
794 SettingsController::global_settings_table,
795 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:10796
797 // Now call SettingsInitialize() on each 'child' setting of Target
798 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35799}
Caroline Ticedaccaa92010-09-20 20:44:43800
Greg Clayton99d0faf2010-11-18 23:32:35801void
Caroline Tice20bd37f2011-03-10 22:14:10802Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35803{
Caroline Tice20bd37f2011-03-10 22:14:10804
805 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
806
807 Process::SettingsTerminate ();
808
809 // Now terminate Target Settings.
810
Greg Clayton99d0faf2010-11-18 23:32:35811 UserSettingsControllerSP &usc = GetSettingsController();
812 UserSettingsController::FinalizeSettingsController (usc);
813 usc.reset();
814}
Caroline Ticedaccaa92010-09-20 20:44:43815
Greg Clayton99d0faf2010-11-18 23:32:35816UserSettingsControllerSP &
817Target::GetSettingsController ()
818{
819 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:43820 return g_settings_controller;
821}
822
823ArchSpec
824Target::GetDefaultArchitecture ()
825{
Greg Clayton64195a22011-02-23 00:35:02826 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
827
828 if (settings_controller_sp)
829 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
830 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43831}
832
833void
Greg Clayton64195a22011-02-23 00:35:02834Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:43835{
Greg Clayton64195a22011-02-23 00:35:02836 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
837
838 if (settings_controller_sp)
839 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:43840}
841
Greg Clayton0603aa92010-10-04 01:05:56842Target *
843Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
844{
845 // The target can either exist in the "process" of ExecutionContext, or in
846 // the "target_sp" member of SymbolContext. This accessor helper function
847 // will get the target from one of these locations.
848
849 Target *target = NULL;
850 if (sc_ptr != NULL)
851 target = sc_ptr->target_sp.get();
852 if (target == NULL)
853 {
854 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
855 target = &exe_ctx_ptr->process->GetTarget();
856 }
857 return target;
858}
859
860
Caroline Tice1559a462010-09-27 00:30:10861void
862Target::UpdateInstanceName ()
863{
864 StreamString sstr;
865
866 ModuleSP module_sp = GetExecutableModule();
867 if (module_sp)
868 {
Greg Clayton307de252010-10-27 02:06:37869 sstr.Printf ("%s_%s",
870 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton64195a22011-02-23 00:35:02871 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytondbe54502010-11-19 03:46:01872 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
873 sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:10874 }
875}
876
Sean Callanan322f5292010-10-29 00:29:03877const char *
878Target::GetExpressionPrefixContentsAsCString ()
879{
880 return m_expr_prefix_contents.c_str();
881}
882
Greg Clayton8b2fe6d2010-12-14 02:59:59883ExecutionResults
884Target::EvaluateExpression
885(
886 const char *expr_cstr,
887 StackFrame *frame,
888 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:35889 bool keep_in_memory,
Jim Ingham78a685a2011-04-16 00:01:13890 bool fetch_dynamic_value,
Greg Clayton8b2fe6d2010-12-14 02:59:59891 lldb::ValueObjectSP &result_valobj_sp
892)
893{
894 ExecutionResults execution_results = eExecutionSetupError;
895
896 result_valobj_sp.reset();
897
898 ExecutionContext exe_ctx;
899 if (frame)
900 {
901 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:08902 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:18903 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
904 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
905 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton8b2fe6d2010-12-14 02:59:59906 }
907 else if (m_process_sp)
908 {
909 m_process_sp->CalculateExecutionContext(exe_ctx);
910 }
911 else
912 {
913 CalculateExecutionContext(exe_ctx);
914 }
915
916 if (result_valobj_sp)
917 {
918 execution_results = eExecutionCompleted;
919 // We got a result from the frame variable expression path above...
920 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
921
922 lldb::ValueObjectSP const_valobj_sp;
923
924 // Check in case our value is already a constant value
925 if (result_valobj_sp->GetIsConstant())
926 {
927 const_valobj_sp = result_valobj_sp;
928 const_valobj_sp->SetName (persistent_variable_name);
929 }
930 else
Jim Ingham78a685a2011-04-16 00:01:13931 {
932 if (fetch_dynamic_value)
933 {
934 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(true, result_valobj_sp);
935 if (dynamic_sp)
936 result_valobj_sp = dynamic_sp;
937 }
938
Jim Ingham6035b672011-03-31 00:19:25939 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:13940 }
Greg Clayton8b2fe6d2010-12-14 02:59:59941
Sean Callanan92adcac2011-01-13 08:53:35942 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
943
Greg Clayton8b2fe6d2010-12-14 02:59:59944 result_valobj_sp = const_valobj_sp;
945
Sean Callanan92adcac2011-01-13 08:53:35946 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
947 assert (clang_expr_variable_sp.get());
948
949 // Set flags and live data as appropriate
950
951 const Value &result_value = live_valobj_sp->GetValue();
952
953 switch (result_value.GetValueType())
954 {
955 case Value::eValueTypeHostAddress:
956 case Value::eValueTypeFileAddress:
957 // we don't do anything with these for now
958 break;
959 case Value::eValueTypeScalar:
960 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
961 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
962 break;
963 case Value::eValueTypeLoadAddress:
964 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
965 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
966 break;
967 }
Greg Clayton8b2fe6d2010-12-14 02:59:59968 }
969 else
970 {
971 // Make sure we aren't just trying to see the value of a persistent
972 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:35973 lldb::ClangExpressionVariableSP persistent_var_sp;
974 // Only check for persistent variables the expression starts with a '$'
975 if (expr_cstr[0] == '$')
976 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
977
Greg Clayton8b2fe6d2010-12-14 02:59:59978 if (persistent_var_sp)
979 {
980 result_valobj_sp = persistent_var_sp->GetValueObject ();
981 execution_results = eExecutionCompleted;
982 }
983 else
984 {
985 const char *prefix = GetExpressionPrefixContentsAsCString();
986
987 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:35988 unwind_on_error,
989 keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59990 expr_cstr,
991 prefix,
992 result_valobj_sp);
993 }
994 }
995 return execution_results;
996}
997
Jim Ingham9575d842011-03-11 03:53:59998lldb::user_id_t
999Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1000{
1001 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1002 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1003 m_stop_hooks[new_uid] = new_hook_sp;
1004 return new_uid;
1005}
1006
1007bool
1008Target::RemoveStopHookByID (lldb::user_id_t user_id)
1009{
1010 size_t num_removed;
1011 num_removed = m_stop_hooks.erase (user_id);
1012 if (num_removed == 0)
1013 return false;
1014 else
1015 return true;
1016}
1017
1018void
1019Target::RemoveAllStopHooks ()
1020{
1021 m_stop_hooks.clear();
1022}
1023
1024Target::StopHookSP
1025Target::GetStopHookByID (lldb::user_id_t user_id)
1026{
1027 StopHookSP found_hook;
1028
1029 StopHookCollection::iterator specified_hook_iter;
1030 specified_hook_iter = m_stop_hooks.find (user_id);
1031 if (specified_hook_iter != m_stop_hooks.end())
1032 found_hook = (*specified_hook_iter).second;
1033 return found_hook;
1034}
1035
1036bool
1037Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1038{
1039 StopHookCollection::iterator specified_hook_iter;
1040 specified_hook_iter = m_stop_hooks.find (user_id);
1041 if (specified_hook_iter == m_stop_hooks.end())
1042 return false;
1043
1044 (*specified_hook_iter).second->SetIsActive (active_state);
1045 return true;
1046}
1047
1048void
1049Target::SetAllStopHooksActiveState (bool active_state)
1050{
1051 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1052 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1053 {
1054 (*pos).second->SetIsActive (active_state);
1055 }
1056}
1057
1058void
1059Target::RunStopHooks ()
1060{
1061 if (!m_process_sp)
1062 return;
1063
1064 if (m_stop_hooks.empty())
1065 return;
1066
1067 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1068
1069 // If there aren't any active stop hooks, don't bother either:
1070 bool any_active_hooks = false;
1071 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1072 {
1073 if ((*pos).second->IsActive())
1074 {
1075 any_active_hooks = true;
1076 break;
1077 }
1078 }
1079 if (!any_active_hooks)
1080 return;
1081
1082 CommandReturnObject result;
1083
1084 std::vector<ExecutionContext> exc_ctx_with_reasons;
1085 std::vector<SymbolContext> sym_ctx_with_reasons;
1086
1087 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1088 size_t num_threads = cur_threadlist.GetSize();
1089 for (size_t i = 0; i < num_threads; i++)
1090 {
1091 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1092 if (cur_thread_sp->ThreadStoppedForAReason())
1093 {
1094 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1095 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1096 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1097 }
1098 }
1099
1100 // If no threads stopped for a reason, don't run the stop-hooks.
1101 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1102 if (num_exe_ctx == 0)
1103 return;
1104
1105 result.SetImmediateOutputFile (m_debugger.GetOutputFile().GetStream());
1106 result.SetImmediateErrorFile (m_debugger.GetErrorFile().GetStream());
1107
1108 bool keep_going = true;
1109 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271110 bool print_hook_header;
1111 bool print_thread_header;
1112
1113 if (num_exe_ctx == 1)
1114 print_thread_header = false;
1115 else
1116 print_thread_header = true;
1117
1118 if (m_stop_hooks.size() == 1)
1119 print_hook_header = false;
1120 else
1121 print_hook_header = true;
1122
Jim Ingham9575d842011-03-11 03:53:591123 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1124 {
1125 // result.Clear();
1126 StopHookSP cur_hook_sp = (*pos).second;
1127 if (!cur_hook_sp->IsActive())
1128 continue;
1129
1130 bool any_thread_matched = false;
1131 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1132 {
1133 if ((cur_hook_sp->GetSpecifier () == NULL
1134 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1135 && (cur_hook_sp->GetThreadSpecifier() == NULL
1136 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1137 {
1138 if (!hooks_ran)
1139 {
Jim Ingham381e25b2011-03-22 01:47:271140 result.AppendMessage("\n** Stop Hooks **");
Jim Ingham9575d842011-03-11 03:53:591141 hooks_ran = true;
1142 }
Jim Ingham381e25b2011-03-22 01:47:271143 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591144 {
1145 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1146 any_thread_matched = true;
1147 }
1148
Jim Ingham381e25b2011-03-22 01:47:271149 if (print_thread_header)
1150 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591151
1152 bool stop_on_continue = true;
1153 bool stop_on_error = true;
1154 bool echo_commands = false;
1155 bool print_results = true;
1156 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:511157 &exc_ctx_with_reasons[i],
1158 stop_on_continue,
1159 stop_on_error,
1160 echo_commands,
1161 print_results,
1162 result);
Jim Ingham9575d842011-03-11 03:53:591163
1164 // If the command started the target going again, we should bag out of
1165 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:511166 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1167 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:591168 {
1169 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1170 keep_going = false;
1171 }
1172 }
1173 }
1174 }
1175 if (hooks_ran)
1176 result.AppendMessage ("\n** End Stop Hooks **\n");
1177}
1178
1179//--------------------------------------------------------------
1180// class Target::StopHook
1181//--------------------------------------------------------------
1182
1183
1184Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1185 UserID (uid),
1186 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:591187 m_commands (),
1188 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:401189 m_thread_spec_ap(NULL),
1190 m_active (true)
Jim Ingham9575d842011-03-11 03:53:591191{
1192}
1193
1194Target::StopHook::StopHook (const StopHook &rhs) :
1195 UserID (rhs.GetID()),
1196 m_target_sp (rhs.m_target_sp),
1197 m_commands (rhs.m_commands),
1198 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:401199 m_thread_spec_ap (NULL),
1200 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:591201{
1202 if (rhs.m_thread_spec_ap.get() != NULL)
1203 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1204}
1205
1206
1207Target::StopHook::~StopHook ()
1208{
1209}
1210
1211void
1212Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1213{
1214 m_thread_spec_ap.reset (specifier);
1215}
1216
1217
1218void
1219Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1220{
1221 int indent_level = s->GetIndentLevel();
1222
1223 s->SetIndentLevel(indent_level + 2);
1224
1225 s->Printf ("Hook: %d\n", GetID());
1226 if (m_active)
1227 s->Indent ("State: enabled\n");
1228 else
1229 s->Indent ("State: disabled\n");
1230
1231 if (m_specifier_sp)
1232 {
1233 s->Indent();
1234 s->PutCString ("Specifier:\n");
1235 s->SetIndentLevel (indent_level + 4);
1236 m_specifier_sp->GetDescription (s, level);
1237 s->SetIndentLevel (indent_level + 2);
1238 }
1239
1240 if (m_thread_spec_ap.get() != NULL)
1241 {
1242 StreamString tmp;
1243 s->Indent("Thread:\n");
1244 m_thread_spec_ap->GetDescription (&tmp, level);
1245 s->SetIndentLevel (indent_level + 4);
1246 s->Indent (tmp.GetData());
1247 s->PutCString ("\n");
1248 s->SetIndentLevel (indent_level + 2);
1249 }
1250
1251 s->Indent ("Commands: \n");
1252 s->SetIndentLevel (indent_level + 4);
1253 uint32_t num_commands = m_commands.GetSize();
1254 for (uint32_t i = 0; i < num_commands; i++)
1255 {
1256 s->Indent(m_commands.GetStringAtIndex(i));
1257 s->PutCString ("\n");
1258 }
1259 s->SetIndentLevel (indent_level);
1260}
1261
1262
Caroline Ticedaccaa92010-09-20 20:44:431263//--------------------------------------------------------------
1264// class Target::SettingsController
1265//--------------------------------------------------------------
1266
1267Target::SettingsController::SettingsController () :
1268 UserSettingsController ("target", Debugger::GetSettingsController()),
1269 m_default_architecture ()
1270{
1271 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1272 InstanceSettings::GetDefaultName().AsCString()));
1273}
1274
1275Target::SettingsController::~SettingsController ()
1276{
1277}
1278
1279lldb::InstanceSettingsSP
1280Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1281{
Greg Claytondbe54502010-11-19 03:46:011282 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1283 false,
1284 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431285 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1286 return new_settings_sp;
1287}
1288
Caroline Ticedaccaa92010-09-20 20:44:431289
Jim Ingham78a685a2011-04-16 00:01:131290#define TSC_DEFAULT_ARCH "default-arch"
1291#define TSC_EXPR_PREFIX "expr-prefix"
1292#define TSC_EXEC_LEVEL "execution-level"
1293#define TSC_EXEC_MODE "execution-mode"
1294#define TSC_EXEC_OS_TYPE "execution-os-type"
1295#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Claytonbfe5f3b2011-02-18 01:44:251296
1297
1298static const ConstString &
1299GetSettingNameForDefaultArch ()
1300{
1301 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1302
1303 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431304}
1305
Greg Claytonbfe5f3b2011-02-18 01:44:251306static const ConstString &
1307GetSettingNameForExpressionPrefix ()
1308{
1309 static ConstString g_const_string (TSC_EXPR_PREFIX);
1310 return g_const_string;
1311}
1312
1313static const ConstString &
1314GetSettingNameForExecutionLevel ()
1315{
1316 static ConstString g_const_string (TSC_EXEC_LEVEL);
1317 return g_const_string;
1318}
1319
1320static const ConstString &
1321GetSettingNameForExecutionMode ()
1322{
1323 static ConstString g_const_string (TSC_EXEC_MODE);
1324 return g_const_string;
1325}
1326
1327static const ConstString &
1328GetSettingNameForExecutionOSType ()
1329{
1330 static ConstString g_const_string (TSC_EXEC_OS_TYPE);
1331 return g_const_string;
1332}
1333
Jim Ingham78a685a2011-04-16 00:01:131334static const ConstString &
1335GetSettingNameForPreferDynamicValue ()
1336{
1337 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1338 return g_const_string;
1339}
1340
Greg Claytonbfe5f3b2011-02-18 01:44:251341
Caroline Ticedaccaa92010-09-20 20:44:431342bool
1343Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1344 const char *index_value,
1345 const char *value,
1346 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541347 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431348 Error&err)
1349{
Greg Claytonbfe5f3b2011-02-18 01:44:251350 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431351 {
Greg Claytoneb0103f2011-04-07 22:46:351352 m_default_architecture.SetTriple (value, NULL);
Greg Clayton64195a22011-02-23 00:35:021353 if (!m_default_architecture.IsValid())
1354 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431355 }
1356 return true;
1357}
1358
1359
1360bool
1361Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1362 StringList &value,
1363 Error &err)
1364{
Greg Claytonbfe5f3b2011-02-18 01:44:251365 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431366 {
Greg Clayton307de252010-10-27 02:06:371367 // If the arch is invalid (the default), don't show a string for it
1368 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021369 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431370 return true;
1371 }
1372 else
1373 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1374
1375 return false;
1376}
1377
1378//--------------------------------------------------------------
1379// class TargetInstanceSettings
1380//--------------------------------------------------------------
1381
Greg Clayton85851dd2010-12-04 00:10:171382TargetInstanceSettings::TargetInstanceSettings
1383(
1384 UserSettingsController &owner,
1385 bool live_instance,
1386 const char *name
1387) :
Greg Claytonbfe5f3b2011-02-18 01:44:251388 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1389 m_expr_prefix_path (),
Jim Ingham78a685a2011-04-16 00:01:131390 m_expr_prefix_contents (),
1391 m_prefer_dynamic_value (true)
Caroline Ticedaccaa92010-09-20 20:44:431392{
1393 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1394 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1395 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1396 // This is true for CreateInstanceName() too.
1397
1398 if (GetInstanceName () == InstanceSettings::InvalidName())
1399 {
1400 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1401 m_owner.RegisterInstanceSettings (this);
1402 }
1403
1404 if (live_instance)
1405 {
1406 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1407 CopyInstanceSettings (pending_settings,false);
1408 //m_owner.RemovePendingSettings (m_instance_name);
1409 }
1410}
1411
1412TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytondbe54502010-11-19 03:46:011413 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Ticedaccaa92010-09-20 20:44:431414{
1415 if (m_instance_name != InstanceSettings::GetDefaultName())
1416 {
1417 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1418 CopyInstanceSettings (pending_settings,false);
1419 //m_owner.RemovePendingSettings (m_instance_name);
1420 }
1421}
1422
1423TargetInstanceSettings::~TargetInstanceSettings ()
1424{
1425}
1426
1427TargetInstanceSettings&
1428TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1429{
1430 if (this != &rhs)
1431 {
1432 }
1433
1434 return *this;
1435}
1436
Caroline Ticedaccaa92010-09-20 20:44:431437void
1438TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1439 const char *index_value,
1440 const char *value,
1441 const ConstString &instance_name,
1442 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541443 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431444 Error &err,
1445 bool pending)
1446{
Greg Claytonbfe5f3b2011-02-18 01:44:251447 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031448 {
1449 switch (op)
1450 {
1451 default:
1452 err.SetErrorToGenericError ();
1453 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1454 return;
Greg Claytone0d378b2011-03-24 21:19:541455 case eVarSetOperationAssign:
Sean Callanan322f5292010-10-29 00:29:031456 {
1457 FileSpec file_spec(value, true);
1458
1459 if (!file_spec.Exists())
1460 {
1461 err.SetErrorToGenericError ();
1462 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1463 return;
1464 }
1465
Greg Claytonc3f381b2011-02-03 17:47:471466 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan322f5292010-10-29 00:29:031467
Greg Claytonc3f381b2011-02-03 17:47:471468 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan322f5292010-10-29 00:29:031469 {
1470 err.SetErrorToGenericError ();
Greg Claytonc3f381b2011-02-03 17:47:471471 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan322f5292010-10-29 00:29:031472 return;
1473 }
1474
1475 m_expr_prefix_path = value;
Greg Claytonc3f381b2011-02-03 17:47:471476 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan322f5292010-10-29 00:29:031477 }
1478 return;
Greg Claytone0d378b2011-03-24 21:19:541479 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:031480 err.SetErrorToGenericError ();
1481 err.SetErrorString ("Cannot append to a path.\n");
1482 return;
Greg Claytone0d378b2011-03-24 21:19:541483 case eVarSetOperationClear:
Sean Callanan322f5292010-10-29 00:29:031484 m_expr_prefix_path.clear ();
1485 m_expr_prefix_contents.clear ();
1486 return;
1487 }
1488 }
Jim Ingham78a685a2011-04-16 00:01:131489 else if (var_name == GetSettingNameForPreferDynamicValue())
1490 {
1491 switch (op)
1492 {
1493 default:
1494 err.SetErrorToGenericError ();
1495 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1496 return;
1497 case eVarSetOperationAssign:
1498 {
1499 bool success;
1500 bool result = Args::StringToBoolean(value, false, &success);
1501
1502 if (success)
1503 {
1504 m_prefer_dynamic_value = result;
1505 }
1506 else
1507 {
1508 err.SetErrorStringWithFormat ("Bad value \"%s\" for %s, should be Boolean.",
1509 value,
1510 GetSettingNameForPreferDynamicValue().AsCString());
1511 }
1512 return;
1513 }
1514 case eVarSetOperationClear:
1515 m_prefer_dynamic_value = true;
1516 case eVarSetOperationAppend:
1517 err.SetErrorToGenericError ();
1518 err.SetErrorString ("Cannot append to a bool.\n");
1519 return;
1520 }
1521 }
Caroline Ticedaccaa92010-09-20 20:44:431522}
1523
1524void
Greg Claytonbfe5f3b2011-02-18 01:44:251525TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431526{
Sean Callanan322f5292010-10-29 00:29:031527 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1528
1529 if (!new_settings_ptr)
1530 return;
1531
Greg Claytonbfe5f3b2011-02-18 01:44:251532 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1533 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
Jim Ingham78a685a2011-04-16 00:01:131534 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
Caroline Ticedaccaa92010-09-20 20:44:431535}
1536
Caroline Tice12cecd72010-09-20 21:37:421537bool
Caroline Ticedaccaa92010-09-20 20:44:431538TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1539 const ConstString &var_name,
1540 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421541 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431542{
Greg Claytonbfe5f3b2011-02-18 01:44:251543 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031544 {
1545 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1546 }
Jim Ingham78a685a2011-04-16 00:01:131547 else if (var_name == GetSettingNameForPreferDynamicValue())
1548 {
1549 if (m_prefer_dynamic_value)
1550 value.AppendString ("true");
1551 else
1552 value.AppendString ("false");
1553 }
Sean Callanan322f5292010-10-29 00:29:031554 else
1555 {
1556 if (err)
1557 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1558 return false;
1559 }
1560
1561 return true;
Caroline Ticedaccaa92010-09-20 20:44:431562}
1563
1564const ConstString
1565TargetInstanceSettings::CreateInstanceName ()
1566{
Caroline Ticedaccaa92010-09-20 20:44:431567 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:101568 static int instance_count = 1;
1569
Caroline Ticedaccaa92010-09-20 20:44:431570 sstr.Printf ("target_%d", instance_count);
1571 ++instance_count;
1572
1573 const ConstString ret_val (sstr.GetData());
1574 return ret_val;
1575}
1576
1577//--------------------------------------------------
1578// Target::SettingsController Variable Tables
1579//--------------------------------------------------
1580
1581SettingEntry
1582Target::SettingsController::global_settings_table[] =
1583{
Greg Claytonbfe5f3b2011-02-18 01:44:251584 // var-name var-type default enum init'd hidden help-text
1585 // ================= ================== =========== ==== ====== ====== =========================================================================
1586 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1587 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1588};
1589
Caroline Ticedaccaa92010-09-20 20:44:431590SettingEntry
1591Target::SettingsController::instance_settings_table[] =
1592{
Greg Claytone0d378b2011-03-24 21:19:541593 // var-name var-type default enum init'd hidden help-text
1594 // ================= ================== =========== ==== ====== ====== =========================================================================
1595 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
Jim Ingham78a685a2011-04-16 00:01:131596 { TSC_PREFER_DYNAMIC, eSetVarTypeBoolean ,"true" , NULL, false, false, "Should printed values be shown as their dynamic value." },
Greg Claytone0d378b2011-03-24 21:19:541597 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431598};