blob: 2298a1fa3a0b00d4d25676a989e7137cc23a91a3 [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"
Greg Clayton49ce8962012-08-29 21:13:06 +000025#include "lldb/Core/Module.h"
26#include "lldb/Core/ModuleSpec.h"
27#include "lldb/Core/Section.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000029#include "lldb/Core/Timer.h"
30#include "lldb/Core/ValueObject.h"
Sean Callanandcf03f82011-11-15 22:27:19 +000031#include "lldb/Expression/ClangASTSource.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000032#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000034#include "lldb/Interpreter/CommandInterpreter.h"
35#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chen3f883492012-06-04 23:19:54 +000036#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton73844aa2012-08-22 17:17:09 +000037#include "lldb/Interpreter/OptionValues.h"
38#include "lldb/Interpreter/Property.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039#include "lldb/lldb-private-log.h"
40#include "lldb/Symbol/ObjectFile.h"
41#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000042#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000043#include "lldb/Target/Thread.h"
44#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000045
46using namespace lldb;
47using namespace lldb_private;
48
Jim Ingham5a15e692012-02-16 06:50:00 +000049ConstString &
50Target::GetStaticBroadcasterClass ()
51{
52 static ConstString class_name ("lldb.target");
53 return class_name;
54}
55
Chris Lattner24943d22010-06-08 16:52:24 +000056//----------------------------------------------------------------------
57// Target constructor
58//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000059Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton73844aa2012-08-22 17:17:09 +000060 TargetProperties (this),
Jim Ingham5a15e692012-02-16 06:50:00 +000061 Broadcaster (&debugger, "lldb.target"),
Greg Clayton24bc5d92011-03-30 18:16:51 +000062 ExecutionContextScope (),
Greg Clayton63094e02010-06-23 01:19:29 +000063 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000064 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000065 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000066 m_arch (target_arch),
67 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000068 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000069 m_breakpoint_list (false),
70 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000071 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000072 m_process_sp (),
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +000073 m_valid (true),
Greg Clayton24bc5d92011-03-30 18:16:51 +000074 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000075 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000076 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000077 m_scratch_ast_source_ap (NULL),
78 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000079 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000080 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000081 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000082 m_stop_hook_next_id (0),
Enrico Granatadba1de82012-03-27 02:35:13 +000083 m_suppress_stop_hooks (false),
84 m_suppress_synthetic_value(false)
Chris Lattner24943d22010-06-08 16:52:24 +000085{
Greg Clayton49ce6822010-10-31 03:01:06 +000086 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
87 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
88 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000089
90 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000091
Greg Claytone005f2c2010-11-06 01:53:30 +000092 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000093 if (log)
94 log->Printf ("%p Target::Target()", this);
95}
96
97//----------------------------------------------------------------------
98// Destructor
99//----------------------------------------------------------------------
100Target::~Target()
101{
Greg Claytone005f2c2010-11-06 01:53:30 +0000102 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000103 if (log)
104 log->Printf ("%p Target::~Target()", this);
105 DeleteCurrentProcess ();
106}
107
108void
Caroline Tice7826c882010-10-26 03:11:13 +0000109Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000110{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000111// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000112 if (description_level != lldb::eDescriptionLevelBrief)
113 {
114 s->Indent();
115 s->PutCString("Target\n");
116 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000117 m_images.Dump(s);
118 m_breakpoint_list.Dump(s);
119 m_internal_breakpoint_list.Dump(s);
120 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000121 }
122 else
123 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000124 Module *exe_module = GetExecutableModulePointer();
125 if (exe_module)
126 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000127 else
128 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000129 }
Chris Lattner24943d22010-06-08 16:52:24 +0000130}
131
132void
133Target::DeleteCurrentProcess ()
134{
135 if (m_process_sp.get())
136 {
Greg Clayton49480b12010-09-14 23:52:43 +0000137 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000138 if (m_process_sp->IsAlive())
139 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000140
141 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000142
143 // Do any cleanup of the target we need to do between process instances.
144 // NB It is better to do this before destroying the process in case the
145 // clean up needs some help from the process.
146 m_breakpoint_list.ClearAllBreakpointSites();
147 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000148 // Disable watchpoints just on the debugger side.
Johnny Chen116a5cd2012-02-25 06:44:30 +0000149 Mutex::Locker locker;
150 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000151 DisableAllWatchpoints(false);
Johnny Chen116a5cd2012-02-25 06:44:30 +0000152 ClearAllWatchpointHitCounts();
Chris Lattner24943d22010-06-08 16:52:24 +0000153 m_process_sp.reset();
154 }
155}
156
157const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000158Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000159{
160 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000161 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000162 return m_process_sp;
163}
164
165const lldb::ProcessSP &
166Target::GetProcessSP () const
167{
168 return m_process_sp;
169}
170
Greg Clayton153ccd72011-08-10 02:10:13 +0000171void
172Target::Destroy()
173{
174 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000175 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000176 DeleteCurrentProcess ();
177 m_platform_sp.reset();
178 m_arch.Clear();
179 m_images.Clear();
180 m_section_load_list.Clear();
181 const bool notify = false;
182 m_breakpoint_list.RemoveAll(notify);
183 m_internal_breakpoint_list.RemoveAll(notify);
184 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000185 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000186 m_search_filter_sp.reset();
187 m_image_search_paths.Clear(notify);
188 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000189 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000190 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000191 m_persistent_variables.Clear();
192 m_stop_hooks.clear();
193 m_stop_hook_next_id = 0;
194 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000195 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000196}
197
198
Chris Lattner24943d22010-06-08 16:52:24 +0000199BreakpointList &
200Target::GetBreakpointList(bool internal)
201{
202 if (internal)
203 return m_internal_breakpoint_list;
204 else
205 return m_breakpoint_list;
206}
207
208const BreakpointList &
209Target::GetBreakpointList(bool internal) const
210{
211 if (internal)
212 return m_internal_breakpoint_list;
213 else
214 return m_breakpoint_list;
215}
216
217BreakpointSP
218Target::GetBreakpointByID (break_id_t break_id)
219{
220 BreakpointSP bp_sp;
221
222 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
223 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
224 else
225 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
226
227 return bp_sp;
228}
229
230BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000231Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton49ce8962012-08-29 21:13:06 +0000232 const FileSpecList *source_file_spec_list,
233 RegularExpression &source_regex,
234 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000235{
Jim Inghamd6d47972011-09-23 00:54:11 +0000236 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
237 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000238 return CreateBreakpoint (filter_sp, resolver_sp, internal);
239}
240
241
242BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000243Target::CreateBreakpoint (const FileSpecList *containingModules,
244 const FileSpec &file,
245 uint32_t line_no,
Greg Clayton49ce8962012-08-29 21:13:06 +0000246 LazyBool check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000247 LazyBool skip_prologue,
248 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000249{
250 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000251
Greg Clayton49ce8962012-08-29 21:13:06 +0000252 if (check_inlines == eLazyBoolCalculate)
253 {
254 const InlineStrategy inline_strategy = GetInlineStrategy();
255 switch (inline_strategy)
256 {
257 case eInlineBreakpointsNever:
258 check_inlines = eLazyBoolNo;
259 break;
260
261 case eInlineBreakpointsHeaders:
262 if (file.IsSourceImplementationFile())
263 check_inlines = eLazyBoolNo;
264 else
265 check_inlines = eLazyBoolYes;
266 break;
267
268 case eInlineBreakpointsAlways:
269 check_inlines = eLazyBoolYes;
270 break;
271 }
272 }
273 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
274 file,
275 line_no,
276 check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000277 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000278 return CreateBreakpoint (filter_sp, resolver_sp, internal);
279}
280
281
282BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000283Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000284{
Chris Lattner24943d22010-06-08 16:52:24 +0000285 Address so_addr;
286 // Attempt to resolve our load address if possible, though it is ok if
287 // it doesn't resolve to section/offset.
288
Greg Clayton33ed1702010-08-24 00:45:41 +0000289 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000290 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000291 if (!so_addr.IsValid())
292 {
293 // The address didn't resolve, so just set this as an absolute address
294 so_addr.SetOffset (addr);
295 }
296 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000297 return bp_sp;
298}
299
300BreakpointSP
301Target::CreateBreakpoint (Address &addr, bool internal)
302{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000303 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000304 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
305 return CreateBreakpoint (filter_sp, resolver_sp, internal);
306}
307
308BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000309Target::CreateBreakpoint (const FileSpecList *containingModules,
310 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000311 const char *func_name,
312 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000313 LazyBool skip_prologue,
314 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000315{
Greg Clayton12bec712010-06-28 21:30:43 +0000316 BreakpointSP bp_sp;
317 if (func_name)
318 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000319 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000320
321 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
322 func_name,
323 func_name_type_mask,
324 Breakpoint::Exact,
325 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000326 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
327 }
328 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000329}
330
Jim Ingham4722b102012-03-06 00:37:27 +0000331lldb::BreakpointSP
332Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000333 const FileSpecList *containingSourceFiles,
334 const std::vector<std::string> &func_names,
335 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000336 LazyBool skip_prologue,
337 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000338{
339 BreakpointSP bp_sp;
340 size_t num_names = func_names.size();
341 if (num_names > 0)
342 {
343 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
344
345 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
346 func_names,
347 func_name_type_mask,
348 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
349 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
350 }
351 return bp_sp;
352}
353
Jim Inghamc1053622012-03-03 02:05:11 +0000354BreakpointSP
355Target::CreateBreakpoint (const FileSpecList *containingModules,
356 const FileSpecList *containingSourceFiles,
357 const char *func_names[],
358 size_t num_names,
359 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000360 LazyBool skip_prologue,
361 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000362{
363 BreakpointSP bp_sp;
364 if (num_names > 0)
365 {
366 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
367
368 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
369 func_names,
370 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000371 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000372 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
373 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
374 }
375 return bp_sp;
376}
Chris Lattner24943d22010-06-08 16:52:24 +0000377
378SearchFilterSP
379Target::GetSearchFilterForModule (const FileSpec *containingModule)
380{
381 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000382 if (containingModule != NULL)
383 {
384 // TODO: We should look into sharing module based search filters
385 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000386 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000387 }
388 else
389 {
390 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000391 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000392 filter_sp = m_search_filter_sp;
393 }
394 return filter_sp;
395}
396
Jim Ingham03c8ee52011-09-21 01:17:13 +0000397SearchFilterSP
398Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
399{
400 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000401 if (containingModules && containingModules->GetSize() != 0)
402 {
403 // TODO: We should look into sharing module based search filters
404 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000405 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000406 }
407 else
408 {
409 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000410 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000411 filter_sp = m_search_filter_sp;
412 }
413 return filter_sp;
414}
415
Jim Inghamd6d47972011-09-23 00:54:11 +0000416SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000417Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
418 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000419{
420 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
421 return GetSearchFilterForModuleList(containingModules);
422
423 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000424 if (containingModules == NULL)
425 {
426 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
427 // but that will take a little reworking.
428
Greg Clayton13d24fb2012-01-29 20:56:30 +0000429 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000430 }
431 else
432 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000433 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000434 }
435 return filter_sp;
436}
437
Chris Lattner24943d22010-06-08 16:52:24 +0000438BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000439Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000440 const FileSpecList *containingSourceFiles,
441 RegularExpression &func_regex,
442 LazyBool skip_prologue,
443 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000444{
Jim Inghamd6d47972011-09-23 00:54:11 +0000445 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000446 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
447 func_regex,
448 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000449
450 return CreateBreakpoint (filter_sp, resolver_sp, internal);
451}
452
Jim Ingham3df164e2012-03-05 04:47:34 +0000453lldb::BreakpointSP
454Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
455{
456 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
457}
458
Chris Lattner24943d22010-06-08 16:52:24 +0000459BreakpointSP
460Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
461{
462 BreakpointSP bp_sp;
463 if (filter_sp && resolver_sp)
464 {
465 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
466 resolver_sp->SetBreakpoint (bp_sp.get());
467
468 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000469 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000470 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000471 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000472
Greg Claytone005f2c2010-11-06 01:53:30 +0000473 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000474 if (log)
475 {
476 StreamString s;
477 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
478 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
479 }
480
Chris Lattner24943d22010-06-08 16:52:24 +0000481 bp_sp->ResolveBreakpoint();
482 }
Jim Inghamd1686902010-10-14 23:45:03 +0000483
484 if (!internal && bp_sp)
485 {
486 m_last_created_breakpoint = bp_sp;
487 }
488
Chris Lattner24943d22010-06-08 16:52:24 +0000489 return bp_sp;
490}
491
Johnny Chenda5a8022011-09-20 23:28:55 +0000492bool
493Target::ProcessIsValid()
494{
495 return (m_process_sp && m_process_sp->IsAlive());
496}
497
Johnny Chen3f883492012-06-04 23:19:54 +0000498static bool
499CheckIfWatchpointsExhausted(Target *target, Error &error)
500{
501 uint32_t num_supported_hardware_watchpoints;
502 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
503 if (rc.Success())
504 {
505 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
506 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
507 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
508 num_supported_hardware_watchpoints);
509 }
510 return false;
511}
512
Johnny Chenecd4feb2011-10-14 00:42:25 +0000513// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000514// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000515WatchpointSP
Johnny Chen3f883492012-06-04 23:19:54 +0000516Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000517{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000518 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
519 if (log)
520 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
521 __FUNCTION__, addr, size, type);
522
Johnny Chenecd4feb2011-10-14 00:42:25 +0000523 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000524 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000525 {
526 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000527 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000528 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000529 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000530 {
531 if (size == 0)
532 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
533 else
534 error.SetErrorStringWithFormat("invalid watch address: %llu", addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000535 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000536 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000537
Johnny Chenecd4feb2011-10-14 00:42:25 +0000538 // Currently we only support one watchpoint per address, with total number
539 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000540
541 // Grab the list mutex while doing operations.
542 Mutex::Locker locker;
543 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000544 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000545 if (matched_sp)
546 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000547 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000548 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000549 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
550 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000551 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000552 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000553 wp_sp = matched_sp;
554 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000555 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000556 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000557 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000558 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000559 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000560 }
561
Johnny Chenecd4feb2011-10-14 00:42:25 +0000562 if (!wp_sp) {
563 Watchpoint *new_wp = new Watchpoint(addr, size);
564 if (!new_wp) {
565 printf("Watchpoint ctor failed, out of memory?\n");
566 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000567 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000568 new_wp->SetWatchpointType(type);
569 new_wp->SetTarget(this);
570 wp_sp.reset(new_wp);
571 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000572 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000573
Johnny Chen3f883492012-06-04 23:19:54 +0000574 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000575 if (log)
576 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
577 __FUNCTION__,
Johnny Chen3f883492012-06-04 23:19:54 +0000578 error.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000579 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000580
Johnny Chen3f883492012-06-04 23:19:54 +0000581 if (error.Fail()) {
Johnny Chen155599b2012-03-26 22:00:10 +0000582 // Enabling the watchpoint on the device side failed.
583 // Remove the said watchpoint from the list maintained by the target instance.
584 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chen3f883492012-06-04 23:19:54 +0000585 // See if we could provide more helpful error message.
586 if (!CheckIfWatchpointsExhausted(this, error))
587 {
588 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
589 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
590 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000591 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000592 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000593 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000594 m_last_created_watchpoint = wp_sp;
595 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000596}
597
Chris Lattner24943d22010-06-08 16:52:24 +0000598void
599Target::RemoveAllBreakpoints (bool internal_also)
600{
Greg Claytone005f2c2010-11-06 01:53:30 +0000601 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000602 if (log)
603 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
604
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000605 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000606 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000607 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000608
609 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000610}
611
612void
613Target::DisableAllBreakpoints (bool internal_also)
614{
Greg Claytone005f2c2010-11-06 01:53:30 +0000615 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000616 if (log)
617 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
618
619 m_breakpoint_list.SetEnabledAll (false);
620 if (internal_also)
621 m_internal_breakpoint_list.SetEnabledAll (false);
622}
623
624void
625Target::EnableAllBreakpoints (bool internal_also)
626{
Greg Claytone005f2c2010-11-06 01:53:30 +0000627 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000628 if (log)
629 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
630
631 m_breakpoint_list.SetEnabledAll (true);
632 if (internal_also)
633 m_internal_breakpoint_list.SetEnabledAll (true);
634}
635
636bool
637Target::RemoveBreakpointByID (break_id_t break_id)
638{
Greg Claytone005f2c2010-11-06 01:53:30 +0000639 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000640 if (log)
641 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
642
643 if (DisableBreakpointByID (break_id))
644 {
645 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000646 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000647 else
Jim Inghamd1686902010-10-14 23:45:03 +0000648 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000649 if (m_last_created_breakpoint)
650 {
651 if (m_last_created_breakpoint->GetID() == break_id)
652 m_last_created_breakpoint.reset();
653 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000654 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000655 }
Chris Lattner24943d22010-06-08 16:52:24 +0000656 return true;
657 }
658 return false;
659}
660
661bool
662Target::DisableBreakpointByID (break_id_t break_id)
663{
Greg Claytone005f2c2010-11-06 01:53:30 +0000664 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000665 if (log)
666 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
667
668 BreakpointSP bp_sp;
669
670 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
671 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
672 else
673 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
674 if (bp_sp)
675 {
676 bp_sp->SetEnabled (false);
677 return true;
678 }
679 return false;
680}
681
682bool
683Target::EnableBreakpointByID (break_id_t break_id)
684{
Greg Claytone005f2c2010-11-06 01:53:30 +0000685 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000686 if (log)
687 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
688 __FUNCTION__,
689 break_id,
690 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
691
692 BreakpointSP bp_sp;
693
694 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
695 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
696 else
697 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
698
699 if (bp_sp)
700 {
701 bp_sp->SetEnabled (true);
702 return true;
703 }
704 return false;
705}
706
Johnny Chenc86582f2011-09-23 21:21:43 +0000707// The flag 'end_to_end', default to true, signifies that the operation is
708// performed end to end, for both the debugger and the debuggee.
709
Johnny Chenecd4feb2011-10-14 00:42:25 +0000710// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
711// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000712bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000713Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000714{
715 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
716 if (log)
717 log->Printf ("Target::%s\n", __FUNCTION__);
718
Johnny Chenc86582f2011-09-23 21:21:43 +0000719 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000720 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000721 return true;
722 }
723
724 // Otherwise, it's an end to end operation.
725
Johnny Chenda5a8022011-09-20 23:28:55 +0000726 if (!ProcessIsValid())
727 return false;
728
Johnny Chenecd4feb2011-10-14 00:42:25 +0000729 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000730 for (size_t i = 0; i < num_watchpoints; ++i)
731 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000732 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
733 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000734 return false;
735
Johnny Chenecd4feb2011-10-14 00:42:25 +0000736 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000737 if (rc.Fail())
738 return false;
739 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000740 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000741 return true; // Success!
742}
743
Johnny Chenecd4feb2011-10-14 00:42:25 +0000744// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
745// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000746bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000747Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000748{
749 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
750 if (log)
751 log->Printf ("Target::%s\n", __FUNCTION__);
752
Johnny Chenc86582f2011-09-23 21:21:43 +0000753 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000754 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000755 return true;
756 }
757
758 // Otherwise, it's an end to end operation.
759
Johnny Chenda5a8022011-09-20 23:28:55 +0000760 if (!ProcessIsValid())
761 return false;
762
Johnny Chenecd4feb2011-10-14 00:42:25 +0000763 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000764 for (size_t i = 0; i < num_watchpoints; ++i)
765 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000766 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
767 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000768 return false;
769
Johnny Chenecd4feb2011-10-14 00:42:25 +0000770 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000771 if (rc.Fail())
772 return false;
773 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000774 return true; // Success!
775}
776
Johnny Chenecd4feb2011-10-14 00:42:25 +0000777// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
778// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000779bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000780Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000781{
782 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
783 if (log)
784 log->Printf ("Target::%s\n", __FUNCTION__);
785
Johnny Chenc86582f2011-09-23 21:21:43 +0000786 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000787 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000788 return true;
789 }
790
791 // Otherwise, it's an end to end operation.
792
Johnny Chenda5a8022011-09-20 23:28:55 +0000793 if (!ProcessIsValid())
794 return false;
795
Johnny Chenecd4feb2011-10-14 00:42:25 +0000796 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000797 for (size_t i = 0; i < num_watchpoints; ++i)
798 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000799 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
800 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000801 return false;
802
Johnny Chenecd4feb2011-10-14 00:42:25 +0000803 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000804 if (rc.Fail())
805 return false;
806 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000807 return true; // Success!
808}
809
Johnny Chen116a5cd2012-02-25 06:44:30 +0000810// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
811bool
812Target::ClearAllWatchpointHitCounts ()
813{
814 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
815 if (log)
816 log->Printf ("Target::%s\n", __FUNCTION__);
817
818 size_t num_watchpoints = m_watchpoint_list.GetSize();
819 for (size_t i = 0; i < num_watchpoints; ++i)
820 {
821 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
822 if (!wp_sp)
823 return false;
824
825 wp_sp->ResetHitCount();
826 }
827 return true; // Success!
828}
829
Johnny Chenecd4feb2011-10-14 00:42:25 +0000830// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000831// during these operations.
832bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000833Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000834{
835 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
836 if (log)
837 log->Printf ("Target::%s\n", __FUNCTION__);
838
839 if (!ProcessIsValid())
840 return false;
841
Johnny Chenecd4feb2011-10-14 00:42:25 +0000842 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000843 for (size_t i = 0; i < num_watchpoints; ++i)
844 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000845 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
846 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000847 return false;
848
Johnny Chenecd4feb2011-10-14 00:42:25 +0000849 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000850 }
851 return true; // Success!
852}
853
Johnny Chenecd4feb2011-10-14 00:42:25 +0000854// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000855bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000856Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000857{
858 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
859 if (log)
860 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
861
862 if (!ProcessIsValid())
863 return false;
864
Johnny Chenecd4feb2011-10-14 00:42:25 +0000865 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
866 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000867 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000868 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000869 if (rc.Success())
870 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000871
Johnny Chen01acfa72011-09-22 18:04:58 +0000872 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000873 }
874 return false;
875}
876
Johnny Chenecd4feb2011-10-14 00:42:25 +0000877// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000878bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000879Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000880{
881 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
882 if (log)
883 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
884
885 if (!ProcessIsValid())
886 return false;
887
Johnny Chenecd4feb2011-10-14 00:42:25 +0000888 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
889 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000890 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000891 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000892 if (rc.Success())
893 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000894
Johnny Chen01acfa72011-09-22 18:04:58 +0000895 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000896 }
897 return false;
898}
899
Johnny Chenecd4feb2011-10-14 00:42:25 +0000900// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000901bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000902Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000903{
904 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
905 if (log)
906 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
907
Johnny Chenecd4feb2011-10-14 00:42:25 +0000908 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000909 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000910 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000911 return true;
912 }
913 return false;
914}
915
Johnny Chenecd4feb2011-10-14 00:42:25 +0000916// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000917bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000918Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000919{
920 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
921 if (log)
922 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
923
924 if (!ProcessIsValid())
925 return false;
926
Johnny Chenecd4feb2011-10-14 00:42:25 +0000927 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
928 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000929 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000930 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000931 return true;
932 }
933 return false;
934}
935
Chris Lattner24943d22010-06-08 16:52:24 +0000936ModuleSP
937Target::GetExecutableModule ()
938{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000939 return m_images.GetModuleAtIndex(0);
940}
941
942Module*
943Target::GetExecutableModulePointer ()
944{
945 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000946}
947
948void
949Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
950{
951 m_images.Clear();
952 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000953 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000954 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000955
956 if (executable_sp.get())
957 {
958 Timer scoped_timer (__PRETTY_FUNCTION__,
959 "Target::SetExecutableModule (executable = '%s/%s')",
960 executable_sp->GetFileSpec().GetDirectory().AsCString(),
961 executable_sp->GetFileSpec().GetFilename().AsCString());
962
963 m_images.Append(executable_sp); // The first image is our exectuable file
964
Jim Ingham7508e732010-08-09 23:31:02 +0000965 // 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 +0000966 if (!m_arch.IsValid())
967 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000968
Chris Lattner24943d22010-06-08 16:52:24 +0000969 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000970 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000971
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000972 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000973 {
974 executable_objfile->GetDependentModules(dependent_files);
975 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
976 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000977 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
978 FileSpec platform_dependent_file_spec;
979 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000980 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000981 else
982 platform_dependent_file_spec = dependent_file_spec;
983
Greg Clayton444fe992012-02-26 05:51:37 +0000984 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
985 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +0000986 if (image_module_sp.get())
987 {
Chris Lattner24943d22010-06-08 16:52:24 +0000988 ObjectFile *objfile = image_module_sp->GetObjectFile();
989 if (objfile)
990 objfile->GetDependentModules(dependent_files);
991 }
992 }
993 }
Chris Lattner24943d22010-06-08 16:52:24 +0000994 }
995}
996
997
Jim Ingham7508e732010-08-09 23:31:02 +0000998bool
999Target::SetArchitecture (const ArchSpec &arch_spec)
1000{
Greg Claytonb170aee2012-05-08 01:45:38 +00001001 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001002 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001003 // If we haven't got a valid arch spec, or the architectures are
1004 // compatible, so just update the architecture. Architectures can be
1005 // equal, yet the triple OS and vendor might change, so we need to do
1006 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001007 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001008 return true;
1009 }
1010 else
1011 {
1012 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +00001013 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001014 ModuleSP executable_sp = GetExecutableModule ();
1015 m_images.Clear();
1016 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001017 m_scratch_ast_source_ap.reset();
1018 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001019 // Need to do something about unsetting breakpoints.
1020
1021 if (executable_sp)
1022 {
Greg Clayton444fe992012-02-26 05:51:37 +00001023 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1024 Error error = ModuleList::GetSharedModule (module_spec,
1025 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001026 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001027 NULL,
1028 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001029
1030 if (!error.Fail() && executable_sp)
1031 {
1032 SetExecutableModule (executable_sp, true);
1033 return true;
1034 }
Jim Ingham7508e732010-08-09 23:31:02 +00001035 }
1036 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001037 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001038}
Chris Lattner24943d22010-06-08 16:52:24 +00001039
Chris Lattner24943d22010-06-08 16:52:24 +00001040void
1041Target::ModuleAdded (ModuleSP &module_sp)
1042{
1043 // A module is being added to this target for the first time
1044 ModuleList module_list;
1045 module_list.Append(module_sp);
1046 ModulesDidLoad (module_list);
1047}
1048
1049void
1050Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
1051{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001052 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001053 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001054}
1055
1056void
1057Target::ModulesDidLoad (ModuleList &module_list)
1058{
1059 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1060 // TODO: make event data that packages up the module_list
1061 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1062}
1063
1064void
1065Target::ModulesDidUnload (ModuleList &module_list)
1066{
1067 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +00001068
1069 // Remove the images from the target image list
1070 m_images.Remove(module_list);
1071
Chris Lattner24943d22010-06-08 16:52:24 +00001072 // TODO: make event data that packages up the module_list
1073 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1074}
1075
Jim Ingham7089d8a2011-10-28 23:14:11 +00001076
Daniel Dunbar705a0982011-10-31 22:50:37 +00001077bool
Greg Clayton444fe992012-02-26 05:51:37 +00001078Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001079{
1080
Greg Clayton73844aa2012-08-22 17:17:09 +00001081 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001082 {
1083 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001084 ModuleSpec module_spec (module_file_spec);
1085 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001086
1087 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1088 // black list.
1089 if (num_modules > 0)
1090 {
1091 for (int i = 0; i < num_modules; i++)
1092 {
1093 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1094 return false;
1095 }
1096 return true;
1097 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001098 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001099 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001100}
1101
Daniel Dunbar705a0982011-10-31 22:50:37 +00001102bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001103Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1104{
Greg Clayton73844aa2012-08-22 17:17:09 +00001105 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001106 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001107 if (m_platform_sp)
1108 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001109 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001110 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001111}
1112
Chris Lattner24943d22010-06-08 16:52:24 +00001113size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001114Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1115{
Greg Clayton3508c382012-02-24 01:59:29 +00001116 SectionSP section_sp (addr.GetSection());
1117 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001118 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001119 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1120 if (section_sp->IsEncrypted())
1121 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001122 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001123 return 0;
1124 }
Greg Clayton3508c382012-02-24 01:59:29 +00001125 ModuleSP module_sp (section_sp->GetModule());
1126 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001127 {
Greg Clayton3508c382012-02-24 01:59:29 +00001128 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1129 if (objfile)
1130 {
1131 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1132 addr.GetOffset(),
1133 dst,
1134 dst_len);
1135 if (bytes_read > 0)
1136 return bytes_read;
1137 else
1138 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1139 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001140 else
Greg Clayton3508c382012-02-24 01:59:29 +00001141 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001142 }
1143 else
Greg Clayton3508c382012-02-24 01:59:29 +00001144 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001145 }
1146 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001147 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001148
Greg Clayton26100dc2011-01-07 01:57:07 +00001149 return 0;
1150}
1151
1152size_t
Enrico Granata91544802011-09-06 19:20:51 +00001153Target::ReadMemory (const Address& addr,
1154 bool prefer_file_cache,
1155 void *dst,
1156 size_t dst_len,
1157 Error &error,
1158 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001159{
Chris Lattner24943d22010-06-08 16:52:24 +00001160 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001161
Enrico Granata91544802011-09-06 19:20:51 +00001162 // if we end up reading this from process memory, we will fill this
1163 // with the actual load address
1164 if (load_addr_ptr)
1165 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1166
Greg Clayton26100dc2011-01-07 01:57:07 +00001167 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001168
1169 addr_t load_addr = LLDB_INVALID_ADDRESS;
1170 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001171 Address resolved_addr;
1172 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001173 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001174 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001175 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001176 // No sections are loaded, so we must assume we are not running
1177 // yet and anything we are given is a file address.
1178 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1179 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001180 }
Greg Clayton70436352010-06-30 23:03:03 +00001181 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001182 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001183 // We have at least one section loaded. This can be becuase
1184 // we have manually loaded some sections with "target modules load ..."
1185 // or because we have have a live process that has sections loaded
1186 // through the dynamic loader
1187 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1188 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001189 }
Greg Clayton70436352010-06-30 23:03:03 +00001190 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001191 if (!resolved_addr.IsValid())
1192 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001193
Greg Clayton9b82f862011-07-11 05:12:02 +00001194
Greg Clayton26100dc2011-01-07 01:57:07 +00001195 if (prefer_file_cache)
1196 {
1197 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1198 if (bytes_read > 0)
1199 return bytes_read;
1200 }
Greg Clayton70436352010-06-30 23:03:03 +00001201
Johnny Chenda5a8022011-09-20 23:28:55 +00001202 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001203 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001204 if (load_addr == LLDB_INVALID_ADDRESS)
1205 load_addr = resolved_addr.GetLoadAddress (this);
1206
Greg Clayton70436352010-06-30 23:03:03 +00001207 if (load_addr == LLDB_INVALID_ADDRESS)
1208 {
Greg Clayton3508c382012-02-24 01:59:29 +00001209 ModuleSP addr_module_sp (resolved_addr.GetModule());
1210 if (addr_module_sp && addr_module_sp->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001211 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001212 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001213 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001214 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001215 else
Greg Clayton9c236732011-10-26 00:56:27 +00001216 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001217 }
1218 else
1219 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001220 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001221 if (bytes_read != dst_len)
1222 {
1223 if (error.Success())
1224 {
1225 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001226 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001227 else
Greg Clayton9c236732011-10-26 00:56:27 +00001228 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 +00001229 }
1230 }
Greg Clayton70436352010-06-30 23:03:03 +00001231 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001232 {
1233 if (load_addr_ptr)
1234 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001235 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001236 }
Greg Clayton70436352010-06-30 23:03:03 +00001237 // If the address is not section offset we have an address that
1238 // doesn't resolve to any address in any currently loaded shared
1239 // libaries and we failed to read memory so there isn't anything
1240 // more we can do. If it is section offset, we might be able to
1241 // read cached memory from the object file.
1242 if (!resolved_addr.IsSectionOffset())
1243 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001244 }
Chris Lattner24943d22010-06-08 16:52:24 +00001245 }
Greg Clayton70436352010-06-30 23:03:03 +00001246
Greg Clayton9b82f862011-07-11 05:12:02 +00001247 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001248 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001249 // If we didn't already try and read from the object file cache, then
1250 // try it after failing to read from the process.
1251 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001252 }
1253 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001254}
1255
Greg Clayton7dd98df2011-07-12 17:06:17 +00001256size_t
1257Target::ReadScalarIntegerFromMemory (const Address& addr,
1258 bool prefer_file_cache,
1259 uint32_t byte_size,
1260 bool is_signed,
1261 Scalar &scalar,
1262 Error &error)
1263{
1264 uint64_t uval;
1265
1266 if (byte_size <= sizeof(uval))
1267 {
1268 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1269 if (bytes_read == byte_size)
1270 {
1271 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1272 uint32_t offset = 0;
1273 if (byte_size <= 4)
1274 scalar = data.GetMaxU32 (&offset, byte_size);
1275 else
1276 scalar = data.GetMaxU64 (&offset, byte_size);
1277
1278 if (is_signed)
1279 scalar.SignExtend(byte_size * 8);
1280 return bytes_read;
1281 }
1282 }
1283 else
1284 {
1285 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1286 }
1287 return 0;
1288}
1289
1290uint64_t
1291Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1292 bool prefer_file_cache,
1293 size_t integer_byte_size,
1294 uint64_t fail_value,
1295 Error &error)
1296{
1297 Scalar scalar;
1298 if (ReadScalarIntegerFromMemory (addr,
1299 prefer_file_cache,
1300 integer_byte_size,
1301 false,
1302 scalar,
1303 error))
1304 return scalar.ULongLong(fail_value);
1305 return fail_value;
1306}
1307
1308bool
1309Target::ReadPointerFromMemory (const Address& addr,
1310 bool prefer_file_cache,
1311 Error &error,
1312 Address &pointer_addr)
1313{
1314 Scalar scalar;
1315 if (ReadScalarIntegerFromMemory (addr,
1316 prefer_file_cache,
1317 m_arch.GetAddressByteSize(),
1318 false,
1319 scalar,
1320 error))
1321 {
1322 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1323 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1324 {
1325 if (m_section_load_list.IsEmpty())
1326 {
1327 // No sections are loaded, so we must assume we are not running
1328 // yet and anything we are given is a file address.
1329 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1330 }
1331 else
1332 {
1333 // We have at least one section loaded. This can be becuase
1334 // we have manually loaded some sections with "target modules load ..."
1335 // or because we have have a live process that has sections loaded
1336 // through the dynamic loader
1337 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1338 }
1339 // We weren't able to resolve the pointer value, so just return
1340 // an address with no section
1341 if (!pointer_addr.IsValid())
1342 pointer_addr.SetOffset (pointer_vm_addr);
1343 return true;
1344
1345 }
1346 }
1347 return false;
1348}
Chris Lattner24943d22010-06-08 16:52:24 +00001349
1350ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001351Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001352{
Chris Lattner24943d22010-06-08 16:52:24 +00001353 ModuleSP module_sp;
1354
Chris Lattner24943d22010-06-08 16:52:24 +00001355 Error error;
1356
Jim Ingham03e5e512012-05-17 18:38:42 +00001357 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1358 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001359
Jim Ingham03e5e512012-05-17 18:38:42 +00001360 if (module_spec.GetUUID().IsValid())
1361 module_sp = m_images.FindFirstModule(module_spec);
1362
Greg Claytone1ef1e32012-04-27 00:58:27 +00001363 if (!module_sp)
1364 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001365 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1366 bool did_create_module = false;
1367
1368 // If there are image search path entries, try to use them first to acquire a suitable image.
1369 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001370 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001371 ModuleSpec transformed_spec (module_spec);
1372 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1373 {
1374 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1375 error = ModuleList::GetSharedModule (transformed_spec,
1376 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001377 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001378 &old_module_sp,
1379 &did_create_module);
1380 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001381 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001382
Greg Claytone1ef1e32012-04-27 00:58:27 +00001383 if (!module_sp)
1384 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001385 // If we have a UUID, we can check our global shared module list in case
1386 // we already have it. If we don't have a valid UUID, then we can't since
1387 // the path in "module_spec" will be a platform path, and we will need to
1388 // let the platform find that file. For example, we could be asking for
1389 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1390 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1391 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1392 // cache.
1393 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001394 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001395 // We have a UUID, it is OK to check the global module list...
1396 error = ModuleList::GetSharedModule (module_spec,
1397 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001398 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001399 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001400 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001401 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001402
1403 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001404 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001405 // The platform is responsible for finding and caching an appropriate
1406 // module in the shared module cache.
1407 if (m_platform_sp)
1408 {
1409 FileSpec platform_file_spec;
1410 error = m_platform_sp->GetSharedModule (module_spec,
1411 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001412 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001413 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001414 &did_create_module);
1415 }
1416 else
1417 {
1418 error.SetErrorString("no platform is currently set");
1419 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001420 }
1421 }
Chris Lattner24943d22010-06-08 16:52:24 +00001422
Jim Ingham03e5e512012-05-17 18:38:42 +00001423 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1424 // module in the list already, and if there was, let's remove it.
1425 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001426 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001427 // GetSharedModule is not guaranteed to find the old shared module, for instance
1428 // in the common case where you pass in the UUID, it is only going to find the one
1429 // module matching the UUID. In fact, it has no good way to know what the "old module"
1430 // relevant to this target is, since there might be many copies of a module with this file spec
1431 // in various running debug sessions, but only one of them will belong to this target.
1432 // So let's remove the UUID from the module list, and look in the target's module list.
1433 // Only do this if there is SOMETHING else in the module spec...
1434 if (!old_module_sp)
1435 {
1436 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
1437 {
1438 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1439 module_spec_copy.GetUUID().Clear();
1440
1441 ModuleList found_modules;
1442 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1443 if (num_found == 1)
1444 {
1445 old_module_sp = found_modules.GetModuleAtIndex(0);
1446 }
1447 }
1448 }
1449
1450 m_images.Append (module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001451 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
Jim Ingham03e5e512012-05-17 18:38:42 +00001452 {
Chris Lattner24943d22010-06-08 16:52:24 +00001453 ModuleUpdated(old_module_sp, module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001454 m_images.Remove (old_module_sp);
1455 Module *old_module_ptr = old_module_sp.get();
1456 old_module_sp.reset();
1457 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1458 }
Chris Lattner24943d22010-06-08 16:52:24 +00001459 else
1460 ModuleAdded(module_sp);
1461 }
1462 }
1463 if (error_ptr)
1464 *error_ptr = error;
1465 return module_sp;
1466}
1467
1468
Greg Clayton289afcb2012-02-18 05:35:26 +00001469TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001470Target::CalculateTarget ()
1471{
Greg Clayton289afcb2012-02-18 05:35:26 +00001472 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001473}
1474
Greg Clayton289afcb2012-02-18 05:35:26 +00001475ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001476Target::CalculateProcess ()
1477{
Greg Clayton289afcb2012-02-18 05:35:26 +00001478 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001479}
1480
Greg Clayton289afcb2012-02-18 05:35:26 +00001481ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001482Target::CalculateThread ()
1483{
Greg Clayton289afcb2012-02-18 05:35:26 +00001484 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001485}
1486
Greg Clayton289afcb2012-02-18 05:35:26 +00001487StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001488Target::CalculateStackFrame ()
1489{
Greg Clayton289afcb2012-02-18 05:35:26 +00001490 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001491}
1492
1493void
Greg Claytona830adb2010-10-04 01:05:56 +00001494Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001495{
Greg Clayton567e7f32011-09-22 04:58:26 +00001496 exe_ctx.Clear();
1497 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001498}
1499
1500PathMappingList &
1501Target::GetImageSearchPathList ()
1502{
1503 return m_image_search_paths;
1504}
1505
1506void
1507Target::ImageSearchPathsChanged
1508(
1509 const PathMappingList &path_list,
1510 void *baton
1511)
1512{
1513 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001514 ModuleSP exe_module_sp (target->GetExecutableModule());
1515 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001516 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001517 target->m_images.Clear();
1518 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001519 }
1520}
1521
1522ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001523Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001524{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001525 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001526 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001527 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001528 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001529 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001530 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1531 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1532 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1533 }
Chris Lattner24943d22010-06-08 16:52:24 +00001534 return m_scratch_ast_context_ap.get();
1535}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001536
Sean Callanan4938bd62011-11-16 18:20:47 +00001537ClangASTImporter *
1538Target::GetClangASTImporter()
1539{
1540 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1541
1542 if (!ast_importer)
1543 {
1544 ast_importer = new ClangASTImporter();
1545 m_ast_importer_ap.reset(ast_importer);
1546 }
1547
1548 return ast_importer;
1549}
1550
Greg Clayton990de7b2010-11-18 23:32:35 +00001551void
Caroline Tice2a456812011-03-10 22:14:10 +00001552Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001553{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001554 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001555}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001556
Greg Clayton990de7b2010-11-18 23:32:35 +00001557void
Caroline Tice2a456812011-03-10 22:14:10 +00001558Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001559{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001560 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001561}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001562
Greg Clayton9ce95382012-02-13 23:10:39 +00001563FileSpecList
1564Target::GetDefaultExecutableSearchPaths ()
1565{
Greg Clayton73844aa2012-08-22 17:17:09 +00001566 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1567 if (properties_sp)
1568 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001569 return FileSpecList();
1570}
1571
Caroline Tice5bc8c972010-09-20 20:44:43 +00001572ArchSpec
1573Target::GetDefaultArchitecture ()
1574{
Greg Clayton73844aa2012-08-22 17:17:09 +00001575 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1576 if (properties_sp)
1577 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001578 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001579}
1580
1581void
Greg Clayton73844aa2012-08-22 17:17:09 +00001582Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001583{
Greg Clayton73844aa2012-08-22 17:17:09 +00001584 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1585 if (properties_sp)
1586 return properties_sp->SetDefaultArchitecture(arch);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001587}
1588
Greg Claytona830adb2010-10-04 01:05:56 +00001589Target *
1590Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1591{
1592 // The target can either exist in the "process" of ExecutionContext, or in
1593 // the "target_sp" member of SymbolContext. This accessor helper function
1594 // will get the target from one of these locations.
1595
1596 Target *target = NULL;
1597 if (sc_ptr != NULL)
1598 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001599 if (target == NULL && exe_ctx_ptr)
1600 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001601 return target;
1602}
1603
Greg Clayton427f2902010-12-14 02:59:59 +00001604ExecutionResults
1605Target::EvaluateExpression
1606(
1607 const char *expr_cstr,
1608 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001609 lldb_private::ExecutionPolicy execution_policy,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001610 bool coerce_to_id,
Greg Clayton427f2902010-12-14 02:59:59 +00001611 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001612 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001613 lldb::DynamicValueType use_dynamic,
Enrico Granata6cca9692012-07-16 23:10:35 +00001614 lldb::ValueObjectSP &result_valobj_sp,
1615 uint32_t single_thread_timeout_usec
Greg Clayton427f2902010-12-14 02:59:59 +00001616)
1617{
1618 ExecutionResults execution_results = eExecutionSetupError;
1619
1620 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001621
1622 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1623 return execution_results;
1624
Jim Ingham3613ae12011-05-12 02:06:14 +00001625 // We shouldn't run stop hooks in expressions.
1626 // Be sure to reset this if you return anywhere within this function.
1627 bool old_suppress_value = m_suppress_stop_hooks;
1628 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001629
1630 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001631
1632 const size_t expr_cstr_len = ::strlen (expr_cstr);
1633
Greg Clayton427f2902010-12-14 02:59:59 +00001634 if (frame)
1635 {
1636 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001637 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001638 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001639 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1640 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001641 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001642
1643 // Make sure we don't have any things that we know a variable expression
1644 // won't be able to deal with before calling into it
1645 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1646 {
1647 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1648 use_dynamic,
1649 expr_path_options,
1650 var_sp,
1651 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001652 // if this expression results in a bitfield, we give up and let the IR handle it
1653 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1654 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001655 }
Greg Clayton427f2902010-12-14 02:59:59 +00001656 }
1657 else if (m_process_sp)
1658 {
1659 m_process_sp->CalculateExecutionContext(exe_ctx);
1660 }
1661 else
1662 {
1663 CalculateExecutionContext(exe_ctx);
1664 }
1665
1666 if (result_valobj_sp)
1667 {
1668 execution_results = eExecutionCompleted;
1669 // We got a result from the frame variable expression path above...
1670 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1671
1672 lldb::ValueObjectSP const_valobj_sp;
1673
1674 // Check in case our value is already a constant value
1675 if (result_valobj_sp->GetIsConstant())
1676 {
1677 const_valobj_sp = result_valobj_sp;
1678 const_valobj_sp->SetName (persistent_variable_name);
1679 }
1680 else
Jim Inghame41494a2011-04-16 00:01:13 +00001681 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001682 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001683 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001684 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001685 if (dynamic_sp)
1686 result_valobj_sp = dynamic_sp;
1687 }
1688
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001689 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001690 }
Greg Clayton427f2902010-12-14 02:59:59 +00001691
Sean Callanan6a925532011-01-13 08:53:35 +00001692 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1693
Greg Clayton427f2902010-12-14 02:59:59 +00001694 result_valobj_sp = const_valobj_sp;
1695
Sean Callanan6a925532011-01-13 08:53:35 +00001696 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1697 assert (clang_expr_variable_sp.get());
1698
1699 // Set flags and live data as appropriate
1700
1701 const Value &result_value = live_valobj_sp->GetValue();
1702
1703 switch (result_value.GetValueType())
1704 {
1705 case Value::eValueTypeHostAddress:
1706 case Value::eValueTypeFileAddress:
1707 // we don't do anything with these for now
1708 break;
1709 case Value::eValueTypeScalar:
1710 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1711 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1712 break;
1713 case Value::eValueTypeLoadAddress:
1714 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1715 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1716 break;
1717 }
Greg Clayton427f2902010-12-14 02:59:59 +00001718 }
1719 else
1720 {
1721 // Make sure we aren't just trying to see the value of a persistent
1722 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001723 lldb::ClangExpressionVariableSP persistent_var_sp;
1724 // Only check for persistent variables the expression starts with a '$'
1725 if (expr_cstr[0] == '$')
1726 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1727
Greg Clayton427f2902010-12-14 02:59:59 +00001728 if (persistent_var_sp)
1729 {
1730 result_valobj_sp = persistent_var_sp->GetValueObject ();
1731 execution_results = eExecutionCompleted;
1732 }
1733 else
1734 {
1735 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001736
Greg Clayton427f2902010-12-14 02:59:59 +00001737 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001738 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001739 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001740 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan6a925532011-01-13 08:53:35 +00001741 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001742 expr_cstr,
1743 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001744 result_valobj_sp,
1745 single_thread_timeout_usec);
Greg Clayton427f2902010-12-14 02:59:59 +00001746 }
1747 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001748
1749 m_suppress_stop_hooks = old_suppress_value;
1750
Greg Clayton427f2902010-12-14 02:59:59 +00001751 return execution_results;
1752}
1753
Greg Claytonc0fa5332011-05-22 22:46:53 +00001754lldb::addr_t
1755Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1756{
1757 addr_t code_addr = load_addr;
1758 switch (m_arch.GetMachine())
1759 {
1760 case llvm::Triple::arm:
1761 case llvm::Triple::thumb:
1762 switch (addr_class)
1763 {
1764 case eAddressClassData:
1765 case eAddressClassDebug:
1766 return LLDB_INVALID_ADDRESS;
1767
1768 case eAddressClassUnknown:
1769 case eAddressClassInvalid:
1770 case eAddressClassCode:
1771 case eAddressClassCodeAlternateISA:
1772 case eAddressClassRuntime:
1773 // Check if bit zero it no set?
1774 if ((code_addr & 1ull) == 0)
1775 {
1776 // Bit zero isn't set, check if the address is a multiple of 2?
1777 if (code_addr & 2ull)
1778 {
1779 // The address is a multiple of 2 so it must be thumb, set bit zero
1780 code_addr |= 1ull;
1781 }
1782 else if (addr_class == eAddressClassCodeAlternateISA)
1783 {
1784 // We checked the address and the address claims to be the alternate ISA
1785 // which means thumb, so set bit zero.
1786 code_addr |= 1ull;
1787 }
1788 }
1789 break;
1790 }
1791 break;
1792
1793 default:
1794 break;
1795 }
1796 return code_addr;
1797}
1798
1799lldb::addr_t
1800Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1801{
1802 addr_t opcode_addr = load_addr;
1803 switch (m_arch.GetMachine())
1804 {
1805 case llvm::Triple::arm:
1806 case llvm::Triple::thumb:
1807 switch (addr_class)
1808 {
1809 case eAddressClassData:
1810 case eAddressClassDebug:
1811 return LLDB_INVALID_ADDRESS;
1812
1813 case eAddressClassInvalid:
1814 case eAddressClassUnknown:
1815 case eAddressClassCode:
1816 case eAddressClassCodeAlternateISA:
1817 case eAddressClassRuntime:
1818 opcode_addr &= ~(1ull);
1819 break;
1820 }
1821 break;
1822
1823 default:
1824 break;
1825 }
1826 return opcode_addr;
1827}
1828
Jim Inghamd60d94a2011-03-11 03:53:59 +00001829lldb::user_id_t
1830Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1831{
1832 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001833 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001834 m_stop_hooks[new_uid] = new_hook_sp;
1835 return new_uid;
1836}
1837
1838bool
1839Target::RemoveStopHookByID (lldb::user_id_t user_id)
1840{
1841 size_t num_removed;
1842 num_removed = m_stop_hooks.erase (user_id);
1843 if (num_removed == 0)
1844 return false;
1845 else
1846 return true;
1847}
1848
1849void
1850Target::RemoveAllStopHooks ()
1851{
1852 m_stop_hooks.clear();
1853}
1854
1855Target::StopHookSP
1856Target::GetStopHookByID (lldb::user_id_t user_id)
1857{
1858 StopHookSP found_hook;
1859
1860 StopHookCollection::iterator specified_hook_iter;
1861 specified_hook_iter = m_stop_hooks.find (user_id);
1862 if (specified_hook_iter != m_stop_hooks.end())
1863 found_hook = (*specified_hook_iter).second;
1864 return found_hook;
1865}
1866
1867bool
1868Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1869{
1870 StopHookCollection::iterator specified_hook_iter;
1871 specified_hook_iter = m_stop_hooks.find (user_id);
1872 if (specified_hook_iter == m_stop_hooks.end())
1873 return false;
1874
1875 (*specified_hook_iter).second->SetIsActive (active_state);
1876 return true;
1877}
1878
1879void
1880Target::SetAllStopHooksActiveState (bool active_state)
1881{
1882 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1883 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1884 {
1885 (*pos).second->SetIsActive (active_state);
1886 }
1887}
1888
1889void
1890Target::RunStopHooks ()
1891{
Jim Ingham3613ae12011-05-12 02:06:14 +00001892 if (m_suppress_stop_hooks)
1893 return;
1894
Jim Inghamd60d94a2011-03-11 03:53:59 +00001895 if (!m_process_sp)
1896 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001897
1898 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1899 // since in that case we do not want to run the stop-hooks
1900 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1901 return;
1902
Jim Inghamd60d94a2011-03-11 03:53:59 +00001903 if (m_stop_hooks.empty())
1904 return;
1905
1906 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1907
1908 // If there aren't any active stop hooks, don't bother either:
1909 bool any_active_hooks = false;
1910 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1911 {
1912 if ((*pos).second->IsActive())
1913 {
1914 any_active_hooks = true;
1915 break;
1916 }
1917 }
1918 if (!any_active_hooks)
1919 return;
1920
1921 CommandReturnObject result;
1922
1923 std::vector<ExecutionContext> exc_ctx_with_reasons;
1924 std::vector<SymbolContext> sym_ctx_with_reasons;
1925
1926 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1927 size_t num_threads = cur_threadlist.GetSize();
1928 for (size_t i = 0; i < num_threads; i++)
1929 {
1930 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1931 if (cur_thread_sp->ThreadStoppedForAReason())
1932 {
1933 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1934 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1935 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1936 }
1937 }
1938
1939 // If no threads stopped for a reason, don't run the stop-hooks.
1940 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1941 if (num_exe_ctx == 0)
1942 return;
1943
Jim Inghame5ed8e92011-06-02 23:58:26 +00001944 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1945 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001946
1947 bool keep_going = true;
1948 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001949 bool print_hook_header;
1950 bool print_thread_header;
1951
1952 if (num_exe_ctx == 1)
1953 print_thread_header = false;
1954 else
1955 print_thread_header = true;
1956
1957 if (m_stop_hooks.size() == 1)
1958 print_hook_header = false;
1959 else
1960 print_hook_header = true;
1961
Jim Inghamd60d94a2011-03-11 03:53:59 +00001962 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1963 {
1964 // result.Clear();
1965 StopHookSP cur_hook_sp = (*pos).second;
1966 if (!cur_hook_sp->IsActive())
1967 continue;
1968
1969 bool any_thread_matched = false;
1970 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1971 {
1972 if ((cur_hook_sp->GetSpecifier () == NULL
1973 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1974 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00001975 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001976 {
1977 if (!hooks_ran)
1978 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001979 hooks_ran = true;
1980 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001981 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001982 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001983 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1984 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1985 NULL);
1986 if (cmd)
1987 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1988 else
1989 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001990 any_thread_matched = true;
1991 }
1992
Jim Inghamc54840c2011-03-22 01:47:27 +00001993 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001994 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001995
1996 bool stop_on_continue = true;
1997 bool stop_on_error = true;
1998 bool echo_commands = false;
1999 bool print_results = true;
2000 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002001 &exc_ctx_with_reasons[i],
2002 stop_on_continue,
2003 stop_on_error,
2004 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002005 print_results,
2006 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002007 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002008
2009 // If the command started the target going again, we should bag out of
2010 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002011 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2012 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002013 {
Greg Clayton444e35b2011-10-19 18:09:39 +00002014 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002015 keep_going = false;
2016 }
2017 }
2018 }
2019 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002020
Caroline Tice4a348082011-05-02 20:41:46 +00002021 result.GetImmediateOutputStream()->Flush();
2022 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002023}
2024
Greg Claytonbbea1332011-07-08 00:48:09 +00002025
Jim Inghamd60d94a2011-03-11 03:53:59 +00002026//--------------------------------------------------------------
2027// class Target::StopHook
2028//--------------------------------------------------------------
2029
2030
2031Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2032 UserID (uid),
2033 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002034 m_commands (),
2035 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002036 m_thread_spec_ap(NULL),
2037 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002038{
2039}
2040
2041Target::StopHook::StopHook (const StopHook &rhs) :
2042 UserID (rhs.GetID()),
2043 m_target_sp (rhs.m_target_sp),
2044 m_commands (rhs.m_commands),
2045 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002046 m_thread_spec_ap (NULL),
2047 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002048{
2049 if (rhs.m_thread_spec_ap.get() != NULL)
2050 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2051}
2052
2053
2054Target::StopHook::~StopHook ()
2055{
2056}
2057
2058void
2059Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2060{
2061 m_thread_spec_ap.reset (specifier);
2062}
2063
2064
2065void
2066Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2067{
2068 int indent_level = s->GetIndentLevel();
2069
2070 s->SetIndentLevel(indent_level + 2);
2071
Greg Clayton444e35b2011-10-19 18:09:39 +00002072 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002073 if (m_active)
2074 s->Indent ("State: enabled\n");
2075 else
2076 s->Indent ("State: disabled\n");
2077
2078 if (m_specifier_sp)
2079 {
2080 s->Indent();
2081 s->PutCString ("Specifier:\n");
2082 s->SetIndentLevel (indent_level + 4);
2083 m_specifier_sp->GetDescription (s, level);
2084 s->SetIndentLevel (indent_level + 2);
2085 }
2086
2087 if (m_thread_spec_ap.get() != NULL)
2088 {
2089 StreamString tmp;
2090 s->Indent("Thread:\n");
2091 m_thread_spec_ap->GetDescription (&tmp, level);
2092 s->SetIndentLevel (indent_level + 4);
2093 s->Indent (tmp.GetData());
2094 s->PutCString ("\n");
2095 s->SetIndentLevel (indent_level + 2);
2096 }
2097
2098 s->Indent ("Commands: \n");
2099 s->SetIndentLevel (indent_level + 4);
2100 uint32_t num_commands = m_commands.GetSize();
2101 for (uint32_t i = 0; i < num_commands; i++)
2102 {
2103 s->Indent(m_commands.GetStringAtIndex(i));
2104 s->PutCString ("\n");
2105 }
2106 s->SetIndentLevel (indent_level);
2107}
2108
Greg Clayton73844aa2012-08-22 17:17:09 +00002109//--------------------------------------------------------------
2110// class TargetProperties
2111//--------------------------------------------------------------
2112
2113OptionEnumValueElement
2114lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002115{
Greg Clayton73844aa2012-08-22 17:17:09 +00002116 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2117 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2118 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2119 { 0, NULL, NULL }
2120};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002121
Greg Clayton49ce8962012-08-29 21:13:06 +00002122static OptionEnumValueElement
2123g_inline_breakpoint_enums[] =
2124{
2125 { eInlineBreakpointsNever, "never", "Never look for inline breakpoint locations (fastest). This setting should only be used if you know that no inlining occurs in your programs."},
2126 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2127 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2128 { 0, NULL, NULL }
2129};
2130
Greg Clayton73844aa2012-08-22 17:17:09 +00002131static PropertyDefinition
2132g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002133{
Greg Clayton73844aa2012-08-22 17:17:09 +00002134 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2135 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2136 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2137 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2138 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2139 { "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 "
2140 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2141 "some part (starting at the root) of the path to the file when it was built, "
2142 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2143 "Each element of the array is checked in order and the first one that results in a match wins." },
2144 { "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." },
2145 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2146 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2147 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." },
2148 { "run-args" , OptionValue::eTypeArgs , false, 0 , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run." },
2149 { "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." },
2150 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2151 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2152 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2153 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2154 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2155 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Disable stdin/stdout for process (e.g. for a GUI application)" },
Greg Clayton49ce8962012-08-29 21:13:06 +00002156 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2157 "Breakpoint locations can end up being inlined by the compiler, so that a compile unit 'a.c' might contain an inlined function from another source file. "
2158 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2159 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2160 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2161 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2162 "file and line breakpoints." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002163 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2164};
2165enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002166{
Greg Clayton73844aa2012-08-22 17:17:09 +00002167 ePropertyDefaultArch,
2168 ePropertyExprPrefix,
2169 ePropertyPreferDynamic,
2170 ePropertyEnableSynthetic,
2171 ePropertySkipPrologue,
2172 ePropertySourceMap,
2173 ePropertyExecutableSearchPaths,
2174 ePropertyMaxChildrenCount,
2175 ePropertyMaxSummaryLength,
2176 ePropertyBreakpointUseAvoidList,
2177 ePropertyRunArgs,
2178 ePropertyEnvVars,
2179 ePropertyInheritEnv,
2180 ePropertyInputPath,
2181 ePropertyOutputPath,
2182 ePropertyErrorPath,
2183 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002184 ePropertyDisableSTDIO,
2185 ePropertyInlineStrategy
Greg Clayton73844aa2012-08-22 17:17:09 +00002186};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002187
Caroline Tice5bc8c972010-09-20 20:44:43 +00002188
Greg Clayton73844aa2012-08-22 17:17:09 +00002189class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002190{
Greg Clayton73844aa2012-08-22 17:17:09 +00002191public:
2192 TargetOptionValueProperties (const ConstString &name) :
2193 OptionValueProperties (name),
2194 m_target (NULL),
2195 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002196 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002197 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002198
Greg Clayton73844aa2012-08-22 17:17:09 +00002199 // This constructor is used when creating TargetOptionValueProperties when it
2200 // is part of a new lldb_private::Target instance. It will copy all current
2201 // global property values as needed
2202 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2203 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2204 m_target (target),
2205 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002206 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002207 }
2208
2209 virtual const Property *
2210 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2211 {
2212 // When gettings the value for a key from the target options, we will always
2213 // try and grab the setting from the current target if there is one. Else we just
2214 // use the one from this instance.
2215 if (idx == ePropertyEnvVars)
2216 GetHostEnvironmentIfNeeded ();
2217
2218 if (exe_ctx)
2219 {
2220 Target *target = exe_ctx->GetTargetPtr();
2221 if (target)
2222 {
2223 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2224 if (this != target_properties)
2225 return target_properties->ProtectedGetPropertyAtIndex (idx);
2226 }
2227 }
2228 return ProtectedGetPropertyAtIndex (idx);
2229 }
2230protected:
2231
2232 void
2233 GetHostEnvironmentIfNeeded () const
2234 {
2235 if (!m_got_host_env)
2236 {
2237 if (m_target)
2238 {
2239 m_got_host_env = true;
2240 const uint32_t idx = ePropertyInheritEnv;
2241 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2242 {
2243 PlatformSP platform_sp (m_target->GetPlatform());
2244 if (platform_sp)
2245 {
2246 StringList env;
2247 if (platform_sp->GetEnvironment(env))
2248 {
2249 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2250 if (env_dict)
2251 {
2252 const bool can_replace = false;
2253 const size_t envc = env.GetSize();
2254 for (size_t idx=0; idx<envc; idx++)
2255 {
2256 const char *env_entry = env.GetStringAtIndex (idx);
2257 if (env_entry)
2258 {
2259 const char *equal_pos = ::strchr(env_entry, '=');
2260 ConstString key;
2261 // It is ok to have environment variables with no values
2262 const char *value = NULL;
2263 if (equal_pos)
2264 {
2265 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2266 if (equal_pos[1])
2267 value = equal_pos + 1;
2268 }
2269 else
2270 {
2271 key.SetCString(env_entry);
2272 }
2273 // Don't allow existing keys to be replaced with ones we get from the platform environment
2274 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2275 }
2276 }
2277 }
2278 }
2279 }
2280 }
2281 }
2282 }
2283 }
2284 Target *m_target;
2285 mutable bool m_got_host_env;
2286};
2287
2288TargetProperties::TargetProperties (Target *target) :
2289 Properties ()
2290{
2291 if (target)
2292 {
2293 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002294 }
2295 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002296 {
2297 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2298 m_collection_sp->Initialize(g_properties);
2299 m_collection_sp->AppendProperty(ConstString("process"),
2300 ConstString("Settings specify to processes."),
2301 true,
2302 Process::GetGlobalProperties()->GetValueProperties());
2303 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002304}
2305
Greg Clayton73844aa2012-08-22 17:17:09 +00002306TargetProperties::~TargetProperties ()
2307{
2308}
2309ArchSpec
2310TargetProperties::GetDefaultArchitecture () const
2311{
2312 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2313 if (value)
2314 return value->GetCurrentValue();
2315 return ArchSpec();
2316}
2317
2318void
2319TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2320{
2321 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2322 if (value)
2323 return value->SetCurrentValue(arch, true);
2324}
2325
2326lldb::DynamicValueType
2327TargetProperties::GetPreferDynamicValue() const
2328{
2329 const uint32_t idx = ePropertyPreferDynamic;
2330 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2331}
2332
2333bool
2334TargetProperties::GetDisableASLR () const
2335{
2336 const uint32_t idx = ePropertyDisableASLR;
2337 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2338}
2339
2340void
2341TargetProperties::SetDisableASLR (bool b)
2342{
2343 const uint32_t idx = ePropertyDisableASLR;
2344 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2345}
2346
2347bool
2348TargetProperties::GetDisableSTDIO () const
2349{
2350 const uint32_t idx = ePropertyDisableSTDIO;
2351 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2352}
2353
2354void
2355TargetProperties::SetDisableSTDIO (bool b)
2356{
2357 const uint32_t idx = ePropertyDisableSTDIO;
2358 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2359}
2360
Greg Clayton49ce8962012-08-29 21:13:06 +00002361InlineStrategy
2362TargetProperties::GetInlineStrategy () const
2363{
2364 const uint32_t idx = ePropertyInlineStrategy;
2365 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2366}
2367
Greg Clayton73844aa2012-08-22 17:17:09 +00002368bool
2369TargetProperties::GetRunArguments (Args &args) const
2370{
2371 const uint32_t idx = ePropertyRunArgs;
2372 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2373}
2374
2375void
2376TargetProperties::SetRunArguments (const Args &args)
2377{
2378 const uint32_t idx = ePropertyRunArgs;
2379 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2380}
2381
2382size_t
2383TargetProperties::GetEnvironmentAsArgs (Args &env) const
2384{
2385 const uint32_t idx = ePropertyEnvVars;
2386 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2387}
2388
2389bool
2390TargetProperties::GetSkipPrologue() const
2391{
2392 const uint32_t idx = ePropertySkipPrologue;
2393 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2394}
2395
2396PathMappingList &
2397TargetProperties::GetSourcePathMap () const
2398{
2399 const uint32_t idx = ePropertySourceMap;
2400 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2401 assert(option_value);
2402 return option_value->GetCurrentValue();
2403}
2404
2405FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002406TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002407{
2408 const uint32_t idx = ePropertyExecutableSearchPaths;
2409 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2410 assert(option_value);
2411 return option_value->GetCurrentValue();
2412}
2413
2414bool
2415TargetProperties::GetEnableSyntheticValue () const
2416{
2417 const uint32_t idx = ePropertyEnableSynthetic;
2418 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2419}
2420
2421uint32_t
2422TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2423{
2424 const uint32_t idx = ePropertyMaxChildrenCount;
2425 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2426}
2427
2428uint32_t
2429TargetProperties::GetMaximumSizeOfStringSummary() const
2430{
2431 const uint32_t idx = ePropertyMaxSummaryLength;
2432 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2433}
2434
2435FileSpec
2436TargetProperties::GetStandardInputPath () const
2437{
2438 const uint32_t idx = ePropertyInputPath;
2439 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2440}
2441
2442void
2443TargetProperties::SetStandardInputPath (const char *p)
2444{
2445 const uint32_t idx = ePropertyInputPath;
2446 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2447}
2448
2449FileSpec
2450TargetProperties::GetStandardOutputPath () const
2451{
2452 const uint32_t idx = ePropertyOutputPath;
2453 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2454}
2455
2456void
2457TargetProperties::SetStandardOutputPath (const char *p)
2458{
2459 const uint32_t idx = ePropertyOutputPath;
2460 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2461}
2462
2463FileSpec
2464TargetProperties::GetStandardErrorPath () const
2465{
2466 const uint32_t idx = ePropertyErrorPath;
2467 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2468}
2469
Greg Claytonc6e82e42012-08-22 18:39:03 +00002470const char *
2471TargetProperties::GetExpressionPrefixContentsAsCString ()
2472{
2473 const uint32_t idx = ePropertyExprPrefix;
2474 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2475 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002476 {
2477 DataBufferSP data_sp(file->GetFileContents());
2478 if (data_sp)
2479 return (const char *) data_sp->GetBytes();
2480 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002481 return NULL;
2482}
2483
Greg Clayton73844aa2012-08-22 17:17:09 +00002484void
2485TargetProperties::SetStandardErrorPath (const char *p)
2486{
2487 const uint32_t idx = ePropertyErrorPath;
2488 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2489}
2490
2491bool
2492TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2493{
2494 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2495 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2496}
2497
2498const TargetPropertiesSP &
2499Target::GetGlobalProperties()
2500{
2501 static TargetPropertiesSP g_settings_sp;
2502 if (!g_settings_sp)
2503 {
2504 g_settings_sp.reset (new TargetProperties (NULL));
2505 }
2506 return g_settings_sp;
2507}
2508
Jim Ingham5a15e692012-02-16 06:50:00 +00002509const ConstString &
2510Target::TargetEventData::GetFlavorString ()
2511{
2512 static ConstString g_flavor ("Target::TargetEventData");
2513 return g_flavor;
2514}
2515
2516const ConstString &
2517Target::TargetEventData::GetFlavor () const
2518{
2519 return TargetEventData::GetFlavorString ();
2520}
2521
2522Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2523 EventData(),
2524 m_target_sp (new_target_sp)
2525{
2526}
2527
2528Target::TargetEventData::~TargetEventData()
2529{
2530
2531}
2532
2533void
2534Target::TargetEventData::Dump (Stream *s) const
2535{
2536
2537}
2538
2539const TargetSP
2540Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2541{
2542 TargetSP target_sp;
2543
2544 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2545 if (data)
2546 target_sp = data->m_target_sp;
2547
2548 return target_sp;
2549}
2550
2551const Target::TargetEventData *
2552Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2553{
2554 if (event_ptr)
2555 {
2556 const EventData *event_data = event_ptr->GetData();
2557 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2558 return static_cast <const TargetEventData *> (event_ptr->GetData());
2559 }
2560 return NULL;
2561}
2562