blob: 433ddd036cd8d268633dbdab5b33291f3cdd39da [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 Ingham94a5d0d2012-10-10 18:32:14 +000061 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
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),
Enrico Granata146d9522012-11-08 02:22:02 +000067 m_images (this),
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
Greg Clayton0bce9a22012-12-05 00:16:59 +0000133Target::CleanupProcess ()
134{
135 // Do any cleanup of the target we need to do between process instances.
136 // NB It is better to do this before destroying the process in case the
137 // clean up needs some help from the process.
138 m_breakpoint_list.ClearAllBreakpointSites();
139 m_internal_breakpoint_list.ClearAllBreakpointSites();
140 // Disable watchpoints just on the debugger side.
141 Mutex::Locker locker;
142 this->GetWatchpointList().GetListMutex(locker);
143 DisableAllWatchpoints(false);
144 ClearAllWatchpointHitCounts();
145}
146
147void
Chris Lattner24943d22010-06-08 16:52:24 +0000148Target::DeleteCurrentProcess ()
149{
150 if (m_process_sp.get())
151 {
Greg Clayton49480b12010-09-14 23:52:43 +0000152 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000153 if (m_process_sp->IsAlive())
154 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000155
156 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000157
Greg Clayton0bce9a22012-12-05 00:16:59 +0000158 CleanupProcess ();
159
Chris Lattner24943d22010-06-08 16:52:24 +0000160 m_process_sp.reset();
161 }
162}
163
164const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000165Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000166{
167 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000168 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000169 return m_process_sp;
170}
171
172const lldb::ProcessSP &
173Target::GetProcessSP () const
174{
175 return m_process_sp;
176}
177
Greg Clayton153ccd72011-08-10 02:10:13 +0000178void
179Target::Destroy()
180{
181 Mutex::Locker locker (m_mutex);
Filipe Cabecinhasf7d782b2012-05-19 09:59:08 +0000182 m_valid = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000183 DeleteCurrentProcess ();
184 m_platform_sp.reset();
185 m_arch.Clear();
186 m_images.Clear();
187 m_section_load_list.Clear();
188 const bool notify = false;
189 m_breakpoint_list.RemoveAll(notify);
190 m_internal_breakpoint_list.RemoveAll(notify);
191 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000192 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000193 m_search_filter_sp.reset();
194 m_image_search_paths.Clear(notify);
195 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000196 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000197 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000198 m_persistent_variables.Clear();
199 m_stop_hooks.clear();
200 m_stop_hook_next_id = 0;
201 m_suppress_stop_hooks = false;
Enrico Granatadba1de82012-03-27 02:35:13 +0000202 m_suppress_synthetic_value = false;
Greg Clayton153ccd72011-08-10 02:10:13 +0000203}
204
205
Chris Lattner24943d22010-06-08 16:52:24 +0000206BreakpointList &
207Target::GetBreakpointList(bool internal)
208{
209 if (internal)
210 return m_internal_breakpoint_list;
211 else
212 return m_breakpoint_list;
213}
214
215const BreakpointList &
216Target::GetBreakpointList(bool internal) const
217{
218 if (internal)
219 return m_internal_breakpoint_list;
220 else
221 return m_breakpoint_list;
222}
223
224BreakpointSP
225Target::GetBreakpointByID (break_id_t break_id)
226{
227 BreakpointSP bp_sp;
228
229 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
230 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
231 else
232 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
233
234 return bp_sp;
235}
236
237BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000238Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton49ce8962012-08-29 21:13:06 +0000239 const FileSpecList *source_file_spec_list,
240 RegularExpression &source_regex,
241 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000242{
Jim Inghamd6d47972011-09-23 00:54:11 +0000243 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
244 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000245 return CreateBreakpoint (filter_sp, resolver_sp, internal);
246}
247
248
249BreakpointSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000250Target::CreateBreakpoint (const FileSpecList *containingModules,
251 const FileSpec &file,
252 uint32_t line_no,
Greg Clayton49ce8962012-08-29 21:13:06 +0000253 LazyBool check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000254 LazyBool skip_prologue,
255 bool internal)
Jim Ingham03c8ee52011-09-21 01:17:13 +0000256{
Greg Clayton49ce8962012-08-29 21:13:06 +0000257 if (check_inlines == eLazyBoolCalculate)
258 {
259 const InlineStrategy inline_strategy = GetInlineStrategy();
260 switch (inline_strategy)
261 {
262 case eInlineBreakpointsNever:
263 check_inlines = eLazyBoolNo;
264 break;
265
266 case eInlineBreakpointsHeaders:
267 if (file.IsSourceImplementationFile())
268 check_inlines = eLazyBoolNo;
269 else
270 check_inlines = eLazyBoolYes;
271 break;
272
273 case eInlineBreakpointsAlways:
274 check_inlines = eLazyBoolYes;
275 break;
276 }
277 }
Greg Clayton46365522012-09-07 23:48:57 +0000278 SearchFilterSP filter_sp;
279 if (check_inlines == eLazyBoolNo)
280 {
281 // Not checking for inlines, we are looking only for matching compile units
282 FileSpecList compile_unit_list;
283 compile_unit_list.Append (file);
284 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
285 }
286 else
287 {
288 filter_sp = GetSearchFilterForModuleList (containingModules);
289 }
Greg Clayton49ce8962012-08-29 21:13:06 +0000290 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
291 file,
292 line_no,
293 check_inlines,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000294 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000295 return CreateBreakpoint (filter_sp, resolver_sp, internal);
296}
297
298
299BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000300Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000301{
Chris Lattner24943d22010-06-08 16:52:24 +0000302 Address so_addr;
303 // Attempt to resolve our load address if possible, though it is ok if
304 // it doesn't resolve to section/offset.
305
Greg Clayton33ed1702010-08-24 00:45:41 +0000306 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000307 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000308 if (!so_addr.IsValid())
309 {
310 // The address didn't resolve, so just set this as an absolute address
311 so_addr.SetOffset (addr);
312 }
313 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000314 return bp_sp;
315}
316
317BreakpointSP
318Target::CreateBreakpoint (Address &addr, bool internal)
319{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000320 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000321 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
322 return CreateBreakpoint (filter_sp, resolver_sp, internal);
323}
324
325BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000326Target::CreateBreakpoint (const FileSpecList *containingModules,
327 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000328 const char *func_name,
329 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000330 LazyBool skip_prologue,
331 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000332{
Greg Clayton12bec712010-06-28 21:30:43 +0000333 BreakpointSP bp_sp;
334 if (func_name)
335 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000336 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000337
338 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
339 func_name,
340 func_name_type_mask,
341 Breakpoint::Exact,
342 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000343 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
344 }
345 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000346}
347
Jim Ingham4722b102012-03-06 00:37:27 +0000348lldb::BreakpointSP
349Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Claytonbd5c23d2012-05-15 02:33:01 +0000350 const FileSpecList *containingSourceFiles,
351 const std::vector<std::string> &func_names,
352 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000353 LazyBool skip_prologue,
354 bool internal)
Jim Ingham4722b102012-03-06 00:37:27 +0000355{
356 BreakpointSP bp_sp;
357 size_t num_names = func_names.size();
358 if (num_names > 0)
359 {
360 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
361
362 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
363 func_names,
364 func_name_type_mask,
365 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
366 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
367 }
368 return bp_sp;
369}
370
Jim Inghamc1053622012-03-03 02:05:11 +0000371BreakpointSP
372Target::CreateBreakpoint (const FileSpecList *containingModules,
373 const FileSpecList *containingSourceFiles,
374 const char *func_names[],
375 size_t num_names,
376 uint32_t func_name_type_mask,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000377 LazyBool skip_prologue,
378 bool internal)
Jim Inghamc1053622012-03-03 02:05:11 +0000379{
380 BreakpointSP bp_sp;
381 if (num_names > 0)
382 {
383 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
384
385 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
386 func_names,
387 num_names,
Jim Ingham4722b102012-03-06 00:37:27 +0000388 func_name_type_mask,
Jim Inghamc1053622012-03-03 02:05:11 +0000389 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
390 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
391 }
392 return bp_sp;
393}
Chris Lattner24943d22010-06-08 16:52:24 +0000394
395SearchFilterSP
396Target::GetSearchFilterForModule (const FileSpec *containingModule)
397{
398 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000399 if (containingModule != NULL)
400 {
401 // TODO: We should look into sharing module based search filters
402 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000403 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000404 }
405 else
406 {
407 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000408 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000409 filter_sp = m_search_filter_sp;
410 }
411 return filter_sp;
412}
413
Jim Ingham03c8ee52011-09-21 01:17:13 +0000414SearchFilterSP
415Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
416{
417 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000418 if (containingModules && containingModules->GetSize() != 0)
419 {
420 // TODO: We should look into sharing module based search filters
421 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000422 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000423 }
424 else
425 {
426 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000427 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000428 filter_sp = m_search_filter_sp;
429 }
430 return filter_sp;
431}
432
Jim Inghamd6d47972011-09-23 00:54:11 +0000433SearchFilterSP
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000434Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
435 const FileSpecList *containingSourceFiles)
Jim Inghamd6d47972011-09-23 00:54:11 +0000436{
437 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
438 return GetSearchFilterForModuleList(containingModules);
439
440 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000441 if (containingModules == NULL)
442 {
443 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
444 // but that will take a little reworking.
445
Greg Clayton13d24fb2012-01-29 20:56:30 +0000446 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000447 }
448 else
449 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000450 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000451 }
452 return filter_sp;
453}
454
Chris Lattner24943d22010-06-08 16:52:24 +0000455BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000456Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Ingham2cf5ccb2012-05-22 00:12:20 +0000457 const FileSpecList *containingSourceFiles,
458 RegularExpression &func_regex,
459 LazyBool skip_prologue,
460 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000461{
Jim Inghamd6d47972011-09-23 00:54:11 +0000462 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000463 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
464 func_regex,
465 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000466
467 return CreateBreakpoint (filter_sp, resolver_sp, internal);
468}
469
Jim Ingham3df164e2012-03-05 04:47:34 +0000470lldb::BreakpointSP
471Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
472{
473 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
474}
475
Chris Lattner24943d22010-06-08 16:52:24 +0000476BreakpointSP
477Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
478{
479 BreakpointSP bp_sp;
480 if (filter_sp && resolver_sp)
481 {
482 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
483 resolver_sp->SetBreakpoint (bp_sp.get());
484
485 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000486 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000487 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000488 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000489
Greg Claytone005f2c2010-11-06 01:53:30 +0000490 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000491 if (log)
492 {
493 StreamString s;
494 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
495 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
496 }
497
Chris Lattner24943d22010-06-08 16:52:24 +0000498 bp_sp->ResolveBreakpoint();
499 }
Jim Inghamd1686902010-10-14 23:45:03 +0000500
501 if (!internal && bp_sp)
502 {
503 m_last_created_breakpoint = bp_sp;
504 }
505
Chris Lattner24943d22010-06-08 16:52:24 +0000506 return bp_sp;
507}
508
Johnny Chenda5a8022011-09-20 23:28:55 +0000509bool
510Target::ProcessIsValid()
511{
512 return (m_process_sp && m_process_sp->IsAlive());
513}
514
Johnny Chen3f883492012-06-04 23:19:54 +0000515static bool
516CheckIfWatchpointsExhausted(Target *target, Error &error)
517{
518 uint32_t num_supported_hardware_watchpoints;
519 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
520 if (rc.Success())
521 {
522 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
523 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
524 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
525 num_supported_hardware_watchpoints);
526 }
527 return false;
528}
529
Johnny Chenecd4feb2011-10-14 00:42:25 +0000530// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000531// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000532WatchpointSP
Jim Ingham9e376622012-10-23 07:20:06 +0000533Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen34bbf852011-09-12 23:38:44 +0000534{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000535 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
536 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000537 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Ingham9e376622012-10-23 07:20:06 +0000538 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen5b2fc572011-09-14 20:23:45 +0000539
Johnny Chenecd4feb2011-10-14 00:42:25 +0000540 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000541 if (!ProcessIsValid())
Johnny Chen3f883492012-06-04 23:19:54 +0000542 {
543 error.SetErrorString("process is not alive");
Johnny Chenecd4feb2011-10-14 00:42:25 +0000544 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000545 }
Johnny Chen22a56cc2011-09-14 22:20:15 +0000546 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen3f883492012-06-04 23:19:54 +0000547 {
548 if (size == 0)
549 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
550 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000551 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000552 return wp_sp;
Johnny Chen3f883492012-06-04 23:19:54 +0000553 }
Johnny Chen9bf11992011-09-13 01:15:36 +0000554
Johnny Chenecd4feb2011-10-14 00:42:25 +0000555 // Currently we only support one watchpoint per address, with total number
556 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chenbbf6aa52012-05-31 22:56:36 +0000557
558 // Grab the list mutex while doing operations.
559 Mutex::Locker locker;
560 this->GetWatchpointList().GetListMutex(locker);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000561 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000562 if (matched_sp)
563 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000564 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000565 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000566 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
567 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000568 // Return the existing watchpoint if both size and type match.
Jim Ingham9e376622012-10-23 07:20:06 +0000569 if (size == old_size && kind == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000570 wp_sp = matched_sp;
571 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000572 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000573 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000574 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000575 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000576 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000577 }
578
Johnny Chenecd4feb2011-10-14 00:42:25 +0000579 if (!wp_sp) {
Jim Ingham9e376622012-10-23 07:20:06 +0000580 Watchpoint *new_wp = new Watchpoint(*this, addr, size, type);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000581 if (!new_wp) {
582 printf("Watchpoint ctor failed, out of memory?\n");
583 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000584 }
Jim Ingham9e376622012-10-23 07:20:06 +0000585 new_wp->SetWatchpointType(kind);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000586 wp_sp.reset(new_wp);
587 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000588 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000589
Johnny Chen3f883492012-06-04 23:19:54 +0000590 error = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000591 if (log)
592 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
593 __FUNCTION__,
Johnny Chen3f883492012-06-04 23:19:54 +0000594 error.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000595 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000596
Johnny Chen3f883492012-06-04 23:19:54 +0000597 if (error.Fail()) {
Johnny Chen155599b2012-03-26 22:00:10 +0000598 // Enabling the watchpoint on the device side failed.
599 // Remove the said watchpoint from the list maintained by the target instance.
600 m_watchpoint_list.Remove(wp_sp->GetID());
Johnny Chen3f883492012-06-04 23:19:54 +0000601 // See if we could provide more helpful error message.
602 if (!CheckIfWatchpointsExhausted(this, error))
603 {
604 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
605 error.SetErrorStringWithFormat("watch size of %lu is not supported", size);
606 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000607 wp_sp.reset();
Johnny Chen155599b2012-03-26 22:00:10 +0000608 }
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000609 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000610 m_last_created_watchpoint = wp_sp;
611 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000612}
613
Chris Lattner24943d22010-06-08 16:52:24 +0000614void
615Target::RemoveAllBreakpoints (bool internal_also)
616{
Greg Claytone005f2c2010-11-06 01:53:30 +0000617 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000618 if (log)
619 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
620
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000621 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000622 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000623 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000624
625 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000626}
627
628void
629Target::DisableAllBreakpoints (bool internal_also)
630{
Greg Claytone005f2c2010-11-06 01:53:30 +0000631 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000632 if (log)
633 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
634
635 m_breakpoint_list.SetEnabledAll (false);
636 if (internal_also)
637 m_internal_breakpoint_list.SetEnabledAll (false);
638}
639
640void
641Target::EnableAllBreakpoints (bool internal_also)
642{
Greg Claytone005f2c2010-11-06 01:53:30 +0000643 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000644 if (log)
645 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
646
647 m_breakpoint_list.SetEnabledAll (true);
648 if (internal_also)
649 m_internal_breakpoint_list.SetEnabledAll (true);
650}
651
652bool
653Target::RemoveBreakpointByID (break_id_t break_id)
654{
Greg Claytone005f2c2010-11-06 01:53:30 +0000655 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000656 if (log)
657 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
658
659 if (DisableBreakpointByID (break_id))
660 {
661 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000662 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000663 else
Jim Inghamd1686902010-10-14 23:45:03 +0000664 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000665 if (m_last_created_breakpoint)
666 {
667 if (m_last_created_breakpoint->GetID() == break_id)
668 m_last_created_breakpoint.reset();
669 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000670 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000671 }
Chris Lattner24943d22010-06-08 16:52:24 +0000672 return true;
673 }
674 return false;
675}
676
677bool
678Target::DisableBreakpointByID (break_id_t break_id)
679{
Greg Claytone005f2c2010-11-06 01:53:30 +0000680 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000681 if (log)
682 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
683
684 BreakpointSP bp_sp;
685
686 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
687 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
688 else
689 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
690 if (bp_sp)
691 {
692 bp_sp->SetEnabled (false);
693 return true;
694 }
695 return false;
696}
697
698bool
699Target::EnableBreakpointByID (break_id_t break_id)
700{
Greg Claytone005f2c2010-11-06 01:53:30 +0000701 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000702 if (log)
703 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
704 __FUNCTION__,
705 break_id,
706 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
707
708 BreakpointSP bp_sp;
709
710 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
711 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
712 else
713 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
714
715 if (bp_sp)
716 {
717 bp_sp->SetEnabled (true);
718 return true;
719 }
720 return false;
721}
722
Johnny Chenc86582f2011-09-23 21:21:43 +0000723// The flag 'end_to_end', default to true, signifies that the operation is
724// performed end to end, for both the debugger and the debuggee.
725
Johnny Chenecd4feb2011-10-14 00:42:25 +0000726// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
727// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000728bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000729Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000730{
731 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
732 if (log)
733 log->Printf ("Target::%s\n", __FUNCTION__);
734
Johnny Chenc86582f2011-09-23 21:21:43 +0000735 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000736 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000737 return true;
738 }
739
740 // Otherwise, it's an end to end operation.
741
Johnny Chenda5a8022011-09-20 23:28:55 +0000742 if (!ProcessIsValid())
743 return false;
744
Johnny Chenecd4feb2011-10-14 00:42:25 +0000745 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000746 for (size_t i = 0; i < num_watchpoints; ++i)
747 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000748 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
749 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000750 return false;
751
Johnny Chenecd4feb2011-10-14 00:42:25 +0000752 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000753 if (rc.Fail())
754 return false;
755 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000756 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000757 return true; // Success!
758}
759
Johnny Chenecd4feb2011-10-14 00:42:25 +0000760// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
761// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000762bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000763Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000764{
765 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
766 if (log)
767 log->Printf ("Target::%s\n", __FUNCTION__);
768
Johnny Chenc86582f2011-09-23 21:21:43 +0000769 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000770 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000771 return true;
772 }
773
774 // Otherwise, it's an end to end operation.
775
Johnny Chenda5a8022011-09-20 23:28:55 +0000776 if (!ProcessIsValid())
777 return false;
778
Johnny Chenecd4feb2011-10-14 00:42:25 +0000779 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000780 for (size_t i = 0; i < num_watchpoints; ++i)
781 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000782 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
783 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000784 return false;
785
Johnny Chenecd4feb2011-10-14 00:42:25 +0000786 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000787 if (rc.Fail())
788 return false;
789 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000790 return true; // Success!
791}
792
Johnny Chenecd4feb2011-10-14 00:42:25 +0000793// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
794// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000795bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000796Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000797{
798 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
799 if (log)
800 log->Printf ("Target::%s\n", __FUNCTION__);
801
Johnny Chenc86582f2011-09-23 21:21:43 +0000802 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000803 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000804 return true;
805 }
806
807 // Otherwise, it's an end to end operation.
808
Johnny Chenda5a8022011-09-20 23:28:55 +0000809 if (!ProcessIsValid())
810 return false;
811
Johnny Chenecd4feb2011-10-14 00:42:25 +0000812 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000813 for (size_t i = 0; i < num_watchpoints; ++i)
814 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000815 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
816 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000817 return false;
818
Johnny Chenecd4feb2011-10-14 00:42:25 +0000819 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000820 if (rc.Fail())
821 return false;
822 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000823 return true; // Success!
824}
825
Johnny Chen116a5cd2012-02-25 06:44:30 +0000826// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
827bool
828Target::ClearAllWatchpointHitCounts ()
829{
830 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
831 if (log)
832 log->Printf ("Target::%s\n", __FUNCTION__);
833
834 size_t num_watchpoints = m_watchpoint_list.GetSize();
835 for (size_t i = 0; i < num_watchpoints; ++i)
836 {
837 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
838 if (!wp_sp)
839 return false;
840
841 wp_sp->ResetHitCount();
842 }
843 return true; // Success!
844}
845
Johnny Chenecd4feb2011-10-14 00:42:25 +0000846// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000847// during these operations.
848bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000849Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000850{
851 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
852 if (log)
853 log->Printf ("Target::%s\n", __FUNCTION__);
854
855 if (!ProcessIsValid())
856 return false;
857
Johnny Chenecd4feb2011-10-14 00:42:25 +0000858 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000859 for (size_t i = 0; i < num_watchpoints; ++i)
860 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000861 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
862 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000863 return false;
864
Johnny Chenecd4feb2011-10-14 00:42:25 +0000865 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000866 }
867 return true; // Success!
868}
869
Johnny Chenecd4feb2011-10-14 00:42:25 +0000870// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000871bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000872Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000873{
874 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
875 if (log)
876 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
877
878 if (!ProcessIsValid())
879 return false;
880
Johnny Chenecd4feb2011-10-14 00:42:25 +0000881 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
882 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000883 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000884 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000885 if (rc.Success())
886 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000887
Johnny Chen01acfa72011-09-22 18:04:58 +0000888 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000889 }
890 return false;
891}
892
Johnny Chenecd4feb2011-10-14 00:42:25 +0000893// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000894bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000895Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000896{
897 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
898 if (log)
899 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
900
901 if (!ProcessIsValid())
902 return false;
903
Johnny Chenecd4feb2011-10-14 00:42:25 +0000904 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
905 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000906 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000907 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000908 if (rc.Success())
909 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000910
Johnny Chen01acfa72011-09-22 18:04:58 +0000911 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000912 }
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 Chenda5a8022011-09-20 23:28:55 +0000917bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000918Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +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
Johnny Chenecd4feb2011-10-14 00:42:25 +0000924 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000925 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000926 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000927 return true;
928 }
929 return false;
930}
931
Johnny Chenecd4feb2011-10-14 00:42:25 +0000932// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000933bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000934Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000935{
936 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
937 if (log)
938 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
939
940 if (!ProcessIsValid())
941 return false;
942
Johnny Chenecd4feb2011-10-14 00:42:25 +0000943 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
944 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000945 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000946 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000947 return true;
948 }
949 return false;
950}
951
Chris Lattner24943d22010-06-08 16:52:24 +0000952ModuleSP
953Target::GetExecutableModule ()
954{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000955 return m_images.GetModuleAtIndex(0);
956}
957
958Module*
959Target::GetExecutableModulePointer ()
960{
961 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000962}
963
Enrico Granata146d9522012-11-08 02:22:02 +0000964static void
965LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
966{
967 Error error;
968 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error))
969 {
970 target->GetDebugger().GetOutputStream().Printf("unable to load scripting data for module %s - error reported was %s\n",
971 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
972 error.AsCString());
973 }
974}
975
Chris Lattner24943d22010-06-08 16:52:24 +0000976void
977Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
978{
979 m_images.Clear();
980 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000981 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000982 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000983
984 if (executable_sp.get())
985 {
986 Timer scoped_timer (__PRETTY_FUNCTION__,
987 "Target::SetExecutableModule (executable = '%s/%s')",
988 executable_sp->GetFileSpec().GetDirectory().AsCString(),
989 executable_sp->GetFileSpec().GetFilename().AsCString());
990
991 m_images.Append(executable_sp); // The first image is our exectuable file
992
Jim Ingham7508e732010-08-09 23:31:02 +0000993 // 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 +0000994 if (!m_arch.IsValid())
995 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000996
Chris Lattner24943d22010-06-08 16:52:24 +0000997 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000998 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000999
Greg Clayton3e8c25f2011-09-24 00:52:29 +00001000 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +00001001 {
1002 executable_objfile->GetDependentModules(dependent_files);
1003 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1004 {
Greg Claytonb1888f22011-03-19 01:12:21 +00001005 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1006 FileSpec platform_dependent_file_spec;
1007 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +00001008 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +00001009 else
1010 platform_dependent_file_spec = dependent_file_spec;
1011
Greg Clayton444fe992012-02-26 05:51:37 +00001012 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1013 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner24943d22010-06-08 16:52:24 +00001014 if (image_module_sp.get())
1015 {
Chris Lattner24943d22010-06-08 16:52:24 +00001016 ObjectFile *objfile = image_module_sp->GetObjectFile();
1017 if (objfile)
1018 objfile->GetDependentModules(dependent_files);
1019 }
1020 }
1021 }
Chris Lattner24943d22010-06-08 16:52:24 +00001022 }
1023}
1024
1025
Jim Ingham7508e732010-08-09 23:31:02 +00001026bool
1027Target::SetArchitecture (const ArchSpec &arch_spec)
1028{
Greg Claytonb170aee2012-05-08 01:45:38 +00001029 if (m_arch == arch_spec || !m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +00001030 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001031 // If we haven't got a valid arch spec, or the architectures are
1032 // compatible, so just update the architecture. Architectures can be
1033 // equal, yet the triple OS and vendor might change, so we need to do
1034 // the assignment here just in case.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001035 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001036 return true;
1037 }
1038 else
1039 {
1040 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +00001041 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +00001042 ModuleSP executable_sp = GetExecutableModule ();
1043 m_images.Clear();
1044 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +00001045 m_scratch_ast_source_ap.reset();
1046 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +00001047 // Need to do something about unsetting breakpoints.
1048
1049 if (executable_sp)
1050 {
Greg Clayton444fe992012-02-26 05:51:37 +00001051 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1052 Error error = ModuleList::GetSharedModule (module_spec,
1053 executable_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001054 &GetExecutableSearchPaths(),
Greg Clayton444fe992012-02-26 05:51:37 +00001055 NULL,
1056 NULL);
Jim Ingham7508e732010-08-09 23:31:02 +00001057
1058 if (!error.Fail() && executable_sp)
1059 {
1060 SetExecutableModule (executable_sp, true);
1061 return true;
1062 }
Jim Ingham7508e732010-08-09 23:31:02 +00001063 }
1064 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001065 return false;
Jim Ingham7508e732010-08-09 23:31:02 +00001066}
Chris Lattner24943d22010-06-08 16:52:24 +00001067
Chris Lattner24943d22010-06-08 16:52:24 +00001068void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001069Target::WillClearList (const ModuleList& module_list)
Enrico Granata146d9522012-11-08 02:22:02 +00001070{
1071}
1072
1073void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001074Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001075{
1076 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001077 ModuleList my_module_list;
1078 my_module_list.Append(module_sp);
Enrico Granata146d9522012-11-08 02:22:02 +00001079 LoadScriptingResourceForModule(module_sp, this);
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001080 ModulesDidLoad (my_module_list);
Chris Lattner24943d22010-06-08 16:52:24 +00001081}
1082
1083void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001084Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata146d9522012-11-08 02:22:02 +00001085{
1086 // A module is being added to this target for the first time
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001087 ModuleList my_module_list;
1088 my_module_list.Append(module_sp);
1089 ModulesDidUnload (my_module_list);
Enrico Granata146d9522012-11-08 02:22:02 +00001090}
1091
1092void
Enrico Granata0b2f5cc2012-11-08 19:16:03 +00001093Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001094{
Jim Ingham3b8a6052011-08-03 01:00:06 +00001095 // A module is replacing an already added module
Jim Ingham03e5e512012-05-17 18:38:42 +00001096 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001097}
1098
1099void
1100Target::ModulesDidLoad (ModuleList &module_list)
1101{
Enrico Granata146d9522012-11-08 02:22:02 +00001102 if (module_list.GetSize())
1103 {
1104 m_breakpoint_list.UpdateBreakpoints (module_list, true);
1105 // TODO: make event data that packages up the module_list
1106 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1107 }
Chris Lattner24943d22010-06-08 16:52:24 +00001108}
1109
1110void
1111Target::ModulesDidUnload (ModuleList &module_list)
1112{
Enrico Granata146d9522012-11-08 02:22:02 +00001113 if (module_list.GetSize())
1114 {
1115 m_breakpoint_list.UpdateBreakpoints (module_list, false);
1116 // TODO: make event data that packages up the module_list
1117 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1118 }
Chris Lattner24943d22010-06-08 16:52:24 +00001119}
1120
Daniel Dunbar705a0982011-10-31 22:50:37 +00001121bool
Greg Clayton444fe992012-02-26 05:51:37 +00001122Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Ingham7089d8a2011-10-28 23:14:11 +00001123{
Greg Clayton73844aa2012-08-22 17:17:09 +00001124 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001125 {
1126 ModuleList matchingModules;
Greg Clayton444fe992012-02-26 05:51:37 +00001127 ModuleSpec module_spec (module_file_spec);
1128 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001129
1130 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1131 // black list.
1132 if (num_modules > 0)
1133 {
1134 for (int i = 0; i < num_modules; i++)
1135 {
1136 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1137 return false;
1138 }
1139 return true;
1140 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00001141 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001142 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001143}
1144
Daniel Dunbar705a0982011-10-31 22:50:37 +00001145bool
Jim Ingham7089d8a2011-10-28 23:14:11 +00001146Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1147{
Greg Clayton73844aa2012-08-22 17:17:09 +00001148 if (GetBreakpointsConsultPlatformAvoidList())
Jim Ingham7089d8a2011-10-28 23:14:11 +00001149 {
Greg Clayton73844aa2012-08-22 17:17:09 +00001150 if (m_platform_sp)
1151 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Ingham7089d8a2011-10-28 23:14:11 +00001152 }
Greg Clayton73844aa2012-08-22 17:17:09 +00001153 return false;
Jim Ingham7089d8a2011-10-28 23:14:11 +00001154}
1155
Chris Lattner24943d22010-06-08 16:52:24 +00001156size_t
Greg Clayton26100dc2011-01-07 01:57:07 +00001157Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1158{
Greg Clayton3508c382012-02-24 01:59:29 +00001159 SectionSP section_sp (addr.GetSection());
1160 if (section_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001161 {
Jason Molenda6b028d62012-04-25 00:06:56 +00001162 // If the contents of this section are encrypted, the on-disk file is unusuable. Read only from live memory.
1163 if (section_sp->IsEncrypted())
1164 {
Greg Clayton04e6ada2012-05-25 17:05:55 +00001165 error.SetErrorString("section is encrypted");
Jason Molenda6b028d62012-04-25 00:06:56 +00001166 return 0;
1167 }
Greg Clayton3508c382012-02-24 01:59:29 +00001168 ModuleSP module_sp (section_sp->GetModule());
1169 if (module_sp)
Greg Clayton26100dc2011-01-07 01:57:07 +00001170 {
Greg Clayton3508c382012-02-24 01:59:29 +00001171 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1172 if (objfile)
1173 {
1174 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1175 addr.GetOffset(),
1176 dst,
1177 dst_len);
1178 if (bytes_read > 0)
1179 return bytes_read;
1180 else
1181 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1182 }
Greg Clayton26100dc2011-01-07 01:57:07 +00001183 else
Greg Clayton3508c382012-02-24 01:59:29 +00001184 error.SetErrorString("address isn't from a object file");
Greg Clayton26100dc2011-01-07 01:57:07 +00001185 }
1186 else
Greg Clayton3508c382012-02-24 01:59:29 +00001187 error.SetErrorString("address isn't in a module");
Greg Clayton26100dc2011-01-07 01:57:07 +00001188 }
1189 else
Greg Clayton26100dc2011-01-07 01:57:07 +00001190 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Clayton3508c382012-02-24 01:59:29 +00001191
Greg Clayton26100dc2011-01-07 01:57:07 +00001192 return 0;
1193}
1194
1195size_t
Enrico Granata91544802011-09-06 19:20:51 +00001196Target::ReadMemory (const Address& addr,
1197 bool prefer_file_cache,
1198 void *dst,
1199 size_t dst_len,
1200 Error &error,
1201 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001202{
Chris Lattner24943d22010-06-08 16:52:24 +00001203 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001204
Enrico Granata91544802011-09-06 19:20:51 +00001205 // if we end up reading this from process memory, we will fill this
1206 // with the actual load address
1207 if (load_addr_ptr)
1208 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1209
Greg Clayton26100dc2011-01-07 01:57:07 +00001210 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001211
1212 addr_t load_addr = LLDB_INVALID_ADDRESS;
1213 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001214 Address resolved_addr;
1215 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001216 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001217 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001218 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001219 // No sections are loaded, so we must assume we are not running
1220 // yet and anything we are given is a file address.
1221 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1222 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001223 }
Greg Clayton70436352010-06-30 23:03:03 +00001224 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001225 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001226 // We have at least one section loaded. This can be becuase
1227 // we have manually loaded some sections with "target modules load ..."
1228 // or because we have have a live process that has sections loaded
1229 // through the dynamic loader
1230 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1231 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001232 }
Greg Clayton70436352010-06-30 23:03:03 +00001233 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001234 if (!resolved_addr.IsValid())
1235 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001236
Greg Clayton9b82f862011-07-11 05:12:02 +00001237
Greg Clayton26100dc2011-01-07 01:57:07 +00001238 if (prefer_file_cache)
1239 {
1240 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1241 if (bytes_read > 0)
1242 return bytes_read;
1243 }
Greg Clayton70436352010-06-30 23:03:03 +00001244
Johnny Chenda5a8022011-09-20 23:28:55 +00001245 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001246 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001247 if (load_addr == LLDB_INVALID_ADDRESS)
1248 load_addr = resolved_addr.GetLoadAddress (this);
1249
Greg Clayton70436352010-06-30 23:03:03 +00001250 if (load_addr == LLDB_INVALID_ADDRESS)
1251 {
Greg Clayton3508c382012-02-24 01:59:29 +00001252 ModuleSP addr_module_sp (resolved_addr.GetModule());
1253 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001254 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Clayton3508c382012-02-24 01:59:29 +00001255 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001256 resolved_addr.GetFileAddress(),
Greg Clayton3508c382012-02-24 01:59:29 +00001257 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001258 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001259 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001260 }
1261 else
1262 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001263 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001264 if (bytes_read != dst_len)
1265 {
1266 if (error.Success())
1267 {
1268 if (bytes_read == 0)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001269 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001270 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001271 error.SetErrorStringWithFormat("only %" PRIu64 " of %" PRIu64 " bytes were read from memory at 0x%" PRIx64, (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001272 }
1273 }
Greg Clayton70436352010-06-30 23:03:03 +00001274 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001275 {
1276 if (load_addr_ptr)
1277 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001278 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001279 }
Greg Clayton70436352010-06-30 23:03:03 +00001280 // If the address is not section offset we have an address that
1281 // doesn't resolve to any address in any currently loaded shared
1282 // libaries and we failed to read memory so there isn't anything
1283 // more we can do. If it is section offset, we might be able to
1284 // read cached memory from the object file.
1285 if (!resolved_addr.IsSectionOffset())
1286 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001287 }
Chris Lattner24943d22010-06-08 16:52:24 +00001288 }
Greg Clayton70436352010-06-30 23:03:03 +00001289
Greg Clayton9b82f862011-07-11 05:12:02 +00001290 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001291 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001292 // If we didn't already try and read from the object file cache, then
1293 // try it after failing to read from the process.
1294 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001295 }
1296 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001297}
1298
Greg Clayton7dd98df2011-07-12 17:06:17 +00001299size_t
1300Target::ReadScalarIntegerFromMemory (const Address& addr,
1301 bool prefer_file_cache,
1302 uint32_t byte_size,
1303 bool is_signed,
1304 Scalar &scalar,
1305 Error &error)
1306{
1307 uint64_t uval;
1308
1309 if (byte_size <= sizeof(uval))
1310 {
1311 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1312 if (bytes_read == byte_size)
1313 {
1314 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1315 uint32_t offset = 0;
1316 if (byte_size <= 4)
1317 scalar = data.GetMaxU32 (&offset, byte_size);
1318 else
1319 scalar = data.GetMaxU64 (&offset, byte_size);
1320
1321 if (is_signed)
1322 scalar.SignExtend(byte_size * 8);
1323 return bytes_read;
1324 }
1325 }
1326 else
1327 {
1328 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1329 }
1330 return 0;
1331}
1332
1333uint64_t
1334Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1335 bool prefer_file_cache,
1336 size_t integer_byte_size,
1337 uint64_t fail_value,
1338 Error &error)
1339{
1340 Scalar scalar;
1341 if (ReadScalarIntegerFromMemory (addr,
1342 prefer_file_cache,
1343 integer_byte_size,
1344 false,
1345 scalar,
1346 error))
1347 return scalar.ULongLong(fail_value);
1348 return fail_value;
1349}
1350
1351bool
1352Target::ReadPointerFromMemory (const Address& addr,
1353 bool prefer_file_cache,
1354 Error &error,
1355 Address &pointer_addr)
1356{
1357 Scalar scalar;
1358 if (ReadScalarIntegerFromMemory (addr,
1359 prefer_file_cache,
1360 m_arch.GetAddressByteSize(),
1361 false,
1362 scalar,
1363 error))
1364 {
1365 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1366 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1367 {
1368 if (m_section_load_list.IsEmpty())
1369 {
1370 // No sections are loaded, so we must assume we are not running
1371 // yet and anything we are given is a file address.
1372 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1373 }
1374 else
1375 {
1376 // We have at least one section loaded. This can be becuase
1377 // we have manually loaded some sections with "target modules load ..."
1378 // or because we have have a live process that has sections loaded
1379 // through the dynamic loader
1380 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1381 }
1382 // We weren't able to resolve the pointer value, so just return
1383 // an address with no section
1384 if (!pointer_addr.IsValid())
1385 pointer_addr.SetOffset (pointer_vm_addr);
1386 return true;
1387
1388 }
1389 }
1390 return false;
1391}
Chris Lattner24943d22010-06-08 16:52:24 +00001392
1393ModuleSP
Greg Clayton444fe992012-02-26 05:51:37 +00001394Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001395{
Chris Lattner24943d22010-06-08 16:52:24 +00001396 ModuleSP module_sp;
1397
Chris Lattner24943d22010-06-08 16:52:24 +00001398 Error error;
1399
Jim Ingham03e5e512012-05-17 18:38:42 +00001400 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1401 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytone1ef1e32012-04-27 00:58:27 +00001402
Jim Ingham03e5e512012-05-17 18:38:42 +00001403 if (module_spec.GetUUID().IsValid())
1404 module_sp = m_images.FindFirstModule(module_spec);
1405
Greg Claytone1ef1e32012-04-27 00:58:27 +00001406 if (!module_sp)
1407 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001408 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1409 bool did_create_module = false;
1410
1411 // If there are image search path entries, try to use them first to acquire a suitable image.
1412 if (m_image_search_paths.GetSize())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001413 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001414 ModuleSpec transformed_spec (module_spec);
1415 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1416 {
1417 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1418 error = ModuleList::GetSharedModule (transformed_spec,
1419 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001420 &GetExecutableSearchPaths(),
Jim Ingham03e5e512012-05-17 18:38:42 +00001421 &old_module_sp,
1422 &did_create_module);
1423 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001424 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001425
Greg Claytone1ef1e32012-04-27 00:58:27 +00001426 if (!module_sp)
1427 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001428 // If we have a UUID, we can check our global shared module list in case
1429 // we already have it. If we don't have a valid UUID, then we can't since
1430 // the path in "module_spec" will be a platform path, and we will need to
1431 // let the platform find that file. For example, we could be asking for
1432 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1433 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1434 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1435 // cache.
1436 if (module_spec.GetUUID().IsValid())
Greg Claytone1ef1e32012-04-27 00:58:27 +00001437 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001438 // We have a UUID, it is OK to check the global module list...
1439 error = ModuleList::GetSharedModule (module_spec,
1440 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001441 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001442 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001443 &did_create_module);
Greg Claytone1ef1e32012-04-27 00:58:27 +00001444 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001445
1446 if (!module_sp)
Greg Claytone1ef1e32012-04-27 00:58:27 +00001447 {
Jim Ingham03e5e512012-05-17 18:38:42 +00001448 // The platform is responsible for finding and caching an appropriate
1449 // module in the shared module cache.
1450 if (m_platform_sp)
1451 {
1452 FileSpec platform_file_spec;
1453 error = m_platform_sp->GetSharedModule (module_spec,
1454 module_sp,
Greg Claytonc6e82e42012-08-22 18:39:03 +00001455 &GetExecutableSearchPaths(),
Greg Clayton73844aa2012-08-22 17:17:09 +00001456 &old_module_sp,
Jim Ingham03e5e512012-05-17 18:38:42 +00001457 &did_create_module);
1458 }
1459 else
1460 {
1461 error.SetErrorString("no platform is currently set");
1462 }
Greg Claytone1ef1e32012-04-27 00:58:27 +00001463 }
1464 }
Chris Lattner24943d22010-06-08 16:52:24 +00001465
Jim Ingham03e5e512012-05-17 18:38:42 +00001466 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1467 // module in the list already, and if there was, let's remove it.
1468 if (module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001469 {
Greg Clayton1649a722012-11-29 22:16:27 +00001470 ObjectFile *objfile = module_sp->GetObjectFile();
1471 if (objfile)
Jim Ingham03e5e512012-05-17 18:38:42 +00001472 {
Greg Clayton1649a722012-11-29 22:16:27 +00001473 switch (objfile->GetType())
Jim Ingham03e5e512012-05-17 18:38:42 +00001474 {
Greg Clayton1649a722012-11-29 22:16:27 +00001475 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1476 case ObjectFile::eTypeExecutable: /// A normal executable
1477 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1478 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1479 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1480 break;
1481 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1482 if (error_ptr)
1483 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1484 return ModuleSP();
1485 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1486 if (error_ptr)
1487 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1488 return ModuleSP();
1489 default:
1490 if (error_ptr)
1491 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1492 return ModuleSP();
1493 }
1494 // GetSharedModule is not guaranteed to find the old shared module, for instance
1495 // in the common case where you pass in the UUID, it is only going to find the one
1496 // module matching the UUID. In fact, it has no good way to know what the "old module"
1497 // relevant to this target is, since there might be many copies of a module with this file spec
1498 // in various running debug sessions, but only one of them will belong to this target.
1499 // So let's remove the UUID from the module list, and look in the target's module list.
1500 // Only do this if there is SOMETHING else in the module spec...
1501 if (!old_module_sp)
1502 {
1503 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham03e5e512012-05-17 18:38:42 +00001504 {
Greg Clayton1649a722012-11-29 22:16:27 +00001505 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1506 module_spec_copy.GetUUID().Clear();
1507
1508 ModuleList found_modules;
1509 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1510 if (num_found == 1)
1511 {
1512 old_module_sp = found_modules.GetModuleAtIndex(0);
1513 }
Jim Ingham03e5e512012-05-17 18:38:42 +00001514 }
1515 }
Greg Clayton1649a722012-11-29 22:16:27 +00001516
1517 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1518 {
1519 m_images.ReplaceModule(old_module_sp, module_sp);
1520 Module *old_module_ptr = old_module_sp.get();
1521 old_module_sp.reset();
1522 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1523 }
1524 else
1525 m_images.Append(module_sp);
Jim Ingham03e5e512012-05-17 18:38:42 +00001526 }
Chris Lattner24943d22010-06-08 16:52:24 +00001527 }
1528 }
1529 if (error_ptr)
1530 *error_ptr = error;
1531 return module_sp;
1532}
1533
1534
Greg Clayton289afcb2012-02-18 05:35:26 +00001535TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001536Target::CalculateTarget ()
1537{
Greg Clayton289afcb2012-02-18 05:35:26 +00001538 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001539}
1540
Greg Clayton289afcb2012-02-18 05:35:26 +00001541ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001542Target::CalculateProcess ()
1543{
Greg Clayton289afcb2012-02-18 05:35:26 +00001544 return ProcessSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001545}
1546
Greg Clayton289afcb2012-02-18 05:35:26 +00001547ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001548Target::CalculateThread ()
1549{
Greg Clayton289afcb2012-02-18 05:35:26 +00001550 return ThreadSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001551}
1552
Greg Clayton289afcb2012-02-18 05:35:26 +00001553StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001554Target::CalculateStackFrame ()
1555{
Greg Clayton289afcb2012-02-18 05:35:26 +00001556 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001557}
1558
1559void
Greg Claytona830adb2010-10-04 01:05:56 +00001560Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001561{
Greg Clayton567e7f32011-09-22 04:58:26 +00001562 exe_ctx.Clear();
1563 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001564}
1565
1566PathMappingList &
1567Target::GetImageSearchPathList ()
1568{
1569 return m_image_search_paths;
1570}
1571
1572void
1573Target::ImageSearchPathsChanged
1574(
1575 const PathMappingList &path_list,
1576 void *baton
1577)
1578{
1579 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001580 ModuleSP exe_module_sp (target->GetExecutableModule());
1581 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001582 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001583 target->m_images.Clear();
1584 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001585 }
1586}
1587
1588ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001589Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001590{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001591 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001592 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001593 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001594 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001595 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001596 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1597 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1598 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1599 }
Chris Lattner24943d22010-06-08 16:52:24 +00001600 return m_scratch_ast_context_ap.get();
1601}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001602
Sean Callanan4938bd62011-11-16 18:20:47 +00001603ClangASTImporter *
1604Target::GetClangASTImporter()
1605{
1606 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1607
1608 if (!ast_importer)
1609 {
1610 ast_importer = new ClangASTImporter();
1611 m_ast_importer_ap.reset(ast_importer);
1612 }
1613
1614 return ast_importer;
1615}
1616
Greg Clayton990de7b2010-11-18 23:32:35 +00001617void
Caroline Tice2a456812011-03-10 22:14:10 +00001618Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001619{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001620 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001621}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001622
Greg Clayton990de7b2010-11-18 23:32:35 +00001623void
Caroline Tice2a456812011-03-10 22:14:10 +00001624Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001625{
Greg Claytonc6e82e42012-08-22 18:39:03 +00001626 Process::SettingsTerminate ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001627}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001628
Greg Clayton9ce95382012-02-13 23:10:39 +00001629FileSpecList
1630Target::GetDefaultExecutableSearchPaths ()
1631{
Greg Clayton73844aa2012-08-22 17:17:09 +00001632 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1633 if (properties_sp)
1634 return properties_sp->GetExecutableSearchPaths();
Greg Clayton9ce95382012-02-13 23:10:39 +00001635 return FileSpecList();
1636}
1637
Caroline Tice5bc8c972010-09-20 20:44:43 +00001638ArchSpec
1639Target::GetDefaultArchitecture ()
1640{
Greg Clayton73844aa2012-08-22 17:17:09 +00001641 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1642 if (properties_sp)
1643 return properties_sp->GetDefaultArchitecture();
Greg Clayton469e08d2012-05-15 02:44:13 +00001644 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001645}
1646
1647void
Greg Clayton73844aa2012-08-22 17:17:09 +00001648Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001649{
Greg Clayton73844aa2012-08-22 17:17:09 +00001650 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1651 if (properties_sp)
1652 return properties_sp->SetDefaultArchitecture(arch);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001653}
1654
Greg Claytona830adb2010-10-04 01:05:56 +00001655Target *
1656Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1657{
1658 // The target can either exist in the "process" of ExecutionContext, or in
1659 // the "target_sp" member of SymbolContext. This accessor helper function
1660 // will get the target from one of these locations.
1661
1662 Target *target = NULL;
1663 if (sc_ptr != NULL)
1664 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001665 if (target == NULL && exe_ctx_ptr)
1666 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001667 return target;
1668}
1669
Greg Clayton427f2902010-12-14 02:59:59 +00001670ExecutionResults
1671Target::EvaluateExpression
1672(
1673 const char *expr_cstr,
1674 StackFrame *frame,
Enrico Granata6cca9692012-07-16 23:10:35 +00001675 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +00001676 const EvaluateExpressionOptions& options
Greg Clayton427f2902010-12-14 02:59:59 +00001677)
1678{
Enrico Granata3a08fd12012-09-18 17:43:16 +00001679 result_valobj_sp.reset();
1680
Greg Clayton427f2902010-12-14 02:59:59 +00001681 ExecutionResults execution_results = eExecutionSetupError;
1682
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001683 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1684 return execution_results;
1685
Jim Ingham3613ae12011-05-12 02:06:14 +00001686 // We shouldn't run stop hooks in expressions.
1687 // Be sure to reset this if you return anywhere within this function.
1688 bool old_suppress_value = m_suppress_stop_hooks;
1689 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001690
1691 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001692
1693 const size_t expr_cstr_len = ::strlen (expr_cstr);
1694
Greg Clayton427f2902010-12-14 02:59:59 +00001695 if (frame)
1696 {
1697 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001698 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001699 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001700 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1701 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001702 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001703
1704 // Make sure we don't have any things that we know a variable expression
1705 // won't be able to deal with before calling into it
1706 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1707 {
1708 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
Enrico Granatad27026e2012-09-05 20:41:26 +00001709 options.GetUseDynamic(),
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001710 expr_path_options,
1711 var_sp,
1712 error);
Enrico Granata4d609c92012-04-24 22:15:37 +00001713 // if this expression results in a bitfield, we give up and let the IR handle it
1714 if (result_valobj_sp && result_valobj_sp->IsBitfield())
1715 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001716 }
Greg Clayton427f2902010-12-14 02:59:59 +00001717 }
1718 else if (m_process_sp)
1719 {
1720 m_process_sp->CalculateExecutionContext(exe_ctx);
1721 }
1722 else
1723 {
1724 CalculateExecutionContext(exe_ctx);
1725 }
1726
1727 if (result_valobj_sp)
1728 {
1729 execution_results = eExecutionCompleted;
1730 // We got a result from the frame variable expression path above...
1731 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1732
1733 lldb::ValueObjectSP const_valobj_sp;
1734
1735 // Check in case our value is already a constant value
1736 if (result_valobj_sp->GetIsConstant())
1737 {
1738 const_valobj_sp = result_valobj_sp;
1739 const_valobj_sp->SetName (persistent_variable_name);
1740 }
1741 else
Jim Inghame41494a2011-04-16 00:01:13 +00001742 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001743 if (options.GetUseDynamic() != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001744 {
Enrico Granatad27026e2012-09-05 20:41:26 +00001745 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(options.GetUseDynamic());
Jim Inghame41494a2011-04-16 00:01:13 +00001746 if (dynamic_sp)
1747 result_valobj_sp = dynamic_sp;
1748 }
1749
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001750 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001751 }
Greg Clayton427f2902010-12-14 02:59:59 +00001752
Sean Callanan6a925532011-01-13 08:53:35 +00001753 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1754
Greg Clayton427f2902010-12-14 02:59:59 +00001755 result_valobj_sp = const_valobj_sp;
1756
Sean Callanan6a925532011-01-13 08:53:35 +00001757 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1758 assert (clang_expr_variable_sp.get());
1759
1760 // Set flags and live data as appropriate
1761
1762 const Value &result_value = live_valobj_sp->GetValue();
1763
1764 switch (result_value.GetValueType())
1765 {
1766 case Value::eValueTypeHostAddress:
1767 case Value::eValueTypeFileAddress:
1768 // we don't do anything with these for now
1769 break;
1770 case Value::eValueTypeScalar:
Greg Claytonf0fab4f2012-10-30 23:56:14 +00001771 case Value::eValueTypeVector:
Sean Callanan6a925532011-01-13 08:53:35 +00001772 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1773 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1774 break;
1775 case Value::eValueTypeLoadAddress:
1776 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1777 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1778 break;
1779 }
Greg Clayton427f2902010-12-14 02:59:59 +00001780 }
1781 else
1782 {
1783 // Make sure we aren't just trying to see the value of a persistent
1784 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001785 lldb::ClangExpressionVariableSP persistent_var_sp;
1786 // Only check for persistent variables the expression starts with a '$'
1787 if (expr_cstr[0] == '$')
1788 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1789
Greg Clayton427f2902010-12-14 02:59:59 +00001790 if (persistent_var_sp)
1791 {
1792 result_valobj_sp = persistent_var_sp->GetValueObject ();
1793 execution_results = eExecutionCompleted;
1794 }
1795 else
1796 {
1797 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001798
Greg Clayton427f2902010-12-14 02:59:59 +00001799 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Enrico Granatad27026e2012-09-05 20:41:26 +00001800 options.GetExecutionPolicy(),
Sean Callanan5b658cc2011-11-07 23:35:40 +00001801 lldb::eLanguageTypeUnknown,
Enrico Granatad27026e2012-09-05 20:41:26 +00001802 options.DoesCoerceToId() ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
1803 options.DoesUnwindOnError(),
Greg Clayton427f2902010-12-14 02:59:59 +00001804 expr_cstr,
1805 prefix,
Enrico Granata6cca9692012-07-16 23:10:35 +00001806 result_valobj_sp,
Jim Ingham47beabb2012-10-16 21:41:58 +00001807 options.GetRunOthers(),
1808 options.GetTimeoutUsec());
Greg Clayton427f2902010-12-14 02:59:59 +00001809 }
1810 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001811
1812 m_suppress_stop_hooks = old_suppress_value;
1813
Greg Clayton427f2902010-12-14 02:59:59 +00001814 return execution_results;
1815}
1816
Greg Claytonc0fa5332011-05-22 22:46:53 +00001817lldb::addr_t
1818Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1819{
1820 addr_t code_addr = load_addr;
1821 switch (m_arch.GetMachine())
1822 {
1823 case llvm::Triple::arm:
1824 case llvm::Triple::thumb:
1825 switch (addr_class)
1826 {
1827 case eAddressClassData:
1828 case eAddressClassDebug:
1829 return LLDB_INVALID_ADDRESS;
1830
1831 case eAddressClassUnknown:
1832 case eAddressClassInvalid:
1833 case eAddressClassCode:
1834 case eAddressClassCodeAlternateISA:
1835 case eAddressClassRuntime:
1836 // Check if bit zero it no set?
1837 if ((code_addr & 1ull) == 0)
1838 {
1839 // Bit zero isn't set, check if the address is a multiple of 2?
1840 if (code_addr & 2ull)
1841 {
1842 // The address is a multiple of 2 so it must be thumb, set bit zero
1843 code_addr |= 1ull;
1844 }
1845 else if (addr_class == eAddressClassCodeAlternateISA)
1846 {
1847 // We checked the address and the address claims to be the alternate ISA
1848 // which means thumb, so set bit zero.
1849 code_addr |= 1ull;
1850 }
1851 }
1852 break;
1853 }
1854 break;
1855
1856 default:
1857 break;
1858 }
1859 return code_addr;
1860}
1861
1862lldb::addr_t
1863Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1864{
1865 addr_t opcode_addr = load_addr;
1866 switch (m_arch.GetMachine())
1867 {
1868 case llvm::Triple::arm:
1869 case llvm::Triple::thumb:
1870 switch (addr_class)
1871 {
1872 case eAddressClassData:
1873 case eAddressClassDebug:
1874 return LLDB_INVALID_ADDRESS;
1875
1876 case eAddressClassInvalid:
1877 case eAddressClassUnknown:
1878 case eAddressClassCode:
1879 case eAddressClassCodeAlternateISA:
1880 case eAddressClassRuntime:
1881 opcode_addr &= ~(1ull);
1882 break;
1883 }
1884 break;
1885
1886 default:
1887 break;
1888 }
1889 return opcode_addr;
1890}
1891
Jim Inghamd60d94a2011-03-11 03:53:59 +00001892lldb::user_id_t
1893Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1894{
1895 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001896 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001897 m_stop_hooks[new_uid] = new_hook_sp;
1898 return new_uid;
1899}
1900
1901bool
1902Target::RemoveStopHookByID (lldb::user_id_t user_id)
1903{
1904 size_t num_removed;
1905 num_removed = m_stop_hooks.erase (user_id);
1906 if (num_removed == 0)
1907 return false;
1908 else
1909 return true;
1910}
1911
1912void
1913Target::RemoveAllStopHooks ()
1914{
1915 m_stop_hooks.clear();
1916}
1917
1918Target::StopHookSP
1919Target::GetStopHookByID (lldb::user_id_t user_id)
1920{
1921 StopHookSP found_hook;
1922
1923 StopHookCollection::iterator specified_hook_iter;
1924 specified_hook_iter = m_stop_hooks.find (user_id);
1925 if (specified_hook_iter != m_stop_hooks.end())
1926 found_hook = (*specified_hook_iter).second;
1927 return found_hook;
1928}
1929
1930bool
1931Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1932{
1933 StopHookCollection::iterator specified_hook_iter;
1934 specified_hook_iter = m_stop_hooks.find (user_id);
1935 if (specified_hook_iter == m_stop_hooks.end())
1936 return false;
1937
1938 (*specified_hook_iter).second->SetIsActive (active_state);
1939 return true;
1940}
1941
1942void
1943Target::SetAllStopHooksActiveState (bool active_state)
1944{
1945 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1946 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1947 {
1948 (*pos).second->SetIsActive (active_state);
1949 }
1950}
1951
1952void
1953Target::RunStopHooks ()
1954{
Jim Ingham3613ae12011-05-12 02:06:14 +00001955 if (m_suppress_stop_hooks)
1956 return;
1957
Jim Inghamd60d94a2011-03-11 03:53:59 +00001958 if (!m_process_sp)
1959 return;
Enrico Granata5a60f5e2012-08-03 22:24:48 +00001960
1961 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
1962 // since in that case we do not want to run the stop-hooks
1963 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
1964 return;
1965
Jim Inghamd60d94a2011-03-11 03:53:59 +00001966 if (m_stop_hooks.empty())
1967 return;
1968
1969 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1970
1971 // If there aren't any active stop hooks, don't bother either:
1972 bool any_active_hooks = false;
1973 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1974 {
1975 if ((*pos).second->IsActive())
1976 {
1977 any_active_hooks = true;
1978 break;
1979 }
1980 }
1981 if (!any_active_hooks)
1982 return;
1983
1984 CommandReturnObject result;
1985
1986 std::vector<ExecutionContext> exc_ctx_with_reasons;
1987 std::vector<SymbolContext> sym_ctx_with_reasons;
1988
1989 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1990 size_t num_threads = cur_threadlist.GetSize();
1991 for (size_t i = 0; i < num_threads; i++)
1992 {
1993 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1994 if (cur_thread_sp->ThreadStoppedForAReason())
1995 {
1996 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1997 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1998 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1999 }
2000 }
2001
2002 // If no threads stopped for a reason, don't run the stop-hooks.
2003 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2004 if (num_exe_ctx == 0)
2005 return;
2006
Jim Inghame5ed8e92011-06-02 23:58:26 +00002007 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2008 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002009
2010 bool keep_going = true;
2011 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00002012 bool print_hook_header;
2013 bool print_thread_header;
2014
2015 if (num_exe_ctx == 1)
2016 print_thread_header = false;
2017 else
2018 print_thread_header = true;
2019
2020 if (m_stop_hooks.size() == 1)
2021 print_hook_header = false;
2022 else
2023 print_hook_header = true;
2024
Jim Inghamd60d94a2011-03-11 03:53:59 +00002025 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2026 {
2027 // result.Clear();
2028 StopHookSP cur_hook_sp = (*pos).second;
2029 if (!cur_hook_sp->IsActive())
2030 continue;
2031
2032 bool any_thread_matched = false;
2033 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2034 {
2035 if ((cur_hook_sp->GetSpecifier () == NULL
2036 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2037 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Inghama2664912012-03-07 22:03:04 +00002038 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002039 {
2040 if (!hooks_ran)
2041 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00002042 hooks_ran = true;
2043 }
Jim Inghamc54840c2011-03-22 01:47:27 +00002044 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002045 {
Johnny Chen4d96a742011-10-24 23:01:06 +00002046 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2047 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2048 NULL);
2049 if (cmd)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002050 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chen4d96a742011-10-24 23:01:06 +00002051 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002052 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002053 any_thread_matched = true;
2054 }
2055
Jim Inghamc54840c2011-03-22 01:47:27 +00002056 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00002057 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002058
2059 bool stop_on_continue = true;
2060 bool stop_on_error = true;
2061 bool echo_commands = false;
2062 bool print_results = true;
2063 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00002064 &exc_ctx_with_reasons[i],
2065 stop_on_continue,
2066 stop_on_error,
2067 echo_commands,
Enrico Granata01bc2d42012-05-31 01:09:06 +00002068 print_results,
2069 eLazyBoolNo,
Greg Clayton24bc5d92011-03-30 18:16:51 +00002070 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00002071
2072 // If the command started the target going again, we should bag out of
2073 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00002074 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2075 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00002076 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002077 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002078 keep_going = false;
2079 }
2080 }
2081 }
2082 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00002083
Caroline Tice4a348082011-05-02 20:41:46 +00002084 result.GetImmediateOutputStream()->Flush();
2085 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00002086}
2087
Greg Claytonbbea1332011-07-08 00:48:09 +00002088
Jim Inghamd60d94a2011-03-11 03:53:59 +00002089//--------------------------------------------------------------
2090// class Target::StopHook
2091//--------------------------------------------------------------
2092
2093
2094Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2095 UserID (uid),
2096 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00002097 m_commands (),
2098 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002099 m_thread_spec_ap(NULL),
2100 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002101{
2102}
2103
2104Target::StopHook::StopHook (const StopHook &rhs) :
2105 UserID (rhs.GetID()),
2106 m_target_sp (rhs.m_target_sp),
2107 m_commands (rhs.m_commands),
2108 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00002109 m_thread_spec_ap (NULL),
2110 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00002111{
2112 if (rhs.m_thread_spec_ap.get() != NULL)
2113 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2114}
2115
2116
2117Target::StopHook::~StopHook ()
2118{
2119}
2120
2121void
2122Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2123{
2124 m_thread_spec_ap.reset (specifier);
2125}
2126
2127
2128void
2129Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2130{
2131 int indent_level = s->GetIndentLevel();
2132
2133 s->SetIndentLevel(indent_level + 2);
2134
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002135 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00002136 if (m_active)
2137 s->Indent ("State: enabled\n");
2138 else
2139 s->Indent ("State: disabled\n");
2140
2141 if (m_specifier_sp)
2142 {
2143 s->Indent();
2144 s->PutCString ("Specifier:\n");
2145 s->SetIndentLevel (indent_level + 4);
2146 m_specifier_sp->GetDescription (s, level);
2147 s->SetIndentLevel (indent_level + 2);
2148 }
2149
2150 if (m_thread_spec_ap.get() != NULL)
2151 {
2152 StreamString tmp;
2153 s->Indent("Thread:\n");
2154 m_thread_spec_ap->GetDescription (&tmp, level);
2155 s->SetIndentLevel (indent_level + 4);
2156 s->Indent (tmp.GetData());
2157 s->PutCString ("\n");
2158 s->SetIndentLevel (indent_level + 2);
2159 }
2160
2161 s->Indent ("Commands: \n");
2162 s->SetIndentLevel (indent_level + 4);
2163 uint32_t num_commands = m_commands.GetSize();
2164 for (uint32_t i = 0; i < num_commands; i++)
2165 {
2166 s->Indent(m_commands.GetStringAtIndex(i));
2167 s->PutCString ("\n");
2168 }
2169 s->SetIndentLevel (indent_level);
2170}
2171
Greg Clayton73844aa2012-08-22 17:17:09 +00002172//--------------------------------------------------------------
2173// class TargetProperties
2174//--------------------------------------------------------------
2175
2176OptionEnumValueElement
2177lldb_private::g_dynamic_value_types[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002178{
Greg Clayton73844aa2012-08-22 17:17:09 +00002179 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2180 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2181 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2182 { 0, NULL, NULL }
2183};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002184
Greg Clayton49ce8962012-08-29 21:13:06 +00002185static OptionEnumValueElement
2186g_inline_breakpoint_enums[] =
2187{
2188 { 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."},
2189 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2190 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2191 { 0, NULL, NULL }
2192};
2193
Greg Clayton73844aa2012-08-22 17:17:09 +00002194static PropertyDefinition
2195g_properties[] =
Caroline Tice5bc8c972010-09-20 20:44:43 +00002196{
Greg Clayton73844aa2012-08-22 17:17:09 +00002197 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2198 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2199 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2200 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2201 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2202 { "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 "
2203 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2204 "some part (starting at the root) of the path to the file when it was built, "
2205 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2206 "Each element of the array is checked in order and the first one that results in a match wins." },
2207 { "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." },
2208 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2209 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
2210 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." },
Greg Clayton0c8446c2012-10-17 22:57:12 +00002211 { "arg0" , OptionValue::eTypeString , false, 0 , NULL, NULL, "The first argument passed to the program in the argument array which can be different from the executable itself." },
2212 { "run-args" , OptionValue::eTypeArgs , false, 0 , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run. Note that this does NOT include the argv[0] which is in target.arg0." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002213 { "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." },
2214 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2215 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2216 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2217 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
2218 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2219 { "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 +00002220 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsHeaders , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
2221 "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. "
2222 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2223 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
2224 "Always checking for inlined breakpoint locations can be expensive (memory and time), so we try to minimize the "
2225 "times we look for inlined locations. This setting allows you to control exactly which strategy is used when settings "
2226 "file and line breakpoints." },
Greg Clayton73844aa2012-08-22 17:17:09 +00002227 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2228};
2229enum
Caroline Tice5bc8c972010-09-20 20:44:43 +00002230{
Greg Clayton73844aa2012-08-22 17:17:09 +00002231 ePropertyDefaultArch,
2232 ePropertyExprPrefix,
2233 ePropertyPreferDynamic,
2234 ePropertyEnableSynthetic,
2235 ePropertySkipPrologue,
2236 ePropertySourceMap,
2237 ePropertyExecutableSearchPaths,
2238 ePropertyMaxChildrenCount,
2239 ePropertyMaxSummaryLength,
2240 ePropertyBreakpointUseAvoidList,
Greg Clayton0c8446c2012-10-17 22:57:12 +00002241 ePropertyArg0,
Greg Clayton73844aa2012-08-22 17:17:09 +00002242 ePropertyRunArgs,
2243 ePropertyEnvVars,
2244 ePropertyInheritEnv,
2245 ePropertyInputPath,
2246 ePropertyOutputPath,
2247 ePropertyErrorPath,
2248 ePropertyDisableASLR,
Greg Clayton49ce8962012-08-29 21:13:06 +00002249 ePropertyDisableSTDIO,
Greg Clayton87e9d322012-10-19 18:02:49 +00002250 ePropertyInlineStrategy
Greg Clayton73844aa2012-08-22 17:17:09 +00002251};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002252
Caroline Tice5bc8c972010-09-20 20:44:43 +00002253
Greg Clayton73844aa2012-08-22 17:17:09 +00002254class TargetOptionValueProperties : public OptionValueProperties
Greg Claytond284b662011-02-18 01:44:25 +00002255{
Greg Clayton73844aa2012-08-22 17:17:09 +00002256public:
2257 TargetOptionValueProperties (const ConstString &name) :
2258 OptionValueProperties (name),
2259 m_target (NULL),
2260 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002261 {
Caroline Tice5bc8c972010-09-20 20:44:43 +00002262 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002263
Greg Clayton73844aa2012-08-22 17:17:09 +00002264 // This constructor is used when creating TargetOptionValueProperties when it
2265 // is part of a new lldb_private::Target instance. It will copy all current
2266 // global property values as needed
2267 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2268 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2269 m_target (target),
2270 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002271 {
Greg Clayton73844aa2012-08-22 17:17:09 +00002272 }
2273
2274 virtual const Property *
2275 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2276 {
2277 // When gettings the value for a key from the target options, we will always
2278 // try and grab the setting from the current target if there is one. Else we just
2279 // use the one from this instance.
2280 if (idx == ePropertyEnvVars)
2281 GetHostEnvironmentIfNeeded ();
2282
2283 if (exe_ctx)
2284 {
2285 Target *target = exe_ctx->GetTargetPtr();
2286 if (target)
2287 {
2288 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2289 if (this != target_properties)
2290 return target_properties->ProtectedGetPropertyAtIndex (idx);
2291 }
2292 }
2293 return ProtectedGetPropertyAtIndex (idx);
2294 }
2295protected:
2296
2297 void
2298 GetHostEnvironmentIfNeeded () const
2299 {
2300 if (!m_got_host_env)
2301 {
2302 if (m_target)
2303 {
2304 m_got_host_env = true;
2305 const uint32_t idx = ePropertyInheritEnv;
2306 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2307 {
2308 PlatformSP platform_sp (m_target->GetPlatform());
2309 if (platform_sp)
2310 {
2311 StringList env;
2312 if (platform_sp->GetEnvironment(env))
2313 {
2314 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2315 if (env_dict)
2316 {
2317 const bool can_replace = false;
2318 const size_t envc = env.GetSize();
2319 for (size_t idx=0; idx<envc; idx++)
2320 {
2321 const char *env_entry = env.GetStringAtIndex (idx);
2322 if (env_entry)
2323 {
2324 const char *equal_pos = ::strchr(env_entry, '=');
2325 ConstString key;
2326 // It is ok to have environment variables with no values
2327 const char *value = NULL;
2328 if (equal_pos)
2329 {
2330 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2331 if (equal_pos[1])
2332 value = equal_pos + 1;
2333 }
2334 else
2335 {
2336 key.SetCString(env_entry);
2337 }
2338 // Don't allow existing keys to be replaced with ones we get from the platform environment
2339 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2340 }
2341 }
2342 }
2343 }
2344 }
2345 }
2346 }
2347 }
2348 }
2349 Target *m_target;
2350 mutable bool m_got_host_env;
2351};
2352
2353TargetProperties::TargetProperties (Target *target) :
2354 Properties ()
2355{
2356 if (target)
2357 {
2358 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002359 }
2360 else
Greg Clayton73844aa2012-08-22 17:17:09 +00002361 {
2362 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2363 m_collection_sp->Initialize(g_properties);
2364 m_collection_sp->AppendProperty(ConstString("process"),
2365 ConstString("Settings specify to processes."),
2366 true,
2367 Process::GetGlobalProperties()->GetValueProperties());
2368 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002369}
2370
Greg Clayton73844aa2012-08-22 17:17:09 +00002371TargetProperties::~TargetProperties ()
2372{
2373}
2374ArchSpec
2375TargetProperties::GetDefaultArchitecture () const
2376{
2377 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2378 if (value)
2379 return value->GetCurrentValue();
2380 return ArchSpec();
2381}
2382
2383void
2384TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2385{
2386 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2387 if (value)
2388 return value->SetCurrentValue(arch, true);
2389}
2390
2391lldb::DynamicValueType
2392TargetProperties::GetPreferDynamicValue() const
2393{
2394 const uint32_t idx = ePropertyPreferDynamic;
2395 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2396}
2397
2398bool
2399TargetProperties::GetDisableASLR () const
2400{
2401 const uint32_t idx = ePropertyDisableASLR;
2402 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2403}
2404
2405void
2406TargetProperties::SetDisableASLR (bool b)
2407{
2408 const uint32_t idx = ePropertyDisableASLR;
2409 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2410}
2411
2412bool
2413TargetProperties::GetDisableSTDIO () const
2414{
2415 const uint32_t idx = ePropertyDisableSTDIO;
2416 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2417}
2418
2419void
2420TargetProperties::SetDisableSTDIO (bool b)
2421{
2422 const uint32_t idx = ePropertyDisableSTDIO;
2423 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2424}
2425
Greg Clayton49ce8962012-08-29 21:13:06 +00002426InlineStrategy
2427TargetProperties::GetInlineStrategy () const
2428{
2429 const uint32_t idx = ePropertyInlineStrategy;
2430 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2431}
2432
Greg Clayton0c8446c2012-10-17 22:57:12 +00002433const char *
2434TargetProperties::GetArg0 () const
2435{
2436 const uint32_t idx = ePropertyArg0;
2437 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2438}
2439
2440void
2441TargetProperties::SetArg0 (const char *arg)
2442{
2443 const uint32_t idx = ePropertyArg0;
2444 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2445}
2446
Greg Clayton73844aa2012-08-22 17:17:09 +00002447bool
2448TargetProperties::GetRunArguments (Args &args) const
2449{
2450 const uint32_t idx = ePropertyRunArgs;
2451 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2452}
2453
2454void
2455TargetProperties::SetRunArguments (const Args &args)
2456{
2457 const uint32_t idx = ePropertyRunArgs;
2458 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2459}
2460
2461size_t
2462TargetProperties::GetEnvironmentAsArgs (Args &env) const
2463{
2464 const uint32_t idx = ePropertyEnvVars;
2465 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2466}
2467
2468bool
2469TargetProperties::GetSkipPrologue() const
2470{
2471 const uint32_t idx = ePropertySkipPrologue;
2472 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2473}
2474
2475PathMappingList &
2476TargetProperties::GetSourcePathMap () const
2477{
2478 const uint32_t idx = ePropertySourceMap;
2479 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
2480 assert(option_value);
2481 return option_value->GetCurrentValue();
2482}
2483
2484FileSpecList &
Greg Claytonc6e82e42012-08-22 18:39:03 +00002485TargetProperties::GetExecutableSearchPaths ()
Greg Clayton73844aa2012-08-22 17:17:09 +00002486{
2487 const uint32_t idx = ePropertyExecutableSearchPaths;
2488 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
2489 assert(option_value);
2490 return option_value->GetCurrentValue();
2491}
2492
2493bool
2494TargetProperties::GetEnableSyntheticValue () const
2495{
2496 const uint32_t idx = ePropertyEnableSynthetic;
2497 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2498}
2499
2500uint32_t
2501TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
2502{
2503 const uint32_t idx = ePropertyMaxChildrenCount;
2504 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2505}
2506
2507uint32_t
2508TargetProperties::GetMaximumSizeOfStringSummary() const
2509{
2510 const uint32_t idx = ePropertyMaxSummaryLength;
2511 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
2512}
2513
2514FileSpec
2515TargetProperties::GetStandardInputPath () const
2516{
2517 const uint32_t idx = ePropertyInputPath;
2518 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2519}
2520
2521void
2522TargetProperties::SetStandardInputPath (const char *p)
2523{
2524 const uint32_t idx = ePropertyInputPath;
2525 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2526}
2527
2528FileSpec
2529TargetProperties::GetStandardOutputPath () const
2530{
2531 const uint32_t idx = ePropertyOutputPath;
2532 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
2533}
2534
2535void
2536TargetProperties::SetStandardOutputPath (const char *p)
2537{
2538 const uint32_t idx = ePropertyOutputPath;
2539 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2540}
2541
2542FileSpec
2543TargetProperties::GetStandardErrorPath () const
2544{
2545 const uint32_t idx = ePropertyErrorPath;
2546 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
2547}
2548
Greg Claytonc6e82e42012-08-22 18:39:03 +00002549const char *
2550TargetProperties::GetExpressionPrefixContentsAsCString ()
2551{
2552 const uint32_t idx = ePropertyExprPrefix;
2553 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
2554 if (file)
Jim Ingham7021cda2012-08-22 21:21:16 +00002555 {
Greg Claytonfc04d242012-08-30 18:15:10 +00002556 const bool null_terminate = true;
2557 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham7021cda2012-08-22 21:21:16 +00002558 if (data_sp)
2559 return (const char *) data_sp->GetBytes();
2560 }
Greg Claytonc6e82e42012-08-22 18:39:03 +00002561 return NULL;
2562}
2563
Greg Clayton73844aa2012-08-22 17:17:09 +00002564void
2565TargetProperties::SetStandardErrorPath (const char *p)
2566{
2567 const uint32_t idx = ePropertyErrorPath;
2568 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
2569}
2570
2571bool
2572TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
2573{
2574 const uint32_t idx = ePropertyBreakpointUseAvoidList;
2575 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2576}
2577
2578const TargetPropertiesSP &
2579Target::GetGlobalProperties()
2580{
2581 static TargetPropertiesSP g_settings_sp;
2582 if (!g_settings_sp)
2583 {
2584 g_settings_sp.reset (new TargetProperties (NULL));
2585 }
2586 return g_settings_sp;
2587}
2588
Jim Ingham5a15e692012-02-16 06:50:00 +00002589const ConstString &
2590Target::TargetEventData::GetFlavorString ()
2591{
2592 static ConstString g_flavor ("Target::TargetEventData");
2593 return g_flavor;
2594}
2595
2596const ConstString &
2597Target::TargetEventData::GetFlavor () const
2598{
2599 return TargetEventData::GetFlavorString ();
2600}
2601
2602Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2603 EventData(),
2604 m_target_sp (new_target_sp)
2605{
2606}
2607
2608Target::TargetEventData::~TargetEventData()
2609{
2610
2611}
2612
2613void
2614Target::TargetEventData::Dump (Stream *s) const
2615{
2616
2617}
2618
2619const TargetSP
2620Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2621{
2622 TargetSP target_sp;
2623
2624 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2625 if (data)
2626 target_sp = data->m_target_sp;
2627
2628 return target_sp;
2629}
2630
2631const Target::TargetEventData *
2632Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2633{
2634 if (event_ptr)
2635 {
2636 const EventData *event_data = event_ptr->GetData();
2637 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2638 return static_cast <const TargetEventData *> (event_ptr->GetData());
2639 }
2640 return NULL;
2641}
2642