blob: 2d951f4b6c06e5a33041916672c6db8b1213dfb1 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/Target.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Breakpoint/BreakpointResolver.h"
19#include "lldb/Breakpoint/BreakpointResolverAddress.h"
20#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000021#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000023#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000024#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Event.h"
26#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/ModuleSpec.h"
29#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000030#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000031#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000032#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000034#include "lldb/Core/Timer.h"
35#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000036#include "lldb/Expression/ClangASTSource.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000037#include "lldb/Expression/ClangUserExpression.h"
Zachary Turner10687b02014-10-20 17:46:43 +000038#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000040#include "lldb/Interpreter/CommandInterpreter.h"
41#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000042#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000043#include "lldb/Interpreter/OptionValues.h"
44#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/lldb-private-log.h"
46#include "lldb/Symbol/ObjectFile.h"
47#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000048#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000049#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000050#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000051#include "lldb/Target/Thread.h"
52#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54using namespace lldb;
55using namespace lldb_private;
56
Jim Ingham4bddaeb2012-02-16 06:50:00 +000057ConstString &
58Target::GetStaticBroadcasterClass ()
59{
60 static ConstString class_name ("lldb.target");
61 return class_name;
62}
63
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064//----------------------------------------------------------------------
65// Target constructor
66//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +000067Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000068 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000069 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000070 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000071 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000072 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000073 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000074 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000075 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000076 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077 m_breakpoint_list (false),
78 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000079 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000080 m_process_sp (),
81 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000083 m_scratch_ast_context_ap (),
84 m_scratch_ast_source_ap (),
85 m_ast_importer_ap (),
Jim Ingham9575d842011-03-11 03:53:59 +000086 m_persistent_variables (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000087 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000088 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000089 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000090 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +000091 m_suppress_stop_hooks (false),
92 m_is_dummy_target(is_dummy_target)
93
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000095 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
96 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
97 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +000098 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +000099 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000100
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000101 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000102
Greg Clayton5160ce52013-03-27 23:08:40 +0000103 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000105 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000106 if (m_arch.IsValid())
107 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000108 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 +0000109 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
Jim Ingham893c9322014-11-22 01:42:44 +0000112void
113Target::PrimeFromDummyTarget(Target *target)
114{
115 if (!target)
116 return;
117
118 m_stop_hooks = target->m_stop_hooks;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000119
120 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints())
121 {
122 if (breakpoint_sp->IsInternal())
123 continue;
124
125 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get()));
126 AddBreakpoint (new_bp, false);
127 }
Jim Ingham893c9322014-11-22 01:42:44 +0000128}
129
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130//----------------------------------------------------------------------
131// Destructor
132//----------------------------------------------------------------------
133Target::~Target()
134{
Greg Clayton5160ce52013-03-27 23:08:40 +0000135 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000137 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 DeleteCurrentProcess ();
139}
140
141void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000142Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143{
Greg Clayton89411422010-10-08 00:21:05 +0000144// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000145 if (description_level != lldb::eDescriptionLevelBrief)
146 {
147 s->Indent();
148 s->PutCString("Target\n");
149 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000150 m_images.Dump(s);
151 m_breakpoint_list.Dump(s);
152 m_internal_breakpoint_list.Dump(s);
153 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000154 }
155 else
156 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000157 Module *exe_module = GetExecutableModulePointer();
158 if (exe_module)
159 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000160 else
161 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000162 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163}
164
165void
Greg Clayton90ba8112012-12-05 00:16:59 +0000166Target::CleanupProcess ()
167{
168 // Do any cleanup of the target we need to do between process instances.
169 // NB It is better to do this before destroying the process in case the
170 // clean up needs some help from the process.
171 m_breakpoint_list.ClearAllBreakpointSites();
172 m_internal_breakpoint_list.ClearAllBreakpointSites();
173 // Disable watchpoints just on the debugger side.
174 Mutex::Locker locker;
175 this->GetWatchpointList().GetListMutex(locker);
176 DisableAllWatchpoints(false);
177 ClearAllWatchpointHitCounts();
Enrico Granata5e3fe042015-02-11 00:37:54 +0000178 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000179}
180
181void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182Target::DeleteCurrentProcess ()
183{
184 if (m_process_sp.get())
185 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000186 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 if (m_process_sp->IsAlive())
188 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000189
190 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191
Greg Clayton90ba8112012-12-05 00:16:59 +0000192 CleanupProcess ();
193
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 m_process_sp.reset();
195 }
196}
197
198const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000199Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200{
201 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000202 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 return m_process_sp;
204}
205
206const lldb::ProcessSP &
207Target::GetProcessSP () const
208{
209 return m_process_sp;
210}
211
Greg Clayton3418c852011-08-10 02:10:13 +0000212void
213Target::Destroy()
214{
215 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000216 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000217 DeleteCurrentProcess ();
218 m_platform_sp.reset();
219 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000220 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000221 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000222 const bool notify = false;
223 m_breakpoint_list.RemoveAll(notify);
224 m_internal_breakpoint_list.RemoveAll(notify);
225 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000226 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000227 m_search_filter_sp.reset();
228 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000229 m_persistent_variables.Clear();
230 m_stop_hooks.clear();
231 m_stop_hook_next_id = 0;
232 m_suppress_stop_hooks = false;
233}
234
235
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236BreakpointList &
237Target::GetBreakpointList(bool internal)
238{
239 if (internal)
240 return m_internal_breakpoint_list;
241 else
242 return m_breakpoint_list;
243}
244
245const BreakpointList &
246Target::GetBreakpointList(bool internal) const
247{
248 if (internal)
249 return m_internal_breakpoint_list;
250 else
251 return m_breakpoint_list;
252}
253
254BreakpointSP
255Target::GetBreakpointByID (break_id_t break_id)
256{
257 BreakpointSP bp_sp;
258
259 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
260 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
261 else
262 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
263
264 return bp_sp;
265}
266
267BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000268Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000269 const FileSpecList *source_file_spec_list,
270 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000271 bool internal,
272 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273{
Jim Ingham87df91b2011-09-23 00:54:11 +0000274 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
275 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000276 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000277}
278
279
280BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000281Target::CreateBreakpoint (const FileSpecList *containingModules,
282 const FileSpec &file,
283 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000284 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000285 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000286 bool internal,
287 bool hardware)
Jim Ingham969795f2011-09-21 01:17:13 +0000288{
Greg Clayton1f746072012-08-29 21:13:06 +0000289 if (check_inlines == eLazyBoolCalculate)
290 {
291 const InlineStrategy inline_strategy = GetInlineStrategy();
292 switch (inline_strategy)
293 {
294 case eInlineBreakpointsNever:
295 check_inlines = eLazyBoolNo;
296 break;
297
298 case eInlineBreakpointsHeaders:
299 if (file.IsSourceImplementationFile())
300 check_inlines = eLazyBoolNo;
301 else
302 check_inlines = eLazyBoolYes;
303 break;
304
305 case eInlineBreakpointsAlways:
306 check_inlines = eLazyBoolYes;
307 break;
308 }
309 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000310 SearchFilterSP filter_sp;
311 if (check_inlines == eLazyBoolNo)
312 {
313 // Not checking for inlines, we are looking only for matching compile units
314 FileSpecList compile_unit_list;
315 compile_unit_list.Append (file);
316 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
317 }
318 else
319 {
320 filter_sp = GetSearchFilterForModuleList (containingModules);
321 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000322 if (skip_prologue == eLazyBoolCalculate)
323 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
324
Greg Clayton1f746072012-08-29 21:13:06 +0000325 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
326 file,
327 line_no,
328 check_inlines,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000329 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000330 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331}
332
333
334BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000335Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 Address so_addr;
338 // Attempt to resolve our load address if possible, though it is ok if
339 // it doesn't resolve to section/offset.
340
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000341 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000342 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000343 if (!so_addr.IsValid())
344 {
345 // The address didn't resolve, so just set this as an absolute address
346 so_addr.SetOffset (addr);
347 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000348 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 return bp_sp;
350}
351
352BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000353Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000355 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000357 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358}
359
360BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000361Target::CreateBreakpoint (const FileSpecList *containingModules,
362 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000363 const char *func_name,
364 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000365 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000366 bool internal,
367 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000369 BreakpointSP bp_sp;
370 if (func_name)
371 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000372 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000373
374 if (skip_prologue == eLazyBoolCalculate)
375 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
376
Greg Claytond16e1e52011-07-12 17:06:17 +0000377 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
378 func_name,
379 func_name_type_mask,
380 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000381 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000382 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000383 }
384 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385}
386
Jim Inghamfab10e82012-03-06 00:37:27 +0000387lldb::BreakpointSP
388Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000389 const FileSpecList *containingSourceFiles,
390 const std::vector<std::string> &func_names,
391 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000392 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000393 bool internal,
394 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000395{
396 BreakpointSP bp_sp;
397 size_t num_names = func_names.size();
398 if (num_names > 0)
399 {
400 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000401
402 if (skip_prologue == eLazyBoolCalculate)
403 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
404
405 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000406 func_names,
407 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000408 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000409 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000410 }
411 return bp_sp;
412}
413
Jim Ingham133e0fb2012-03-03 02:05:11 +0000414BreakpointSP
415Target::CreateBreakpoint (const FileSpecList *containingModules,
416 const FileSpecList *containingSourceFiles,
417 const char *func_names[],
418 size_t num_names,
419 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000420 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000421 bool internal,
422 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000423{
424 BreakpointSP bp_sp;
425 if (num_names > 0)
426 {
427 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
428
Greg Clayton03da4cc2013-04-19 21:31:16 +0000429 if (skip_prologue == eLazyBoolCalculate)
430 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
431
432 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000433 func_names,
434 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000435 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000436 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000437 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000438 }
439 return bp_sp;
440}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441
442SearchFilterSP
443Target::GetSearchFilterForModule (const FileSpec *containingModule)
444{
445 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 if (containingModule != NULL)
447 {
448 // TODO: We should look into sharing module based search filters
449 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000450 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451 }
452 else
453 {
454 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000455 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 filter_sp = m_search_filter_sp;
457 }
458 return filter_sp;
459}
460
Jim Ingham969795f2011-09-21 01:17:13 +0000461SearchFilterSP
462Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
463{
464 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000465 if (containingModules && containingModules->GetSize() != 0)
466 {
467 // TODO: We should look into sharing module based search filters
468 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000469 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000470 }
471 else
472 {
473 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000474 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000475 filter_sp = m_search_filter_sp;
476 }
477 return filter_sp;
478}
479
Jim Ingham87df91b2011-09-23 00:54:11 +0000480SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000481Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
482 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000483{
484 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
485 return GetSearchFilterForModuleList(containingModules);
486
487 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000488 if (containingModules == NULL)
489 {
490 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
491 // but that will take a little reworking.
492
Greg Claytone1cd1be2012-01-29 20:56:30 +0000493 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000494 }
495 else
496 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000497 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000498 }
499 return filter_sp;
500}
501
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000503Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000504 const FileSpecList *containingSourceFiles,
505 RegularExpression &func_regex,
506 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000507 bool internal,
508 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509{
Jim Ingham87df91b2011-09-23 00:54:11 +0000510 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000511 bool skip =
512 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
513 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000514 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
515 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000516 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517
Jim Ingham1460e4b2014-01-10 23:46:59 +0000518 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519}
520
Jim Ingham219ba192012-03-05 04:47:34 +0000521lldb::BreakpointSP
522Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
523{
524 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
525}
526
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000528Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529{
530 BreakpointSP bp_sp;
531 if (filter_sp && resolver_sp)
532 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000533 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000535 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000537 return bp_sp;
538}
539
540void
541Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
542{
543 if (!bp_sp)
544 return;
545 if (internal)
546 m_internal_breakpoint_list.Add (bp_sp, false);
547 else
548 m_breakpoint_list.Add (bp_sp, true);
549
550 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
551 if (log)
552 {
553 StreamString s;
554 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
555 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
556 }
557
558 bp_sp->ResolveBreakpoint();
559
560 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000561 {
562 m_last_created_breakpoint = bp_sp;
563 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564}
565
Johnny Chen86364b42011-09-20 23:28:55 +0000566bool
567Target::ProcessIsValid()
568{
569 return (m_process_sp && m_process_sp->IsAlive());
570}
571
Johnny Chenb90827e2012-06-04 23:19:54 +0000572static bool
573CheckIfWatchpointsExhausted(Target *target, Error &error)
574{
575 uint32_t num_supported_hardware_watchpoints;
576 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
577 if (rc.Success())
578 {
579 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
580 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
581 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
582 num_supported_hardware_watchpoints);
583 }
584 return false;
585}
586
Johnny Chen01a67862011-10-14 00:42:25 +0000587// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000588// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000589WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000590Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000591{
Greg Clayton5160ce52013-03-27 23:08:40 +0000592 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000593 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000594 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000595 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000596
Johnny Chen01a67862011-10-14 00:42:25 +0000597 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000598 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000599 {
600 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000601 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000602 }
Jim Inghamc6462312013-06-18 21:52:48 +0000603
Johnny Chen45e541f2011-09-14 22:20:15 +0000604 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000605 {
606 if (size == 0)
607 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
608 else
Daniel Malead01b2952012-11-29 21:49:15 +0000609 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000610 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000611 }
Jim Inghamc6462312013-06-18 21:52:48 +0000612
613 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
614 {
615 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
616 }
Johnny Chen7313a642011-09-13 01:15:36 +0000617
Johnny Chen01a67862011-10-14 00:42:25 +0000618 // Currently we only support one watchpoint per address, with total number
619 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000620
621 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000622 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 +0000623 Mutex::Locker locker;
624 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000625 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000626 if (matched_sp)
627 {
Johnny Chen0c406372011-09-14 20:23:45 +0000628 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000629 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000630 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
631 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000632 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000633 if (size == old_size && kind == old_type)
634 {
Johnny Chen01a67862011-10-14 00:42:25 +0000635 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000636 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000637 }
638 else
639 {
Johnny Chen01a67862011-10-14 00:42:25 +0000640 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000641 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
642 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000643 }
Johnny Chen3c532582011-09-13 23:29:31 +0000644 }
645
Jason Molenda727e3922012-12-05 23:07:34 +0000646 if (!wp_sp)
647 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000648 wp_sp.reset(new Watchpoint(*this, addr, size, type));
649 wp_sp->SetWatchpointType(kind, notify);
650 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000651 }
Johnny Chen0c406372011-09-14 20:23:45 +0000652
Jim Ingham1b5792e2012-12-18 02:03:49 +0000653 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000654 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000655 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
656 __FUNCTION__,
657 error.Success() ? "succeeded" : "failed",
658 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000659
Jason Molenda727e3922012-12-05 23:07:34 +0000660 if (error.Fail())
661 {
Johnny Chen41b77262012-03-26 22:00:10 +0000662 // Enabling the watchpoint on the device side failed.
663 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000664 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000665 // See if we could provide more helpful error message.
666 if (!CheckIfWatchpointsExhausted(this, error))
667 {
668 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000669 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000670 }
Johnny Chen01a67862011-10-14 00:42:25 +0000671 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000672 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000673 else
Johnny Chen01a67862011-10-14 00:42:25 +0000674 m_last_created_watchpoint = wp_sp;
675 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000676}
677
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678void
679Target::RemoveAllBreakpoints (bool internal_also)
680{
Greg Clayton5160ce52013-03-27 23:08:40 +0000681 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682 if (log)
683 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
684
Greg Clayton9fed0d82010-07-23 23:33:17 +0000685 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000687 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000688
689 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690}
691
692void
693Target::DisableAllBreakpoints (bool internal_also)
694{
Greg Clayton5160ce52013-03-27 23:08:40 +0000695 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 if (log)
697 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
698
699 m_breakpoint_list.SetEnabledAll (false);
700 if (internal_also)
701 m_internal_breakpoint_list.SetEnabledAll (false);
702}
703
704void
705Target::EnableAllBreakpoints (bool internal_also)
706{
Greg Clayton5160ce52013-03-27 23:08:40 +0000707 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 if (log)
709 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
710
711 m_breakpoint_list.SetEnabledAll (true);
712 if (internal_also)
713 m_internal_breakpoint_list.SetEnabledAll (true);
714}
715
716bool
717Target::RemoveBreakpointByID (break_id_t break_id)
718{
Greg Clayton5160ce52013-03-27 23:08:40 +0000719 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720 if (log)
721 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
722
723 if (DisableBreakpointByID (break_id))
724 {
725 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000726 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000728 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000729 if (m_last_created_breakpoint)
730 {
731 if (m_last_created_breakpoint->GetID() == break_id)
732 m_last_created_breakpoint.reset();
733 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000734 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000735 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 return true;
737 }
738 return false;
739}
740
741bool
742Target::DisableBreakpointByID (break_id_t break_id)
743{
Greg Clayton5160ce52013-03-27 23:08:40 +0000744 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 if (log)
746 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
747
748 BreakpointSP bp_sp;
749
750 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
751 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
752 else
753 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
754 if (bp_sp)
755 {
756 bp_sp->SetEnabled (false);
757 return true;
758 }
759 return false;
760}
761
762bool
763Target::EnableBreakpointByID (break_id_t break_id)
764{
Greg Clayton5160ce52013-03-27 23:08:40 +0000765 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766 if (log)
767 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
768 __FUNCTION__,
769 break_id,
770 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
771
772 BreakpointSP bp_sp;
773
774 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
775 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
776 else
777 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
778
779 if (bp_sp)
780 {
781 bp_sp->SetEnabled (true);
782 return true;
783 }
784 return false;
785}
786
Johnny Chenedf50372011-09-23 21:21:43 +0000787// The flag 'end_to_end', default to true, signifies that the operation is
788// performed end to end, for both the debugger and the debuggee.
789
Johnny Chen01a67862011-10-14 00:42:25 +0000790// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
791// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000792bool
Johnny Chen01a67862011-10-14 00:42:25 +0000793Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000794{
Greg Clayton5160ce52013-03-27 23:08:40 +0000795 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000796 if (log)
797 log->Printf ("Target::%s\n", __FUNCTION__);
798
Johnny Chenedf50372011-09-23 21:21:43 +0000799 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000800 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000801 return true;
802 }
803
804 // Otherwise, it's an end to end operation.
805
Johnny Chen86364b42011-09-20 23:28:55 +0000806 if (!ProcessIsValid())
807 return false;
808
Johnny Chen01a67862011-10-14 00:42:25 +0000809 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000810 for (size_t i = 0; i < num_watchpoints; ++i)
811 {
Johnny Chen01a67862011-10-14 00:42:25 +0000812 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
813 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000814 return false;
815
Johnny Chen01a67862011-10-14 00:42:25 +0000816 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000817 if (rc.Fail())
818 return false;
819 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000820 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000821 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000822 return true; // Success!
823}
824
Johnny Chen01a67862011-10-14 00:42:25 +0000825// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
826// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000827bool
Johnny Chen01a67862011-10-14 00:42:25 +0000828Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000829{
Greg Clayton5160ce52013-03-27 23:08:40 +0000830 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000831 if (log)
832 log->Printf ("Target::%s\n", __FUNCTION__);
833
Johnny Chenedf50372011-09-23 21:21:43 +0000834 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000835 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000836 return true;
837 }
838
839 // Otherwise, it's an end to end operation.
840
Johnny Chen86364b42011-09-20 23:28:55 +0000841 if (!ProcessIsValid())
842 return false;
843
Johnny Chen01a67862011-10-14 00:42:25 +0000844 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000845 for (size_t i = 0; i < num_watchpoints; ++i)
846 {
Johnny Chen01a67862011-10-14 00:42:25 +0000847 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
848 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000849 return false;
850
Johnny Chen01a67862011-10-14 00:42:25 +0000851 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000852 if (rc.Fail())
853 return false;
854 }
Johnny Chen86364b42011-09-20 23:28:55 +0000855 return true; // Success!
856}
857
Johnny Chen01a67862011-10-14 00:42:25 +0000858// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
859// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000860bool
Johnny Chen01a67862011-10-14 00:42:25 +0000861Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000862{
Greg Clayton5160ce52013-03-27 23:08:40 +0000863 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000864 if (log)
865 log->Printf ("Target::%s\n", __FUNCTION__);
866
Johnny Chenedf50372011-09-23 21:21:43 +0000867 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000868 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000869 return true;
870 }
871
872 // Otherwise, it's an end to end operation.
873
Johnny Chen86364b42011-09-20 23:28:55 +0000874 if (!ProcessIsValid())
875 return false;
876
Johnny Chen01a67862011-10-14 00:42:25 +0000877 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000878 for (size_t i = 0; i < num_watchpoints; ++i)
879 {
Johnny Chen01a67862011-10-14 00:42:25 +0000880 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
881 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000882 return false;
883
Johnny Chen01a67862011-10-14 00:42:25 +0000884 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000885 if (rc.Fail())
886 return false;
887 }
Johnny Chen86364b42011-09-20 23:28:55 +0000888 return true; // Success!
889}
890
Johnny Chena4d6bc92012-02-25 06:44:30 +0000891// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
892bool
893Target::ClearAllWatchpointHitCounts ()
894{
Greg Clayton5160ce52013-03-27 23:08:40 +0000895 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000896 if (log)
897 log->Printf ("Target::%s\n", __FUNCTION__);
898
899 size_t num_watchpoints = m_watchpoint_list.GetSize();
900 for (size_t i = 0; i < num_watchpoints; ++i)
901 {
902 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
903 if (!wp_sp)
904 return false;
905
906 wp_sp->ResetHitCount();
907 }
908 return true; // Success!
909}
910
Enrico Granata5e3fe042015-02-11 00:37:54 +0000911// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
912bool
913Target::ClearAllWatchpointHistoricValues ()
914{
915 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
916 if (log)
917 log->Printf ("Target::%s\n", __FUNCTION__);
918
919 size_t num_watchpoints = m_watchpoint_list.GetSize();
920 for (size_t i = 0; i < num_watchpoints; ++i)
921 {
922 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
923 if (!wp_sp)
924 return false;
925
926 wp_sp->ResetHistoricValues();
927 }
928 return true; // Success!
929}
930
Johnny Chen01a67862011-10-14 00:42:25 +0000931// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000932// during these operations.
933bool
Johnny Chen01a67862011-10-14 00:42:25 +0000934Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000935{
Greg Clayton5160ce52013-03-27 23:08:40 +0000936 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000937 if (log)
938 log->Printf ("Target::%s\n", __FUNCTION__);
939
940 if (!ProcessIsValid())
941 return false;
942
Johnny Chen01a67862011-10-14 00:42:25 +0000943 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000944 for (size_t i = 0; i < num_watchpoints; ++i)
945 {
Johnny Chen01a67862011-10-14 00:42:25 +0000946 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
947 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000948 return false;
949
Johnny Chen01a67862011-10-14 00:42:25 +0000950 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000951 }
952 return true; // Success!
953}
954
Johnny Chen01a67862011-10-14 00:42:25 +0000955// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000956bool
Johnny Chen01a67862011-10-14 00:42:25 +0000957Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000958{
Greg Clayton5160ce52013-03-27 23:08:40 +0000959 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000960 if (log)
961 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
962
963 if (!ProcessIsValid())
964 return false;
965
Johnny Chen01a67862011-10-14 00:42:25 +0000966 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
967 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000968 {
Johnny Chen01a67862011-10-14 00:42:25 +0000969 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000970 if (rc.Success())
971 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000972
Johnny Chenf04ee932011-09-22 18:04:58 +0000973 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000974 }
975 return false;
976}
977
Johnny Chen01a67862011-10-14 00:42:25 +0000978// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000979bool
Johnny Chen01a67862011-10-14 00:42:25 +0000980Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000981{
Greg Clayton5160ce52013-03-27 23:08:40 +0000982 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000983 if (log)
984 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
985
986 if (!ProcessIsValid())
987 return false;
988
Johnny Chen01a67862011-10-14 00:42:25 +0000989 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
990 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000991 {
Johnny Chen01a67862011-10-14 00:42:25 +0000992 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000993 if (rc.Success())
994 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000995
Johnny Chenf04ee932011-09-22 18:04:58 +0000996 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000997 }
998 return false;
999}
1000
Johnny Chen01a67862011-10-14 00:42:25 +00001001// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001002bool
Johnny Chen01a67862011-10-14 00:42:25 +00001003Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001004{
Greg Clayton5160ce52013-03-27 23:08:40 +00001005 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001006 if (log)
1007 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1008
Jim Inghamb0b45132013-07-02 02:09:46 +00001009 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1010 if (watch_to_remove_sp == m_last_created_watchpoint)
1011 m_last_created_watchpoint.reset();
1012
Johnny Chen01a67862011-10-14 00:42:25 +00001013 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001014 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001015 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001016 return true;
1017 }
1018 return false;
1019}
1020
Johnny Chen01a67862011-10-14 00:42:25 +00001021// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001022bool
Johnny Chen01a67862011-10-14 00:42:25 +00001023Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001024{
Greg Clayton5160ce52013-03-27 23:08:40 +00001025 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001026 if (log)
1027 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1028
1029 if (!ProcessIsValid())
1030 return false;
1031
Johnny Chen01a67862011-10-14 00:42:25 +00001032 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1033 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001034 {
Johnny Chen01a67862011-10-14 00:42:25 +00001035 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001036 return true;
1037 }
1038 return false;
1039}
1040
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041ModuleSP
1042Target::GetExecutableModule ()
1043{
Greg Claytonaa149cb2011-08-11 02:48:45 +00001044 return m_images.GetModuleAtIndex(0);
1045}
1046
1047Module*
1048Target::GetExecutableModulePointer ()
1049{
1050 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051}
1052
Enrico Granata17598482012-11-08 02:22:02 +00001053static void
1054LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1055{
1056 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001057 StreamString feedback_stream;
1058 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001059 {
Enrico Granata97303392013-05-21 00:00:30 +00001060 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001061 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001062 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1063 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001064 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001065 if (feedback_stream.GetSize())
1066 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1067 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001068}
1069
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070void
Greg Claytonb35db632013-11-09 00:03:31 +00001071Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072{
Greg Claytonb35db632013-11-09 00:03:31 +00001073 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001074 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075 m_images.Clear();
1076 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001077 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001078 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001079}
1080
1081void
Greg Claytonb35db632013-11-09 00:03:31 +00001082Target::DidExec ()
1083{
1084 // When a process exec's we need to know about it so we can do some cleanup.
1085 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1086 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1087}
1088
1089void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001090Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1091{
1092 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001093 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094
1095 if (executable_sp.get())
1096 {
1097 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001098 "Target::SetExecutableModule (executable = '%s')",
1099 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001101 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102
Jim Ingham5aee1622010-08-09 23:31:02 +00001103 // 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 +00001104 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001105 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001106 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001107 if (log)
1108 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1109 }
1110
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001112 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001113
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001114 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115 {
1116 executable_objfile->GetDependentModules(dependent_files);
1117 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1118 {
Greg Claytonded470d2011-03-19 01:12:21 +00001119 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1120 FileSpec platform_dependent_file_spec;
1121 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001122 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001123 else
1124 platform_dependent_file_spec = dependent_file_spec;
1125
Greg Claytonb9a01b32012-02-26 05:51:37 +00001126 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1127 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128 if (image_module_sp.get())
1129 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130 ObjectFile *objfile = image_module_sp->GetObjectFile();
1131 if (objfile)
1132 objfile->GetDependentModules(dependent_files);
1133 }
1134 }
1135 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136 }
1137}
1138
1139
Jim Ingham5aee1622010-08-09 23:31:02 +00001140bool
1141Target::SetArchitecture (const ArchSpec &arch_spec)
1142{
Greg Clayton5160ce52013-03-27 23:08:40 +00001143 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001144 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001145 {
Greg Clayton70512312012-05-08 01:45:38 +00001146 // If we haven't got a valid arch spec, or the architectures are
1147 // compatible, so just update the architecture. Architectures can be
1148 // equal, yet the triple OS and vendor might change, so we need to do
1149 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001150 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001151 if (log)
1152 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 +00001153 return true;
1154 }
1155 else
1156 {
1157 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001158 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001159 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 +00001160 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001161 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001162
Greg Claytonb35db632013-11-09 00:03:31 +00001163 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001164 // Need to do something about unsetting breakpoints.
1165
1166 if (executable_sp)
1167 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001168 if (log)
1169 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 +00001170 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1171 Error error = ModuleList::GetSharedModule (module_spec,
1172 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001173 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001174 NULL,
1175 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001176
1177 if (!error.Fail() && executable_sp)
1178 {
1179 SetExecutableModule (executable_sp, true);
1180 return true;
1181 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001182 }
1183 }
Greg Clayton70512312012-05-08 01:45:38 +00001184 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001185}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187void
Enrico Granataefe637d2012-11-08 19:16:03 +00001188Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001189{
1190}
1191
1192void
Enrico Granataefe637d2012-11-08 19:16:03 +00001193Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194{
1195 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001196 if (m_valid)
1197 {
1198 ModuleList my_module_list;
1199 my_module_list.Append(module_sp);
1200 LoadScriptingResourceForModule(module_sp, this);
1201 ModulesDidLoad (my_module_list);
1202 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203}
1204
1205void
Enrico Granataefe637d2012-11-08 19:16:03 +00001206Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001207{
1208 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001209 if (m_valid)
1210 {
1211 ModuleList my_module_list;
1212 my_module_list.Append(module_sp);
1213 ModulesDidUnload (my_module_list, false);
1214 }
Enrico Granata17598482012-11-08 02:22:02 +00001215}
1216
1217void
Enrico Granataefe637d2012-11-08 19:16:03 +00001218Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219{
Jim Inghame716ae02011-08-03 01:00:06 +00001220 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001221 if (m_valid)
1222 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001223}
1224
1225void
1226Target::ModulesDidLoad (ModuleList &module_list)
1227{
Greg Clayton23f8c952014-03-24 23:10:19 +00001228 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001229 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001230 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001231 if (m_process_sp)
1232 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001233 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001234 }
Enrico Granata17598482012-11-08 02:22:02 +00001235 // TODO: make event data that packages up the module_list
1236 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1237 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238}
1239
1240void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001241Target::SymbolsDidLoad (ModuleList &module_list)
1242{
Greg Clayton23f8c952014-03-24 23:10:19 +00001243 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001244 {
Jim Ingham31caf982013-06-04 23:01:35 +00001245 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001246 {
Jim Ingham31caf982013-06-04 23:01:35 +00001247 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1248 if (runtime)
1249 {
1250 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1251 objc_runtime->SymbolsDidLoad(module_list);
1252 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001253 }
Jim Ingham31caf982013-06-04 23:01:35 +00001254
Greg Clayton095eeaa2013-11-05 23:28:00 +00001255 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jim Ingham31caf982013-06-04 23:01:35 +00001256 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001257 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001258}
1259
1260void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001261Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262{
Greg Clayton23f8c952014-03-24 23:10:19 +00001263 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001264 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001265 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001266 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Enrico Granata17598482012-11-08 02:22:02 +00001267 // TODO: make event data that packages up the module_list
1268 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1269 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270}
1271
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001272bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001273Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001274{
Greg Clayton67cc0632012-08-22 17:17:09 +00001275 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001276 {
1277 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001278 ModuleSpec module_spec (module_file_spec);
1279 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001280
1281 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1282 // black list.
1283 if (num_modules > 0)
1284 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001285 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001286 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001287 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001288 return false;
1289 }
1290 return true;
1291 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001292 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001293 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001294}
1295
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001296bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001297Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001298{
Greg Clayton67cc0632012-08-22 17:17:09 +00001299 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001300 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001301 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001302 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001303 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001304 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001305}
1306
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307size_t
Greg Claytondb598232011-01-07 01:57:07 +00001308Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1309{
Greg Claytone72dfb32012-02-24 01:59:29 +00001310 SectionSP section_sp (addr.GetSection());
1311 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001312 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001313 // 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 +00001314 if (section_sp->IsEncrypted())
1315 {
Greg Clayton57f06302012-05-25 17:05:55 +00001316 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001317 return 0;
1318 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001319 ModuleSP module_sp (section_sp->GetModule());
1320 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001321 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001322 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1323 if (objfile)
1324 {
1325 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1326 addr.GetOffset(),
1327 dst,
1328 dst_len);
1329 if (bytes_read > 0)
1330 return bytes_read;
1331 else
1332 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1333 }
Greg Claytondb598232011-01-07 01:57:07 +00001334 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001335 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001336 }
1337 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001338 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001339 }
1340 else
Greg Claytondb598232011-01-07 01:57:07 +00001341 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001342
Greg Claytondb598232011-01-07 01:57:07 +00001343 return 0;
1344}
1345
1346size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001347Target::ReadMemory (const Address& addr,
1348 bool prefer_file_cache,
1349 void *dst,
1350 size_t dst_len,
1351 Error &error,
1352 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001353{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001355
Enrico Granata9128ee22011-09-06 19:20:51 +00001356 // if we end up reading this from process memory, we will fill this
1357 // with the actual load address
1358 if (load_addr_ptr)
1359 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1360
Greg Claytondb598232011-01-07 01:57:07 +00001361 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001362
1363 addr_t load_addr = LLDB_INVALID_ADDRESS;
1364 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001365 Address resolved_addr;
1366 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001367 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001368 SectionLoadList &section_load_list = GetSectionLoadList();
1369 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001370 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001371 // No sections are loaded, so we must assume we are not running
1372 // yet and anything we are given is a file address.
1373 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1374 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001375 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001376 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001377 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001378 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001379 // we have manually loaded some sections with "target modules load ..."
1380 // or because we have have a live process that has sections loaded
1381 // through the dynamic loader
1382 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001383 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001384 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001385 }
Greg Clayton357132e2011-03-26 19:14:58 +00001386 if (!resolved_addr.IsValid())
1387 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001388
Greg Claytonc749eb82011-07-11 05:12:02 +00001389
Greg Claytondb598232011-01-07 01:57:07 +00001390 if (prefer_file_cache)
1391 {
1392 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1393 if (bytes_read > 0)
1394 return bytes_read;
1395 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001396
Johnny Chen86364b42011-09-20 23:28:55 +00001397 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001398 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001399 if (load_addr == LLDB_INVALID_ADDRESS)
1400 load_addr = resolved_addr.GetLoadAddress (this);
1401
Greg Claytondda4f7b2010-06-30 23:03:03 +00001402 if (load_addr == LLDB_INVALID_ADDRESS)
1403 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001404 ModuleSP addr_module_sp (resolved_addr.GetModule());
1405 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001406 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001407 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001408 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001409 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001410 else
Daniel Malead01b2952012-11-29 21:49:15 +00001411 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001412 }
1413 else
1414 {
Greg Claytondb598232011-01-07 01:57:07 +00001415 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001416 if (bytes_read != dst_len)
1417 {
1418 if (error.Success())
1419 {
1420 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001421 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001422 else
Daniel Malead01b2952012-11-29 21:49:15 +00001423 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 +00001424 }
1425 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001426 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001427 {
1428 if (load_addr_ptr)
1429 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001430 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001431 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001432 // If the address is not section offset we have an address that
1433 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001434 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001435 // more we can do. If it is section offset, we might be able to
1436 // read cached memory from the object file.
1437 if (!resolved_addr.IsSectionOffset())
1438 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001441
Greg Claytonc749eb82011-07-11 05:12:02 +00001442 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001443 {
Greg Claytondb598232011-01-07 01:57:07 +00001444 // If we didn't already try and read from the object file cache, then
1445 // try it after failing to read from the process.
1446 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001447 }
1448 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001449}
1450
Greg Claytond16e1e52011-07-12 17:06:17 +00001451size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001452Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1453{
1454 char buf[256];
1455 out_str.clear();
1456 addr_t curr_addr = addr.GetLoadAddress(this);
1457 Address address(addr);
1458 while (1)
1459 {
1460 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1461 if (length == 0)
1462 break;
1463 out_str.append(buf, length);
1464 // If we got "length - 1" bytes, we didn't get the whole C string, we
1465 // need to read some more characters
1466 if (length == sizeof(buf) - 1)
1467 curr_addr += length;
1468 else
1469 break;
1470 address = Address(curr_addr);
1471 }
1472 return out_str.size();
1473}
1474
1475
1476size_t
1477Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1478{
1479 size_t total_cstr_len = 0;
1480 if (dst && dst_max_len)
1481 {
1482 result_error.Clear();
1483 // NULL out everything just to be safe
1484 memset (dst, 0, dst_max_len);
1485 Error error;
1486 addr_t curr_addr = addr.GetLoadAddress(this);
1487 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001488
1489 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1490 // think this really needs to be tied to the memory cache subsystem's
1491 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001492 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001493
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001494 size_t bytes_left = dst_max_len - 1;
1495 char *curr_dst = dst;
1496
1497 while (bytes_left > 0)
1498 {
1499 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1500 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1501 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1502
1503 if (bytes_read == 0)
1504 {
1505 result_error = error;
1506 dst[total_cstr_len] = '\0';
1507 break;
1508 }
1509 const size_t len = strlen(curr_dst);
1510
1511 total_cstr_len += len;
1512
1513 if (len < bytes_to_read)
1514 break;
1515
1516 curr_dst += bytes_read;
1517 curr_addr += bytes_read;
1518 bytes_left -= bytes_read;
1519 address = Address(curr_addr);
1520 }
1521 }
1522 else
1523 {
1524 if (dst == NULL)
1525 result_error.SetErrorString("invalid arguments");
1526 else
1527 result_error.Clear();
1528 }
1529 return total_cstr_len;
1530}
1531
1532size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001533Target::ReadScalarIntegerFromMemory (const Address& addr,
1534 bool prefer_file_cache,
1535 uint32_t byte_size,
1536 bool is_signed,
1537 Scalar &scalar,
1538 Error &error)
1539{
1540 uint64_t uval;
1541
1542 if (byte_size <= sizeof(uval))
1543 {
1544 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1545 if (bytes_read == byte_size)
1546 {
1547 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001548 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001549 if (byte_size <= 4)
1550 scalar = data.GetMaxU32 (&offset, byte_size);
1551 else
1552 scalar = data.GetMaxU64 (&offset, byte_size);
1553
1554 if (is_signed)
1555 scalar.SignExtend(byte_size * 8);
1556 return bytes_read;
1557 }
1558 }
1559 else
1560 {
1561 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1562 }
1563 return 0;
1564}
1565
1566uint64_t
1567Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1568 bool prefer_file_cache,
1569 size_t integer_byte_size,
1570 uint64_t fail_value,
1571 Error &error)
1572{
1573 Scalar scalar;
1574 if (ReadScalarIntegerFromMemory (addr,
1575 prefer_file_cache,
1576 integer_byte_size,
1577 false,
1578 scalar,
1579 error))
1580 return scalar.ULongLong(fail_value);
1581 return fail_value;
1582}
1583
1584bool
1585Target::ReadPointerFromMemory (const Address& addr,
1586 bool prefer_file_cache,
1587 Error &error,
1588 Address &pointer_addr)
1589{
1590 Scalar scalar;
1591 if (ReadScalarIntegerFromMemory (addr,
1592 prefer_file_cache,
1593 m_arch.GetAddressByteSize(),
1594 false,
1595 scalar,
1596 error))
1597 {
1598 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1599 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1600 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001601 SectionLoadList &section_load_list = GetSectionLoadList();
1602 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001603 {
1604 // No sections are loaded, so we must assume we are not running
1605 // yet and anything we are given is a file address.
1606 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1607 }
1608 else
1609 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001610 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001611 // we have manually loaded some sections with "target modules load ..."
1612 // or because we have have a live process that has sections loaded
1613 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001614 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001615 }
1616 // We weren't able to resolve the pointer value, so just return
1617 // an address with no section
1618 if (!pointer_addr.IsValid())
1619 pointer_addr.SetOffset (pointer_vm_addr);
1620 return true;
1621
1622 }
1623 }
1624 return false;
1625}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001626
1627ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001628Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001629{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630 ModuleSP module_sp;
1631
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001632 Error error;
1633
Jim Ingham4a94c912012-05-17 18:38:42 +00001634 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1635 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001636
Jim Ingham4a94c912012-05-17 18:38:42 +00001637 if (module_spec.GetUUID().IsValid())
1638 module_sp = m_images.FindFirstModule(module_spec);
1639
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001640 if (!module_sp)
1641 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001642 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1643 bool did_create_module = false;
1644
1645 // If there are image search path entries, try to use them first to acquire a suitable image.
1646 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001647 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001648 ModuleSpec transformed_spec (module_spec);
1649 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1650 {
1651 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1652 error = ModuleList::GetSharedModule (transformed_spec,
1653 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001654 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001655 &old_module_sp,
1656 &did_create_module);
1657 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001658 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001659
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001660 if (!module_sp)
1661 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001662 // If we have a UUID, we can check our global shared module list in case
1663 // we already have it. If we don't have a valid UUID, then we can't since
1664 // the path in "module_spec" will be a platform path, and we will need to
1665 // let the platform find that file. For example, we could be asking for
1666 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1667 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1668 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1669 // cache.
1670 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001671 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001672 // We have a UUID, it is OK to check the global module list...
1673 error = ModuleList::GetSharedModule (module_spec,
1674 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001675 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001676 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001677 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001678 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001679
1680 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001681 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001682 // The platform is responsible for finding and caching an appropriate
1683 // module in the shared module cache.
1684 if (m_platform_sp)
1685 {
1686 FileSpec platform_file_spec;
1687 error = m_platform_sp->GetSharedModule (module_spec,
1688 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001689 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001690 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001691 &did_create_module);
1692 }
1693 else
1694 {
1695 error.SetErrorString("no platform is currently set");
1696 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001697 }
1698 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699
Jim Ingham4a94c912012-05-17 18:38:42 +00001700 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1701 // module in the list already, and if there was, let's remove it.
1702 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001704 ObjectFile *objfile = module_sp->GetObjectFile();
1705 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001706 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001707 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001708 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001709 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1710 case ObjectFile::eTypeExecutable: /// A normal executable
1711 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1712 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1713 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1714 break;
1715 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1716 if (error_ptr)
1717 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1718 return ModuleSP();
1719 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1720 if (error_ptr)
1721 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1722 return ModuleSP();
1723 default:
1724 if (error_ptr)
1725 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1726 return ModuleSP();
1727 }
1728 // GetSharedModule is not guaranteed to find the old shared module, for instance
1729 // in the common case where you pass in the UUID, it is only going to find the one
1730 // module matching the UUID. In fact, it has no good way to know what the "old module"
1731 // relevant to this target is, since there might be many copies of a module with this file spec
1732 // in various running debug sessions, but only one of them will belong to this target.
1733 // So let's remove the UUID from the module list, and look in the target's module list.
1734 // Only do this if there is SOMETHING else in the module spec...
1735 if (!old_module_sp)
1736 {
1737 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001738 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001739 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1740 module_spec_copy.GetUUID().Clear();
1741
1742 ModuleList found_modules;
1743 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1744 if (num_found == 1)
1745 {
1746 old_module_sp = found_modules.GetModuleAtIndex(0);
1747 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001748 }
1749 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001750
1751 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1752 {
1753 m_images.ReplaceModule(old_module_sp, module_sp);
1754 Module *old_module_ptr = old_module_sp.get();
1755 old_module_sp.reset();
1756 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1757 }
1758 else
1759 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001760 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001761 else
1762 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001763 }
1764 }
1765 if (error_ptr)
1766 *error_ptr = error;
1767 return module_sp;
1768}
1769
1770
Greg Claytond9e416c2012-02-18 05:35:26 +00001771TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772Target::CalculateTarget ()
1773{
Greg Claytond9e416c2012-02-18 05:35:26 +00001774 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775}
1776
Greg Claytond9e416c2012-02-18 05:35:26 +00001777ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001778Target::CalculateProcess ()
1779{
Greg Claytond9e416c2012-02-18 05:35:26 +00001780 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001781}
1782
Greg Claytond9e416c2012-02-18 05:35:26 +00001783ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001784Target::CalculateThread ()
1785{
Greg Claytond9e416c2012-02-18 05:35:26 +00001786 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001787}
1788
Jason Molendab57e4a12013-11-04 09:33:30 +00001789StackFrameSP
1790Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001791{
Jason Molendab57e4a12013-11-04 09:33:30 +00001792 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001793}
1794
1795void
Greg Clayton0603aa92010-10-04 01:05:56 +00001796Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001797{
Greg Claytonc14ee322011-09-22 04:58:26 +00001798 exe_ctx.Clear();
1799 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800}
1801
1802PathMappingList &
1803Target::GetImageSearchPathList ()
1804{
1805 return m_image_search_paths;
1806}
1807
1808void
1809Target::ImageSearchPathsChanged
1810(
1811 const PathMappingList &path_list,
1812 void *baton
1813)
1814{
1815 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001816 ModuleSP exe_module_sp (target->GetExecutableModule());
1817 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001818 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819}
1820
1821ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001822Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001823{
Greg Clayton73da2442011-08-03 01:23:55 +00001824 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001825 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001826 {
Greg Clayton73da2442011-08-03 01:23:55 +00001827 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001828 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001829 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001830 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001831 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1832 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001833 return m_scratch_ast_context_ap.get();
1834}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001835
Sean Callanan686b2312011-11-16 18:20:47 +00001836ClangASTImporter *
1837Target::GetClangASTImporter()
1838{
1839 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1840
1841 if (!ast_importer)
1842 {
1843 ast_importer = new ClangASTImporter();
1844 m_ast_importer_ap.reset(ast_importer);
1845 }
1846
1847 return ast_importer;
1848}
1849
Greg Clayton99d0faf2010-11-18 23:32:35 +00001850void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001851Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001852{
Greg Clayton6920b522012-08-22 18:39:03 +00001853 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001854}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001855
Greg Clayton99d0faf2010-11-18 23:32:35 +00001856void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001857Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001858{
Greg Clayton6920b522012-08-22 18:39:03 +00001859 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001860}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001861
Greg Claytonc859e2d2012-02-13 23:10:39 +00001862FileSpecList
1863Target::GetDefaultExecutableSearchPaths ()
1864{
Greg Clayton67cc0632012-08-22 17:17:09 +00001865 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1866 if (properties_sp)
1867 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001868 return FileSpecList();
1869}
1870
Michael Sartaina7499c92013-07-01 19:45:50 +00001871FileSpecList
1872Target::GetDefaultDebugFileSearchPaths ()
1873{
1874 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1875 if (properties_sp)
1876 return properties_sp->GetDebugFileSearchPaths();
1877 return FileSpecList();
1878}
1879
Caroline Ticedaccaa92010-09-20 20:44:43 +00001880ArchSpec
1881Target::GetDefaultArchitecture ()
1882{
Greg Clayton67cc0632012-08-22 17:17:09 +00001883 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1884 if (properties_sp)
1885 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001886 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001887}
1888
1889void
Greg Clayton67cc0632012-08-22 17:17:09 +00001890Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001891{
Greg Clayton67cc0632012-08-22 17:17:09 +00001892 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1893 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001894 {
1895 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 +00001896 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001897 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001898}
1899
Greg Clayton0603aa92010-10-04 01:05:56 +00001900Target *
1901Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1902{
1903 // The target can either exist in the "process" of ExecutionContext, or in
1904 // the "target_sp" member of SymbolContext. This accessor helper function
1905 // will get the target from one of these locations.
1906
1907 Target *target = NULL;
1908 if (sc_ptr != NULL)
1909 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001910 if (target == NULL && exe_ctx_ptr)
1911 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001912 return target;
1913}
1914
Jim Ingham1624a2d2014-05-05 02:26:40 +00001915ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001916Target::EvaluateExpression
1917(
1918 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001919 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001920 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001921 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001922)
1923{
Enrico Granata97fca502012-09-18 17:43:16 +00001924 result_valobj_sp.reset();
1925
Jim Ingham8646d3c2014-05-05 02:47:44 +00001926 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001927
Greg Claytond1767f02011-12-08 02:13:16 +00001928 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1929 return execution_results;
1930
Jim Ingham6026ca32011-05-12 02:06:14 +00001931 // We shouldn't run stop hooks in expressions.
1932 // Be sure to reset this if you return anywhere within this function.
1933 bool old_suppress_value = m_suppress_stop_hooks;
1934 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001935
1936 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001937
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001938 if (frame)
1939 {
1940 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001941 }
1942 else if (m_process_sp)
1943 {
1944 m_process_sp->CalculateExecutionContext(exe_ctx);
1945 }
1946 else
1947 {
1948 CalculateExecutionContext(exe_ctx);
1949 }
1950
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001951 // Make sure we aren't just trying to see the value of a persistent
1952 // variable (something like "$0")
1953 lldb::ClangExpressionVariableSP persistent_var_sp;
1954 // Only check for persistent variables the expression starts with a '$'
1955 if (expr_cstr[0] == '$')
1956 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1957
1958 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001959 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001960 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00001961 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001962 }
1963 else
1964 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001965 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00001966 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001967 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001968 options,
1969 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001970 prefix,
1971 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001972 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001973 }
Jim Ingham6026ca32011-05-12 02:06:14 +00001974
1975 m_suppress_stop_hooks = old_suppress_value;
1976
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001977 return execution_results;
1978}
1979
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001980lldb::addr_t
1981Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1982{
1983 addr_t code_addr = load_addr;
1984 switch (m_arch.GetMachine())
1985 {
1986 case llvm::Triple::arm:
1987 case llvm::Triple::thumb:
1988 switch (addr_class)
1989 {
1990 case eAddressClassData:
1991 case eAddressClassDebug:
1992 return LLDB_INVALID_ADDRESS;
1993
1994 case eAddressClassUnknown:
1995 case eAddressClassInvalid:
1996 case eAddressClassCode:
1997 case eAddressClassCodeAlternateISA:
1998 case eAddressClassRuntime:
1999 // Check if bit zero it no set?
2000 if ((code_addr & 1ull) == 0)
2001 {
2002 // Bit zero isn't set, check if the address is a multiple of 2?
2003 if (code_addr & 2ull)
2004 {
2005 // The address is a multiple of 2 so it must be thumb, set bit zero
2006 code_addr |= 1ull;
2007 }
2008 else if (addr_class == eAddressClassCodeAlternateISA)
2009 {
2010 // We checked the address and the address claims to be the alternate ISA
2011 // which means thumb, so set bit zero.
2012 code_addr |= 1ull;
2013 }
2014 }
2015 break;
2016 }
2017 break;
2018
2019 default:
2020 break;
2021 }
2022 return code_addr;
2023}
2024
2025lldb::addr_t
2026Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2027{
2028 addr_t opcode_addr = load_addr;
2029 switch (m_arch.GetMachine())
2030 {
2031 case llvm::Triple::arm:
2032 case llvm::Triple::thumb:
2033 switch (addr_class)
2034 {
2035 case eAddressClassData:
2036 case eAddressClassDebug:
2037 return LLDB_INVALID_ADDRESS;
2038
2039 case eAddressClassInvalid:
2040 case eAddressClassUnknown:
2041 case eAddressClassCode:
2042 case eAddressClassCodeAlternateISA:
2043 case eAddressClassRuntime:
2044 opcode_addr &= ~(1ull);
2045 break;
2046 }
2047 break;
2048
2049 default:
2050 break;
2051 }
2052 return opcode_addr;
2053}
2054
Greg Clayton9585fbf2013-03-19 00:20:55 +00002055SourceManager &
2056Target::GetSourceManager ()
2057{
2058 if (m_source_manager_ap.get() == NULL)
2059 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2060 return *m_source_manager_ap;
2061}
2062
Sean Callanan9998acd2014-12-05 01:21:59 +00002063ClangModulesDeclVendor *
2064Target::GetClangModulesDeclVendor ()
2065{
2066 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2067
2068 {
2069 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2070
2071 if (!m_clang_modules_decl_vendor_ap)
2072 {
2073 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2074 }
2075 }
2076
2077 return m_clang_modules_decl_vendor_ap.get();
2078}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002079
Greg Clayton44d93782014-01-27 23:43:24 +00002080Target::StopHookSP
2081Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002082{
2083 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002084 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2085 m_stop_hooks[new_uid] = stop_hook_sp;
2086 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002087}
2088
2089bool
2090Target::RemoveStopHookByID (lldb::user_id_t user_id)
2091{
2092 size_t num_removed;
2093 num_removed = m_stop_hooks.erase (user_id);
2094 if (num_removed == 0)
2095 return false;
2096 else
2097 return true;
2098}
2099
2100void
2101Target::RemoveAllStopHooks ()
2102{
2103 m_stop_hooks.clear();
2104}
2105
2106Target::StopHookSP
2107Target::GetStopHookByID (lldb::user_id_t user_id)
2108{
2109 StopHookSP found_hook;
2110
2111 StopHookCollection::iterator specified_hook_iter;
2112 specified_hook_iter = m_stop_hooks.find (user_id);
2113 if (specified_hook_iter != m_stop_hooks.end())
2114 found_hook = (*specified_hook_iter).second;
2115 return found_hook;
2116}
2117
2118bool
2119Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2120{
2121 StopHookCollection::iterator specified_hook_iter;
2122 specified_hook_iter = m_stop_hooks.find (user_id);
2123 if (specified_hook_iter == m_stop_hooks.end())
2124 return false;
2125
2126 (*specified_hook_iter).second->SetIsActive (active_state);
2127 return true;
2128}
2129
2130void
2131Target::SetAllStopHooksActiveState (bool active_state)
2132{
2133 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2134 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2135 {
2136 (*pos).second->SetIsActive (active_state);
2137 }
2138}
2139
2140void
2141Target::RunStopHooks ()
2142{
Jim Ingham6026ca32011-05-12 02:06:14 +00002143 if (m_suppress_stop_hooks)
2144 return;
2145
Jim Ingham9575d842011-03-11 03:53:59 +00002146 if (!m_process_sp)
2147 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002148
2149 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2150 // since in that case we do not want to run the stop-hooks
2151 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2152 return;
2153
Jim Ingham9575d842011-03-11 03:53:59 +00002154 if (m_stop_hooks.empty())
2155 return;
2156
2157 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2158
2159 // If there aren't any active stop hooks, don't bother either:
2160 bool any_active_hooks = false;
2161 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2162 {
2163 if ((*pos).second->IsActive())
2164 {
2165 any_active_hooks = true;
2166 break;
2167 }
2168 }
2169 if (!any_active_hooks)
2170 return;
2171
2172 CommandReturnObject result;
2173
2174 std::vector<ExecutionContext> exc_ctx_with_reasons;
2175 std::vector<SymbolContext> sym_ctx_with_reasons;
2176
2177 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2178 size_t num_threads = cur_threadlist.GetSize();
2179 for (size_t i = 0; i < num_threads; i++)
2180 {
2181 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2182 if (cur_thread_sp->ThreadStoppedForAReason())
2183 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002184 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002185 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2186 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2187 }
2188 }
2189
2190 // If no threads stopped for a reason, don't run the stop-hooks.
2191 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2192 if (num_exe_ctx == 0)
2193 return;
2194
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002195 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2196 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002197
2198 bool keep_going = true;
2199 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002200 bool print_hook_header;
2201 bool print_thread_header;
2202
2203 if (num_exe_ctx == 1)
2204 print_thread_header = false;
2205 else
2206 print_thread_header = true;
2207
2208 if (m_stop_hooks.size() == 1)
2209 print_hook_header = false;
2210 else
2211 print_hook_header = true;
2212
Jim Ingham9575d842011-03-11 03:53:59 +00002213 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2214 {
2215 // result.Clear();
2216 StopHookSP cur_hook_sp = (*pos).second;
2217 if (!cur_hook_sp->IsActive())
2218 continue;
2219
2220 bool any_thread_matched = false;
2221 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2222 {
2223 if ((cur_hook_sp->GetSpecifier () == NULL
2224 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2225 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002226 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002227 {
2228 if (!hooks_ran)
2229 {
Jim Ingham9575d842011-03-11 03:53:59 +00002230 hooks_ran = true;
2231 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002232 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002233 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002234 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2235 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2236 NULL);
2237 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002238 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002239 else
Daniel Malead01b2952012-11-29 21:49:15 +00002240 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002241 any_thread_matched = true;
2242 }
2243
Jim Ingham381e25b2011-03-22 01:47:27 +00002244 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002245 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002246
2247 CommandInterpreterRunOptions options;
2248 options.SetStopOnContinue (true);
2249 options.SetStopOnError (true);
2250 options.SetEchoCommands (false);
2251 options.SetPrintResults (true);
2252 options.SetAddToHistory (false);
2253
2254 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2255 &exc_ctx_with_reasons[i],
2256 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002257 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002258
2259 // If the command started the target going again, we should bag out of
2260 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002261 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2262 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002263 {
Daniel Malead01b2952012-11-29 21:49:15 +00002264 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002265 keep_going = false;
2266 }
2267 }
2268 }
2269 }
Jason Molenda879cf772011-09-23 00:42:55 +00002270
Caroline Tice969ed3d2011-05-02 20:41:46 +00002271 result.GetImmediateOutputStream()->Flush();
2272 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002273}
2274
Greg Claytonfbb76342013-11-20 21:07:01 +00002275const TargetPropertiesSP &
2276Target::GetGlobalProperties()
2277{
2278 static TargetPropertiesSP g_settings_sp;
2279 if (!g_settings_sp)
2280 {
2281 g_settings_sp.reset (new TargetProperties (NULL));
2282 }
2283 return g_settings_sp;
2284}
2285
2286Error
2287Target::Install (ProcessLaunchInfo *launch_info)
2288{
2289 Error error;
2290 PlatformSP platform_sp (GetPlatform());
2291 if (platform_sp)
2292 {
2293 if (platform_sp->IsRemote())
2294 {
2295 if (platform_sp->IsConnected())
2296 {
2297 // Install all files that have an install path, and always install the
2298 // main executable when connected to a remote platform
2299 const ModuleList& modules = GetImages();
2300 const size_t num_images = modules.GetSize();
2301 for (size_t idx = 0; idx < num_images; ++idx)
2302 {
2303 const bool is_main_executable = idx == 0;
2304 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2305 if (module_sp)
2306 {
2307 FileSpec local_file (module_sp->GetFileSpec());
2308 if (local_file)
2309 {
2310 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2311 if (!remote_file)
2312 {
2313 if (is_main_executable) // TODO: add setting for always installing main executable???
2314 {
2315 // Always install the main executable
2316 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
2317 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename();
2318 }
2319 }
2320 if (remote_file)
2321 {
2322 error = platform_sp->Install(local_file, remote_file);
2323 if (error.Success())
2324 {
2325 module_sp->SetPlatformFileSpec(remote_file);
2326 if (is_main_executable)
2327 {
2328 if (launch_info)
2329 launch_info->SetExecutableFile(remote_file, false);
2330 }
2331 }
2332 else
2333 break;
2334 }
2335 }
2336 }
2337 }
2338 }
2339 }
2340 }
2341 return error;
2342}
Greg Clayton7b242382011-07-08 00:48:09 +00002343
Greg Claytond5944cd2013-12-06 01:12:00 +00002344bool
2345Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2346{
2347 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2348}
2349
2350bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002351Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2352{
2353 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2354}
2355
2356bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002357Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2358{
2359 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2360 if (old_section_load_addr != new_section_load_addr)
2361 {
2362 uint32_t stop_id = 0;
2363 ProcessSP process_sp(GetProcessSP());
2364 if (process_sp)
2365 stop_id = process_sp->GetStopID();
2366 else
2367 stop_id = m_section_load_history.GetLastStopID();
2368 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2369 return true; // Return true if the section load address was changed...
2370 }
2371 return false; // Return false to indicate nothing changed
2372
2373}
2374
Greg Clayton8012cad2014-11-17 19:39:20 +00002375size_t
2376Target::UnloadModuleSections (const ModuleList &module_list)
2377{
2378 size_t section_unload_count = 0;
2379 size_t num_modules = module_list.GetSize();
2380 for (size_t i=0; i<num_modules; ++i)
2381 {
2382 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2383 }
2384 return section_unload_count;
2385}
2386
2387size_t
2388Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2389{
2390 uint32_t stop_id = 0;
2391 ProcessSP process_sp(GetProcessSP());
2392 if (process_sp)
2393 stop_id = process_sp->GetStopID();
2394 else
2395 stop_id = m_section_load_history.GetLastStopID();
2396 SectionList *sections = module_sp->GetSectionList();
2397 size_t section_unload_count = 0;
2398 if (sections)
2399 {
2400 const uint32_t num_sections = sections->GetNumSections(0);
2401 for (uint32_t i = 0; i < num_sections; ++i)
2402 {
2403 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2404 }
2405 }
2406 return section_unload_count;
2407}
2408
Greg Claytond5944cd2013-12-06 01:12:00 +00002409bool
2410Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2411{
2412 uint32_t stop_id = 0;
2413 ProcessSP process_sp(GetProcessSP());
2414 if (process_sp)
2415 stop_id = process_sp->GetStopID();
2416 else
2417 stop_id = m_section_load_history.GetLastStopID();
2418 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2419}
2420
2421bool
2422Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2423{
2424 uint32_t stop_id = 0;
2425 ProcessSP process_sp(GetProcessSP());
2426 if (process_sp)
2427 stop_id = process_sp->GetStopID();
2428 else
2429 stop_id = m_section_load_history.GetLastStopID();
2430 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2431}
2432
2433void
2434Target::ClearAllLoadedSections ()
2435{
2436 m_section_load_history.Clear();
2437}
2438
Greg Claytonb09c5382013-12-13 17:20:18 +00002439
2440Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002441Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002442{
2443 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002444 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2445
2446 if (log)
2447 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2448
Greg Claytonb09c5382013-12-13 17:20:18 +00002449 StateType state = eStateInvalid;
2450
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002451 // Scope to temporarily get the process state in case someone has manually
2452 // remotely connected already to a process and we can skip the platform
2453 // launching.
2454 {
2455 ProcessSP process_sp (GetProcessSP());
2456
2457 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002458 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002459 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002460 if (log)
2461 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2462 }
2463 else
2464 {
2465 if (log)
2466 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2467 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002468 }
2469
Greg Claytonb09c5382013-12-13 17:20:18 +00002470 launch_info.GetFlags().Set (eLaunchFlagDebug);
2471
2472 // Get the value of synchronous execution here. If you wait till after you have started to
2473 // run, then you could have hit a breakpoint, whose command might switch the value, and
2474 // then you'll pick up that incorrect value.
2475 Debugger &debugger = GetDebugger();
2476 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2477
2478 PlatformSP platform_sp (GetPlatform());
2479
2480 // Finalize the file actions, and if none were given, default to opening
2481 // up a pseudo terminal
2482 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002483 if (log)
2484 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2485 __FUNCTION__,
2486 platform_sp ? "true" : "false",
2487 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2488 default_to_use_pty ? "true" : "false");
2489
Greg Claytonb09c5382013-12-13 17:20:18 +00002490 launch_info.FinalizeFileActions (this, default_to_use_pty);
2491
2492 if (state == eStateConnected)
2493 {
2494 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2495 {
2496 error.SetErrorString("can't launch in tty when launching through a remote connection");
2497 return error;
2498 }
2499 }
2500
2501 if (!launch_info.GetArchitecture().IsValid())
2502 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002503
2504 // 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 +00002505 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2506 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002507 if (log)
2508 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2509
Greg Claytonb09c5382013-12-13 17:20:18 +00002510 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2511 debugger,
2512 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002513 error);
2514 }
2515 else
2516 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002517 if (log)
2518 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2519
Greg Claytonb09c5382013-12-13 17:20:18 +00002520 if (state == eStateConnected)
2521 {
2522 assert(m_process_sp);
2523 }
2524 else
2525 {
Todd Fiala015d8182014-07-22 23:41:36 +00002526 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002527 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002528 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002529 }
Todd Fiala015d8182014-07-22 23:41:36 +00002530
2531 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002532 if (m_process_sp)
2533 error = m_process_sp->Launch (launch_info);
2534 }
2535
2536 if (!m_process_sp)
2537 {
2538 if (error.Success())
2539 error.SetErrorString("failed to launch or debug process");
2540 return error;
2541 }
2542
2543 if (error.Success())
2544 {
2545 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
2546 {
Greg Clayton44d93782014-01-27 23:43:24 +00002547 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00002548
Greg Claytondc6224e2014-10-21 01:00:42 +00002549 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002550
2551 if (state == eStateStopped)
2552 {
Greg Clayton44d93782014-01-27 23:43:24 +00002553 if (!synchronous_execution)
2554 m_process_sp->RestoreProcessEvents ();
2555
2556 error = m_process_sp->PrivateResume();
Todd Fialaa3b89e22014-08-12 14:33:19 +00002557
Greg Claytonb09c5382013-12-13 17:20:18 +00002558 if (error.Success())
2559 {
Todd Fialaa3b89e22014-08-12 14:33:19 +00002560 // there is a race condition where this thread will return up the call stack to the main command
2561 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
2562 // a chance to call PushProcessIOHandler()
2563 m_process_sp->SyncIOHandler(2000);
2564
Greg Claytonb09c5382013-12-13 17:20:18 +00002565 if (synchronous_execution)
2566 {
Greg Claytondc6224e2014-10-21 01:00:42 +00002567 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
Greg Claytonb09c5382013-12-13 17:20:18 +00002568 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2569 if (!StateIsStoppedState(state, must_be_alive))
2570 {
Greg Clayton44d93782014-01-27 23:43:24 +00002571 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
Greg Claytonb09c5382013-12-13 17:20:18 +00002572 }
2573 }
2574 }
2575 else
2576 {
Greg Clayton44d93782014-01-27 23:43:24 +00002577 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002578 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002579 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002580 }
2581 }
Greg Clayton40286e02014-04-30 20:29:09 +00002582 else if (state == eStateExited)
2583 {
Zachary Turner10687b02014-10-20 17:46:43 +00002584 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002585 const int exit_status = m_process_sp->GetExitStatus();
2586 const char *exit_desc = m_process_sp->GetExitDescription();
2587#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'."
2588 if (exit_desc && exit_desc[0])
2589 {
2590 if (with_shell)
2591 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2592 else
2593 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2594 }
2595 else
2596 {
2597 if (with_shell)
2598 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2599 else
2600 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2601 }
2602 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002603 else
2604 {
2605 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2606 }
2607 }
Greg Clayton44d93782014-01-27 23:43:24 +00002608 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002609 }
2610 else
2611 {
Greg Clayton44d93782014-01-27 23:43:24 +00002612 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002613 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002614 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002615 }
2616 return error;
2617}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002618
2619Error
2620Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
2621{
2622 auto state = eStateInvalid;
2623 auto process_sp = GetProcessSP ();
2624 if (process_sp)
2625 {
2626 state = process_sp->GetState ();
2627 if (process_sp->IsAlive () && state != eStateConnected)
2628 {
2629 if (state == eStateAttaching)
2630 return Error ("process attach is in progress");
2631 return Error ("a process is already being debugged");
2632 }
2633 }
2634
2635 ListenerSP hijack_listener_sp (new Listener ("lldb.Target.Attach.attach.hijack"));
2636 attach_info.SetHijackListener (hijack_listener_sp);
2637
2638 const ModuleSP old_exec_module_sp = GetExecutableModule ();
2639
2640 // If no process info was specified, then use the target executable
2641 // name as the process to attach to by default
2642 if (!attach_info.ProcessInfoSpecified ())
2643 {
2644 if (old_exec_module_sp)
2645 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
2646
2647 if (!attach_info.ProcessInfoSpecified ())
2648 {
2649 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
2650 }
2651 }
2652
2653 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
2654
2655 Error error;
2656 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
2657 {
2658 SetPlatform (platform_sp);
2659 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
2660 }
2661 else
2662 {
2663 if (state != eStateConnected)
2664 {
2665 const char *plugin_name = attach_info.GetProcessPluginName ();
2666 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
2667 if (process_sp == nullptr)
2668 {
2669 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
2670 return error;
2671 }
2672 }
2673 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
2674 error = process_sp->Attach (attach_info);
2675 }
2676
2677 if (error.Success () && process_sp)
2678 {
2679 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
2680 process_sp->RestoreProcessEvents ();
2681
2682 if (state != eStateStopped)
2683 {
2684 const char *exit_desc = process_sp->GetExitDescription ();
2685 if (exit_desc)
2686 error.SetErrorStringWithFormat ("attach failed: %s", exit_desc);
2687 else
2688 error.SetErrorString ("attach failed: process did not stop (no such process or permission problem?)");
2689 process_sp->Destroy ();
2690 }
2691 }
2692 return error;
2693}
2694
Jim Ingham9575d842011-03-11 03:53:59 +00002695//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002696// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002697//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002698Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2699 UserID (uid),
2700 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002701 m_commands (),
2702 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002703 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002704 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002705{
2706}
2707
2708Target::StopHook::StopHook (const StopHook &rhs) :
2709 UserID (rhs.GetID()),
2710 m_target_sp (rhs.m_target_sp),
2711 m_commands (rhs.m_commands),
2712 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002713 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002714 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002715{
2716 if (rhs.m_thread_spec_ap.get() != NULL)
2717 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2718}
2719
2720
2721Target::StopHook::~StopHook ()
2722{
2723}
2724
2725void
2726Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2727{
2728 m_thread_spec_ap.reset (specifier);
2729}
2730
2731
2732void
2733Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2734{
2735 int indent_level = s->GetIndentLevel();
2736
2737 s->SetIndentLevel(indent_level + 2);
2738
Daniel Malead01b2952012-11-29 21:49:15 +00002739 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002740 if (m_active)
2741 s->Indent ("State: enabled\n");
2742 else
2743 s->Indent ("State: disabled\n");
2744
2745 if (m_specifier_sp)
2746 {
2747 s->Indent();
2748 s->PutCString ("Specifier:\n");
2749 s->SetIndentLevel (indent_level + 4);
2750 m_specifier_sp->GetDescription (s, level);
2751 s->SetIndentLevel (indent_level + 2);
2752 }
2753
2754 if (m_thread_spec_ap.get() != NULL)
2755 {
2756 StreamString tmp;
2757 s->Indent("Thread:\n");
2758 m_thread_spec_ap->GetDescription (&tmp, level);
2759 s->SetIndentLevel (indent_level + 4);
2760 s->Indent (tmp.GetData());
2761 s->PutCString ("\n");
2762 s->SetIndentLevel (indent_level + 2);
2763 }
2764
2765 s->Indent ("Commands: \n");
2766 s->SetIndentLevel (indent_level + 4);
2767 uint32_t num_commands = m_commands.GetSize();
2768 for (uint32_t i = 0; i < num_commands; i++)
2769 {
2770 s->Indent(m_commands.GetStringAtIndex(i));
2771 s->PutCString ("\n");
2772 }
2773 s->SetIndentLevel (indent_level);
2774}
2775
Greg Clayton67cc0632012-08-22 17:17:09 +00002776//--------------------------------------------------------------
2777// class TargetProperties
2778//--------------------------------------------------------------
2779
2780OptionEnumValueElement
2781lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002782{
Greg Clayton67cc0632012-08-22 17:17:09 +00002783 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2784 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2785 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2786 { 0, NULL, NULL }
2787};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002788
Greg Clayton1f746072012-08-29 21:13:06 +00002789static OptionEnumValueElement
2790g_inline_breakpoint_enums[] =
2791{
2792 { 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."},
2793 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2794 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2795 { 0, NULL, NULL }
2796};
2797
Jim Ingham0f063ba2013-03-02 00:26:47 +00002798typedef enum x86DisassemblyFlavor
2799{
2800 eX86DisFlavorDefault,
2801 eX86DisFlavorIntel,
2802 eX86DisFlavorATT
2803} x86DisassemblyFlavor;
2804
2805static OptionEnumValueElement
2806g_x86_dis_flavor_value_types[] =
2807{
2808 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2809 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2810 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2811 { 0, NULL, NULL }
2812};
2813
Enrico Granata397ddd52013-05-21 20:13:34 +00002814static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002815g_hex_immediate_style_values[] =
2816{
2817 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2818 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2819 { 0, NULL, NULL }
2820};
2821
2822static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002823g_load_script_from_sym_file_values[] =
2824{
2825 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2826 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2827 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2828 { 0, NULL, NULL }
2829};
2830
Greg Claytonfd814c52013-08-13 01:42:25 +00002831
2832static OptionEnumValueElement
2833g_memory_module_load_level_values[] =
2834{
Greg Clayton86eac942013-08-13 21:32:34 +00002835 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002836 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2837 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2838 { 0, NULL, NULL }
2839};
2840
Greg Clayton67cc0632012-08-22 17:17:09 +00002841static PropertyDefinition
2842g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002843{
Greg Clayton67cc0632012-08-22 17:17:09 +00002844 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2845 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
Enrico Granata9aa7e8e2015-01-09 00:47:24 +00002846 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eDynamicDontRunTarget , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002847 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2848 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2849 { "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 "
2850 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2851 "some part (starting at the root) of the path to the file when it was built, "
2852 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2853 "Each element of the array is checked in order and the first one that results in a match wins." },
2854 { "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 +00002855 { "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 +00002856 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2857 { "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 +00002858 { "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 +00002859 { "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 +00002860 { "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." },
2861 { "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 +00002862 { "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." },
2863 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2864 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2865 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2866 { "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 +00002867 { "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 +00002868 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2869 { "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 +00002870 { "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 +00002871 "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. "
2872 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2873 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002874 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2875 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2876 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002877 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002878 // 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.
2879 { "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 +00002880 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2881 { "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 +00002882 { "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 +00002883 { "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 +00002884 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2885 "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. "
2886 "This setting helps users control how much information gets loaded when loading modules from memory."
2887 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2888 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2889 "'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 +00002890 { "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 +00002891 { "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." },
Enrico Granata560558e2015-02-11 02:35:39 +00002892 { "display-runtime-support-values" , OptionValue::eTypeBoolean , false, false, NULL, NULL, "If true, LLDB will show variables that are meant to support the operation of a language's runtime support." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002893 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2894};
Enrico Granata560558e2015-02-11 02:35:39 +00002895
Greg Clayton67cc0632012-08-22 17:17:09 +00002896enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002897{
Greg Clayton67cc0632012-08-22 17:17:09 +00002898 ePropertyDefaultArch,
2899 ePropertyExprPrefix,
2900 ePropertyPreferDynamic,
2901 ePropertyEnableSynthetic,
2902 ePropertySkipPrologue,
2903 ePropertySourceMap,
2904 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002905 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002906 ePropertyMaxChildrenCount,
2907 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002908 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002909 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002910 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002911 ePropertyRunArgs,
2912 ePropertyEnvVars,
2913 ePropertyInheritEnv,
2914 ePropertyInputPath,
2915 ePropertyOutputPath,
2916 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00002917 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00002918 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00002919 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002920 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00002921 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00002922 ePropertyUseHexImmediates,
2923 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002924 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00002925 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00002926 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00002927 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00002928 ePropertyTrapHandlerNames,
2929 ePropertyDisplayRuntimeSupportValues
Greg Clayton67cc0632012-08-22 17:17:09 +00002930};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002931
Caroline Ticedaccaa92010-09-20 20:44:43 +00002932
Greg Clayton67cc0632012-08-22 17:17:09 +00002933class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002934{
Greg Clayton67cc0632012-08-22 17:17:09 +00002935public:
2936 TargetOptionValueProperties (const ConstString &name) :
2937 OptionValueProperties (name),
2938 m_target (NULL),
2939 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002940 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00002941 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002942
Greg Clayton67cc0632012-08-22 17:17:09 +00002943 // This constructor is used when creating TargetOptionValueProperties when it
2944 // is part of a new lldb_private::Target instance. It will copy all current
2945 // global property values as needed
2946 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2947 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2948 m_target (target),
2949 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002950 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002951 }
2952
2953 virtual const Property *
2954 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2955 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002956 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00002957 // try and grab the setting from the current target if there is one. Else we just
2958 // use the one from this instance.
2959 if (idx == ePropertyEnvVars)
2960 GetHostEnvironmentIfNeeded ();
2961
2962 if (exe_ctx)
2963 {
2964 Target *target = exe_ctx->GetTargetPtr();
2965 if (target)
2966 {
2967 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2968 if (this != target_properties)
2969 return target_properties->ProtectedGetPropertyAtIndex (idx);
2970 }
2971 }
2972 return ProtectedGetPropertyAtIndex (idx);
2973 }
Enrico Granata84a53df2013-05-20 22:29:23 +00002974
2975 lldb::TargetSP
2976 GetTargetSP ()
2977 {
2978 return m_target->shared_from_this();
2979 }
2980
Greg Clayton67cc0632012-08-22 17:17:09 +00002981protected:
2982
2983 void
2984 GetHostEnvironmentIfNeeded () const
2985 {
2986 if (!m_got_host_env)
2987 {
2988 if (m_target)
2989 {
2990 m_got_host_env = true;
2991 const uint32_t idx = ePropertyInheritEnv;
2992 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2993 {
2994 PlatformSP platform_sp (m_target->GetPlatform());
2995 if (platform_sp)
2996 {
2997 StringList env;
2998 if (platform_sp->GetEnvironment(env))
2999 {
3000 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3001 if (env_dict)
3002 {
3003 const bool can_replace = false;
3004 const size_t envc = env.GetSize();
3005 for (size_t idx=0; idx<envc; idx++)
3006 {
3007 const char *env_entry = env.GetStringAtIndex (idx);
3008 if (env_entry)
3009 {
3010 const char *equal_pos = ::strchr(env_entry, '=');
3011 ConstString key;
3012 // It is ok to have environment variables with no values
3013 const char *value = NULL;
3014 if (equal_pos)
3015 {
3016 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3017 if (equal_pos[1])
3018 value = equal_pos + 1;
3019 }
3020 else
3021 {
3022 key.SetCString(env_entry);
3023 }
3024 // Don't allow existing keys to be replaced with ones we get from the platform environment
3025 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3026 }
3027 }
3028 }
3029 }
3030 }
3031 }
3032 }
3033 }
3034 }
3035 Target *m_target;
3036 mutable bool m_got_host_env;
3037};
3038
Greg Claytonfbb76342013-11-20 21:07:01 +00003039//----------------------------------------------------------------------
3040// TargetProperties
3041//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003042TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003043 Properties (),
3044 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003045{
3046 if (target)
3047 {
3048 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003049
3050 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3051 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3052 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3053 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3054 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3055 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3056 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3057 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3058 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3059 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3060
3061 // Update m_launch_info once it was created
3062 Arg0ValueChangedCallback(this, NULL);
3063 RunArgsValueChangedCallback(this, NULL);
3064 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3065 InputPathValueChangedCallback(this, NULL);
3066 OutputPathValueChangedCallback(this, NULL);
3067 ErrorPathValueChangedCallback(this, NULL);
3068 DetachOnErrorValueChangedCallback(this, NULL);
3069 DisableASLRValueChangedCallback(this, NULL);
3070 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003071 }
3072 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003073 {
3074 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3075 m_collection_sp->Initialize(g_properties);
3076 m_collection_sp->AppendProperty(ConstString("process"),
3077 ConstString("Settings specify to processes."),
3078 true,
3079 Process::GetGlobalProperties()->GetValueProperties());
3080 }
Ilia K8f37ca52015-02-13 14:31:06 +00003081
Caroline Ticedaccaa92010-09-20 20:44:43 +00003082}
3083
Greg Clayton67cc0632012-08-22 17:17:09 +00003084TargetProperties::~TargetProperties ()
3085{
3086}
3087ArchSpec
3088TargetProperties::GetDefaultArchitecture () const
3089{
3090 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3091 if (value)
3092 return value->GetCurrentValue();
3093 return ArchSpec();
3094}
3095
3096void
3097TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3098{
3099 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3100 if (value)
3101 return value->SetCurrentValue(arch, true);
3102}
3103
3104lldb::DynamicValueType
3105TargetProperties::GetPreferDynamicValue() const
3106{
3107 const uint32_t idx = ePropertyPreferDynamic;
3108 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3109}
3110
3111bool
3112TargetProperties::GetDisableASLR () const
3113{
3114 const uint32_t idx = ePropertyDisableASLR;
3115 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3116}
3117
3118void
3119TargetProperties::SetDisableASLR (bool b)
3120{
3121 const uint32_t idx = ePropertyDisableASLR;
3122 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3123}
3124
3125bool
Jim Ingham106d0282014-06-25 02:32:56 +00003126TargetProperties::GetDetachOnError () const
3127{
3128 const uint32_t idx = ePropertyDetachOnError;
3129 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3130}
3131
3132void
3133TargetProperties::SetDetachOnError (bool b)
3134{
3135 const uint32_t idx = ePropertyDetachOnError;
3136 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3137}
3138
3139bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003140TargetProperties::GetDisableSTDIO () const
3141{
3142 const uint32_t idx = ePropertyDisableSTDIO;
3143 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3144}
3145
3146void
3147TargetProperties::SetDisableSTDIO (bool b)
3148{
3149 const uint32_t idx = ePropertyDisableSTDIO;
3150 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3151}
3152
Jim Ingham0f063ba2013-03-02 00:26:47 +00003153const char *
3154TargetProperties::GetDisassemblyFlavor () const
3155{
3156 const uint32_t idx = ePropertyDisassemblyFlavor;
3157 const char *return_value;
3158
3159 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3160 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3161 return return_value;
3162}
3163
Greg Clayton1f746072012-08-29 21:13:06 +00003164InlineStrategy
3165TargetProperties::GetInlineStrategy () const
3166{
3167 const uint32_t idx = ePropertyInlineStrategy;
3168 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3169}
3170
Greg Clayton45392552012-10-17 22:57:12 +00003171const char *
3172TargetProperties::GetArg0 () const
3173{
3174 const uint32_t idx = ePropertyArg0;
3175 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3176}
3177
3178void
3179TargetProperties::SetArg0 (const char *arg)
3180{
3181 const uint32_t idx = ePropertyArg0;
3182 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
3183}
3184
Greg Clayton67cc0632012-08-22 17:17:09 +00003185bool
3186TargetProperties::GetRunArguments (Args &args) const
3187{
3188 const uint32_t idx = ePropertyRunArgs;
3189 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3190}
3191
3192void
3193TargetProperties::SetRunArguments (const Args &args)
3194{
3195 const uint32_t idx = ePropertyRunArgs;
3196 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3197}
3198
3199size_t
3200TargetProperties::GetEnvironmentAsArgs (Args &env) const
3201{
3202 const uint32_t idx = ePropertyEnvVars;
3203 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3204}
3205
Ilia K8f37ca52015-02-13 14:31:06 +00003206void
3207TargetProperties::SetEnvironmentFromArgs (const Args &env)
3208{
3209 const uint32_t idx = ePropertyEnvVars;
3210 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
3211}
3212
Greg Clayton67cc0632012-08-22 17:17:09 +00003213bool
3214TargetProperties::GetSkipPrologue() const
3215{
3216 const uint32_t idx = ePropertySkipPrologue;
3217 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3218}
3219
3220PathMappingList &
3221TargetProperties::GetSourcePathMap () const
3222{
3223 const uint32_t idx = ePropertySourceMap;
3224 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3225 assert(option_value);
3226 return option_value->GetCurrentValue();
3227}
3228
3229FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003230TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003231{
3232 const uint32_t idx = ePropertyExecutableSearchPaths;
3233 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3234 assert(option_value);
3235 return option_value->GetCurrentValue();
3236}
3237
Michael Sartaina7499c92013-07-01 19:45:50 +00003238FileSpecList &
3239TargetProperties::GetDebugFileSearchPaths ()
3240{
3241 const uint32_t idx = ePropertyDebugFileSearchPaths;
3242 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3243 assert(option_value);
3244 return option_value->GetCurrentValue();
3245}
3246
Greg Clayton67cc0632012-08-22 17:17:09 +00003247bool
3248TargetProperties::GetEnableSyntheticValue () const
3249{
3250 const uint32_t idx = ePropertyEnableSynthetic;
3251 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3252}
3253
3254uint32_t
3255TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3256{
3257 const uint32_t idx = ePropertyMaxChildrenCount;
3258 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3259}
3260
3261uint32_t
3262TargetProperties::GetMaximumSizeOfStringSummary() const
3263{
3264 const uint32_t idx = ePropertyMaxSummaryLength;
3265 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3266}
3267
Enrico Granatad325bf92013-06-04 22:54:16 +00003268uint32_t
3269TargetProperties::GetMaximumMemReadSize () const
3270{
3271 const uint32_t idx = ePropertyMaxMemReadSize;
3272 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3273}
3274
Greg Clayton67cc0632012-08-22 17:17:09 +00003275FileSpec
3276TargetProperties::GetStandardInputPath () const
3277{
3278 const uint32_t idx = ePropertyInputPath;
3279 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3280}
3281
3282void
3283TargetProperties::SetStandardInputPath (const char *p)
3284{
3285 const uint32_t idx = ePropertyInputPath;
3286 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3287}
3288
3289FileSpec
3290TargetProperties::GetStandardOutputPath () const
3291{
3292 const uint32_t idx = ePropertyOutputPath;
3293 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3294}
3295
3296void
3297TargetProperties::SetStandardOutputPath (const char *p)
3298{
3299 const uint32_t idx = ePropertyOutputPath;
3300 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3301}
3302
3303FileSpec
3304TargetProperties::GetStandardErrorPath () const
3305{
3306 const uint32_t idx = ePropertyErrorPath;
3307 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3308}
3309
Greg Clayton6920b522012-08-22 18:39:03 +00003310const char *
3311TargetProperties::GetExpressionPrefixContentsAsCString ()
3312{
3313 const uint32_t idx = ePropertyExprPrefix;
3314 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3315 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003316 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003317 const bool null_terminate = true;
3318 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003319 if (data_sp)
3320 return (const char *) data_sp->GetBytes();
3321 }
Greg Clayton6920b522012-08-22 18:39:03 +00003322 return NULL;
3323}
3324
Greg Clayton67cc0632012-08-22 17:17:09 +00003325void
3326TargetProperties::SetStandardErrorPath (const char *p)
3327{
3328 const uint32_t idx = ePropertyErrorPath;
3329 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3330}
3331
3332bool
3333TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3334{
3335 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3336 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3337}
3338
Jim Ingham17d023f2013-03-13 17:58:04 +00003339bool
Daniel Malead79ae052013-08-07 21:54:09 +00003340TargetProperties::GetUseHexImmediates () const
3341{
3342 const uint32_t idx = ePropertyUseHexImmediates;
3343 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3344}
3345
3346bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003347TargetProperties::GetUseFastStepping () const
3348{
3349 const uint32_t idx = ePropertyUseFastStepping;
3350 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3351}
3352
Greg Claytonfb6621e2013-12-06 21:59:52 +00003353bool
3354TargetProperties::GetDisplayExpressionsInCrashlogs () const
3355{
3356 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3357 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3358}
3359
Enrico Granata397ddd52013-05-21 20:13:34 +00003360LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003361TargetProperties::GetLoadScriptFromSymbolFile () const
3362{
3363 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003364 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003365}
3366
Daniel Malead79ae052013-08-07 21:54:09 +00003367Disassembler::HexImmediateStyle
3368TargetProperties::GetHexImmediateStyle () const
3369{
3370 const uint32_t idx = ePropertyHexImmediateStyle;
3371 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3372}
3373
Greg Claytonfd814c52013-08-13 01:42:25 +00003374MemoryModuleLoadLevel
3375TargetProperties::GetMemoryModuleLoadLevel() const
3376{
3377 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3378 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3379}
3380
Jason Molendaa4bea722014-02-14 05:06:49 +00003381bool
3382TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3383{
3384 const uint32_t idx = ePropertyTrapHandlerNames;
3385 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3386}
Greg Claytonfd814c52013-08-13 01:42:25 +00003387
Jason Molendaa4bea722014-02-14 05:06:49 +00003388void
3389TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3390{
3391 const uint32_t idx = ePropertyTrapHandlerNames;
3392 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3393}
Greg Clayton67cc0632012-08-22 17:17:09 +00003394
Enrico Granata560558e2015-02-11 02:35:39 +00003395bool
3396TargetProperties::GetDisplayRuntimeSupportValues () const
3397{
3398 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3399 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3400}
3401
3402void
3403TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3404{
3405 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3406 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3407}
3408
Ilia K8f37ca52015-02-13 14:31:06 +00003409const ProcessLaunchInfo &
3410TargetProperties::GetProcessLaunchInfo () const
3411{
3412 return m_launch_info;
3413}
3414
3415void
3416TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3417{
3418 m_launch_info = launch_info;
3419 SetArg0(launch_info.GetArg0());
3420 SetRunArguments(launch_info.GetArguments());
3421 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3422 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3423 if (input_file_action)
3424 {
3425 const char *input_path = input_file_action->GetPath();
3426 if (input_path)
3427 SetStandardInputPath(input_path);
3428 }
3429 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3430 if (output_file_action)
3431 {
3432 const char *output_path = output_file_action->GetPath();
3433 if (output_path)
3434 SetStandardOutputPath(output_path);
3435 }
3436 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3437 if (error_file_action)
3438 {
3439 const char *error_path = error_file_action->GetPath();
3440 if (error_path)
3441 SetStandardErrorPath(error_path);
3442 }
3443 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3444 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3445 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3446}
3447
3448void
3449TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
3450{
3451 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3452 this_->m_launch_info.SetArg0(this_->GetArg0());
3453}
3454
3455void
3456TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
3457{
3458 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3459 Args args;
3460 if (this_->GetRunArguments(args))
3461 this_->m_launch_info.GetArguments() = args;
3462}
3463
3464void
3465TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
3466{
3467 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3468 Args args;
3469 if (this_->GetEnvironmentAsArgs(args))
3470 this_->m_launch_info.GetEnvironmentEntries() = args;
3471}
3472
3473void
3474TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3475{
3476 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3477 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath().GetPath().c_str(), true, false);
3478}
3479
3480void
3481TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3482{
3483 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3484 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath().GetPath().c_str(), false, true);
3485}
3486
3487void
3488TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3489{
3490 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3491 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath().GetPath().c_str(), false, true);
3492}
3493
3494void
3495TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
3496{
3497 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3498 if (this_->GetDetachOnError())
3499 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
3500 else
3501 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
3502}
3503
3504void
3505TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
3506{
3507 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3508 if (this_->GetDisableASLR())
3509 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
3510 else
3511 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
3512}
3513
3514void
3515TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
3516{
3517 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3518 if (this_->GetDisableSTDIO())
3519 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
3520 else
3521 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
3522}
3523
Greg Claytonfbb76342013-11-20 21:07:01 +00003524//----------------------------------------------------------------------
3525// Target::TargetEventData
3526//----------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +00003527const ConstString &
3528Target::TargetEventData::GetFlavorString ()
3529{
3530 static ConstString g_flavor ("Target::TargetEventData");
3531 return g_flavor;
3532}
3533
3534const ConstString &
3535Target::TargetEventData::GetFlavor () const
3536{
3537 return TargetEventData::GetFlavorString ();
3538}
3539
3540Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
3541 EventData(),
3542 m_target_sp (new_target_sp)
3543{
3544}
3545
3546Target::TargetEventData::~TargetEventData()
3547{
3548
3549}
3550
3551void
3552Target::TargetEventData::Dump (Stream *s) const
3553{
3554
3555}
3556
3557const TargetSP
3558Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
3559{
3560 TargetSP target_sp;
3561
3562 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
3563 if (data)
3564 target_sp = data->m_target_sp;
3565
3566 return target_sp;
3567}
3568
3569const Target::TargetEventData *
3570Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3571{
3572 if (event_ptr)
3573 {
3574 const EventData *event_data = event_ptr->GetData();
3575 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3576 return static_cast <const TargetEventData *> (event_ptr->GetData());
3577 }
3578 return NULL;
3579}
3580