blob: ef1eed3482b2c82388f4a1ae31bc2ff07ab45bfd [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham03c8ee52011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chenecd4feb2011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton427f2902010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Caroline Tice4a348082011-05-02 20:41:46 +000025#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000027#include "lldb/Core/Timer.h"
28#include "lldb/Core/ValueObject.h"
Sean Callanandcf03f82011-11-15 22:27:19 +000029#include "lldb/Expression/ClangASTSource.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000030#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000032#include "lldb/Interpreter/CommandInterpreter.h"
33#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034#include "lldb/lldb-private-log.h"
35#include "lldb/Symbol/ObjectFile.h"
36#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000037#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000038#include "lldb/Target/Thread.h"
39#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000040
41using namespace lldb;
42using namespace lldb_private;
43
Jim Ingham5a15e692012-02-16 06:50:00 +000044ConstString &
45Target::GetStaticBroadcasterClass ()
46{
47 static ConstString class_name ("lldb.target");
48 return class_name;
49}
50
Chris Lattner24943d22010-06-08 16:52:24 +000051//----------------------------------------------------------------------
52// Target constructor
53//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000054Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
Jim Ingham5a15e692012-02-16 06:50:00 +000055 Broadcaster (&debugger, "lldb.target"),
Greg Clayton24bc5d92011-03-30 18:16:51 +000056 ExecutionContextScope (),
Greg Clayton334d33a2012-01-30 07:41:31 +000057 TargetInstanceSettings (GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000058 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000059 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000060 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000061 m_arch (target_arch),
62 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000063 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000064 m_breakpoint_list (false),
65 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000066 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000067 m_process_sp (),
68 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000069 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000070 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000071 m_scratch_ast_source_ap (NULL),
72 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000073 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000074 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000075 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000076 m_stop_hook_next_id (0),
77 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000078{
Greg Clayton49ce6822010-10-31 03:01:06 +000079 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
80 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
81 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham5a15e692012-02-16 06:50:00 +000082
83 CheckInWithManager();
Greg Clayton49ce6822010-10-31 03:01:06 +000084
Greg Claytone005f2c2010-11-06 01:53:30 +000085 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000086 if (log)
87 log->Printf ("%p Target::Target()", this);
88}
89
90//----------------------------------------------------------------------
91// Destructor
92//----------------------------------------------------------------------
93Target::~Target()
94{
Greg Claytone005f2c2010-11-06 01:53:30 +000095 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000096 if (log)
97 log->Printf ("%p Target::~Target()", this);
98 DeleteCurrentProcess ();
99}
100
101void
Caroline Tice7826c882010-10-26 03:11:13 +0000102Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +0000103{
Greg Clayton3fed8b92010-10-08 00:21:05 +0000104// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +0000105 if (description_level != lldb::eDescriptionLevelBrief)
106 {
107 s->Indent();
108 s->PutCString("Target\n");
109 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000110 m_images.Dump(s);
111 m_breakpoint_list.Dump(s);
112 m_internal_breakpoint_list.Dump(s);
113 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000114 }
115 else
116 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000117 Module *exe_module = GetExecutableModulePointer();
118 if (exe_module)
119 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000120 else
121 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000122 }
Chris Lattner24943d22010-06-08 16:52:24 +0000123}
124
125void
126Target::DeleteCurrentProcess ()
127{
128 if (m_process_sp.get())
129 {
Greg Clayton49480b12010-09-14 23:52:43 +0000130 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000131 if (m_process_sp->IsAlive())
132 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000133
134 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000135
136 // Do any cleanup of the target we need to do between process instances.
137 // NB It is better to do this before destroying the process in case the
138 // clean up needs some help from the process.
139 m_breakpoint_list.ClearAllBreakpointSites();
140 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000141 // Disable watchpoints just on the debugger side.
142 DisableAllWatchpoints(false);
Chris Lattner24943d22010-06-08 16:52:24 +0000143 m_process_sp.reset();
144 }
145}
146
147const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000148Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000149{
150 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000151 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000152 return m_process_sp;
153}
154
155const lldb::ProcessSP &
156Target::GetProcessSP () const
157{
158 return m_process_sp;
159}
160
Greg Clayton153ccd72011-08-10 02:10:13 +0000161void
162Target::Destroy()
163{
164 Mutex::Locker locker (m_mutex);
165 DeleteCurrentProcess ();
166 m_platform_sp.reset();
167 m_arch.Clear();
168 m_images.Clear();
169 m_section_load_list.Clear();
170 const bool notify = false;
171 m_breakpoint_list.RemoveAll(notify);
172 m_internal_breakpoint_list.RemoveAll(notify);
173 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000174 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000175 m_search_filter_sp.reset();
176 m_image_search_paths.Clear(notify);
177 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000178 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000179 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000180 m_persistent_variables.Clear();
181 m_stop_hooks.clear();
182 m_stop_hook_next_id = 0;
183 m_suppress_stop_hooks = false;
184}
185
186
Chris Lattner24943d22010-06-08 16:52:24 +0000187BreakpointList &
188Target::GetBreakpointList(bool internal)
189{
190 if (internal)
191 return m_internal_breakpoint_list;
192 else
193 return m_breakpoint_list;
194}
195
196const BreakpointList &
197Target::GetBreakpointList(bool internal) const
198{
199 if (internal)
200 return m_internal_breakpoint_list;
201 else
202 return m_breakpoint_list;
203}
204
205BreakpointSP
206Target::GetBreakpointByID (break_id_t break_id)
207{
208 BreakpointSP bp_sp;
209
210 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
211 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
212 else
213 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
214
215 return bp_sp;
216}
217
218BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000219Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
220 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000221 RegularExpression &source_regex,
222 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000223{
Jim Inghamd6d47972011-09-23 00:54:11 +0000224 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
225 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000226 return CreateBreakpoint (filter_sp, resolver_sp, internal);
227}
228
229
230BreakpointSP
231Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
232{
233 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner24943d22010-06-08 16:52:24 +0000234 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
235 return CreateBreakpoint (filter_sp, resolver_sp, internal);
236}
237
238
239BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000240Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000241{
Chris Lattner24943d22010-06-08 16:52:24 +0000242 Address so_addr;
243 // Attempt to resolve our load address if possible, though it is ok if
244 // it doesn't resolve to section/offset.
245
Greg Clayton33ed1702010-08-24 00:45:41 +0000246 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000247 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000248 if (!so_addr.IsValid())
249 {
250 // The address didn't resolve, so just set this as an absolute address
251 so_addr.SetOffset (addr);
252 }
253 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000254 return bp_sp;
255}
256
257BreakpointSP
258Target::CreateBreakpoint (Address &addr, bool internal)
259{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000260 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000261 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
262 return CreateBreakpoint (filter_sp, resolver_sp, internal);
263}
264
265BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000266Target::CreateBreakpoint (const FileSpecList *containingModules,
267 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000268 const char *func_name,
269 uint32_t func_name_type_mask,
270 bool internal,
271 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000272{
Greg Clayton12bec712010-06-28 21:30:43 +0000273 BreakpointSP bp_sp;
274 if (func_name)
275 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000276 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000277
278 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
279 func_name,
280 func_name_type_mask,
281 Breakpoint::Exact,
282 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000283 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
284 }
285 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000286}
287
288
289SearchFilterSP
290Target::GetSearchFilterForModule (const FileSpec *containingModule)
291{
292 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000293 if (containingModule != NULL)
294 {
295 // TODO: We should look into sharing module based search filters
296 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000297 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000298 }
299 else
300 {
301 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000302 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000303 filter_sp = m_search_filter_sp;
304 }
305 return filter_sp;
306}
307
Jim Ingham03c8ee52011-09-21 01:17:13 +0000308SearchFilterSP
309Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
310{
311 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000312 if (containingModules && containingModules->GetSize() != 0)
313 {
314 // TODO: We should look into sharing module based search filters
315 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000316 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000317 }
318 else
319 {
320 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000321 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000322 filter_sp = m_search_filter_sp;
323 }
324 return filter_sp;
325}
326
Jim Inghamd6d47972011-09-23 00:54:11 +0000327SearchFilterSP
328Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles)
329{
330 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
331 return GetSearchFilterForModuleList(containingModules);
332
333 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000334 if (containingModules == NULL)
335 {
336 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
337 // but that will take a little reworking.
338
Greg Clayton13d24fb2012-01-29 20:56:30 +0000339 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000340 }
341 else
342 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000343 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000344 }
345 return filter_sp;
346}
347
Chris Lattner24943d22010-06-08 16:52:24 +0000348BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000349Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
350 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000351 RegularExpression &func_regex,
352 bool internal,
353 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000354{
Jim Inghamd6d47972011-09-23 00:54:11 +0000355 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000356 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
357 func_regex,
358 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000359
360 return CreateBreakpoint (filter_sp, resolver_sp, internal);
361}
362
363BreakpointSP
364Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
365{
366 BreakpointSP bp_sp;
367 if (filter_sp && resolver_sp)
368 {
369 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
370 resolver_sp->SetBreakpoint (bp_sp.get());
371
372 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000373 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000374 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000375 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000376
Greg Claytone005f2c2010-11-06 01:53:30 +0000377 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000378 if (log)
379 {
380 StreamString s;
381 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
382 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
383 }
384
Chris Lattner24943d22010-06-08 16:52:24 +0000385 bp_sp->ResolveBreakpoint();
386 }
Jim Inghamd1686902010-10-14 23:45:03 +0000387
388 if (!internal && bp_sp)
389 {
390 m_last_created_breakpoint = bp_sp;
391 }
392
Chris Lattner24943d22010-06-08 16:52:24 +0000393 return bp_sp;
394}
395
Johnny Chenda5a8022011-09-20 23:28:55 +0000396bool
397Target::ProcessIsValid()
398{
399 return (m_process_sp && m_process_sp->IsAlive());
400}
401
Johnny Chenecd4feb2011-10-14 00:42:25 +0000402// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000403// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000404WatchpointSP
405Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type)
Johnny Chen34bbf852011-09-12 23:38:44 +0000406{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000407 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
408 if (log)
409 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
410 __FUNCTION__, addr, size, type);
411
Johnny Chenecd4feb2011-10-14 00:42:25 +0000412 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000413 if (!ProcessIsValid())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000414 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000415 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000416 return wp_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000417
Johnny Chenecd4feb2011-10-14 00:42:25 +0000418 // Currently we only support one watchpoint per address, with total number
419 // of watchpoints limited by the hardware which the inferior is running on.
420 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000421 if (matched_sp)
422 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000423 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000424 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000425 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
426 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000427 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000428 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000429 wp_sp = matched_sp;
430 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000431 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000432 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000433 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000434 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000435 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000436 }
437
Johnny Chenecd4feb2011-10-14 00:42:25 +0000438 if (!wp_sp) {
439 Watchpoint *new_wp = new Watchpoint(addr, size);
440 if (!new_wp) {
441 printf("Watchpoint ctor failed, out of memory?\n");
442 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000443 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000444 new_wp->SetWatchpointType(type);
445 new_wp->SetTarget(this);
446 wp_sp.reset(new_wp);
447 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000448 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000449
Johnny Chenecd4feb2011-10-14 00:42:25 +0000450 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000451 if (log)
452 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
453 __FUNCTION__,
454 rc.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000455 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000456
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000457 if (rc.Fail())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000458 wp_sp.reset();
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000459 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000460 m_last_created_watchpoint = wp_sp;
461 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000462}
463
Chris Lattner24943d22010-06-08 16:52:24 +0000464void
465Target::RemoveAllBreakpoints (bool internal_also)
466{
Greg Claytone005f2c2010-11-06 01:53:30 +0000467 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000468 if (log)
469 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
470
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000471 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000472 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000473 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000474
475 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000476}
477
478void
479Target::DisableAllBreakpoints (bool internal_also)
480{
Greg Claytone005f2c2010-11-06 01:53:30 +0000481 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000482 if (log)
483 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
484
485 m_breakpoint_list.SetEnabledAll (false);
486 if (internal_also)
487 m_internal_breakpoint_list.SetEnabledAll (false);
488}
489
490void
491Target::EnableAllBreakpoints (bool internal_also)
492{
Greg Claytone005f2c2010-11-06 01:53:30 +0000493 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000494 if (log)
495 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
496
497 m_breakpoint_list.SetEnabledAll (true);
498 if (internal_also)
499 m_internal_breakpoint_list.SetEnabledAll (true);
500}
501
502bool
503Target::RemoveBreakpointByID (break_id_t break_id)
504{
Greg Claytone005f2c2010-11-06 01:53:30 +0000505 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000506 if (log)
507 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
508
509 if (DisableBreakpointByID (break_id))
510 {
511 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000512 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000513 else
Jim Inghamd1686902010-10-14 23:45:03 +0000514 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000515 if (m_last_created_breakpoint)
516 {
517 if (m_last_created_breakpoint->GetID() == break_id)
518 m_last_created_breakpoint.reset();
519 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000520 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000521 }
Chris Lattner24943d22010-06-08 16:52:24 +0000522 return true;
523 }
524 return false;
525}
526
527bool
528Target::DisableBreakpointByID (break_id_t break_id)
529{
Greg Claytone005f2c2010-11-06 01:53:30 +0000530 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000531 if (log)
532 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
533
534 BreakpointSP bp_sp;
535
536 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
537 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
538 else
539 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
540 if (bp_sp)
541 {
542 bp_sp->SetEnabled (false);
543 return true;
544 }
545 return false;
546}
547
548bool
549Target::EnableBreakpointByID (break_id_t break_id)
550{
Greg Claytone005f2c2010-11-06 01:53:30 +0000551 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000552 if (log)
553 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
554 __FUNCTION__,
555 break_id,
556 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
557
558 BreakpointSP bp_sp;
559
560 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
561 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
562 else
563 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
564
565 if (bp_sp)
566 {
567 bp_sp->SetEnabled (true);
568 return true;
569 }
570 return false;
571}
572
Johnny Chenc86582f2011-09-23 21:21:43 +0000573// The flag 'end_to_end', default to true, signifies that the operation is
574// performed end to end, for both the debugger and the debuggee.
575
Johnny Chenecd4feb2011-10-14 00:42:25 +0000576// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
577// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000578bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000579Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000580{
581 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
582 if (log)
583 log->Printf ("Target::%s\n", __FUNCTION__);
584
Johnny Chenc86582f2011-09-23 21:21:43 +0000585 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000586 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000587 return true;
588 }
589
590 // Otherwise, it's an end to end operation.
591
Johnny Chenda5a8022011-09-20 23:28:55 +0000592 if (!ProcessIsValid())
593 return false;
594
Johnny Chenecd4feb2011-10-14 00:42:25 +0000595 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000596 for (size_t i = 0; i < num_watchpoints; ++i)
597 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000598 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
599 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000600 return false;
601
Johnny Chenecd4feb2011-10-14 00:42:25 +0000602 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000603 if (rc.Fail())
604 return false;
605 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000606 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000607 return true; // Success!
608}
609
Johnny Chenecd4feb2011-10-14 00:42:25 +0000610// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
611// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000612bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000613Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000614{
615 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
616 if (log)
617 log->Printf ("Target::%s\n", __FUNCTION__);
618
Johnny Chenc86582f2011-09-23 21:21:43 +0000619 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000620 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000621 return true;
622 }
623
624 // Otherwise, it's an end to end operation.
625
Johnny Chenda5a8022011-09-20 23:28:55 +0000626 if (!ProcessIsValid())
627 return false;
628
Johnny Chenecd4feb2011-10-14 00:42:25 +0000629 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000630 for (size_t i = 0; i < num_watchpoints; ++i)
631 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000632 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
633 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000634 return false;
635
Johnny Chenecd4feb2011-10-14 00:42:25 +0000636 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000637 if (rc.Fail())
638 return false;
639 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000640 return true; // Success!
641}
642
Johnny Chenecd4feb2011-10-14 00:42:25 +0000643// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
644// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000645bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000646Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000647{
648 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
649 if (log)
650 log->Printf ("Target::%s\n", __FUNCTION__);
651
Johnny Chenc86582f2011-09-23 21:21:43 +0000652 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000653 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000654 return true;
655 }
656
657 // Otherwise, it's an end to end operation.
658
Johnny Chenda5a8022011-09-20 23:28:55 +0000659 if (!ProcessIsValid())
660 return false;
661
Johnny Chenecd4feb2011-10-14 00:42:25 +0000662 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000663 for (size_t i = 0; i < num_watchpoints; ++i)
664 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000665 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
666 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000667 return false;
668
Johnny Chenecd4feb2011-10-14 00:42:25 +0000669 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000670 if (rc.Fail())
671 return false;
672 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000673 return true; // Success!
674}
675
Johnny Chenecd4feb2011-10-14 00:42:25 +0000676// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000677// during these operations.
678bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000679Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000680{
681 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
682 if (log)
683 log->Printf ("Target::%s\n", __FUNCTION__);
684
685 if (!ProcessIsValid())
686 return false;
687
Johnny Chenecd4feb2011-10-14 00:42:25 +0000688 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000689 for (size_t i = 0; i < num_watchpoints; ++i)
690 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000691 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
692 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000693 return false;
694
Johnny Chenecd4feb2011-10-14 00:42:25 +0000695 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000696 }
697 return true; // Success!
698}
699
Johnny Chenecd4feb2011-10-14 00:42:25 +0000700// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000701bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000702Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000703{
704 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
705 if (log)
706 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
707
708 if (!ProcessIsValid())
709 return false;
710
Johnny Chenecd4feb2011-10-14 00:42:25 +0000711 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
712 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000713 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000714 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000715 if (rc.Success())
716 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000717
Johnny Chen01acfa72011-09-22 18:04:58 +0000718 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000719 }
720 return false;
721}
722
Johnny Chenecd4feb2011-10-14 00:42:25 +0000723// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000724bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000725Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000726{
727 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
728 if (log)
729 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
730
731 if (!ProcessIsValid())
732 return false;
733
Johnny Chenecd4feb2011-10-14 00:42:25 +0000734 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
735 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000736 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000737 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000738 if (rc.Success())
739 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000740
Johnny Chen01acfa72011-09-22 18:04:58 +0000741 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000742 }
743 return false;
744}
745
Johnny Chenecd4feb2011-10-14 00:42:25 +0000746// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000747bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000748Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000749{
750 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
751 if (log)
752 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
753
Johnny Chenecd4feb2011-10-14 00:42:25 +0000754 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000755 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000756 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000757 return true;
758 }
759 return false;
760}
761
Johnny Chenecd4feb2011-10-14 00:42:25 +0000762// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000763bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000764Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000765{
766 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
767 if (log)
768 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
769
770 if (!ProcessIsValid())
771 return false;
772
Johnny Chenecd4feb2011-10-14 00:42:25 +0000773 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
774 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000775 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000776 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000777 return true;
778 }
779 return false;
780}
781
Chris Lattner24943d22010-06-08 16:52:24 +0000782ModuleSP
783Target::GetExecutableModule ()
784{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000785 return m_images.GetModuleAtIndex(0);
786}
787
788Module*
789Target::GetExecutableModulePointer ()
790{
791 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000792}
793
794void
795Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
796{
797 m_images.Clear();
798 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000799 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000800 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000801
802 if (executable_sp.get())
803 {
804 Timer scoped_timer (__PRETTY_FUNCTION__,
805 "Target::SetExecutableModule (executable = '%s/%s')",
806 executable_sp->GetFileSpec().GetDirectory().AsCString(),
807 executable_sp->GetFileSpec().GetFilename().AsCString());
808
809 m_images.Append(executable_sp); // The first image is our exectuable file
810
Jim Ingham7508e732010-08-09 23:31:02 +0000811 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000812 if (!m_arch.IsValid())
813 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000814
Chris Lattner24943d22010-06-08 16:52:24 +0000815 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000816 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000817
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000818 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000819 {
820 executable_objfile->GetDependentModules(dependent_files);
821 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
822 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000823 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
824 FileSpec platform_dependent_file_spec;
825 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000826 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000827 else
828 platform_dependent_file_spec = dependent_file_spec;
829
830 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000831 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000832 if (image_module_sp.get())
833 {
Chris Lattner24943d22010-06-08 16:52:24 +0000834 ObjectFile *objfile = image_module_sp->GetObjectFile();
835 if (objfile)
836 objfile->GetDependentModules(dependent_files);
837 }
838 }
839 }
Chris Lattner24943d22010-06-08 16:52:24 +0000840 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000841
842 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000843}
844
845
Jim Ingham7508e732010-08-09 23:31:02 +0000846bool
847Target::SetArchitecture (const ArchSpec &arch_spec)
848{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000849 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000850 {
851 // If we're setting the architecture to our current architecture, we
852 // don't need to do anything.
853 return true;
854 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000855 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000856 {
857 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000858 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000859 return true;
860 }
861 else
862 {
863 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000864 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000865 ModuleSP executable_sp = GetExecutableModule ();
866 m_images.Clear();
867 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000868 m_scratch_ast_source_ap.reset();
869 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000870 // Need to do something about unsetting breakpoints.
871
872 if (executable_sp)
873 {
874 FileSpec exec_file_spec = executable_sp->GetFileSpec();
875 Error error = ModuleList::GetSharedModule(exec_file_spec,
876 arch_spec,
877 NULL,
878 NULL,
879 0,
880 executable_sp,
Greg Clayton9ce95382012-02-13 23:10:39 +0000881 &GetExecutableSearchPaths(),
Jim Ingham7508e732010-08-09 23:31:02 +0000882 NULL,
883 NULL);
884
885 if (!error.Fail() && executable_sp)
886 {
887 SetExecutableModule (executable_sp, true);
888 return true;
889 }
890 else
891 {
892 return false;
893 }
894 }
895 else
896 {
897 return false;
898 }
899 }
900}
Chris Lattner24943d22010-06-08 16:52:24 +0000901
Chris Lattner24943d22010-06-08 16:52:24 +0000902void
903Target::ModuleAdded (ModuleSP &module_sp)
904{
905 // A module is being added to this target for the first time
906 ModuleList module_list;
907 module_list.Append(module_sp);
908 ModulesDidLoad (module_list);
909}
910
911void
912Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
913{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000914 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000915 ModuleList module_list;
916 module_list.Append (old_module_sp);
917 ModulesDidUnload (module_list);
918 module_list.Clear ();
919 module_list.Append (new_module_sp);
920 ModulesDidLoad (module_list);
921}
922
923void
924Target::ModulesDidLoad (ModuleList &module_list)
925{
926 m_breakpoint_list.UpdateBreakpoints (module_list, true);
927 // TODO: make event data that packages up the module_list
928 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
929}
930
931void
932Target::ModulesDidUnload (ModuleList &module_list)
933{
934 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000935
936 // Remove the images from the target image list
937 m_images.Remove(module_list);
938
Chris Lattner24943d22010-06-08 16:52:24 +0000939 // TODO: make event data that packages up the module_list
940 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
941}
942
Jim Ingham7089d8a2011-10-28 23:14:11 +0000943
Daniel Dunbar705a0982011-10-31 22:50:37 +0000944bool
Jim Ingham7089d8a2011-10-28 23:14:11 +0000945Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_spec)
946{
947
948 if (!m_breakpoints_use_platform_avoid)
949 return false;
950 else
951 {
952 ModuleList matchingModules;
953 const ArchSpec *arch_ptr = NULL;
954 const lldb_private::UUID *uuid_ptr= NULL;
955 const ConstString *object_name = NULL;
956 size_t num_modules = GetImages().FindModules(&module_spec, arch_ptr, uuid_ptr, object_name, matchingModules);
957
958 // If there is more than one module for this file spec, only return true if ALL the modules are on the
959 // black list.
960 if (num_modules > 0)
961 {
962 for (int i = 0; i < num_modules; i++)
963 {
964 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
965 return false;
966 }
967 return true;
968 }
969 else
970 return false;
971 }
972}
973
Daniel Dunbar705a0982011-10-31 22:50:37 +0000974bool
Jim Ingham7089d8a2011-10-28 23:14:11 +0000975Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
976{
977 if (!m_breakpoints_use_platform_avoid)
978 return false;
979 else if (GetPlatform())
980 {
981 return GetPlatform()->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
982 }
983 else
984 return false;
985}
986
Chris Lattner24943d22010-06-08 16:52:24 +0000987size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000988Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
989{
990 const Section *section = addr.GetSection();
991 if (section && section->GetModule())
992 {
993 ObjectFile *objfile = section->GetModule()->GetObjectFile();
994 if (objfile)
995 {
Greg Claytonb5a8f142012-02-05 02:38:54 +0000996 size_t bytes_read = objfile->ReadSectionData (section,
997 addr.GetOffset(),
998 dst,
999 dst_len);
Greg Clayton26100dc2011-01-07 01:57:07 +00001000 if (bytes_read > 0)
1001 return bytes_read;
1002 else
1003 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
1004 }
1005 else
1006 {
1007 error.SetErrorString("address isn't from a object file");
1008 }
1009 }
1010 else
1011 {
1012 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
1013 }
1014 return 0;
1015}
1016
1017size_t
Enrico Granata91544802011-09-06 19:20:51 +00001018Target::ReadMemory (const Address& addr,
1019 bool prefer_file_cache,
1020 void *dst,
1021 size_t dst_len,
1022 Error &error,
1023 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001024{
Chris Lattner24943d22010-06-08 16:52:24 +00001025 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001026
Enrico Granata91544802011-09-06 19:20:51 +00001027 // if we end up reading this from process memory, we will fill this
1028 // with the actual load address
1029 if (load_addr_ptr)
1030 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1031
Greg Clayton26100dc2011-01-07 01:57:07 +00001032 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001033
1034 addr_t load_addr = LLDB_INVALID_ADDRESS;
1035 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001036 Address resolved_addr;
1037 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001038 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001039 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001040 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001041 // No sections are loaded, so we must assume we are not running
1042 // yet and anything we are given is a file address.
1043 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1044 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001045 }
Greg Clayton70436352010-06-30 23:03:03 +00001046 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001047 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001048 // We have at least one section loaded. This can be becuase
1049 // we have manually loaded some sections with "target modules load ..."
1050 // or because we have have a live process that has sections loaded
1051 // through the dynamic loader
1052 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1053 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001054 }
Greg Clayton70436352010-06-30 23:03:03 +00001055 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001056 if (!resolved_addr.IsValid())
1057 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001058
Greg Clayton9b82f862011-07-11 05:12:02 +00001059
Greg Clayton26100dc2011-01-07 01:57:07 +00001060 if (prefer_file_cache)
1061 {
1062 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1063 if (bytes_read > 0)
1064 return bytes_read;
1065 }
Greg Clayton70436352010-06-30 23:03:03 +00001066
Johnny Chenda5a8022011-09-20 23:28:55 +00001067 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001068 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001069 if (load_addr == LLDB_INVALID_ADDRESS)
1070 load_addr = resolved_addr.GetLoadAddress (this);
1071
Greg Clayton70436352010-06-30 23:03:03 +00001072 if (load_addr == LLDB_INVALID_ADDRESS)
1073 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001074 Module *addr_module = resolved_addr.GetModulePtr();
1075 if (addr_module && addr_module->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001076 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton13d24fb2012-01-29 20:56:30 +00001077 addr_module->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001078 resolved_addr.GetFileAddress(),
Greg Clayton13d24fb2012-01-29 20:56:30 +00001079 addr_module->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001080 else
Greg Clayton9c236732011-10-26 00:56:27 +00001081 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001082 }
1083 else
1084 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001085 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001086 if (bytes_read != dst_len)
1087 {
1088 if (error.Success())
1089 {
1090 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001091 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001092 else
Greg Clayton9c236732011-10-26 00:56:27 +00001093 error.SetErrorStringWithFormat("only %zu of %zu bytes were read from memory at 0x%llx", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001094 }
1095 }
Greg Clayton70436352010-06-30 23:03:03 +00001096 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001097 {
1098 if (load_addr_ptr)
1099 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001100 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001101 }
Greg Clayton70436352010-06-30 23:03:03 +00001102 // If the address is not section offset we have an address that
1103 // doesn't resolve to any address in any currently loaded shared
1104 // libaries and we failed to read memory so there isn't anything
1105 // more we can do. If it is section offset, we might be able to
1106 // read cached memory from the object file.
1107 if (!resolved_addr.IsSectionOffset())
1108 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001109 }
Chris Lattner24943d22010-06-08 16:52:24 +00001110 }
Greg Clayton70436352010-06-30 23:03:03 +00001111
Greg Clayton9b82f862011-07-11 05:12:02 +00001112 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001113 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001114 // If we didn't already try and read from the object file cache, then
1115 // try it after failing to read from the process.
1116 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001117 }
1118 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001119}
1120
Greg Clayton7dd98df2011-07-12 17:06:17 +00001121size_t
1122Target::ReadScalarIntegerFromMemory (const Address& addr,
1123 bool prefer_file_cache,
1124 uint32_t byte_size,
1125 bool is_signed,
1126 Scalar &scalar,
1127 Error &error)
1128{
1129 uint64_t uval;
1130
1131 if (byte_size <= sizeof(uval))
1132 {
1133 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1134 if (bytes_read == byte_size)
1135 {
1136 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1137 uint32_t offset = 0;
1138 if (byte_size <= 4)
1139 scalar = data.GetMaxU32 (&offset, byte_size);
1140 else
1141 scalar = data.GetMaxU64 (&offset, byte_size);
1142
1143 if (is_signed)
1144 scalar.SignExtend(byte_size * 8);
1145 return bytes_read;
1146 }
1147 }
1148 else
1149 {
1150 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1151 }
1152 return 0;
1153}
1154
1155uint64_t
1156Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1157 bool prefer_file_cache,
1158 size_t integer_byte_size,
1159 uint64_t fail_value,
1160 Error &error)
1161{
1162 Scalar scalar;
1163 if (ReadScalarIntegerFromMemory (addr,
1164 prefer_file_cache,
1165 integer_byte_size,
1166 false,
1167 scalar,
1168 error))
1169 return scalar.ULongLong(fail_value);
1170 return fail_value;
1171}
1172
1173bool
1174Target::ReadPointerFromMemory (const Address& addr,
1175 bool prefer_file_cache,
1176 Error &error,
1177 Address &pointer_addr)
1178{
1179 Scalar scalar;
1180 if (ReadScalarIntegerFromMemory (addr,
1181 prefer_file_cache,
1182 m_arch.GetAddressByteSize(),
1183 false,
1184 scalar,
1185 error))
1186 {
1187 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1188 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1189 {
1190 if (m_section_load_list.IsEmpty())
1191 {
1192 // No sections are loaded, so we must assume we are not running
1193 // yet and anything we are given is a file address.
1194 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1195 }
1196 else
1197 {
1198 // We have at least one section loaded. This can be becuase
1199 // we have manually loaded some sections with "target modules load ..."
1200 // or because we have have a live process that has sections loaded
1201 // through the dynamic loader
1202 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1203 }
1204 // We weren't able to resolve the pointer value, so just return
1205 // an address with no section
1206 if (!pointer_addr.IsValid())
1207 pointer_addr.SetOffset (pointer_vm_addr);
1208 return true;
1209
1210 }
1211 }
1212 return false;
1213}
Chris Lattner24943d22010-06-08 16:52:24 +00001214
1215ModuleSP
1216Target::GetSharedModule
1217(
1218 const FileSpec& file_spec,
1219 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +00001220 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +00001221 const ConstString *object_name,
1222 off_t object_offset,
1223 Error *error_ptr
1224)
1225{
1226 // Don't pass in the UUID so we can tell if we have a stale value in our list
1227 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1228 bool did_create_module = false;
1229 ModuleSP module_sp;
1230
Chris Lattner24943d22010-06-08 16:52:24 +00001231 Error error;
1232
Greg Clayton24bc5d92011-03-30 18:16:51 +00001233 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +00001234 if (m_image_search_paths.GetSize())
1235 {
1236 FileSpec transformed_spec;
1237 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1238 {
1239 transformed_spec.GetFilename() = file_spec.GetFilename();
Greg Clayton9ce95382012-02-13 23:10:39 +00001240 error = ModuleList::GetSharedModule (transformed_spec,
1241 arch,
1242 uuid_ptr,
1243 object_name,
1244 object_offset,
1245 module_sp,
1246 &GetExecutableSearchPaths(),
1247 &old_module_sp,
1248 &did_create_module);
Chris Lattner24943d22010-06-08 16:52:24 +00001249 }
1250 }
1251
Greg Clayton24bc5d92011-03-30 18:16:51 +00001252 // The platform is responsible for finding and caching an appropriate
1253 // module in the shared module cache.
1254 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001255 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001256 FileSpec platform_file_spec;
1257 error = m_platform_sp->GetSharedModule (file_spec,
1258 arch,
1259 uuid_ptr,
1260 object_name,
1261 object_offset,
1262 module_sp,
Greg Clayton9ce95382012-02-13 23:10:39 +00001263 &GetExecutableSearchPaths(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001264 &old_module_sp,
1265 &did_create_module);
1266 }
1267 else
1268 {
1269 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +00001270 }
1271
Greg Clayton24bc5d92011-03-30 18:16:51 +00001272 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +00001273 if (module_sp)
1274 {
1275 m_images.Append (module_sp);
1276 if (did_create_module)
1277 {
1278 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1279 ModuleUpdated(old_module_sp, module_sp);
1280 else
1281 ModuleAdded(module_sp);
1282 }
1283 }
1284 if (error_ptr)
1285 *error_ptr = error;
1286 return module_sp;
1287}
1288
1289
1290Target *
1291Target::CalculateTarget ()
1292{
1293 return this;
1294}
1295
1296Process *
1297Target::CalculateProcess ()
1298{
1299 return NULL;
1300}
1301
1302Thread *
1303Target::CalculateThread ()
1304{
1305 return NULL;
1306}
1307
1308StackFrame *
1309Target::CalculateStackFrame ()
1310{
1311 return NULL;
1312}
1313
1314void
Greg Claytona830adb2010-10-04 01:05:56 +00001315Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001316{
Greg Clayton567e7f32011-09-22 04:58:26 +00001317 exe_ctx.Clear();
1318 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001319}
1320
1321PathMappingList &
1322Target::GetImageSearchPathList ()
1323{
1324 return m_image_search_paths;
1325}
1326
1327void
1328Target::ImageSearchPathsChanged
1329(
1330 const PathMappingList &path_list,
1331 void *baton
1332)
1333{
1334 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001335 ModuleSP exe_module_sp (target->GetExecutableModule());
1336 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001337 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001338 target->m_images.Clear();
1339 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001340 }
1341}
1342
1343ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001344Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001345{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001346 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001347 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001348 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001349 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001350 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001351 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1352 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1353 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1354 }
Chris Lattner24943d22010-06-08 16:52:24 +00001355 return m_scratch_ast_context_ap.get();
1356}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001357
Sean Callanan4938bd62011-11-16 18:20:47 +00001358ClangASTImporter *
1359Target::GetClangASTImporter()
1360{
1361 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1362
1363 if (!ast_importer)
1364 {
1365 ast_importer = new ClangASTImporter();
1366 m_ast_importer_ap.reset(ast_importer);
1367 }
1368
1369 return ast_importer;
1370}
1371
Greg Clayton990de7b2010-11-18 23:32:35 +00001372void
Caroline Tice2a456812011-03-10 22:14:10 +00001373Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001374{
Greg Clayton334d33a2012-01-30 07:41:31 +00001375 UserSettingsController::InitializeSettingsController (GetSettingsController(),
Greg Clayton990de7b2010-11-18 23:32:35 +00001376 SettingsController::global_settings_table,
1377 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001378
1379 // Now call SettingsInitialize() on each 'child' setting of Target
1380 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001381}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001382
Greg Clayton990de7b2010-11-18 23:32:35 +00001383void
Caroline Tice2a456812011-03-10 22:14:10 +00001384Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001385{
Caroline Tice2a456812011-03-10 22:14:10 +00001386
1387 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1388
1389 Process::SettingsTerminate ();
1390
1391 // Now terminate Target Settings.
1392
Greg Clayton990de7b2010-11-18 23:32:35 +00001393 UserSettingsControllerSP &usc = GetSettingsController();
1394 UserSettingsController::FinalizeSettingsController (usc);
1395 usc.reset();
1396}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001397
Greg Clayton990de7b2010-11-18 23:32:35 +00001398UserSettingsControllerSP &
1399Target::GetSettingsController ()
1400{
Greg Clayton334d33a2012-01-30 07:41:31 +00001401 static UserSettingsControllerSP g_settings_controller_sp;
1402 if (!g_settings_controller_sp)
1403 {
1404 g_settings_controller_sp.reset (new Target::SettingsController);
1405 // The first shared pointer to Target::SettingsController in
1406 // g_settings_controller_sp must be fully created above so that
1407 // the TargetInstanceSettings can use a weak_ptr to refer back
1408 // to the master setttings controller
1409 InstanceSettingsSP default_instance_settings_sp (new TargetInstanceSettings (g_settings_controller_sp,
1410 false,
1411 InstanceSettings::GetDefaultName().AsCString()));
1412 g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
1413 }
1414 return g_settings_controller_sp;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001415}
1416
Greg Clayton9ce95382012-02-13 23:10:39 +00001417FileSpecList
1418Target::GetDefaultExecutableSearchPaths ()
1419{
1420 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1421 if (settings_controller_sp)
1422 {
1423 lldb::InstanceSettingsSP instance_settings_sp (settings_controller_sp->GetDefaultInstanceSettings ());
1424 if (instance_settings_sp)
1425 return static_cast<TargetInstanceSettings *>(instance_settings_sp.get())->GetExecutableSearchPaths ();
1426 }
1427 return FileSpecList();
1428}
1429
1430
Caroline Tice5bc8c972010-09-20 20:44:43 +00001431ArchSpec
1432Target::GetDefaultArchitecture ()
1433{
Greg Clayton940b1032011-02-23 00:35:02 +00001434 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1435
1436 if (settings_controller_sp)
1437 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1438 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001439}
1440
1441void
Greg Clayton940b1032011-02-23 00:35:02 +00001442Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001443{
Greg Clayton940b1032011-02-23 00:35:02 +00001444 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1445
1446 if (settings_controller_sp)
1447 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001448}
1449
Greg Claytona830adb2010-10-04 01:05:56 +00001450Target *
1451Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1452{
1453 // The target can either exist in the "process" of ExecutionContext, or in
1454 // the "target_sp" member of SymbolContext. This accessor helper function
1455 // will get the target from one of these locations.
1456
1457 Target *target = NULL;
1458 if (sc_ptr != NULL)
1459 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001460 if (target == NULL && exe_ctx_ptr)
1461 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001462 return target;
1463}
1464
1465
Caroline Tice1ebef442010-09-27 00:30:10 +00001466void
1467Target::UpdateInstanceName ()
1468{
1469 StreamString sstr;
1470
Greg Clayton5beb99d2011-08-11 02:48:45 +00001471 Module *exe_module = GetExecutableModulePointer();
1472 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001473 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001474 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001475 exe_module->GetFileSpec().GetFilename().AsCString(),
1476 exe_module->GetArchitecture().GetArchitectureName());
1477 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001478 }
1479}
1480
Sean Callanan77e93942010-10-29 00:29:03 +00001481const char *
1482Target::GetExpressionPrefixContentsAsCString ()
1483{
Sean Callanane0b7f942011-11-16 01:54:57 +00001484 if (!m_expr_prefix_contents.empty())
1485 return m_expr_prefix_contents.c_str();
Greg Claytonff44ab42011-04-23 02:04:55 +00001486 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001487}
1488
Greg Clayton427f2902010-12-14 02:59:59 +00001489ExecutionResults
1490Target::EvaluateExpression
1491(
1492 const char *expr_cstr,
1493 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001494 lldb_private::ExecutionPolicy execution_policy,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001495 bool coerce_to_id,
Greg Clayton427f2902010-12-14 02:59:59 +00001496 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001497 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001498 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001499 lldb::ValueObjectSP &result_valobj_sp
1500)
1501{
1502 ExecutionResults execution_results = eExecutionSetupError;
1503
1504 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001505
1506 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1507 return execution_results;
1508
Jim Ingham3613ae12011-05-12 02:06:14 +00001509 // We shouldn't run stop hooks in expressions.
1510 // Be sure to reset this if you return anywhere within this function.
1511 bool old_suppress_value = m_suppress_stop_hooks;
1512 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001513
1514 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001515
1516 const size_t expr_cstr_len = ::strlen (expr_cstr);
1517
Greg Clayton427f2902010-12-14 02:59:59 +00001518 if (frame)
1519 {
1520 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001521 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001522 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001523 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1524 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001525 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001526
1527 // Make sure we don't have any things that we know a variable expression
1528 // won't be able to deal with before calling into it
1529 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1530 {
1531 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1532 use_dynamic,
1533 expr_path_options,
1534 var_sp,
1535 error);
1536 }
Greg Clayton427f2902010-12-14 02:59:59 +00001537 }
1538 else if (m_process_sp)
1539 {
1540 m_process_sp->CalculateExecutionContext(exe_ctx);
1541 }
1542 else
1543 {
1544 CalculateExecutionContext(exe_ctx);
1545 }
1546
1547 if (result_valobj_sp)
1548 {
1549 execution_results = eExecutionCompleted;
1550 // We got a result from the frame variable expression path above...
1551 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1552
1553 lldb::ValueObjectSP const_valobj_sp;
1554
1555 // Check in case our value is already a constant value
1556 if (result_valobj_sp->GetIsConstant())
1557 {
1558 const_valobj_sp = result_valobj_sp;
1559 const_valobj_sp->SetName (persistent_variable_name);
1560 }
1561 else
Jim Inghame41494a2011-04-16 00:01:13 +00001562 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001563 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001564 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001565 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001566 if (dynamic_sp)
1567 result_valobj_sp = dynamic_sp;
1568 }
1569
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001570 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001571 }
Greg Clayton427f2902010-12-14 02:59:59 +00001572
Sean Callanan6a925532011-01-13 08:53:35 +00001573 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1574
Greg Clayton427f2902010-12-14 02:59:59 +00001575 result_valobj_sp = const_valobj_sp;
1576
Sean Callanan6a925532011-01-13 08:53:35 +00001577 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1578 assert (clang_expr_variable_sp.get());
1579
1580 // Set flags and live data as appropriate
1581
1582 const Value &result_value = live_valobj_sp->GetValue();
1583
1584 switch (result_value.GetValueType())
1585 {
1586 case Value::eValueTypeHostAddress:
1587 case Value::eValueTypeFileAddress:
1588 // we don't do anything with these for now
1589 break;
1590 case Value::eValueTypeScalar:
1591 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1592 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1593 break;
1594 case Value::eValueTypeLoadAddress:
1595 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1596 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1597 break;
1598 }
Greg Clayton427f2902010-12-14 02:59:59 +00001599 }
1600 else
1601 {
1602 // Make sure we aren't just trying to see the value of a persistent
1603 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001604 lldb::ClangExpressionVariableSP persistent_var_sp;
1605 // Only check for persistent variables the expression starts with a '$'
1606 if (expr_cstr[0] == '$')
1607 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1608
Greg Clayton427f2902010-12-14 02:59:59 +00001609 if (persistent_var_sp)
1610 {
1611 result_valobj_sp = persistent_var_sp->GetValueObject ();
1612 execution_results = eExecutionCompleted;
1613 }
1614 else
1615 {
1616 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001617
Greg Clayton427f2902010-12-14 02:59:59 +00001618 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001619 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001620 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001621 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan6a925532011-01-13 08:53:35 +00001622 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001623 expr_cstr,
1624 prefix,
1625 result_valobj_sp);
1626 }
1627 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001628
1629 m_suppress_stop_hooks = old_suppress_value;
1630
Greg Clayton427f2902010-12-14 02:59:59 +00001631 return execution_results;
1632}
1633
Greg Claytonc0fa5332011-05-22 22:46:53 +00001634lldb::addr_t
1635Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1636{
1637 addr_t code_addr = load_addr;
1638 switch (m_arch.GetMachine())
1639 {
1640 case llvm::Triple::arm:
1641 case llvm::Triple::thumb:
1642 switch (addr_class)
1643 {
1644 case eAddressClassData:
1645 case eAddressClassDebug:
1646 return LLDB_INVALID_ADDRESS;
1647
1648 case eAddressClassUnknown:
1649 case eAddressClassInvalid:
1650 case eAddressClassCode:
1651 case eAddressClassCodeAlternateISA:
1652 case eAddressClassRuntime:
1653 // Check if bit zero it no set?
1654 if ((code_addr & 1ull) == 0)
1655 {
1656 // Bit zero isn't set, check if the address is a multiple of 2?
1657 if (code_addr & 2ull)
1658 {
1659 // The address is a multiple of 2 so it must be thumb, set bit zero
1660 code_addr |= 1ull;
1661 }
1662 else if (addr_class == eAddressClassCodeAlternateISA)
1663 {
1664 // We checked the address and the address claims to be the alternate ISA
1665 // which means thumb, so set bit zero.
1666 code_addr |= 1ull;
1667 }
1668 }
1669 break;
1670 }
1671 break;
1672
1673 default:
1674 break;
1675 }
1676 return code_addr;
1677}
1678
1679lldb::addr_t
1680Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1681{
1682 addr_t opcode_addr = load_addr;
1683 switch (m_arch.GetMachine())
1684 {
1685 case llvm::Triple::arm:
1686 case llvm::Triple::thumb:
1687 switch (addr_class)
1688 {
1689 case eAddressClassData:
1690 case eAddressClassDebug:
1691 return LLDB_INVALID_ADDRESS;
1692
1693 case eAddressClassInvalid:
1694 case eAddressClassUnknown:
1695 case eAddressClassCode:
1696 case eAddressClassCodeAlternateISA:
1697 case eAddressClassRuntime:
1698 opcode_addr &= ~(1ull);
1699 break;
1700 }
1701 break;
1702
1703 default:
1704 break;
1705 }
1706 return opcode_addr;
1707}
1708
Jim Inghamd60d94a2011-03-11 03:53:59 +00001709lldb::user_id_t
1710Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1711{
1712 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001713 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001714 m_stop_hooks[new_uid] = new_hook_sp;
1715 return new_uid;
1716}
1717
1718bool
1719Target::RemoveStopHookByID (lldb::user_id_t user_id)
1720{
1721 size_t num_removed;
1722 num_removed = m_stop_hooks.erase (user_id);
1723 if (num_removed == 0)
1724 return false;
1725 else
1726 return true;
1727}
1728
1729void
1730Target::RemoveAllStopHooks ()
1731{
1732 m_stop_hooks.clear();
1733}
1734
1735Target::StopHookSP
1736Target::GetStopHookByID (lldb::user_id_t user_id)
1737{
1738 StopHookSP found_hook;
1739
1740 StopHookCollection::iterator specified_hook_iter;
1741 specified_hook_iter = m_stop_hooks.find (user_id);
1742 if (specified_hook_iter != m_stop_hooks.end())
1743 found_hook = (*specified_hook_iter).second;
1744 return found_hook;
1745}
1746
1747bool
1748Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1749{
1750 StopHookCollection::iterator specified_hook_iter;
1751 specified_hook_iter = m_stop_hooks.find (user_id);
1752 if (specified_hook_iter == m_stop_hooks.end())
1753 return false;
1754
1755 (*specified_hook_iter).second->SetIsActive (active_state);
1756 return true;
1757}
1758
1759void
1760Target::SetAllStopHooksActiveState (bool active_state)
1761{
1762 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1763 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1764 {
1765 (*pos).second->SetIsActive (active_state);
1766 }
1767}
1768
1769void
1770Target::RunStopHooks ()
1771{
Jim Ingham3613ae12011-05-12 02:06:14 +00001772 if (m_suppress_stop_hooks)
1773 return;
1774
Jim Inghamd60d94a2011-03-11 03:53:59 +00001775 if (!m_process_sp)
1776 return;
1777
1778 if (m_stop_hooks.empty())
1779 return;
1780
1781 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1782
1783 // If there aren't any active stop hooks, don't bother either:
1784 bool any_active_hooks = false;
1785 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1786 {
1787 if ((*pos).second->IsActive())
1788 {
1789 any_active_hooks = true;
1790 break;
1791 }
1792 }
1793 if (!any_active_hooks)
1794 return;
1795
1796 CommandReturnObject result;
1797
1798 std::vector<ExecutionContext> exc_ctx_with_reasons;
1799 std::vector<SymbolContext> sym_ctx_with_reasons;
1800
1801 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1802 size_t num_threads = cur_threadlist.GetSize();
1803 for (size_t i = 0; i < num_threads; i++)
1804 {
1805 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1806 if (cur_thread_sp->ThreadStoppedForAReason())
1807 {
1808 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1809 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1810 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1811 }
1812 }
1813
1814 // If no threads stopped for a reason, don't run the stop-hooks.
1815 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1816 if (num_exe_ctx == 0)
1817 return;
1818
Jim Inghame5ed8e92011-06-02 23:58:26 +00001819 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1820 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001821
1822 bool keep_going = true;
1823 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001824 bool print_hook_header;
1825 bool print_thread_header;
1826
1827 if (num_exe_ctx == 1)
1828 print_thread_header = false;
1829 else
1830 print_thread_header = true;
1831
1832 if (m_stop_hooks.size() == 1)
1833 print_hook_header = false;
1834 else
1835 print_hook_header = true;
1836
Jim Inghamd60d94a2011-03-11 03:53:59 +00001837 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1838 {
1839 // result.Clear();
1840 StopHookSP cur_hook_sp = (*pos).second;
1841 if (!cur_hook_sp->IsActive())
1842 continue;
1843
1844 bool any_thread_matched = false;
1845 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1846 {
1847 if ((cur_hook_sp->GetSpecifier () == NULL
1848 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1849 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Clayton567e7f32011-09-22 04:58:26 +00001850 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001851 {
1852 if (!hooks_ran)
1853 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001854 hooks_ran = true;
1855 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001856 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001857 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001858 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1859 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1860 NULL);
1861 if (cmd)
1862 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1863 else
1864 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001865 any_thread_matched = true;
1866 }
1867
Jim Inghamc54840c2011-03-22 01:47:27 +00001868 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001869 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001870
1871 bool stop_on_continue = true;
1872 bool stop_on_error = true;
1873 bool echo_commands = false;
1874 bool print_results = true;
1875 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001876 &exc_ctx_with_reasons[i],
1877 stop_on_continue,
1878 stop_on_error,
1879 echo_commands,
1880 print_results,
1881 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001882
1883 // If the command started the target going again, we should bag out of
1884 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001885 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1886 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001887 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001888 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001889 keep_going = false;
1890 }
1891 }
1892 }
1893 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00001894
Caroline Tice4a348082011-05-02 20:41:46 +00001895 result.GetImmediateOutputStream()->Flush();
1896 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001897}
1898
Greg Claytonbbea1332011-07-08 00:48:09 +00001899bool
1900Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1901{
1902 bool changed = false;
1903 if (module)
1904 {
1905 ObjectFile *object_file = module->GetObjectFile();
1906 if (object_file)
1907 {
1908 SectionList *section_list = object_file->GetSectionList ();
1909 if (section_list)
1910 {
1911 // All sections listed in the dyld image info structure will all
1912 // either be fixed up already, or they will all be off by a single
1913 // slide amount that is determined by finding the first segment
1914 // that is at file offset zero which also has bytes (a file size
1915 // that is greater than zero) in the object file.
1916
1917 // Determine the slide amount (if any)
1918 const size_t num_sections = section_list->GetSize();
1919 size_t sect_idx = 0;
1920 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1921 {
1922 // Iterate through the object file sections to find the
1923 // first section that starts of file offset zero and that
1924 // has bytes in the file...
1925 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1926 if (section)
1927 {
1928 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1929 changed = true;
1930 }
1931 }
1932 }
1933 }
1934 }
1935 return changed;
1936}
1937
1938
Jim Inghamd60d94a2011-03-11 03:53:59 +00001939//--------------------------------------------------------------
1940// class Target::StopHook
1941//--------------------------------------------------------------
1942
1943
1944Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1945 UserID (uid),
1946 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001947 m_commands (),
1948 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001949 m_thread_spec_ap(NULL),
1950 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001951{
1952}
1953
1954Target::StopHook::StopHook (const StopHook &rhs) :
1955 UserID (rhs.GetID()),
1956 m_target_sp (rhs.m_target_sp),
1957 m_commands (rhs.m_commands),
1958 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001959 m_thread_spec_ap (NULL),
1960 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001961{
1962 if (rhs.m_thread_spec_ap.get() != NULL)
1963 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1964}
1965
1966
1967Target::StopHook::~StopHook ()
1968{
1969}
1970
1971void
1972Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1973{
1974 m_thread_spec_ap.reset (specifier);
1975}
1976
1977
1978void
1979Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1980{
1981 int indent_level = s->GetIndentLevel();
1982
1983 s->SetIndentLevel(indent_level + 2);
1984
Greg Clayton444e35b2011-10-19 18:09:39 +00001985 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001986 if (m_active)
1987 s->Indent ("State: enabled\n");
1988 else
1989 s->Indent ("State: disabled\n");
1990
1991 if (m_specifier_sp)
1992 {
1993 s->Indent();
1994 s->PutCString ("Specifier:\n");
1995 s->SetIndentLevel (indent_level + 4);
1996 m_specifier_sp->GetDescription (s, level);
1997 s->SetIndentLevel (indent_level + 2);
1998 }
1999
2000 if (m_thread_spec_ap.get() != NULL)
2001 {
2002 StreamString tmp;
2003 s->Indent("Thread:\n");
2004 m_thread_spec_ap->GetDescription (&tmp, level);
2005 s->SetIndentLevel (indent_level + 4);
2006 s->Indent (tmp.GetData());
2007 s->PutCString ("\n");
2008 s->SetIndentLevel (indent_level + 2);
2009 }
2010
2011 s->Indent ("Commands: \n");
2012 s->SetIndentLevel (indent_level + 4);
2013 uint32_t num_commands = m_commands.GetSize();
2014 for (uint32_t i = 0; i < num_commands; i++)
2015 {
2016 s->Indent(m_commands.GetStringAtIndex(i));
2017 s->PutCString ("\n");
2018 }
2019 s->SetIndentLevel (indent_level);
2020}
2021
2022
Caroline Tice5bc8c972010-09-20 20:44:43 +00002023//--------------------------------------------------------------
2024// class Target::SettingsController
2025//--------------------------------------------------------------
2026
2027Target::SettingsController::SettingsController () :
2028 UserSettingsController ("target", Debugger::GetSettingsController()),
2029 m_default_architecture ()
2030{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002031}
2032
2033Target::SettingsController::~SettingsController ()
2034{
2035}
2036
2037lldb::InstanceSettingsSP
2038Target::SettingsController::CreateInstanceSettings (const char *instance_name)
2039{
Greg Clayton334d33a2012-01-30 07:41:31 +00002040 lldb::InstanceSettingsSP new_settings_sp (new TargetInstanceSettings (GetSettingsController(),
2041 false,
2042 instance_name));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002043 return new_settings_sp;
2044}
2045
Caroline Tice5bc8c972010-09-20 20:44:43 +00002046
Greg Claytonabb33022011-11-08 02:43:13 +00002047#define TSC_DEFAULT_ARCH "default-arch"
2048#define TSC_EXPR_PREFIX "expr-prefix"
2049#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
2050#define TSC_SKIP_PROLOGUE "skip-prologue"
2051#define TSC_SOURCE_MAP "source-map"
Greg Clayton9ce95382012-02-13 23:10:39 +00002052#define TSC_EXE_SEARCH_PATHS "exec-search-paths"
Greg Claytonabb33022011-11-08 02:43:13 +00002053#define TSC_MAX_CHILDREN "max-children-count"
2054#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
2055#define TSC_PLATFORM_AVOID "breakpoints-use-platform-avoid-list"
2056#define TSC_RUN_ARGS "run-args"
2057#define TSC_ENV_VARS "env-vars"
2058#define TSC_INHERIT_ENV "inherit-env"
2059#define TSC_STDIN_PATH "input-path"
2060#define TSC_STDOUT_PATH "output-path"
2061#define TSC_STDERR_PATH "error-path"
2062#define TSC_DISABLE_ASLR "disable-aslr"
2063#define TSC_DISABLE_STDIO "disable-stdio"
Greg Claytond284b662011-02-18 01:44:25 +00002064
2065
2066static const ConstString &
2067GetSettingNameForDefaultArch ()
2068{
2069 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00002070 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002071}
2072
Greg Claytond284b662011-02-18 01:44:25 +00002073static const ConstString &
2074GetSettingNameForExpressionPrefix ()
2075{
2076 static ConstString g_const_string (TSC_EXPR_PREFIX);
2077 return g_const_string;
2078}
2079
2080static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00002081GetSettingNameForPreferDynamicValue ()
2082{
2083 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
2084 return g_const_string;
2085}
2086
Greg Claytonff44ab42011-04-23 02:04:55 +00002087static const ConstString &
2088GetSettingNameForSourcePathMap ()
2089{
2090 static ConstString g_const_string (TSC_SOURCE_MAP);
2091 return g_const_string;
2092}
Greg Claytond284b662011-02-18 01:44:25 +00002093
Greg Clayton17cd9952011-04-22 03:55:06 +00002094static const ConstString &
Greg Clayton9ce95382012-02-13 23:10:39 +00002095GetSettingNameForExecutableSearchPaths ()
2096{
2097 static ConstString g_const_string (TSC_EXE_SEARCH_PATHS);
2098 return g_const_string;
2099}
2100
2101static const ConstString &
Greg Clayton17cd9952011-04-22 03:55:06 +00002102GetSettingNameForSkipPrologue ()
2103{
2104 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
2105 return g_const_string;
2106}
2107
Enrico Granata018921d2011-08-12 02:00:06 +00002108static const ConstString &
2109GetSettingNameForMaxChildren ()
2110{
2111 static ConstString g_const_string (TSC_MAX_CHILDREN);
2112 return g_const_string;
2113}
Greg Clayton17cd9952011-04-22 03:55:06 +00002114
Enrico Granata91544802011-09-06 19:20:51 +00002115static const ConstString &
2116GetSettingNameForMaxStringSummaryLength ()
2117{
2118 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2119 return g_const_string;
2120}
Greg Clayton17cd9952011-04-22 03:55:06 +00002121
Jim Ingham7089d8a2011-10-28 23:14:11 +00002122static const ConstString &
2123GetSettingNameForPlatformAvoid ()
2124{
2125 static ConstString g_const_string (TSC_PLATFORM_AVOID);
2126 return g_const_string;
2127}
2128
Greg Claytonabb33022011-11-08 02:43:13 +00002129const ConstString &
2130GetSettingNameForRunArgs ()
2131{
2132 static ConstString g_const_string (TSC_RUN_ARGS);
2133 return g_const_string;
2134}
2135
2136const ConstString &
2137GetSettingNameForEnvVars ()
2138{
2139 static ConstString g_const_string (TSC_ENV_VARS);
2140 return g_const_string;
2141}
2142
2143const ConstString &
2144GetSettingNameForInheritHostEnv ()
2145{
2146 static ConstString g_const_string (TSC_INHERIT_ENV);
2147 return g_const_string;
2148}
2149
2150const ConstString &
2151GetSettingNameForInputPath ()
2152{
2153 static ConstString g_const_string (TSC_STDIN_PATH);
2154 return g_const_string;
2155}
2156
2157const ConstString &
2158GetSettingNameForOutputPath ()
2159{
2160 static ConstString g_const_string (TSC_STDOUT_PATH);
2161 return g_const_string;
2162}
2163
2164const ConstString &
2165GetSettingNameForErrorPath ()
2166{
2167 static ConstString g_const_string (TSC_STDERR_PATH);
2168 return g_const_string;
2169}
2170
2171const ConstString &
2172GetSettingNameForDisableASLR ()
2173{
2174 static ConstString g_const_string (TSC_DISABLE_ASLR);
2175 return g_const_string;
2176}
2177
2178const ConstString &
2179GetSettingNameForDisableSTDIO ()
2180{
2181 static ConstString g_const_string (TSC_DISABLE_STDIO);
2182 return g_const_string;
2183}
Jim Ingham7089d8a2011-10-28 23:14:11 +00002184
Caroline Tice5bc8c972010-09-20 20:44:43 +00002185bool
2186Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2187 const char *index_value,
2188 const char *value,
2189 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002190 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002191 Error&err)
2192{
Greg Claytond284b662011-02-18 01:44:25 +00002193 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002194 {
Greg Claytonf15996e2011-04-07 22:46:35 +00002195 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00002196 if (!m_default_architecture.IsValid())
2197 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002198 }
2199 return true;
2200}
2201
2202
2203bool
2204Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2205 StringList &value,
2206 Error &err)
2207{
Greg Claytond284b662011-02-18 01:44:25 +00002208 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002209 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002210 // If the arch is invalid (the default), don't show a string for it
2211 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002212 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002213 return true;
2214 }
2215 else
2216 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2217
2218 return false;
2219}
2220
2221//--------------------------------------------------------------
2222// class TargetInstanceSettings
2223//--------------------------------------------------------------
2224
Greg Clayton638351a2010-12-04 00:10:17 +00002225TargetInstanceSettings::TargetInstanceSettings
2226(
Greg Clayton334d33a2012-01-30 07:41:31 +00002227 const lldb::UserSettingsControllerSP &owner_sp,
Greg Clayton638351a2010-12-04 00:10:17 +00002228 bool live_instance,
2229 const char *name
2230) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002231 InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002232 m_expr_prefix_file (),
Sean Callanane0b7f942011-11-16 01:54:57 +00002233 m_expr_prefix_contents (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002234 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00002235 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002236 m_source_map (NULL, NULL),
Greg Clayton9ce95382012-02-13 23:10:39 +00002237 m_exe_search_paths (),
Enrico Granata91544802011-09-06 19:20:51 +00002238 m_max_children_display(256),
Jim Ingham7089d8a2011-10-28 23:14:11 +00002239 m_max_strlen_length(1024),
Greg Claytonabb33022011-11-08 02:43:13 +00002240 m_breakpoints_use_platform_avoid (true, true),
2241 m_run_args (),
2242 m_env_vars (),
2243 m_input_path (),
2244 m_output_path (),
2245 m_error_path (),
2246 m_disable_aslr (true),
2247 m_disable_stdio (false),
2248 m_inherit_host_env (true),
2249 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002250{
2251 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2252 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2253 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2254 // This is true for CreateInstanceName() too.
2255
2256 if (GetInstanceName () == InstanceSettings::InvalidName())
2257 {
2258 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
Greg Clayton334d33a2012-01-30 07:41:31 +00002259 owner_sp->RegisterInstanceSettings (this);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002260 }
2261
2262 if (live_instance)
2263 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002264 const lldb::InstanceSettingsSP &pending_settings = owner_sp->FindPendingSettings (m_instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002265 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002266 }
2267}
2268
2269TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002270 InstanceSettings (Target::GetSettingsController(), CreateInstanceName().AsCString()),
Greg Claytonff44ab42011-04-23 02:04:55 +00002271 m_expr_prefix_file (rhs.m_expr_prefix_file),
Sean Callanane0b7f942011-11-16 01:54:57 +00002272 m_expr_prefix_contents (rhs.m_expr_prefix_contents),
Greg Claytonff44ab42011-04-23 02:04:55 +00002273 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2274 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002275 m_source_map (rhs.m_source_map),
Greg Clayton9ce95382012-02-13 23:10:39 +00002276 m_exe_search_paths (rhs.m_exe_search_paths),
Greg Claytonabb33022011-11-08 02:43:13 +00002277 m_max_children_display (rhs.m_max_children_display),
2278 m_max_strlen_length (rhs.m_max_strlen_length),
2279 m_breakpoints_use_platform_avoid (rhs.m_breakpoints_use_platform_avoid),
2280 m_run_args (rhs.m_run_args),
2281 m_env_vars (rhs.m_env_vars),
2282 m_input_path (rhs.m_input_path),
2283 m_output_path (rhs.m_output_path),
2284 m_error_path (rhs.m_error_path),
2285 m_disable_aslr (rhs.m_disable_aslr),
2286 m_disable_stdio (rhs.m_disable_stdio),
2287 m_inherit_host_env (rhs.m_inherit_host_env)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002288{
2289 if (m_instance_name != InstanceSettings::GetDefaultName())
2290 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002291 UserSettingsControllerSP owner_sp (m_owner_wp.lock());
2292 if (owner_sp)
2293 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name),false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002294 }
2295}
2296
2297TargetInstanceSettings::~TargetInstanceSettings ()
2298{
2299}
2300
2301TargetInstanceSettings&
2302TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2303{
2304 if (this != &rhs)
2305 {
Greg Claytonabb33022011-11-08 02:43:13 +00002306 m_expr_prefix_file = rhs.m_expr_prefix_file;
Sean Callanane0b7f942011-11-16 01:54:57 +00002307 m_expr_prefix_contents = rhs.m_expr_prefix_contents;
Greg Claytonabb33022011-11-08 02:43:13 +00002308 m_prefer_dynamic_value = rhs.m_prefer_dynamic_value;
2309 m_skip_prologue = rhs.m_skip_prologue;
2310 m_source_map = rhs.m_source_map;
Greg Clayton9ce95382012-02-13 23:10:39 +00002311 m_exe_search_paths = rhs.m_exe_search_paths;
Greg Claytonabb33022011-11-08 02:43:13 +00002312 m_max_children_display = rhs.m_max_children_display;
2313 m_max_strlen_length = rhs.m_max_strlen_length;
2314 m_breakpoints_use_platform_avoid = rhs.m_breakpoints_use_platform_avoid;
2315 m_run_args = rhs.m_run_args;
2316 m_env_vars = rhs.m_env_vars;
2317 m_input_path = rhs.m_input_path;
2318 m_output_path = rhs.m_output_path;
2319 m_error_path = rhs.m_error_path;
2320 m_disable_aslr = rhs.m_disable_aslr;
2321 m_disable_stdio = rhs.m_disable_stdio;
2322 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002323 }
2324
2325 return *this;
2326}
2327
Caroline Tice5bc8c972010-09-20 20:44:43 +00002328void
2329TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2330 const char *index_value,
2331 const char *value,
2332 const ConstString &instance_name,
2333 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002334 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002335 Error &err,
2336 bool pending)
2337{
Greg Claytond284b662011-02-18 01:44:25 +00002338 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002339 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002340 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2341 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002342 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002343 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002344 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002345 default:
2346 break;
2347 case eVarSetOperationAssign:
2348 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002349 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002350 m_expr_prefix_contents.clear();
2351
Greg Claytonff44ab42011-04-23 02:04:55 +00002352 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2353 {
2354 err.SetErrorToGenericError ();
Greg Clayton9c236732011-10-26 00:56:27 +00002355 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002356 return;
2357 }
2358
Greg Clayton4b23ab32012-01-06 02:01:06 +00002359 DataBufferSP file_data_sp (m_expr_prefix_file.GetCurrentValue().ReadFileContents(0, SIZE_MAX, &err));
Sean Callanane0b7f942011-11-16 01:54:57 +00002360
Greg Clayton4b23ab32012-01-06 02:01:06 +00002361 if (err.Success())
Greg Claytonff44ab42011-04-23 02:04:55 +00002362 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002363 if (file_data_sp && file_data_sp->GetByteSize() > 0)
2364 {
2365 m_expr_prefix_contents.assign((const char*)file_data_sp->GetBytes(), file_data_sp->GetByteSize());
2366 }
2367 else
2368 {
2369 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
2370 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002371 }
Sean Callanan77e93942010-10-29 00:29:03 +00002372 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002373 break;
2374 case eVarSetOperationClear:
Sean Callanane0b7f942011-11-16 01:54:57 +00002375 m_expr_prefix_contents.clear();
Sean Callanan77e93942010-10-29 00:29:03 +00002376 }
Sean Callanan77e93942010-10-29 00:29:03 +00002377 }
2378 }
Jim Inghame41494a2011-04-16 00:01:13 +00002379 else if (var_name == GetSettingNameForPreferDynamicValue())
2380 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002381 int new_value;
2382 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2383 if (err.Success())
2384 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002385 }
2386 else if (var_name == GetSettingNameForSkipPrologue())
2387 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002388 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2389 }
Enrico Granata018921d2011-08-12 02:00:06 +00002390 else if (var_name == GetSettingNameForMaxChildren())
2391 {
2392 bool ok;
2393 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2394 if (ok)
2395 m_max_children_display = new_value;
2396 }
Enrico Granata91544802011-09-06 19:20:51 +00002397 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2398 {
2399 bool ok;
2400 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2401 if (ok)
2402 m_max_strlen_length = new_value;
2403 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002404 else if (var_name == GetSettingNameForExecutableSearchPaths())
2405 {
2406 switch (op)
2407 {
2408 case eVarSetOperationReplace:
2409 case eVarSetOperationInsertBefore:
2410 case eVarSetOperationInsertAfter:
2411 case eVarSetOperationRemove:
2412 default:
2413 break;
2414 case eVarSetOperationAssign:
2415 m_exe_search_paths.Clear();
2416 // Fall through to append....
2417 case eVarSetOperationAppend:
2418 {
2419 Args args(value);
2420 const uint32_t argc = args.GetArgumentCount();
2421 if (argc > 0)
2422 {
2423 const char *exe_search_path_dir;
2424 for (uint32_t idx = 0; (exe_search_path_dir = args.GetArgumentAtIndex(idx)) != NULL; ++idx)
2425 {
2426 FileSpec file_spec;
2427 file_spec.GetDirectory().SetCString(exe_search_path_dir);
2428 FileSpec::FileType file_type = file_spec.GetFileType();
2429 if (file_type == FileSpec::eFileTypeDirectory || file_type == FileSpec::eFileTypeInvalid)
2430 {
2431 m_exe_search_paths.Append(file_spec);
2432 }
2433 else
2434 {
2435 err.SetErrorStringWithFormat("executable search path '%s' exists, but it does not resolve to a directory", exe_search_path_dir);
2436 }
2437 }
2438 }
2439 }
2440 break;
2441
2442 case eVarSetOperationClear:
2443 m_exe_search_paths.Clear();
2444 break;
2445 }
2446 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002447 else if (var_name == GetSettingNameForSourcePathMap ())
2448 {
2449 switch (op)
2450 {
2451 case eVarSetOperationReplace:
2452 case eVarSetOperationInsertBefore:
2453 case eVarSetOperationInsertAfter:
2454 case eVarSetOperationRemove:
2455 default:
2456 break;
2457 case eVarSetOperationAssign:
2458 m_source_map.Clear(true);
2459 // Fall through to append....
2460 case eVarSetOperationAppend:
2461 {
2462 Args args(value);
2463 const uint32_t argc = args.GetArgumentCount();
2464 if (argc & 1 || argc == 0)
2465 {
2466 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2467 }
2468 else
2469 {
2470 char resolved_new_path[PATH_MAX];
2471 FileSpec file_spec;
2472 const char *old_path;
2473 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2474 {
2475 const char *new_path = args.GetArgumentAtIndex(idx+1);
2476 assert (new_path); // We have an even number of paths, this shouldn't happen!
2477
2478 file_spec.SetFile(new_path, true);
2479 if (file_spec.Exists())
2480 {
2481 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2482 {
2483 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2484 return;
2485 }
2486 }
2487 else
2488 {
2489 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2490 return;
2491 }
2492 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2493 }
2494 }
2495 }
2496 break;
2497
2498 case eVarSetOperationClear:
2499 m_source_map.Clear(true);
2500 break;
2501 }
Jim Inghame41494a2011-04-16 00:01:13 +00002502 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002503 else if (var_name == GetSettingNameForPlatformAvoid ())
2504 {
2505 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_breakpoints_use_platform_avoid);
2506 }
Greg Claytonabb33022011-11-08 02:43:13 +00002507 else if (var_name == GetSettingNameForRunArgs())
2508 {
2509 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2510 }
2511 else if (var_name == GetSettingNameForEnvVars())
2512 {
2513 // This is nice for local debugging, but it is isn't correct for
2514 // remote debugging. We need to stop process.env-vars from being
2515 // populated with the host environment and add this as a launch option
2516 // and get the correct environment from the Target's platform.
2517 // GetHostEnvironmentIfNeeded ();
2518 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2519 }
2520 else if (var_name == GetSettingNameForInputPath())
2521 {
2522 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2523 }
2524 else if (var_name == GetSettingNameForOutputPath())
2525 {
2526 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2527 }
2528 else if (var_name == GetSettingNameForErrorPath())
2529 {
2530 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2531 }
2532 else if (var_name == GetSettingNameForDisableASLR())
2533 {
2534 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
2535 }
2536 else if (var_name == GetSettingNameForDisableSTDIO ())
2537 {
2538 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
2539 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002540}
2541
2542void
Greg Claytond284b662011-02-18 01:44:25 +00002543TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002544{
Sean Callanan77e93942010-10-29 00:29:03 +00002545 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2546
2547 if (!new_settings_ptr)
2548 return;
2549
Greg Claytonabb33022011-11-08 02:43:13 +00002550 *this = *new_settings_ptr;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002551}
2552
Caroline Ticebcb5b452010-09-20 21:37:42 +00002553bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002554TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2555 const ConstString &var_name,
2556 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002557 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002558{
Greg Claytond284b662011-02-18 01:44:25 +00002559 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002560 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002561 char path[PATH_MAX];
2562 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2563 if (path_len > 0)
2564 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002565 }
Jim Inghame41494a2011-04-16 00:01:13 +00002566 else if (var_name == GetSettingNameForPreferDynamicValue())
2567 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002568 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002569 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002570 else if (var_name == GetSettingNameForSkipPrologue())
2571 {
2572 if (m_skip_prologue)
2573 value.AppendString ("true");
2574 else
2575 value.AppendString ("false");
2576 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002577 else if (var_name == GetSettingNameForExecutableSearchPaths())
2578 {
2579 if (m_exe_search_paths.GetSize())
2580 {
2581 for (size_t i = 0, n = m_exe_search_paths.GetSize(); i < n; ++i)
2582 {
2583 value.AppendString(m_exe_search_paths.GetFileSpecAtIndex (i).GetDirectory().AsCString());
2584 }
2585 }
2586 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002587 else if (var_name == GetSettingNameForSourcePathMap ())
2588 {
Johnny Chen931449e2011-12-12 21:59:28 +00002589 if (m_source_map.GetSize())
2590 {
2591 size_t i;
2592 for (i = 0; i < m_source_map.GetSize(); ++i) {
2593 StreamString sstr;
2594 m_source_map.Dump(&sstr, i);
2595 value.AppendString(sstr.GetData());
2596 }
2597 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002598 }
Enrico Granata018921d2011-08-12 02:00:06 +00002599 else if (var_name == GetSettingNameForMaxChildren())
2600 {
2601 StreamString count_str;
2602 count_str.Printf ("%d", m_max_children_display);
2603 value.AppendString (count_str.GetData());
2604 }
Enrico Granata91544802011-09-06 19:20:51 +00002605 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2606 {
2607 StreamString count_str;
2608 count_str.Printf ("%d", m_max_strlen_length);
2609 value.AppendString (count_str.GetData());
2610 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002611 else if (var_name == GetSettingNameForPlatformAvoid())
2612 {
2613 if (m_breakpoints_use_platform_avoid)
2614 value.AppendString ("true");
2615 else
2616 value.AppendString ("false");
2617 }
Greg Claytonabb33022011-11-08 02:43:13 +00002618 else if (var_name == GetSettingNameForRunArgs())
2619 {
2620 if (m_run_args.GetArgumentCount() > 0)
2621 {
2622 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2623 value.AppendString (m_run_args.GetArgumentAtIndex (i));
2624 }
2625 }
2626 else if (var_name == GetSettingNameForEnvVars())
2627 {
2628 GetHostEnvironmentIfNeeded ();
2629
2630 if (m_env_vars.size() > 0)
2631 {
2632 std::map<std::string, std::string>::iterator pos;
2633 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2634 {
2635 StreamString value_str;
2636 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2637 value.AppendString (value_str.GetData());
2638 }
2639 }
2640 }
2641 else if (var_name == GetSettingNameForInputPath())
2642 {
2643 value.AppendString (m_input_path.c_str());
2644 }
2645 else if (var_name == GetSettingNameForOutputPath())
2646 {
2647 value.AppendString (m_output_path.c_str());
2648 }
2649 else if (var_name == GetSettingNameForErrorPath())
2650 {
2651 value.AppendString (m_error_path.c_str());
2652 }
2653 else if (var_name == GetSettingNameForInheritHostEnv())
2654 {
2655 if (m_inherit_host_env)
2656 value.AppendString ("true");
2657 else
2658 value.AppendString ("false");
2659 }
2660 else if (var_name == GetSettingNameForDisableASLR())
2661 {
2662 if (m_disable_aslr)
2663 value.AppendString ("true");
2664 else
2665 value.AppendString ("false");
2666 }
2667 else if (var_name == GetSettingNameForDisableSTDIO())
2668 {
2669 if (m_disable_stdio)
2670 value.AppendString ("true");
2671 else
2672 value.AppendString ("false");
2673 }
Sean Callanan77e93942010-10-29 00:29:03 +00002674 else
2675 {
2676 if (err)
2677 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2678 return false;
2679 }
Sean Callanan77e93942010-10-29 00:29:03 +00002680 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002681}
2682
Greg Claytonabb33022011-11-08 02:43:13 +00002683void
2684Target::TargetInstanceSettings::GetHostEnvironmentIfNeeded ()
2685{
2686 if (m_inherit_host_env && !m_got_host_env)
2687 {
2688 m_got_host_env = true;
2689 StringList host_env;
2690 const size_t host_env_count = Host::GetEnvironment (host_env);
2691 for (size_t idx=0; idx<host_env_count; idx++)
2692 {
2693 const char *env_entry = host_env.GetStringAtIndex (idx);
2694 if (env_entry)
2695 {
2696 const char *equal_pos = ::strchr(env_entry, '=');
2697 if (equal_pos)
2698 {
2699 std::string key (env_entry, equal_pos - env_entry);
2700 std::string value (equal_pos + 1);
2701 if (m_env_vars.find (key) == m_env_vars.end())
2702 m_env_vars[key] = value;
2703 }
2704 }
2705 }
2706 }
2707}
2708
2709
2710size_t
2711Target::TargetInstanceSettings::GetEnvironmentAsArgs (Args &env)
2712{
2713 GetHostEnvironmentIfNeeded ();
2714
2715 dictionary::const_iterator pos, end = m_env_vars.end();
2716 for (pos = m_env_vars.begin(); pos != end; ++pos)
2717 {
2718 std::string env_var_equal_value (pos->first);
2719 env_var_equal_value.append(1, '=');
2720 env_var_equal_value.append (pos->second);
2721 env.AppendArgument (env_var_equal_value.c_str());
2722 }
2723 return env.GetArgumentCount();
2724}
2725
2726
Caroline Tice5bc8c972010-09-20 20:44:43 +00002727const ConstString
2728TargetInstanceSettings::CreateInstanceName ()
2729{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002730 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002731 static int instance_count = 1;
2732
Caroline Tice5bc8c972010-09-20 20:44:43 +00002733 sstr.Printf ("target_%d", instance_count);
2734 ++instance_count;
2735
2736 const ConstString ret_val (sstr.GetData());
2737 return ret_val;
2738}
2739
2740//--------------------------------------------------
2741// Target::SettingsController Variable Tables
2742//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002743OptionEnumValueElement
2744TargetInstanceSettings::g_dynamic_value_types[] =
2745{
Greg Clayton577fbc32011-05-30 00:39:48 +00002746{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2747{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2748{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002749{ 0, NULL, NULL }
2750};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002751
2752SettingEntry
2753Target::SettingsController::global_settings_table[] =
2754{
Greg Claytond284b662011-02-18 01:44:25 +00002755 // var-name var-type default enum init'd hidden help-text
2756 // ================= ================== =========== ==== ====== ====== =========================================================================
2757 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2758 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2759};
2760
Caroline Tice5bc8c972010-09-20 20:44:43 +00002761SettingEntry
2762Target::SettingsController::instance_settings_table[] =
2763{
Enrico Granata91544802011-09-06 19:20:51 +00002764 // var-name var-type default enum init'd hidden help-text
2765 // ================= ================== =============== ======================= ====== ====== =========================================================================
2766 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2767 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2768 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2769 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
Greg Clayton9ce95382012-02-13 23:10:39 +00002770 { TSC_EXE_SEARCH_PATHS , eSetVarTypeArray , NULL , NULL, false, false, "Executable search paths to use when locating executable files whose paths don't match the local file system." },
Enrico Granata91544802011-09-06 19:20:51 +00002771 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2772 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
Jim Ingham7089d8a2011-10-28 23:14:11 +00002773 { TSC_PLATFORM_AVOID , eSetVarTypeBoolean, "true" , NULL, false, false, "Consult the platform module avoid list when setting non-module specific breakpoints." },
Greg Claytonabb33022011-11-08 02:43:13 +00002774 { TSC_RUN_ARGS , eSetVarTypeArray , NULL , NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2775 { TSC_ENV_VARS , eSetVarTypeDictionary, NULL , NULL, false, false, "A list of all the environment variables to be passed to the executable's environment, and their values." },
2776 { TSC_INHERIT_ENV , eSetVarTypeBoolean, "true" , NULL, false, false, "Inherit the environment from the process that is running LLDB." },
2777 { TSC_STDIN_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for reading its standard input." },
2778 { TSC_STDOUT_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard output." },
2779 { TSC_STDERR_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard error." },
2780// { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
2781 { TSC_DISABLE_ASLR , eSetVarTypeBoolean, "true" , NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2782 { TSC_DISABLE_STDIO , eSetVarTypeBoolean, "false" , NULL, false, false, "Disable stdin/stdout for process (e.g. for a GUI application)" },
Enrico Granata91544802011-09-06 19:20:51 +00002783 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002784};
Jim Ingham5a15e692012-02-16 06:50:00 +00002785
2786const ConstString &
2787Target::TargetEventData::GetFlavorString ()
2788{
2789 static ConstString g_flavor ("Target::TargetEventData");
2790 return g_flavor;
2791}
2792
2793const ConstString &
2794Target::TargetEventData::GetFlavor () const
2795{
2796 return TargetEventData::GetFlavorString ();
2797}
2798
2799Target::TargetEventData::TargetEventData (const lldb::TargetSP &new_target_sp) :
2800 EventData(),
2801 m_target_sp (new_target_sp)
2802{
2803}
2804
2805Target::TargetEventData::~TargetEventData()
2806{
2807
2808}
2809
2810void
2811Target::TargetEventData::Dump (Stream *s) const
2812{
2813
2814}
2815
2816const TargetSP
2817Target::TargetEventData::GetTargetFromEvent (const lldb::EventSP &event_sp)
2818{
2819 TargetSP target_sp;
2820
2821 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
2822 if (data)
2823 target_sp = data->m_target_sp;
2824
2825 return target_sp;
2826}
2827
2828const Target::TargetEventData *
2829Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
2830{
2831 if (event_ptr)
2832 {
2833 const EventData *event_data = event_ptr->GetData();
2834 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
2835 return static_cast <const TargetEventData *> (event_ptr->GetData());
2836 }
2837 return NULL;
2838}
2839