blob: 5a49aab8e4e03370319e27b7e5e5f2dcba3fc19c [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"
Jim Ingham03c8ee52011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chenecd4feb2011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton427f2902010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000026#include "lldb/Core/Timer.h"
27#include "lldb/Core/ValueObject.h"
Sean Callanandcf03f82011-11-15 22:27:19 +000028#include "lldb/Expression/ClangASTSource.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000029#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000031#include "lldb/Interpreter/CommandInterpreter.h"
32#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chen3f883492012-06-04 23:19:54 +000033#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton73844aa2012-08-22 17:17:09 +000034#include "lldb/Interpreter/OptionValues.h"
35#include "lldb/Interpreter/Property.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036#include "lldb/lldb-private-log.h"
37#include "lldb/Symbol/ObjectFile.h"
38#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000039#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000040#include "lldb/Target/Thread.h"
41#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000042
43using namespace lldb;
44using namespace lldb_private;
45
Jim Ingham5a15e692012-02-16 06:50:00 +000046ConstString &
47Target::GetStaticBroadcasterClass ()
48{
49 static ConstString class_name ("lldb.target");
50 return class_name;
51}
52
Chris Lattner24943d22010-06-08 16:52:24 +000053//----------------------------------------------------------------------
54// Target constructor
55//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000056Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton73844aa2012-08-22 17:17:09 +000057 TargetProperties (this),
Jim Ingham5a15e692012-02-16 06:50:00 +000058 Broadcaster (&debugger, "lldb.target"),
Greg Clayton24bc5d92011-03-30 18:16:51 +000059 ExecutionContextScope (),
Greg Clayton63094e02010-06-23 01:19:29 +000060 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000061 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000062 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000063 m_arch (target_arch),
64 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000065 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000066 m_breakpoint_list (false),
67 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000068 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000069 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000070 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000071 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000072 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000073 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000074 m_scratch_ast_source_ap (NULL),
75 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000076 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000077 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000078 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000079 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000080 m_suppress_stop_hooks (false),
81 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +000082{
Greg Clayton49ce6822010-10-31 03:01:06 +000083 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
84 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
85 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000086
87 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000088
Greg Claytone005f2c2010-11-06 01:53:30 +000089 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000090 if (log)
91 log->Printf ("%p Target::Target()", this);
92}
93
94//----------------------------------------------------------------------
95// Destructor
96//----------------------------------------------------------------------
97Target::~Target()
98{
Greg Claytone005f2c2010-11-06 01:53:30 +000099 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000100 if (log)
101 log->Printf ("%p Target::~Target()", this);
102 DeleteCurrentProcess ();
103}
104
105void
Caroline Tice7826c882010-10-26 03:11:13 +0000106Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000107{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000108// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000109 if (description_level != lldb::eDescriptionLevelBrief)
110 {
111 s->Indent();
112 s->PutCString("Target\n");
113 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000114 m_images.Dump(s);
115 m_breakpoint_list.Dump(s);
116 m_internal_breakpoint_list.Dump(s);
117 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000118 }
119 else
120 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000121 Module *exe_module = GetExecutableModulePointer();
122 if (exe_module)
123 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000124 else
125 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000126 }
Chris Lattner24943d22010-06-08 16:52:24 +0000127}
128
129void
130Target::DeleteCurrentProcess ()
131{
132 if (m_process_sp.get())
133 {
Greg Clayton49480b12010-09-14 23:52:43 +0000134 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000135 if (m_process_sp->IsAlive())
136 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000137
138 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000139
140 // Do any cleanup of the target we need to do between process instances.
141 // NB It is better to do this before destroying the process in case the
142 // clean up needs some help from the process.
143 m_breakpoint_list.ClearAllBreakpointSites();
144 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000145 // Disable watchpoints just on the debugger side.
Johnny Chen116a5cd2012-02-25 06:44:30 +0000146 Mutex::Locker locker;
147 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000148 DisableAllWatchpoints(false);
Johnny Chen116a5cd2012-02-25 06:44:30 +0000149 ClearAllWatchpointHitCounts();
Chris Lattner24943d22010-06-08 16:52:24 +0000150 m_process_sp.reset();
151 }
152}
153
154const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000155Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000156{
157 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000158 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000159 return m_process_sp;
160}
161
162const lldb::ProcessSP &
163Target::GetProcessSP () const
164{
165 return m_process_sp;
166}
167
Greg Clayton153ccd72011-08-10 02:10:13 +0000168void
169Target::Destroy()
170{
171 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000172 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000173 DeleteCurrentProcess ();
174 m_platform_sp.reset();
175 m_arch.Clear();
176 m_images.Clear();
177 m_section_load_list.Clear();
178 const bool notify = false;
179 m_breakpoint_list.RemoveAll(notify);
180 m_internal_breakpoint_list.RemoveAll(notify);
181 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000182 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000183 m_search_filter_sp.reset();
184 m_image_search_paths.Clear(notify);
185 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000186 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000187 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000188 m_persistent_variables.Clear();
189 m_stop_hooks.clear();
190 m_stop_hook_next_id = 0;
191 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000192 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000193}
194
195
Chris Lattner24943d22010-06-08 16:52:24 +0000196BreakpointList &
197Target::GetBreakpointList(bool internal)
198{
199 if (internal)
200 return m_internal_breakpoint_list;
201 else
202 return m_breakpoint_list;
203}
204
205const BreakpointList &
206Target::GetBreakpointList(bool internal) const
207{
208 if (internal)
209 return m_internal_breakpoint_list;
210 else
211 return m_breakpoint_list;
212}
213
214BreakpointSP
215Target::GetBreakpointByID (break_id_t break_id)
216{
217 BreakpointSP bp_sp;
218
219 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
220 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
221 else
222 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
223
224 return bp_sp;
225}
226
227BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000228Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
229 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000230 RegularExpression &source_regex,
231 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000232{
Jim Inghamd6d47972011-09-23 00:54:11 +0000233 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
234 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000235 return CreateBreakpoint (filter_sp, resolver_sp, internal);
236}
237
238
239BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000240Target::CreateBreakpoint (const FileSpecList *containingModules,
241 const FileSpec &file,
242 uint32_t line_no,
243 bool check_inlines,
244 LazyBool skip_prologue,
245 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000246{
247 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000248
249 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines,
250 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000251 return CreateBreakpoint (filter_sp, resolver_sp, internal);
252}
253
254
255BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000256Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000257{
Chris Lattner24943d22010-06-08 16:52:24 +0000258 Address so_addr;
259 // Attempt to resolve our load address if possible, though it is ok if
260 // it doesn't resolve to section/offset.
261
Greg Clayton33ed1702010-08-24 00:45:41 +0000262 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000263 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000264 if (!so_addr.IsValid())
265 {
266 // The address didn't resolve, so just set this as an absolute address
267 so_addr.SetOffset (addr);
268 }
269 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000270 return bp_sp;
271}
272
273BreakpointSP
274Target::CreateBreakpoint (Address &addr, bool internal)
275{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000276 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000277 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
278 return CreateBreakpoint (filter_sp, resolver_sp, internal);
279}
280
281BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000282Target::CreateBreakpoint (const FileSpecList *containingModules,
283 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000284 const char *func_name,
285 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000286 LazyBool skip_prologue,
287 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000288{
Greg Clayton12bec712010-06-28 21:30:43 +0000289 BreakpointSP bp_sp;
290 if (func_name)
291 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000292 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000293
294 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
295 func_name,
296 func_name_type_mask,
297 Breakpoint::Exact,
298 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000299 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
300 }
301 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000302}
303
Jim Ingham4722b102012-03-06 00:37:27 +0000304lldb::BreakpointSP
305Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000306 const FileSpecList *containingSourceFiles,
307 const std::vector<std::string> &func_names,
308 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000309 LazyBool skip_prologue,
310 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000311{
312 BreakpointSP bp_sp;
313 size_t num_names = func_names.size();
314 if (num_names > 0)
315 {
316 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
317
318 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
319 func_names,
320 func_name_type_mask,
321 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
322 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
323 }
324 return bp_sp;
325}
326
Jim Inghamc1053622012-03-03 02:05:11 +0000327BreakpointSP
328Target::CreateBreakpoint (const FileSpecList *containingModules,
329 const FileSpecList *containingSourceFiles,
330 const char *func_names[],
331 size_t num_names,
332 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000333 LazyBool skip_prologue,
334 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000335{
336 BreakpointSP bp_sp;
337 if (num_names > 0)
338 {
339 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
340
341 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
342 func_names,
343 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000344 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000345 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
346 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
347 }
348 return bp_sp;
349}
Chris Lattner24943d22010-06-08 16:52:24 +0000350
351SearchFilterSP
352Target::GetSearchFilterForModule (const FileSpec *containingModule)
353{
354 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000355 if (containingModule != NULL)
356 {
357 // TODO: We should look into sharing module based search filters
358 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000359 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000360 }
361 else
362 {
363 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000364 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000365 filter_sp = m_search_filter_sp;
366 }
367 return filter_sp;
368}
369
Jim Ingham03c8ee52011-09-21 01:17:13 +0000370SearchFilterSP
371Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
372{
373 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000374 if (containingModules && containingModules->GetSize() != 0)
375 {
376 // TODO: We should look into sharing module based search filters
377 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000378 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000379 }
380 else
381 {
382 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000383 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000384 filter_sp = m_search_filter_sp;
385 }
386 return filter_sp;
387}
388
Jim Inghamd6d47972011-09-23 00:54:11 +0000389SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000390Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
391 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000392{
393 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
394 return GetSearchFilterForModuleList(containingModules);
395
396 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000397 if (containingModules == NULL)
398 {
399 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
400 // but that will take a little reworking.
401
Greg Clayton13d24fb2012-01-29 20:56:30 +0000402 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000403 }
404 else
405 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000406 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000407 }
408 return filter_sp;
409}
410
Chris Lattner24943d22010-06-08 16:52:24 +0000411BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000412Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000413 const FileSpecList *containingSourceFiles,
414 RegularExpression &func_regex,
415 LazyBool skip_prologue,
416 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000417{
Jim Inghamd6d47972011-09-23 00:54:11 +0000418 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000419 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
420 func_regex,
421 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000422
423 return CreateBreakpoint (filter_sp, resolver_sp, internal);
424}
425
Jim Ingham3df164e2012-03-05 04:47:34 +0000426lldb::BreakpointSP
427Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
428{
429 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
430}
431
Chris Lattner24943d22010-06-08 16:52:24 +0000432BreakpointSP
433Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
434{
435 BreakpointSP bp_sp;
436 if (filter_sp && resolver_sp)
437 {
438 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
439 resolver_sp->SetBreakpoint (bp_sp.get());
440
441 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000442 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000443 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000444 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000445
Greg Claytone005f2c2010-11-06 01:53:30 +0000446 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000447 if (log)
448 {
449 StreamString s;
450 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
451 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
452 }
453
Chris Lattner24943d22010-06-08 16:52:24 +0000454 bp_sp->ResolveBreakpoint();
455 }
Jim Inghamd1686902010-10-14 23:45:03 +0000456
457 if (!internal && bp_sp)
458 {
459 m_last_created_breakpoint = bp_sp;
460 }
461
Chris Lattner24943d22010-06-08 16:52:24 +0000462 return bp_sp;
463}
464
Johnny Chenda5a8022011-09-20 23:28:55 +0000465bool
466Target::ProcessIsValid()
467{
468 return (m_process_sp && m_process_sp->IsAlive());
469}
470
Johnny Chen3f883492012-06-04 23:19:54 +0000471static bool
472CheckIfWatchpointsExhausted(Target *target, Error &error)
473{
474 uint32_t num_supported_hardware_watchpoints;
475 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
476 if (rc.Success())
477 {
478 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
479 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
480 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
481 num_supported_hardware_watchpoints);
482 }
483 return false;
484}
485
Johnny Chenecd4feb2011-10-14 00:42:25 +0000486// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000487// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000488WatchpointSP
Johnny Chen3f883492012-06-04 23:19:54 +0000489Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000490{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000491 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
492 if (log)
493 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
494 __FUNCTION__, addr, size, type);
495
Johnny Chenecd4feb2011-10-14 00:42:25 +0000496 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000497 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000498 {
499 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000500 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000501 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000502 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000503 {
504 if (size == 0)
505 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
506 else
507 error.SetErrorStringWithFormat("invalid watch address: %llu", addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000508 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000509 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000510
Johnny Chenecd4feb2011-10-14 00:42:25 +0000511 // Currently we only support one watchpoint per address, with total number
512 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000513
514 // Grab the list mutex while doing operations.
515 Mutex::Locker locker;
516 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000517 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000518 if (matched_sp)
519 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000520 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000521 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000522 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
523 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000524 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000525 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000526 wp_sp = matched_sp;
527 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000528 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000529 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000530 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000531 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000532 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000533 }
534
Johnny Chenecd4feb2011-10-14 00:42:25 +0000535 if (!wp_sp) {
536 Watchpoint *new_wp = new Watchpoint(addr, size);
537 if (!new_wp) {
538 printf("Watchpoint ctor failed, out of memory?\n");
539 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000540 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000541 new_wp->SetWatchpointType(type);
542 new_wp->SetTarget(this);
543 wp_sp.reset(new_wp);
544 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000545 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000546
Johnny Chen3f883492012-06-04 23:19:54 +0000547 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000548 if (log)
549 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
550 __FUNCTION__,
Johnny Chen3f883492012-06-04 23:19:54 +0000551 error.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000552 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000553
Johnny Chen3f883492012-06-04 23:19:54 +0000554 if (error.Fail()) {
Johnny Chen155599b2012-03-26 22:00:10 +0000555 // Enabling the watchpoint on the device side failed.
556 // Remove the said watchpoint from the list maintained by the target instance.
557 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chen3f883492012-06-04 23:19:54 +0000558 // See if we could provide more helpful error message.
559 if (!CheckIfWatchpointsExhausted(this, error))
560 {
561 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
562 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
563 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000564 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000565 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000566 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000567 m_last_created_watchpoint = wp_sp;
568 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000569}
570
Chris Lattner24943d22010-06-08 16:52:24 +0000571void
572Target::RemoveAllBreakpoints (bool internal_also)
573{
Greg Claytone005f2c2010-11-06 01:53:30 +0000574 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000575 if (log)
576 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
577
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000578 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000579 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000580 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000581
582 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000583}
584
585void
586Target::DisableAllBreakpoints (bool internal_also)
587{
Greg Claytone005f2c2010-11-06 01:53:30 +0000588 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000589 if (log)
590 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
591
592 m_breakpoint_list.SetEnabledAll (false);
593 if (internal_also)
594 m_internal_breakpoint_list.SetEnabledAll (false);
595}
596
597void
598Target::EnableAllBreakpoints (bool internal_also)
599{
Greg Claytone005f2c2010-11-06 01:53:30 +0000600 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000601 if (log)
602 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
603
604 m_breakpoint_list.SetEnabledAll (true);
605 if (internal_also)
606 m_internal_breakpoint_list.SetEnabledAll (true);
607}
608
609bool
610Target::RemoveBreakpointByID (break_id_t break_id)
611{
Greg Claytone005f2c2010-11-06 01:53:30 +0000612 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000613 if (log)
614 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
615
616 if (DisableBreakpointByID (break_id))
617 {
618 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000619 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000620 else
Jim Inghamd1686902010-10-14 23:45:03 +0000621 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000622 if (m_last_created_breakpoint)
623 {
624 if (m_last_created_breakpoint->GetID() == break_id)
625 m_last_created_breakpoint.reset();
626 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000627 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000628 }
Chris Lattner24943d22010-06-08 16:52:24 +0000629 return true;
630 }
631 return false;
632}
633
634bool
635Target::DisableBreakpointByID (break_id_t break_id)
636{
Greg Claytone005f2c2010-11-06 01:53:30 +0000637 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000638 if (log)
639 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
640
641 BreakpointSP bp_sp;
642
643 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
644 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
645 else
646 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
647 if (bp_sp)
648 {
649 bp_sp->SetEnabled (false);
650 return true;
651 }
652 return false;
653}
654
655bool
656Target::EnableBreakpointByID (break_id_t break_id)
657{
Greg Claytone005f2c2010-11-06 01:53:30 +0000658 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000659 if (log)
660 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
661 __FUNCTION__,
662 break_id,
663 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
664
665 BreakpointSP bp_sp;
666
667 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
668 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
669 else
670 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
671
672 if (bp_sp)
673 {
674 bp_sp->SetEnabled (true);
675 return true;
676 }
677 return false;
678}
679
Johnny Chenc86582f2011-09-23 21:21:43 +0000680// The flag 'end_to_end', default to true, signifies that the operation is
681// performed end to end, for both the debugger and the debuggee.
682
Johnny Chenecd4feb2011-10-14 00:42:25 +0000683// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
684// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000685bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000686Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000687{
688 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
689 if (log)
690 log->Printf ("Target::%s\n", __FUNCTION__);
691
Johnny Chenc86582f2011-09-23 21:21:43 +0000692 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000693 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000694 return true;
695 }
696
697 // Otherwise, it's an end to end operation.
698
Johnny Chenda5a8022011-09-20 23:28:55 +0000699 if (!ProcessIsValid())
700 return false;
701
Johnny Chenecd4feb2011-10-14 00:42:25 +0000702 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000703 for (size_t i = 0; i < num_watchpoints; ++i)
704 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000705 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
706 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000707 return false;
708
Johnny Chenecd4feb2011-10-14 00:42:25 +0000709 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000710 if (rc.Fail())
711 return false;
712 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000713 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000714 return true; // Success!
715}
716
Johnny Chenecd4feb2011-10-14 00:42:25 +0000717// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
718// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000719bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000720Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000721{
722 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
723 if (log)
724 log->Printf ("Target::%s\n", __FUNCTION__);
725
Johnny Chenc86582f2011-09-23 21:21:43 +0000726 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000727 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000728 return true;
729 }
730
731 // Otherwise, it's an end to end operation.
732
Johnny Chenda5a8022011-09-20 23:28:55 +0000733 if (!ProcessIsValid())
734 return false;
735
Johnny Chenecd4feb2011-10-14 00:42:25 +0000736 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000737 for (size_t i = 0; i < num_watchpoints; ++i)
738 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000739 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
740 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000741 return false;
742
Johnny Chenecd4feb2011-10-14 00:42:25 +0000743 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000744 if (rc.Fail())
745 return false;
746 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000747 return true; // Success!
748}
749
Johnny Chenecd4feb2011-10-14 00:42:25 +0000750// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
751// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000752bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000753Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000754{
755 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
756 if (log)
757 log->Printf ("Target::%s\n", __FUNCTION__);
758
Johnny Chenc86582f2011-09-23 21:21:43 +0000759 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000760 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000761 return true;
762 }
763
764 // Otherwise, it's an end to end operation.
765
Johnny Chenda5a8022011-09-20 23:28:55 +0000766 if (!ProcessIsValid())
767 return false;
768
Johnny Chenecd4feb2011-10-14 00:42:25 +0000769 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000770 for (size_t i = 0; i < num_watchpoints; ++i)
771 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000772 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
773 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000774 return false;
775
Johnny Chenecd4feb2011-10-14 00:42:25 +0000776 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000777 if (rc.Fail())
778 return false;
779 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000780 return true; // Success!
781}
782
Johnny Chen116a5cd2012-02-25 06:44:30 +0000783// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
784bool
785Target::ClearAllWatchpointHitCounts ()
786{
787 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
788 if (log)
789 log->Printf ("Target::%s\n", __FUNCTION__);
790
791 size_t num_watchpoints = m_watchpoint_list.GetSize();
792 for (size_t i = 0; i < num_watchpoints; ++i)
793 {
794 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
795 if (!wp_sp)
796 return false;
797
798 wp_sp->ResetHitCount();
799 }
800 return true; // Success!
801}
802
Johnny Chenecd4feb2011-10-14 00:42:25 +0000803// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000804// during these operations.
805bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000806Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000807{
808 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
809 if (log)
810 log->Printf ("Target::%s\n", __FUNCTION__);
811
812 if (!ProcessIsValid())
813 return false;
814
Johnny Chenecd4feb2011-10-14 00:42:25 +0000815 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000816 for (size_t i = 0; i < num_watchpoints; ++i)
817 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000818 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
819 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000820 return false;
821
Johnny Chenecd4feb2011-10-14 00:42:25 +0000822 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000823 }
824 return true; // Success!
825}
826
Johnny Chenecd4feb2011-10-14 00:42:25 +0000827// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000828bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000829Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000830{
831 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
832 if (log)
833 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
834
835 if (!ProcessIsValid())
836 return false;
837
Johnny Chenecd4feb2011-10-14 00:42:25 +0000838 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
839 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000840 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000841 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000842 if (rc.Success())
843 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000844
Johnny Chen01acfa72011-09-22 18:04:58 +0000845 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000846 }
847 return false;
848}
849
Johnny Chenecd4feb2011-10-14 00:42:25 +0000850// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000851bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000852Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000853{
854 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
855 if (log)
856 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
857
858 if (!ProcessIsValid())
859 return false;
860
Johnny Chenecd4feb2011-10-14 00:42:25 +0000861 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
862 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000863 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000864 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000865 if (rc.Success())
866 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000867
Johnny Chen01acfa72011-09-22 18:04:58 +0000868 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000869 }
870 return false;
871}
872
Johnny Chenecd4feb2011-10-14 00:42:25 +0000873// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000874bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000875Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000876{
877 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
878 if (log)
879 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
880
Johnny Chenecd4feb2011-10-14 00:42:25 +0000881 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000882 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000883 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000884 return true;
885 }
886 return false;
887}
888
Johnny Chenecd4feb2011-10-14 00:42:25 +0000889// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000890bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000891Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000892{
893 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
894 if (log)
895 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
896
897 if (!ProcessIsValid())
898 return false;
899
Johnny Chenecd4feb2011-10-14 00:42:25 +0000900 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
901 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000902 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000903 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000904 return true;
905 }
906 return false;
907}
908
Chris Lattner24943d22010-06-08 16:52:24 +0000909ModuleSP
910Target::GetExecutableModule ()
911{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000912 return m_images.GetModuleAtIndex(0);
913}
914
915Module*
916Target::GetExecutableModulePointer ()
917{
918 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000919}
920
921void
922Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
923{
924 m_images.Clear();
925 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000926 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000927 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000928
929 if (executable_sp.get())
930 {
931 Timer scoped_timer (__PRETTY_FUNCTION__,
932 "Target::SetExecutableModule (executable = '%s/%s')",
933 executable_sp->GetFileSpec().GetDirectory().AsCString(),
934 executable_sp->GetFileSpec().GetFilename().AsCString());
935
936 m_images.Append(executable_sp); // The first image is our exectuable file
937
Jim Ingham7508e732010-08-09 23:31:02 +0000938 // 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 +0000939 if (!m_arch.IsValid())
940 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000941
Chris Lattner24943d22010-06-08 16:52:24 +0000942 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000943 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000944
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000945 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000946 {
947 executable_objfile->GetDependentModules(dependent_files);
948 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
949 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000950 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
951 FileSpec platform_dependent_file_spec;
952 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000953 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000954 else
955 platform_dependent_file_spec = dependent_file_spec;
956
Greg Clayton444fe992012-02-26 05:51:37 +0000957 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
958 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +0000959 if (image_module_sp.get())
960 {
Chris Lattner24943d22010-06-08 16:52:24 +0000961 ObjectFile *objfile = image_module_sp->GetObjectFile();
962 if (objfile)
963 objfile->GetDependentModules(dependent_files);
964 }
965 }
966 }
Chris Lattner24943d22010-06-08 16:52:24 +0000967 }
968}
969
970
Jim Ingham7508e732010-08-09 23:31:02 +0000971bool
972Target::SetArchitecture (const ArchSpec &arch_spec)
973{
Greg Claytonb170aee2012-05-08 01:45:38 +0000974 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000975 {
Greg Claytonb170aee2012-05-08 01:45:38 +0000976 // If we haven't got a valid arch spec, or the architectures are
977 // compatible, so just update the architecture. Architectures can be
978 // equal, yet the triple OS and vendor might change, so we need to do
979 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000980 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000981 return true;
982 }
983 else
984 {
985 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000986 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000987 ModuleSP executable_sp = GetExecutableModule ();
988 m_images.Clear();
989 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000990 m_scratch_ast_source_ap.reset();
991 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000992 // Need to do something about unsetting breakpoints.
993
994 if (executable_sp)
995 {
Greg Clayton444fe992012-02-26 05:51:37 +0000996 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
997 Error error = ModuleList::GetSharedModule (module_spec,
998 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +0000999 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001000 NULL,
1001 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001002
1003 if (!error.Fail() && executable_sp)
1004 {
1005 SetExecutableModule (executable_sp, true);
1006 return true;
1007 }
Jim Ingham7508e732010-08-09 23:31:02 +00001008 }
1009 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001010 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001011}
Chris Lattner24943d22010-06-08 16:52:24 +00001012
Chris Lattner24943d22010-06-08 16:52:24 +00001013void
1014Target::ModuleAdded (ModuleSP &module_sp)
1015{
1016 // A module is being added to this target for the first time
1017 ModuleList module_list;
1018 module_list.Append(module_sp);
1019 ModulesDidLoad (module_list);
1020}
1021
1022void
1023Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
1024{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001025 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001026 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001027}
1028
1029void
1030Target::ModulesDidLoad (ModuleList &module_list)
1031{
1032 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1033 // TODO: make event data that packages up the module_list
1034 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1035}
1036
1037void
1038Target::ModulesDidUnload (ModuleList &module_list)
1039{
1040 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +00001041
1042 // Remove the images from the target image list
1043 m_images.Remove(module_list);
1044
Chris Lattner24943d22010-06-08 16:52:24 +00001045 // TODO: make event data that packages up the module_list
1046 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1047}
1048
Jim Ingham7089d8a2011-10-28 23:14:11 +00001049
Daniel Dunbar705a0982011-10-31 22:50:37 +00001050bool
Greg Clayton444fe992012-02-26 05:51:37 +00001051Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001052{
1053
Greg Clayton73844aa2012-08-22 17:17:09 +00001054 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001055 {
1056 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001057 ModuleSpec module_spec (module_file_spec);
1058 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001059
1060 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1061 // black list.
1062 if (num_modules > 0)
1063 {
1064 for (int i = 0; i < num_modules; i++)
1065 {
1066 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1067 return false;
1068 }
1069 return true;
1070 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001071 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001072 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001073}
1074
Daniel Dunbar705a0982011-10-31 22:50:37 +00001075bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001076Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1077{
Greg Clayton73844aa2012-08-22 17:17:09 +00001078 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001079 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001080 if (m_platform_sp)
1081 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001082 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001083 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001084}
1085
Chris Lattner24943d22010-06-08 16:52:24 +00001086size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001087Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1088{
Greg Clayton3508c382012-02-24 01:59:29 +00001089 SectionSP section_sp (addr.GetSection());
1090 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001091 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001092 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1093 if (section_sp->IsEncrypted())
1094 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001095 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001096 return 0;
1097 }
Greg Clayton3508c382012-02-24 01:59:29 +00001098 ModuleSP module_sp (section_sp->GetModule());
1099 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001100 {
Greg Clayton3508c382012-02-24 01:59:29 +00001101 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1102 if (objfile)
1103 {
1104 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1105 addr.GetOffset(),
1106 dst,
1107 dst_len);
1108 if (bytes_read > 0)
1109 return bytes_read;
1110 else
1111 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1112 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001113 else
Greg Clayton3508c382012-02-24 01:59:29 +00001114 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001115 }
1116 else
Greg Clayton3508c382012-02-24 01:59:29 +00001117 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001118 }
1119 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001120 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001121
Greg Clayton26100dc2011-01-07 01:57:07 +00001122 return 0;
1123}
1124
1125size_t
Enrico Granata91544802011-09-06 19:20:51 +00001126Target::ReadMemory (const Address& addr,
1127 bool prefer_file_cache,
1128 void *dst,
1129 size_t dst_len,
1130 Error &error,
1131 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001132{
Chris Lattner24943d22010-06-08 16:52:24 +00001133 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001134
Enrico Granata91544802011-09-06 19:20:51 +00001135 // if we end up reading this from process memory, we will fill this
1136 // with the actual load address
1137 if (load_addr_ptr)
1138 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1139
Greg Clayton26100dc2011-01-07 01:57:07 +00001140 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001141
1142 addr_t load_addr = LLDB_INVALID_ADDRESS;
1143 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001144 Address resolved_addr;
1145 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001146 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001147 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001148 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001149 // No sections are loaded, so we must assume we are not running
1150 // yet and anything we are given is a file address.
1151 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1152 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001153 }
Greg Clayton70436352010-06-30 23:03:03 +00001154 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001155 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001156 // We have at least one section loaded. This can be becuase
1157 // we have manually loaded some sections with "target modules load ..."
1158 // or because we have have a live process that has sections loaded
1159 // through the dynamic loader
1160 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1161 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001162 }
Greg Clayton70436352010-06-30 23:03:03 +00001163 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001164 if (!resolved_addr.IsValid())
1165 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001166
Greg Clayton9b82f862011-07-11 05:12:02 +00001167
Greg Clayton26100dc2011-01-07 01:57:07 +00001168 if (prefer_file_cache)
1169 {
1170 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1171 if (bytes_read > 0)
1172 return bytes_read;
1173 }
Greg Clayton70436352010-06-30 23:03:03 +00001174
Johnny Chenda5a8022011-09-20 23:28:55 +00001175 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001176 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001177 if (load_addr == LLDB_INVALID_ADDRESS)
1178 load_addr = resolved_addr.GetLoadAddress (this);
1179
Greg Clayton70436352010-06-30 23:03:03 +00001180 if (load_addr == LLDB_INVALID_ADDRESS)
1181 {
Greg Clayton3508c382012-02-24 01:59:29 +00001182 ModuleSP addr_module_sp (resolved_addr.GetModule());
1183 if (addr_module_sp && addr_module_sp->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001184 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001185 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001186 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001187 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001188 else
Greg Clayton9c236732011-10-26 00:56:27 +00001189 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001190 }
1191 else
1192 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001193 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001194 if (bytes_read != dst_len)
1195 {
1196 if (error.Success())
1197 {
1198 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001199 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001200 else
Greg Clayton9c236732011-10-26 00:56:27 +00001201 error.SetErrorStringWithFormat("only %zu of %zu bytes were read from memory at 0x%llx", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001202 }
1203 }
Greg Clayton70436352010-06-30 23:03:03 +00001204 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001205 {
1206 if (load_addr_ptr)
1207 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001208 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001209 }
Greg Clayton70436352010-06-30 23:03:03 +00001210 // If the address is not section offset we have an address that
1211 // doesn't resolve to any address in any currently loaded shared
1212 // libaries and we failed to read memory so there isn't anything
1213 // more we can do. If it is section offset, we might be able to
1214 // read cached memory from the object file.
1215 if (!resolved_addr.IsSectionOffset())
1216 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001217 }
Chris Lattner24943d22010-06-08 16:52:24 +00001218 }
Greg Clayton70436352010-06-30 23:03:03 +00001219
Greg Clayton9b82f862011-07-11 05:12:02 +00001220 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001221 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001222 // If we didn't already try and read from the object file cache, then
1223 // try it after failing to read from the process.
1224 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001225 }
1226 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001227}
1228
Greg Clayton7dd98df2011-07-12 17:06:17 +00001229size_t
1230Target::ReadScalarIntegerFromMemory (const Address& addr,
1231 bool prefer_file_cache,
1232 uint32_t byte_size,
1233 bool is_signed,
1234 Scalar &scalar,
1235 Error &error)
1236{
1237 uint64_t uval;
1238
1239 if (byte_size <= sizeof(uval))
1240 {
1241 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1242 if (bytes_read == byte_size)
1243 {
1244 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1245 uint32_t offset = 0;
1246 if (byte_size <= 4)
1247 scalar = data.GetMaxU32 (&offset, byte_size);
1248 else
1249 scalar = data.GetMaxU64 (&offset, byte_size);
1250
1251 if (is_signed)
1252 scalar.SignExtend(byte_size * 8);
1253 return bytes_read;
1254 }
1255 }
1256 else
1257 {
1258 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1259 }
1260 return 0;
1261}
1262
1263uint64_t
1264Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1265 bool prefer_file_cache,
1266 size_t integer_byte_size,
1267 uint64_t fail_value,
1268 Error &error)
1269{
1270 Scalar scalar;
1271 if (ReadScalarIntegerFromMemory (addr,
1272 prefer_file_cache,
1273 integer_byte_size,
1274 false,
1275 scalar,
1276 error))
1277 return scalar.ULongLong(fail_value);
1278 return fail_value;
1279}
1280
1281bool
1282Target::ReadPointerFromMemory (const Address& addr,
1283 bool prefer_file_cache,
1284 Error &error,
1285 Address &pointer_addr)
1286{
1287 Scalar scalar;
1288 if (ReadScalarIntegerFromMemory (addr,
1289 prefer_file_cache,
1290 m_arch.GetAddressByteSize(),
1291 false,
1292 scalar,
1293 error))
1294 {
1295 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1296 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1297 {
1298 if (m_section_load_list.IsEmpty())
1299 {
1300 // No sections are loaded, so we must assume we are not running
1301 // yet and anything we are given is a file address.
1302 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1303 }
1304 else
1305 {
1306 // We have at least one section loaded. This can be becuase
1307 // we have manually loaded some sections with "target modules load ..."
1308 // or because we have have a live process that has sections loaded
1309 // through the dynamic loader
1310 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1311 }
1312 // We weren't able to resolve the pointer value, so just return
1313 // an address with no section
1314 if (!pointer_addr.IsValid())
1315 pointer_addr.SetOffset (pointer_vm_addr);
1316 return true;
1317
1318 }
1319 }
1320 return false;
1321}
Chris Lattner24943d22010-06-08 16:52:24 +00001322
1323ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001324Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001325{
Chris Lattner24943d22010-06-08 16:52:24 +00001326 ModuleSP module_sp;
1327
Chris Lattner24943d22010-06-08 16:52:24 +00001328 Error error;
1329
Jim Ingham03e5e512012-05-17 18:38:42 +00001330 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1331 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001332
Jim Ingham03e5e512012-05-17 18:38:42 +00001333 if (module_spec.GetUUID().IsValid())
1334 module_sp = m_images.FindFirstModule(module_spec);
1335
Greg Claytone1ef1e32012-04-27 00:58:27 +00001336 if (!module_sp)
1337 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001338 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1339 bool did_create_module = false;
1340
1341 // If there are image search path entries, try to use them first to acquire a suitable image.
1342 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001343 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001344 ModuleSpec transformed_spec (module_spec);
1345 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1346 {
1347 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1348 error = ModuleList::GetSharedModule (transformed_spec,
1349 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001350 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001351 &old_module_sp,
1352 &did_create_module);
1353 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001354 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001355
Greg Claytone1ef1e32012-04-27 00:58:27 +00001356 if (!module_sp)
1357 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001358 // If we have a UUID, we can check our global shared module list in case
1359 // we already have it. If we don't have a valid UUID, then we can't since
1360 // the path in "module_spec" will be a platform path, and we will need to
1361 // let the platform find that file. For example, we could be asking for
1362 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1363 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1364 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1365 // cache.
1366 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001367 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001368 // We have a UUID, it is OK to check the global module list...
1369 error = ModuleList::GetSharedModule (module_spec,
1370 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001371 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001372 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001373 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001374 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001375
1376 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001377 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001378 // The platform is responsible for finding and caching an appropriate
1379 // module in the shared module cache.
1380 if (m_platform_sp)
1381 {
1382 FileSpec platform_file_spec;
1383 error = m_platform_sp->GetSharedModule (module_spec,
1384 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001385 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001386 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001387 &did_create_module);
1388 }
1389 else
1390 {
1391 error.SetErrorString("no platform is currently set");
1392 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001393 }
1394 }
Chris Lattner24943d22010-06-08 16:52:24 +00001395
Jim Ingham03e5e512012-05-17 18:38:42 +00001396 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1397 // module in the list already, and if there was, let's remove it.
1398 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001399 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001400 // GetSharedModule is not guaranteed to find the old shared module, for instance
1401 // in the common case where you pass in the UUID, it is only going to find the one
1402 // module matching the UUID. In fact, it has no good way to know what the "old module"
1403 // relevant to this target is, since there might be many copies of a module with this file spec
1404 // in various running debug sessions, but only one of them will belong to this target.
1405 // So let's remove the UUID from the module list, and look in the target's module list.
1406 // Only do this if there is SOMETHING else in the module spec...
1407 if (!old_module_sp)
1408 {
1409 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
1410 {
1411 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1412 module_spec_copy.GetUUID().Clear();
1413
1414 ModuleList found_modules;
1415 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1416 if (num_found == 1)
1417 {
1418 old_module_sp = found_modules.GetModuleAtIndex(0);
1419 }
1420 }
1421 }
1422
1423 m_images.Append (module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001424 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
Jim Ingham03e5e512012-05-17 18:38:42 +00001425 {
Chris Lattner24943d22010-06-08 16:52:24 +00001426 ModuleUpdated(old_module_sp, module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001427 m_images.Remove (old_module_sp);
1428 Module *old_module_ptr = old_module_sp.get();
1429 old_module_sp.reset();
1430 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1431 }
Chris Lattner24943d22010-06-08 16:52:24 +00001432 else
1433 ModuleAdded(module_sp);
1434 }
1435 }
1436 if (error_ptr)
1437 *error_ptr = error;
1438 return module_sp;
1439}
1440
1441
Greg Clayton289afcb2012-02-18 05:35:26 +00001442TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001443Target::CalculateTarget ()
1444{
Greg Clayton289afcb2012-02-18 05:35:26 +00001445 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001446}
1447
Greg Clayton289afcb2012-02-18 05:35:26 +00001448ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001449Target::CalculateProcess ()
1450{
Greg Clayton289afcb2012-02-18 05:35:26 +00001451 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001452}
1453
Greg Clayton289afcb2012-02-18 05:35:26 +00001454ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001455Target::CalculateThread ()
1456{
Greg Clayton289afcb2012-02-18 05:35:26 +00001457 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001458}
1459
Greg Clayton289afcb2012-02-18 05:35:26 +00001460StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001461Target::CalculateStackFrame ()
1462{
Greg Clayton289afcb2012-02-18 05:35:26 +00001463 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001464}
1465
1466void
Greg Claytona830adb2010-10-04 01:05:56 +00001467Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001468{
Greg Clayton567e7f32011-09-22 04:58:26 +00001469 exe_ctx.Clear();
1470 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001471}
1472
1473PathMappingList &
1474Target::GetImageSearchPathList ()
1475{
1476 return m_image_search_paths;
1477}
1478
1479void
1480Target::ImageSearchPathsChanged
1481(
1482 const PathMappingList &path_list,
1483 void *baton
1484)
1485{
1486 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001487 ModuleSP exe_module_sp (target->GetExecutableModule());
1488 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001489 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001490 target->m_images.Clear();
1491 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001492 }
1493}
1494
1495ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001496Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001497{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001498 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001499 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001500 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001501 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001502 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001503 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1504 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1505 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1506 }
Chris Lattner24943d22010-06-08 16:52:24 +00001507 return m_scratch_ast_context_ap.get();
1508}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001509
Sean Callanan4938bd62011-11-16 18:20:47 +00001510ClangASTImporter *
1511Target::GetClangASTImporter()
1512{
1513 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1514
1515 if (!ast_importer)
1516 {
1517 ast_importer = new ClangASTImporter();
1518 m_ast_importer_ap.reset(ast_importer);
1519 }
1520
1521 return ast_importer;
1522}
1523
Greg Clayton990de7b2010-11-18 23:32:35 +00001524void
Caroline Tice2a456812011-03-10 22:14:10 +00001525Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001526{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001527 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001528}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001529
Greg Clayton990de7b2010-11-18 23:32:35 +00001530void
Caroline Tice2a456812011-03-10 22:14:10 +00001531Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001532{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001533 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001534}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001535
Greg Clayton9ce95382012-02-13 23:10:39 +00001536FileSpecList
1537Target::GetDefaultExecutableSearchPaths ()
1538{
Greg Clayton73844aa2012-08-22 17:17:09 +00001539 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1540 if (properties_sp)
1541 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001542 return FileSpecList();
1543}
1544
Caroline Tice5bc8c972010-09-20 20:44:43 +00001545ArchSpec
1546Target::GetDefaultArchitecture ()
1547{
Greg Clayton73844aa2012-08-22 17:17:09 +00001548 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1549 if (properties_sp)
1550 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001551 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001552}
1553
1554void
Greg Clayton73844aa2012-08-22 17:17:09 +00001555Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001556{
Greg Clayton73844aa2012-08-22 17:17:09 +00001557 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1558 if (properties_sp)
1559 return properties_sp->SetDefaultArchitecture(arch);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001560}
1561
Greg Claytona830adb2010-10-04 01:05:56 +00001562Target *
1563Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1564{
1565 // The target can either exist in the "process" of ExecutionContext, or in
1566 // the "target_sp" member of SymbolContext. This accessor helper function
1567 // will get the target from one of these locations.
1568
1569 Target *target = NULL;
1570 if (sc_ptr != NULL)
1571 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001572 if (target == NULL && exe_ctx_ptr)
1573 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001574 return target;
1575}
1576
Greg Clayton427f2902010-12-14 02:59:59 +00001577ExecutionResults
1578Target::EvaluateExpression
1579(
1580 const char *expr_cstr,
1581 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001582 lldb_private::ExecutionPolicy execution_policy,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001583 bool coerce_to_id,
Greg Clayton427f2902010-12-14 02:59:59 +00001584 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001585 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001586 lldb::DynamicValueType use_dynamic,
Enrico Granata6cca9692012-07-16 23:10:35 +00001587 lldb::ValueObjectSP &result_valobj_sp,
1588 uint32_t single_thread_timeout_usec
Greg Clayton427f2902010-12-14 02:59:59 +00001589)
1590{
1591 ExecutionResults execution_results = eExecutionSetupError;
1592
1593 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001594
1595 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1596 return execution_results;
1597
Jim Ingham3613ae12011-05-12 02:06:14 +00001598 // We shouldn't run stop hooks in expressions.
1599 // Be sure to reset this if you return anywhere within this function.
1600 bool old_suppress_value = m_suppress_stop_hooks;
1601 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001602
1603 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001604
1605 const size_t expr_cstr_len = ::strlen (expr_cstr);
1606
Greg Clayton427f2902010-12-14 02:59:59 +00001607 if (frame)
1608 {
1609 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001610 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001611 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001612 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1613 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001614 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001615
1616 // Make sure we don't have any things that we know a variable expression
1617 // won't be able to deal with before calling into it
1618 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1619 {
1620 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1621 use_dynamic,
1622 expr_path_options,
1623 var_sp,
1624 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001625 // if this expression results in a bitfield, we give up and let the IR handle it
1626 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1627 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001628 }
Greg Clayton427f2902010-12-14 02:59:59 +00001629 }
1630 else if (m_process_sp)
1631 {
1632 m_process_sp->CalculateExecutionContext(exe_ctx);
1633 }
1634 else
1635 {
1636 CalculateExecutionContext(exe_ctx);
1637 }
1638
1639 if (result_valobj_sp)
1640 {
1641 execution_results = eExecutionCompleted;
1642 // We got a result from the frame variable expression path above...
1643 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1644
1645 lldb::ValueObjectSP const_valobj_sp;
1646
1647 // Check in case our value is already a constant value
1648 if (result_valobj_sp->GetIsConstant())
1649 {
1650 const_valobj_sp = result_valobj_sp;
1651 const_valobj_sp->SetName (persistent_variable_name);
1652 }
1653 else
Jim Inghame41494a2011-04-16 00:01:13 +00001654 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001655 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001656 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001657 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001658 if (dynamic_sp)
1659 result_valobj_sp = dynamic_sp;
1660 }
1661
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001662 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001663 }
Greg Clayton427f2902010-12-14 02:59:59 +00001664
Sean Callanan6a925532011-01-13 08:53:35 +00001665 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1666
Greg Clayton427f2902010-12-14 02:59:59 +00001667 result_valobj_sp = const_valobj_sp;
1668
Sean Callanan6a925532011-01-13 08:53:35 +00001669 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1670 assert (clang_expr_variable_sp.get());
1671
1672 // Set flags and live data as appropriate
1673
1674 const Value &result_value = live_valobj_sp->GetValue();
1675
1676 switch (result_value.GetValueType())
1677 {
1678 case Value::eValueTypeHostAddress:
1679 case Value::eValueTypeFileAddress:
1680 // we don't do anything with these for now
1681 break;
1682 case Value::eValueTypeScalar:
1683 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1684 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1685 break;
1686 case Value::eValueTypeLoadAddress:
1687 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1688 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1689 break;
1690 }
Greg Clayton427f2902010-12-14 02:59:59 +00001691 }
1692 else
1693 {
1694 // Make sure we aren't just trying to see the value of a persistent
1695 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001696 lldb::ClangExpressionVariableSP persistent_var_sp;
1697 // Only check for persistent variables the expression starts with a '$'
1698 if (expr_cstr[0] == '$')
1699 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1700
Greg Clayton427f2902010-12-14 02:59:59 +00001701 if (persistent_var_sp)
1702 {
1703 result_valobj_sp = persistent_var_sp->GetValueObject ();
1704 execution_results = eExecutionCompleted;
1705 }
1706 else
1707 {
1708 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001709
Greg Clayton427f2902010-12-14 02:59:59 +00001710 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001711 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001712 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001713 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan6a925532011-01-13 08:53:35 +00001714 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001715 expr_cstr,
1716 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001717 result_valobj_sp,
1718 single_thread_timeout_usec);
Greg Clayton427f2902010-12-14 02:59:59 +00001719 }
1720 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001721
1722 m_suppress_stop_hooks = old_suppress_value;
1723
Greg Clayton427f2902010-12-14 02:59:59 +00001724 return execution_results;
1725}
1726
Greg Claytonc0fa5332011-05-22 22:46:53 +00001727lldb::addr_t
1728Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1729{
1730 addr_t code_addr = load_addr;
1731 switch (m_arch.GetMachine())
1732 {
1733 case llvm::Triple::arm:
1734 case llvm::Triple::thumb:
1735 switch (addr_class)
1736 {
1737 case eAddressClassData:
1738 case eAddressClassDebug:
1739 return LLDB_INVALID_ADDRESS;
1740
1741 case eAddressClassUnknown:
1742 case eAddressClassInvalid:
1743 case eAddressClassCode:
1744 case eAddressClassCodeAlternateISA:
1745 case eAddressClassRuntime:
1746 // Check if bit zero it no set?
1747 if ((code_addr & 1ull) == 0)
1748 {
1749 // Bit zero isn't set, check if the address is a multiple of 2?
1750 if (code_addr & 2ull)
1751 {
1752 // The address is a multiple of 2 so it must be thumb, set bit zero
1753 code_addr |= 1ull;
1754 }
1755 else if (addr_class == eAddressClassCodeAlternateISA)
1756 {
1757 // We checked the address and the address claims to be the alternate ISA
1758 // which means thumb, so set bit zero.
1759 code_addr |= 1ull;
1760 }
1761 }
1762 break;
1763 }
1764 break;
1765
1766 default:
1767 break;
1768 }
1769 return code_addr;
1770}
1771
1772lldb::addr_t
1773Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1774{
1775 addr_t opcode_addr = load_addr;
1776 switch (m_arch.GetMachine())
1777 {
1778 case llvm::Triple::arm:
1779 case llvm::Triple::thumb:
1780 switch (addr_class)
1781 {
1782 case eAddressClassData:
1783 case eAddressClassDebug:
1784 return LLDB_INVALID_ADDRESS;
1785
1786 case eAddressClassInvalid:
1787 case eAddressClassUnknown:
1788 case eAddressClassCode:
1789 case eAddressClassCodeAlternateISA:
1790 case eAddressClassRuntime:
1791 opcode_addr &= ~(1ull);
1792 break;
1793 }
1794 break;
1795
1796 default:
1797 break;
1798 }
1799 return opcode_addr;
1800}
1801
Jim Inghamd60d94a2011-03-11 03:53:59 +00001802lldb::user_id_t
1803Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1804{
1805 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001806 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001807 m_stop_hooks[new_uid] = new_hook_sp;
1808 return new_uid;
1809}
1810
1811bool
1812Target::RemoveStopHookByID (lldb::user_id_t user_id)
1813{
1814 size_t num_removed;
1815 num_removed = m_stop_hooks.erase (user_id);
1816 if (num_removed == 0)
1817 return false;
1818 else
1819 return true;
1820}
1821
1822void
1823Target::RemoveAllStopHooks ()
1824{
1825 m_stop_hooks.clear();
1826}
1827
1828Target::StopHookSP
1829Target::GetStopHookByID (lldb::user_id_t user_id)
1830{
1831 StopHookSP found_hook;
1832
1833 StopHookCollection::iterator specified_hook_iter;
1834 specified_hook_iter = m_stop_hooks.find (user_id);
1835 if (specified_hook_iter != m_stop_hooks.end())
1836 found_hook = (*specified_hook_iter).second;
1837 return found_hook;
1838}
1839
1840bool
1841Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1842{
1843 StopHookCollection::iterator specified_hook_iter;
1844 specified_hook_iter = m_stop_hooks.find (user_id);
1845 if (specified_hook_iter == m_stop_hooks.end())
1846 return false;
1847
1848 (*specified_hook_iter).second->SetIsActive (active_state);
1849 return true;
1850}
1851
1852void
1853Target::SetAllStopHooksActiveState (bool active_state)
1854{
1855 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1856 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1857 {
1858 (*pos).second->SetIsActive (active_state);
1859 }
1860}
1861
1862void
1863Target::RunStopHooks ()
1864{
Jim Ingham3613ae12011-05-12 02:06:14 +00001865 if (m_suppress_stop_hooks)
1866 return;
1867
Jim Inghamd60d94a2011-03-11 03:53:59 +00001868 if (!m_process_sp)
1869 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001870
1871 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1872 // since in that case we do not want to run the stop-hooks
1873 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1874 return;
1875
Jim Inghamd60d94a2011-03-11 03:53:59 +00001876 if (m_stop_hooks.empty())
1877 return;
1878
1879 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1880
1881 // If there aren't any active stop hooks, don't bother either:
1882 bool any_active_hooks = false;
1883 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1884 {
1885 if ((*pos).second->IsActive())
1886 {
1887 any_active_hooks = true;
1888 break;
1889 }
1890 }
1891 if (!any_active_hooks)
1892 return;
1893
1894 CommandReturnObject result;
1895
1896 std::vector<ExecutionContext> exc_ctx_with_reasons;
1897 std::vector<SymbolContext> sym_ctx_with_reasons;
1898
1899 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1900 size_t num_threads = cur_threadlist.GetSize();
1901 for (size_t i = 0; i < num_threads; i++)
1902 {
1903 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1904 if (cur_thread_sp->ThreadStoppedForAReason())
1905 {
1906 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1907 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1908 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1909 }
1910 }
1911
1912 // If no threads stopped for a reason, don't run the stop-hooks.
1913 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1914 if (num_exe_ctx == 0)
1915 return;
1916
Jim Inghame5ed8e92011-06-02 23:58:26 +00001917 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1918 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001919
1920 bool keep_going = true;
1921 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001922 bool print_hook_header;
1923 bool print_thread_header;
1924
1925 if (num_exe_ctx == 1)
1926 print_thread_header = false;
1927 else
1928 print_thread_header = true;
1929
1930 if (m_stop_hooks.size() == 1)
1931 print_hook_header = false;
1932 else
1933 print_hook_header = true;
1934
Jim Inghamd60d94a2011-03-11 03:53:59 +00001935 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1936 {
1937 // result.Clear();
1938 StopHookSP cur_hook_sp = (*pos).second;
1939 if (!cur_hook_sp->IsActive())
1940 continue;
1941
1942 bool any_thread_matched = false;
1943 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1944 {
1945 if ((cur_hook_sp->GetSpecifier () == NULL
1946 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1947 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00001948 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001949 {
1950 if (!hooks_ran)
1951 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001952 hooks_ran = true;
1953 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001954 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001955 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001956 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1957 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1958 NULL);
1959 if (cmd)
1960 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1961 else
1962 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001963 any_thread_matched = true;
1964 }
1965
Jim Inghamc54840c2011-03-22 01:47:27 +00001966 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001967 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001968
1969 bool stop_on_continue = true;
1970 bool stop_on_error = true;
1971 bool echo_commands = false;
1972 bool print_results = true;
1973 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001974 &exc_ctx_with_reasons[i],
1975 stop_on_continue,
1976 stop_on_error,
1977 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00001978 print_results,
1979 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00001980 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001981
1982 // If the command started the target going again, we should bag out of
1983 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001984 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1985 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001986 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001987 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001988 keep_going = false;
1989 }
1990 }
1991 }
1992 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00001993
Caroline Tice4a348082011-05-02 20:41:46 +00001994 result.GetImmediateOutputStream()->Flush();
1995 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001996}
1997
Greg Claytonbbea1332011-07-08 00:48:09 +00001998
Jim Inghamd60d94a2011-03-11 03:53:59 +00001999//--------------------------------------------------------------
2000// class Target::StopHook
2001//--------------------------------------------------------------
2002
2003
2004Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2005 UserID (uid),
2006 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002007 m_commands (),
2008 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002009 m_thread_spec_ap(NULL),
2010 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002011{
2012}
2013
2014Target::StopHook::StopHook (const StopHook &rhs) :
2015 UserID (rhs.GetID()),
2016 m_target_sp (rhs.m_target_sp),
2017 m_commands (rhs.m_commands),
2018 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002019 m_thread_spec_ap (NULL),
2020 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002021{
2022 if (rhs.m_thread_spec_ap.get() != NULL)
2023 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2024}
2025
2026
2027Target::StopHook::~StopHook ()
2028{
2029}
2030
2031void
2032Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2033{
2034 m_thread_spec_ap.reset (specifier);
2035}
2036
2037
2038void
2039Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2040{
2041 int indent_level = s->GetIndentLevel();
2042
2043 s->SetIndentLevel(indent_level + 2);
2044
Greg Clayton444e35b2011-10-19 18:09:39 +00002045 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002046 if (m_active)
2047 s->Indent ("State: enabled\n");
2048 else
2049 s->Indent ("State: disabled\n");
2050
2051 if (m_specifier_sp)
2052 {
2053 s->Indent();
2054 s->PutCString ("Specifier:\n");
2055 s->SetIndentLevel (indent_level + 4);
2056 m_specifier_sp->GetDescription (s, level);
2057 s->SetIndentLevel (indent_level + 2);
2058 }
2059
2060 if (m_thread_spec_ap.get() != NULL)
2061 {
2062 StreamString tmp;
2063 s->Indent("Thread:\n");
2064 m_thread_spec_ap->GetDescription (&tmp, level);
2065 s->SetIndentLevel (indent_level + 4);
2066 s->Indent (tmp.GetData());
2067 s->PutCString ("\n");
2068 s->SetIndentLevel (indent_level + 2);
2069 }
2070
2071 s->Indent ("Commands: \n");
2072 s->SetIndentLevel (indent_level + 4);
2073 uint32_t num_commands = m_commands.GetSize();
2074 for (uint32_t i = 0; i < num_commands; i++)
2075 {
2076 s->Indent(m_commands.GetStringAtIndex(i));
2077 s->PutCString ("\n");
2078 }
2079 s->SetIndentLevel (indent_level);
2080}
2081
Greg Clayton73844aa2012-08-22 17:17:09 +00002082//--------------------------------------------------------------
2083// class TargetProperties
2084//--------------------------------------------------------------
2085
2086OptionEnumValueElement
2087lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002088{
Greg Clayton73844aa2012-08-22 17:17:09 +00002089 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2090 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2091 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2092 { 0, NULL, NULL }
2093};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002094
Greg Clayton73844aa2012-08-22 17:17:09 +00002095static PropertyDefinition
2096g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002097{
Greg Clayton73844aa2012-08-22 17:17:09 +00002098 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2099 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2100 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2101 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2102 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2103 { "source-map" , OptionValue::eTypePathMap , false, 0 , NULL, NULL, "Source path remappings used to track the change of location between a source file when built, and "
2104 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2105 "some part (starting at the root) of the path to the file when it was built, "
2106 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2107 "Each element of the array is checked in order and the first one that results in a match wins." },
2108 { "exec-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "Executable search paths to use when locating executable files whose paths don't match the local file system." },
2109 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2110 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2111 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." },
2112 { "run-args" , OptionValue::eTypeArgs , false, 0 , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run." },
2113 { "env-vars" , OptionValue::eTypeDictionary, false, OptionValue::eTypeString , NULL, NULL, "A list of all the environment variables to be passed to the executable's environment, and their values." },
2114 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2115 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2116 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2117 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2118 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2119 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Disable stdin/stdout for process (e.g. for a GUI application)" },
2120 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2121};
2122enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002123{
Greg Clayton73844aa2012-08-22 17:17:09 +00002124 ePropertyDefaultArch,
2125 ePropertyExprPrefix,
2126 ePropertyPreferDynamic,
2127 ePropertyEnableSynthetic,
2128 ePropertySkipPrologue,
2129 ePropertySourceMap,
2130 ePropertyExecutableSearchPaths,
2131 ePropertyMaxChildrenCount,
2132 ePropertyMaxSummaryLength,
2133 ePropertyBreakpointUseAvoidList,
2134 ePropertyRunArgs,
2135 ePropertyEnvVars,
2136 ePropertyInheritEnv,
2137 ePropertyInputPath,
2138 ePropertyOutputPath,
2139 ePropertyErrorPath,
2140 ePropertyDisableASLR,
2141 ePropertyDisableSTDIO
2142};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002143
Caroline Tice5bc8c972010-09-20 20:44:43 +00002144
Greg Clayton73844aa2012-08-22 17:17:09 +00002145class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002146{
Greg Clayton73844aa2012-08-22 17:17:09 +00002147public:
2148 TargetOptionValueProperties (const ConstString &name) :
2149 OptionValueProperties (name),
2150 m_target (NULL),
2151 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002152 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002153 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002154
Greg Clayton73844aa2012-08-22 17:17:09 +00002155 // This constructor is used when creating TargetOptionValueProperties when it
2156 // is part of a new lldb_private::Target instance. It will copy all current
2157 // global property values as needed
2158 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2159 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2160 m_target (target),
2161 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002162 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002163 }
2164
2165 virtual const Property *
2166 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2167 {
2168 // When gettings the value for a key from the target options, we will always
2169 // try and grab the setting from the current target if there is one. Else we just
2170 // use the one from this instance.
2171 if (idx == ePropertyEnvVars)
2172 GetHostEnvironmentIfNeeded ();
2173
2174 if (exe_ctx)
2175 {
2176 Target *target = exe_ctx->GetTargetPtr();
2177 if (target)
2178 {
2179 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2180 if (this != target_properties)
2181 return target_properties->ProtectedGetPropertyAtIndex (idx);
2182 }
2183 }
2184 return ProtectedGetPropertyAtIndex (idx);
2185 }
2186protected:
2187
2188 void
2189 GetHostEnvironmentIfNeeded () const
2190 {
2191 if (!m_got_host_env)
2192 {
2193 if (m_target)
2194 {
2195 m_got_host_env = true;
2196 const uint32_t idx = ePropertyInheritEnv;
2197 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2198 {
2199 PlatformSP platform_sp (m_target->GetPlatform());
2200 if (platform_sp)
2201 {
2202 StringList env;
2203 if (platform_sp->GetEnvironment(env))
2204 {
2205 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2206 if (env_dict)
2207 {
2208 const bool can_replace = false;
2209 const size_t envc = env.GetSize();
2210 for (size_t idx=0; idx<envc; idx++)
2211 {
2212 const char *env_entry = env.GetStringAtIndex (idx);
2213 if (env_entry)
2214 {
2215 const char *equal_pos = ::strchr(env_entry, '=');
2216 ConstString key;
2217 // It is ok to have environment variables with no values
2218 const char *value = NULL;
2219 if (equal_pos)
2220 {
2221 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2222 if (equal_pos[1])
2223 value = equal_pos + 1;
2224 }
2225 else
2226 {
2227 key.SetCString(env_entry);
2228 }
2229 // Don't allow existing keys to be replaced with ones we get from the platform environment
2230 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2231 }
2232 }
2233 }
2234 }
2235 }
2236 }
2237 }
2238 }
2239 }
2240 Target *m_target;
2241 mutable bool m_got_host_env;
2242};
2243
2244TargetProperties::TargetProperties (Target *target) :
2245 Properties ()
2246{
2247 if (target)
2248 {
2249 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002250 }
2251 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002252 {
2253 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2254 m_collection_sp->Initialize(g_properties);
2255 m_collection_sp->AppendProperty(ConstString("process"),
2256 ConstString("Settings specify to processes."),
2257 true,
2258 Process::GetGlobalProperties()->GetValueProperties());
2259 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002260}
2261
Greg Clayton73844aa2012-08-22 17:17:09 +00002262TargetProperties::~TargetProperties ()
2263{
2264}
2265ArchSpec
2266TargetProperties::GetDefaultArchitecture () const
2267{
2268 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2269 if (value)
2270 return value->GetCurrentValue();
2271 return ArchSpec();
2272}
2273
2274void
2275TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2276{
2277 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2278 if (value)
2279 return value->SetCurrentValue(arch, true);
2280}
2281
2282lldb::DynamicValueType
2283TargetProperties::GetPreferDynamicValue() const
2284{
2285 const uint32_t idx = ePropertyPreferDynamic;
2286 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2287}
2288
2289bool
2290TargetProperties::GetDisableASLR () const
2291{
2292 const uint32_t idx = ePropertyDisableASLR;
2293 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2294}
2295
2296void
2297TargetProperties::SetDisableASLR (bool b)
2298{
2299 const uint32_t idx = ePropertyDisableASLR;
2300 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2301}
2302
2303bool
2304TargetProperties::GetDisableSTDIO () const
2305{
2306 const uint32_t idx = ePropertyDisableSTDIO;
2307 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2308}
2309
2310void
2311TargetProperties::SetDisableSTDIO (bool b)
2312{
2313 const uint32_t idx = ePropertyDisableSTDIO;
2314 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2315}
2316
2317bool
2318TargetProperties::GetRunArguments (Args &args) const
2319{
2320 const uint32_t idx = ePropertyRunArgs;
2321 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2322}
2323
2324void
2325TargetProperties::SetRunArguments (const Args &args)
2326{
2327 const uint32_t idx = ePropertyRunArgs;
2328 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2329}
2330
2331size_t
2332TargetProperties::GetEnvironmentAsArgs (Args &env) const
2333{
2334 const uint32_t idx = ePropertyEnvVars;
2335 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2336}
2337
2338bool
2339TargetProperties::GetSkipPrologue() const
2340{
2341 const uint32_t idx = ePropertySkipPrologue;
2342 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2343}
2344
2345PathMappingList &
2346TargetProperties::GetSourcePathMap () const
2347{
2348 const uint32_t idx = ePropertySourceMap;
2349 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2350 assert(option_value);
2351 return option_value->GetCurrentValue();
2352}
2353
2354FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002355TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002356{
2357 const uint32_t idx = ePropertyExecutableSearchPaths;
2358 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2359 assert(option_value);
2360 return option_value->GetCurrentValue();
2361}
2362
2363bool
2364TargetProperties::GetEnableSyntheticValue () const
2365{
2366 const uint32_t idx = ePropertyEnableSynthetic;
2367 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2368}
2369
2370uint32_t
2371TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2372{
2373 const uint32_t idx = ePropertyMaxChildrenCount;
2374 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2375}
2376
2377uint32_t
2378TargetProperties::GetMaximumSizeOfStringSummary() const
2379{
2380 const uint32_t idx = ePropertyMaxSummaryLength;
2381 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2382}
2383
2384FileSpec
2385TargetProperties::GetStandardInputPath () const
2386{
2387 const uint32_t idx = ePropertyInputPath;
2388 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2389}
2390
2391void
2392TargetProperties::SetStandardInputPath (const char *p)
2393{
2394 const uint32_t idx = ePropertyInputPath;
2395 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2396}
2397
2398FileSpec
2399TargetProperties::GetStandardOutputPath () const
2400{
2401 const uint32_t idx = ePropertyOutputPath;
2402 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2403}
2404
2405void
2406TargetProperties::SetStandardOutputPath (const char *p)
2407{
2408 const uint32_t idx = ePropertyOutputPath;
2409 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2410}
2411
2412FileSpec
2413TargetProperties::GetStandardErrorPath () const
2414{
2415 const uint32_t idx = ePropertyErrorPath;
2416 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2417}
2418
Greg Claytonc6e82e42012-08-22 18:39:03 +00002419const char *
2420TargetProperties::GetExpressionPrefixContentsAsCString ()
2421{
2422 const uint32_t idx = ePropertyExprPrefix;
2423 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2424 if (file)
2425 return (const char *)file->GetFileContents()->GetBytes();
2426 return NULL;
2427}
2428
Greg Clayton73844aa2012-08-22 17:17:09 +00002429void
2430TargetProperties::SetStandardErrorPath (const char *p)
2431{
2432 const uint32_t idx = ePropertyErrorPath;
2433 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2434}
2435
2436bool
2437TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2438{
2439 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2440 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2441}
2442
2443const TargetPropertiesSP &
2444Target::GetGlobalProperties()
2445{
2446 static TargetPropertiesSP g_settings_sp;
2447 if (!g_settings_sp)
2448 {
2449 g_settings_sp.reset (new TargetProperties (NULL));
2450 }
2451 return g_settings_sp;
2452}
2453
Jim Ingham5a15e692012-02-16 06:50:00 +00002454const ConstString &
2455Target::TargetEventData::GetFlavorString ()
2456{
2457 static ConstString g_flavor ("Target::TargetEventData");
2458 return g_flavor;
2459}
2460
2461const ConstString &
2462Target::TargetEventData::GetFlavor () const
2463{
2464 return TargetEventData::GetFlavorString ();
2465}
2466
2467Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2468 EventData(),
2469 m_target_sp (new_target_sp)
2470{
2471}
2472
2473Target::TargetEventData::~TargetEventData()
2474{
2475
2476}
2477
2478void
2479Target::TargetEventData::Dump (Stream *s) const
2480{
2481
2482}
2483
2484const TargetSP
2485Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2486{
2487 TargetSP target_sp;
2488
2489 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2490 if (data)
2491 target_sp = data->m_target_sp;
2492
2493 return target_sp;
2494}
2495
2496const Target::TargetEventData *
2497Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2498{
2499 if (event_ptr)
2500 {
2501 const EventData *event_data = event_ptr->GetData();
2502 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2503 return static_cast <const TargetEventData *> (event_ptr->GetData());
2504 }
2505 return NULL;
2506}
2507