blob: 76a89a4518731ed97ec537ca9dc8b5fbae653c1a [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();
178}
179
180void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181Target::DeleteCurrentProcess ()
182{
183 if (m_process_sp.get())
184 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000185 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 if (m_process_sp->IsAlive())
187 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000188
189 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190
Greg Clayton90ba8112012-12-05 00:16:59 +0000191 CleanupProcess ();
192
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 m_process_sp.reset();
194 }
195}
196
197const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000198Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199{
200 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000201 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 return m_process_sp;
203}
204
205const lldb::ProcessSP &
206Target::GetProcessSP () const
207{
208 return m_process_sp;
209}
210
Greg Clayton3418c852011-08-10 02:10:13 +0000211void
212Target::Destroy()
213{
214 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000215 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000216 DeleteCurrentProcess ();
217 m_platform_sp.reset();
218 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000219 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000220 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000221 const bool notify = false;
222 m_breakpoint_list.RemoveAll(notify);
223 m_internal_breakpoint_list.RemoveAll(notify);
224 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000225 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000226 m_search_filter_sp.reset();
227 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000228 m_persistent_variables.Clear();
229 m_stop_hooks.clear();
230 m_stop_hook_next_id = 0;
231 m_suppress_stop_hooks = false;
232}
233
234
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235BreakpointList &
236Target::GetBreakpointList(bool internal)
237{
238 if (internal)
239 return m_internal_breakpoint_list;
240 else
241 return m_breakpoint_list;
242}
243
244const BreakpointList &
245Target::GetBreakpointList(bool internal) const
246{
247 if (internal)
248 return m_internal_breakpoint_list;
249 else
250 return m_breakpoint_list;
251}
252
253BreakpointSP
254Target::GetBreakpointByID (break_id_t break_id)
255{
256 BreakpointSP bp_sp;
257
258 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
259 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
260 else
261 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
262
263 return bp_sp;
264}
265
266BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000267Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000268 const FileSpecList *source_file_spec_list,
269 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000270 bool internal,
271 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272{
Jim Ingham87df91b2011-09-23 00:54:11 +0000273 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
274 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000275 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000276}
277
278
279BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000280Target::CreateBreakpoint (const FileSpecList *containingModules,
281 const FileSpec &file,
282 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000283 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000284 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000285 bool internal,
286 bool hardware)
Jim Ingham969795f2011-09-21 01:17:13 +0000287{
Greg Clayton1f746072012-08-29 21:13:06 +0000288 if (check_inlines == eLazyBoolCalculate)
289 {
290 const InlineStrategy inline_strategy = GetInlineStrategy();
291 switch (inline_strategy)
292 {
293 case eInlineBreakpointsNever:
294 check_inlines = eLazyBoolNo;
295 break;
296
297 case eInlineBreakpointsHeaders:
298 if (file.IsSourceImplementationFile())
299 check_inlines = eLazyBoolNo;
300 else
301 check_inlines = eLazyBoolYes;
302 break;
303
304 case eInlineBreakpointsAlways:
305 check_inlines = eLazyBoolYes;
306 break;
307 }
308 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000309 SearchFilterSP filter_sp;
310 if (check_inlines == eLazyBoolNo)
311 {
312 // Not checking for inlines, we are looking only for matching compile units
313 FileSpecList compile_unit_list;
314 compile_unit_list.Append (file);
315 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
316 }
317 else
318 {
319 filter_sp = GetSearchFilterForModuleList (containingModules);
320 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000321 if (skip_prologue == eLazyBoolCalculate)
322 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
323
Greg Clayton1f746072012-08-29 21:13:06 +0000324 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
325 file,
326 line_no,
327 check_inlines,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000328 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000329 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330}
331
332
333BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000334Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 Address so_addr;
337 // Attempt to resolve our load address if possible, though it is ok if
338 // it doesn't resolve to section/offset.
339
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000340 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000341 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000342 if (!so_addr.IsValid())
343 {
344 // The address didn't resolve, so just set this as an absolute address
345 so_addr.SetOffset (addr);
346 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000347 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 return bp_sp;
349}
350
351BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000352Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000354 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000356 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357}
358
359BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000360Target::CreateBreakpoint (const FileSpecList *containingModules,
361 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000362 const char *func_name,
363 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000364 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000365 bool internal,
366 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000368 BreakpointSP bp_sp;
369 if (func_name)
370 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000371 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000372
373 if (skip_prologue == eLazyBoolCalculate)
374 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
375
Greg Claytond16e1e52011-07-12 17:06:17 +0000376 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
377 func_name,
378 func_name_type_mask,
379 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000380 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000381 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000382 }
383 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384}
385
Jim Inghamfab10e82012-03-06 00:37:27 +0000386lldb::BreakpointSP
387Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000388 const FileSpecList *containingSourceFiles,
389 const std::vector<std::string> &func_names,
390 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000391 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000392 bool internal,
393 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000394{
395 BreakpointSP bp_sp;
396 size_t num_names = func_names.size();
397 if (num_names > 0)
398 {
399 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000400
401 if (skip_prologue == eLazyBoolCalculate)
402 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
403
404 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000405 func_names,
406 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000407 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000408 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000409 }
410 return bp_sp;
411}
412
Jim Ingham133e0fb2012-03-03 02:05:11 +0000413BreakpointSP
414Target::CreateBreakpoint (const FileSpecList *containingModules,
415 const FileSpecList *containingSourceFiles,
416 const char *func_names[],
417 size_t num_names,
418 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000419 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000420 bool internal,
421 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000422{
423 BreakpointSP bp_sp;
424 if (num_names > 0)
425 {
426 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
427
Greg Clayton03da4cc2013-04-19 21:31:16 +0000428 if (skip_prologue == eLazyBoolCalculate)
429 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
430
431 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000432 func_names,
433 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000434 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000435 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000436 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000437 }
438 return bp_sp;
439}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440
441SearchFilterSP
442Target::GetSearchFilterForModule (const FileSpec *containingModule)
443{
444 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 if (containingModule != NULL)
446 {
447 // TODO: We should look into sharing module based search filters
448 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000449 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 }
451 else
452 {
453 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000454 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 filter_sp = m_search_filter_sp;
456 }
457 return filter_sp;
458}
459
Jim Ingham969795f2011-09-21 01:17:13 +0000460SearchFilterSP
461Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
462{
463 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000464 if (containingModules && containingModules->GetSize() != 0)
465 {
466 // TODO: We should look into sharing module based search filters
467 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000468 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000469 }
470 else
471 {
472 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000473 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000474 filter_sp = m_search_filter_sp;
475 }
476 return filter_sp;
477}
478
Jim Ingham87df91b2011-09-23 00:54:11 +0000479SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000480Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
481 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000482{
483 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
484 return GetSearchFilterForModuleList(containingModules);
485
486 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000487 if (containingModules == NULL)
488 {
489 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
490 // but that will take a little reworking.
491
Greg Claytone1cd1be2012-01-29 20:56:30 +0000492 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000493 }
494 else
495 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000496 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000497 }
498 return filter_sp;
499}
500
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000502Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000503 const FileSpecList *containingSourceFiles,
504 RegularExpression &func_regex,
505 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000506 bool internal,
507 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508{
Jim Ingham87df91b2011-09-23 00:54:11 +0000509 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000510 bool skip =
511 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
512 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000513 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
514 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000515 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516
Jim Ingham1460e4b2014-01-10 23:46:59 +0000517 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518}
519
Jim Ingham219ba192012-03-05 04:47:34 +0000520lldb::BreakpointSP
521Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
522{
523 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
524}
525
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000527Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528{
529 BreakpointSP bp_sp;
530 if (filter_sp && resolver_sp)
531 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000532 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000534 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000536 return bp_sp;
537}
538
539void
540Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
541{
542 if (!bp_sp)
543 return;
544 if (internal)
545 m_internal_breakpoint_list.Add (bp_sp, false);
546 else
547 m_breakpoint_list.Add (bp_sp, true);
548
549 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
550 if (log)
551 {
552 StreamString s;
553 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
554 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
555 }
556
557 bp_sp->ResolveBreakpoint();
558
559 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000560 {
561 m_last_created_breakpoint = bp_sp;
562 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563}
564
Johnny Chen86364b42011-09-20 23:28:55 +0000565bool
566Target::ProcessIsValid()
567{
568 return (m_process_sp && m_process_sp->IsAlive());
569}
570
Johnny Chenb90827e2012-06-04 23:19:54 +0000571static bool
572CheckIfWatchpointsExhausted(Target *target, Error &error)
573{
574 uint32_t num_supported_hardware_watchpoints;
575 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
576 if (rc.Success())
577 {
578 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
579 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
580 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
581 num_supported_hardware_watchpoints);
582 }
583 return false;
584}
585
Johnny Chen01a67862011-10-14 00:42:25 +0000586// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000587// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000588WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000589Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000590{
Greg Clayton5160ce52013-03-27 23:08:40 +0000591 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000592 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000593 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000594 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000595
Johnny Chen01a67862011-10-14 00:42:25 +0000596 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000597 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000598 {
599 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000600 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000601 }
Jim Inghamc6462312013-06-18 21:52:48 +0000602
Johnny Chen45e541f2011-09-14 22:20:15 +0000603 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000604 {
605 if (size == 0)
606 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
607 else
Daniel Malead01b2952012-11-29 21:49:15 +0000608 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000609 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000610 }
Jim Inghamc6462312013-06-18 21:52:48 +0000611
612 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
613 {
614 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
615 }
Johnny Chen7313a642011-09-13 01:15:36 +0000616
Johnny Chen01a67862011-10-14 00:42:25 +0000617 // Currently we only support one watchpoint per address, with total number
618 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000619
620 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000621 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 +0000622 Mutex::Locker locker;
623 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000624 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000625 if (matched_sp)
626 {
Johnny Chen0c406372011-09-14 20:23:45 +0000627 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000628 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000629 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
630 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000631 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000632 if (size == old_size && kind == old_type)
633 {
Johnny Chen01a67862011-10-14 00:42:25 +0000634 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000635 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000636 }
637 else
638 {
Johnny Chen01a67862011-10-14 00:42:25 +0000639 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000640 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
641 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000642 }
Johnny Chen3c532582011-09-13 23:29:31 +0000643 }
644
Jason Molenda727e3922012-12-05 23:07:34 +0000645 if (!wp_sp)
646 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000647 wp_sp.reset(new Watchpoint(*this, addr, size, type));
648 wp_sp->SetWatchpointType(kind, notify);
649 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000650 }
Johnny Chen0c406372011-09-14 20:23:45 +0000651
Jim Ingham1b5792e2012-12-18 02:03:49 +0000652 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000653 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000654 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
655 __FUNCTION__,
656 error.Success() ? "succeeded" : "failed",
657 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000658
Jason Molenda727e3922012-12-05 23:07:34 +0000659 if (error.Fail())
660 {
Johnny Chen41b77262012-03-26 22:00:10 +0000661 // Enabling the watchpoint on the device side failed.
662 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000663 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000664 // See if we could provide more helpful error message.
665 if (!CheckIfWatchpointsExhausted(this, error))
666 {
667 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000668 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000669 }
Johnny Chen01a67862011-10-14 00:42:25 +0000670 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000671 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000672 else
Johnny Chen01a67862011-10-14 00:42:25 +0000673 m_last_created_watchpoint = wp_sp;
674 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000675}
676
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677void
678Target::RemoveAllBreakpoints (bool internal_also)
679{
Greg Clayton5160ce52013-03-27 23:08:40 +0000680 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681 if (log)
682 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
683
Greg Clayton9fed0d82010-07-23 23:33:17 +0000684 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000686 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000687
688 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689}
690
691void
692Target::DisableAllBreakpoints (bool internal_also)
693{
Greg Clayton5160ce52013-03-27 23:08:40 +0000694 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 if (log)
696 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
697
698 m_breakpoint_list.SetEnabledAll (false);
699 if (internal_also)
700 m_internal_breakpoint_list.SetEnabledAll (false);
701}
702
703void
704Target::EnableAllBreakpoints (bool internal_also)
705{
Greg Clayton5160ce52013-03-27 23:08:40 +0000706 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 if (log)
708 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
709
710 m_breakpoint_list.SetEnabledAll (true);
711 if (internal_also)
712 m_internal_breakpoint_list.SetEnabledAll (true);
713}
714
715bool
716Target::RemoveBreakpointByID (break_id_t break_id)
717{
Greg Clayton5160ce52013-03-27 23:08:40 +0000718 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 if (log)
720 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
721
722 if (DisableBreakpointByID (break_id))
723 {
724 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000725 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000727 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000728 if (m_last_created_breakpoint)
729 {
730 if (m_last_created_breakpoint->GetID() == break_id)
731 m_last_created_breakpoint.reset();
732 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000733 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000734 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 return true;
736 }
737 return false;
738}
739
740bool
741Target::DisableBreakpointByID (break_id_t break_id)
742{
Greg Clayton5160ce52013-03-27 23:08:40 +0000743 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 if (log)
745 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
746
747 BreakpointSP bp_sp;
748
749 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
750 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
751 else
752 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
753 if (bp_sp)
754 {
755 bp_sp->SetEnabled (false);
756 return true;
757 }
758 return false;
759}
760
761bool
762Target::EnableBreakpointByID (break_id_t break_id)
763{
Greg Clayton5160ce52013-03-27 23:08:40 +0000764 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 if (log)
766 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
767 __FUNCTION__,
768 break_id,
769 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
770
771 BreakpointSP bp_sp;
772
773 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
774 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
775 else
776 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
777
778 if (bp_sp)
779 {
780 bp_sp->SetEnabled (true);
781 return true;
782 }
783 return false;
784}
785
Johnny Chenedf50372011-09-23 21:21:43 +0000786// The flag 'end_to_end', default to true, signifies that the operation is
787// performed end to end, for both the debugger and the debuggee.
788
Johnny Chen01a67862011-10-14 00:42:25 +0000789// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
790// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000791bool
Johnny Chen01a67862011-10-14 00:42:25 +0000792Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000793{
Greg Clayton5160ce52013-03-27 23:08:40 +0000794 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000795 if (log)
796 log->Printf ("Target::%s\n", __FUNCTION__);
797
Johnny Chenedf50372011-09-23 21:21:43 +0000798 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000799 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000800 return true;
801 }
802
803 // Otherwise, it's an end to end operation.
804
Johnny Chen86364b42011-09-20 23:28:55 +0000805 if (!ProcessIsValid())
806 return false;
807
Johnny Chen01a67862011-10-14 00:42:25 +0000808 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000809 for (size_t i = 0; i < num_watchpoints; ++i)
810 {
Johnny Chen01a67862011-10-14 00:42:25 +0000811 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
812 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000813 return false;
814
Johnny Chen01a67862011-10-14 00:42:25 +0000815 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000816 if (rc.Fail())
817 return false;
818 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000819 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000820 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000821 return true; // Success!
822}
823
Johnny Chen01a67862011-10-14 00:42:25 +0000824// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
825// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000826bool
Johnny Chen01a67862011-10-14 00:42:25 +0000827Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000828{
Greg Clayton5160ce52013-03-27 23:08:40 +0000829 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000830 if (log)
831 log->Printf ("Target::%s\n", __FUNCTION__);
832
Johnny Chenedf50372011-09-23 21:21:43 +0000833 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000834 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000835 return true;
836 }
837
838 // Otherwise, it's an end to end operation.
839
Johnny Chen86364b42011-09-20 23:28:55 +0000840 if (!ProcessIsValid())
841 return false;
842
Johnny Chen01a67862011-10-14 00:42:25 +0000843 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000844 for (size_t i = 0; i < num_watchpoints; ++i)
845 {
Johnny Chen01a67862011-10-14 00:42:25 +0000846 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
847 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000848 return false;
849
Johnny Chen01a67862011-10-14 00:42:25 +0000850 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000851 if (rc.Fail())
852 return false;
853 }
Johnny Chen86364b42011-09-20 23:28:55 +0000854 return true; // Success!
855}
856
Johnny Chen01a67862011-10-14 00:42:25 +0000857// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
858// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000859bool
Johnny Chen01a67862011-10-14 00:42:25 +0000860Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000861{
Greg Clayton5160ce52013-03-27 23:08:40 +0000862 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000863 if (log)
864 log->Printf ("Target::%s\n", __FUNCTION__);
865
Johnny Chenedf50372011-09-23 21:21:43 +0000866 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000867 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000868 return true;
869 }
870
871 // Otherwise, it's an end to end operation.
872
Johnny Chen86364b42011-09-20 23:28:55 +0000873 if (!ProcessIsValid())
874 return false;
875
Johnny Chen01a67862011-10-14 00:42:25 +0000876 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000877 for (size_t i = 0; i < num_watchpoints; ++i)
878 {
Johnny Chen01a67862011-10-14 00:42:25 +0000879 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
880 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000881 return false;
882
Johnny Chen01a67862011-10-14 00:42:25 +0000883 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000884 if (rc.Fail())
885 return false;
886 }
Johnny Chen86364b42011-09-20 23:28:55 +0000887 return true; // Success!
888}
889
Johnny Chena4d6bc92012-02-25 06:44:30 +0000890// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
891bool
892Target::ClearAllWatchpointHitCounts ()
893{
Greg Clayton5160ce52013-03-27 23:08:40 +0000894 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000895 if (log)
896 log->Printf ("Target::%s\n", __FUNCTION__);
897
898 size_t num_watchpoints = m_watchpoint_list.GetSize();
899 for (size_t i = 0; i < num_watchpoints; ++i)
900 {
901 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
902 if (!wp_sp)
903 return false;
904
905 wp_sp->ResetHitCount();
906 }
907 return true; // Success!
908}
909
Johnny Chen01a67862011-10-14 00:42:25 +0000910// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000911// during these operations.
912bool
Johnny Chen01a67862011-10-14 00:42:25 +0000913Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000914{
Greg Clayton5160ce52013-03-27 23:08:40 +0000915 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000916 if (log)
917 log->Printf ("Target::%s\n", __FUNCTION__);
918
919 if (!ProcessIsValid())
920 return false;
921
Johnny Chen01a67862011-10-14 00:42:25 +0000922 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000923 for (size_t i = 0; i < num_watchpoints; ++i)
924 {
Johnny Chen01a67862011-10-14 00:42:25 +0000925 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
926 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000927 return false;
928
Johnny Chen01a67862011-10-14 00:42:25 +0000929 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000930 }
931 return true; // Success!
932}
933
Johnny Chen01a67862011-10-14 00:42:25 +0000934// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000935bool
Johnny Chen01a67862011-10-14 00:42:25 +0000936Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000937{
Greg Clayton5160ce52013-03-27 23:08:40 +0000938 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000939 if (log)
940 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
941
942 if (!ProcessIsValid())
943 return false;
944
Johnny Chen01a67862011-10-14 00:42:25 +0000945 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
946 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000947 {
Johnny Chen01a67862011-10-14 00:42:25 +0000948 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000949 if (rc.Success())
950 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000951
Johnny Chenf04ee932011-09-22 18:04:58 +0000952 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000953 }
954 return false;
955}
956
Johnny Chen01a67862011-10-14 00:42:25 +0000957// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000958bool
Johnny Chen01a67862011-10-14 00:42:25 +0000959Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000960{
Greg Clayton5160ce52013-03-27 23:08:40 +0000961 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000962 if (log)
963 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
964
965 if (!ProcessIsValid())
966 return false;
967
Johnny Chen01a67862011-10-14 00:42:25 +0000968 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
969 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000970 {
Johnny Chen01a67862011-10-14 00:42:25 +0000971 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000972 if (rc.Success())
973 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000974
Johnny Chenf04ee932011-09-22 18:04:58 +0000975 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000976 }
977 return false;
978}
979
Johnny Chen01a67862011-10-14 00:42:25 +0000980// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000981bool
Johnny Chen01a67862011-10-14 00:42:25 +0000982Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000983{
Greg Clayton5160ce52013-03-27 23:08:40 +0000984 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000985 if (log)
986 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
987
Jim Inghamb0b45132013-07-02 02:09:46 +0000988 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
989 if (watch_to_remove_sp == m_last_created_watchpoint)
990 m_last_created_watchpoint.reset();
991
Johnny Chen01a67862011-10-14 00:42:25 +0000992 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +0000993 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000994 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +0000995 return true;
996 }
997 return false;
998}
999
Johnny Chen01a67862011-10-14 00:42:25 +00001000// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001001bool
Johnny Chen01a67862011-10-14 00:42:25 +00001002Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001003{
Greg Clayton5160ce52013-03-27 23:08:40 +00001004 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001005 if (log)
1006 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1007
1008 if (!ProcessIsValid())
1009 return false;
1010
Johnny Chen01a67862011-10-14 00:42:25 +00001011 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1012 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001013 {
Johnny Chen01a67862011-10-14 00:42:25 +00001014 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001015 return true;
1016 }
1017 return false;
1018}
1019
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020ModuleSP
1021Target::GetExecutableModule ()
1022{
Greg Claytonaa149cb2011-08-11 02:48:45 +00001023 return m_images.GetModuleAtIndex(0);
1024}
1025
1026Module*
1027Target::GetExecutableModulePointer ()
1028{
1029 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030}
1031
Enrico Granata17598482012-11-08 02:22:02 +00001032static void
1033LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1034{
1035 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001036 StreamString feedback_stream;
1037 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001038 {
Enrico Granata97303392013-05-21 00:00:30 +00001039 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001040 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001041 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1042 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001043 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001044 if (feedback_stream.GetSize())
1045 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1046 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001047}
1048
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049void
Greg Claytonb35db632013-11-09 00:03:31 +00001050Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051{
Greg Claytonb35db632013-11-09 00:03:31 +00001052 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001053 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054 m_images.Clear();
1055 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001056 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001057 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001058}
1059
1060void
Greg Claytonb35db632013-11-09 00:03:31 +00001061Target::DidExec ()
1062{
1063 // When a process exec's we need to know about it so we can do some cleanup.
1064 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1065 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1066}
1067
1068void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001069Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1070{
1071 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001072 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073
1074 if (executable_sp.get())
1075 {
1076 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001077 "Target::SetExecutableModule (executable = '%s')",
1078 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001080 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081
Jim Ingham5aee1622010-08-09 23:31:02 +00001082 // 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 +00001083 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001084 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001085 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001086 if (log)
1087 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1088 }
1089
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001091 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001093 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094 {
1095 executable_objfile->GetDependentModules(dependent_files);
1096 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1097 {
Greg Claytonded470d2011-03-19 01:12:21 +00001098 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1099 FileSpec platform_dependent_file_spec;
1100 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001101 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001102 else
1103 platform_dependent_file_spec = dependent_file_spec;
1104
Greg Claytonb9a01b32012-02-26 05:51:37 +00001105 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1106 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107 if (image_module_sp.get())
1108 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109 ObjectFile *objfile = image_module_sp->GetObjectFile();
1110 if (objfile)
1111 objfile->GetDependentModules(dependent_files);
1112 }
1113 }
1114 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115 }
1116}
1117
1118
Jim Ingham5aee1622010-08-09 23:31:02 +00001119bool
1120Target::SetArchitecture (const ArchSpec &arch_spec)
1121{
Greg Clayton5160ce52013-03-27 23:08:40 +00001122 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001123 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001124 {
Greg Clayton70512312012-05-08 01:45:38 +00001125 // If we haven't got a valid arch spec, or the architectures are
1126 // compatible, so just update the architecture. Architectures can be
1127 // equal, yet the triple OS and vendor might change, so we need to do
1128 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001129 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001130 if (log)
1131 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 +00001132 return true;
1133 }
1134 else
1135 {
1136 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001137 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001138 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 +00001139 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001140 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001141
Greg Claytonb35db632013-11-09 00:03:31 +00001142 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001143 // Need to do something about unsetting breakpoints.
1144
1145 if (executable_sp)
1146 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001147 if (log)
1148 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 +00001149 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1150 Error error = ModuleList::GetSharedModule (module_spec,
1151 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001152 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001153 NULL,
1154 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001155
1156 if (!error.Fail() && executable_sp)
1157 {
1158 SetExecutableModule (executable_sp, true);
1159 return true;
1160 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001161 }
1162 }
Greg Clayton70512312012-05-08 01:45:38 +00001163 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001164}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166void
Enrico Granataefe637d2012-11-08 19:16:03 +00001167Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001168{
1169}
1170
1171void
Enrico Granataefe637d2012-11-08 19:16:03 +00001172Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173{
1174 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001175 if (m_valid)
1176 {
1177 ModuleList my_module_list;
1178 my_module_list.Append(module_sp);
1179 LoadScriptingResourceForModule(module_sp, this);
1180 ModulesDidLoad (my_module_list);
1181 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182}
1183
1184void
Enrico Granataefe637d2012-11-08 19:16:03 +00001185Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001186{
1187 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001188 if (m_valid)
1189 {
1190 ModuleList my_module_list;
1191 my_module_list.Append(module_sp);
1192 ModulesDidUnload (my_module_list, false);
1193 }
Enrico Granata17598482012-11-08 02:22:02 +00001194}
1195
1196void
Enrico Granataefe637d2012-11-08 19:16:03 +00001197Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198{
Jim Inghame716ae02011-08-03 01:00:06 +00001199 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001200 if (m_valid)
1201 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202}
1203
1204void
1205Target::ModulesDidLoad (ModuleList &module_list)
1206{
Greg Clayton23f8c952014-03-24 23:10:19 +00001207 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001208 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001209 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001210 if (m_process_sp)
1211 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001212 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001213 }
Enrico Granata17598482012-11-08 02:22:02 +00001214 // TODO: make event data that packages up the module_list
1215 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1216 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217}
1218
1219void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001220Target::SymbolsDidLoad (ModuleList &module_list)
1221{
Greg Clayton23f8c952014-03-24 23:10:19 +00001222 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001223 {
Jim Ingham31caf982013-06-04 23:01:35 +00001224 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001225 {
Jim Ingham31caf982013-06-04 23:01:35 +00001226 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1227 if (runtime)
1228 {
1229 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1230 objc_runtime->SymbolsDidLoad(module_list);
1231 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001232 }
Jim Ingham31caf982013-06-04 23:01:35 +00001233
Greg Clayton095eeaa2013-11-05 23:28:00 +00001234 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jim Ingham31caf982013-06-04 23:01:35 +00001235 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001236 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001237}
1238
1239void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001240Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241{
Greg Clayton23f8c952014-03-24 23:10:19 +00001242 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001243 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001244 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001245 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Enrico Granata17598482012-11-08 02:22:02 +00001246 // TODO: make event data that packages up the module_list
1247 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1248 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249}
1250
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001251bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001252Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001253{
Greg Clayton67cc0632012-08-22 17:17:09 +00001254 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001255 {
1256 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001257 ModuleSpec module_spec (module_file_spec);
1258 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001259
1260 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1261 // black list.
1262 if (num_modules > 0)
1263 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001264 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001265 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001266 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001267 return false;
1268 }
1269 return true;
1270 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001271 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001272 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001273}
1274
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001275bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001276Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001277{
Greg Clayton67cc0632012-08-22 17:17:09 +00001278 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001279 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001280 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001281 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001282 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001283 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001284}
1285
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286size_t
Greg Claytondb598232011-01-07 01:57:07 +00001287Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1288{
Greg Claytone72dfb32012-02-24 01:59:29 +00001289 SectionSP section_sp (addr.GetSection());
1290 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001291 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001292 // 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 +00001293 if (section_sp->IsEncrypted())
1294 {
Greg Clayton57f06302012-05-25 17:05:55 +00001295 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001296 return 0;
1297 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001298 ModuleSP module_sp (section_sp->GetModule());
1299 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001300 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001301 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1302 if (objfile)
1303 {
1304 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1305 addr.GetOffset(),
1306 dst,
1307 dst_len);
1308 if (bytes_read > 0)
1309 return bytes_read;
1310 else
1311 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1312 }
Greg Claytondb598232011-01-07 01:57:07 +00001313 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001314 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001315 }
1316 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001317 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001318 }
1319 else
Greg Claytondb598232011-01-07 01:57:07 +00001320 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001321
Greg Claytondb598232011-01-07 01:57:07 +00001322 return 0;
1323}
1324
1325size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001326Target::ReadMemory (const Address& addr,
1327 bool prefer_file_cache,
1328 void *dst,
1329 size_t dst_len,
1330 Error &error,
1331 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001334
Enrico Granata9128ee22011-09-06 19:20:51 +00001335 // if we end up reading this from process memory, we will fill this
1336 // with the actual load address
1337 if (load_addr_ptr)
1338 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1339
Greg Claytondb598232011-01-07 01:57:07 +00001340 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001341
1342 addr_t load_addr = LLDB_INVALID_ADDRESS;
1343 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001344 Address resolved_addr;
1345 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001346 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001347 SectionLoadList &section_load_list = GetSectionLoadList();
1348 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001349 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001350 // No sections are loaded, so we must assume we are not running
1351 // yet and anything we are given is a file address.
1352 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1353 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001354 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001355 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001356 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001357 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001358 // we have manually loaded some sections with "target modules load ..."
1359 // or because we have have a live process that has sections loaded
1360 // through the dynamic loader
1361 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001362 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001363 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001364 }
Greg Clayton357132e2011-03-26 19:14:58 +00001365 if (!resolved_addr.IsValid())
1366 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001367
Greg Claytonc749eb82011-07-11 05:12:02 +00001368
Greg Claytondb598232011-01-07 01:57:07 +00001369 if (prefer_file_cache)
1370 {
1371 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1372 if (bytes_read > 0)
1373 return bytes_read;
1374 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001375
Johnny Chen86364b42011-09-20 23:28:55 +00001376 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001377 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001378 if (load_addr == LLDB_INVALID_ADDRESS)
1379 load_addr = resolved_addr.GetLoadAddress (this);
1380
Greg Claytondda4f7b2010-06-30 23:03:03 +00001381 if (load_addr == LLDB_INVALID_ADDRESS)
1382 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001383 ModuleSP addr_module_sp (resolved_addr.GetModule());
1384 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001385 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Greg Claytone72dfb32012-02-24 01:59:29 +00001386 addr_module_sp->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08 +00001387 resolved_addr.GetFileAddress(),
Greg Claytone72dfb32012-02-24 01:59:29 +00001388 addr_module_sp->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001389 else
Daniel Malead01b2952012-11-29 21:49:15 +00001390 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001391 }
1392 else
1393 {
Greg Claytondb598232011-01-07 01:57:07 +00001394 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395 if (bytes_read != dst_len)
1396 {
1397 if (error.Success())
1398 {
1399 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001400 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401 else
Daniel Malead01b2952012-11-29 21:49:15 +00001402 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 +00001403 }
1404 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001405 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001406 {
1407 if (load_addr_ptr)
1408 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001409 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001410 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001411 // If the address is not section offset we have an address that
1412 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001413 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001414 // more we can do. If it is section offset, we might be able to
1415 // read cached memory from the object file.
1416 if (!resolved_addr.IsSectionOffset())
1417 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001418 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001419 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001420
Greg Claytonc749eb82011-07-11 05:12:02 +00001421 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001422 {
Greg Claytondb598232011-01-07 01:57:07 +00001423 // If we didn't already try and read from the object file cache, then
1424 // try it after failing to read from the process.
1425 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001426 }
1427 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428}
1429
Greg Claytond16e1e52011-07-12 17:06:17 +00001430size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001431Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1432{
1433 char buf[256];
1434 out_str.clear();
1435 addr_t curr_addr = addr.GetLoadAddress(this);
1436 Address address(addr);
1437 while (1)
1438 {
1439 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1440 if (length == 0)
1441 break;
1442 out_str.append(buf, length);
1443 // If we got "length - 1" bytes, we didn't get the whole C string, we
1444 // need to read some more characters
1445 if (length == sizeof(buf) - 1)
1446 curr_addr += length;
1447 else
1448 break;
1449 address = Address(curr_addr);
1450 }
1451 return out_str.size();
1452}
1453
1454
1455size_t
1456Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1457{
1458 size_t total_cstr_len = 0;
1459 if (dst && dst_max_len)
1460 {
1461 result_error.Clear();
1462 // NULL out everything just to be safe
1463 memset (dst, 0, dst_max_len);
1464 Error error;
1465 addr_t curr_addr = addr.GetLoadAddress(this);
1466 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001467
1468 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1469 // think this really needs to be tied to the memory cache subsystem's
1470 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001471 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001472
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001473 size_t bytes_left = dst_max_len - 1;
1474 char *curr_dst = dst;
1475
1476 while (bytes_left > 0)
1477 {
1478 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1479 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1480 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1481
1482 if (bytes_read == 0)
1483 {
1484 result_error = error;
1485 dst[total_cstr_len] = '\0';
1486 break;
1487 }
1488 const size_t len = strlen(curr_dst);
1489
1490 total_cstr_len += len;
1491
1492 if (len < bytes_to_read)
1493 break;
1494
1495 curr_dst += bytes_read;
1496 curr_addr += bytes_read;
1497 bytes_left -= bytes_read;
1498 address = Address(curr_addr);
1499 }
1500 }
1501 else
1502 {
1503 if (dst == NULL)
1504 result_error.SetErrorString("invalid arguments");
1505 else
1506 result_error.Clear();
1507 }
1508 return total_cstr_len;
1509}
1510
1511size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001512Target::ReadScalarIntegerFromMemory (const Address& addr,
1513 bool prefer_file_cache,
1514 uint32_t byte_size,
1515 bool is_signed,
1516 Scalar &scalar,
1517 Error &error)
1518{
1519 uint64_t uval;
1520
1521 if (byte_size <= sizeof(uval))
1522 {
1523 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1524 if (bytes_read == byte_size)
1525 {
1526 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001527 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001528 if (byte_size <= 4)
1529 scalar = data.GetMaxU32 (&offset, byte_size);
1530 else
1531 scalar = data.GetMaxU64 (&offset, byte_size);
1532
1533 if (is_signed)
1534 scalar.SignExtend(byte_size * 8);
1535 return bytes_read;
1536 }
1537 }
1538 else
1539 {
1540 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1541 }
1542 return 0;
1543}
1544
1545uint64_t
1546Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1547 bool prefer_file_cache,
1548 size_t integer_byte_size,
1549 uint64_t fail_value,
1550 Error &error)
1551{
1552 Scalar scalar;
1553 if (ReadScalarIntegerFromMemory (addr,
1554 prefer_file_cache,
1555 integer_byte_size,
1556 false,
1557 scalar,
1558 error))
1559 return scalar.ULongLong(fail_value);
1560 return fail_value;
1561}
1562
1563bool
1564Target::ReadPointerFromMemory (const Address& addr,
1565 bool prefer_file_cache,
1566 Error &error,
1567 Address &pointer_addr)
1568{
1569 Scalar scalar;
1570 if (ReadScalarIntegerFromMemory (addr,
1571 prefer_file_cache,
1572 m_arch.GetAddressByteSize(),
1573 false,
1574 scalar,
1575 error))
1576 {
1577 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1578 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1579 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001580 SectionLoadList &section_load_list = GetSectionLoadList();
1581 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001582 {
1583 // No sections are loaded, so we must assume we are not running
1584 // yet and anything we are given is a file address.
1585 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1586 }
1587 else
1588 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001589 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001590 // we have manually loaded some sections with "target modules load ..."
1591 // or because we have have a live process that has sections loaded
1592 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001593 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001594 }
1595 // We weren't able to resolve the pointer value, so just return
1596 // an address with no section
1597 if (!pointer_addr.IsValid())
1598 pointer_addr.SetOffset (pointer_vm_addr);
1599 return true;
1600
1601 }
1602 }
1603 return false;
1604}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001605
1606ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001607Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001608{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001609 ModuleSP module_sp;
1610
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001611 Error error;
1612
Jim Ingham4a94c912012-05-17 18:38:42 +00001613 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1614 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001615
Jim Ingham4a94c912012-05-17 18:38:42 +00001616 if (module_spec.GetUUID().IsValid())
1617 module_sp = m_images.FindFirstModule(module_spec);
1618
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001619 if (!module_sp)
1620 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001621 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1622 bool did_create_module = false;
1623
1624 // If there are image search path entries, try to use them first to acquire a suitable image.
1625 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001626 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001627 ModuleSpec transformed_spec (module_spec);
1628 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1629 {
1630 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1631 error = ModuleList::GetSharedModule (transformed_spec,
1632 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001633 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001634 &old_module_sp,
1635 &did_create_module);
1636 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001637 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001638
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001639 if (!module_sp)
1640 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001641 // If we have a UUID, we can check our global shared module list in case
1642 // we already have it. If we don't have a valid UUID, then we can't since
1643 // the path in "module_spec" will be a platform path, and we will need to
1644 // let the platform find that file. For example, we could be asking for
1645 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1646 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1647 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1648 // cache.
1649 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001650 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001651 // We have a UUID, it is OK to check the global module list...
1652 error = ModuleList::GetSharedModule (module_spec,
1653 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001654 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001655 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001656 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001657 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001658
1659 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001660 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001661 // The platform is responsible for finding and caching an appropriate
1662 // module in the shared module cache.
1663 if (m_platform_sp)
1664 {
1665 FileSpec platform_file_spec;
1666 error = m_platform_sp->GetSharedModule (module_spec,
1667 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001668 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001669 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001670 &did_create_module);
1671 }
1672 else
1673 {
1674 error.SetErrorString("no platform is currently set");
1675 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001676 }
1677 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001678
Jim Ingham4a94c912012-05-17 18:38:42 +00001679 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1680 // module in the list already, and if there was, let's remove it.
1681 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001683 ObjectFile *objfile = module_sp->GetObjectFile();
1684 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001685 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001686 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001687 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001688 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1689 case ObjectFile::eTypeExecutable: /// A normal executable
1690 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1691 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1692 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1693 break;
1694 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1695 if (error_ptr)
1696 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1697 return ModuleSP();
1698 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1699 if (error_ptr)
1700 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1701 return ModuleSP();
1702 default:
1703 if (error_ptr)
1704 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1705 return ModuleSP();
1706 }
1707 // GetSharedModule is not guaranteed to find the old shared module, for instance
1708 // in the common case where you pass in the UUID, it is only going to find the one
1709 // module matching the UUID. In fact, it has no good way to know what the "old module"
1710 // relevant to this target is, since there might be many copies of a module with this file spec
1711 // in various running debug sessions, but only one of them will belong to this target.
1712 // So let's remove the UUID from the module list, and look in the target's module list.
1713 // Only do this if there is SOMETHING else in the module spec...
1714 if (!old_module_sp)
1715 {
1716 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001717 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001718 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1719 module_spec_copy.GetUUID().Clear();
1720
1721 ModuleList found_modules;
1722 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1723 if (num_found == 1)
1724 {
1725 old_module_sp = found_modules.GetModuleAtIndex(0);
1726 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001727 }
1728 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001729
1730 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1731 {
1732 m_images.ReplaceModule(old_module_sp, module_sp);
1733 Module *old_module_ptr = old_module_sp.get();
1734 old_module_sp.reset();
1735 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1736 }
1737 else
1738 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001739 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001740 else
1741 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001742 }
1743 }
1744 if (error_ptr)
1745 *error_ptr = error;
1746 return module_sp;
1747}
1748
1749
Greg Claytond9e416c2012-02-18 05:35:26 +00001750TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751Target::CalculateTarget ()
1752{
Greg Claytond9e416c2012-02-18 05:35:26 +00001753 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001754}
1755
Greg Claytond9e416c2012-02-18 05:35:26 +00001756ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001757Target::CalculateProcess ()
1758{
Greg Claytond9e416c2012-02-18 05:35:26 +00001759 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001760}
1761
Greg Claytond9e416c2012-02-18 05:35:26 +00001762ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001763Target::CalculateThread ()
1764{
Greg Claytond9e416c2012-02-18 05:35:26 +00001765 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001766}
1767
Jason Molendab57e4a12013-11-04 09:33:30 +00001768StackFrameSP
1769Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001770{
Jason Molendab57e4a12013-11-04 09:33:30 +00001771 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772}
1773
1774void
Greg Clayton0603aa92010-10-04 01:05:56 +00001775Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001776{
Greg Claytonc14ee322011-09-22 04:58:26 +00001777 exe_ctx.Clear();
1778 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001779}
1780
1781PathMappingList &
1782Target::GetImageSearchPathList ()
1783{
1784 return m_image_search_paths;
1785}
1786
1787void
1788Target::ImageSearchPathsChanged
1789(
1790 const PathMappingList &path_list,
1791 void *baton
1792)
1793{
1794 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001795 ModuleSP exe_module_sp (target->GetExecutableModule());
1796 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001797 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001798}
1799
1800ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001801Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001802{
Greg Clayton73da2442011-08-03 01:23:55 +00001803 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001804 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001805 {
Greg Clayton73da2442011-08-03 01:23:55 +00001806 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001807 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001808 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001809 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001810 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1811 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001812 return m_scratch_ast_context_ap.get();
1813}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001814
Sean Callanan686b2312011-11-16 18:20:47 +00001815ClangASTImporter *
1816Target::GetClangASTImporter()
1817{
1818 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1819
1820 if (!ast_importer)
1821 {
1822 ast_importer = new ClangASTImporter();
1823 m_ast_importer_ap.reset(ast_importer);
1824 }
1825
1826 return ast_importer;
1827}
1828
Greg Clayton99d0faf2010-11-18 23:32:35 +00001829void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001830Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001831{
Greg Clayton6920b522012-08-22 18:39:03 +00001832 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001833}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001834
Greg Clayton99d0faf2010-11-18 23:32:35 +00001835void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001836Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001837{
Greg Clayton6920b522012-08-22 18:39:03 +00001838 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001839}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001840
Greg Claytonc859e2d2012-02-13 23:10:39 +00001841FileSpecList
1842Target::GetDefaultExecutableSearchPaths ()
1843{
Greg Clayton67cc0632012-08-22 17:17:09 +00001844 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1845 if (properties_sp)
1846 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001847 return FileSpecList();
1848}
1849
Michael Sartaina7499c92013-07-01 19:45:50 +00001850FileSpecList
1851Target::GetDefaultDebugFileSearchPaths ()
1852{
1853 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1854 if (properties_sp)
1855 return properties_sp->GetDebugFileSearchPaths();
1856 return FileSpecList();
1857}
1858
Caroline Ticedaccaa92010-09-20 20:44:43 +00001859ArchSpec
1860Target::GetDefaultArchitecture ()
1861{
Greg Clayton67cc0632012-08-22 17:17:09 +00001862 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1863 if (properties_sp)
1864 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001865 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001866}
1867
1868void
Greg Clayton67cc0632012-08-22 17:17:09 +00001869Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001870{
Greg Clayton67cc0632012-08-22 17:17:09 +00001871 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1872 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001873 {
1874 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 +00001875 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001876 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001877}
1878
Greg Clayton0603aa92010-10-04 01:05:56 +00001879Target *
1880Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1881{
1882 // The target can either exist in the "process" of ExecutionContext, or in
1883 // the "target_sp" member of SymbolContext. This accessor helper function
1884 // will get the target from one of these locations.
1885
1886 Target *target = NULL;
1887 if (sc_ptr != NULL)
1888 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001889 if (target == NULL && exe_ctx_ptr)
1890 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001891 return target;
1892}
1893
Jim Ingham1624a2d2014-05-05 02:26:40 +00001894ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001895Target::EvaluateExpression
1896(
1897 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001898 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001899 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001900 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001901)
1902{
Enrico Granata97fca502012-09-18 17:43:16 +00001903 result_valobj_sp.reset();
1904
Jim Ingham8646d3c2014-05-05 02:47:44 +00001905 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001906
Greg Claytond1767f02011-12-08 02:13:16 +00001907 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1908 return execution_results;
1909
Jim Ingham6026ca32011-05-12 02:06:14 +00001910 // We shouldn't run stop hooks in expressions.
1911 // Be sure to reset this if you return anywhere within this function.
1912 bool old_suppress_value = m_suppress_stop_hooks;
1913 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001914
1915 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001916
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001917 if (frame)
1918 {
1919 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001920 }
1921 else if (m_process_sp)
1922 {
1923 m_process_sp->CalculateExecutionContext(exe_ctx);
1924 }
1925 else
1926 {
1927 CalculateExecutionContext(exe_ctx);
1928 }
1929
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001930 // Make sure we aren't just trying to see the value of a persistent
1931 // variable (something like "$0")
1932 lldb::ClangExpressionVariableSP persistent_var_sp;
1933 // Only check for persistent variables the expression starts with a '$'
1934 if (expr_cstr[0] == '$')
1935 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1936
1937 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001938 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001939 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00001940 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001941 }
1942 else
1943 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001944 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00001945 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001946 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001947 options,
1948 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001949 prefix,
1950 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00001951 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001952 }
Jim Ingham6026ca32011-05-12 02:06:14 +00001953
1954 m_suppress_stop_hooks = old_suppress_value;
1955
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001956 return execution_results;
1957}
1958
Greg Claytonf3ef3d22011-05-22 22:46:53 +00001959lldb::addr_t
1960Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1961{
1962 addr_t code_addr = load_addr;
1963 switch (m_arch.GetMachine())
1964 {
1965 case llvm::Triple::arm:
1966 case llvm::Triple::thumb:
1967 switch (addr_class)
1968 {
1969 case eAddressClassData:
1970 case eAddressClassDebug:
1971 return LLDB_INVALID_ADDRESS;
1972
1973 case eAddressClassUnknown:
1974 case eAddressClassInvalid:
1975 case eAddressClassCode:
1976 case eAddressClassCodeAlternateISA:
1977 case eAddressClassRuntime:
1978 // Check if bit zero it no set?
1979 if ((code_addr & 1ull) == 0)
1980 {
1981 // Bit zero isn't set, check if the address is a multiple of 2?
1982 if (code_addr & 2ull)
1983 {
1984 // The address is a multiple of 2 so it must be thumb, set bit zero
1985 code_addr |= 1ull;
1986 }
1987 else if (addr_class == eAddressClassCodeAlternateISA)
1988 {
1989 // We checked the address and the address claims to be the alternate ISA
1990 // which means thumb, so set bit zero.
1991 code_addr |= 1ull;
1992 }
1993 }
1994 break;
1995 }
1996 break;
1997
1998 default:
1999 break;
2000 }
2001 return code_addr;
2002}
2003
2004lldb::addr_t
2005Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2006{
2007 addr_t opcode_addr = load_addr;
2008 switch (m_arch.GetMachine())
2009 {
2010 case llvm::Triple::arm:
2011 case llvm::Triple::thumb:
2012 switch (addr_class)
2013 {
2014 case eAddressClassData:
2015 case eAddressClassDebug:
2016 return LLDB_INVALID_ADDRESS;
2017
2018 case eAddressClassInvalid:
2019 case eAddressClassUnknown:
2020 case eAddressClassCode:
2021 case eAddressClassCodeAlternateISA:
2022 case eAddressClassRuntime:
2023 opcode_addr &= ~(1ull);
2024 break;
2025 }
2026 break;
2027
2028 default:
2029 break;
2030 }
2031 return opcode_addr;
2032}
2033
Greg Clayton9585fbf2013-03-19 00:20:55 +00002034SourceManager &
2035Target::GetSourceManager ()
2036{
2037 if (m_source_manager_ap.get() == NULL)
2038 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2039 return *m_source_manager_ap;
2040}
2041
Sean Callanan9998acd2014-12-05 01:21:59 +00002042ClangModulesDeclVendor *
2043Target::GetClangModulesDeclVendor ()
2044{
2045 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2046
2047 {
2048 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2049
2050 if (!m_clang_modules_decl_vendor_ap)
2051 {
2052 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2053 }
2054 }
2055
2056 return m_clang_modules_decl_vendor_ap.get();
2057}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002058
Greg Clayton44d93782014-01-27 23:43:24 +00002059Target::StopHookSP
2060Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002061{
2062 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002063 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2064 m_stop_hooks[new_uid] = stop_hook_sp;
2065 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002066}
2067
2068bool
2069Target::RemoveStopHookByID (lldb::user_id_t user_id)
2070{
2071 size_t num_removed;
2072 num_removed = m_stop_hooks.erase (user_id);
2073 if (num_removed == 0)
2074 return false;
2075 else
2076 return true;
2077}
2078
2079void
2080Target::RemoveAllStopHooks ()
2081{
2082 m_stop_hooks.clear();
2083}
2084
2085Target::StopHookSP
2086Target::GetStopHookByID (lldb::user_id_t user_id)
2087{
2088 StopHookSP found_hook;
2089
2090 StopHookCollection::iterator specified_hook_iter;
2091 specified_hook_iter = m_stop_hooks.find (user_id);
2092 if (specified_hook_iter != m_stop_hooks.end())
2093 found_hook = (*specified_hook_iter).second;
2094 return found_hook;
2095}
2096
2097bool
2098Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2099{
2100 StopHookCollection::iterator specified_hook_iter;
2101 specified_hook_iter = m_stop_hooks.find (user_id);
2102 if (specified_hook_iter == m_stop_hooks.end())
2103 return false;
2104
2105 (*specified_hook_iter).second->SetIsActive (active_state);
2106 return true;
2107}
2108
2109void
2110Target::SetAllStopHooksActiveState (bool active_state)
2111{
2112 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2113 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2114 {
2115 (*pos).second->SetIsActive (active_state);
2116 }
2117}
2118
2119void
2120Target::RunStopHooks ()
2121{
Jim Ingham6026ca32011-05-12 02:06:14 +00002122 if (m_suppress_stop_hooks)
2123 return;
2124
Jim Ingham9575d842011-03-11 03:53:59 +00002125 if (!m_process_sp)
2126 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002127
2128 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2129 // since in that case we do not want to run the stop-hooks
2130 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2131 return;
2132
Jim Ingham9575d842011-03-11 03:53:59 +00002133 if (m_stop_hooks.empty())
2134 return;
2135
2136 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2137
2138 // If there aren't any active stop hooks, don't bother either:
2139 bool any_active_hooks = false;
2140 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2141 {
2142 if ((*pos).second->IsActive())
2143 {
2144 any_active_hooks = true;
2145 break;
2146 }
2147 }
2148 if (!any_active_hooks)
2149 return;
2150
2151 CommandReturnObject result;
2152
2153 std::vector<ExecutionContext> exc_ctx_with_reasons;
2154 std::vector<SymbolContext> sym_ctx_with_reasons;
2155
2156 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2157 size_t num_threads = cur_threadlist.GetSize();
2158 for (size_t i = 0; i < num_threads; i++)
2159 {
2160 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2161 if (cur_thread_sp->ThreadStoppedForAReason())
2162 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002163 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002164 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2165 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2166 }
2167 }
2168
2169 // If no threads stopped for a reason, don't run the stop-hooks.
2170 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2171 if (num_exe_ctx == 0)
2172 return;
2173
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002174 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2175 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002176
2177 bool keep_going = true;
2178 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002179 bool print_hook_header;
2180 bool print_thread_header;
2181
2182 if (num_exe_ctx == 1)
2183 print_thread_header = false;
2184 else
2185 print_thread_header = true;
2186
2187 if (m_stop_hooks.size() == 1)
2188 print_hook_header = false;
2189 else
2190 print_hook_header = true;
2191
Jim Ingham9575d842011-03-11 03:53:59 +00002192 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2193 {
2194 // result.Clear();
2195 StopHookSP cur_hook_sp = (*pos).second;
2196 if (!cur_hook_sp->IsActive())
2197 continue;
2198
2199 bool any_thread_matched = false;
2200 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2201 {
2202 if ((cur_hook_sp->GetSpecifier () == NULL
2203 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2204 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002205 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002206 {
2207 if (!hooks_ran)
2208 {
Jim Ingham9575d842011-03-11 03:53:59 +00002209 hooks_ran = true;
2210 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002211 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002212 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002213 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2214 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2215 NULL);
2216 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002217 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002218 else
Daniel Malead01b2952012-11-29 21:49:15 +00002219 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002220 any_thread_matched = true;
2221 }
2222
Jim Ingham381e25b2011-03-22 01:47:27 +00002223 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002224 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002225
2226 CommandInterpreterRunOptions options;
2227 options.SetStopOnContinue (true);
2228 options.SetStopOnError (true);
2229 options.SetEchoCommands (false);
2230 options.SetPrintResults (true);
2231 options.SetAddToHistory (false);
2232
2233 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2234 &exc_ctx_with_reasons[i],
2235 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002236 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002237
2238 // If the command started the target going again, we should bag out of
2239 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002240 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2241 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002242 {
Daniel Malead01b2952012-11-29 21:49:15 +00002243 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002244 keep_going = false;
2245 }
2246 }
2247 }
2248 }
Jason Molenda879cf772011-09-23 00:42:55 +00002249
Caroline Tice969ed3d2011-05-02 20:41:46 +00002250 result.GetImmediateOutputStream()->Flush();
2251 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002252}
2253
Greg Claytonfbb76342013-11-20 21:07:01 +00002254const TargetPropertiesSP &
2255Target::GetGlobalProperties()
2256{
2257 static TargetPropertiesSP g_settings_sp;
2258 if (!g_settings_sp)
2259 {
2260 g_settings_sp.reset (new TargetProperties (NULL));
2261 }
2262 return g_settings_sp;
2263}
2264
2265Error
2266Target::Install (ProcessLaunchInfo *launch_info)
2267{
2268 Error error;
2269 PlatformSP platform_sp (GetPlatform());
2270 if (platform_sp)
2271 {
2272 if (platform_sp->IsRemote())
2273 {
2274 if (platform_sp->IsConnected())
2275 {
2276 // Install all files that have an install path, and always install the
2277 // main executable when connected to a remote platform
2278 const ModuleList& modules = GetImages();
2279 const size_t num_images = modules.GetSize();
2280 for (size_t idx = 0; idx < num_images; ++idx)
2281 {
2282 const bool is_main_executable = idx == 0;
2283 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2284 if (module_sp)
2285 {
2286 FileSpec local_file (module_sp->GetFileSpec());
2287 if (local_file)
2288 {
2289 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2290 if (!remote_file)
2291 {
2292 if (is_main_executable) // TODO: add setting for always installing main executable???
2293 {
2294 // Always install the main executable
2295 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
2296 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename();
2297 }
2298 }
2299 if (remote_file)
2300 {
2301 error = platform_sp->Install(local_file, remote_file);
2302 if (error.Success())
2303 {
2304 module_sp->SetPlatformFileSpec(remote_file);
2305 if (is_main_executable)
2306 {
2307 if (launch_info)
2308 launch_info->SetExecutableFile(remote_file, false);
2309 }
2310 }
2311 else
2312 break;
2313 }
2314 }
2315 }
2316 }
2317 }
2318 }
2319 }
2320 return error;
2321}
Greg Clayton7b242382011-07-08 00:48:09 +00002322
Greg Claytond5944cd2013-12-06 01:12:00 +00002323bool
2324Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2325{
2326 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2327}
2328
2329bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002330Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2331{
2332 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2333}
2334
2335bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002336Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2337{
2338 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2339 if (old_section_load_addr != new_section_load_addr)
2340 {
2341 uint32_t stop_id = 0;
2342 ProcessSP process_sp(GetProcessSP());
2343 if (process_sp)
2344 stop_id = process_sp->GetStopID();
2345 else
2346 stop_id = m_section_load_history.GetLastStopID();
2347 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2348 return true; // Return true if the section load address was changed...
2349 }
2350 return false; // Return false to indicate nothing changed
2351
2352}
2353
Greg Clayton8012cad2014-11-17 19:39:20 +00002354size_t
2355Target::UnloadModuleSections (const ModuleList &module_list)
2356{
2357 size_t section_unload_count = 0;
2358 size_t num_modules = module_list.GetSize();
2359 for (size_t i=0; i<num_modules; ++i)
2360 {
2361 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2362 }
2363 return section_unload_count;
2364}
2365
2366size_t
2367Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2368{
2369 uint32_t stop_id = 0;
2370 ProcessSP process_sp(GetProcessSP());
2371 if (process_sp)
2372 stop_id = process_sp->GetStopID();
2373 else
2374 stop_id = m_section_load_history.GetLastStopID();
2375 SectionList *sections = module_sp->GetSectionList();
2376 size_t section_unload_count = 0;
2377 if (sections)
2378 {
2379 const uint32_t num_sections = sections->GetNumSections(0);
2380 for (uint32_t i = 0; i < num_sections; ++i)
2381 {
2382 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2383 }
2384 }
2385 return section_unload_count;
2386}
2387
Greg Claytond5944cd2013-12-06 01:12:00 +00002388bool
2389Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2390{
2391 uint32_t stop_id = 0;
2392 ProcessSP process_sp(GetProcessSP());
2393 if (process_sp)
2394 stop_id = process_sp->GetStopID();
2395 else
2396 stop_id = m_section_load_history.GetLastStopID();
2397 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2398}
2399
2400bool
2401Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2402{
2403 uint32_t stop_id = 0;
2404 ProcessSP process_sp(GetProcessSP());
2405 if (process_sp)
2406 stop_id = process_sp->GetStopID();
2407 else
2408 stop_id = m_section_load_history.GetLastStopID();
2409 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2410}
2411
2412void
2413Target::ClearAllLoadedSections ()
2414{
2415 m_section_load_history.Clear();
2416}
2417
Greg Claytonb09c5382013-12-13 17:20:18 +00002418
2419Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002420Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002421{
2422 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002423 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2424
2425 if (log)
2426 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2427
Greg Claytonb09c5382013-12-13 17:20:18 +00002428 StateType state = eStateInvalid;
2429
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002430 // Scope to temporarily get the process state in case someone has manually
2431 // remotely connected already to a process and we can skip the platform
2432 // launching.
2433 {
2434 ProcessSP process_sp (GetProcessSP());
2435
2436 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002437 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002438 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002439 if (log)
2440 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2441 }
2442 else
2443 {
2444 if (log)
2445 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2446 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002447 }
2448
Greg Claytonb09c5382013-12-13 17:20:18 +00002449 launch_info.GetFlags().Set (eLaunchFlagDebug);
2450
2451 // Get the value of synchronous execution here. If you wait till after you have started to
2452 // run, then you could have hit a breakpoint, whose command might switch the value, and
2453 // then you'll pick up that incorrect value.
2454 Debugger &debugger = GetDebugger();
2455 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2456
2457 PlatformSP platform_sp (GetPlatform());
2458
2459 // Finalize the file actions, and if none were given, default to opening
2460 // up a pseudo terminal
2461 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002462 if (log)
2463 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2464 __FUNCTION__,
2465 platform_sp ? "true" : "false",
2466 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2467 default_to_use_pty ? "true" : "false");
2468
Greg Claytonb09c5382013-12-13 17:20:18 +00002469 launch_info.FinalizeFileActions (this, default_to_use_pty);
2470
2471 if (state == eStateConnected)
2472 {
2473 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2474 {
2475 error.SetErrorString("can't launch in tty when launching through a remote connection");
2476 return error;
2477 }
2478 }
2479
2480 if (!launch_info.GetArchitecture().IsValid())
2481 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002482
2483 // 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 +00002484 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2485 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002486 if (log)
2487 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2488
Greg Claytonb09c5382013-12-13 17:20:18 +00002489 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2490 debugger,
2491 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002492 error);
2493 }
2494 else
2495 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002496 if (log)
2497 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2498
Greg Claytonb09c5382013-12-13 17:20:18 +00002499 if (state == eStateConnected)
2500 {
2501 assert(m_process_sp);
2502 }
2503 else
2504 {
Todd Fiala015d8182014-07-22 23:41:36 +00002505 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002506 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002507 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002508 }
Todd Fiala015d8182014-07-22 23:41:36 +00002509
2510 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002511 if (m_process_sp)
2512 error = m_process_sp->Launch (launch_info);
2513 }
2514
2515 if (!m_process_sp)
2516 {
2517 if (error.Success())
2518 error.SetErrorString("failed to launch or debug process");
2519 return error;
2520 }
2521
2522 if (error.Success())
2523 {
2524 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
2525 {
Greg Clayton44d93782014-01-27 23:43:24 +00002526 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00002527
Greg Claytondc6224e2014-10-21 01:00:42 +00002528 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002529
2530 if (state == eStateStopped)
2531 {
Greg Clayton44d93782014-01-27 23:43:24 +00002532 if (!synchronous_execution)
2533 m_process_sp->RestoreProcessEvents ();
2534
2535 error = m_process_sp->PrivateResume();
Todd Fialaa3b89e22014-08-12 14:33:19 +00002536
Greg Claytonb09c5382013-12-13 17:20:18 +00002537 if (error.Success())
2538 {
Todd Fialaa3b89e22014-08-12 14:33:19 +00002539 // there is a race condition where this thread will return up the call stack to the main command
2540 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
2541 // a chance to call PushProcessIOHandler()
2542 m_process_sp->SyncIOHandler(2000);
2543
Greg Claytonb09c5382013-12-13 17:20:18 +00002544 if (synchronous_execution)
2545 {
Greg Claytondc6224e2014-10-21 01:00:42 +00002546 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
Greg Claytonb09c5382013-12-13 17:20:18 +00002547 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2548 if (!StateIsStoppedState(state, must_be_alive))
2549 {
Greg Clayton44d93782014-01-27 23:43:24 +00002550 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
Greg Claytonb09c5382013-12-13 17:20:18 +00002551 }
2552 }
2553 }
2554 else
2555 {
Greg Clayton44d93782014-01-27 23:43:24 +00002556 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002557 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002558 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002559 }
2560 }
Greg Clayton40286e02014-04-30 20:29:09 +00002561 else if (state == eStateExited)
2562 {
Zachary Turner10687b02014-10-20 17:46:43 +00002563 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002564 const int exit_status = m_process_sp->GetExitStatus();
2565 const char *exit_desc = m_process_sp->GetExitDescription();
2566#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'."
2567 if (exit_desc && exit_desc[0])
2568 {
2569 if (with_shell)
2570 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2571 else
2572 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2573 }
2574 else
2575 {
2576 if (with_shell)
2577 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2578 else
2579 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2580 }
2581 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002582 else
2583 {
2584 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2585 }
2586 }
Greg Clayton44d93782014-01-27 23:43:24 +00002587 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002588 }
2589 else
2590 {
Greg Clayton44d93782014-01-27 23:43:24 +00002591 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002592 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002593 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002594 }
2595 return error;
2596}
Jim Ingham9575d842011-03-11 03:53:59 +00002597//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002598// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002599//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002600Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2601 UserID (uid),
2602 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002603 m_commands (),
2604 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002605 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002606 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002607{
2608}
2609
2610Target::StopHook::StopHook (const StopHook &rhs) :
2611 UserID (rhs.GetID()),
2612 m_target_sp (rhs.m_target_sp),
2613 m_commands (rhs.m_commands),
2614 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002615 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002616 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002617{
2618 if (rhs.m_thread_spec_ap.get() != NULL)
2619 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2620}
2621
2622
2623Target::StopHook::~StopHook ()
2624{
2625}
2626
2627void
2628Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2629{
2630 m_thread_spec_ap.reset (specifier);
2631}
2632
2633
2634void
2635Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2636{
2637 int indent_level = s->GetIndentLevel();
2638
2639 s->SetIndentLevel(indent_level + 2);
2640
Daniel Malead01b2952012-11-29 21:49:15 +00002641 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002642 if (m_active)
2643 s->Indent ("State: enabled\n");
2644 else
2645 s->Indent ("State: disabled\n");
2646
2647 if (m_specifier_sp)
2648 {
2649 s->Indent();
2650 s->PutCString ("Specifier:\n");
2651 s->SetIndentLevel (indent_level + 4);
2652 m_specifier_sp->GetDescription (s, level);
2653 s->SetIndentLevel (indent_level + 2);
2654 }
2655
2656 if (m_thread_spec_ap.get() != NULL)
2657 {
2658 StreamString tmp;
2659 s->Indent("Thread:\n");
2660 m_thread_spec_ap->GetDescription (&tmp, level);
2661 s->SetIndentLevel (indent_level + 4);
2662 s->Indent (tmp.GetData());
2663 s->PutCString ("\n");
2664 s->SetIndentLevel (indent_level + 2);
2665 }
2666
2667 s->Indent ("Commands: \n");
2668 s->SetIndentLevel (indent_level + 4);
2669 uint32_t num_commands = m_commands.GetSize();
2670 for (uint32_t i = 0; i < num_commands; i++)
2671 {
2672 s->Indent(m_commands.GetStringAtIndex(i));
2673 s->PutCString ("\n");
2674 }
2675 s->SetIndentLevel (indent_level);
2676}
2677
Greg Clayton67cc0632012-08-22 17:17:09 +00002678//--------------------------------------------------------------
2679// class TargetProperties
2680//--------------------------------------------------------------
2681
2682OptionEnumValueElement
2683lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002684{
Greg Clayton67cc0632012-08-22 17:17:09 +00002685 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2686 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2687 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2688 { 0, NULL, NULL }
2689};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002690
Greg Clayton1f746072012-08-29 21:13:06 +00002691static OptionEnumValueElement
2692g_inline_breakpoint_enums[] =
2693{
2694 { 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."},
2695 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2696 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2697 { 0, NULL, NULL }
2698};
2699
Jim Ingham0f063ba2013-03-02 00:26:47 +00002700typedef enum x86DisassemblyFlavor
2701{
2702 eX86DisFlavorDefault,
2703 eX86DisFlavorIntel,
2704 eX86DisFlavorATT
2705} x86DisassemblyFlavor;
2706
2707static OptionEnumValueElement
2708g_x86_dis_flavor_value_types[] =
2709{
2710 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2711 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2712 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2713 { 0, NULL, NULL }
2714};
2715
Enrico Granata397ddd52013-05-21 20:13:34 +00002716static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002717g_hex_immediate_style_values[] =
2718{
2719 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2720 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2721 { 0, NULL, NULL }
2722};
2723
2724static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002725g_load_script_from_sym_file_values[] =
2726{
2727 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2728 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2729 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2730 { 0, NULL, NULL }
2731};
2732
Greg Claytonfd814c52013-08-13 01:42:25 +00002733
2734static OptionEnumValueElement
2735g_memory_module_load_level_values[] =
2736{
Greg Clayton86eac942013-08-13 21:32:34 +00002737 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002738 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2739 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2740 { 0, NULL, NULL }
2741};
2742
Greg Clayton67cc0632012-08-22 17:17:09 +00002743static PropertyDefinition
2744g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002745{
Greg Clayton67cc0632012-08-22 17:17:09 +00002746 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2747 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
2748 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eNoDynamicValues , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
2749 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2750 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2751 { "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 "
2752 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2753 "some part (starting at the root) of the path to the file when it was built, "
2754 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2755 "Each element of the array is checked in order and the first one that results in a match wins." },
2756 { "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 +00002757 { "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 +00002758 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2759 { "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 +00002760 { "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 +00002761 { "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 +00002762 { "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." },
2763 { "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 +00002764 { "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." },
2765 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2766 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2767 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2768 { "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 +00002769 { "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 +00002770 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2771 { "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 +00002772 { "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 +00002773 "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. "
2774 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2775 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002776 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2777 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2778 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002779 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002780 // 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.
2781 { "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 +00002782 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2783 { "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 +00002784 { "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 +00002785 { "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 +00002786 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2787 "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. "
2788 "This setting helps users control how much information gets loaded when loading modules from memory."
2789 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2790 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2791 "'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 +00002792 { "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 +00002793 { "trap-handler-names" , OptionValue::eTypeArray , true, OptionValue::eTypeString, NULL, NULL, "A list of trap handler function names, e.g. a common Unix user process one is _sigtramp." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002794 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2795};
2796enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002797{
Greg Clayton67cc0632012-08-22 17:17:09 +00002798 ePropertyDefaultArch,
2799 ePropertyExprPrefix,
2800 ePropertyPreferDynamic,
2801 ePropertyEnableSynthetic,
2802 ePropertySkipPrologue,
2803 ePropertySourceMap,
2804 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002805 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002806 ePropertyMaxChildrenCount,
2807 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002808 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002809 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002810 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002811 ePropertyRunArgs,
2812 ePropertyEnvVars,
2813 ePropertyInheritEnv,
2814 ePropertyInputPath,
2815 ePropertyOutputPath,
2816 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00002817 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00002818 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00002819 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002820 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00002821 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00002822 ePropertyUseHexImmediates,
2823 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00002824 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00002825 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00002826 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00002827 ePropertyDisplayExpressionsInCrashlogs,
2828 ePropertyTrapHandlerNames
Greg Clayton67cc0632012-08-22 17:17:09 +00002829};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002830
Caroline Ticedaccaa92010-09-20 20:44:43 +00002831
Greg Clayton67cc0632012-08-22 17:17:09 +00002832class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002833{
Greg Clayton67cc0632012-08-22 17:17:09 +00002834public:
2835 TargetOptionValueProperties (const ConstString &name) :
2836 OptionValueProperties (name),
2837 m_target (NULL),
2838 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002839 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00002840 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002841
Greg Clayton67cc0632012-08-22 17:17:09 +00002842 // This constructor is used when creating TargetOptionValueProperties when it
2843 // is part of a new lldb_private::Target instance. It will copy all current
2844 // global property values as needed
2845 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
2846 OptionValueProperties(*target_properties_sp->GetValueProperties()),
2847 m_target (target),
2848 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002849 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002850 }
2851
2852 virtual const Property *
2853 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
2854 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002855 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00002856 // try and grab the setting from the current target if there is one. Else we just
2857 // use the one from this instance.
2858 if (idx == ePropertyEnvVars)
2859 GetHostEnvironmentIfNeeded ();
2860
2861 if (exe_ctx)
2862 {
2863 Target *target = exe_ctx->GetTargetPtr();
2864 if (target)
2865 {
2866 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
2867 if (this != target_properties)
2868 return target_properties->ProtectedGetPropertyAtIndex (idx);
2869 }
2870 }
2871 return ProtectedGetPropertyAtIndex (idx);
2872 }
Enrico Granata84a53df2013-05-20 22:29:23 +00002873
2874 lldb::TargetSP
2875 GetTargetSP ()
2876 {
2877 return m_target->shared_from_this();
2878 }
2879
Greg Clayton67cc0632012-08-22 17:17:09 +00002880protected:
2881
2882 void
2883 GetHostEnvironmentIfNeeded () const
2884 {
2885 if (!m_got_host_env)
2886 {
2887 if (m_target)
2888 {
2889 m_got_host_env = true;
2890 const uint32_t idx = ePropertyInheritEnv;
2891 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
2892 {
2893 PlatformSP platform_sp (m_target->GetPlatform());
2894 if (platform_sp)
2895 {
2896 StringList env;
2897 if (platform_sp->GetEnvironment(env))
2898 {
2899 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
2900 if (env_dict)
2901 {
2902 const bool can_replace = false;
2903 const size_t envc = env.GetSize();
2904 for (size_t idx=0; idx<envc; idx++)
2905 {
2906 const char *env_entry = env.GetStringAtIndex (idx);
2907 if (env_entry)
2908 {
2909 const char *equal_pos = ::strchr(env_entry, '=');
2910 ConstString key;
2911 // It is ok to have environment variables with no values
2912 const char *value = NULL;
2913 if (equal_pos)
2914 {
2915 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
2916 if (equal_pos[1])
2917 value = equal_pos + 1;
2918 }
2919 else
2920 {
2921 key.SetCString(env_entry);
2922 }
2923 // Don't allow existing keys to be replaced with ones we get from the platform environment
2924 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
2925 }
2926 }
2927 }
2928 }
2929 }
2930 }
2931 }
2932 }
2933 }
2934 Target *m_target;
2935 mutable bool m_got_host_env;
2936};
2937
Greg Claytonfbb76342013-11-20 21:07:01 +00002938//----------------------------------------------------------------------
2939// TargetProperties
2940//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00002941TargetProperties::TargetProperties (Target *target) :
2942 Properties ()
2943{
2944 if (target)
2945 {
2946 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Caroline Ticedaccaa92010-09-20 20:44:43 +00002947 }
2948 else
Greg Clayton67cc0632012-08-22 17:17:09 +00002949 {
2950 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
2951 m_collection_sp->Initialize(g_properties);
2952 m_collection_sp->AppendProperty(ConstString("process"),
2953 ConstString("Settings specify to processes."),
2954 true,
2955 Process::GetGlobalProperties()->GetValueProperties());
2956 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002957}
2958
Greg Clayton67cc0632012-08-22 17:17:09 +00002959TargetProperties::~TargetProperties ()
2960{
2961}
2962ArchSpec
2963TargetProperties::GetDefaultArchitecture () const
2964{
2965 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2966 if (value)
2967 return value->GetCurrentValue();
2968 return ArchSpec();
2969}
2970
2971void
2972TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
2973{
2974 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
2975 if (value)
2976 return value->SetCurrentValue(arch, true);
2977}
2978
2979lldb::DynamicValueType
2980TargetProperties::GetPreferDynamicValue() const
2981{
2982 const uint32_t idx = ePropertyPreferDynamic;
2983 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
2984}
2985
2986bool
2987TargetProperties::GetDisableASLR () const
2988{
2989 const uint32_t idx = ePropertyDisableASLR;
2990 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
2991}
2992
2993void
2994TargetProperties::SetDisableASLR (bool b)
2995{
2996 const uint32_t idx = ePropertyDisableASLR;
2997 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
2998}
2999
3000bool
Jim Ingham106d0282014-06-25 02:32:56 +00003001TargetProperties::GetDetachOnError () const
3002{
3003 const uint32_t idx = ePropertyDetachOnError;
3004 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3005}
3006
3007void
3008TargetProperties::SetDetachOnError (bool b)
3009{
3010 const uint32_t idx = ePropertyDetachOnError;
3011 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3012}
3013
3014bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003015TargetProperties::GetDisableSTDIO () const
3016{
3017 const uint32_t idx = ePropertyDisableSTDIO;
3018 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3019}
3020
3021void
3022TargetProperties::SetDisableSTDIO (bool b)
3023{
3024 const uint32_t idx = ePropertyDisableSTDIO;
3025 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3026}
3027
Jim Ingham0f063ba2013-03-02 00:26:47 +00003028const char *
3029TargetProperties::GetDisassemblyFlavor () const
3030{
3031 const uint32_t idx = ePropertyDisassemblyFlavor;
3032 const char *return_value;
3033
3034 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3035 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3036 return return_value;
3037}
3038
Greg Clayton1f746072012-08-29 21:13:06 +00003039InlineStrategy
3040TargetProperties::GetInlineStrategy () const
3041{
3042 const uint32_t idx = ePropertyInlineStrategy;
3043 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3044}
3045
Greg Clayton45392552012-10-17 22:57:12 +00003046const char *
3047TargetProperties::GetArg0 () const
3048{
3049 const uint32_t idx = ePropertyArg0;
3050 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3051}
3052
3053void
3054TargetProperties::SetArg0 (const char *arg)
3055{
3056 const uint32_t idx = ePropertyArg0;
3057 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
3058}
3059
Greg Clayton67cc0632012-08-22 17:17:09 +00003060bool
3061TargetProperties::GetRunArguments (Args &args) const
3062{
3063 const uint32_t idx = ePropertyRunArgs;
3064 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3065}
3066
3067void
3068TargetProperties::SetRunArguments (const Args &args)
3069{
3070 const uint32_t idx = ePropertyRunArgs;
3071 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3072}
3073
3074size_t
3075TargetProperties::GetEnvironmentAsArgs (Args &env) const
3076{
3077 const uint32_t idx = ePropertyEnvVars;
3078 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3079}
3080
3081bool
3082TargetProperties::GetSkipPrologue() const
3083{
3084 const uint32_t idx = ePropertySkipPrologue;
3085 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3086}
3087
3088PathMappingList &
3089TargetProperties::GetSourcePathMap () const
3090{
3091 const uint32_t idx = ePropertySourceMap;
3092 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3093 assert(option_value);
3094 return option_value->GetCurrentValue();
3095}
3096
3097FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003098TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003099{
3100 const uint32_t idx = ePropertyExecutableSearchPaths;
3101 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3102 assert(option_value);
3103 return option_value->GetCurrentValue();
3104}
3105
Michael Sartaina7499c92013-07-01 19:45:50 +00003106FileSpecList &
3107TargetProperties::GetDebugFileSearchPaths ()
3108{
3109 const uint32_t idx = ePropertyDebugFileSearchPaths;
3110 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3111 assert(option_value);
3112 return option_value->GetCurrentValue();
3113}
3114
Greg Clayton67cc0632012-08-22 17:17:09 +00003115bool
3116TargetProperties::GetEnableSyntheticValue () const
3117{
3118 const uint32_t idx = ePropertyEnableSynthetic;
3119 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3120}
3121
3122uint32_t
3123TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3124{
3125 const uint32_t idx = ePropertyMaxChildrenCount;
3126 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3127}
3128
3129uint32_t
3130TargetProperties::GetMaximumSizeOfStringSummary() const
3131{
3132 const uint32_t idx = ePropertyMaxSummaryLength;
3133 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3134}
3135
Enrico Granatad325bf92013-06-04 22:54:16 +00003136uint32_t
3137TargetProperties::GetMaximumMemReadSize () const
3138{
3139 const uint32_t idx = ePropertyMaxMemReadSize;
3140 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3141}
3142
Greg Clayton67cc0632012-08-22 17:17:09 +00003143FileSpec
3144TargetProperties::GetStandardInputPath () const
3145{
3146 const uint32_t idx = ePropertyInputPath;
3147 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3148}
3149
3150void
3151TargetProperties::SetStandardInputPath (const char *p)
3152{
3153 const uint32_t idx = ePropertyInputPath;
3154 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3155}
3156
3157FileSpec
3158TargetProperties::GetStandardOutputPath () const
3159{
3160 const uint32_t idx = ePropertyOutputPath;
3161 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3162}
3163
3164void
3165TargetProperties::SetStandardOutputPath (const char *p)
3166{
3167 const uint32_t idx = ePropertyOutputPath;
3168 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3169}
3170
3171FileSpec
3172TargetProperties::GetStandardErrorPath () const
3173{
3174 const uint32_t idx = ePropertyErrorPath;
3175 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3176}
3177
Greg Clayton6920b522012-08-22 18:39:03 +00003178const char *
3179TargetProperties::GetExpressionPrefixContentsAsCString ()
3180{
3181 const uint32_t idx = ePropertyExprPrefix;
3182 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3183 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003184 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003185 const bool null_terminate = true;
3186 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003187 if (data_sp)
3188 return (const char *) data_sp->GetBytes();
3189 }
Greg Clayton6920b522012-08-22 18:39:03 +00003190 return NULL;
3191}
3192
Greg Clayton67cc0632012-08-22 17:17:09 +00003193void
3194TargetProperties::SetStandardErrorPath (const char *p)
3195{
3196 const uint32_t idx = ePropertyErrorPath;
3197 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3198}
3199
3200bool
3201TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3202{
3203 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3204 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3205}
3206
Jim Ingham17d023f2013-03-13 17:58:04 +00003207bool
Daniel Malead79ae052013-08-07 21:54:09 +00003208TargetProperties::GetUseHexImmediates () const
3209{
3210 const uint32_t idx = ePropertyUseHexImmediates;
3211 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3212}
3213
3214bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003215TargetProperties::GetUseFastStepping () const
3216{
3217 const uint32_t idx = ePropertyUseFastStepping;
3218 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3219}
3220
Greg Claytonfb6621e2013-12-06 21:59:52 +00003221bool
3222TargetProperties::GetDisplayExpressionsInCrashlogs () const
3223{
3224 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3225 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3226}
3227
Enrico Granata397ddd52013-05-21 20:13:34 +00003228LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003229TargetProperties::GetLoadScriptFromSymbolFile () const
3230{
3231 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003232 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003233}
3234
Daniel Malead79ae052013-08-07 21:54:09 +00003235Disassembler::HexImmediateStyle
3236TargetProperties::GetHexImmediateStyle () const
3237{
3238 const uint32_t idx = ePropertyHexImmediateStyle;
3239 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3240}
3241
Greg Claytonfd814c52013-08-13 01:42:25 +00003242MemoryModuleLoadLevel
3243TargetProperties::GetMemoryModuleLoadLevel() const
3244{
3245 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3246 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3247}
3248
Jason Molendaa4bea722014-02-14 05:06:49 +00003249bool
3250TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3251{
3252 const uint32_t idx = ePropertyTrapHandlerNames;
3253 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3254}
Greg Claytonfd814c52013-08-13 01:42:25 +00003255
Jason Molendaa4bea722014-02-14 05:06:49 +00003256void
3257TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3258{
3259 const uint32_t idx = ePropertyTrapHandlerNames;
3260 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3261}
Greg Clayton67cc0632012-08-22 17:17:09 +00003262
Greg Claytonfbb76342013-11-20 21:07:01 +00003263//----------------------------------------------------------------------
3264// Target::TargetEventData
3265//----------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +00003266const ConstString &
3267Target::TargetEventData::GetFlavorString ()
3268{
3269 static ConstString g_flavor ("Target::TargetEventData");
3270 return g_flavor;
3271}
3272
3273const ConstString &
3274Target::TargetEventData::GetFlavor () const
3275{
3276 return TargetEventData::GetFlavorString ();
3277}
3278
3279Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
3280 EventData(),
3281 m_target_sp (new_target_sp)
3282{
3283}
3284
3285Target::TargetEventData::~TargetEventData()
3286{
3287
3288}
3289
3290void
3291Target::TargetEventData::Dump (Stream *s) const
3292{
3293
3294}
3295
3296const TargetSP
3297Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
3298{
3299 TargetSP target_sp;
3300
3301 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
3302 if (data)
3303 target_sp = data->m_target_sp;
3304
3305 return target_sp;
3306}
3307
3308const Target::TargetEventData *
3309Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3310{
3311 if (event_ptr)
3312 {
3313 const EventData *event_data = event_ptr->GetData();
3314 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3315 return static_cast <const TargetEventData *> (event_ptr->GetData());
3316 }
3317 return NULL;
3318}
3319