blob: bfd222accd4b3b2d9500ea11dc813ef0e1a3b6d9 [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,
Enrico Granata6cca9692012-07-16 23:10:35 +00001609 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001610 const EvaluateExpressionOptions& options
Greg Clayton427f2902010-12-14 02:59:59 +00001611)
1612{
1613 ExecutionResults execution_results = eExecutionSetupError;
1614
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001615 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1616 return execution_results;
1617
Jim Ingham3613ae12011-05-12 02:06:14 +00001618 // We shouldn't run stop hooks in expressions.
1619 // Be sure to reset this if you return anywhere within this function.
1620 bool old_suppress_value = m_suppress_stop_hooks;
1621 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001622
1623 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001624
1625 const size_t expr_cstr_len = ::strlen (expr_cstr);
1626
Greg Clayton427f2902010-12-14 02:59:59 +00001627 if (frame)
1628 {
1629 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001630 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001631 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001632 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1633 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001634 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001635
1636 // Make sure we don't have any things that we know a variable expression
1637 // won't be able to deal with before calling into it
1638 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1639 {
1640 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
Enrico Granatad27026e2012-09-05 20:41:26 +00001641 options.GetUseDynamic(),
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001642 expr_path_options,
1643 var_sp,
1644 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001645 // if this expression results in a bitfield, we give up and let the IR handle it
1646 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1647 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001648 }
Greg Clayton427f2902010-12-14 02:59:59 +00001649 }
1650 else if (m_process_sp)
1651 {
1652 m_process_sp->CalculateExecutionContext(exe_ctx);
1653 }
1654 else
1655 {
1656 CalculateExecutionContext(exe_ctx);
1657 }
1658
1659 if (result_valobj_sp)
1660 {
1661 execution_results = eExecutionCompleted;
1662 // We got a result from the frame variable expression path above...
1663 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1664
1665 lldb::ValueObjectSP const_valobj_sp;
1666
1667 // Check in case our value is already a constant value
1668 if (result_valobj_sp->GetIsConstant())
1669 {
1670 const_valobj_sp = result_valobj_sp;
1671 const_valobj_sp->SetName (persistent_variable_name);
1672 }
1673 else
Jim Inghame41494a2011-04-16 00:01:13 +00001674 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001675 if (options.GetUseDynamic() != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001676 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001677 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(options.GetUseDynamic());
Jim Inghame41494a2011-04-16 00:01:13 +00001678 if (dynamic_sp)
1679 result_valobj_sp = dynamic_sp;
1680 }
1681
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001682 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001683 }
Greg Clayton427f2902010-12-14 02:59:59 +00001684
Sean Callanan6a925532011-01-13 08:53:35 +00001685 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1686
Greg Clayton427f2902010-12-14 02:59:59 +00001687 result_valobj_sp = const_valobj_sp;
1688
Sean Callanan6a925532011-01-13 08:53:35 +00001689 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1690 assert (clang_expr_variable_sp.get());
1691
1692 // Set flags and live data as appropriate
1693
1694 const Value &result_value = live_valobj_sp->GetValue();
1695
1696 switch (result_value.GetValueType())
1697 {
1698 case Value::eValueTypeHostAddress:
1699 case Value::eValueTypeFileAddress:
1700 // we don't do anything with these for now
1701 break;
1702 case Value::eValueTypeScalar:
1703 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1704 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1705 break;
1706 case Value::eValueTypeLoadAddress:
1707 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1708 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1709 break;
1710 }
Greg Clayton427f2902010-12-14 02:59:59 +00001711 }
1712 else
1713 {
1714 // Make sure we aren't just trying to see the value of a persistent
1715 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001716 lldb::ClangExpressionVariableSP persistent_var_sp;
1717 // Only check for persistent variables the expression starts with a '$'
1718 if (expr_cstr[0] == '$')
1719 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1720
Greg Clayton427f2902010-12-14 02:59:59 +00001721 if (persistent_var_sp)
1722 {
1723 result_valobj_sp = persistent_var_sp->GetValueObject ();
1724 execution_results = eExecutionCompleted;
1725 }
1726 else
1727 {
1728 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001729
Greg Clayton427f2902010-12-14 02:59:59 +00001730 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Enrico Granatad27026e2012-09-05 20:41:26 +00001731 options.GetExecutionPolicy(),
Sean Callanan5b658cc2011-11-07 23:35:40 +00001732 lldb::eLanguageTypeUnknown,
Enrico Granatad27026e2012-09-05 20:41:26 +00001733 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1734 options.DoesUnwindOnError(),
Greg Clayton427f2902010-12-14 02:59:59 +00001735 expr_cstr,
1736 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001737 result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001738 options.GetSingleThreadTimeoutUsec());
Greg Clayton427f2902010-12-14 02:59:59 +00001739 }
1740 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001741
1742 m_suppress_stop_hooks = old_suppress_value;
1743
Greg Clayton427f2902010-12-14 02:59:59 +00001744 return execution_results;
1745}
1746
Greg Claytonc0fa5332011-05-22 22:46:53 +00001747lldb::addr_t
1748Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1749{
1750 addr_t code_addr = load_addr;
1751 switch (m_arch.GetMachine())
1752 {
1753 case llvm::Triple::arm:
1754 case llvm::Triple::thumb:
1755 switch (addr_class)
1756 {
1757 case eAddressClassData:
1758 case eAddressClassDebug:
1759 return LLDB_INVALID_ADDRESS;
1760
1761 case eAddressClassUnknown:
1762 case eAddressClassInvalid:
1763 case eAddressClassCode:
1764 case eAddressClassCodeAlternateISA:
1765 case eAddressClassRuntime:
1766 // Check if bit zero it no set?
1767 if ((code_addr & 1ull) == 0)
1768 {
1769 // Bit zero isn't set, check if the address is a multiple of 2?
1770 if (code_addr & 2ull)
1771 {
1772 // The address is a multiple of 2 so it must be thumb, set bit zero
1773 code_addr |= 1ull;
1774 }
1775 else if (addr_class == eAddressClassCodeAlternateISA)
1776 {
1777 // We checked the address and the address claims to be the alternate ISA
1778 // which means thumb, so set bit zero.
1779 code_addr |= 1ull;
1780 }
1781 }
1782 break;
1783 }
1784 break;
1785
1786 default:
1787 break;
1788 }
1789 return code_addr;
1790}
1791
1792lldb::addr_t
1793Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1794{
1795 addr_t opcode_addr = load_addr;
1796 switch (m_arch.GetMachine())
1797 {
1798 case llvm::Triple::arm:
1799 case llvm::Triple::thumb:
1800 switch (addr_class)
1801 {
1802 case eAddressClassData:
1803 case eAddressClassDebug:
1804 return LLDB_INVALID_ADDRESS;
1805
1806 case eAddressClassInvalid:
1807 case eAddressClassUnknown:
1808 case eAddressClassCode:
1809 case eAddressClassCodeAlternateISA:
1810 case eAddressClassRuntime:
1811 opcode_addr &= ~(1ull);
1812 break;
1813 }
1814 break;
1815
1816 default:
1817 break;
1818 }
1819 return opcode_addr;
1820}
1821
Jim Inghamd60d94a2011-03-11 03:53:59 +00001822lldb::user_id_t
1823Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1824{
1825 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001826 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001827 m_stop_hooks[new_uid] = new_hook_sp;
1828 return new_uid;
1829}
1830
1831bool
1832Target::RemoveStopHookByID (lldb::user_id_t user_id)
1833{
1834 size_t num_removed;
1835 num_removed = m_stop_hooks.erase (user_id);
1836 if (num_removed == 0)
1837 return false;
1838 else
1839 return true;
1840}
1841
1842void
1843Target::RemoveAllStopHooks ()
1844{
1845 m_stop_hooks.clear();
1846}
1847
1848Target::StopHookSP
1849Target::GetStopHookByID (lldb::user_id_t user_id)
1850{
1851 StopHookSP found_hook;
1852
1853 StopHookCollection::iterator specified_hook_iter;
1854 specified_hook_iter = m_stop_hooks.find (user_id);
1855 if (specified_hook_iter != m_stop_hooks.end())
1856 found_hook = (*specified_hook_iter).second;
1857 return found_hook;
1858}
1859
1860bool
1861Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1862{
1863 StopHookCollection::iterator specified_hook_iter;
1864 specified_hook_iter = m_stop_hooks.find (user_id);
1865 if (specified_hook_iter == m_stop_hooks.end())
1866 return false;
1867
1868 (*specified_hook_iter).second->SetIsActive (active_state);
1869 return true;
1870}
1871
1872void
1873Target::SetAllStopHooksActiveState (bool active_state)
1874{
1875 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1876 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1877 {
1878 (*pos).second->SetIsActive (active_state);
1879 }
1880}
1881
1882void
1883Target::RunStopHooks ()
1884{
Jim Ingham3613ae12011-05-12 02:06:14 +00001885 if (m_suppress_stop_hooks)
1886 return;
1887
Jim Inghamd60d94a2011-03-11 03:53:59 +00001888 if (!m_process_sp)
1889 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001890
1891 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1892 // since in that case we do not want to run the stop-hooks
1893 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1894 return;
1895
Jim Inghamd60d94a2011-03-11 03:53:59 +00001896 if (m_stop_hooks.empty())
1897 return;
1898
1899 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1900
1901 // If there aren't any active stop hooks, don't bother either:
1902 bool any_active_hooks = false;
1903 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1904 {
1905 if ((*pos).second->IsActive())
1906 {
1907 any_active_hooks = true;
1908 break;
1909 }
1910 }
1911 if (!any_active_hooks)
1912 return;
1913
1914 CommandReturnObject result;
1915
1916 std::vector<ExecutionContext> exc_ctx_with_reasons;
1917 std::vector<SymbolContext> sym_ctx_with_reasons;
1918
1919 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1920 size_t num_threads = cur_threadlist.GetSize();
1921 for (size_t i = 0; i < num_threads; i++)
1922 {
1923 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1924 if (cur_thread_sp->ThreadStoppedForAReason())
1925 {
1926 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1927 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1928 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1929 }
1930 }
1931
1932 // If no threads stopped for a reason, don't run the stop-hooks.
1933 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1934 if (num_exe_ctx == 0)
1935 return;
1936
Jim Inghame5ed8e92011-06-02 23:58:26 +00001937 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1938 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001939
1940 bool keep_going = true;
1941 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001942 bool print_hook_header;
1943 bool print_thread_header;
1944
1945 if (num_exe_ctx == 1)
1946 print_thread_header = false;
1947 else
1948 print_thread_header = true;
1949
1950 if (m_stop_hooks.size() == 1)
1951 print_hook_header = false;
1952 else
1953 print_hook_header = true;
1954
Jim Inghamd60d94a2011-03-11 03:53:59 +00001955 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1956 {
1957 // result.Clear();
1958 StopHookSP cur_hook_sp = (*pos).second;
1959 if (!cur_hook_sp->IsActive())
1960 continue;
1961
1962 bool any_thread_matched = false;
1963 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1964 {
1965 if ((cur_hook_sp->GetSpecifier () == NULL
1966 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1967 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00001968 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001969 {
1970 if (!hooks_ran)
1971 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001972 hooks_ran = true;
1973 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001974 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001975 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001976 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1977 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1978 NULL);
1979 if (cmd)
1980 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1981 else
1982 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001983 any_thread_matched = true;
1984 }
1985
Jim Inghamc54840c2011-03-22 01:47:27 +00001986 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001987 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001988
1989 bool stop_on_continue = true;
1990 bool stop_on_error = true;
1991 bool echo_commands = false;
1992 bool print_results = true;
1993 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001994 &exc_ctx_with_reasons[i],
1995 stop_on_continue,
1996 stop_on_error,
1997 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00001998 print_results,
1999 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002000 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002001
2002 // If the command started the target going again, we should bag out of
2003 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002004 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2005 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002006 {
Greg Clayton444e35b2011-10-19 18:09:39 +00002007 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002008 keep_going = false;
2009 }
2010 }
2011 }
2012 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002013
Caroline Tice4a348082011-05-02 20:41:46 +00002014 result.GetImmediateOutputStream()->Flush();
2015 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002016}
2017
Greg Claytonbbea1332011-07-08 00:48:09 +00002018
Jim Inghamd60d94a2011-03-11 03:53:59 +00002019//--------------------------------------------------------------
2020// class Target::StopHook
2021//--------------------------------------------------------------
2022
2023
2024Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2025 UserID (uid),
2026 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002027 m_commands (),
2028 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002029 m_thread_spec_ap(NULL),
2030 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002031{
2032}
2033
2034Target::StopHook::StopHook (const StopHook &rhs) :
2035 UserID (rhs.GetID()),
2036 m_target_sp (rhs.m_target_sp),
2037 m_commands (rhs.m_commands),
2038 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002039 m_thread_spec_ap (NULL),
2040 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002041{
2042 if (rhs.m_thread_spec_ap.get() != NULL)
2043 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2044}
2045
2046
2047Target::StopHook::~StopHook ()
2048{
2049}
2050
2051void
2052Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2053{
2054 m_thread_spec_ap.reset (specifier);
2055}
2056
2057
2058void
2059Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2060{
2061 int indent_level = s->GetIndentLevel();
2062
2063 s->SetIndentLevel(indent_level + 2);
2064
Greg Clayton444e35b2011-10-19 18:09:39 +00002065 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002066 if (m_active)
2067 s->Indent ("State: enabled\n");
2068 else
2069 s->Indent ("State: disabled\n");
2070
2071 if (m_specifier_sp)
2072 {
2073 s->Indent();
2074 s->PutCString ("Specifier:\n");
2075 s->SetIndentLevel (indent_level + 4);
2076 m_specifier_sp->GetDescription (s, level);
2077 s->SetIndentLevel (indent_level + 2);
2078 }
2079
2080 if (m_thread_spec_ap.get() != NULL)
2081 {
2082 StreamString tmp;
2083 s->Indent("Thread:\n");
2084 m_thread_spec_ap->GetDescription (&tmp, level);
2085 s->SetIndentLevel (indent_level + 4);
2086 s->Indent (tmp.GetData());
2087 s->PutCString ("\n");
2088 s->SetIndentLevel (indent_level + 2);
2089 }
2090
2091 s->Indent ("Commands: \n");
2092 s->SetIndentLevel (indent_level + 4);
2093 uint32_t num_commands = m_commands.GetSize();
2094 for (uint32_t i = 0; i < num_commands; i++)
2095 {
2096 s->Indent(m_commands.GetStringAtIndex(i));
2097 s->PutCString ("\n");
2098 }
2099 s->SetIndentLevel (indent_level);
2100}
2101
Greg Clayton73844aa2012-08-22 17:17:09 +00002102//--------------------------------------------------------------
2103// class TargetProperties
2104//--------------------------------------------------------------
2105
2106OptionEnumValueElement
2107lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002108{
Greg Clayton73844aa2012-08-22 17:17:09 +00002109 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2110 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2111 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2112 { 0, NULL, NULL }
2113};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002114
Greg Clayton49ce8962012-08-29 21:13:06 +00002115static OptionEnumValueElement
2116g_inline_breakpoint_enums[] =
2117{
2118 { 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."},
2119 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2120 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2121 { 0, NULL, NULL }
2122};
2123
Greg Clayton73844aa2012-08-22 17:17:09 +00002124static PropertyDefinition
2125g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002126{
Greg Clayton73844aa2012-08-22 17:17:09 +00002127 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2128 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2129 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2130 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2131 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2132 { "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 "
2133 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2134 "some part (starting at the root) of the path to the file when it was built, "
2135 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2136 "Each element of the array is checked in order and the first one that results in a match wins." },
2137 { "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." },
2138 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2139 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2140 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." },
2141 { "run-args" , OptionValue::eTypeArgs , false, 0 , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run." },
2142 { "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." },
2143 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2144 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2145 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2146 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2147 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2148 { "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 +00002149 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2150 "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. "
2151 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2152 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2153 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2154 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2155 "file and line breakpoints." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002156 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2157};
2158enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002159{
Greg Clayton73844aa2012-08-22 17:17:09 +00002160 ePropertyDefaultArch,
2161 ePropertyExprPrefix,
2162 ePropertyPreferDynamic,
2163 ePropertyEnableSynthetic,
2164 ePropertySkipPrologue,
2165 ePropertySourceMap,
2166 ePropertyExecutableSearchPaths,
2167 ePropertyMaxChildrenCount,
2168 ePropertyMaxSummaryLength,
2169 ePropertyBreakpointUseAvoidList,
2170 ePropertyRunArgs,
2171 ePropertyEnvVars,
2172 ePropertyInheritEnv,
2173 ePropertyInputPath,
2174 ePropertyOutputPath,
2175 ePropertyErrorPath,
2176 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002177 ePropertyDisableSTDIO,
2178 ePropertyInlineStrategy
Greg Clayton73844aa2012-08-22 17:17:09 +00002179};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002180
Caroline Tice5bc8c972010-09-20 20:44:43 +00002181
Greg Clayton73844aa2012-08-22 17:17:09 +00002182class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002183{
Greg Clayton73844aa2012-08-22 17:17:09 +00002184public:
2185 TargetOptionValueProperties (const ConstString &name) :
2186 OptionValueProperties (name),
2187 m_target (NULL),
2188 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002189 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002190 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002191
Greg Clayton73844aa2012-08-22 17:17:09 +00002192 // This constructor is used when creating TargetOptionValueProperties when it
2193 // is part of a new lldb_private::Target instance. It will copy all current
2194 // global property values as needed
2195 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2196 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2197 m_target (target),
2198 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002199 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002200 }
2201
2202 virtual const Property *
2203 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2204 {
2205 // When gettings the value for a key from the target options, we will always
2206 // try and grab the setting from the current target if there is one. Else we just
2207 // use the one from this instance.
2208 if (idx == ePropertyEnvVars)
2209 GetHostEnvironmentIfNeeded ();
2210
2211 if (exe_ctx)
2212 {
2213 Target *target = exe_ctx->GetTargetPtr();
2214 if (target)
2215 {
2216 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2217 if (this != target_properties)
2218 return target_properties->ProtectedGetPropertyAtIndex (idx);
2219 }
2220 }
2221 return ProtectedGetPropertyAtIndex (idx);
2222 }
2223protected:
2224
2225 void
2226 GetHostEnvironmentIfNeeded () const
2227 {
2228 if (!m_got_host_env)
2229 {
2230 if (m_target)
2231 {
2232 m_got_host_env = true;
2233 const uint32_t idx = ePropertyInheritEnv;
2234 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2235 {
2236 PlatformSP platform_sp (m_target->GetPlatform());
2237 if (platform_sp)
2238 {
2239 StringList env;
2240 if (platform_sp->GetEnvironment(env))
2241 {
2242 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2243 if (env_dict)
2244 {
2245 const bool can_replace = false;
2246 const size_t envc = env.GetSize();
2247 for (size_t idx=0; idx<envc; idx++)
2248 {
2249 const char *env_entry = env.GetStringAtIndex (idx);
2250 if (env_entry)
2251 {
2252 const char *equal_pos = ::strchr(env_entry, '=');
2253 ConstString key;
2254 // It is ok to have environment variables with no values
2255 const char *value = NULL;
2256 if (equal_pos)
2257 {
2258 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2259 if (equal_pos[1])
2260 value = equal_pos + 1;
2261 }
2262 else
2263 {
2264 key.SetCString(env_entry);
2265 }
2266 // Don't allow existing keys to be replaced with ones we get from the platform environment
2267 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2268 }
2269 }
2270 }
2271 }
2272 }
2273 }
2274 }
2275 }
2276 }
2277 Target *m_target;
2278 mutable bool m_got_host_env;
2279};
2280
2281TargetProperties::TargetProperties (Target *target) :
2282 Properties ()
2283{
2284 if (target)
2285 {
2286 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002287 }
2288 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002289 {
2290 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2291 m_collection_sp->Initialize(g_properties);
2292 m_collection_sp->AppendProperty(ConstString("process"),
2293 ConstString("Settings specify to processes."),
2294 true,
2295 Process::GetGlobalProperties()->GetValueProperties());
2296 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002297}
2298
Greg Clayton73844aa2012-08-22 17:17:09 +00002299TargetProperties::~TargetProperties ()
2300{
2301}
2302ArchSpec
2303TargetProperties::GetDefaultArchitecture () const
2304{
2305 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2306 if (value)
2307 return value->GetCurrentValue();
2308 return ArchSpec();
2309}
2310
2311void
2312TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2313{
2314 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2315 if (value)
2316 return value->SetCurrentValue(arch, true);
2317}
2318
2319lldb::DynamicValueType
2320TargetProperties::GetPreferDynamicValue() const
2321{
2322 const uint32_t idx = ePropertyPreferDynamic;
2323 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2324}
2325
2326bool
2327TargetProperties::GetDisableASLR () const
2328{
2329 const uint32_t idx = ePropertyDisableASLR;
2330 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2331}
2332
2333void
2334TargetProperties::SetDisableASLR (bool b)
2335{
2336 const uint32_t idx = ePropertyDisableASLR;
2337 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2338}
2339
2340bool
2341TargetProperties::GetDisableSTDIO () const
2342{
2343 const uint32_t idx = ePropertyDisableSTDIO;
2344 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2345}
2346
2347void
2348TargetProperties::SetDisableSTDIO (bool b)
2349{
2350 const uint32_t idx = ePropertyDisableSTDIO;
2351 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2352}
2353
Greg Clayton49ce8962012-08-29 21:13:06 +00002354InlineStrategy
2355TargetProperties::GetInlineStrategy () const
2356{
2357 const uint32_t idx = ePropertyInlineStrategy;
2358 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2359}
2360
Greg Clayton73844aa2012-08-22 17:17:09 +00002361bool
2362TargetProperties::GetRunArguments (Args &args) const
2363{
2364 const uint32_t idx = ePropertyRunArgs;
2365 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2366}
2367
2368void
2369TargetProperties::SetRunArguments (const Args &args)
2370{
2371 const uint32_t idx = ePropertyRunArgs;
2372 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2373}
2374
2375size_t
2376TargetProperties::GetEnvironmentAsArgs (Args &env) const
2377{
2378 const uint32_t idx = ePropertyEnvVars;
2379 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2380}
2381
2382bool
2383TargetProperties::GetSkipPrologue() const
2384{
2385 const uint32_t idx = ePropertySkipPrologue;
2386 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2387}
2388
2389PathMappingList &
2390TargetProperties::GetSourcePathMap () const
2391{
2392 const uint32_t idx = ePropertySourceMap;
2393 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2394 assert(option_value);
2395 return option_value->GetCurrentValue();
2396}
2397
2398FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002399TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002400{
2401 const uint32_t idx = ePropertyExecutableSearchPaths;
2402 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2403 assert(option_value);
2404 return option_value->GetCurrentValue();
2405}
2406
2407bool
2408TargetProperties::GetEnableSyntheticValue () const
2409{
2410 const uint32_t idx = ePropertyEnableSynthetic;
2411 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2412}
2413
2414uint32_t
2415TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2416{
2417 const uint32_t idx = ePropertyMaxChildrenCount;
2418 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2419}
2420
2421uint32_t
2422TargetProperties::GetMaximumSizeOfStringSummary() const
2423{
2424 const uint32_t idx = ePropertyMaxSummaryLength;
2425 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2426}
2427
2428FileSpec
2429TargetProperties::GetStandardInputPath () const
2430{
2431 const uint32_t idx = ePropertyInputPath;
2432 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2433}
2434
2435void
2436TargetProperties::SetStandardInputPath (const char *p)
2437{
2438 const uint32_t idx = ePropertyInputPath;
2439 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2440}
2441
2442FileSpec
2443TargetProperties::GetStandardOutputPath () const
2444{
2445 const uint32_t idx = ePropertyOutputPath;
2446 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2447}
2448
2449void
2450TargetProperties::SetStandardOutputPath (const char *p)
2451{
2452 const uint32_t idx = ePropertyOutputPath;
2453 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2454}
2455
2456FileSpec
2457TargetProperties::GetStandardErrorPath () const
2458{
2459 const uint32_t idx = ePropertyErrorPath;
2460 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2461}
2462
Greg Claytonc6e82e42012-08-22 18:39:03 +00002463const char *
2464TargetProperties::GetExpressionPrefixContentsAsCString ()
2465{
2466 const uint32_t idx = ePropertyExprPrefix;
2467 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2468 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002469 {
Greg Claytonfc04d242012-08-30 18:15:10 +00002470 const bool null_terminate = true;
2471 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham7021cda2012-08-22 21:21:16 +00002472 if (data_sp)
2473 return (const char *) data_sp->GetBytes();
2474 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002475 return NULL;
2476}
2477
Greg Clayton73844aa2012-08-22 17:17:09 +00002478void
2479TargetProperties::SetStandardErrorPath (const char *p)
2480{
2481 const uint32_t idx = ePropertyErrorPath;
2482 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2483}
2484
2485bool
2486TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2487{
2488 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2489 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2490}
2491
2492const TargetPropertiesSP &
2493Target::GetGlobalProperties()
2494{
2495 static TargetPropertiesSP g_settings_sp;
2496 if (!g_settings_sp)
2497 {
2498 g_settings_sp.reset (new TargetProperties (NULL));
2499 }
2500 return g_settings_sp;
2501}
2502
Jim Ingham5a15e692012-02-16 06:50:00 +00002503const ConstString &
2504Target::TargetEventData::GetFlavorString ()
2505{
2506 static ConstString g_flavor ("Target::TargetEventData");
2507 return g_flavor;
2508}
2509
2510const ConstString &
2511Target::TargetEventData::GetFlavor () const
2512{
2513 return TargetEventData::GetFlavorString ();
2514}
2515
2516Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2517 EventData(),
2518 m_target_sp (new_target_sp)
2519{
2520}
2521
2522Target::TargetEventData::~TargetEventData()
2523{
2524
2525}
2526
2527void
2528Target::TargetEventData::Dump (Stream *s) const
2529{
2530
2531}
2532
2533const TargetSP
2534Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2535{
2536 TargetSP target_sp;
2537
2538 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2539 if (data)
2540 target_sp = data->m_target_sp;
2541
2542 return target_sp;
2543}
2544
2545const Target::TargetEventData *
2546Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2547{
2548 if (event_ptr)
2549 {
2550 const EventData *event_data = event_ptr->GetData();
2551 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2552 return static_cast <const TargetEventData *> (event_ptr->GetData());
2553 }
2554 return NULL;
2555}
2556