blob: abda39f44a068d3cfc555724098f94a1813c88c2 [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"
Greg Claytonf15996e2011-04-07 22:46:35 +000026#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000028#include "lldb/Interpreter/CommandInterpreter.h"
29#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/lldb-private-log.h"
31#include "lldb/Symbol/ObjectFile.h"
32#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000033#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000034#include "lldb/Target/Thread.h"
35#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036
37using namespace lldb;
38using namespace lldb_private;
39
40//----------------------------------------------------------------------
41// Target constructor
42//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000043Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
44 Broadcaster ("lldb.target"),
45 ExecutionContextScope (),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000046 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000047 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000048 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000049 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000050 m_arch (target_arch),
51 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000052 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000053 m_breakpoint_list (false),
54 m_internal_breakpoint_list (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000055 m_process_sp (),
56 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000057 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000058 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000059 m_persistent_variables (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000060 m_stop_hooks (),
61 m_stop_hook_next_id (0)
Chris Lattner24943d22010-06-08 16:52:24 +000062{
Greg Clayton49ce6822010-10-31 03:01:06 +000063 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
64 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
65 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
66
Greg Claytone005f2c2010-11-06 01:53:30 +000067 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000068 if (log)
69 log->Printf ("%p Target::Target()", this);
70}
71
72//----------------------------------------------------------------------
73// Destructor
74//----------------------------------------------------------------------
75Target::~Target()
76{
Greg Claytone005f2c2010-11-06 01:53:30 +000077 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000078 if (log)
79 log->Printf ("%p Target::~Target()", this);
80 DeleteCurrentProcess ();
81}
82
83void
Caroline Tice7826c882010-10-26 03:11:13 +000084Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000085{
Greg Clayton3fed8b92010-10-08 00:21:05 +000086// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000087 if (description_level != lldb::eDescriptionLevelBrief)
88 {
89 s->Indent();
90 s->PutCString("Target\n");
91 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000092 m_images.Dump(s);
93 m_breakpoint_list.Dump(s);
94 m_internal_breakpoint_list.Dump(s);
95 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +000096 }
97 else
98 {
Greg Claytona66ba462010-10-30 04:51:46 +000099 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Tice7826c882010-10-26 03:11:13 +0000100 }
Chris Lattner24943d22010-06-08 16:52:24 +0000101}
102
103void
104Target::DeleteCurrentProcess ()
105{
106 if (m_process_sp.get())
107 {
Greg Clayton49480b12010-09-14 23:52:43 +0000108 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000109 if (m_process_sp->IsAlive())
110 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000111
112 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000113
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 Clayton63094e02010-06-23 01:19:29 +0000140 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000141}
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 Clayton33ed1702010-08-24 00:45:41 +0000184Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000185{
Chris Lattner24943d22010-06-08 16:52:24 +0000186 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 Clayton33ed1702010-08-24 00:45:41 +0000190 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000191 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000192 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 Lattner24943d22010-06-08 16:52:24 +0000198 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 Clayton12bec712010-06-28 21:30:43 +0000211Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000212{
Greg Clayton12bec712010-06-28 21:30:43 +0000213 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 Lattner24943d22010-06-08 16:52:24 +0000221}
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 Claytonc7f5d5c2010-07-23 23:33:17 +0000263 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000264 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000265 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000266
Greg Claytone005f2c2010-11-06 01:53:30 +0000267 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000268 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 Lattner24943d22010-06-08 16:52:24 +0000275 bp_sp->ResolveBreakpoint();
276 }
Jim Inghamd1686902010-10-14 23:45:03 +0000277
278 if (!internal && bp_sp)
279 {
280 m_last_created_breakpoint = bp_sp;
281 }
282
Chris Lattner24943d22010-06-08 16:52:24 +0000283 return bp_sp;
284}
285
286void
287Target::RemoveAllBreakpoints (bool internal_also)
288{
Greg Claytone005f2c2010-11-06 01:53:30 +0000289 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000290 if (log)
291 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
292
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000293 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000294 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000295 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000296
297 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000298}
299
300void
301Target::DisableAllBreakpoints (bool internal_also)
302{
Greg Claytone005f2c2010-11-06 01:53:30 +0000303 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000304 if (log)
305 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
306
307 m_breakpoint_list.SetEnabledAll (false);
308 if (internal_also)
309 m_internal_breakpoint_list.SetEnabledAll (false);
310}
311
312void
313Target::EnableAllBreakpoints (bool internal_also)
314{
Greg Claytone005f2c2010-11-06 01:53:30 +0000315 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000316 if (log)
317 log->Printf ("Target::%s (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 Claytone005f2c2010-11-06 01:53:30 +0000327 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000328 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 Claytonc7f5d5c2010-07-23 23:33:17 +0000334 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000335 else
Jim Inghamd1686902010-10-14 23:45:03 +0000336 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000337 if (m_last_created_breakpoint)
338 {
339 if (m_last_created_breakpoint->GetID() == break_id)
340 m_last_created_breakpoint.reset();
341 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000342 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000343 }
Chris Lattner24943d22010-06-08 16:52:24 +0000344 return true;
345 }
346 return false;
347}
348
349bool
350Target::DisableBreakpointByID (break_id_t break_id)
351{
Greg Claytone005f2c2010-11-06 01:53:30 +0000352 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000353 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 Claytone005f2c2010-11-06 01:53:30 +0000373 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000374 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 Ingham7508e732010-08-09 23:31:02 +0000419 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000420 if (!m_arch.IsValid())
421 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000422
Chris Lattner24943d22010-06-08 16:52:24 +0000423 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000424 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000425
426 if (executable_objfile)
427 {
428 executable_objfile->GetDependentModules(dependent_files);
429 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
430 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000431 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
432 FileSpec platform_dependent_file_spec;
433 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000434 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000435 else
436 platform_dependent_file_spec = dependent_file_spec;
437
438 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000439 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000440 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 Clayton24bc5d92011-03-30 18:16:51 +0000451 if (m_arch.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000452 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000453 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000454 }
455 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000456
457 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000458}
459
460
Jim Ingham7508e732010-08-09 23:31:02 +0000461bool
462Target::SetArchitecture (const ArchSpec &arch_spec)
463{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000464 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000465 {
466 // If we're setting the architecture to our current architecture, we
467 // don't need to do anything.
468 return true;
469 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000470 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000471 {
472 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000473 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000474 return true;
475 }
476 else
477 {
478 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000479 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000480 ModuleSP executable_sp = GetExecutableModule ();
481 m_images.Clear();
482 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000483 // 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 Lattner24943d22010-06-08 16:52:24 +0000513
Chris Lattner24943d22010-06-08 16:52:24 +0000514void
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 Clayton7b9fcc02010-12-06 23:51:26 +0000547
548 // Remove the images from the target image list
549 m_images.Remove(module_list);
550
Chris Lattner24943d22010-06-08 16:52:24 +0000551 // TODO: make event data that packages up the module_list
552 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
553}
554
555size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000556Target::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 Lattner24943d22010-06-08 16:52:24 +0000587{
Chris Lattner24943d22010-06-08 16:52:24 +0000588 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000589
Greg Clayton70436352010-06-30 23:03:03 +0000590 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
591
Greg Clayton26100dc2011-01-07 01:57:07 +0000592 size_t bytes_read = 0;
Greg Clayton889fbd02011-03-26 19:14:58 +0000593 Address resolved_addr;
594 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000595 {
596 if (process_is_valid)
Greg Claytoneea26402010-09-14 23:36:40 +0000597 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000598 else
Greg Clayton70436352010-06-30 23:03:03 +0000599 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000600 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000601 if (!resolved_addr.IsValid())
602 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000603
Greg Clayton26100dc2011-01-07 01:57:07 +0000604 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 Clayton70436352010-06-30 23:03:03 +0000610
611 if (process_is_valid)
612 {
Greg Claytoneea26402010-09-14 23:36:40 +0000613 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000614 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 Clayton26100dc2011-01-07 01:57:07 +0000625 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000626 if (bytes_read != dst_len)
627 {
628 if (error.Success())
629 {
630 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000631 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000632 else
Greg Clayton70436352010-06-30 23:03:03 +0000633 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 +0000634 }
635 }
Greg Clayton70436352010-06-30 23:03:03 +0000636 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 Lattner24943d22010-06-08 16:52:24 +0000645 }
Chris Lattner24943d22010-06-08 16:52:24 +0000646 }
Greg Clayton70436352010-06-30 23:03:03 +0000647
Greg Clayton26100dc2011-01-07 01:57:07 +0000648 if (!prefer_file_cache)
Greg Clayton70436352010-06-30 23:03:03 +0000649 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000650 // 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 Clayton70436352010-06-30 23:03:03 +0000653 }
654 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000655}
656
657
658ModuleSP
659Target::GetSharedModule
660(
661 const FileSpec& file_spec,
662 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000663 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000664 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 Lattner24943d22010-06-08 16:52:24 +0000674 Error error;
675
Greg Clayton24bc5d92011-03-30 18:16:51 +0000676 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +0000677 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 Clayton24bc5d92011-03-30 18:16:51 +0000687 // The platform is responsible for finding and caching an appropriate
688 // module in the shared module cache.
689 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000690 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000691 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 Lattner24943d22010-06-08 16:52:24 +0000704 }
705
Greg Clayton24bc5d92011-03-30 18:16:51 +0000706 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +0000707 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 Claytona830adb2010-10-04 01:05:56 +0000749Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000750{
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 Tice5bc8c972010-09-20 20:44:43 +0000787
Greg Clayton990de7b2010-11-18 23:32:35 +0000788void
Caroline Tice2a456812011-03-10 22:14:10 +0000789Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000790{
Greg Clayton990de7b2010-11-18 23:32:35 +0000791 UserSettingsControllerSP &usc = GetSettingsController();
792 usc.reset (new SettingsController);
793 UserSettingsController::InitializeSettingsController (usc,
794 SettingsController::global_settings_table,
795 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000796
797 // Now call SettingsInitialize() on each 'child' setting of Target
798 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000799}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000800
Greg Clayton990de7b2010-11-18 23:32:35 +0000801void
Caroline Tice2a456812011-03-10 22:14:10 +0000802Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000803{
Caroline Tice2a456812011-03-10 22:14:10 +0000804
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 Clayton990de7b2010-11-18 23:32:35 +0000811 UserSettingsControllerSP &usc = GetSettingsController();
812 UserSettingsController::FinalizeSettingsController (usc);
813 usc.reset();
814}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000815
Greg Clayton990de7b2010-11-18 23:32:35 +0000816UserSettingsControllerSP &
817Target::GetSettingsController ()
818{
819 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000820 return g_settings_controller;
821}
822
823ArchSpec
824Target::GetDefaultArchitecture ()
825{
Greg Clayton940b1032011-02-23 00:35:02 +0000826 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 Tice5bc8c972010-09-20 20:44:43 +0000831}
832
833void
Greg Clayton940b1032011-02-23 00:35:02 +0000834Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +0000835{
Greg Clayton940b1032011-02-23 00:35:02 +0000836 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
837
838 if (settings_controller_sp)
839 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000840}
841
Greg Claytona830adb2010-10-04 01:05:56 +0000842Target *
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 Tice1ebef442010-09-27 00:30:10 +0000861void
862Target::UpdateInstanceName ()
863{
864 StreamString sstr;
865
866 ModuleSP module_sp = GetExecutableModule();
867 if (module_sp)
868 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000869 sstr.Printf ("%s_%s",
870 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton940b1032011-02-23 00:35:02 +0000871 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000872 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
873 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +0000874 }
875}
876
Sean Callanan77e93942010-10-29 00:29:03 +0000877const char *
878Target::GetExpressionPrefixContentsAsCString ()
879{
880 return m_expr_prefix_contents.c_str();
881}
882
Greg Clayton427f2902010-12-14 02:59:59 +0000883ExecutionResults
884Target::EvaluateExpression
885(
886 const char *expr_cstr,
887 StackFrame *frame,
888 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +0000889 bool keep_in_memory,
Jim Inghame41494a2011-04-16 00:01:13 +0000890 bool fetch_dynamic_value,
Greg Clayton427f2902010-12-14 02:59:59 +0000891 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 Claytonc3b61d22010-12-15 05:08:08 +0000902 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +0000903 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
904 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
905 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton427f2902010-12-14 02:59:59 +0000906 }
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 Inghame41494a2011-04-16 00:01:13 +0000931 {
932 if (fetch_dynamic_value)
933 {
Jim Ingham47da8102011-04-22 23:53:53 +0000934 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(true);
Jim Inghame41494a2011-04-16 00:01:13 +0000935 if (dynamic_sp)
936 result_valobj_sp = dynamic_sp;
937 }
938
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000939 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +0000940 }
Greg Clayton427f2902010-12-14 02:59:59 +0000941
Sean Callanan6a925532011-01-13 08:53:35 +0000942 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
943
Greg Clayton427f2902010-12-14 02:59:59 +0000944 result_valobj_sp = const_valobj_sp;
945
Sean Callanan6a925532011-01-13 08:53:35 +0000946 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 Clayton427f2902010-12-14 02:59:59 +0000968 }
969 else
970 {
971 // Make sure we aren't just trying to see the value of a persistent
972 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +0000973 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 Clayton427f2902010-12-14 02:59:59 +0000978 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 Callanan6a925532011-01-13 08:53:35 +0000988 unwind_on_error,
989 keep_in_memory,
Greg Clayton427f2902010-12-14 02:59:59 +0000990 expr_cstr,
991 prefix,
992 result_valobj_sp);
993 }
994 }
995 return execution_results;
996}
997
Jim Inghamd60d94a2011-03-11 03:53:59 +0000998lldb::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 Inghamc54840c2011-03-22 01:47:27 +00001110 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 Inghamd60d94a2011-03-11 03:53:59 +00001123 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 Inghamc54840c2011-03-22 01:47:27 +00001140 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001141 hooks_ran = true;
1142 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001143 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001144 {
1145 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1146 any_thread_matched = true;
1147 }
1148
Jim Inghamc54840c2011-03-22 01:47:27 +00001149 if (print_thread_header)
1150 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001151
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 Clayton24bc5d92011-03-30 18:16:51 +00001157 &exc_ctx_with_reasons[i],
1158 stop_on_continue,
1159 stop_on_error,
1160 echo_commands,
1161 print_results,
1162 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001163
1164 // If the command started the target going again, we should bag out of
1165 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001166 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1167 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001168 {
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 Inghamd60d94a2011-03-11 03:53:59 +00001187 m_commands (),
1188 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001189 m_thread_spec_ap(NULL),
1190 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001191{
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 Wilsondbeb3e12011-04-11 19:41:40 +00001199 m_thread_spec_ap (NULL),
1200 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001201{
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 Tice5bc8c972010-09-20 20:44:43 +00001263//--------------------------------------------------------------
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 Claytonc0c1b0c2010-11-19 03:46:01 +00001282 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1283 false,
1284 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001285 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1286 return new_settings_sp;
1287}
1288
Caroline Tice5bc8c972010-09-20 20:44:43 +00001289
Jim Inghame41494a2011-04-16 00:01:13 +00001290#define TSC_DEFAULT_ARCH "default-arch"
1291#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001292#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001293#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytond284b662011-02-18 01:44:25 +00001294
1295
1296static const ConstString &
1297GetSettingNameForDefaultArch ()
1298{
1299 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1300
1301 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001302}
1303
Greg Claytond284b662011-02-18 01:44:25 +00001304static const ConstString &
1305GetSettingNameForExpressionPrefix ()
1306{
1307 static ConstString g_const_string (TSC_EXPR_PREFIX);
1308 return g_const_string;
1309}
1310
1311static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001312GetSettingNameForPreferDynamicValue ()
1313{
1314 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1315 return g_const_string;
1316}
1317
Greg Claytond284b662011-02-18 01:44:25 +00001318
Greg Clayton17cd9952011-04-22 03:55:06 +00001319static const ConstString &
1320GetSettingNameForSkipPrologue ()
1321{
1322 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1323 return g_const_string;
1324}
1325
1326
1327
Caroline Tice5bc8c972010-09-20 20:44:43 +00001328bool
1329Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1330 const char *index_value,
1331 const char *value,
1332 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001333 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001334 Error&err)
1335{
Greg Claytond284b662011-02-18 01:44:25 +00001336 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001337 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001338 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001339 if (!m_default_architecture.IsValid())
1340 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001341 }
1342 return true;
1343}
1344
1345
1346bool
1347Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1348 StringList &value,
1349 Error &err)
1350{
Greg Claytond284b662011-02-18 01:44:25 +00001351 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001352 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001353 // If the arch is invalid (the default), don't show a string for it
1354 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001355 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001356 return true;
1357 }
1358 else
1359 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1360
1361 return false;
1362}
1363
1364//--------------------------------------------------------------
1365// class TargetInstanceSettings
1366//--------------------------------------------------------------
1367
Greg Clayton638351a2010-12-04 00:10:17 +00001368TargetInstanceSettings::TargetInstanceSettings
1369(
1370 UserSettingsController &owner,
1371 bool live_instance,
1372 const char *name
1373) :
Greg Claytond284b662011-02-18 01:44:25 +00001374 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1375 m_expr_prefix_path (),
Jim Inghame41494a2011-04-16 00:01:13 +00001376 m_expr_prefix_contents (),
Greg Clayton17cd9952011-04-22 03:55:06 +00001377 m_prefer_dynamic_value (true),
1378 m_skip_prologue (true)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001379{
1380 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1381 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1382 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1383 // This is true for CreateInstanceName() too.
1384
1385 if (GetInstanceName () == InstanceSettings::InvalidName())
1386 {
1387 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1388 m_owner.RegisterInstanceSettings (this);
1389 }
1390
1391 if (live_instance)
1392 {
1393 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1394 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001395 }
1396}
1397
1398TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001399 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001400{
1401 if (m_instance_name != InstanceSettings::GetDefaultName())
1402 {
1403 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1404 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001405 }
1406}
1407
1408TargetInstanceSettings::~TargetInstanceSettings ()
1409{
1410}
1411
1412TargetInstanceSettings&
1413TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1414{
1415 if (this != &rhs)
1416 {
1417 }
1418
1419 return *this;
1420}
1421
Caroline Tice5bc8c972010-09-20 20:44:43 +00001422void
1423TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1424 const char *index_value,
1425 const char *value,
1426 const ConstString &instance_name,
1427 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001428 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001429 Error &err,
1430 bool pending)
1431{
Greg Claytond284b662011-02-18 01:44:25 +00001432 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001433 {
1434 switch (op)
1435 {
1436 default:
1437 err.SetErrorToGenericError ();
1438 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1439 return;
Greg Claytonb3448432011-03-24 21:19:54 +00001440 case eVarSetOperationAssign:
Sean Callanan77e93942010-10-29 00:29:03 +00001441 {
1442 FileSpec file_spec(value, true);
1443
1444 if (!file_spec.Exists())
1445 {
1446 err.SetErrorToGenericError ();
1447 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1448 return;
1449 }
1450
Greg Clayton9f527042011-02-03 17:47:47 +00001451 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan77e93942010-10-29 00:29:03 +00001452
Greg Clayton9f527042011-02-03 17:47:47 +00001453 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan77e93942010-10-29 00:29:03 +00001454 {
1455 err.SetErrorToGenericError ();
Greg Clayton9f527042011-02-03 17:47:47 +00001456 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan77e93942010-10-29 00:29:03 +00001457 return;
1458 }
1459
1460 m_expr_prefix_path = value;
Greg Clayton9f527042011-02-03 17:47:47 +00001461 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan77e93942010-10-29 00:29:03 +00001462 }
1463 return;
Greg Claytonb3448432011-03-24 21:19:54 +00001464 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00001465 err.SetErrorToGenericError ();
1466 err.SetErrorString ("Cannot append to a path.\n");
1467 return;
Greg Claytonb3448432011-03-24 21:19:54 +00001468 case eVarSetOperationClear:
Sean Callanan77e93942010-10-29 00:29:03 +00001469 m_expr_prefix_path.clear ();
1470 m_expr_prefix_contents.clear ();
1471 return;
1472 }
1473 }
Jim Inghame41494a2011-04-16 00:01:13 +00001474 else if (var_name == GetSettingNameForPreferDynamicValue())
1475 {
Greg Clayton17cd9952011-04-22 03:55:06 +00001476 UserSettingsController::UpdateBooleanVariable (op, m_prefer_dynamic_value, value, true, err);
1477 }
1478 else if (var_name == GetSettingNameForSkipPrologue())
1479 {
1480 UserSettingsController::UpdateBooleanVariable (op, m_skip_prologue, value, true, err);
Jim Inghame41494a2011-04-16 00:01:13 +00001481 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001482}
1483
1484void
Greg Claytond284b662011-02-18 01:44:25 +00001485TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001486{
Sean Callanan77e93942010-10-29 00:29:03 +00001487 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1488
1489 if (!new_settings_ptr)
1490 return;
1491
Greg Claytond284b662011-02-18 01:44:25 +00001492 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1493 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
Jim Inghame41494a2011-04-16 00:01:13 +00001494 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001495}
1496
Caroline Ticebcb5b452010-09-20 21:37:42 +00001497bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001498TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1499 const ConstString &var_name,
1500 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001501 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001502{
Greg Claytond284b662011-02-18 01:44:25 +00001503 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001504 {
1505 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1506 }
Jim Inghame41494a2011-04-16 00:01:13 +00001507 else if (var_name == GetSettingNameForPreferDynamicValue())
1508 {
1509 if (m_prefer_dynamic_value)
1510 value.AppendString ("true");
1511 else
1512 value.AppendString ("false");
1513 }
Greg Clayton17cd9952011-04-22 03:55:06 +00001514 else if (var_name == GetSettingNameForSkipPrologue())
1515 {
1516 if (m_skip_prologue)
1517 value.AppendString ("true");
1518 else
1519 value.AppendString ("false");
1520 }
Sean Callanan77e93942010-10-29 00:29:03 +00001521 else
1522 {
1523 if (err)
1524 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1525 return false;
1526 }
1527
1528 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001529}
1530
1531const ConstString
1532TargetInstanceSettings::CreateInstanceName ()
1533{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001534 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001535 static int instance_count = 1;
1536
Caroline Tice5bc8c972010-09-20 20:44:43 +00001537 sstr.Printf ("target_%d", instance_count);
1538 ++instance_count;
1539
1540 const ConstString ret_val (sstr.GetData());
1541 return ret_val;
1542}
1543
1544//--------------------------------------------------
1545// Target::SettingsController Variable Tables
1546//--------------------------------------------------
1547
1548SettingEntry
1549Target::SettingsController::global_settings_table[] =
1550{
Greg Claytond284b662011-02-18 01:44:25 +00001551 // var-name var-type default enum init'd hidden help-text
1552 // ================= ================== =========== ==== ====== ====== =========================================================================
1553 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1554 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1555};
1556
Caroline Tice5bc8c972010-09-20 20:44:43 +00001557SettingEntry
1558Target::SettingsController::instance_settings_table[] =
1559{
Greg Claytonb3448432011-03-24 21:19:54 +00001560 // var-name var-type default enum init'd hidden help-text
1561 // ================= ================== =========== ==== ====== ====== =========================================================================
1562 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
Jim Inghame41494a2011-04-16 00:01:13 +00001563 { TSC_PREFER_DYNAMIC, eSetVarTypeBoolean ,"true" , NULL, false, false, "Should printed values be shown as their dynamic value." },
Greg Clayton17cd9952011-04-22 03:55:06 +00001564 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean ,"true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
Greg Claytonb3448432011-03-24 21:19:54 +00001565 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001566};