blob: d30f91d156654adb5f9430711fc171b14ae369f6 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000039#include "lldb/Interpreter/CommandInterpreter.h"
40#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000041#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000042#include "lldb/Interpreter/OptionValues.h"
43#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "lldb/lldb-private-log.h"
45#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000047#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000048#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000049#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000050#include "lldb/Target/Thread.h"
51#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
53using namespace lldb;
54using namespace lldb_private;
55
Jim Ingham4bddaeb2012-02-16 06:50:00 +000056ConstString &
57Target::GetStaticBroadcasterClass ()
58{
59 static ConstString class_name ("lldb.target");
60 return class_name;
61}
62
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063//----------------------------------------------------------------------
64// Target constructor
65//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:51 +000066Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Greg Clayton67cc0632012-08-22 17:17:09 +000067 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000068 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000069 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000070 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000071 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000072 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000073 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000074 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000075 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076 m_breakpoint_list (false),
77 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000078 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000079 m_process_sp (),
80 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000082 m_scratch_ast_context_ap (),
83 m_scratch_ast_source_ap (),
84 m_ast_importer_ap (),
Jim Ingham9575d842011-03-11 03:53:59 +000085 m_persistent_variables (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000086 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000087 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000088 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000089 m_valid (true),
Enrico Granata5d5f60c2013-09-24 22:58:37 +000090 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000092 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
93 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
94 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +000095 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +000096 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000097
Jim Ingham4bddaeb2012-02-16 06:50:00 +000098 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +000099
Greg Clayton5160ce52013-03-27 23:08:40 +0000100 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000102 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000103 if (m_arch.IsValid())
104 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000105 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 +0000106 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107}
108
109//----------------------------------------------------------------------
110// Destructor
111//----------------------------------------------------------------------
112Target::~Target()
113{
Greg Clayton5160ce52013-03-27 23:08:40 +0000114 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000116 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117 DeleteCurrentProcess ();
118}
119
120void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000121Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122{
Greg Clayton89411422010-10-08 00:21:05 +0000123// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000124 if (description_level != lldb::eDescriptionLevelBrief)
125 {
126 s->Indent();
127 s->PutCString("Target\n");
128 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000129 m_images.Dump(s);
130 m_breakpoint_list.Dump(s);
131 m_internal_breakpoint_list.Dump(s);
132 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000133 }
134 else
135 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000136 Module *exe_module = GetExecutableModulePointer();
137 if (exe_module)
138 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000139 else
140 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000141 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142}
143
144void
Greg Clayton90ba8112012-12-05 00:16:59 +0000145Target::CleanupProcess ()
146{
147 // Do any cleanup of the target we need to do between process instances.
148 // NB It is better to do this before destroying the process in case the
149 // clean up needs some help from the process.
150 m_breakpoint_list.ClearAllBreakpointSites();
151 m_internal_breakpoint_list.ClearAllBreakpointSites();
152 // Disable watchpoints just on the debugger side.
153 Mutex::Locker locker;
154 this->GetWatchpointList().GetListMutex(locker);
155 DisableAllWatchpoints(false);
156 ClearAllWatchpointHitCounts();
157}
158
159void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160Target::DeleteCurrentProcess ()
161{
162 if (m_process_sp.get())
163 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000164 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 if (m_process_sp->IsAlive())
166 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000167
168 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169
Greg Clayton90ba8112012-12-05 00:16:59 +0000170 CleanupProcess ();
171
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172 m_process_sp.reset();
173 }
174}
175
176const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000177Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178{
179 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000180 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 return m_process_sp;
182}
183
184const lldb::ProcessSP &
185Target::GetProcessSP () const
186{
187 return m_process_sp;
188}
189
Greg Clayton3418c852011-08-10 02:10:13 +0000190void
191Target::Destroy()
192{
193 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000194 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000195 DeleteCurrentProcess ();
196 m_platform_sp.reset();
197 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000198 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000199 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000200 const bool notify = false;
201 m_breakpoint_list.RemoveAll(notify);
202 m_internal_breakpoint_list.RemoveAll(notify);
203 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000204 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000205 m_search_filter_sp.reset();
206 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000207 m_persistent_variables.Clear();
208 m_stop_hooks.clear();
209 m_stop_hook_next_id = 0;
210 m_suppress_stop_hooks = false;
211}
212
213
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214BreakpointList &
215Target::GetBreakpointList(bool internal)
216{
217 if (internal)
218 return m_internal_breakpoint_list;
219 else
220 return m_breakpoint_list;
221}
222
223const BreakpointList &
224Target::GetBreakpointList(bool internal) const
225{
226 if (internal)
227 return m_internal_breakpoint_list;
228 else
229 return m_breakpoint_list;
230}
231
232BreakpointSP
233Target::GetBreakpointByID (break_id_t break_id)
234{
235 BreakpointSP bp_sp;
236
237 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
238 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
239 else
240 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
241
242 return bp_sp;
243}
244
245BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000246Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000247 const FileSpecList *source_file_spec_list,
248 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000249 bool internal,
250 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251{
Jim Ingham87df91b2011-09-23 00:54:11 +0000252 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
253 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000254 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000255}
256
257
258BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000259Target::CreateBreakpoint (const FileSpecList *containingModules,
260 const FileSpec &file,
261 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000262 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000263 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000264 bool internal,
265 bool hardware)
Jim Ingham969795f2011-09-21 01:17:13 +0000266{
Greg Clayton1f746072012-08-29 21:13:06 +0000267 if (check_inlines == eLazyBoolCalculate)
268 {
269 const InlineStrategy inline_strategy = GetInlineStrategy();
270 switch (inline_strategy)
271 {
272 case eInlineBreakpointsNever:
273 check_inlines = eLazyBoolNo;
274 break;
275
276 case eInlineBreakpointsHeaders:
277 if (file.IsSourceImplementationFile())
278 check_inlines = eLazyBoolNo;
279 else
280 check_inlines = eLazyBoolYes;
281 break;
282
283 case eInlineBreakpointsAlways:
284 check_inlines = eLazyBoolYes;
285 break;
286 }
287 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000288 SearchFilterSP filter_sp;
289 if (check_inlines == eLazyBoolNo)
290 {
291 // Not checking for inlines, we are looking only for matching compile units
292 FileSpecList compile_unit_list;
293 compile_unit_list.Append (file);
294 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
295 }
296 else
297 {
298 filter_sp = GetSearchFilterForModuleList (containingModules);
299 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000300 if (skip_prologue == eLazyBoolCalculate)
301 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
302
Greg Clayton1f746072012-08-29 21:13:06 +0000303 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
304 file,
305 line_no,
306 check_inlines,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000307 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000308 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309}
310
311
312BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000313Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 Address so_addr;
316 // Attempt to resolve our load address if possible, though it is ok if
317 // it doesn't resolve to section/offset.
318
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000319 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000320 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000321 if (!so_addr.IsValid())
322 {
323 // The address didn't resolve, so just set this as an absolute address
324 so_addr.SetOffset (addr);
325 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000326 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 return bp_sp;
328}
329
330BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000331Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000333 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000335 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336}
337
338BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000339Target::CreateBreakpoint (const FileSpecList *containingModules,
340 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000341 const char *func_name,
342 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000343 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000344 bool internal,
345 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000347 BreakpointSP bp_sp;
348 if (func_name)
349 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000350 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000351
352 if (skip_prologue == eLazyBoolCalculate)
353 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
354
Greg Claytond16e1e52011-07-12 17:06:17 +0000355 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
356 func_name,
357 func_name_type_mask,
358 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000359 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000360 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000361 }
362 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363}
364
Jim Inghamfab10e82012-03-06 00:37:27 +0000365lldb::BreakpointSP
366Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000367 const FileSpecList *containingSourceFiles,
368 const std::vector<std::string> &func_names,
369 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000370 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000371 bool internal,
372 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000373{
374 BreakpointSP bp_sp;
375 size_t num_names = func_names.size();
376 if (num_names > 0)
377 {
378 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000379
380 if (skip_prologue == eLazyBoolCalculate)
381 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
382
383 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000384 func_names,
385 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000386 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000387 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000388 }
389 return bp_sp;
390}
391
Jim Ingham133e0fb2012-03-03 02:05:11 +0000392BreakpointSP
393Target::CreateBreakpoint (const FileSpecList *containingModules,
394 const FileSpecList *containingSourceFiles,
395 const char *func_names[],
396 size_t num_names,
397 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000398 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000399 bool internal,
400 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000401{
402 BreakpointSP bp_sp;
403 if (num_names > 0)
404 {
405 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
406
Greg Clayton03da4cc2013-04-19 21:31:16 +0000407 if (skip_prologue == eLazyBoolCalculate)
408 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
409
410 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000411 func_names,
412 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000413 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000414 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000415 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000416 }
417 return bp_sp;
418}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419
420SearchFilterSP
421Target::GetSearchFilterForModule (const FileSpec *containingModule)
422{
423 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 if (containingModule != NULL)
425 {
426 // TODO: We should look into sharing module based search filters
427 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000428 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 }
430 else
431 {
432 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000433 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 filter_sp = m_search_filter_sp;
435 }
436 return filter_sp;
437}
438
Jim Ingham969795f2011-09-21 01:17:13 +0000439SearchFilterSP
440Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
441{
442 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000443 if (containingModules && containingModules->GetSize() != 0)
444 {
445 // TODO: We should look into sharing module based search filters
446 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000447 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000448 }
449 else
450 {
451 if (m_search_filter_sp.get() == NULL)
Greg Claytone1cd1be2012-01-29 20:56:30 +0000452 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000453 filter_sp = m_search_filter_sp;
454 }
455 return filter_sp;
456}
457
Jim Ingham87df91b2011-09-23 00:54:11 +0000458SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000459Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
460 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000461{
462 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
463 return GetSearchFilterForModuleList(containingModules);
464
465 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000466 if (containingModules == NULL)
467 {
468 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
469 // but that will take a little reworking.
470
Greg Claytone1cd1be2012-01-29 20:56:30 +0000471 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000472 }
473 else
474 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000475 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000476 }
477 return filter_sp;
478}
479
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000481Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000482 const FileSpecList *containingSourceFiles,
483 RegularExpression &func_regex,
484 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000485 bool internal,
486 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487{
Jim Ingham87df91b2011-09-23 00:54:11 +0000488 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000489 bool skip =
490 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
491 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000492 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
493 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000494 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495
Jim Ingham1460e4b2014-01-10 23:46:59 +0000496 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497}
498
Jim Ingham219ba192012-03-05 04:47:34 +0000499lldb::BreakpointSP
500Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
501{
502 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
503}
504
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000506Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507{
508 BreakpointSP bp_sp;
509 if (filter_sp && resolver_sp)
510 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000511 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 resolver_sp->SetBreakpoint (bp_sp.get());
513
514 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000515 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516 else
Greg Clayton9fed0d82010-07-23 23:33:17 +0000517 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518
Greg Clayton5160ce52013-03-27 23:08:40 +0000519 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520 if (log)
521 {
522 StreamString s;
523 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
524 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
525 }
526
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527 bp_sp->ResolveBreakpoint();
528 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000529
530 if (!internal && bp_sp)
531 {
532 m_last_created_breakpoint = bp_sp;
533 }
534
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 return bp_sp;
536}
537
Johnny Chen86364b42011-09-20 23:28:55 +0000538bool
539Target::ProcessIsValid()
540{
541 return (m_process_sp && m_process_sp->IsAlive());
542}
543
Johnny Chenb90827e2012-06-04 23:19:54 +0000544static bool
545CheckIfWatchpointsExhausted(Target *target, Error &error)
546{
547 uint32_t num_supported_hardware_watchpoints;
548 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
549 if (rc.Success())
550 {
551 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
552 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
553 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
554 num_supported_hardware_watchpoints);
555 }
556 return false;
557}
558
Johnny Chen01a67862011-10-14 00:42:25 +0000559// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000560// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000561WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000562Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000563{
Greg Clayton5160ce52013-03-27 23:08:40 +0000564 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000565 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000566 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000567 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000568
Johnny Chen01a67862011-10-14 00:42:25 +0000569 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000570 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000571 {
572 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000573 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000574 }
Jim Inghamc6462312013-06-18 21:52:48 +0000575
Johnny Chen45e541f2011-09-14 22:20:15 +0000576 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000577 {
578 if (size == 0)
579 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
580 else
Daniel Malead01b2952012-11-29 21:49:15 +0000581 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000582 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000583 }
Jim Inghamc6462312013-06-18 21:52:48 +0000584
585 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
586 {
587 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
588 }
Johnny Chen7313a642011-09-13 01:15:36 +0000589
Johnny Chen01a67862011-10-14 00:42:25 +0000590 // Currently we only support one watchpoint per address, with total number
591 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000592
593 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000594 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 +0000595 Mutex::Locker locker;
596 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000597 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000598 if (matched_sp)
599 {
Johnny Chen0c406372011-09-14 20:23:45 +0000600 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000601 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000602 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
603 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000604 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000605 if (size == old_size && kind == old_type)
606 {
Johnny Chen01a67862011-10-14 00:42:25 +0000607 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000608 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000609 }
610 else
611 {
Johnny Chen01a67862011-10-14 00:42:25 +0000612 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000613 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
614 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000615 }
Johnny Chen3c532582011-09-13 23:29:31 +0000616 }
617
Jason Molenda727e3922012-12-05 23:07:34 +0000618 if (!wp_sp)
619 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000620 wp_sp.reset(new Watchpoint(*this, addr, size, type));
621 wp_sp->SetWatchpointType(kind, notify);
622 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000623 }
Johnny Chen0c406372011-09-14 20:23:45 +0000624
Jim Ingham1b5792e2012-12-18 02:03:49 +0000625 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000626 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000627 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
628 __FUNCTION__,
629 error.Success() ? "succeeded" : "failed",
630 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000631
Jason Molenda727e3922012-12-05 23:07:34 +0000632 if (error.Fail())
633 {
Johnny Chen41b77262012-03-26 22:00:10 +0000634 // Enabling the watchpoint on the device side failed.
635 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000636 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000637 // See if we could provide more helpful error message.
638 if (!CheckIfWatchpointsExhausted(this, error))
639 {
640 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000641 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000642 }
Johnny Chen01a67862011-10-14 00:42:25 +0000643 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000644 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000645 else
Johnny Chen01a67862011-10-14 00:42:25 +0000646 m_last_created_watchpoint = wp_sp;
647 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000648}
649
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650void
651Target::RemoveAllBreakpoints (bool internal_also)
652{
Greg Clayton5160ce52013-03-27 23:08:40 +0000653 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 if (log)
655 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
656
Greg Clayton9fed0d82010-07-23 23:33:17 +0000657 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000659 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000660
661 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662}
663
664void
665Target::DisableAllBreakpoints (bool internal_also)
666{
Greg Clayton5160ce52013-03-27 23:08:40 +0000667 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 if (log)
669 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
670
671 m_breakpoint_list.SetEnabledAll (false);
672 if (internal_also)
673 m_internal_breakpoint_list.SetEnabledAll (false);
674}
675
676void
677Target::EnableAllBreakpoints (bool internal_also)
678{
Greg Clayton5160ce52013-03-27 23:08:40 +0000679 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 if (log)
681 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
682
683 m_breakpoint_list.SetEnabledAll (true);
684 if (internal_also)
685 m_internal_breakpoint_list.SetEnabledAll (true);
686}
687
688bool
689Target::RemoveBreakpointByID (break_id_t break_id)
690{
Greg Clayton5160ce52013-03-27 23:08:40 +0000691 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 if (log)
693 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
694
695 if (DisableBreakpointByID (break_id))
696 {
697 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000698 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000700 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000701 if (m_last_created_breakpoint)
702 {
703 if (m_last_created_breakpoint->GetID() == break_id)
704 m_last_created_breakpoint.reset();
705 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000706 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000707 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 return true;
709 }
710 return false;
711}
712
713bool
714Target::DisableBreakpointByID (break_id_t break_id)
715{
Greg Clayton5160ce52013-03-27 23:08:40 +0000716 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 if (log)
718 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
719
720 BreakpointSP bp_sp;
721
722 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
723 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
724 else
725 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
726 if (bp_sp)
727 {
728 bp_sp->SetEnabled (false);
729 return true;
730 }
731 return false;
732}
733
734bool
735Target::EnableBreakpointByID (break_id_t break_id)
736{
Greg Clayton5160ce52013-03-27 23:08:40 +0000737 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738 if (log)
739 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
740 __FUNCTION__,
741 break_id,
742 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
743
744 BreakpointSP bp_sp;
745
746 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
747 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
748 else
749 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
750
751 if (bp_sp)
752 {
753 bp_sp->SetEnabled (true);
754 return true;
755 }
756 return false;
757}
758
Johnny Chenedf50372011-09-23 21:21:43 +0000759// The flag 'end_to_end', default to true, signifies that the operation is
760// performed end to end, for both the debugger and the debuggee.
761
Johnny Chen01a67862011-10-14 00:42:25 +0000762// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
763// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000764bool
Johnny Chen01a67862011-10-14 00:42:25 +0000765Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000766{
Greg Clayton5160ce52013-03-27 23:08:40 +0000767 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000768 if (log)
769 log->Printf ("Target::%s\n", __FUNCTION__);
770
Johnny Chenedf50372011-09-23 21:21:43 +0000771 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000772 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000773 return true;
774 }
775
776 // Otherwise, it's an end to end operation.
777
Johnny Chen86364b42011-09-20 23:28:55 +0000778 if (!ProcessIsValid())
779 return false;
780
Johnny Chen01a67862011-10-14 00:42:25 +0000781 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000782 for (size_t i = 0; i < num_watchpoints; ++i)
783 {
Johnny Chen01a67862011-10-14 00:42:25 +0000784 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
785 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000786 return false;
787
Johnny Chen01a67862011-10-14 00:42:25 +0000788 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000789 if (rc.Fail())
790 return false;
791 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000792 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000793 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000794 return true; // Success!
795}
796
Johnny Chen01a67862011-10-14 00:42:25 +0000797// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
798// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000799bool
Johnny Chen01a67862011-10-14 00:42:25 +0000800Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000801{
Greg Clayton5160ce52013-03-27 23:08:40 +0000802 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000803 if (log)
804 log->Printf ("Target::%s\n", __FUNCTION__);
805
Johnny Chenedf50372011-09-23 21:21:43 +0000806 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000807 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000808 return true;
809 }
810
811 // Otherwise, it's an end to end operation.
812
Johnny Chen86364b42011-09-20 23:28:55 +0000813 if (!ProcessIsValid())
814 return false;
815
Johnny Chen01a67862011-10-14 00:42:25 +0000816 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000817 for (size_t i = 0; i < num_watchpoints; ++i)
818 {
Johnny Chen01a67862011-10-14 00:42:25 +0000819 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
820 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000821 return false;
822
Johnny Chen01a67862011-10-14 00:42:25 +0000823 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000824 if (rc.Fail())
825 return false;
826 }
Johnny Chen86364b42011-09-20 23:28:55 +0000827 return true; // Success!
828}
829
Johnny Chen01a67862011-10-14 00:42:25 +0000830// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
831// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000832bool
Johnny Chen01a67862011-10-14 00:42:25 +0000833Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000834{
Greg Clayton5160ce52013-03-27 23:08:40 +0000835 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000836 if (log)
837 log->Printf ("Target::%s\n", __FUNCTION__);
838
Johnny Chenedf50372011-09-23 21:21:43 +0000839 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000840 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000841 return true;
842 }
843
844 // Otherwise, it's an end to end operation.
845
Johnny Chen86364b42011-09-20 23:28:55 +0000846 if (!ProcessIsValid())
847 return false;
848
Johnny Chen01a67862011-10-14 00:42:25 +0000849 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000850 for (size_t i = 0; i < num_watchpoints; ++i)
851 {
Johnny Chen01a67862011-10-14 00:42:25 +0000852 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
853 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000854 return false;
855
Johnny Chen01a67862011-10-14 00:42:25 +0000856 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000857 if (rc.Fail())
858 return false;
859 }
Johnny Chen86364b42011-09-20 23:28:55 +0000860 return true; // Success!
861}
862
Johnny Chena4d6bc92012-02-25 06:44:30 +0000863// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
864bool
865Target::ClearAllWatchpointHitCounts ()
866{
Greg Clayton5160ce52013-03-27 23:08:40 +0000867 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000868 if (log)
869 log->Printf ("Target::%s\n", __FUNCTION__);
870
871 size_t num_watchpoints = m_watchpoint_list.GetSize();
872 for (size_t i = 0; i < num_watchpoints; ++i)
873 {
874 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
875 if (!wp_sp)
876 return false;
877
878 wp_sp->ResetHitCount();
879 }
880 return true; // Success!
881}
882
Johnny Chen01a67862011-10-14 00:42:25 +0000883// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000884// during these operations.
885bool
Johnny Chen01a67862011-10-14 00:42:25 +0000886Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000887{
Greg Clayton5160ce52013-03-27 23:08:40 +0000888 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000889 if (log)
890 log->Printf ("Target::%s\n", __FUNCTION__);
891
892 if (!ProcessIsValid())
893 return false;
894
Johnny Chen01a67862011-10-14 00:42:25 +0000895 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000896 for (size_t i = 0; i < num_watchpoints; ++i)
897 {
Johnny Chen01a67862011-10-14 00:42:25 +0000898 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
899 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000900 return false;
901
Johnny Chen01a67862011-10-14 00:42:25 +0000902 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000903 }
904 return true; // Success!
905}
906
Johnny Chen01a67862011-10-14 00:42:25 +0000907// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000908bool
Johnny Chen01a67862011-10-14 00:42:25 +0000909Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000910{
Greg Clayton5160ce52013-03-27 23:08:40 +0000911 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000912 if (log)
913 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
914
915 if (!ProcessIsValid())
916 return false;
917
Johnny Chen01a67862011-10-14 00:42:25 +0000918 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
919 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000920 {
Johnny Chen01a67862011-10-14 00:42:25 +0000921 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000922 if (rc.Success())
923 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000924
Johnny Chenf04ee932011-09-22 18:04:58 +0000925 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000926 }
927 return false;
928}
929
Johnny Chen01a67862011-10-14 00:42:25 +0000930// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000931bool
Johnny Chen01a67862011-10-14 00:42:25 +0000932Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000933{
Greg Clayton5160ce52013-03-27 23:08:40 +0000934 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000935 if (log)
936 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
937
938 if (!ProcessIsValid())
939 return false;
940
Johnny Chen01a67862011-10-14 00:42:25 +0000941 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
942 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000943 {
Johnny Chen01a67862011-10-14 00:42:25 +0000944 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000945 if (rc.Success())
946 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000947
Johnny Chenf04ee932011-09-22 18:04:58 +0000948 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000949 }
950 return false;
951}
952
Johnny Chen01a67862011-10-14 00:42:25 +0000953// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000954bool
Johnny Chen01a67862011-10-14 00:42:25 +0000955Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000956{
Greg Clayton5160ce52013-03-27 23:08:40 +0000957 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000958 if (log)
959 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
960
Jim Inghamb0b45132013-07-02 02:09:46 +0000961 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
962 if (watch_to_remove_sp == m_last_created_watchpoint)
963 m_last_created_watchpoint.reset();
964
Johnny Chen01a67862011-10-14 00:42:25 +0000965 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +0000966 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000967 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +0000968 return true;
969 }
970 return false;
971}
972
Johnny Chen01a67862011-10-14 00:42:25 +0000973// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +0000974bool
Johnny Chen01a67862011-10-14 00:42:25 +0000975Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000976{
Greg Clayton5160ce52013-03-27 23:08:40 +0000977 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000978 if (log)
979 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
980
981 if (!ProcessIsValid())
982 return false;
983
Johnny Chen01a67862011-10-14 00:42:25 +0000984 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
985 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000986 {
Johnny Chen01a67862011-10-14 00:42:25 +0000987 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000988 return true;
989 }
990 return false;
991}
992
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993ModuleSP
994Target::GetExecutableModule ()
995{
Greg Claytonaa149cb2011-08-11 02:48:45 +0000996 return m_images.GetModuleAtIndex(0);
997}
998
999Module*
1000Target::GetExecutableModulePointer ()
1001{
1002 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003}
1004
Enrico Granata17598482012-11-08 02:22:02 +00001005static void
1006LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1007{
1008 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001009 StreamString feedback_stream;
1010 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001011 {
Enrico Granata97303392013-05-21 00:00:30 +00001012 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001013 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001014 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1015 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001016 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001017 if (feedback_stream.GetSize())
1018 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1019 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001020}
1021
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022void
Greg Claytonb35db632013-11-09 00:03:31 +00001023Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001024{
Greg Claytonb35db632013-11-09 00:03:31 +00001025 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001026 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027 m_images.Clear();
1028 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001029 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001030 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001031}
1032
1033void
Greg Claytonb35db632013-11-09 00:03:31 +00001034Target::DidExec ()
1035{
1036 // When a process exec's we need to know about it so we can do some cleanup.
1037 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1038 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1039}
1040
1041void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001042Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1043{
1044 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001045 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046
1047 if (executable_sp.get())
1048 {
1049 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001050 "Target::SetExecutableModule (executable = '%s')",
1051 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001053 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054
Jim Ingham5aee1622010-08-09 23:31:02 +00001055 // 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 +00001056 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001057 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001058 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001059 if (log)
1060 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1061 }
1062
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001064 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001066 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001067 {
1068 executable_objfile->GetDependentModules(dependent_files);
1069 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1070 {
Greg Claytonded470d2011-03-19 01:12:21 +00001071 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1072 FileSpec platform_dependent_file_spec;
1073 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001074 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001075 else
1076 platform_dependent_file_spec = dependent_file_spec;
1077
Greg Claytonb9a01b32012-02-26 05:51:37 +00001078 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1079 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080 if (image_module_sp.get())
1081 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082 ObjectFile *objfile = image_module_sp->GetObjectFile();
1083 if (objfile)
1084 objfile->GetDependentModules(dependent_files);
1085 }
1086 }
1087 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001088 }
1089}
1090
1091
Jim Ingham5aee1622010-08-09 23:31:02 +00001092bool
1093Target::SetArchitecture (const ArchSpec &arch_spec)
1094{
Greg Clayton5160ce52013-03-27 23:08:40 +00001095 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001096 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001097 {
Greg Clayton70512312012-05-08 01:45:38 +00001098 // If we haven't got a valid arch spec, or the architectures are
1099 // compatible, so just update the architecture. Architectures can be
1100 // equal, yet the triple OS and vendor might change, so we need to do
1101 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001102 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001103 if (log)
1104 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 +00001105 return true;
1106 }
1107 else
1108 {
1109 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001110 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001111 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 +00001112 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001113 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001114
Greg Claytonb35db632013-11-09 00:03:31 +00001115 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001116 // Need to do something about unsetting breakpoints.
1117
1118 if (executable_sp)
1119 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001120 if (log)
1121 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 +00001122 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1123 Error error = ModuleList::GetSharedModule (module_spec,
1124 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001125 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001126 NULL,
1127 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001128
1129 if (!error.Fail() && executable_sp)
1130 {
1131 SetExecutableModule (executable_sp, true);
1132 return true;
1133 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001134 }
1135 }
Greg Clayton70512312012-05-08 01:45:38 +00001136 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001137}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139void
Enrico Granataefe637d2012-11-08 19:16:03 +00001140Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001141{
1142}
1143
1144void
Enrico Granataefe637d2012-11-08 19:16:03 +00001145Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146{
1147 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001148 if (m_valid)
1149 {
1150 ModuleList my_module_list;
1151 my_module_list.Append(module_sp);
1152 LoadScriptingResourceForModule(module_sp, this);
1153 ModulesDidLoad (my_module_list);
1154 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155}
1156
1157void
Enrico Granataefe637d2012-11-08 19:16:03 +00001158Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001159{
1160 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001161 if (m_valid)
1162 {
1163 ModuleList my_module_list;
1164 my_module_list.Append(module_sp);
1165 ModulesDidUnload (my_module_list, false);
1166 }
Enrico Granata17598482012-11-08 02:22:02 +00001167}
1168
1169void
Enrico Granataefe637d2012-11-08 19:16:03 +00001170Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171{
Jim Inghame716ae02011-08-03 01:00:06 +00001172 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001173 if (m_valid)
1174 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001175}
1176
1177void
1178Target::ModulesDidLoad (ModuleList &module_list)
1179{
Greg Clayton23f8c952014-03-24 23:10:19 +00001180 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001181 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001182 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001183 if (m_process_sp)
1184 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001185 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001186 }
Enrico Granata17598482012-11-08 02:22:02 +00001187 // TODO: make event data that packages up the module_list
1188 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1189 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001190}
1191
1192void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001193Target::SymbolsDidLoad (ModuleList &module_list)
1194{
Greg Clayton23f8c952014-03-24 23:10:19 +00001195 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001196 {
Jim Ingham31caf982013-06-04 23:01:35 +00001197 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001198 {
Jim Ingham31caf982013-06-04 23:01:35 +00001199 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1200 if (runtime)
1201 {
1202 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1203 objc_runtime->SymbolsDidLoad(module_list);
1204 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001205 }
Jim Ingham31caf982013-06-04 23:01:35 +00001206
Greg Clayton095eeaa2013-11-05 23:28:00 +00001207 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jim Ingham31caf982013-06-04 23:01:35 +00001208 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001209 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001210}
1211
1212void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001213Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214{
Greg Clayton23f8c952014-03-24 23:10:19 +00001215 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001216 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001217 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Enrico Granata17598482012-11-08 02:22:02 +00001218 // TODO: make event data that packages up the module_list
1219 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1220 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221}
1222
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001223bool
Greg Claytonb9a01b32012-02-26 05:51:37 +00001224Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001225{
Greg Clayton67cc0632012-08-22 17:17:09 +00001226 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001227 {
1228 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001229 ModuleSpec module_spec (module_file_spec);
1230 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001231
1232 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1233 // black list.
1234 if (num_modules > 0)
1235 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001236 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001237 {
1238 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
1239 return false;
1240 }
1241 return true;
1242 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001243 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001244 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001245}
1246
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001247bool
Jim Inghamc6674fd2011-10-28 23:14:11 +00001248Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
1249{
Greg Clayton67cc0632012-08-22 17:17:09 +00001250 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001251 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001252 if (m_platform_sp)
1253 return m_platform_sp->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001254 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001255 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001256}
1257
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258size_t
Greg Claytondb598232011-01-07 01:57:07 +00001259Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1260{
Greg Claytone72dfb32012-02-24 01:59:29 +00001261 SectionSP section_sp (addr.GetSection());
1262 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001263 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001264 // 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 +00001265 if (section_sp->IsEncrypted())
1266 {
Greg Clayton57f06302012-05-25 17:05:55 +00001267 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001268 return 0;
1269 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001270 ModuleSP module_sp (section_sp->GetModule());
1271 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001272 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001273 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1274 if (objfile)
1275 {
1276 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1277 addr.GetOffset(),
1278 dst,
1279 dst_len);
1280 if (bytes_read > 0)
1281 return bytes_read;
1282 else
1283 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1284 }
Greg Claytondb598232011-01-07 01:57:07 +00001285 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001286 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001287 }
1288 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001289 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001290 }
1291 else
Greg Claytondb598232011-01-07 01:57:07 +00001292 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001293
Greg Claytondb598232011-01-07 01:57:07 +00001294 return 0;
1295}
1296
1297size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001298Target::ReadMemory (const Address& addr,
1299 bool prefer_file_cache,
1300 void *dst,
1301 size_t dst_len,
1302 Error &error,
1303 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001304{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001305 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001306
Enrico Granata9128ee22011-09-06 19:20:51 +00001307 // if we end up reading this from process memory, we will fill this
1308 // with the actual load address
1309 if (load_addr_ptr)
1310 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1311
Greg Claytondb598232011-01-07 01:57:07 +00001312 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001313
1314 addr_t load_addr = LLDB_INVALID_ADDRESS;
1315 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001316 Address resolved_addr;
1317 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001318 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001319 SectionLoadList &section_load_list = GetSectionLoadList();
1320 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001321 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001322 // No sections are loaded, so we must assume we are not running
1323 // yet and anything we are given is a file address.
1324 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1325 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001326 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001327 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001328 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001329 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001330 // we have manually loaded some sections with "target modules load ..."
1331 // or because we have have a live process that has sections loaded
1332 // through the dynamic loader
1333 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001334 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001335 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001336 }
Greg Clayton357132e2011-03-26 19:14:58 +00001337 if (!resolved_addr.IsValid())
1338 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001339
Greg Claytonc749eb82011-07-11 05:12:02 +00001340
Greg Claytondb598232011-01-07 01:57:07 +00001341 if (prefer_file_cache)
1342 {
1343 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1344 if (bytes_read > 0)
1345 return bytes_read;
1346 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001347
Johnny Chen86364b42011-09-20 23:28:55 +00001348 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001349 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001350 if (load_addr == LLDB_INVALID_ADDRESS)
1351 load_addr = resolved_addr.GetLoadAddress (this);
1352
Greg Claytondda4f7b2010-06-30 23:03:03 +00001353 if (load_addr == LLDB_INVALID_ADDRESS)
1354 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001355 ModuleSP addr_module_sp (resolved_addr.GetModule());
1356 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001357 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Claytone72dfb32012-02-24 01:59:29 +00001358 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08 +00001359 resolved_addr.GetFileAddress(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001360 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001361 else
Daniel Malead01b2952012-11-29 21:49:15 +00001362 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001363 }
1364 else
1365 {
Greg Claytondb598232011-01-07 01:57:07 +00001366 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367 if (bytes_read != dst_len)
1368 {
1369 if (error.Success())
1370 {
1371 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001372 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373 else
Daniel Malead01b2952012-11-29 21:49:15 +00001374 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 +00001375 }
1376 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001377 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001378 {
1379 if (load_addr_ptr)
1380 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001381 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001382 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001383 // If the address is not section offset we have an address that
1384 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001385 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001386 // more we can do. If it is section offset, we might be able to
1387 // read cached memory from the object file.
1388 if (!resolved_addr.IsSectionOffset())
1389 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001392
Greg Claytonc749eb82011-07-11 05:12:02 +00001393 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001394 {
Greg Claytondb598232011-01-07 01:57:07 +00001395 // If we didn't already try and read from the object file cache, then
1396 // try it after failing to read from the process.
1397 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001398 }
1399 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400}
1401
Greg Claytond16e1e52011-07-12 17:06:17 +00001402size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001403Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1404{
1405 char buf[256];
1406 out_str.clear();
1407 addr_t curr_addr = addr.GetLoadAddress(this);
1408 Address address(addr);
1409 while (1)
1410 {
1411 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1412 if (length == 0)
1413 break;
1414 out_str.append(buf, length);
1415 // If we got "length - 1" bytes, we didn't get the whole C string, we
1416 // need to read some more characters
1417 if (length == sizeof(buf) - 1)
1418 curr_addr += length;
1419 else
1420 break;
1421 address = Address(curr_addr);
1422 }
1423 return out_str.size();
1424}
1425
1426
1427size_t
1428Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1429{
1430 size_t total_cstr_len = 0;
1431 if (dst && dst_max_len)
1432 {
1433 result_error.Clear();
1434 // NULL out everything just to be safe
1435 memset (dst, 0, dst_max_len);
1436 Error error;
1437 addr_t curr_addr = addr.GetLoadAddress(this);
1438 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001439
1440 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1441 // think this really needs to be tied to the memory cache subsystem's
1442 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001443 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001444
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001445 size_t bytes_left = dst_max_len - 1;
1446 char *curr_dst = dst;
1447
1448 while (bytes_left > 0)
1449 {
1450 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1451 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1452 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1453
1454 if (bytes_read == 0)
1455 {
1456 result_error = error;
1457 dst[total_cstr_len] = '\0';
1458 break;
1459 }
1460 const size_t len = strlen(curr_dst);
1461
1462 total_cstr_len += len;
1463
1464 if (len < bytes_to_read)
1465 break;
1466
1467 curr_dst += bytes_read;
1468 curr_addr += bytes_read;
1469 bytes_left -= bytes_read;
1470 address = Address(curr_addr);
1471 }
1472 }
1473 else
1474 {
1475 if (dst == NULL)
1476 result_error.SetErrorString("invalid arguments");
1477 else
1478 result_error.Clear();
1479 }
1480 return total_cstr_len;
1481}
1482
1483size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001484Target::ReadScalarIntegerFromMemory (const Address& addr,
1485 bool prefer_file_cache,
1486 uint32_t byte_size,
1487 bool is_signed,
1488 Scalar &scalar,
1489 Error &error)
1490{
1491 uint64_t uval;
1492
1493 if (byte_size <= sizeof(uval))
1494 {
1495 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1496 if (bytes_read == byte_size)
1497 {
1498 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001499 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001500 if (byte_size <= 4)
1501 scalar = data.GetMaxU32 (&offset, byte_size);
1502 else
1503 scalar = data.GetMaxU64 (&offset, byte_size);
1504
1505 if (is_signed)
1506 scalar.SignExtend(byte_size * 8);
1507 return bytes_read;
1508 }
1509 }
1510 else
1511 {
1512 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1513 }
1514 return 0;
1515}
1516
1517uint64_t
1518Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1519 bool prefer_file_cache,
1520 size_t integer_byte_size,
1521 uint64_t fail_value,
1522 Error &error)
1523{
1524 Scalar scalar;
1525 if (ReadScalarIntegerFromMemory (addr,
1526 prefer_file_cache,
1527 integer_byte_size,
1528 false,
1529 scalar,
1530 error))
1531 return scalar.ULongLong(fail_value);
1532 return fail_value;
1533}
1534
1535bool
1536Target::ReadPointerFromMemory (const Address& addr,
1537 bool prefer_file_cache,
1538 Error &error,
1539 Address &pointer_addr)
1540{
1541 Scalar scalar;
1542 if (ReadScalarIntegerFromMemory (addr,
1543 prefer_file_cache,
1544 m_arch.GetAddressByteSize(),
1545 false,
1546 scalar,
1547 error))
1548 {
1549 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1550 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1551 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001552 SectionLoadList &section_load_list = GetSectionLoadList();
1553 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001554 {
1555 // No sections are loaded, so we must assume we are not running
1556 // yet and anything we are given is a file address.
1557 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1558 }
1559 else
1560 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001561 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001562 // we have manually loaded some sections with "target modules load ..."
1563 // or because we have have a live process that has sections loaded
1564 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001565 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001566 }
1567 // We weren't able to resolve the pointer value, so just return
1568 // an address with no section
1569 if (!pointer_addr.IsValid())
1570 pointer_addr.SetOffset (pointer_vm_addr);
1571 return true;
1572
1573 }
1574 }
1575 return false;
1576}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001577
1578ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001579Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001580{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001581 ModuleSP module_sp;
1582
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583 Error error;
1584
Jim Ingham4a94c912012-05-17 18:38:42 +00001585 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1586 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001587
Jim Ingham4a94c912012-05-17 18:38:42 +00001588 if (module_spec.GetUUID().IsValid())
1589 module_sp = m_images.FindFirstModule(module_spec);
1590
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001591 if (!module_sp)
1592 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001593 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1594 bool did_create_module = false;
1595
1596 // If there are image search path entries, try to use them first to acquire a suitable image.
1597 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001598 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001599 ModuleSpec transformed_spec (module_spec);
1600 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1601 {
1602 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1603 error = ModuleList::GetSharedModule (transformed_spec,
1604 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001605 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001606 &old_module_sp,
1607 &did_create_module);
1608 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001609 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001610
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001611 if (!module_sp)
1612 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001613 // If we have a UUID, we can check our global shared module list in case
1614 // we already have it. If we don't have a valid UUID, then we can't since
1615 // the path in "module_spec" will be a platform path, and we will need to
1616 // let the platform find that file. For example, we could be asking for
1617 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1618 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1619 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1620 // cache.
1621 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001622 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001623 // We have a UUID, it is OK to check the global module list...
1624 error = ModuleList::GetSharedModule (module_spec,
1625 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001626 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001627 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001628 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001629 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001630
1631 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001632 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001633 // The platform is responsible for finding and caching an appropriate
1634 // module in the shared module cache.
1635 if (m_platform_sp)
1636 {
1637 FileSpec platform_file_spec;
1638 error = m_platform_sp->GetSharedModule (module_spec,
1639 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001640 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001641 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001642 &did_create_module);
1643 }
1644 else
1645 {
1646 error.SetErrorString("no platform is currently set");
1647 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001648 }
1649 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001650
Jim Ingham4a94c912012-05-17 18:38:42 +00001651 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1652 // module in the list already, and if there was, let's remove it.
1653 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001654 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001655 ObjectFile *objfile = module_sp->GetObjectFile();
1656 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001657 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001658 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001659 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001660 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1661 case ObjectFile::eTypeExecutable: /// A normal executable
1662 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1663 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1664 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1665 break;
1666 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1667 if (error_ptr)
1668 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1669 return ModuleSP();
1670 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1671 if (error_ptr)
1672 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1673 return ModuleSP();
1674 default:
1675 if (error_ptr)
1676 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1677 return ModuleSP();
1678 }
1679 // GetSharedModule is not guaranteed to find the old shared module, for instance
1680 // in the common case where you pass in the UUID, it is only going to find the one
1681 // module matching the UUID. In fact, it has no good way to know what the "old module"
1682 // relevant to this target is, since there might be many copies of a module with this file spec
1683 // in various running debug sessions, but only one of them will belong to this target.
1684 // So let's remove the UUID from the module list, and look in the target's module list.
1685 // Only do this if there is SOMETHING else in the module spec...
1686 if (!old_module_sp)
1687 {
1688 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001689 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001690 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1691 module_spec_copy.GetUUID().Clear();
1692
1693 ModuleList found_modules;
1694 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1695 if (num_found == 1)
1696 {
1697 old_module_sp = found_modules.GetModuleAtIndex(0);
1698 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001699 }
1700 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001701
1702 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1703 {
1704 m_images.ReplaceModule(old_module_sp, module_sp);
1705 Module *old_module_ptr = old_module_sp.get();
1706 old_module_sp.reset();
1707 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1708 }
1709 else
1710 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001711 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001712 else
1713 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001714 }
1715 }
1716 if (error_ptr)
1717 *error_ptr = error;
1718 return module_sp;
1719}
1720
1721
Greg Claytond9e416c2012-02-18 05:35:26 +00001722TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723Target::CalculateTarget ()
1724{
Greg Claytond9e416c2012-02-18 05:35:26 +00001725 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726}
1727
Greg Claytond9e416c2012-02-18 05:35:26 +00001728ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729Target::CalculateProcess ()
1730{
Greg Claytond9e416c2012-02-18 05:35:26 +00001731 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001732}
1733
Greg Claytond9e416c2012-02-18 05:35:26 +00001734ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735Target::CalculateThread ()
1736{
Greg Claytond9e416c2012-02-18 05:35:26 +00001737 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001738}
1739
Jason Molendab57e4a12013-11-04 09:33:30 +00001740StackFrameSP
1741Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001742{
Jason Molendab57e4a12013-11-04 09:33:30 +00001743 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001744}
1745
1746void
Greg Clayton0603aa92010-10-04 01:05:56 +00001747Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748{
Greg Claytonc14ee322011-09-22 04:58:26 +00001749 exe_ctx.Clear();
1750 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751}
1752
1753PathMappingList &
1754Target::GetImageSearchPathList ()
1755{
1756 return m_image_search_paths;
1757}
1758
1759void
1760Target::ImageSearchPathsChanged
1761(
1762 const PathMappingList &path_list,
1763 void *baton
1764)
1765{
1766 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001767 ModuleSP exe_module_sp (target->GetExecutableModule());
1768 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001769 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001770}
1771
1772ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001773Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001774{
Greg Clayton73da2442011-08-03 01:23:55 +00001775 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001776 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001777 {
Greg Clayton73da2442011-08-03 01:23:55 +00001778 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001779 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001780 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001781 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001782 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1783 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001784 return m_scratch_ast_context_ap.get();
1785}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001786
Sean Callanan686b2312011-11-16 18:20:47 +00001787ClangASTImporter *
1788Target::GetClangASTImporter()
1789{
1790 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1791
1792 if (!ast_importer)
1793 {
1794 ast_importer = new ClangASTImporter();
1795 m_ast_importer_ap.reset(ast_importer);
1796 }
1797
1798 return ast_importer;
1799}
1800
Greg Clayton99d0faf2010-11-18 23:32:35 +00001801void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001802Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001803{
Greg Clayton6920b522012-08-22 18:39:03 +00001804 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001805}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001806
Greg Clayton99d0faf2010-11-18 23:32:35 +00001807void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001808Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001809{
Greg Clayton6920b522012-08-22 18:39:03 +00001810 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001811}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001812
Greg Claytonc859e2d2012-02-13 23:10:39 +00001813FileSpecList
1814Target::GetDefaultExecutableSearchPaths ()
1815{
Greg Clayton67cc0632012-08-22 17:17:09 +00001816 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1817 if (properties_sp)
1818 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001819 return FileSpecList();
1820}
1821
Michael Sartaina7499c92013-07-01 19:45:50 +00001822FileSpecList
1823Target::GetDefaultDebugFileSearchPaths ()
1824{
1825 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1826 if (properties_sp)
1827 return properties_sp->GetDebugFileSearchPaths();
1828 return FileSpecList();
1829}
1830
Caroline Ticedaccaa92010-09-20 20:44:43 +00001831ArchSpec
1832Target::GetDefaultArchitecture ()
1833{
Greg Clayton67cc0632012-08-22 17:17:09 +00001834 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1835 if (properties_sp)
1836 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001837 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001838}
1839
1840void
Greg Clayton67cc0632012-08-22 17:17:09 +00001841Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001842{
Greg Clayton67cc0632012-08-22 17:17:09 +00001843 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1844 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001845 {
1846 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 +00001847 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001848 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001849}
1850
Greg Clayton0603aa92010-10-04 01:05:56 +00001851Target *
1852Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1853{
1854 // The target can either exist in the "process" of ExecutionContext, or in
1855 // the "target_sp" member of SymbolContext. This accessor helper function
1856 // will get the target from one of these locations.
1857
1858 Target *target = NULL;
1859 if (sc_ptr != NULL)
1860 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001861 if (target == NULL && exe_ctx_ptr)
1862 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001863 return target;
1864}
1865
Jim Ingham1624a2d2014-05-05 02:26:40 +00001866ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001867Target::EvaluateExpression
1868(
1869 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001870 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001871 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001872 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001873)
1874{
Enrico Granata97fca502012-09-18 17:43:16 +00001875 result_valobj_sp.reset();
1876
Jim Ingham8646d3c2014-05-05 02:47:44 +00001877 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001878
Greg Claytond1767f02011-12-08 02:13:16 +00001879 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1880 return execution_results;
1881
Jim Ingham6026ca32011-05-12 02:06:14 +00001882 // We shouldn't run stop hooks in expressions.
1883 // Be sure to reset this if you return anywhere within this function.
1884 bool old_suppress_value = m_suppress_stop_hooks;
1885 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001886
1887 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001888
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001889 if (frame)
1890 {
1891 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001892 }
1893 else if (m_process_sp)
1894 {
1895 m_process_sp->CalculateExecutionContext(exe_ctx);
1896 }
1897 else
1898 {
1899 CalculateExecutionContext(exe_ctx);
1900 }
1901
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001902 // Make sure we aren't just trying to see the value of a persistent
1903 // variable (something like "$0")
1904 lldb::ClangExpressionVariableSP persistent_var_sp;
1905 // Only check for persistent variables the expression starts with a '$'
1906 if (expr_cstr[0] == '$')
1907 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1908
1909 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001910 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001911 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00001912 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001913 }
1914 else
1915 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001916 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00001917 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001918 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001919 options,
1920 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001921 prefix,
1922 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001923 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001924 }
Jim Ingham6026ca32011-05-12 02:06:14 +00001925
1926 m_suppress_stop_hooks = old_suppress_value;
1927
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001928 return execution_results;
1929}
1930
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001931lldb::addr_t
1932Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1933{
1934 addr_t code_addr = load_addr;
1935 switch (m_arch.GetMachine())
1936 {
1937 case llvm::Triple::arm:
1938 case llvm::Triple::thumb:
1939 switch (addr_class)
1940 {
1941 case eAddressClassData:
1942 case eAddressClassDebug:
1943 return LLDB_INVALID_ADDRESS;
1944
1945 case eAddressClassUnknown:
1946 case eAddressClassInvalid:
1947 case eAddressClassCode:
1948 case eAddressClassCodeAlternateISA:
1949 case eAddressClassRuntime:
1950 // Check if bit zero it no set?
1951 if ((code_addr & 1ull) == 0)
1952 {
1953 // Bit zero isn't set, check if the address is a multiple of 2?
1954 if (code_addr & 2ull)
1955 {
1956 // The address is a multiple of 2 so it must be thumb, set bit zero
1957 code_addr |= 1ull;
1958 }
1959 else if (addr_class == eAddressClassCodeAlternateISA)
1960 {
1961 // We checked the address and the address claims to be the alternate ISA
1962 // which means thumb, so set bit zero.
1963 code_addr |= 1ull;
1964 }
1965 }
1966 break;
1967 }
1968 break;
1969
1970 default:
1971 break;
1972 }
1973 return code_addr;
1974}
1975
1976lldb::addr_t
1977Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1978{
1979 addr_t opcode_addr = load_addr;
1980 switch (m_arch.GetMachine())
1981 {
1982 case llvm::Triple::arm:
1983 case llvm::Triple::thumb:
1984 switch (addr_class)
1985 {
1986 case eAddressClassData:
1987 case eAddressClassDebug:
1988 return LLDB_INVALID_ADDRESS;
1989
1990 case eAddressClassInvalid:
1991 case eAddressClassUnknown:
1992 case eAddressClassCode:
1993 case eAddressClassCodeAlternateISA:
1994 case eAddressClassRuntime:
1995 opcode_addr &= ~(1ull);
1996 break;
1997 }
1998 break;
1999
2000 default:
2001 break;
2002 }
2003 return opcode_addr;
2004}
2005
Greg Clayton9585fbf2013-03-19 00:20:55 +00002006SourceManager &
2007Target::GetSourceManager ()
2008{
2009 if (m_source_manager_ap.get() == NULL)
2010 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2011 return *m_source_manager_ap;
2012}
2013
2014
Greg Clayton44d93782014-01-27 23:43:24 +00002015Target::StopHookSP
2016Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002017{
2018 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002019 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2020 m_stop_hooks[new_uid] = stop_hook_sp;
2021 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002022}
2023
2024bool
2025Target::RemoveStopHookByID (lldb::user_id_t user_id)
2026{
2027 size_t num_removed;
2028 num_removed = m_stop_hooks.erase (user_id);
2029 if (num_removed == 0)
2030 return false;
2031 else
2032 return true;
2033}
2034
2035void
2036Target::RemoveAllStopHooks ()
2037{
2038 m_stop_hooks.clear();
2039}
2040
2041Target::StopHookSP
2042Target::GetStopHookByID (lldb::user_id_t user_id)
2043{
2044 StopHookSP found_hook;
2045
2046 StopHookCollection::iterator specified_hook_iter;
2047 specified_hook_iter = m_stop_hooks.find (user_id);
2048 if (specified_hook_iter != m_stop_hooks.end())
2049 found_hook = (*specified_hook_iter).second;
2050 return found_hook;
2051}
2052
2053bool
2054Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2055{
2056 StopHookCollection::iterator specified_hook_iter;
2057 specified_hook_iter = m_stop_hooks.find (user_id);
2058 if (specified_hook_iter == m_stop_hooks.end())
2059 return false;
2060
2061 (*specified_hook_iter).second->SetIsActive (active_state);
2062 return true;
2063}
2064
2065void
2066Target::SetAllStopHooksActiveState (bool active_state)
2067{
2068 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2069 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2070 {
2071 (*pos).second->SetIsActive (active_state);
2072 }
2073}
2074
2075void
2076Target::RunStopHooks ()
2077{
Jim Ingham6026ca32011-05-12 02:06:14 +00002078 if (m_suppress_stop_hooks)
2079 return;
2080
Jim Ingham9575d842011-03-11 03:53:59 +00002081 if (!m_process_sp)
2082 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002083
2084 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2085 // since in that case we do not want to run the stop-hooks
2086 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2087 return;
2088
Jim Ingham9575d842011-03-11 03:53:59 +00002089 if (m_stop_hooks.empty())
2090 return;
2091
2092 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2093
2094 // If there aren't any active stop hooks, don't bother either:
2095 bool any_active_hooks = false;
2096 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2097 {
2098 if ((*pos).second->IsActive())
2099 {
2100 any_active_hooks = true;
2101 break;
2102 }
2103 }
2104 if (!any_active_hooks)
2105 return;
2106
2107 CommandReturnObject result;
2108
2109 std::vector<ExecutionContext> exc_ctx_with_reasons;
2110 std::vector<SymbolContext> sym_ctx_with_reasons;
2111
2112 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2113 size_t num_threads = cur_threadlist.GetSize();
2114 for (size_t i = 0; i < num_threads; i++)
2115 {
2116 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2117 if (cur_thread_sp->ThreadStoppedForAReason())
2118 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002119 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002120 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2121 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2122 }
2123 }
2124
2125 // If no threads stopped for a reason, don't run the stop-hooks.
2126 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2127 if (num_exe_ctx == 0)
2128 return;
2129
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002130 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2131 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002132
2133 bool keep_going = true;
2134 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002135 bool print_hook_header;
2136 bool print_thread_header;
2137
2138 if (num_exe_ctx == 1)
2139 print_thread_header = false;
2140 else
2141 print_thread_header = true;
2142
2143 if (m_stop_hooks.size() == 1)
2144 print_hook_header = false;
2145 else
2146 print_hook_header = true;
2147
Jim Ingham9575d842011-03-11 03:53:59 +00002148 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2149 {
2150 // result.Clear();
2151 StopHookSP cur_hook_sp = (*pos).second;
2152 if (!cur_hook_sp->IsActive())
2153 continue;
2154
2155 bool any_thread_matched = false;
2156 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2157 {
2158 if ((cur_hook_sp->GetSpecifier () == NULL
2159 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2160 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002161 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002162 {
2163 if (!hooks_ran)
2164 {
Jim Ingham9575d842011-03-11 03:53:59 +00002165 hooks_ran = true;
2166 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002167 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002168 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002169 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2170 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2171 NULL);
2172 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002173 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002174 else
Daniel Malead01b2952012-11-29 21:49:15 +00002175 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002176 any_thread_matched = true;
2177 }
2178
Jim Ingham381e25b2011-03-22 01:47:27 +00002179 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002180 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002181
2182 CommandInterpreterRunOptions options;
2183 options.SetStopOnContinue (true);
2184 options.SetStopOnError (true);
2185 options.SetEchoCommands (false);
2186 options.SetPrintResults (true);
2187 options.SetAddToHistory (false);
2188
2189 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2190 &exc_ctx_with_reasons[i],
2191 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002192 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002193
2194 // If the command started the target going again, we should bag out of
2195 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002196 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2197 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002198 {
Daniel Malead01b2952012-11-29 21:49:15 +00002199 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002200 keep_going = false;
2201 }
2202 }
2203 }
2204 }
Jason Molenda879cf772011-09-23 00:42:55 +00002205
Caroline Tice969ed3d2011-05-02 20:41:46 +00002206 result.GetImmediateOutputStream()->Flush();
2207 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002208}
2209
Greg Claytonfbb76342013-11-20 21:07:01 +00002210const TargetPropertiesSP &
2211Target::GetGlobalProperties()
2212{
2213 static TargetPropertiesSP g_settings_sp;
2214 if (!g_settings_sp)
2215 {
2216 g_settings_sp.reset (new TargetProperties (NULL));
2217 }
2218 return g_settings_sp;
2219}
2220
2221Error
2222Target::Install (ProcessLaunchInfo *launch_info)
2223{
2224 Error error;
2225 PlatformSP platform_sp (GetPlatform());
2226 if (platform_sp)
2227 {
2228 if (platform_sp->IsRemote())
2229 {
2230 if (platform_sp->IsConnected())
2231 {
2232 // Install all files that have an install path, and always install the
2233 // main executable when connected to a remote platform
2234 const ModuleList& modules = GetImages();
2235 const size_t num_images = modules.GetSize();
2236 for (size_t idx = 0; idx < num_images; ++idx)
2237 {
2238 const bool is_main_executable = idx == 0;
2239 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2240 if (module_sp)
2241 {
2242 FileSpec local_file (module_sp->GetFileSpec());
2243 if (local_file)
2244 {
2245 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2246 if (!remote_file)
2247 {
2248 if (is_main_executable) // TODO: add setting for always installing main executable???
2249 {
2250 // Always install the main executable
2251 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
2252 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename();
2253 }
2254 }
2255 if (remote_file)
2256 {
2257 error = platform_sp->Install(local_file, remote_file);
2258 if (error.Success())
2259 {
2260 module_sp->SetPlatformFileSpec(remote_file);
2261 if (is_main_executable)
2262 {
2263 if (launch_info)
2264 launch_info->SetExecutableFile(remote_file, false);
2265 }
2266 }
2267 else
2268 break;
2269 }
2270 }
2271 }
2272 }
2273 }
2274 }
2275 }
2276 return error;
2277}
Greg Clayton7b242382011-07-08 00:48:09 +00002278
Greg Claytond5944cd2013-12-06 01:12:00 +00002279bool
2280Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2281{
2282 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2283}
2284
2285bool
2286Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2287{
2288 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2289 if (old_section_load_addr != new_section_load_addr)
2290 {
2291 uint32_t stop_id = 0;
2292 ProcessSP process_sp(GetProcessSP());
2293 if (process_sp)
2294 stop_id = process_sp->GetStopID();
2295 else
2296 stop_id = m_section_load_history.GetLastStopID();
2297 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2298 return true; // Return true if the section load address was changed...
2299 }
2300 return false; // Return false to indicate nothing changed
2301
2302}
2303
2304bool
2305Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2306{
2307 uint32_t stop_id = 0;
2308 ProcessSP process_sp(GetProcessSP());
2309 if (process_sp)
2310 stop_id = process_sp->GetStopID();
2311 else
2312 stop_id = m_section_load_history.GetLastStopID();
2313 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2314}
2315
2316bool
2317Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2318{
2319 uint32_t stop_id = 0;
2320 ProcessSP process_sp(GetProcessSP());
2321 if (process_sp)
2322 stop_id = process_sp->GetStopID();
2323 else
2324 stop_id = m_section_load_history.GetLastStopID();
2325 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2326}
2327
2328void
2329Target::ClearAllLoadedSections ()
2330{
2331 m_section_load_history.Clear();
2332}
2333
Greg Claytonb09c5382013-12-13 17:20:18 +00002334
2335Error
2336Target::Launch (Listener &listener, ProcessLaunchInfo &launch_info)
2337{
2338 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002339 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2340
2341 if (log)
2342 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2343
Greg Claytonb09c5382013-12-13 17:20:18 +00002344 StateType state = eStateInvalid;
2345
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002346 // Scope to temporarily get the process state in case someone has manually
2347 // remotely connected already to a process and we can skip the platform
2348 // launching.
2349 {
2350 ProcessSP process_sp (GetProcessSP());
2351
2352 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002353 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002354 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002355 if (log)
2356 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2357 }
2358 else
2359 {
2360 if (log)
2361 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2362 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002363 }
2364
Greg Claytonb09c5382013-12-13 17:20:18 +00002365 launch_info.GetFlags().Set (eLaunchFlagDebug);
2366
2367 // Get the value of synchronous execution here. If you wait till after you have started to
2368 // run, then you could have hit a breakpoint, whose command might switch the value, and
2369 // then you'll pick up that incorrect value.
2370 Debugger &debugger = GetDebugger();
2371 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2372
2373 PlatformSP platform_sp (GetPlatform());
2374
2375 // Finalize the file actions, and if none were given, default to opening
2376 // up a pseudo terminal
2377 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002378 if (log)
2379 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2380 __FUNCTION__,
2381 platform_sp ? "true" : "false",
2382 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2383 default_to_use_pty ? "true" : "false");
2384
Greg Claytonb09c5382013-12-13 17:20:18 +00002385 launch_info.FinalizeFileActions (this, default_to_use_pty);
2386
2387 if (state == eStateConnected)
2388 {
2389 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2390 {
2391 error.SetErrorString("can't launch in tty when launching through a remote connection");
2392 return error;
2393 }
2394 }
2395
2396 if (!launch_info.GetArchitecture().IsValid())
2397 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002398
2399 // 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 +00002400 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2401 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002402 if (log)
2403 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2404
Greg Claytonb09c5382013-12-13 17:20:18 +00002405 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2406 debugger,
2407 this,
2408 listener,
2409 error);
2410 }
2411 else
2412 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002413 if (log)
2414 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2415
Greg Claytonb09c5382013-12-13 17:20:18 +00002416 if (state == eStateConnected)
2417 {
2418 assert(m_process_sp);
2419 }
2420 else
2421 {
Todd Fiala015d8182014-07-22 23:41:36 +00002422 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002423 const char *plugin_name = launch_info.GetProcessPluginName();
2424 CreateProcess (listener, plugin_name, NULL);
2425 }
Todd Fiala015d8182014-07-22 23:41:36 +00002426
2427 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002428 if (m_process_sp)
2429 error = m_process_sp->Launch (launch_info);
2430 }
2431
2432 if (!m_process_sp)
2433 {
2434 if (error.Success())
2435 error.SetErrorString("failed to launch or debug process");
2436 return error;
2437 }
2438
2439 if (error.Success())
2440 {
2441 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
2442 {
Greg Clayton44d93782014-01-27 23:43:24 +00002443 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00002444
Greg Clayton44d93782014-01-27 23:43:24 +00002445 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get());
Greg Claytonb09c5382013-12-13 17:20:18 +00002446
2447 if (state == eStateStopped)
2448 {
Greg Clayton44d93782014-01-27 23:43:24 +00002449 if (!synchronous_execution)
2450 m_process_sp->RestoreProcessEvents ();
2451
2452 error = m_process_sp->PrivateResume();
Todd Fialaa3b89e22014-08-12 14:33:19 +00002453
Greg Claytonb09c5382013-12-13 17:20:18 +00002454 if (error.Success())
2455 {
Todd Fialaa3b89e22014-08-12 14:33:19 +00002456 // there is a race condition where this thread will return up the call stack to the main command
2457 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
2458 // a chance to call PushProcessIOHandler()
2459 m_process_sp->SyncIOHandler(2000);
2460
Greg Claytonb09c5382013-12-13 17:20:18 +00002461 if (synchronous_execution)
2462 {
Greg Clayton44d93782014-01-27 23:43:24 +00002463 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get());
Greg Claytonb09c5382013-12-13 17:20:18 +00002464 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2465 if (!StateIsStoppedState(state, must_be_alive))
2466 {
Greg Clayton44d93782014-01-27 23:43:24 +00002467 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
Greg Claytonb09c5382013-12-13 17:20:18 +00002468 }
2469 }
2470 }
2471 else
2472 {
Greg Clayton44d93782014-01-27 23:43:24 +00002473 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002474 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002475 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002476 }
2477 }
Greg Clayton40286e02014-04-30 20:29:09 +00002478 else if (state == eStateExited)
2479 {
2480 bool with_shell = launch_info.GetShell();
2481 const int exit_status = m_process_sp->GetExitStatus();
2482 const char *exit_desc = m_process_sp->GetExitDescription();
2483#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'."
2484 if (exit_desc && exit_desc[0])
2485 {
2486 if (with_shell)
2487 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2488 else
2489 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2490 }
2491 else
2492 {
2493 if (with_shell)
2494 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2495 else
2496 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2497 }
2498 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002499 else
2500 {
2501 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2502 }
2503 }
Greg Clayton44d93782014-01-27 23:43:24 +00002504 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002505 }
2506 else
2507 {
Greg Clayton44d93782014-01-27 23:43:24 +00002508 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002509 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002510 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002511 }
2512 return error;
2513}
Jim Ingham9575d842011-03-11 03:53:59 +00002514//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002515// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002516//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002517Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2518 UserID (uid),
2519 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002520 m_commands (),
2521 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002522 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002523 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002524{
2525}
2526
2527Target::StopHook::StopHook (const StopHook &rhs) :
2528 UserID (rhs.GetID()),
2529 m_target_sp (rhs.m_target_sp),
2530 m_commands (rhs.m_commands),
2531 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002532 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002533 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002534{
2535 if (rhs.m_thread_spec_ap.get() != NULL)
2536 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2537}
2538
2539
2540Target::StopHook::~StopHook ()
2541{
2542}
2543
2544void
2545Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2546{
2547 m_thread_spec_ap.reset (specifier);
2548}
2549
2550
2551void
2552Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2553{
2554 int indent_level = s->GetIndentLevel();
2555
2556 s->SetIndentLevel(indent_level + 2);
2557
Daniel Malead01b2952012-11-29 21:49:15 +00002558 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002559 if (m_active)
2560 s->Indent ("State: enabled\n");
2561 else
2562 s->Indent ("State: disabled\n");
2563
2564 if (m_specifier_sp)
2565 {
2566 s->Indent();
2567 s->PutCString ("Specifier:\n");
2568 s->SetIndentLevel (indent_level + 4);
2569 m_specifier_sp->GetDescription (s, level);
2570 s->SetIndentLevel (indent_level + 2);
2571 }
2572
2573 if (m_thread_spec_ap.get() != NULL)
2574 {
2575 StreamString tmp;
2576 s->Indent("Thread:\n");
2577 m_thread_spec_ap->GetDescription (&tmp, level);
2578 s->SetIndentLevel (indent_level + 4);
2579 s->Indent (tmp.GetData());
2580 s->PutCString ("\n");
2581 s->SetIndentLevel (indent_level + 2);
2582 }
2583
2584 s->Indent ("Commands: \n");
2585 s->SetIndentLevel (indent_level + 4);
2586 uint32_t num_commands = m_commands.GetSize();
2587 for (uint32_t i = 0; i < num_commands; i++)
2588 {
2589 s->Indent(m_commands.GetStringAtIndex(i));
2590 s->PutCString ("\n");
2591 }
2592 s->SetIndentLevel (indent_level);
2593}
2594
Greg Clayton67cc0632012-08-22 17:17:09 +00002595//--------------------------------------------------------------
2596// class TargetProperties
2597//--------------------------------------------------------------
2598
2599OptionEnumValueElement
2600lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002601{
Greg Clayton67cc0632012-08-22 17:17:09 +00002602 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2603 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2604 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2605 { 0, NULL, NULL }
2606};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002607
Greg Clayton1f746072012-08-29 21:13:06 +00002608static OptionEnumValueElement
2609g_inline_breakpoint_enums[] =
2610{
2611 { 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."},
2612 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2613 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2614 { 0, NULL, NULL }
2615};
2616
Jim Ingham0f063ba2013-03-02 00:26:47 +00002617typedef enum x86DisassemblyFlavor
2618{
2619 eX86DisFlavorDefault,
2620 eX86DisFlavorIntel,
2621 eX86DisFlavorATT
2622} x86DisassemblyFlavor;
2623
2624static OptionEnumValueElement
2625g_x86_dis_flavor_value_types[] =
2626{
2627 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2628 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2629 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2630 { 0, NULL, NULL }
2631};
2632
Enrico Granata397ddd52013-05-21 20:13:34 +00002633static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002634g_hex_immediate_style_values[] =
2635{
2636 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2637 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2638 { 0, NULL, NULL }
2639};
2640
2641static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002642g_load_script_from_sym_file_values[] =
2643{
2644 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2645 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2646 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2647 { 0, NULL, NULL }
2648};
2649
Greg Claytonfd814c52013-08-13 01:42:25 +00002650
2651static OptionEnumValueElement
2652g_memory_module_load_level_values[] =
2653{
Greg Clayton86eac942013-08-13 21:32:34 +00002654 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002655 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2656 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2657 { 0, NULL, NULL }
2658};
2659
Greg Clayton67cc0632012-08-22 17:17:09 +00002660static PropertyDefinition
2661g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002662{
Greg Clayton67cc0632012-08-22 17:17:09 +00002663 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2664 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2665 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2666 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2667 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2668 { "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 "
2669 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2670 "some part (starting at the root) of the path to the file when it was built, "
2671 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2672 "Each element of the array is checked in order and the first one that results in a match wins." },
2673 { "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 +00002674 { "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 +00002675 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2676 { "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 +00002677 { "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 +00002678 { "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 +00002679 { "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." },
2680 { "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 +00002681 { "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." },
2682 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2683 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2684 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2685 { "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 +00002686 { "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 +00002687 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2688 { "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 +00002689 { "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 +00002690 "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. "
2691 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2692 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002693 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2694 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2695 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002696 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002697 // 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.
2698 { "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 +00002699 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2700 { "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 +00002701 { "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 +00002702 { "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 +00002703 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2704 "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. "
2705 "This setting helps users control how much information gets loaded when loading modules from memory."
2706 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2707 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2708 "'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 +00002709 { "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 +00002710 { "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 +00002711 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2712};
2713enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002714{
Greg Clayton67cc0632012-08-22 17:17:09 +00002715 ePropertyDefaultArch,
2716 ePropertyExprPrefix,
2717 ePropertyPreferDynamic,
2718 ePropertyEnableSynthetic,
2719 ePropertySkipPrologue,
2720 ePropertySourceMap,
2721 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002722 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002723 ePropertyMaxChildrenCount,
2724 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002725 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002726 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002727 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002728 ePropertyRunArgs,
2729 ePropertyEnvVars,
2730 ePropertyInheritEnv,
2731 ePropertyInputPath,
2732 ePropertyOutputPath,
2733 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00002734 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00002735 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00002736 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002737 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00002738 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00002739 ePropertyUseHexImmediates,
2740 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002741 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00002742 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00002743 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00002744 ePropertyDisplayExpressionsInCrashlogs,
2745 ePropertyTrapHandlerNames
Greg Clayton67cc0632012-08-22 17:17:09 +00002746};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002747
Caroline Ticedaccaa92010-09-20 20:44:43 +00002748
Greg Clayton67cc0632012-08-22 17:17:09 +00002749class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002750{
Greg Clayton67cc0632012-08-22 17:17:09 +00002751public:
2752 TargetOptionValueProperties (const ConstString &name) :
2753 OptionValueProperties (name),
2754 m_target (NULL),
2755 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002756 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00002757 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002758
Greg Clayton67cc0632012-08-22 17:17:09 +00002759 // This constructor is used when creating TargetOptionValueProperties when it
2760 // is part of a new lldb_private::Target instance. It will copy all current
2761 // global property values as needed
2762 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2763 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2764 m_target (target),
2765 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002766 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002767 }
2768
2769 virtual const Property *
2770 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2771 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002772 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00002773 // try and grab the setting from the current target if there is one. Else we just
2774 // use the one from this instance.
2775 if (idx == ePropertyEnvVars)
2776 GetHostEnvironmentIfNeeded ();
2777
2778 if (exe_ctx)
2779 {
2780 Target *target = exe_ctx->GetTargetPtr();
2781 if (target)
2782 {
2783 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2784 if (this != target_properties)
2785 return target_properties->ProtectedGetPropertyAtIndex (idx);
2786 }
2787 }
2788 return ProtectedGetPropertyAtIndex (idx);
2789 }
Enrico Granata84a53df2013-05-20 22:29:23 +00002790
2791 lldb::TargetSP
2792 GetTargetSP ()
2793 {
2794 return m_target->shared_from_this();
2795 }
2796
Greg Clayton67cc0632012-08-22 17:17:09 +00002797protected:
2798
2799 void
2800 GetHostEnvironmentIfNeeded () const
2801 {
2802 if (!m_got_host_env)
2803 {
2804 if (m_target)
2805 {
2806 m_got_host_env = true;
2807 const uint32_t idx = ePropertyInheritEnv;
2808 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2809 {
2810 PlatformSP platform_sp (m_target->GetPlatform());
2811 if (platform_sp)
2812 {
2813 StringList env;
2814 if (platform_sp->GetEnvironment(env))
2815 {
2816 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2817 if (env_dict)
2818 {
2819 const bool can_replace = false;
2820 const size_t envc = env.GetSize();
2821 for (size_t idx=0; idx<envc; idx++)
2822 {
2823 const char *env_entry = env.GetStringAtIndex (idx);
2824 if (env_entry)
2825 {
2826 const char *equal_pos = ::strchr(env_entry, '=');
2827 ConstString key;
2828 // It is ok to have environment variables with no values
2829 const char *value = NULL;
2830 if (equal_pos)
2831 {
2832 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2833 if (equal_pos[1])
2834 value = equal_pos + 1;
2835 }
2836 else
2837 {
2838 key.SetCString(env_entry);
2839 }
2840 // Don't allow existing keys to be replaced with ones we get from the platform environment
2841 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2842 }
2843 }
2844 }
2845 }
2846 }
2847 }
2848 }
2849 }
2850 }
2851 Target *m_target;
2852 mutable bool m_got_host_env;
2853};
2854
Greg Claytonfbb76342013-11-20 21:07:01 +00002855//----------------------------------------------------------------------
2856// TargetProperties
2857//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00002858TargetProperties::TargetProperties (Target *target) :
2859 Properties ()
2860{
2861 if (target)
2862 {
2863 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Ticedaccaa92010-09-20 20:44:43 +00002864 }
2865 else
Greg Clayton67cc0632012-08-22 17:17:09 +00002866 {
2867 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2868 m_collection_sp->Initialize(g_properties);
2869 m_collection_sp->AppendProperty(ConstString("process"),
2870 ConstString("Settings specify to processes."),
2871 true,
2872 Process::GetGlobalProperties()->GetValueProperties());
2873 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002874}
2875
Greg Clayton67cc0632012-08-22 17:17:09 +00002876TargetProperties::~TargetProperties ()
2877{
2878}
2879ArchSpec
2880TargetProperties::GetDefaultArchitecture () const
2881{
2882 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2883 if (value)
2884 return value->GetCurrentValue();
2885 return ArchSpec();
2886}
2887
2888void
2889TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2890{
2891 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2892 if (value)
2893 return value->SetCurrentValue(arch, true);
2894}
2895
2896lldb::DynamicValueType
2897TargetProperties::GetPreferDynamicValue() const
2898{
2899 const uint32_t idx = ePropertyPreferDynamic;
2900 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2901}
2902
2903bool
2904TargetProperties::GetDisableASLR () const
2905{
2906 const uint32_t idx = ePropertyDisableASLR;
2907 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2908}
2909
2910void
2911TargetProperties::SetDisableASLR (bool b)
2912{
2913 const uint32_t idx = ePropertyDisableASLR;
2914 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2915}
2916
2917bool
Jim Ingham106d0282014-06-25 02:32:56 +00002918TargetProperties::GetDetachOnError () const
2919{
2920 const uint32_t idx = ePropertyDetachOnError;
2921 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2922}
2923
2924void
2925TargetProperties::SetDetachOnError (bool b)
2926{
2927 const uint32_t idx = ePropertyDetachOnError;
2928 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2929}
2930
2931bool
Greg Clayton67cc0632012-08-22 17:17:09 +00002932TargetProperties::GetDisableSTDIO () const
2933{
2934 const uint32_t idx = ePropertyDisableSTDIO;
2935 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2936}
2937
2938void
2939TargetProperties::SetDisableSTDIO (bool b)
2940{
2941 const uint32_t idx = ePropertyDisableSTDIO;
2942 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2943}
2944
Jim Ingham0f063ba2013-03-02 00:26:47 +00002945const char *
2946TargetProperties::GetDisassemblyFlavor () const
2947{
2948 const uint32_t idx = ePropertyDisassemblyFlavor;
2949 const char *return_value;
2950
2951 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2952 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
2953 return return_value;
2954}
2955
Greg Clayton1f746072012-08-29 21:13:06 +00002956InlineStrategy
2957TargetProperties::GetInlineStrategy () const
2958{
2959 const uint32_t idx = ePropertyInlineStrategy;
2960 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2961}
2962
Greg Clayton45392552012-10-17 22:57:12 +00002963const char *
2964TargetProperties::GetArg0 () const
2965{
2966 const uint32_t idx = ePropertyArg0;
2967 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
2968}
2969
2970void
2971TargetProperties::SetArg0 (const char *arg)
2972{
2973 const uint32_t idx = ePropertyArg0;
2974 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
2975}
2976
Greg Clayton67cc0632012-08-22 17:17:09 +00002977bool
2978TargetProperties::GetRunArguments (Args &args) const
2979{
2980 const uint32_t idx = ePropertyRunArgs;
2981 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
2982}
2983
2984void
2985TargetProperties::SetRunArguments (const Args &args)
2986{
2987 const uint32_t idx = ePropertyRunArgs;
2988 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
2989}
2990
2991size_t
2992TargetProperties::GetEnvironmentAsArgs (Args &env) const
2993{
2994 const uint32_t idx = ePropertyEnvVars;
2995 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
2996}
2997
2998bool
2999TargetProperties::GetSkipPrologue() const
3000{
3001 const uint32_t idx = ePropertySkipPrologue;
3002 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3003}
3004
3005PathMappingList &
3006TargetProperties::GetSourcePathMap () const
3007{
3008 const uint32_t idx = ePropertySourceMap;
3009 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3010 assert(option_value);
3011 return option_value->GetCurrentValue();
3012}
3013
3014FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003015TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003016{
3017 const uint32_t idx = ePropertyExecutableSearchPaths;
3018 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3019 assert(option_value);
3020 return option_value->GetCurrentValue();
3021}
3022
Michael Sartaina7499c92013-07-01 19:45:50 +00003023FileSpecList &
3024TargetProperties::GetDebugFileSearchPaths ()
3025{
3026 const uint32_t idx = ePropertyDebugFileSearchPaths;
3027 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3028 assert(option_value);
3029 return option_value->GetCurrentValue();
3030}
3031
Greg Clayton67cc0632012-08-22 17:17:09 +00003032bool
3033TargetProperties::GetEnableSyntheticValue () const
3034{
3035 const uint32_t idx = ePropertyEnableSynthetic;
3036 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3037}
3038
3039uint32_t
3040TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3041{
3042 const uint32_t idx = ePropertyMaxChildrenCount;
3043 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3044}
3045
3046uint32_t
3047TargetProperties::GetMaximumSizeOfStringSummary() const
3048{
3049 const uint32_t idx = ePropertyMaxSummaryLength;
3050 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3051}
3052
Enrico Granatad325bf92013-06-04 22:54:16 +00003053uint32_t
3054TargetProperties::GetMaximumMemReadSize () const
3055{
3056 const uint32_t idx = ePropertyMaxMemReadSize;
3057 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3058}
3059
Greg Clayton67cc0632012-08-22 17:17:09 +00003060FileSpec
3061TargetProperties::GetStandardInputPath () const
3062{
3063 const uint32_t idx = ePropertyInputPath;
3064 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3065}
3066
3067void
3068TargetProperties::SetStandardInputPath (const char *p)
3069{
3070 const uint32_t idx = ePropertyInputPath;
3071 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3072}
3073
3074FileSpec
3075TargetProperties::GetStandardOutputPath () const
3076{
3077 const uint32_t idx = ePropertyOutputPath;
3078 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3079}
3080
3081void
3082TargetProperties::SetStandardOutputPath (const char *p)
3083{
3084 const uint32_t idx = ePropertyOutputPath;
3085 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3086}
3087
3088FileSpec
3089TargetProperties::GetStandardErrorPath () const
3090{
3091 const uint32_t idx = ePropertyErrorPath;
3092 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3093}
3094
Greg Clayton6920b522012-08-22 18:39:03 +00003095const char *
3096TargetProperties::GetExpressionPrefixContentsAsCString ()
3097{
3098 const uint32_t idx = ePropertyExprPrefix;
3099 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3100 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003101 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003102 const bool null_terminate = true;
3103 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003104 if (data_sp)
3105 return (const char *) data_sp->GetBytes();
3106 }
Greg Clayton6920b522012-08-22 18:39:03 +00003107 return NULL;
3108}
3109
Greg Clayton67cc0632012-08-22 17:17:09 +00003110void
3111TargetProperties::SetStandardErrorPath (const char *p)
3112{
3113 const uint32_t idx = ePropertyErrorPath;
3114 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3115}
3116
3117bool
3118TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3119{
3120 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3121 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3122}
3123
Jim Ingham17d023f2013-03-13 17:58:04 +00003124bool
Daniel Malead79ae052013-08-07 21:54:09 +00003125TargetProperties::GetUseHexImmediates () const
3126{
3127 const uint32_t idx = ePropertyUseHexImmediates;
3128 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3129}
3130
3131bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003132TargetProperties::GetUseFastStepping () const
3133{
3134 const uint32_t idx = ePropertyUseFastStepping;
3135 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3136}
3137
Greg Claytonfb6621e2013-12-06 21:59:52 +00003138bool
3139TargetProperties::GetDisplayExpressionsInCrashlogs () const
3140{
3141 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3142 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3143}
3144
Enrico Granata397ddd52013-05-21 20:13:34 +00003145LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003146TargetProperties::GetLoadScriptFromSymbolFile () const
3147{
3148 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003149 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003150}
3151
Daniel Malead79ae052013-08-07 21:54:09 +00003152Disassembler::HexImmediateStyle
3153TargetProperties::GetHexImmediateStyle () const
3154{
3155 const uint32_t idx = ePropertyHexImmediateStyle;
3156 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3157}
3158
Greg Claytonfd814c52013-08-13 01:42:25 +00003159MemoryModuleLoadLevel
3160TargetProperties::GetMemoryModuleLoadLevel() const
3161{
3162 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3163 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3164}
3165
Jason Molendaa4bea722014-02-14 05:06:49 +00003166bool
3167TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3168{
3169 const uint32_t idx = ePropertyTrapHandlerNames;
3170 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3171}
Greg Claytonfd814c52013-08-13 01:42:25 +00003172
Jason Molendaa4bea722014-02-14 05:06:49 +00003173void
3174TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3175{
3176 const uint32_t idx = ePropertyTrapHandlerNames;
3177 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3178}
Greg Clayton67cc0632012-08-22 17:17:09 +00003179
Greg Claytonfbb76342013-11-20 21:07:01 +00003180//----------------------------------------------------------------------
3181// Target::TargetEventData
3182//----------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +00003183const ConstString &
3184Target::TargetEventData::GetFlavorString ()
3185{
3186 static ConstString g_flavor ("Target::TargetEventData");
3187 return g_flavor;
3188}
3189
3190const ConstString &
3191Target::TargetEventData::GetFlavor () const
3192{
3193 return TargetEventData::GetFlavorString ();
3194}
3195
3196Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
3197 EventData(),
3198 m_target_sp (new_target_sp)
3199{
3200}
3201
3202Target::TargetEventData::~TargetEventData()
3203{
3204
3205}
3206
3207void
3208Target::TargetEventData::Dump (Stream *s) const
3209{
3210
3211}
3212
3213const TargetSP
3214Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
3215{
3216 TargetSP target_sp;
3217
3218 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
3219 if (data)
3220 target_sp = data->m_target_sp;
3221
3222 return target_sp;
3223}
3224
3225const Target::TargetEventData *
3226Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3227{
3228 if (event_ptr)
3229 {
3230 const EventData *event_data = event_ptr->GetData();
3231 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3232 return static_cast <const TargetEventData *> (event_ptr->GetData());
3233 }
3234 return NULL;
3235}
3236