blob: 595bcb494e0783aad6fc47212bdc319f2f597121 [file] [log] [blame]
Chris Lattner30fdc8d2010-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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/Target.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Breakpoint/BreakpointResolver.h"
19#include "lldb/Breakpoint/BreakpointResolverAddress.h"
20#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000021#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000023#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000024#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Event.h"
26#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/ModuleSpec.h"
29#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000030#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000031#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000032#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000034#include "lldb/Core/Timer.h"
35#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000036#include "lldb/Expression/ClangASTSource.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000037#include "lldb/Expression/ClangUserExpression.h"
Zachary Turner10687b02014-10-20 17:46:43 +000038#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000040#include "lldb/Interpreter/CommandInterpreter.h"
41#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000042#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000043#include "lldb/Interpreter/OptionValues.h"
44#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/lldb-private-log.h"
46#include "lldb/Symbol/ObjectFile.h"
47#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000048#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000049#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000050#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000051#include "lldb/Target/Thread.h"
52#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54using namespace lldb;
55using namespace lldb_private;
56
Jim Ingham4bddaeb2012-02-16 06:50:00 +000057ConstString &
58Target::GetStaticBroadcasterClass ()
59{
60 static ConstString class_name ("lldb.target");
61 return class_name;
62}
63
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064//----------------------------------------------------------------------
65// Target constructor
66//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:51 +000067Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton67cc0632012-08-22 17:17:09 +000068 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000069 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000070 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000071 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000072 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000073 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000074 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000075 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000076 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077 m_breakpoint_list (false),
78 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000079 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000080 m_process_sp (),
81 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000083 m_scratch_ast_context_ap (),
84 m_scratch_ast_source_ap (),
85 m_ast_importer_ap (),
Jim Ingham9575d842011-03-11 03:53:59 +000086 m_persistent_variables (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000087 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000088 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000089 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000090 m_valid (true),
Enrico Granata5d5f60c2013-09-24 22:58:37 +000091 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000093 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
94 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
95 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +000096 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +000097 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000098
Jim Ingham4bddaeb2012-02-16 06:50:00 +000099 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000100
Greg Clayton5160ce52013-03-27 23:08:40 +0000101 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000103 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000104 if (m_arch.IsValid())
105 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000106 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::Target created with architecture %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
Jason Molendae1b68ad2012-12-05 00:25:49 +0000107 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108}
109
110//----------------------------------------------------------------------
111// Destructor
112//----------------------------------------------------------------------
113Target::~Target()
114{
Greg Clayton5160ce52013-03-27 23:08:40 +0000115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000117 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118 DeleteCurrentProcess ();
119}
120
121void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000122Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123{
Greg Clayton89411422010-10-08 00:21:05 +0000124// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000125 if (description_level != lldb::eDescriptionLevelBrief)
126 {
127 s->Indent();
128 s->PutCString("Target\n");
129 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000130 m_images.Dump(s);
131 m_breakpoint_list.Dump(s);
132 m_internal_breakpoint_list.Dump(s);
133 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000134 }
135 else
136 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000137 Module *exe_module = GetExecutableModulePointer();
138 if (exe_module)
139 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000140 else
141 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000142 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143}
144
145void
Greg Clayton90ba8112012-12-05 00:16:59 +0000146Target::CleanupProcess ()
147{
148 // Do any cleanup of the target we need to do between process instances.
149 // NB It is better to do this before destroying the process in case the
150 // clean up needs some help from the process.
151 m_breakpoint_list.ClearAllBreakpointSites();
152 m_internal_breakpoint_list.ClearAllBreakpointSites();
153 // Disable watchpoints just on the debugger side.
154 Mutex::Locker locker;
155 this->GetWatchpointList().GetListMutex(locker);
156 DisableAllWatchpoints(false);
157 ClearAllWatchpointHitCounts();
158}
159
160void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161Target::DeleteCurrentProcess ()
162{
163 if (m_process_sp.get())
164 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000165 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 if (m_process_sp->IsAlive())
167 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000168
169 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170
Greg Clayton90ba8112012-12-05 00:16:59 +0000171 CleanupProcess ();
172
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 m_process_sp.reset();
174 }
175}
176
177const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000178Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179{
180 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000181 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 return m_process_sp;
183}
184
185const lldb::ProcessSP &
186Target::GetProcessSP () const
187{
188 return m_process_sp;
189}
190
Greg Clayton3418c852011-08-10 02:10:13 +0000191void
192Target::Destroy()
193{
194 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000195 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000196 DeleteCurrentProcess ();
197 m_platform_sp.reset();
198 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000199 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000200 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000201 const bool notify = false;
202 m_breakpoint_list.RemoveAll(notify);
203 m_internal_breakpoint_list.RemoveAll(notify);
204 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000205 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000206 m_search_filter_sp.reset();
207 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000208 m_persistent_variables.Clear();
209 m_stop_hooks.clear();
210 m_stop_hook_next_id = 0;
211 m_suppress_stop_hooks = false;
212}
213
214
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215BreakpointList &
216Target::GetBreakpointList(bool internal)
217{
218 if (internal)
219 return m_internal_breakpoint_list;
220 else
221 return m_breakpoint_list;
222}
223
224const BreakpointList &
225Target::GetBreakpointList(bool internal) const
226{
227 if (internal)
228 return m_internal_breakpoint_list;
229 else
230 return m_breakpoint_list;
231}
232
233BreakpointSP
234Target::GetBreakpointByID (break_id_t break_id)
235{
236 BreakpointSP bp_sp;
237
238 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
239 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
240 else
241 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
242
243 return bp_sp;
244}
245
246BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000247Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000248 const FileSpecList *source_file_spec_list,
249 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000250 bool internal,
251 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252{
Jim Ingham87df91b2011-09-23 00:54:11 +0000253 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
254 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000255 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000256}
257
258
259BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000260Target::CreateBreakpoint (const FileSpecList *containingModules,
261 const FileSpec &file,
262 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000263 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000264 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000265 bool internal,
266 bool hardware)
Jim Ingham969795f2011-09-21 01:17:13 +0000267{
Greg Clayton1f746072012-08-29 21:13:06 +0000268 if (check_inlines == eLazyBoolCalculate)
269 {
270 const InlineStrategy inline_strategy = GetInlineStrategy();
271 switch (inline_strategy)
272 {
273 case eInlineBreakpointsNever:
274 check_inlines = eLazyBoolNo;
275 break;
276
277 case eInlineBreakpointsHeaders:
278 if (file.IsSourceImplementationFile())
279 check_inlines = eLazyBoolNo;
280 else
281 check_inlines = eLazyBoolYes;
282 break;
283
284 case eInlineBreakpointsAlways:
285 check_inlines = eLazyBoolYes;
286 break;
287 }
288 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000289 SearchFilterSP filter_sp;
290 if (check_inlines == eLazyBoolNo)
291 {
292 // Not checking for inlines, we are looking only for matching compile units
293 FileSpecList compile_unit_list;
294 compile_unit_list.Append (file);
295 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
296 }
297 else
298 {
299 filter_sp = GetSearchFilterForModuleList (containingModules);
300 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000301 if (skip_prologue == eLazyBoolCalculate)
302 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
303
Greg Clayton1f746072012-08-29 21:13:06 +0000304 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
305 file,
306 line_no,
307 check_inlines,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000308 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000309 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310}
311
312
313BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000314Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 Address so_addr;
317 // Attempt to resolve our load address if possible, though it is ok if
318 // it doesn't resolve to section/offset.
319
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000320 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000321 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000322 if (!so_addr.IsValid())
323 {
324 // The address didn't resolve, so just set this as an absolute address
325 so_addr.SetOffset (addr);
326 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000327 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 return bp_sp;
329}
330
331BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000332Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000334 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000336 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337}
338
339BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000340Target::CreateBreakpoint (const FileSpecList *containingModules,
341 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000342 const char *func_name,
343 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000344 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000345 bool internal,
346 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000348 BreakpointSP bp_sp;
349 if (func_name)
350 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000351 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000352
353 if (skip_prologue == eLazyBoolCalculate)
354 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
355
Greg Claytond16e1e52011-07-12 17:06:17 +0000356 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
357 func_name,
358 func_name_type_mask,
359 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000360 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000361 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000362 }
363 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364}
365
Jim Inghamfab10e82012-03-06 00:37:27 +0000366lldb::BreakpointSP
367Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000368 const FileSpecList *containingSourceFiles,
369 const std::vector<std::string> &func_names,
370 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000371 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000372 bool internal,
373 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000374{
375 BreakpointSP bp_sp;
376 size_t num_names = func_names.size();
377 if (num_names > 0)
378 {
379 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000380
381 if (skip_prologue == eLazyBoolCalculate)
382 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
383
384 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000385 func_names,
386 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000387 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000388 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000389 }
390 return bp_sp;
391}
392
Jim Ingham133e0fb2012-03-03 02:05:11 +0000393BreakpointSP
394Target::CreateBreakpoint (const FileSpecList *containingModules,
395 const FileSpecList *containingSourceFiles,
396 const char *func_names[],
397 size_t num_names,
398 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000399 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000400 bool internal,
401 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000402{
403 BreakpointSP bp_sp;
404 if (num_names > 0)
405 {
406 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
407
Greg Clayton03da4cc2013-04-19 21:31:16 +0000408 if (skip_prologue == eLazyBoolCalculate)
409 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
410
411 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000412 func_names,
413 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000414 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000415 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000416 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000417 }
418 return bp_sp;
419}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420
421SearchFilterSP
422Target::GetSearchFilterForModule (const FileSpec *containingModule)
423{
424 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 if (containingModule != NULL)
426 {
427 // TODO: We should look into sharing module based search filters
428 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000429 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430 }
431 else
432 {
433 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000434 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 filter_sp = m_search_filter_sp;
436 }
437 return filter_sp;
438}
439
Jim Ingham969795f2011-09-21 01:17:13 +0000440SearchFilterSP
441Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
442{
443 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000444 if (containingModules && containingModules->GetSize() != 0)
445 {
446 // TODO: We should look into sharing module based search filters
447 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000448 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000449 }
450 else
451 {
452 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000453 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000454 filter_sp = m_search_filter_sp;
455 }
456 return filter_sp;
457}
458
Jim Ingham87df91b2011-09-23 00:54:11 +0000459SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000460Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
461 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000462{
463 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
464 return GetSearchFilterForModuleList(containingModules);
465
466 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000467 if (containingModules == NULL)
468 {
469 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
470 // but that will take a little reworking.
471
Greg Claytone1cd1be2012-01-29 20:56:30 +0000472 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000473 }
474 else
475 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000476 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000477 }
478 return filter_sp;
479}
480
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000482Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000483 const FileSpecList *containingSourceFiles,
484 RegularExpression &func_regex,
485 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000486 bool internal,
487 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488{
Jim Ingham87df91b2011-09-23 00:54:11 +0000489 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000490 bool skip =
491 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
492 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000493 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
494 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000495 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496
Jim Ingham1460e4b2014-01-10 23:46:59 +0000497 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498}
499
Jim Ingham219ba192012-03-05 04:47:34 +0000500lldb::BreakpointSP
501Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
502{
503 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
504}
505
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000507Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508{
509 BreakpointSP bp_sp;
510 if (filter_sp && resolver_sp)
511 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000512 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 resolver_sp->SetBreakpoint (bp_sp.get());
514
515 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000516 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 else
Greg Clayton9fed0d82010-07-23 23:33:17 +0000518 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519
Greg Clayton5160ce52013-03-27 23:08:40 +0000520 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 if (log)
522 {
523 StreamString s;
524 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
525 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
526 }
527
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528 bp_sp->ResolveBreakpoint();
529 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000530
531 if (!internal && bp_sp)
532 {
533 m_last_created_breakpoint = bp_sp;
534 }
535
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 return bp_sp;
537}
538
Johnny Chen86364b42011-09-20 23:28:55 +0000539bool
540Target::ProcessIsValid()
541{
542 return (m_process_sp && m_process_sp->IsAlive());
543}
544
Johnny Chenb90827e2012-06-04 23:19:54 +0000545static bool
546CheckIfWatchpointsExhausted(Target *target, Error &error)
547{
548 uint32_t num_supported_hardware_watchpoints;
549 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
550 if (rc.Success())
551 {
552 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
553 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
554 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
555 num_supported_hardware_watchpoints);
556 }
557 return false;
558}
559
Johnny Chen01a67862011-10-14 00:42:25 +0000560// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000561// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000562WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000563Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000564{
Greg Clayton5160ce52013-03-27 23:08:40 +0000565 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000566 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000567 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000568 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000569
Johnny Chen01a67862011-10-14 00:42:25 +0000570 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000571 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000572 {
573 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000574 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000575 }
Jim Inghamc6462312013-06-18 21:52:48 +0000576
Johnny Chen45e541f2011-09-14 22:20:15 +0000577 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000578 {
579 if (size == 0)
580 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
581 else
Daniel Malead01b2952012-11-29 21:49:15 +0000582 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000583 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000584 }
Jim Inghamc6462312013-06-18 21:52:48 +0000585
586 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
587 {
588 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
589 }
Johnny Chen7313a642011-09-13 01:15:36 +0000590
Johnny Chen01a67862011-10-14 00:42:25 +0000591 // Currently we only support one watchpoint per address, with total number
592 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000593
594 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000595 const bool notify = false; // Don't notify about all the state changes we do on creating the watchpoint.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000596 Mutex::Locker locker;
597 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000598 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000599 if (matched_sp)
600 {
Johnny Chen0c406372011-09-14 20:23:45 +0000601 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000602 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000603 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
604 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000605 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000606 if (size == old_size && kind == old_type)
607 {
Johnny Chen01a67862011-10-14 00:42:25 +0000608 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000609 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000610 }
611 else
612 {
Johnny Chen01a67862011-10-14 00:42:25 +0000613 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000614 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
615 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000616 }
Johnny Chen3c532582011-09-13 23:29:31 +0000617 }
618
Jason Molenda727e3922012-12-05 23:07:34 +0000619 if (!wp_sp)
620 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000621 wp_sp.reset(new Watchpoint(*this, addr, size, type));
622 wp_sp->SetWatchpointType(kind, notify);
623 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000624 }
Johnny Chen0c406372011-09-14 20:23:45 +0000625
Jim Ingham1b5792e2012-12-18 02:03:49 +0000626 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000627 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000628 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
629 __FUNCTION__,
630 error.Success() ? "succeeded" : "failed",
631 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000632
Jason Molenda727e3922012-12-05 23:07:34 +0000633 if (error.Fail())
634 {
Johnny Chen41b77262012-03-26 22:00:10 +0000635 // Enabling the watchpoint on the device side failed.
636 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000637 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000638 // See if we could provide more helpful error message.
639 if (!CheckIfWatchpointsExhausted(this, error))
640 {
641 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000642 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000643 }
Johnny Chen01a67862011-10-14 00:42:25 +0000644 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000645 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000646 else
Johnny Chen01a67862011-10-14 00:42:25 +0000647 m_last_created_watchpoint = wp_sp;
648 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000649}
650
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651void
652Target::RemoveAllBreakpoints (bool internal_also)
653{
Greg Clayton5160ce52013-03-27 23:08:40 +0000654 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655 if (log)
656 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
657
Greg Clayton9fed0d82010-07-23 23:33:17 +0000658 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000660 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000661
662 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663}
664
665void
666Target::DisableAllBreakpoints (bool internal_also)
667{
Greg Clayton5160ce52013-03-27 23:08:40 +0000668 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 if (log)
670 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
671
672 m_breakpoint_list.SetEnabledAll (false);
673 if (internal_also)
674 m_internal_breakpoint_list.SetEnabledAll (false);
675}
676
677void
678Target::EnableAllBreakpoints (bool internal_also)
679{
Greg Clayton5160ce52013-03-27 23:08:40 +0000680 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681 if (log)
682 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
683
684 m_breakpoint_list.SetEnabledAll (true);
685 if (internal_also)
686 m_internal_breakpoint_list.SetEnabledAll (true);
687}
688
689bool
690Target::RemoveBreakpointByID (break_id_t break_id)
691{
Greg Clayton5160ce52013-03-27 23:08:40 +0000692 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 if (log)
694 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
695
696 if (DisableBreakpointByID (break_id))
697 {
698 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000699 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000701 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000702 if (m_last_created_breakpoint)
703 {
704 if (m_last_created_breakpoint->GetID() == break_id)
705 m_last_created_breakpoint.reset();
706 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000707 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000708 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 return true;
710 }
711 return false;
712}
713
714bool
715Target::DisableBreakpointByID (break_id_t break_id)
716{
Greg Clayton5160ce52013-03-27 23:08:40 +0000717 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 if (log)
719 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
720
721 BreakpointSP bp_sp;
722
723 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
724 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
725 else
726 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
727 if (bp_sp)
728 {
729 bp_sp->SetEnabled (false);
730 return true;
731 }
732 return false;
733}
734
735bool
736Target::EnableBreakpointByID (break_id_t break_id)
737{
Greg Clayton5160ce52013-03-27 23:08:40 +0000738 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739 if (log)
740 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
741 __FUNCTION__,
742 break_id,
743 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
744
745 BreakpointSP bp_sp;
746
747 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
748 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
749 else
750 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
751
752 if (bp_sp)
753 {
754 bp_sp->SetEnabled (true);
755 return true;
756 }
757 return false;
758}
759
Johnny Chenedf50372011-09-23 21:21:43 +0000760// The flag 'end_to_end', default to true, signifies that the operation is
761// performed end to end, for both the debugger and the debuggee.
762
Johnny Chen01a67862011-10-14 00:42:25 +0000763// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
764// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000765bool
Johnny Chen01a67862011-10-14 00:42:25 +0000766Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000767{
Greg Clayton5160ce52013-03-27 23:08:40 +0000768 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000769 if (log)
770 log->Printf ("Target::%s\n", __FUNCTION__);
771
Johnny Chenedf50372011-09-23 21:21:43 +0000772 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000773 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000774 return true;
775 }
776
777 // Otherwise, it's an end to end operation.
778
Johnny Chen86364b42011-09-20 23:28:55 +0000779 if (!ProcessIsValid())
780 return false;
781
Johnny Chen01a67862011-10-14 00:42:25 +0000782 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000783 for (size_t i = 0; i < num_watchpoints; ++i)
784 {
Johnny Chen01a67862011-10-14 00:42:25 +0000785 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
786 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000787 return false;
788
Johnny Chen01a67862011-10-14 00:42:25 +0000789 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000790 if (rc.Fail())
791 return false;
792 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000793 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000794 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000795 return true; // Success!
796}
797
Johnny Chen01a67862011-10-14 00:42:25 +0000798// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
799// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000800bool
Johnny Chen01a67862011-10-14 00:42:25 +0000801Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000802{
Greg Clayton5160ce52013-03-27 23:08:40 +0000803 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000804 if (log)
805 log->Printf ("Target::%s\n", __FUNCTION__);
806
Johnny Chenedf50372011-09-23 21:21:43 +0000807 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000808 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000809 return true;
810 }
811
812 // Otherwise, it's an end to end operation.
813
Johnny Chen86364b42011-09-20 23:28:55 +0000814 if (!ProcessIsValid())
815 return false;
816
Johnny Chen01a67862011-10-14 00:42:25 +0000817 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000818 for (size_t i = 0; i < num_watchpoints; ++i)
819 {
Johnny Chen01a67862011-10-14 00:42:25 +0000820 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
821 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000822 return false;
823
Johnny Chen01a67862011-10-14 00:42:25 +0000824 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000825 if (rc.Fail())
826 return false;
827 }
Johnny Chen86364b42011-09-20 23:28:55 +0000828 return true; // Success!
829}
830
Johnny Chen01a67862011-10-14 00:42:25 +0000831// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
832// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000833bool
Johnny Chen01a67862011-10-14 00:42:25 +0000834Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000835{
Greg Clayton5160ce52013-03-27 23:08:40 +0000836 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000837 if (log)
838 log->Printf ("Target::%s\n", __FUNCTION__);
839
Johnny Chenedf50372011-09-23 21:21:43 +0000840 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000841 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000842 return true;
843 }
844
845 // Otherwise, it's an end to end operation.
846
Johnny Chen86364b42011-09-20 23:28:55 +0000847 if (!ProcessIsValid())
848 return false;
849
Johnny Chen01a67862011-10-14 00:42:25 +0000850 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000851 for (size_t i = 0; i < num_watchpoints; ++i)
852 {
Johnny Chen01a67862011-10-14 00:42:25 +0000853 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
854 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000855 return false;
856
Johnny Chen01a67862011-10-14 00:42:25 +0000857 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000858 if (rc.Fail())
859 return false;
860 }
Johnny Chen86364b42011-09-20 23:28:55 +0000861 return true; // Success!
862}
863
Johnny Chena4d6bc92012-02-25 06:44:30 +0000864// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
865bool
866Target::ClearAllWatchpointHitCounts ()
867{
Greg Clayton5160ce52013-03-27 23:08:40 +0000868 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000869 if (log)
870 log->Printf ("Target::%s\n", __FUNCTION__);
871
872 size_t num_watchpoints = m_watchpoint_list.GetSize();
873 for (size_t i = 0; i < num_watchpoints; ++i)
874 {
875 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
876 if (!wp_sp)
877 return false;
878
879 wp_sp->ResetHitCount();
880 }
881 return true; // Success!
882}
883
Johnny Chen01a67862011-10-14 00:42:25 +0000884// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000885// during these operations.
886bool
Johnny Chen01a67862011-10-14 00:42:25 +0000887Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000888{
Greg Clayton5160ce52013-03-27 23:08:40 +0000889 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000890 if (log)
891 log->Printf ("Target::%s\n", __FUNCTION__);
892
893 if (!ProcessIsValid())
894 return false;
895
Johnny Chen01a67862011-10-14 00:42:25 +0000896 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000897 for (size_t i = 0; i < num_watchpoints; ++i)
898 {
Johnny Chen01a67862011-10-14 00:42:25 +0000899 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
900 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000901 return false;
902
Johnny Chen01a67862011-10-14 00:42:25 +0000903 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000904 }
905 return true; // Success!
906}
907
Johnny Chen01a67862011-10-14 00:42:25 +0000908// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000909bool
Johnny Chen01a67862011-10-14 00:42:25 +0000910Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000911{
Greg Clayton5160ce52013-03-27 23:08:40 +0000912 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000913 if (log)
914 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
915
916 if (!ProcessIsValid())
917 return false;
918
Johnny Chen01a67862011-10-14 00:42:25 +0000919 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
920 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000921 {
Johnny Chen01a67862011-10-14 00:42:25 +0000922 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000923 if (rc.Success())
924 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000925
Johnny Chenf04ee932011-09-22 18:04:58 +0000926 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000927 }
928 return false;
929}
930
Johnny Chen01a67862011-10-14 00:42:25 +0000931// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000932bool
Johnny Chen01a67862011-10-14 00:42:25 +0000933Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000934{
Greg Clayton5160ce52013-03-27 23:08:40 +0000935 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000936 if (log)
937 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
938
939 if (!ProcessIsValid())
940 return false;
941
Johnny Chen01a67862011-10-14 00:42:25 +0000942 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
943 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000944 {
Johnny Chen01a67862011-10-14 00:42:25 +0000945 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000946 if (rc.Success())
947 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000948
Johnny Chenf04ee932011-09-22 18:04:58 +0000949 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000950 }
951 return false;
952}
953
Johnny Chen01a67862011-10-14 00:42:25 +0000954// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000955bool
Johnny Chen01a67862011-10-14 00:42:25 +0000956Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000957{
Greg Clayton5160ce52013-03-27 23:08:40 +0000958 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000959 if (log)
960 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
961
Jim Inghamb0b45132013-07-02 02:09:46 +0000962 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
963 if (watch_to_remove_sp == m_last_created_watchpoint)
964 m_last_created_watchpoint.reset();
965
Johnny Chen01a67862011-10-14 00:42:25 +0000966 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +0000967 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000968 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +0000969 return true;
970 }
971 return false;
972}
973
Johnny Chen01a67862011-10-14 00:42:25 +0000974// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +0000975bool
Johnny Chen01a67862011-10-14 00:42:25 +0000976Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000977{
Greg Clayton5160ce52013-03-27 23:08:40 +0000978 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000979 if (log)
980 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
981
982 if (!ProcessIsValid())
983 return false;
984
Johnny Chen01a67862011-10-14 00:42:25 +0000985 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
986 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000987 {
Johnny Chen01a67862011-10-14 00:42:25 +0000988 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000989 return true;
990 }
991 return false;
992}
993
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994ModuleSP
995Target::GetExecutableModule ()
996{
Greg Claytonaa149cb2011-08-11 02:48:45 +0000997 return m_images.GetModuleAtIndex(0);
998}
999
1000Module*
1001Target::GetExecutableModulePointer ()
1002{
1003 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004}
1005
Enrico Granata17598482012-11-08 02:22:02 +00001006static void
1007LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1008{
1009 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001010 StreamString feedback_stream;
1011 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001012 {
Enrico Granata97303392013-05-21 00:00:30 +00001013 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001014 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001015 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1016 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001017 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001018 if (feedback_stream.GetSize())
1019 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1020 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001021}
1022
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023void
Greg Claytonb35db632013-11-09 00:03:31 +00001024Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025{
Greg Claytonb35db632013-11-09 00:03:31 +00001026 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001027 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028 m_images.Clear();
1029 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001030 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001031 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001032}
1033
1034void
Greg Claytonb35db632013-11-09 00:03:31 +00001035Target::DidExec ()
1036{
1037 // When a process exec's we need to know about it so we can do some cleanup.
1038 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1039 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1040}
1041
1042void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001043Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1044{
1045 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001046 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047
1048 if (executable_sp.get())
1049 {
1050 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001051 "Target::SetExecutableModule (executable = '%s')",
1052 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001053
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001054 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055
Jim Ingham5aee1622010-08-09 23:31:02 +00001056 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton32e0a752011-03-30 18:16:51 +00001057 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001058 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001059 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001060 if (log)
1061 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1062 }
1063
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001065 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001067 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068 {
1069 executable_objfile->GetDependentModules(dependent_files);
1070 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1071 {
Greg Claytonded470d2011-03-19 01:12:21 +00001072 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1073 FileSpec platform_dependent_file_spec;
1074 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001075 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001076 else
1077 platform_dependent_file_spec = dependent_file_spec;
1078
Greg Claytonb9a01b32012-02-26 05:51:37 +00001079 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1080 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081 if (image_module_sp.get())
1082 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 ObjectFile *objfile = image_module_sp->GetObjectFile();
1084 if (objfile)
1085 objfile->GetDependentModules(dependent_files);
1086 }
1087 }
1088 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 }
1090}
1091
1092
Jim Ingham5aee1622010-08-09 23:31:02 +00001093bool
1094Target::SetArchitecture (const ArchSpec &arch_spec)
1095{
Greg Clayton5160ce52013-03-27 23:08:40 +00001096 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001097 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001098 {
Greg Clayton70512312012-05-08 01:45:38 +00001099 // If we haven't got a valid arch spec, or the architectures are
1100 // compatible, so just update the architecture. Architectures can be
1101 // equal, yet the triple OS and vendor might change, so we need to do
1102 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001103 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001104 if (log)
1105 log->Printf ("Target::SetArchitecture setting architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Jim Ingham5aee1622010-08-09 23:31:02 +00001106 return true;
1107 }
1108 else
1109 {
1110 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001111 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001112 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00001113 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001114 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001115
Greg Claytonb35db632013-11-09 00:03:31 +00001116 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001117 // Need to do something about unsetting breakpoints.
1118
1119 if (executable_sp)
1120 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001121 if (log)
1122 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Greg Claytonb9a01b32012-02-26 05:51:37 +00001123 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1124 Error error = ModuleList::GetSharedModule (module_spec,
1125 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001126 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001127 NULL,
1128 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001129
1130 if (!error.Fail() && executable_sp)
1131 {
1132 SetExecutableModule (executable_sp, true);
1133 return true;
1134 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001135 }
1136 }
Greg Clayton70512312012-05-08 01:45:38 +00001137 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001138}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140void
Enrico Granataefe637d2012-11-08 19:16:03 +00001141Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001142{
1143}
1144
1145void
Enrico Granataefe637d2012-11-08 19:16:03 +00001146Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147{
1148 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001149 if (m_valid)
1150 {
1151 ModuleList my_module_list;
1152 my_module_list.Append(module_sp);
1153 LoadScriptingResourceForModule(module_sp, this);
1154 ModulesDidLoad (my_module_list);
1155 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156}
1157
1158void
Enrico Granataefe637d2012-11-08 19:16:03 +00001159Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001160{
1161 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001162 if (m_valid)
1163 {
1164 ModuleList my_module_list;
1165 my_module_list.Append(module_sp);
1166 ModulesDidUnload (my_module_list, false);
1167 }
Enrico Granata17598482012-11-08 02:22:02 +00001168}
1169
1170void
Enrico Granataefe637d2012-11-08 19:16:03 +00001171Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172{
Jim Inghame716ae02011-08-03 01:00:06 +00001173 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001174 if (m_valid)
1175 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176}
1177
1178void
1179Target::ModulesDidLoad (ModuleList &module_list)
1180{
Greg Clayton23f8c952014-03-24 23:10:19 +00001181 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001182 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001183 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001184 if (m_process_sp)
1185 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001186 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001187 }
Enrico Granata17598482012-11-08 02:22:02 +00001188 // TODO: make event data that packages up the module_list
1189 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1190 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191}
1192
1193void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001194Target::SymbolsDidLoad (ModuleList &module_list)
1195{
Greg Clayton23f8c952014-03-24 23:10:19 +00001196 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001197 {
Jim Ingham31caf982013-06-04 23:01:35 +00001198 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001199 {
Jim Ingham31caf982013-06-04 23:01:35 +00001200 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1201 if (runtime)
1202 {
1203 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1204 objc_runtime->SymbolsDidLoad(module_list);
1205 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001206 }
Jim Ingham31caf982013-06-04 23:01:35 +00001207
Greg Clayton095eeaa2013-11-05 23:28:00 +00001208 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jim Ingham31caf982013-06-04 23:01:35 +00001209 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001210 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001211}
1212
1213void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001214Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215{
Greg Clayton23f8c952014-03-24 23:10:19 +00001216 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001217 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001218 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Enrico Granata17598482012-11-08 02:22:02 +00001219 // TODO: make event data that packages up the module_list
1220 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1221 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222}
1223
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001224bool
Greg Claytonb9a01b32012-02-26 05:51:37 +00001225Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001226{
Greg Clayton67cc0632012-08-22 17:17:09 +00001227 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001228 {
1229 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001230 ModuleSpec module_spec (module_file_spec);
1231 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001232
1233 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1234 // black list.
1235 if (num_modules > 0)
1236 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001237 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001238 {
1239 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1240 return false;
1241 }
1242 return true;
1243 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001244 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001245 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001246}
1247
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001248bool
Jim Inghamc6674fd2011-10-28 23:14:11 +00001249Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1250{
Greg Clayton67cc0632012-08-22 17:17:09 +00001251 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001252 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001253 if (m_platform_sp)
1254 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001255 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001256 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001257}
1258
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259size_t
Greg Claytondb598232011-01-07 01:57:07 +00001260Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1261{
Greg Claytone72dfb32012-02-24 01:59:29 +00001262 SectionSP section_sp (addr.GetSection());
1263 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001264 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001265 // If the contents of this section are encrypted, the on-disk file is unusable. Read only from live memory.
Jason Molenda216d91f2012-04-25 00:06:56 +00001266 if (section_sp->IsEncrypted())
1267 {
Greg Clayton57f06302012-05-25 17:05:55 +00001268 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001269 return 0;
1270 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001271 ModuleSP module_sp (section_sp->GetModule());
1272 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001273 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001274 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1275 if (objfile)
1276 {
1277 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1278 addr.GetOffset(),
1279 dst,
1280 dst_len);
1281 if (bytes_read > 0)
1282 return bytes_read;
1283 else
1284 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1285 }
Greg Claytondb598232011-01-07 01:57:07 +00001286 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001287 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001288 }
1289 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001290 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001291 }
1292 else
Greg Claytondb598232011-01-07 01:57:07 +00001293 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001294
Greg Claytondb598232011-01-07 01:57:07 +00001295 return 0;
1296}
1297
1298size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001299Target::ReadMemory (const Address& addr,
1300 bool prefer_file_cache,
1301 void *dst,
1302 size_t dst_len,
1303 Error &error,
1304 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001305{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001307
Enrico Granata9128ee22011-09-06 19:20:51 +00001308 // if we end up reading this from process memory, we will fill this
1309 // with the actual load address
1310 if (load_addr_ptr)
1311 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1312
Greg Claytondb598232011-01-07 01:57:07 +00001313 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001314
1315 addr_t load_addr = LLDB_INVALID_ADDRESS;
1316 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001317 Address resolved_addr;
1318 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001319 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001320 SectionLoadList &section_load_list = GetSectionLoadList();
1321 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001322 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001323 // No sections are loaded, so we must assume we are not running
1324 // yet and anything we are given is a file address.
1325 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1326 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001327 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001328 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001329 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001330 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001331 // we have manually loaded some sections with "target modules load ..."
1332 // or because we have have a live process that has sections loaded
1333 // through the dynamic loader
1334 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001335 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001336 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001337 }
Greg Clayton357132e2011-03-26 19:14:58 +00001338 if (!resolved_addr.IsValid())
1339 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001340
Greg Claytonc749eb82011-07-11 05:12:02 +00001341
Greg Claytondb598232011-01-07 01:57:07 +00001342 if (prefer_file_cache)
1343 {
1344 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1345 if (bytes_read > 0)
1346 return bytes_read;
1347 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001348
Johnny Chen86364b42011-09-20 23:28:55 +00001349 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001350 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001351 if (load_addr == LLDB_INVALID_ADDRESS)
1352 load_addr = resolved_addr.GetLoadAddress (this);
1353
Greg Claytondda4f7b2010-06-30 23:03:03 +00001354 if (load_addr == LLDB_INVALID_ADDRESS)
1355 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001356 ModuleSP addr_module_sp (resolved_addr.GetModule());
1357 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001358 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Claytone72dfb32012-02-24 01:59:29 +00001359 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08 +00001360 resolved_addr.GetFileAddress(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001361 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001362 else
Daniel Malead01b2952012-11-29 21:49:15 +00001363 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001364 }
1365 else
1366 {
Greg Claytondb598232011-01-07 01:57:07 +00001367 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368 if (bytes_read != dst_len)
1369 {
1370 if (error.Success())
1371 {
1372 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001373 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 else
Daniel Malead01b2952012-11-29 21:49:15 +00001375 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 Lattner30fdc8d2010-06-08 16:52:24 +00001376 }
1377 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001378 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001379 {
1380 if (load_addr_ptr)
1381 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001382 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001383 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001384 // If the address is not section offset we have an address that
1385 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001386 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001387 // more we can do. If it is section offset, we might be able to
1388 // read cached memory from the object file.
1389 if (!resolved_addr.IsSectionOffset())
1390 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001393
Greg Claytonc749eb82011-07-11 05:12:02 +00001394 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001395 {
Greg Claytondb598232011-01-07 01:57:07 +00001396 // If we didn't already try and read from the object file cache, then
1397 // try it after failing to read from the process.
1398 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001399 }
1400 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401}
1402
Greg Claytond16e1e52011-07-12 17:06:17 +00001403size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001404Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1405{
1406 char buf[256];
1407 out_str.clear();
1408 addr_t curr_addr = addr.GetLoadAddress(this);
1409 Address address(addr);
1410 while (1)
1411 {
1412 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1413 if (length == 0)
1414 break;
1415 out_str.append(buf, length);
1416 // If we got "length - 1" bytes, we didn't get the whole C string, we
1417 // need to read some more characters
1418 if (length == sizeof(buf) - 1)
1419 curr_addr += length;
1420 else
1421 break;
1422 address = Address(curr_addr);
1423 }
1424 return out_str.size();
1425}
1426
1427
1428size_t
1429Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1430{
1431 size_t total_cstr_len = 0;
1432 if (dst && dst_max_len)
1433 {
1434 result_error.Clear();
1435 // NULL out everything just to be safe
1436 memset (dst, 0, dst_max_len);
1437 Error error;
1438 addr_t curr_addr = addr.GetLoadAddress(this);
1439 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001440
1441 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1442 // think this really needs to be tied to the memory cache subsystem's
1443 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001444 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001445
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001446 size_t bytes_left = dst_max_len - 1;
1447 char *curr_dst = dst;
1448
1449 while (bytes_left > 0)
1450 {
1451 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1452 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1453 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1454
1455 if (bytes_read == 0)
1456 {
1457 result_error = error;
1458 dst[total_cstr_len] = '\0';
1459 break;
1460 }
1461 const size_t len = strlen(curr_dst);
1462
1463 total_cstr_len += len;
1464
1465 if (len < bytes_to_read)
1466 break;
1467
1468 curr_dst += bytes_read;
1469 curr_addr += bytes_read;
1470 bytes_left -= bytes_read;
1471 address = Address(curr_addr);
1472 }
1473 }
1474 else
1475 {
1476 if (dst == NULL)
1477 result_error.SetErrorString("invalid arguments");
1478 else
1479 result_error.Clear();
1480 }
1481 return total_cstr_len;
1482}
1483
1484size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001485Target::ReadScalarIntegerFromMemory (const Address& addr,
1486 bool prefer_file_cache,
1487 uint32_t byte_size,
1488 bool is_signed,
1489 Scalar &scalar,
1490 Error &error)
1491{
1492 uint64_t uval;
1493
1494 if (byte_size <= sizeof(uval))
1495 {
1496 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1497 if (bytes_read == byte_size)
1498 {
1499 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001500 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001501 if (byte_size <= 4)
1502 scalar = data.GetMaxU32 (&offset, byte_size);
1503 else
1504 scalar = data.GetMaxU64 (&offset, byte_size);
1505
1506 if (is_signed)
1507 scalar.SignExtend(byte_size * 8);
1508 return bytes_read;
1509 }
1510 }
1511 else
1512 {
1513 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1514 }
1515 return 0;
1516}
1517
1518uint64_t
1519Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1520 bool prefer_file_cache,
1521 size_t integer_byte_size,
1522 uint64_t fail_value,
1523 Error &error)
1524{
1525 Scalar scalar;
1526 if (ReadScalarIntegerFromMemory (addr,
1527 prefer_file_cache,
1528 integer_byte_size,
1529 false,
1530 scalar,
1531 error))
1532 return scalar.ULongLong(fail_value);
1533 return fail_value;
1534}
1535
1536bool
1537Target::ReadPointerFromMemory (const Address& addr,
1538 bool prefer_file_cache,
1539 Error &error,
1540 Address &pointer_addr)
1541{
1542 Scalar scalar;
1543 if (ReadScalarIntegerFromMemory (addr,
1544 prefer_file_cache,
1545 m_arch.GetAddressByteSize(),
1546 false,
1547 scalar,
1548 error))
1549 {
1550 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1551 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1552 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001553 SectionLoadList &section_load_list = GetSectionLoadList();
1554 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001555 {
1556 // No sections are loaded, so we must assume we are not running
1557 // yet and anything we are given is a file address.
1558 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1559 }
1560 else
1561 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001562 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001563 // we have manually loaded some sections with "target modules load ..."
1564 // or because we have have a live process that has sections loaded
1565 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001566 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001567 }
1568 // We weren't able to resolve the pointer value, so just return
1569 // an address with no section
1570 if (!pointer_addr.IsValid())
1571 pointer_addr.SetOffset (pointer_vm_addr);
1572 return true;
1573
1574 }
1575 }
1576 return false;
1577}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001578
1579ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001580Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001581{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001582 ModuleSP module_sp;
1583
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001584 Error error;
1585
Jim Ingham4a94c912012-05-17 18:38:42 +00001586 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1587 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001588
Jim Ingham4a94c912012-05-17 18:38:42 +00001589 if (module_spec.GetUUID().IsValid())
1590 module_sp = m_images.FindFirstModule(module_spec);
1591
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001592 if (!module_sp)
1593 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001594 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1595 bool did_create_module = false;
1596
1597 // If there are image search path entries, try to use them first to acquire a suitable image.
1598 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001599 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001600 ModuleSpec transformed_spec (module_spec);
1601 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1602 {
1603 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1604 error = ModuleList::GetSharedModule (transformed_spec,
1605 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001606 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001607 &old_module_sp,
1608 &did_create_module);
1609 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001610 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001611
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001612 if (!module_sp)
1613 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001614 // If we have a UUID, we can check our global shared module list in case
1615 // we already have it. If we don't have a valid UUID, then we can't since
1616 // the path in "module_spec" will be a platform path, and we will need to
1617 // let the platform find that file. For example, we could be asking for
1618 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1619 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1620 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1621 // cache.
1622 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001623 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001624 // We have a UUID, it is OK to check the global module list...
1625 error = ModuleList::GetSharedModule (module_spec,
1626 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001627 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001628 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001629 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001630 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001631
1632 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001633 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001634 // The platform is responsible for finding and caching an appropriate
1635 // module in the shared module cache.
1636 if (m_platform_sp)
1637 {
1638 FileSpec platform_file_spec;
1639 error = m_platform_sp->GetSharedModule (module_spec,
1640 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001641 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001642 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001643 &did_create_module);
1644 }
1645 else
1646 {
1647 error.SetErrorString("no platform is currently set");
1648 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001649 }
1650 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001651
Jim Ingham4a94c912012-05-17 18:38:42 +00001652 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1653 // module in the list already, and if there was, let's remove it.
1654 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001656 ObjectFile *objfile = module_sp->GetObjectFile();
1657 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001658 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001659 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001660 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001661 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1662 case ObjectFile::eTypeExecutable: /// A normal executable
1663 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1664 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1665 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1666 break;
1667 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1668 if (error_ptr)
1669 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1670 return ModuleSP();
1671 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1672 if (error_ptr)
1673 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1674 return ModuleSP();
1675 default:
1676 if (error_ptr)
1677 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1678 return ModuleSP();
1679 }
1680 // GetSharedModule is not guaranteed to find the old shared module, for instance
1681 // in the common case where you pass in the UUID, it is only going to find the one
1682 // module matching the UUID. In fact, it has no good way to know what the "old module"
1683 // relevant to this target is, since there might be many copies of a module with this file spec
1684 // in various running debug sessions, but only one of them will belong to this target.
1685 // So let's remove the UUID from the module list, and look in the target's module list.
1686 // Only do this if there is SOMETHING else in the module spec...
1687 if (!old_module_sp)
1688 {
1689 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001690 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001691 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1692 module_spec_copy.GetUUID().Clear();
1693
1694 ModuleList found_modules;
1695 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1696 if (num_found == 1)
1697 {
1698 old_module_sp = found_modules.GetModuleAtIndex(0);
1699 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001700 }
1701 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001702
1703 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1704 {
1705 m_images.ReplaceModule(old_module_sp, module_sp);
1706 Module *old_module_ptr = old_module_sp.get();
1707 old_module_sp.reset();
1708 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1709 }
1710 else
1711 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001712 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001713 else
1714 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001715 }
1716 }
1717 if (error_ptr)
1718 *error_ptr = error;
1719 return module_sp;
1720}
1721
1722
Greg Claytond9e416c2012-02-18 05:35:26 +00001723TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001724Target::CalculateTarget ()
1725{
Greg Claytond9e416c2012-02-18 05:35:26 +00001726 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727}
1728
Greg Claytond9e416c2012-02-18 05:35:26 +00001729ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730Target::CalculateProcess ()
1731{
Greg Claytond9e416c2012-02-18 05:35:26 +00001732 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733}
1734
Greg Claytond9e416c2012-02-18 05:35:26 +00001735ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736Target::CalculateThread ()
1737{
Greg Claytond9e416c2012-02-18 05:35:26 +00001738 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001739}
1740
Jason Molendab57e4a12013-11-04 09:33:30 +00001741StackFrameSP
1742Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001743{
Jason Molendab57e4a12013-11-04 09:33:30 +00001744 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001745}
1746
1747void
Greg Clayton0603aa92010-10-04 01:05:56 +00001748Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749{
Greg Claytonc14ee322011-09-22 04:58:26 +00001750 exe_ctx.Clear();
1751 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752}
1753
1754PathMappingList &
1755Target::GetImageSearchPathList ()
1756{
1757 return m_image_search_paths;
1758}
1759
1760void
1761Target::ImageSearchPathsChanged
1762(
1763 const PathMappingList &path_list,
1764 void *baton
1765)
1766{
1767 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001768 ModuleSP exe_module_sp (target->GetExecutableModule());
1769 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001770 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001771}
1772
1773ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001774Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775{
Greg Clayton73da2442011-08-03 01:23:55 +00001776 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001777 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001778 {
Greg Clayton73da2442011-08-03 01:23:55 +00001779 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001780 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001781 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001782 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001783 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1784 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001785 return m_scratch_ast_context_ap.get();
1786}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001787
Sean Callanan686b2312011-11-16 18:20:47 +00001788ClangASTImporter *
1789Target::GetClangASTImporter()
1790{
1791 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1792
1793 if (!ast_importer)
1794 {
1795 ast_importer = new ClangASTImporter();
1796 m_ast_importer_ap.reset(ast_importer);
1797 }
1798
1799 return ast_importer;
1800}
1801
Greg Clayton99d0faf2010-11-18 23:32:35 +00001802void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001803Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001804{
Greg Clayton6920b522012-08-22 18:39:03 +00001805 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001806}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001807
Greg Clayton99d0faf2010-11-18 23:32:35 +00001808void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001809Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001810{
Greg Clayton6920b522012-08-22 18:39:03 +00001811 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001812}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001813
Greg Claytonc859e2d2012-02-13 23:10:39 +00001814FileSpecList
1815Target::GetDefaultExecutableSearchPaths ()
1816{
Greg Clayton67cc0632012-08-22 17:17:09 +00001817 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1818 if (properties_sp)
1819 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001820 return FileSpecList();
1821}
1822
Michael Sartaina7499c92013-07-01 19:45:50 +00001823FileSpecList
1824Target::GetDefaultDebugFileSearchPaths ()
1825{
1826 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1827 if (properties_sp)
1828 return properties_sp->GetDebugFileSearchPaths();
1829 return FileSpecList();
1830}
1831
Caroline Ticedaccaa92010-09-20 20:44:43 +00001832ArchSpec
1833Target::GetDefaultArchitecture ()
1834{
Greg Clayton67cc0632012-08-22 17:17:09 +00001835 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1836 if (properties_sp)
1837 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001838 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001839}
1840
1841void
Greg Clayton67cc0632012-08-22 17:17:09 +00001842Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001843{
Greg Clayton67cc0632012-08-22 17:17:09 +00001844 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1845 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001846 {
1847 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::SetDefaultArchitecture setting target's default architecture to %s (%s)", arch.GetArchitectureName(), arch.GetTriple().getTriple().c_str());
Greg Clayton67cc0632012-08-22 17:17:09 +00001848 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001849 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001850}
1851
Greg Clayton0603aa92010-10-04 01:05:56 +00001852Target *
1853Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1854{
1855 // The target can either exist in the "process" of ExecutionContext, or in
1856 // the "target_sp" member of SymbolContext. This accessor helper function
1857 // will get the target from one of these locations.
1858
1859 Target *target = NULL;
1860 if (sc_ptr != NULL)
1861 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001862 if (target == NULL && exe_ctx_ptr)
1863 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001864 return target;
1865}
1866
Jim Ingham1624a2d2014-05-05 02:26:40 +00001867ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001868Target::EvaluateExpression
1869(
1870 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001871 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001872 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001873 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001874)
1875{
Enrico Granata97fca502012-09-18 17:43:16 +00001876 result_valobj_sp.reset();
1877
Jim Ingham8646d3c2014-05-05 02:47:44 +00001878 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001879
Greg Claytond1767f02011-12-08 02:13:16 +00001880 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1881 return execution_results;
1882
Jim Ingham6026ca32011-05-12 02:06:14 +00001883 // We shouldn't run stop hooks in expressions.
1884 // Be sure to reset this if you return anywhere within this function.
1885 bool old_suppress_value = m_suppress_stop_hooks;
1886 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001887
1888 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001889
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001890 if (frame)
1891 {
1892 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001893 }
1894 else if (m_process_sp)
1895 {
1896 m_process_sp->CalculateExecutionContext(exe_ctx);
1897 }
1898 else
1899 {
1900 CalculateExecutionContext(exe_ctx);
1901 }
1902
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001903 // Make sure we aren't just trying to see the value of a persistent
1904 // variable (something like "$0")
1905 lldb::ClangExpressionVariableSP persistent_var_sp;
1906 // Only check for persistent variables the expression starts with a '$'
1907 if (expr_cstr[0] == '$')
1908 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1909
1910 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001911 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001912 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00001913 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001914 }
1915 else
1916 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001917 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00001918 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001919 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001920 options,
1921 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001922 prefix,
1923 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001924 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001925 }
Jim Ingham6026ca32011-05-12 02:06:14 +00001926
1927 m_suppress_stop_hooks = old_suppress_value;
1928
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001929 return execution_results;
1930}
1931
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001932lldb::addr_t
1933Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1934{
1935 addr_t code_addr = load_addr;
1936 switch (m_arch.GetMachine())
1937 {
1938 case llvm::Triple::arm:
1939 case llvm::Triple::thumb:
1940 switch (addr_class)
1941 {
1942 case eAddressClassData:
1943 case eAddressClassDebug:
1944 return LLDB_INVALID_ADDRESS;
1945
1946 case eAddressClassUnknown:
1947 case eAddressClassInvalid:
1948 case eAddressClassCode:
1949 case eAddressClassCodeAlternateISA:
1950 case eAddressClassRuntime:
1951 // Check if bit zero it no set?
1952 if ((code_addr & 1ull) == 0)
1953 {
1954 // Bit zero isn't set, check if the address is a multiple of 2?
1955 if (code_addr & 2ull)
1956 {
1957 // The address is a multiple of 2 so it must be thumb, set bit zero
1958 code_addr |= 1ull;
1959 }
1960 else if (addr_class == eAddressClassCodeAlternateISA)
1961 {
1962 // We checked the address and the address claims to be the alternate ISA
1963 // which means thumb, so set bit zero.
1964 code_addr |= 1ull;
1965 }
1966 }
1967 break;
1968 }
1969 break;
1970
1971 default:
1972 break;
1973 }
1974 return code_addr;
1975}
1976
1977lldb::addr_t
1978Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1979{
1980 addr_t opcode_addr = load_addr;
1981 switch (m_arch.GetMachine())
1982 {
1983 case llvm::Triple::arm:
1984 case llvm::Triple::thumb:
1985 switch (addr_class)
1986 {
1987 case eAddressClassData:
1988 case eAddressClassDebug:
1989 return LLDB_INVALID_ADDRESS;
1990
1991 case eAddressClassInvalid:
1992 case eAddressClassUnknown:
1993 case eAddressClassCode:
1994 case eAddressClassCodeAlternateISA:
1995 case eAddressClassRuntime:
1996 opcode_addr &= ~(1ull);
1997 break;
1998 }
1999 break;
2000
2001 default:
2002 break;
2003 }
2004 return opcode_addr;
2005}
2006
Greg Clayton9585fbf2013-03-19 00:20:55 +00002007SourceManager &
2008Target::GetSourceManager ()
2009{
2010 if (m_source_manager_ap.get() == NULL)
2011 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2012 return *m_source_manager_ap;
2013}
2014
2015
Greg Clayton44d93782014-01-27 23:43:24 +00002016Target::StopHookSP
2017Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002018{
2019 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002020 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2021 m_stop_hooks[new_uid] = stop_hook_sp;
2022 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002023}
2024
2025bool
2026Target::RemoveStopHookByID (lldb::user_id_t user_id)
2027{
2028 size_t num_removed;
2029 num_removed = m_stop_hooks.erase (user_id);
2030 if (num_removed == 0)
2031 return false;
2032 else
2033 return true;
2034}
2035
2036void
2037Target::RemoveAllStopHooks ()
2038{
2039 m_stop_hooks.clear();
2040}
2041
2042Target::StopHookSP
2043Target::GetStopHookByID (lldb::user_id_t user_id)
2044{
2045 StopHookSP found_hook;
2046
2047 StopHookCollection::iterator specified_hook_iter;
2048 specified_hook_iter = m_stop_hooks.find (user_id);
2049 if (specified_hook_iter != m_stop_hooks.end())
2050 found_hook = (*specified_hook_iter).second;
2051 return found_hook;
2052}
2053
2054bool
2055Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2056{
2057 StopHookCollection::iterator specified_hook_iter;
2058 specified_hook_iter = m_stop_hooks.find (user_id);
2059 if (specified_hook_iter == m_stop_hooks.end())
2060 return false;
2061
2062 (*specified_hook_iter).second->SetIsActive (active_state);
2063 return true;
2064}
2065
2066void
2067Target::SetAllStopHooksActiveState (bool active_state)
2068{
2069 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2070 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2071 {
2072 (*pos).second->SetIsActive (active_state);
2073 }
2074}
2075
2076void
2077Target::RunStopHooks ()
2078{
Jim Ingham6026ca32011-05-12 02:06:14 +00002079 if (m_suppress_stop_hooks)
2080 return;
2081
Jim Ingham9575d842011-03-11 03:53:59 +00002082 if (!m_process_sp)
2083 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002084
2085 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2086 // since in that case we do not want to run the stop-hooks
2087 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2088 return;
2089
Jim Ingham9575d842011-03-11 03:53:59 +00002090 if (m_stop_hooks.empty())
2091 return;
2092
2093 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2094
2095 // If there aren't any active stop hooks, don't bother either:
2096 bool any_active_hooks = false;
2097 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2098 {
2099 if ((*pos).second->IsActive())
2100 {
2101 any_active_hooks = true;
2102 break;
2103 }
2104 }
2105 if (!any_active_hooks)
2106 return;
2107
2108 CommandReturnObject result;
2109
2110 std::vector<ExecutionContext> exc_ctx_with_reasons;
2111 std::vector<SymbolContext> sym_ctx_with_reasons;
2112
2113 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2114 size_t num_threads = cur_threadlist.GetSize();
2115 for (size_t i = 0; i < num_threads; i++)
2116 {
2117 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2118 if (cur_thread_sp->ThreadStoppedForAReason())
2119 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002120 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002121 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2122 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2123 }
2124 }
2125
2126 // If no threads stopped for a reason, don't run the stop-hooks.
2127 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2128 if (num_exe_ctx == 0)
2129 return;
2130
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002131 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2132 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002133
2134 bool keep_going = true;
2135 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002136 bool print_hook_header;
2137 bool print_thread_header;
2138
2139 if (num_exe_ctx == 1)
2140 print_thread_header = false;
2141 else
2142 print_thread_header = true;
2143
2144 if (m_stop_hooks.size() == 1)
2145 print_hook_header = false;
2146 else
2147 print_hook_header = true;
2148
Jim Ingham9575d842011-03-11 03:53:59 +00002149 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2150 {
2151 // result.Clear();
2152 StopHookSP cur_hook_sp = (*pos).second;
2153 if (!cur_hook_sp->IsActive())
2154 continue;
2155
2156 bool any_thread_matched = false;
2157 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2158 {
2159 if ((cur_hook_sp->GetSpecifier () == NULL
2160 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2161 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002162 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002163 {
2164 if (!hooks_ran)
2165 {
Jim Ingham9575d842011-03-11 03:53:59 +00002166 hooks_ran = true;
2167 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002168 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002169 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002170 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2171 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2172 NULL);
2173 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002174 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002175 else
Daniel Malead01b2952012-11-29 21:49:15 +00002176 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002177 any_thread_matched = true;
2178 }
2179
Jim Ingham381e25b2011-03-22 01:47:27 +00002180 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002181 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002182
2183 CommandInterpreterRunOptions options;
2184 options.SetStopOnContinue (true);
2185 options.SetStopOnError (true);
2186 options.SetEchoCommands (false);
2187 options.SetPrintResults (true);
2188 options.SetAddToHistory (false);
2189
2190 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2191 &exc_ctx_with_reasons[i],
2192 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002193 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002194
2195 // If the command started the target going again, we should bag out of
2196 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002197 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2198 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002199 {
Daniel Malead01b2952012-11-29 21:49:15 +00002200 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002201 keep_going = false;
2202 }
2203 }
2204 }
2205 }
Jason Molenda879cf772011-09-23 00:42:55 +00002206
Caroline Tice969ed3d2011-05-02 20:41:46 +00002207 result.GetImmediateOutputStream()->Flush();
2208 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002209}
2210
Greg Claytonfbb76342013-11-20 21:07:01 +00002211const TargetPropertiesSP &
2212Target::GetGlobalProperties()
2213{
2214 static TargetPropertiesSP g_settings_sp;
2215 if (!g_settings_sp)
2216 {
2217 g_settings_sp.reset (new TargetProperties (NULL));
2218 }
2219 return g_settings_sp;
2220}
2221
2222Error
2223Target::Install (ProcessLaunchInfo *launch_info)
2224{
2225 Error error;
2226 PlatformSP platform_sp (GetPlatform());
2227 if (platform_sp)
2228 {
2229 if (platform_sp->IsRemote())
2230 {
2231 if (platform_sp->IsConnected())
2232 {
2233 // Install all files that have an install path, and always install the
2234 // main executable when connected to a remote platform
2235 const ModuleList& modules = GetImages();
2236 const size_t num_images = modules.GetSize();
2237 for (size_t idx = 0; idx < num_images; ++idx)
2238 {
2239 const bool is_main_executable = idx == 0;
2240 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2241 if (module_sp)
2242 {
2243 FileSpec local_file (module_sp->GetFileSpec());
2244 if (local_file)
2245 {
2246 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2247 if (!remote_file)
2248 {
2249 if (is_main_executable) // TODO: add setting for always installing main executable???
2250 {
2251 // Always install the main executable
2252 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
2253 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename();
2254 }
2255 }
2256 if (remote_file)
2257 {
2258 error = platform_sp->Install(local_file, remote_file);
2259 if (error.Success())
2260 {
2261 module_sp->SetPlatformFileSpec(remote_file);
2262 if (is_main_executable)
2263 {
2264 if (launch_info)
2265 launch_info->SetExecutableFile(remote_file, false);
2266 }
2267 }
2268 else
2269 break;
2270 }
2271 }
2272 }
2273 }
2274 }
2275 }
2276 }
2277 return error;
2278}
Greg Clayton7b242382011-07-08 00:48:09 +00002279
Greg Claytond5944cd2013-12-06 01:12:00 +00002280bool
2281Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2282{
2283 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2284}
2285
2286bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002287Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2288{
2289 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2290}
2291
2292bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002293Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2294{
2295 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2296 if (old_section_load_addr != new_section_load_addr)
2297 {
2298 uint32_t stop_id = 0;
2299 ProcessSP process_sp(GetProcessSP());
2300 if (process_sp)
2301 stop_id = process_sp->GetStopID();
2302 else
2303 stop_id = m_section_load_history.GetLastStopID();
2304 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2305 return true; // Return true if the section load address was changed...
2306 }
2307 return false; // Return false to indicate nothing changed
2308
2309}
2310
2311bool
2312Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2313{
2314 uint32_t stop_id = 0;
2315 ProcessSP process_sp(GetProcessSP());
2316 if (process_sp)
2317 stop_id = process_sp->GetStopID();
2318 else
2319 stop_id = m_section_load_history.GetLastStopID();
2320 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2321}
2322
2323bool
2324Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2325{
2326 uint32_t stop_id = 0;
2327 ProcessSP process_sp(GetProcessSP());
2328 if (process_sp)
2329 stop_id = process_sp->GetStopID();
2330 else
2331 stop_id = m_section_load_history.GetLastStopID();
2332 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2333}
2334
2335void
2336Target::ClearAllLoadedSections ()
2337{
2338 m_section_load_history.Clear();
2339}
2340
Greg Claytonb09c5382013-12-13 17:20:18 +00002341
2342Error
Greg Claytondc6224e2014-10-21 01:00:42 +00002343Target::Launch (Listener &listener, ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002344{
2345 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002346 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2347
2348 if (log)
2349 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2350
Greg Claytonb09c5382013-12-13 17:20:18 +00002351 StateType state = eStateInvalid;
2352
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002353 // Scope to temporarily get the process state in case someone has manually
2354 // remotely connected already to a process and we can skip the platform
2355 // launching.
2356 {
2357 ProcessSP process_sp (GetProcessSP());
2358
2359 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002360 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002361 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002362 if (log)
2363 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2364 }
2365 else
2366 {
2367 if (log)
2368 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2369 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002370 }
2371
Greg Claytonb09c5382013-12-13 17:20:18 +00002372 launch_info.GetFlags().Set (eLaunchFlagDebug);
2373
2374 // Get the value of synchronous execution here. If you wait till after you have started to
2375 // run, then you could have hit a breakpoint, whose command might switch the value, and
2376 // then you'll pick up that incorrect value.
2377 Debugger &debugger = GetDebugger();
2378 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2379
2380 PlatformSP platform_sp (GetPlatform());
2381
2382 // Finalize the file actions, and if none were given, default to opening
2383 // up a pseudo terminal
2384 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002385 if (log)
2386 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2387 __FUNCTION__,
2388 platform_sp ? "true" : "false",
2389 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2390 default_to_use_pty ? "true" : "false");
2391
Greg Claytonb09c5382013-12-13 17:20:18 +00002392 launch_info.FinalizeFileActions (this, default_to_use_pty);
2393
2394 if (state == eStateConnected)
2395 {
2396 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2397 {
2398 error.SetErrorString("can't launch in tty when launching through a remote connection");
2399 return error;
2400 }
2401 }
2402
2403 if (!launch_info.GetArchitecture().IsValid())
2404 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002405
2406 // If we're not already connected to the process, and if we have a platform that can launch a process for debugging, go ahead and do that here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002407 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2408 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002409 if (log)
2410 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2411
Greg Claytonb09c5382013-12-13 17:20:18 +00002412 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2413 debugger,
2414 this,
2415 listener,
2416 error);
2417 }
2418 else
2419 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002420 if (log)
2421 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2422
Greg Claytonb09c5382013-12-13 17:20:18 +00002423 if (state == eStateConnected)
2424 {
2425 assert(m_process_sp);
2426 }
2427 else
2428 {
Todd Fiala015d8182014-07-22 23:41:36 +00002429 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002430 const char *plugin_name = launch_info.GetProcessPluginName();
2431 CreateProcess (listener, plugin_name, NULL);
2432 }
Todd Fiala015d8182014-07-22 23:41:36 +00002433
2434 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002435 if (m_process_sp)
2436 error = m_process_sp->Launch (launch_info);
2437 }
2438
2439 if (!m_process_sp)
2440 {
2441 if (error.Success())
2442 error.SetErrorString("failed to launch or debug process");
2443 return error;
2444 }
2445
2446 if (error.Success())
2447 {
2448 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
2449 {
Greg Clayton44d93782014-01-27 23:43:24 +00002450 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00002451
Greg Claytondc6224e2014-10-21 01:00:42 +00002452 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002453
2454 if (state == eStateStopped)
2455 {
Greg Clayton44d93782014-01-27 23:43:24 +00002456 if (!synchronous_execution)
2457 m_process_sp->RestoreProcessEvents ();
2458
2459 error = m_process_sp->PrivateResume();
Todd Fialaa3b89e22014-08-12 14:33:19 +00002460
Greg Claytonb09c5382013-12-13 17:20:18 +00002461 if (error.Success())
2462 {
Todd Fialaa3b89e22014-08-12 14:33:19 +00002463 // there is a race condition where this thread will return up the call stack to the main command
2464 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
2465 // a chance to call PushProcessIOHandler()
2466 m_process_sp->SyncIOHandler(2000);
2467
Greg Claytonb09c5382013-12-13 17:20:18 +00002468 if (synchronous_execution)
2469 {
Greg Claytondc6224e2014-10-21 01:00:42 +00002470 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
Greg Claytonb09c5382013-12-13 17:20:18 +00002471 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2472 if (!StateIsStoppedState(state, must_be_alive))
2473 {
Greg Clayton44d93782014-01-27 23:43:24 +00002474 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
Greg Claytonb09c5382013-12-13 17:20:18 +00002475 }
2476 }
2477 }
2478 else
2479 {
Greg Clayton44d93782014-01-27 23:43:24 +00002480 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002481 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002482 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002483 }
2484 }
Greg Clayton40286e02014-04-30 20:29:09 +00002485 else if (state == eStateExited)
2486 {
Zachary Turner10687b02014-10-20 17:46:43 +00002487 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002488 const int exit_status = m_process_sp->GetExitStatus();
2489 const char *exit_desc = m_process_sp->GetExitDescription();
2490#define LAUNCH_SHELL_MESSAGE "\n'r' and 'run' are aliases that default to launching through a shell.\nTry launching without going through a shell by using 'process launch'."
2491 if (exit_desc && exit_desc[0])
2492 {
2493 if (with_shell)
2494 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2495 else
2496 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2497 }
2498 else
2499 {
2500 if (with_shell)
2501 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2502 else
2503 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2504 }
2505 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002506 else
2507 {
2508 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2509 }
2510 }
Greg Clayton44d93782014-01-27 23:43:24 +00002511 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002512 }
2513 else
2514 {
Greg Clayton44d93782014-01-27 23:43:24 +00002515 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002516 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002517 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002518 }
2519 return error;
2520}
Jim Ingham9575d842011-03-11 03:53:59 +00002521//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002522// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002523//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002524Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2525 UserID (uid),
2526 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002527 m_commands (),
2528 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002529 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002530 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002531{
2532}
2533
2534Target::StopHook::StopHook (const StopHook &rhs) :
2535 UserID (rhs.GetID()),
2536 m_target_sp (rhs.m_target_sp),
2537 m_commands (rhs.m_commands),
2538 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002539 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002540 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002541{
2542 if (rhs.m_thread_spec_ap.get() != NULL)
2543 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2544}
2545
2546
2547Target::StopHook::~StopHook ()
2548{
2549}
2550
2551void
2552Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2553{
2554 m_thread_spec_ap.reset (specifier);
2555}
2556
2557
2558void
2559Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2560{
2561 int indent_level = s->GetIndentLevel();
2562
2563 s->SetIndentLevel(indent_level + 2);
2564
Daniel Malead01b2952012-11-29 21:49:15 +00002565 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002566 if (m_active)
2567 s->Indent ("State: enabled\n");
2568 else
2569 s->Indent ("State: disabled\n");
2570
2571 if (m_specifier_sp)
2572 {
2573 s->Indent();
2574 s->PutCString ("Specifier:\n");
2575 s->SetIndentLevel (indent_level + 4);
2576 m_specifier_sp->GetDescription (s, level);
2577 s->SetIndentLevel (indent_level + 2);
2578 }
2579
2580 if (m_thread_spec_ap.get() != NULL)
2581 {
2582 StreamString tmp;
2583 s->Indent("Thread:\n");
2584 m_thread_spec_ap->GetDescription (&tmp, level);
2585 s->SetIndentLevel (indent_level + 4);
2586 s->Indent (tmp.GetData());
2587 s->PutCString ("\n");
2588 s->SetIndentLevel (indent_level + 2);
2589 }
2590
2591 s->Indent ("Commands: \n");
2592 s->SetIndentLevel (indent_level + 4);
2593 uint32_t num_commands = m_commands.GetSize();
2594 for (uint32_t i = 0; i < num_commands; i++)
2595 {
2596 s->Indent(m_commands.GetStringAtIndex(i));
2597 s->PutCString ("\n");
2598 }
2599 s->SetIndentLevel (indent_level);
2600}
2601
Greg Clayton67cc0632012-08-22 17:17:09 +00002602//--------------------------------------------------------------
2603// class TargetProperties
2604//--------------------------------------------------------------
2605
2606OptionEnumValueElement
2607lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002608{
Greg Clayton67cc0632012-08-22 17:17:09 +00002609 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2610 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2611 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2612 { 0, NULL, NULL }
2613};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002614
Greg Clayton1f746072012-08-29 21:13:06 +00002615static OptionEnumValueElement
2616g_inline_breakpoint_enums[] =
2617{
2618 { 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."},
2619 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2620 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2621 { 0, NULL, NULL }
2622};
2623
Jim Ingham0f063ba2013-03-02 00:26:47 +00002624typedef enum x86DisassemblyFlavor
2625{
2626 eX86DisFlavorDefault,
2627 eX86DisFlavorIntel,
2628 eX86DisFlavorATT
2629} x86DisassemblyFlavor;
2630
2631static OptionEnumValueElement
2632g_x86_dis_flavor_value_types[] =
2633{
2634 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2635 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2636 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2637 { 0, NULL, NULL }
2638};
2639
Enrico Granata397ddd52013-05-21 20:13:34 +00002640static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002641g_hex_immediate_style_values[] =
2642{
2643 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2644 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2645 { 0, NULL, NULL }
2646};
2647
2648static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002649g_load_script_from_sym_file_values[] =
2650{
2651 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2652 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2653 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2654 { 0, NULL, NULL }
2655};
2656
Greg Claytonfd814c52013-08-13 01:42:25 +00002657
2658static OptionEnumValueElement
2659g_memory_module_load_level_values[] =
2660{
Greg Clayton86eac942013-08-13 21:32:34 +00002661 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002662 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2663 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2664 { 0, NULL, NULL }
2665};
2666
Greg Clayton67cc0632012-08-22 17:17:09 +00002667static PropertyDefinition
2668g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002669{
Greg Clayton67cc0632012-08-22 17:17:09 +00002670 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2671 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2672 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2673 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2674 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2675 { "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 "
2676 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2677 "some part (starting at the root) of the path to the file when it was built, "
2678 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2679 "Each element of the array is checked in order and the first one that results in a match wins." },
2680 { "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." },
Michael Sartaina7499c92013-07-01 19:45:50 +00002681 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating debug symbol files." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002682 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2683 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
Enrico Granatad325bf92013-06-04 22:54:16 +00002684 { "max-memory-read-size" , OptionValue::eTypeSInt64 , false, 1024 , NULL, NULL, "Maximum number of bytes that 'memory read' will fetch before --force must be specified." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002685 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." },
Greg Clayton45392552012-10-17 22:57:12 +00002686 { "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." },
2687 { "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 Clayton67cc0632012-08-22 17:17:09 +00002688 { "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." },
2689 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2690 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2691 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2692 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard error." },
Jim Ingham106d0282014-06-25 02:32:56 +00002693 { "detach-on-error" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "debugserver will detach (rather than killing) a process if it loses connection with lldb." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002694 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2695 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Disable stdin/stdout for process (e.g. for a GUI application)" },
Todd Fialaad6eee62014-09-24 19:59:13 +00002696 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsAlways , NULL, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
Greg Clayton1f746072012-08-29 21:13:06 +00002697 "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. "
2698 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2699 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002700 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2701 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2702 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002703 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002704 // FIXME: This is the wrong way to do per-architecture settings, but we don't have a general per architecture settings system in place yet.
2705 { "x86-disassembly-flavor" , OptionValue::eTypeEnum , false, eX86DisFlavorDefault, NULL, g_x86_dis_flavor_value_types, "The default disassembly flavor to use for x86 or x86-64 targets." },
Daniel Malead79ae052013-08-07 21:54:09 +00002706 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2707 { "hex-immediate-style" , OptionValue::eTypeEnum , false, Disassembler::eHexStyleC, NULL, g_hex_immediate_style_values, "Which style to use for printing hexadecimal disassembly values." },
Jim Ingham34951272013-04-04 01:38:54 +00002708 { "use-fast-stepping" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Use a fast stepping algorithm based on running from branch to branch rather than instruction single-stepping." },
Enrico Granata397ddd52013-05-21 20:13:34 +00002709 { "load-script-from-symbol-file" , OptionValue::eTypeEnum , false, eLoadScriptFromSymFileWarn, NULL, g_load_script_from_sym_file_values, "Allow LLDB to load scripting resources embedded in symbol files when available." },
Greg Clayton86eac942013-08-13 21:32:34 +00002710 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2711 "Loading modules from memory can be slow as reading the symbol tables and other data can take a long time depending on your connection to the debug target. "
2712 "This setting helps users control how much information gets loaded when loading modules from memory."
2713 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2714 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2715 "'minimal' is the fastest setting and will load section data with no symbols, but should rarely be used as stack frames in these memory regions will be inaccurate and not provide any context (fastest). " },
Greg Claytonfb6621e2013-12-06 21:59:52 +00002716 { "display-expression-in-crashlogs" , OptionValue::eTypeBoolean , false, false, NULL, NULL, "Expressions that crash will show up in crash logs if the host system supports executable specific crash log strings and this setting is set to true." },
Jason Molendaa4bea722014-02-14 05:06:49 +00002717 { "trap-handler-names" , OptionValue::eTypeArray , true, OptionValue::eTypeString, NULL, NULL, "A list of trap handler function names, e.g. a common Unix user process one is _sigtramp." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002718 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2719};
2720enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002721{
Greg Clayton67cc0632012-08-22 17:17:09 +00002722 ePropertyDefaultArch,
2723 ePropertyExprPrefix,
2724 ePropertyPreferDynamic,
2725 ePropertyEnableSynthetic,
2726 ePropertySkipPrologue,
2727 ePropertySourceMap,
2728 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002729 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002730 ePropertyMaxChildrenCount,
2731 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002732 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002733 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002734 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002735 ePropertyRunArgs,
2736 ePropertyEnvVars,
2737 ePropertyInheritEnv,
2738 ePropertyInputPath,
2739 ePropertyOutputPath,
2740 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00002741 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00002742 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00002743 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002744 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00002745 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00002746 ePropertyUseHexImmediates,
2747 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002748 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00002749 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00002750 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00002751 ePropertyDisplayExpressionsInCrashlogs,
2752 ePropertyTrapHandlerNames
Greg Clayton67cc0632012-08-22 17:17:09 +00002753};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002754
Caroline Ticedaccaa92010-09-20 20:44:43 +00002755
Greg Clayton67cc0632012-08-22 17:17:09 +00002756class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002757{
Greg Clayton67cc0632012-08-22 17:17:09 +00002758public:
2759 TargetOptionValueProperties (const ConstString &name) :
2760 OptionValueProperties (name),
2761 m_target (NULL),
2762 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002763 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00002764 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002765
Greg Clayton67cc0632012-08-22 17:17:09 +00002766 // This constructor is used when creating TargetOptionValueProperties when it
2767 // is part of a new lldb_private::Target instance. It will copy all current
2768 // global property values as needed
2769 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2770 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2771 m_target (target),
2772 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002773 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002774 }
2775
2776 virtual const Property *
2777 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2778 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002779 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00002780 // try and grab the setting from the current target if there is one. Else we just
2781 // use the one from this instance.
2782 if (idx == ePropertyEnvVars)
2783 GetHostEnvironmentIfNeeded ();
2784
2785 if (exe_ctx)
2786 {
2787 Target *target = exe_ctx->GetTargetPtr();
2788 if (target)
2789 {
2790 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2791 if (this != target_properties)
2792 return target_properties->ProtectedGetPropertyAtIndex (idx);
2793 }
2794 }
2795 return ProtectedGetPropertyAtIndex (idx);
2796 }
Enrico Granata84a53df2013-05-20 22:29:23 +00002797
2798 lldb::TargetSP
2799 GetTargetSP ()
2800 {
2801 return m_target->shared_from_this();
2802 }
2803
Greg Clayton67cc0632012-08-22 17:17:09 +00002804protected:
2805
2806 void
2807 GetHostEnvironmentIfNeeded () const
2808 {
2809 if (!m_got_host_env)
2810 {
2811 if (m_target)
2812 {
2813 m_got_host_env = true;
2814 const uint32_t idx = ePropertyInheritEnv;
2815 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2816 {
2817 PlatformSP platform_sp (m_target->GetPlatform());
2818 if (platform_sp)
2819 {
2820 StringList env;
2821 if (platform_sp->GetEnvironment(env))
2822 {
2823 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2824 if (env_dict)
2825 {
2826 const bool can_replace = false;
2827 const size_t envc = env.GetSize();
2828 for (size_t idx=0; idx<envc; idx++)
2829 {
2830 const char *env_entry = env.GetStringAtIndex (idx);
2831 if (env_entry)
2832 {
2833 const char *equal_pos = ::strchr(env_entry, '=');
2834 ConstString key;
2835 // It is ok to have environment variables with no values
2836 const char *value = NULL;
2837 if (equal_pos)
2838 {
2839 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2840 if (equal_pos[1])
2841 value = equal_pos + 1;
2842 }
2843 else
2844 {
2845 key.SetCString(env_entry);
2846 }
2847 // Don't allow existing keys to be replaced with ones we get from the platform environment
2848 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2849 }
2850 }
2851 }
2852 }
2853 }
2854 }
2855 }
2856 }
2857 }
2858 Target *m_target;
2859 mutable bool m_got_host_env;
2860};
2861
Greg Claytonfbb76342013-11-20 21:07:01 +00002862//----------------------------------------------------------------------
2863// TargetProperties
2864//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00002865TargetProperties::TargetProperties (Target *target) :
2866 Properties ()
2867{
2868 if (target)
2869 {
2870 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Ticedaccaa92010-09-20 20:44:43 +00002871 }
2872 else
Greg Clayton67cc0632012-08-22 17:17:09 +00002873 {
2874 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2875 m_collection_sp->Initialize(g_properties);
2876 m_collection_sp->AppendProperty(ConstString("process"),
2877 ConstString("Settings specify to processes."),
2878 true,
2879 Process::GetGlobalProperties()->GetValueProperties());
2880 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002881}
2882
Greg Clayton67cc0632012-08-22 17:17:09 +00002883TargetProperties::~TargetProperties ()
2884{
2885}
2886ArchSpec
2887TargetProperties::GetDefaultArchitecture () const
2888{
2889 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2890 if (value)
2891 return value->GetCurrentValue();
2892 return ArchSpec();
2893}
2894
2895void
2896TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2897{
2898 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2899 if (value)
2900 return value->SetCurrentValue(arch, true);
2901}
2902
2903lldb::DynamicValueType
2904TargetProperties::GetPreferDynamicValue() const
2905{
2906 const uint32_t idx = ePropertyPreferDynamic;
2907 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2908}
2909
2910bool
2911TargetProperties::GetDisableASLR () const
2912{
2913 const uint32_t idx = ePropertyDisableASLR;
2914 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2915}
2916
2917void
2918TargetProperties::SetDisableASLR (bool b)
2919{
2920 const uint32_t idx = ePropertyDisableASLR;
2921 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2922}
2923
2924bool
Jim Ingham106d0282014-06-25 02:32:56 +00002925TargetProperties::GetDetachOnError () const
2926{
2927 const uint32_t idx = ePropertyDetachOnError;
2928 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2929}
2930
2931void
2932TargetProperties::SetDetachOnError (bool b)
2933{
2934 const uint32_t idx = ePropertyDetachOnError;
2935 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2936}
2937
2938bool
Greg Clayton67cc0632012-08-22 17:17:09 +00002939TargetProperties::GetDisableSTDIO () const
2940{
2941 const uint32_t idx = ePropertyDisableSTDIO;
2942 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2943}
2944
2945void
2946TargetProperties::SetDisableSTDIO (bool b)
2947{
2948 const uint32_t idx = ePropertyDisableSTDIO;
2949 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2950}
2951
Jim Ingham0f063ba2013-03-02 00:26:47 +00002952const char *
2953TargetProperties::GetDisassemblyFlavor () const
2954{
2955 const uint32_t idx = ePropertyDisassemblyFlavor;
2956 const char *return_value;
2957
2958 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2959 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
2960 return return_value;
2961}
2962
Greg Clayton1f746072012-08-29 21:13:06 +00002963InlineStrategy
2964TargetProperties::GetInlineStrategy () const
2965{
2966 const uint32_t idx = ePropertyInlineStrategy;
2967 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2968}
2969
Greg Clayton45392552012-10-17 22:57:12 +00002970const char *
2971TargetProperties::GetArg0 () const
2972{
2973 const uint32_t idx = ePropertyArg0;
2974 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2975}
2976
2977void
2978TargetProperties::SetArg0 (const char *arg)
2979{
2980 const uint32_t idx = ePropertyArg0;
2981 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2982}
2983
Greg Clayton67cc0632012-08-22 17:17:09 +00002984bool
2985TargetProperties::GetRunArguments (Args &args) const
2986{
2987 const uint32_t idx = ePropertyRunArgs;
2988 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2989}
2990
2991void
2992TargetProperties::SetRunArguments (const Args &args)
2993{
2994 const uint32_t idx = ePropertyRunArgs;
2995 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2996}
2997
2998size_t
2999TargetProperties::GetEnvironmentAsArgs (Args &env) const
3000{
3001 const uint32_t idx = ePropertyEnvVars;
3002 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3003}
3004
3005bool
3006TargetProperties::GetSkipPrologue() const
3007{
3008 const uint32_t idx = ePropertySkipPrologue;
3009 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3010}
3011
3012PathMappingList &
3013TargetProperties::GetSourcePathMap () const
3014{
3015 const uint32_t idx = ePropertySourceMap;
3016 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3017 assert(option_value);
3018 return option_value->GetCurrentValue();
3019}
3020
3021FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003022TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003023{
3024 const uint32_t idx = ePropertyExecutableSearchPaths;
3025 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3026 assert(option_value);
3027 return option_value->GetCurrentValue();
3028}
3029
Michael Sartaina7499c92013-07-01 19:45:50 +00003030FileSpecList &
3031TargetProperties::GetDebugFileSearchPaths ()
3032{
3033 const uint32_t idx = ePropertyDebugFileSearchPaths;
3034 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3035 assert(option_value);
3036 return option_value->GetCurrentValue();
3037}
3038
Greg Clayton67cc0632012-08-22 17:17:09 +00003039bool
3040TargetProperties::GetEnableSyntheticValue () const
3041{
3042 const uint32_t idx = ePropertyEnableSynthetic;
3043 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3044}
3045
3046uint32_t
3047TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3048{
3049 const uint32_t idx = ePropertyMaxChildrenCount;
3050 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3051}
3052
3053uint32_t
3054TargetProperties::GetMaximumSizeOfStringSummary() const
3055{
3056 const uint32_t idx = ePropertyMaxSummaryLength;
3057 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3058}
3059
Enrico Granatad325bf92013-06-04 22:54:16 +00003060uint32_t
3061TargetProperties::GetMaximumMemReadSize () const
3062{
3063 const uint32_t idx = ePropertyMaxMemReadSize;
3064 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3065}
3066
Greg Clayton67cc0632012-08-22 17:17:09 +00003067FileSpec
3068TargetProperties::GetStandardInputPath () const
3069{
3070 const uint32_t idx = ePropertyInputPath;
3071 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3072}
3073
3074void
3075TargetProperties::SetStandardInputPath (const char *p)
3076{
3077 const uint32_t idx = ePropertyInputPath;
3078 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3079}
3080
3081FileSpec
3082TargetProperties::GetStandardOutputPath () const
3083{
3084 const uint32_t idx = ePropertyOutputPath;
3085 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3086}
3087
3088void
3089TargetProperties::SetStandardOutputPath (const char *p)
3090{
3091 const uint32_t idx = ePropertyOutputPath;
3092 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3093}
3094
3095FileSpec
3096TargetProperties::GetStandardErrorPath () const
3097{
3098 const uint32_t idx = ePropertyErrorPath;
3099 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3100}
3101
Greg Clayton6920b522012-08-22 18:39:03 +00003102const char *
3103TargetProperties::GetExpressionPrefixContentsAsCString ()
3104{
3105 const uint32_t idx = ePropertyExprPrefix;
3106 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3107 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003108 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003109 const bool null_terminate = true;
3110 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003111 if (data_sp)
3112 return (const char *) data_sp->GetBytes();
3113 }
Greg Clayton6920b522012-08-22 18:39:03 +00003114 return NULL;
3115}
3116
Greg Clayton67cc0632012-08-22 17:17:09 +00003117void
3118TargetProperties::SetStandardErrorPath (const char *p)
3119{
3120 const uint32_t idx = ePropertyErrorPath;
3121 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3122}
3123
3124bool
3125TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3126{
3127 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3128 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3129}
3130
Jim Ingham17d023f2013-03-13 17:58:04 +00003131bool
Daniel Malead79ae052013-08-07 21:54:09 +00003132TargetProperties::GetUseHexImmediates () const
3133{
3134 const uint32_t idx = ePropertyUseHexImmediates;
3135 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3136}
3137
3138bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003139TargetProperties::GetUseFastStepping () const
3140{
3141 const uint32_t idx = ePropertyUseFastStepping;
3142 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3143}
3144
Greg Claytonfb6621e2013-12-06 21:59:52 +00003145bool
3146TargetProperties::GetDisplayExpressionsInCrashlogs () const
3147{
3148 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3149 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3150}
3151
Enrico Granata397ddd52013-05-21 20:13:34 +00003152LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003153TargetProperties::GetLoadScriptFromSymbolFile () const
3154{
3155 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003156 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003157}
3158
Daniel Malead79ae052013-08-07 21:54:09 +00003159Disassembler::HexImmediateStyle
3160TargetProperties::GetHexImmediateStyle () const
3161{
3162 const uint32_t idx = ePropertyHexImmediateStyle;
3163 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3164}
3165
Greg Claytonfd814c52013-08-13 01:42:25 +00003166MemoryModuleLoadLevel
3167TargetProperties::GetMemoryModuleLoadLevel() const
3168{
3169 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3170 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3171}
3172
Jason Molendaa4bea722014-02-14 05:06:49 +00003173bool
3174TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3175{
3176 const uint32_t idx = ePropertyTrapHandlerNames;
3177 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3178}
Greg Claytonfd814c52013-08-13 01:42:25 +00003179
Jason Molendaa4bea722014-02-14 05:06:49 +00003180void
3181TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3182{
3183 const uint32_t idx = ePropertyTrapHandlerNames;
3184 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3185}
Greg Clayton67cc0632012-08-22 17:17:09 +00003186
Greg Claytonfbb76342013-11-20 21:07:01 +00003187//----------------------------------------------------------------------
3188// Target::TargetEventData
3189//----------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +00003190const ConstString &
3191Target::TargetEventData::GetFlavorString ()
3192{
3193 static ConstString g_flavor ("Target::TargetEventData");
3194 return g_flavor;
3195}
3196
3197const ConstString &
3198Target::TargetEventData::GetFlavor () const
3199{
3200 return TargetEventData::GetFlavorString ();
3201}
3202
3203Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
3204 EventData(),
3205 m_target_sp (new_target_sp)
3206{
3207}
3208
3209Target::TargetEventData::~TargetEventData()
3210{
3211
3212}
3213
3214void
3215Target::TargetEventData::Dump (Stream *s) const
3216{
3217
3218}
3219
3220const TargetSP
3221Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
3222{
3223 TargetSP target_sp;
3224
3225 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
3226 if (data)
3227 target_sp = data->m_target_sp;
3228
3229 return target_sp;
3230}
3231
3232const Target::TargetEventData *
3233Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3234{
3235 if (event_ptr)
3236 {
3237 const EventData *event_data = event_ptr->GetData();
3238 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3239 return static_cast <const TargetEventData *> (event_ptr->GetData());
3240 }
3241 return NULL;
3242}
3243