blob: 6a6cb0eae7e5f8daea14b0084de046c1c515bc3a [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
44//----------------------------------------------------------------------
45// Target constructor
46//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000047Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
48 Broadcaster ("lldb.target"),
49 ExecutionContextScope (),
Greg Clayton334d33a2012-01-30 07:41:31 +000050 TargetInstanceSettings (GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000051 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000052 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000053 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000054 m_arch (target_arch),
55 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000056 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000057 m_breakpoint_list (false),
58 m_internal_breakpoint_list (true),
Johnny Chenecd4feb2011-10-14 00:42:25 +000059 m_watchpoint_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000060 m_process_sp (),
61 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000062 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000063 m_scratch_ast_context_ap (NULL),
Sean Callanan4938bd62011-11-16 18:20:47 +000064 m_scratch_ast_source_ap (NULL),
65 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000066 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000067 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000068 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000069 m_stop_hook_next_id (0),
70 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000071{
Greg Clayton49ce6822010-10-31 03:01:06 +000072 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
73 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
74 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
75
Greg Claytone005f2c2010-11-06 01:53:30 +000076 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000077 if (log)
78 log->Printf ("%p Target::Target()", this);
79}
80
81//----------------------------------------------------------------------
82// Destructor
83//----------------------------------------------------------------------
84Target::~Target()
85{
Greg Claytone005f2c2010-11-06 01:53:30 +000086 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000087 if (log)
88 log->Printf ("%p Target::~Target()", this);
89 DeleteCurrentProcess ();
90}
91
92void
Caroline Tice7826c882010-10-26 03:11:13 +000093Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000094{
Greg Clayton3fed8b92010-10-08 00:21:05 +000095// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000096 if (description_level != lldb::eDescriptionLevelBrief)
97 {
98 s->Indent();
99 s->PutCString("Target\n");
100 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000101 m_images.Dump(s);
102 m_breakpoint_list.Dump(s);
103 m_internal_breakpoint_list.Dump(s);
104 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000105 }
106 else
107 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000108 Module *exe_module = GetExecutableModulePointer();
109 if (exe_module)
110 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000111 else
112 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000113 }
Chris Lattner24943d22010-06-08 16:52:24 +0000114}
115
116void
117Target::DeleteCurrentProcess ()
118{
119 if (m_process_sp.get())
120 {
Greg Clayton49480b12010-09-14 23:52:43 +0000121 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000122 if (m_process_sp->IsAlive())
123 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000124
125 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000126
127 // Do any cleanup of the target we need to do between process instances.
128 // NB It is better to do this before destroying the process in case the
129 // clean up needs some help from the process.
130 m_breakpoint_list.ClearAllBreakpointSites();
131 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000132 // Disable watchpoints just on the debugger side.
133 DisableAllWatchpoints(false);
Chris Lattner24943d22010-06-08 16:52:24 +0000134 m_process_sp.reset();
135 }
136}
137
138const lldb::ProcessSP &
Greg Clayton46c9a352012-02-09 06:16:32 +0000139Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner24943d22010-06-08 16:52:24 +0000140{
141 DeleteCurrentProcess ();
Greg Clayton46c9a352012-02-09 06:16:32 +0000142 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner24943d22010-06-08 16:52:24 +0000143 return m_process_sp;
144}
145
146const lldb::ProcessSP &
147Target::GetProcessSP () const
148{
149 return m_process_sp;
150}
151
Greg Clayton153ccd72011-08-10 02:10:13 +0000152void
153Target::Destroy()
154{
155 Mutex::Locker locker (m_mutex);
156 DeleteCurrentProcess ();
157 m_platform_sp.reset();
158 m_arch.Clear();
159 m_images.Clear();
160 m_section_load_list.Clear();
161 const bool notify = false;
162 m_breakpoint_list.RemoveAll(notify);
163 m_internal_breakpoint_list.RemoveAll(notify);
164 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000165 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000166 m_search_filter_sp.reset();
167 m_image_search_paths.Clear(notify);
168 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000169 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000170 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000171 m_persistent_variables.Clear();
172 m_stop_hooks.clear();
173 m_stop_hook_next_id = 0;
174 m_suppress_stop_hooks = false;
175}
176
177
Chris Lattner24943d22010-06-08 16:52:24 +0000178BreakpointList &
179Target::GetBreakpointList(bool internal)
180{
181 if (internal)
182 return m_internal_breakpoint_list;
183 else
184 return m_breakpoint_list;
185}
186
187const BreakpointList &
188Target::GetBreakpointList(bool internal) const
189{
190 if (internal)
191 return m_internal_breakpoint_list;
192 else
193 return m_breakpoint_list;
194}
195
196BreakpointSP
197Target::GetBreakpointByID (break_id_t break_id)
198{
199 BreakpointSP bp_sp;
200
201 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
202 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
203 else
204 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
205
206 return bp_sp;
207}
208
209BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000210Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
211 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000212 RegularExpression &source_regex,
213 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000214{
Jim Inghamd6d47972011-09-23 00:54:11 +0000215 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
216 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000217 return CreateBreakpoint (filter_sp, resolver_sp, internal);
218}
219
220
221BreakpointSP
222Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
223{
224 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner24943d22010-06-08 16:52:24 +0000225 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
226 return CreateBreakpoint (filter_sp, resolver_sp, internal);
227}
228
229
230BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000231Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000232{
Chris Lattner24943d22010-06-08 16:52:24 +0000233 Address so_addr;
234 // Attempt to resolve our load address if possible, though it is ok if
235 // it doesn't resolve to section/offset.
236
Greg Clayton33ed1702010-08-24 00:45:41 +0000237 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000238 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000239 if (!so_addr.IsValid())
240 {
241 // The address didn't resolve, so just set this as an absolute address
242 so_addr.SetOffset (addr);
243 }
244 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000245 return bp_sp;
246}
247
248BreakpointSP
249Target::CreateBreakpoint (Address &addr, bool internal)
250{
Greg Clayton13d24fb2012-01-29 20:56:30 +0000251 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000252 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
253 return CreateBreakpoint (filter_sp, resolver_sp, internal);
254}
255
256BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000257Target::CreateBreakpoint (const FileSpecList *containingModules,
258 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000259 const char *func_name,
260 uint32_t func_name_type_mask,
261 bool internal,
262 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000263{
Greg Clayton12bec712010-06-28 21:30:43 +0000264 BreakpointSP bp_sp;
265 if (func_name)
266 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000267 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000268
269 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
270 func_name,
271 func_name_type_mask,
272 Breakpoint::Exact,
273 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000274 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
275 }
276 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000277}
278
279
280SearchFilterSP
281Target::GetSearchFilterForModule (const FileSpec *containingModule)
282{
283 SearchFilterSP filter_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000284 if (containingModule != NULL)
285 {
286 // TODO: We should look into sharing module based search filters
287 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000288 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner24943d22010-06-08 16:52:24 +0000289 }
290 else
291 {
292 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000293 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Chris Lattner24943d22010-06-08 16:52:24 +0000294 filter_sp = m_search_filter_sp;
295 }
296 return filter_sp;
297}
298
Jim Ingham03c8ee52011-09-21 01:17:13 +0000299SearchFilterSP
300Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
301{
302 SearchFilterSP filter_sp;
Jim Ingham03c8ee52011-09-21 01:17:13 +0000303 if (containingModules && containingModules->GetSize() != 0)
304 {
305 // TODO: We should look into sharing module based search filters
306 // across many breakpoints like we do for the simple target based one
Greg Clayton13d24fb2012-01-29 20:56:30 +0000307 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000308 }
309 else
310 {
311 if (m_search_filter_sp.get() == NULL)
Greg Clayton13d24fb2012-01-29 20:56:30 +0000312 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (shared_from_this()));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000313 filter_sp = m_search_filter_sp;
314 }
315 return filter_sp;
316}
317
Jim Inghamd6d47972011-09-23 00:54:11 +0000318SearchFilterSP
319Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles)
320{
321 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
322 return GetSearchFilterForModuleList(containingModules);
323
324 SearchFilterSP filter_sp;
Jim Inghamd6d47972011-09-23 00:54:11 +0000325 if (containingModules == NULL)
326 {
327 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
328 // but that will take a little reworking.
329
Greg Clayton13d24fb2012-01-29 20:56:30 +0000330 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000331 }
332 else
333 {
Greg Clayton13d24fb2012-01-29 20:56:30 +0000334 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Inghamd6d47972011-09-23 00:54:11 +0000335 }
336 return filter_sp;
337}
338
Chris Lattner24943d22010-06-08 16:52:24 +0000339BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000340Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
341 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000342 RegularExpression &func_regex,
343 bool internal,
344 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000345{
Jim Inghamd6d47972011-09-23 00:54:11 +0000346 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000347 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
348 func_regex,
349 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000350
351 return CreateBreakpoint (filter_sp, resolver_sp, internal);
352}
353
354BreakpointSP
355Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
356{
357 BreakpointSP bp_sp;
358 if (filter_sp && resolver_sp)
359 {
360 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
361 resolver_sp->SetBreakpoint (bp_sp.get());
362
363 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000364 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000365 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000366 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000367
Greg Claytone005f2c2010-11-06 01:53:30 +0000368 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000369 if (log)
370 {
371 StreamString s;
372 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
373 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
374 }
375
Chris Lattner24943d22010-06-08 16:52:24 +0000376 bp_sp->ResolveBreakpoint();
377 }
Jim Inghamd1686902010-10-14 23:45:03 +0000378
379 if (!internal && bp_sp)
380 {
381 m_last_created_breakpoint = bp_sp;
382 }
383
Chris Lattner24943d22010-06-08 16:52:24 +0000384 return bp_sp;
385}
386
Johnny Chenda5a8022011-09-20 23:28:55 +0000387bool
388Target::ProcessIsValid()
389{
390 return (m_process_sp && m_process_sp->IsAlive());
391}
392
Johnny Chenecd4feb2011-10-14 00:42:25 +0000393// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000394// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000395WatchpointSP
396Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type)
Johnny Chen34bbf852011-09-12 23:38:44 +0000397{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000398 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
399 if (log)
400 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
401 __FUNCTION__, addr, size, type);
402
Johnny Chenecd4feb2011-10-14 00:42:25 +0000403 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000404 if (!ProcessIsValid())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000405 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000406 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000407 return wp_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000408
Johnny Chenecd4feb2011-10-14 00:42:25 +0000409 // Currently we only support one watchpoint per address, with total number
410 // of watchpoints limited by the hardware which the inferior is running on.
411 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000412 if (matched_sp)
413 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000414 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000415 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000416 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
417 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000418 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000419 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000420 wp_sp = matched_sp;
421 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000422 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000423 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000424 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000425 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000426 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000427 }
428
Johnny Chenecd4feb2011-10-14 00:42:25 +0000429 if (!wp_sp) {
430 Watchpoint *new_wp = new Watchpoint(addr, size);
431 if (!new_wp) {
432 printf("Watchpoint ctor failed, out of memory?\n");
433 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000434 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000435 new_wp->SetWatchpointType(type);
436 new_wp->SetTarget(this);
437 wp_sp.reset(new_wp);
438 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000439 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000440
Johnny Chenecd4feb2011-10-14 00:42:25 +0000441 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000442 if (log)
443 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
444 __FUNCTION__,
445 rc.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000446 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000447
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000448 if (rc.Fail())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000449 wp_sp.reset();
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000450 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000451 m_last_created_watchpoint = wp_sp;
452 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000453}
454
Chris Lattner24943d22010-06-08 16:52:24 +0000455void
456Target::RemoveAllBreakpoints (bool internal_also)
457{
Greg Claytone005f2c2010-11-06 01:53:30 +0000458 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000459 if (log)
460 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
461
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000462 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000463 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000464 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000465
466 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000467}
468
469void
470Target::DisableAllBreakpoints (bool internal_also)
471{
Greg Claytone005f2c2010-11-06 01:53:30 +0000472 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000473 if (log)
474 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
475
476 m_breakpoint_list.SetEnabledAll (false);
477 if (internal_also)
478 m_internal_breakpoint_list.SetEnabledAll (false);
479}
480
481void
482Target::EnableAllBreakpoints (bool internal_also)
483{
Greg Claytone005f2c2010-11-06 01:53:30 +0000484 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000485 if (log)
486 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
487
488 m_breakpoint_list.SetEnabledAll (true);
489 if (internal_also)
490 m_internal_breakpoint_list.SetEnabledAll (true);
491}
492
493bool
494Target::RemoveBreakpointByID (break_id_t break_id)
495{
Greg Claytone005f2c2010-11-06 01:53:30 +0000496 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000497 if (log)
498 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
499
500 if (DisableBreakpointByID (break_id))
501 {
502 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000503 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000504 else
Jim Inghamd1686902010-10-14 23:45:03 +0000505 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000506 if (m_last_created_breakpoint)
507 {
508 if (m_last_created_breakpoint->GetID() == break_id)
509 m_last_created_breakpoint.reset();
510 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000511 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000512 }
Chris Lattner24943d22010-06-08 16:52:24 +0000513 return true;
514 }
515 return false;
516}
517
518bool
519Target::DisableBreakpointByID (break_id_t break_id)
520{
Greg Claytone005f2c2010-11-06 01:53:30 +0000521 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000522 if (log)
523 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
524
525 BreakpointSP bp_sp;
526
527 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
528 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
529 else
530 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
531 if (bp_sp)
532 {
533 bp_sp->SetEnabled (false);
534 return true;
535 }
536 return false;
537}
538
539bool
540Target::EnableBreakpointByID (break_id_t break_id)
541{
Greg Claytone005f2c2010-11-06 01:53:30 +0000542 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000543 if (log)
544 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
545 __FUNCTION__,
546 break_id,
547 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
548
549 BreakpointSP bp_sp;
550
551 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
552 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
553 else
554 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
555
556 if (bp_sp)
557 {
558 bp_sp->SetEnabled (true);
559 return true;
560 }
561 return false;
562}
563
Johnny Chenc86582f2011-09-23 21:21:43 +0000564// The flag 'end_to_end', default to true, signifies that the operation is
565// performed end to end, for both the debugger and the debuggee.
566
Johnny Chenecd4feb2011-10-14 00:42:25 +0000567// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
568// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000569bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000570Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000571{
572 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
573 if (log)
574 log->Printf ("Target::%s\n", __FUNCTION__);
575
Johnny Chenc86582f2011-09-23 21:21:43 +0000576 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000577 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000578 return true;
579 }
580
581 // Otherwise, it's an end to end operation.
582
Johnny Chenda5a8022011-09-20 23:28:55 +0000583 if (!ProcessIsValid())
584 return false;
585
Johnny Chenecd4feb2011-10-14 00:42:25 +0000586 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000587 for (size_t i = 0; i < num_watchpoints; ++i)
588 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000589 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
590 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000591 return false;
592
Johnny Chenecd4feb2011-10-14 00:42:25 +0000593 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000594 if (rc.Fail())
595 return false;
596 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000597 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000598 return true; // Success!
599}
600
Johnny Chenecd4feb2011-10-14 00:42:25 +0000601// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
602// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000603bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000604Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000605{
606 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
607 if (log)
608 log->Printf ("Target::%s\n", __FUNCTION__);
609
Johnny Chenc86582f2011-09-23 21:21:43 +0000610 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000611 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000612 return true;
613 }
614
615 // Otherwise, it's an end to end operation.
616
Johnny Chenda5a8022011-09-20 23:28:55 +0000617 if (!ProcessIsValid())
618 return false;
619
Johnny Chenecd4feb2011-10-14 00:42:25 +0000620 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000621 for (size_t i = 0; i < num_watchpoints; ++i)
622 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000623 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
624 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000625 return false;
626
Johnny Chenecd4feb2011-10-14 00:42:25 +0000627 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000628 if (rc.Fail())
629 return false;
630 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000631 return true; // Success!
632}
633
Johnny Chenecd4feb2011-10-14 00:42:25 +0000634// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
635// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000636bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000637Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000638{
639 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
640 if (log)
641 log->Printf ("Target::%s\n", __FUNCTION__);
642
Johnny Chenc86582f2011-09-23 21:21:43 +0000643 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000644 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000645 return true;
646 }
647
648 // Otherwise, it's an end to end operation.
649
Johnny Chenda5a8022011-09-20 23:28:55 +0000650 if (!ProcessIsValid())
651 return false;
652
Johnny Chenecd4feb2011-10-14 00:42:25 +0000653 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000654 for (size_t i = 0; i < num_watchpoints; ++i)
655 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000656 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
657 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000658 return false;
659
Johnny Chenecd4feb2011-10-14 00:42:25 +0000660 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000661 if (rc.Fail())
662 return false;
663 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000664 return true; // Success!
665}
666
Johnny Chenecd4feb2011-10-14 00:42:25 +0000667// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000668// during these operations.
669bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000670Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000671{
672 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
673 if (log)
674 log->Printf ("Target::%s\n", __FUNCTION__);
675
676 if (!ProcessIsValid())
677 return false;
678
Johnny Chenecd4feb2011-10-14 00:42:25 +0000679 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000680 for (size_t i = 0; i < num_watchpoints; ++i)
681 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000682 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
683 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000684 return false;
685
Johnny Chenecd4feb2011-10-14 00:42:25 +0000686 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000687 }
688 return true; // Success!
689}
690
Johnny Chenecd4feb2011-10-14 00:42:25 +0000691// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000692bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000693Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000694{
695 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
696 if (log)
697 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
698
699 if (!ProcessIsValid())
700 return false;
701
Johnny Chenecd4feb2011-10-14 00:42:25 +0000702 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
703 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000704 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000705 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000706 if (rc.Success())
707 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000708
Johnny Chen01acfa72011-09-22 18:04:58 +0000709 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000710 }
711 return false;
712}
713
Johnny Chenecd4feb2011-10-14 00:42:25 +0000714// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000715bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000716Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000717{
718 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
719 if (log)
720 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
721
722 if (!ProcessIsValid())
723 return false;
724
Johnny Chenecd4feb2011-10-14 00:42:25 +0000725 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
726 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000727 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000728 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000729 if (rc.Success())
730 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000731
Johnny Chen01acfa72011-09-22 18:04:58 +0000732 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000733 }
734 return false;
735}
736
Johnny Chenecd4feb2011-10-14 00:42:25 +0000737// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000738bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000739Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000740{
741 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
742 if (log)
743 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
744
Johnny Chenecd4feb2011-10-14 00:42:25 +0000745 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000746 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000747 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000748 return true;
749 }
750 return false;
751}
752
Johnny Chenecd4feb2011-10-14 00:42:25 +0000753// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000754bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000755Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000756{
757 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
758 if (log)
759 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
760
761 if (!ProcessIsValid())
762 return false;
763
Johnny Chenecd4feb2011-10-14 00:42:25 +0000764 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
765 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000766 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000767 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000768 return true;
769 }
770 return false;
771}
772
Chris Lattner24943d22010-06-08 16:52:24 +0000773ModuleSP
774Target::GetExecutableModule ()
775{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000776 return m_images.GetModuleAtIndex(0);
777}
778
779Module*
780Target::GetExecutableModulePointer ()
781{
782 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000783}
784
785void
786Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
787{
788 m_images.Clear();
789 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000790 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000791 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000792
793 if (executable_sp.get())
794 {
795 Timer scoped_timer (__PRETTY_FUNCTION__,
796 "Target::SetExecutableModule (executable = '%s/%s')",
797 executable_sp->GetFileSpec().GetDirectory().AsCString(),
798 executable_sp->GetFileSpec().GetFilename().AsCString());
799
800 m_images.Append(executable_sp); // The first image is our exectuable file
801
Jim Ingham7508e732010-08-09 23:31:02 +0000802 // 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 +0000803 if (!m_arch.IsValid())
804 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000805
Chris Lattner24943d22010-06-08 16:52:24 +0000806 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000807 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000808
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000809 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000810 {
811 executable_objfile->GetDependentModules(dependent_files);
812 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
813 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000814 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
815 FileSpec platform_dependent_file_spec;
816 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000817 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000818 else
819 platform_dependent_file_spec = dependent_file_spec;
820
821 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000822 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000823 if (image_module_sp.get())
824 {
Chris Lattner24943d22010-06-08 16:52:24 +0000825 ObjectFile *objfile = image_module_sp->GetObjectFile();
826 if (objfile)
827 objfile->GetDependentModules(dependent_files);
828 }
829 }
830 }
Chris Lattner24943d22010-06-08 16:52:24 +0000831 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000832
833 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000834}
835
836
Jim Ingham7508e732010-08-09 23:31:02 +0000837bool
838Target::SetArchitecture (const ArchSpec &arch_spec)
839{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000840 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000841 {
842 // If we're setting the architecture to our current architecture, we
843 // don't need to do anything.
844 return true;
845 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000846 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000847 {
848 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000849 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000850 return true;
851 }
852 else
853 {
854 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000855 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000856 ModuleSP executable_sp = GetExecutableModule ();
857 m_images.Clear();
858 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000859 m_scratch_ast_source_ap.reset();
860 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000861 // Need to do something about unsetting breakpoints.
862
863 if (executable_sp)
864 {
865 FileSpec exec_file_spec = executable_sp->GetFileSpec();
866 Error error = ModuleList::GetSharedModule(exec_file_spec,
867 arch_spec,
868 NULL,
869 NULL,
870 0,
871 executable_sp,
Greg Clayton9ce95382012-02-13 23:10:39 +0000872 &GetExecutableSearchPaths(),
Jim Ingham7508e732010-08-09 23:31:02 +0000873 NULL,
874 NULL);
875
876 if (!error.Fail() && executable_sp)
877 {
878 SetExecutableModule (executable_sp, true);
879 return true;
880 }
881 else
882 {
883 return false;
884 }
885 }
886 else
887 {
888 return false;
889 }
890 }
891}
Chris Lattner24943d22010-06-08 16:52:24 +0000892
Chris Lattner24943d22010-06-08 16:52:24 +0000893void
894Target::ModuleAdded (ModuleSP &module_sp)
895{
896 // A module is being added to this target for the first time
897 ModuleList module_list;
898 module_list.Append(module_sp);
899 ModulesDidLoad (module_list);
900}
901
902void
903Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
904{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000905 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000906 ModuleList module_list;
907 module_list.Append (old_module_sp);
908 ModulesDidUnload (module_list);
909 module_list.Clear ();
910 module_list.Append (new_module_sp);
911 ModulesDidLoad (module_list);
912}
913
914void
915Target::ModulesDidLoad (ModuleList &module_list)
916{
917 m_breakpoint_list.UpdateBreakpoints (module_list, true);
918 // TODO: make event data that packages up the module_list
919 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
920}
921
922void
923Target::ModulesDidUnload (ModuleList &module_list)
924{
925 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000926
927 // Remove the images from the target image list
928 m_images.Remove(module_list);
929
Chris Lattner24943d22010-06-08 16:52:24 +0000930 // TODO: make event data that packages up the module_list
931 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
932}
933
Jim Ingham7089d8a2011-10-28 23:14:11 +0000934
Daniel Dunbar705a0982011-10-31 22:50:37 +0000935bool
Jim Ingham7089d8a2011-10-28 23:14:11 +0000936Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_spec)
937{
938
939 if (!m_breakpoints_use_platform_avoid)
940 return false;
941 else
942 {
943 ModuleList matchingModules;
944 const ArchSpec *arch_ptr = NULL;
945 const lldb_private::UUID *uuid_ptr= NULL;
946 const ConstString *object_name = NULL;
947 size_t num_modules = GetImages().FindModules(&module_spec, arch_ptr, uuid_ptr, object_name, matchingModules);
948
949 // If there is more than one module for this file spec, only return true if ALL the modules are on the
950 // black list.
951 if (num_modules > 0)
952 {
953 for (int i = 0; i < num_modules; i++)
954 {
955 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
956 return false;
957 }
958 return true;
959 }
960 else
961 return false;
962 }
963}
964
Daniel Dunbar705a0982011-10-31 22:50:37 +0000965bool
Jim Ingham7089d8a2011-10-28 23:14:11 +0000966Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
967{
968 if (!m_breakpoints_use_platform_avoid)
969 return false;
970 else if (GetPlatform())
971 {
972 return GetPlatform()->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
973 }
974 else
975 return false;
976}
977
Chris Lattner24943d22010-06-08 16:52:24 +0000978size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000979Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
980{
981 const Section *section = addr.GetSection();
982 if (section && section->GetModule())
983 {
984 ObjectFile *objfile = section->GetModule()->GetObjectFile();
985 if (objfile)
986 {
Greg Claytonb5a8f142012-02-05 02:38:54 +0000987 size_t bytes_read = objfile->ReadSectionData (section,
988 addr.GetOffset(),
989 dst,
990 dst_len);
Greg Clayton26100dc2011-01-07 01:57:07 +0000991 if (bytes_read > 0)
992 return bytes_read;
993 else
994 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
995 }
996 else
997 {
998 error.SetErrorString("address isn't from a object file");
999 }
1000 }
1001 else
1002 {
1003 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
1004 }
1005 return 0;
1006}
1007
1008size_t
Enrico Granata91544802011-09-06 19:20:51 +00001009Target::ReadMemory (const Address& addr,
1010 bool prefer_file_cache,
1011 void *dst,
1012 size_t dst_len,
1013 Error &error,
1014 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001015{
Chris Lattner24943d22010-06-08 16:52:24 +00001016 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001017
Enrico Granata91544802011-09-06 19:20:51 +00001018 // if we end up reading this from process memory, we will fill this
1019 // with the actual load address
1020 if (load_addr_ptr)
1021 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1022
Greg Clayton26100dc2011-01-07 01:57:07 +00001023 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001024
1025 addr_t load_addr = LLDB_INVALID_ADDRESS;
1026 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001027 Address resolved_addr;
1028 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001029 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001030 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001031 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001032 // No sections are loaded, so we must assume we are not running
1033 // yet and anything we are given is a file address.
1034 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1035 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001036 }
Greg Clayton70436352010-06-30 23:03:03 +00001037 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001038 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001039 // We have at least one section loaded. This can be becuase
1040 // we have manually loaded some sections with "target modules load ..."
1041 // or because we have have a live process that has sections loaded
1042 // through the dynamic loader
1043 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1044 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001045 }
Greg Clayton70436352010-06-30 23:03:03 +00001046 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001047 if (!resolved_addr.IsValid())
1048 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001049
Greg Clayton9b82f862011-07-11 05:12:02 +00001050
Greg Clayton26100dc2011-01-07 01:57:07 +00001051 if (prefer_file_cache)
1052 {
1053 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1054 if (bytes_read > 0)
1055 return bytes_read;
1056 }
Greg Clayton70436352010-06-30 23:03:03 +00001057
Johnny Chenda5a8022011-09-20 23:28:55 +00001058 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001059 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001060 if (load_addr == LLDB_INVALID_ADDRESS)
1061 load_addr = resolved_addr.GetLoadAddress (this);
1062
Greg Clayton70436352010-06-30 23:03:03 +00001063 if (load_addr == LLDB_INVALID_ADDRESS)
1064 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001065 Module *addr_module = resolved_addr.GetModulePtr();
1066 if (addr_module && addr_module->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001067 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton13d24fb2012-01-29 20:56:30 +00001068 addr_module->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001069 resolved_addr.GetFileAddress(),
Greg Clayton13d24fb2012-01-29 20:56:30 +00001070 addr_module->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001071 else
Greg Clayton9c236732011-10-26 00:56:27 +00001072 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001073 }
1074 else
1075 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001076 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001077 if (bytes_read != dst_len)
1078 {
1079 if (error.Success())
1080 {
1081 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001082 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001083 else
Greg Clayton9c236732011-10-26 00:56:27 +00001084 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 +00001085 }
1086 }
Greg Clayton70436352010-06-30 23:03:03 +00001087 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001088 {
1089 if (load_addr_ptr)
1090 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001091 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001092 }
Greg Clayton70436352010-06-30 23:03:03 +00001093 // If the address is not section offset we have an address that
1094 // doesn't resolve to any address in any currently loaded shared
1095 // libaries and we failed to read memory so there isn't anything
1096 // more we can do. If it is section offset, we might be able to
1097 // read cached memory from the object file.
1098 if (!resolved_addr.IsSectionOffset())
1099 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001100 }
Chris Lattner24943d22010-06-08 16:52:24 +00001101 }
Greg Clayton70436352010-06-30 23:03:03 +00001102
Greg Clayton9b82f862011-07-11 05:12:02 +00001103 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001104 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001105 // If we didn't already try and read from the object file cache, then
1106 // try it after failing to read from the process.
1107 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001108 }
1109 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001110}
1111
Greg Clayton7dd98df2011-07-12 17:06:17 +00001112size_t
1113Target::ReadScalarIntegerFromMemory (const Address& addr,
1114 bool prefer_file_cache,
1115 uint32_t byte_size,
1116 bool is_signed,
1117 Scalar &scalar,
1118 Error &error)
1119{
1120 uint64_t uval;
1121
1122 if (byte_size <= sizeof(uval))
1123 {
1124 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1125 if (bytes_read == byte_size)
1126 {
1127 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1128 uint32_t offset = 0;
1129 if (byte_size <= 4)
1130 scalar = data.GetMaxU32 (&offset, byte_size);
1131 else
1132 scalar = data.GetMaxU64 (&offset, byte_size);
1133
1134 if (is_signed)
1135 scalar.SignExtend(byte_size * 8);
1136 return bytes_read;
1137 }
1138 }
1139 else
1140 {
1141 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1142 }
1143 return 0;
1144}
1145
1146uint64_t
1147Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1148 bool prefer_file_cache,
1149 size_t integer_byte_size,
1150 uint64_t fail_value,
1151 Error &error)
1152{
1153 Scalar scalar;
1154 if (ReadScalarIntegerFromMemory (addr,
1155 prefer_file_cache,
1156 integer_byte_size,
1157 false,
1158 scalar,
1159 error))
1160 return scalar.ULongLong(fail_value);
1161 return fail_value;
1162}
1163
1164bool
1165Target::ReadPointerFromMemory (const Address& addr,
1166 bool prefer_file_cache,
1167 Error &error,
1168 Address &pointer_addr)
1169{
1170 Scalar scalar;
1171 if (ReadScalarIntegerFromMemory (addr,
1172 prefer_file_cache,
1173 m_arch.GetAddressByteSize(),
1174 false,
1175 scalar,
1176 error))
1177 {
1178 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1179 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1180 {
1181 if (m_section_load_list.IsEmpty())
1182 {
1183 // No sections are loaded, so we must assume we are not running
1184 // yet and anything we are given is a file address.
1185 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1186 }
1187 else
1188 {
1189 // We have at least one section loaded. This can be becuase
1190 // we have manually loaded some sections with "target modules load ..."
1191 // or because we have have a live process that has sections loaded
1192 // through the dynamic loader
1193 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1194 }
1195 // We weren't able to resolve the pointer value, so just return
1196 // an address with no section
1197 if (!pointer_addr.IsValid())
1198 pointer_addr.SetOffset (pointer_vm_addr);
1199 return true;
1200
1201 }
1202 }
1203 return false;
1204}
Chris Lattner24943d22010-06-08 16:52:24 +00001205
1206ModuleSP
1207Target::GetSharedModule
1208(
1209 const FileSpec& file_spec,
1210 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +00001211 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +00001212 const ConstString *object_name,
1213 off_t object_offset,
1214 Error *error_ptr
1215)
1216{
1217 // Don't pass in the UUID so we can tell if we have a stale value in our list
1218 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1219 bool did_create_module = false;
1220 ModuleSP module_sp;
1221
Chris Lattner24943d22010-06-08 16:52:24 +00001222 Error error;
1223
Greg Clayton24bc5d92011-03-30 18:16:51 +00001224 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +00001225 if (m_image_search_paths.GetSize())
1226 {
1227 FileSpec transformed_spec;
1228 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1229 {
1230 transformed_spec.GetFilename() = file_spec.GetFilename();
Greg Clayton9ce95382012-02-13 23:10:39 +00001231 error = ModuleList::GetSharedModule (transformed_spec,
1232 arch,
1233 uuid_ptr,
1234 object_name,
1235 object_offset,
1236 module_sp,
1237 &GetExecutableSearchPaths(),
1238 &old_module_sp,
1239 &did_create_module);
Chris Lattner24943d22010-06-08 16:52:24 +00001240 }
1241 }
1242
Greg Clayton24bc5d92011-03-30 18:16:51 +00001243 // The platform is responsible for finding and caching an appropriate
1244 // module in the shared module cache.
1245 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001246 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001247 FileSpec platform_file_spec;
1248 error = m_platform_sp->GetSharedModule (file_spec,
1249 arch,
1250 uuid_ptr,
1251 object_name,
1252 object_offset,
1253 module_sp,
Greg Clayton9ce95382012-02-13 23:10:39 +00001254 &GetExecutableSearchPaths(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001255 &old_module_sp,
1256 &did_create_module);
1257 }
1258 else
1259 {
1260 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +00001261 }
1262
Greg Clayton24bc5d92011-03-30 18:16:51 +00001263 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +00001264 if (module_sp)
1265 {
1266 m_images.Append (module_sp);
1267 if (did_create_module)
1268 {
1269 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1270 ModuleUpdated(old_module_sp, module_sp);
1271 else
1272 ModuleAdded(module_sp);
1273 }
1274 }
1275 if (error_ptr)
1276 *error_ptr = error;
1277 return module_sp;
1278}
1279
1280
1281Target *
1282Target::CalculateTarget ()
1283{
1284 return this;
1285}
1286
1287Process *
1288Target::CalculateProcess ()
1289{
1290 return NULL;
1291}
1292
1293Thread *
1294Target::CalculateThread ()
1295{
1296 return NULL;
1297}
1298
1299StackFrame *
1300Target::CalculateStackFrame ()
1301{
1302 return NULL;
1303}
1304
1305void
Greg Claytona830adb2010-10-04 01:05:56 +00001306Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001307{
Greg Clayton567e7f32011-09-22 04:58:26 +00001308 exe_ctx.Clear();
1309 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001310}
1311
1312PathMappingList &
1313Target::GetImageSearchPathList ()
1314{
1315 return m_image_search_paths;
1316}
1317
1318void
1319Target::ImageSearchPathsChanged
1320(
1321 const PathMappingList &path_list,
1322 void *baton
1323)
1324{
1325 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001326 ModuleSP exe_module_sp (target->GetExecutableModule());
1327 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001328 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001329 target->m_images.Clear();
1330 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001331 }
1332}
1333
1334ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001335Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001336{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001337 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001338 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001339 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001340 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Clayton13d24fb2012-01-29 20:56:30 +00001341 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001342 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1343 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1344 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1345 }
Chris Lattner24943d22010-06-08 16:52:24 +00001346 return m_scratch_ast_context_ap.get();
1347}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001348
Sean Callanan4938bd62011-11-16 18:20:47 +00001349ClangASTImporter *
1350Target::GetClangASTImporter()
1351{
1352 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1353
1354 if (!ast_importer)
1355 {
1356 ast_importer = new ClangASTImporter();
1357 m_ast_importer_ap.reset(ast_importer);
1358 }
1359
1360 return ast_importer;
1361}
1362
Greg Clayton990de7b2010-11-18 23:32:35 +00001363void
Caroline Tice2a456812011-03-10 22:14:10 +00001364Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001365{
Greg Clayton334d33a2012-01-30 07:41:31 +00001366 UserSettingsController::InitializeSettingsController (GetSettingsController(),
Greg Clayton990de7b2010-11-18 23:32:35 +00001367 SettingsController::global_settings_table,
1368 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001369
1370 // Now call SettingsInitialize() on each 'child' setting of Target
1371 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001372}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001373
Greg Clayton990de7b2010-11-18 23:32:35 +00001374void
Caroline Tice2a456812011-03-10 22:14:10 +00001375Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001376{
Caroline Tice2a456812011-03-10 22:14:10 +00001377
1378 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1379
1380 Process::SettingsTerminate ();
1381
1382 // Now terminate Target Settings.
1383
Greg Clayton990de7b2010-11-18 23:32:35 +00001384 UserSettingsControllerSP &usc = GetSettingsController();
1385 UserSettingsController::FinalizeSettingsController (usc);
1386 usc.reset();
1387}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001388
Greg Clayton990de7b2010-11-18 23:32:35 +00001389UserSettingsControllerSP &
1390Target::GetSettingsController ()
1391{
Greg Clayton334d33a2012-01-30 07:41:31 +00001392 static UserSettingsControllerSP g_settings_controller_sp;
1393 if (!g_settings_controller_sp)
1394 {
1395 g_settings_controller_sp.reset (new Target::SettingsController);
1396 // The first shared pointer to Target::SettingsController in
1397 // g_settings_controller_sp must be fully created above so that
1398 // the TargetInstanceSettings can use a weak_ptr to refer back
1399 // to the master setttings controller
1400 InstanceSettingsSP default_instance_settings_sp (new TargetInstanceSettings (g_settings_controller_sp,
1401 false,
1402 InstanceSettings::GetDefaultName().AsCString()));
1403 g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp);
1404 }
1405 return g_settings_controller_sp;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001406}
1407
Greg Clayton9ce95382012-02-13 23:10:39 +00001408FileSpecList
1409Target::GetDefaultExecutableSearchPaths ()
1410{
1411 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1412 if (settings_controller_sp)
1413 {
1414 lldb::InstanceSettingsSP instance_settings_sp (settings_controller_sp->GetDefaultInstanceSettings ());
1415 if (instance_settings_sp)
1416 return static_cast<TargetInstanceSettings *>(instance_settings_sp.get())->GetExecutableSearchPaths ();
1417 }
1418 return FileSpecList();
1419}
1420
1421
Caroline Tice5bc8c972010-09-20 20:44:43 +00001422ArchSpec
1423Target::GetDefaultArchitecture ()
1424{
Greg Clayton940b1032011-02-23 00:35:02 +00001425 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1426
1427 if (settings_controller_sp)
1428 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1429 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001430}
1431
1432void
Greg Clayton940b1032011-02-23 00:35:02 +00001433Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001434{
Greg Clayton940b1032011-02-23 00:35:02 +00001435 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1436
1437 if (settings_controller_sp)
1438 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001439}
1440
Greg Claytona830adb2010-10-04 01:05:56 +00001441Target *
1442Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1443{
1444 // The target can either exist in the "process" of ExecutionContext, or in
1445 // the "target_sp" member of SymbolContext. This accessor helper function
1446 // will get the target from one of these locations.
1447
1448 Target *target = NULL;
1449 if (sc_ptr != NULL)
1450 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001451 if (target == NULL && exe_ctx_ptr)
1452 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001453 return target;
1454}
1455
1456
Caroline Tice1ebef442010-09-27 00:30:10 +00001457void
1458Target::UpdateInstanceName ()
1459{
1460 StreamString sstr;
1461
Greg Clayton5beb99d2011-08-11 02:48:45 +00001462 Module *exe_module = GetExecutableModulePointer();
1463 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001464 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001465 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001466 exe_module->GetFileSpec().GetFilename().AsCString(),
1467 exe_module->GetArchitecture().GetArchitectureName());
1468 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001469 }
1470}
1471
Sean Callanan77e93942010-10-29 00:29:03 +00001472const char *
1473Target::GetExpressionPrefixContentsAsCString ()
1474{
Sean Callanane0b7f942011-11-16 01:54:57 +00001475 if (!m_expr_prefix_contents.empty())
1476 return m_expr_prefix_contents.c_str();
Greg Claytonff44ab42011-04-23 02:04:55 +00001477 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001478}
1479
Greg Clayton427f2902010-12-14 02:59:59 +00001480ExecutionResults
1481Target::EvaluateExpression
1482(
1483 const char *expr_cstr,
1484 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001485 lldb_private::ExecutionPolicy execution_policy,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001486 bool coerce_to_id,
Greg Clayton427f2902010-12-14 02:59:59 +00001487 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001488 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001489 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001490 lldb::ValueObjectSP &result_valobj_sp
1491)
1492{
1493 ExecutionResults execution_results = eExecutionSetupError;
1494
1495 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001496
1497 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1498 return execution_results;
1499
Jim Ingham3613ae12011-05-12 02:06:14 +00001500 // We shouldn't run stop hooks in expressions.
1501 // Be sure to reset this if you return anywhere within this function.
1502 bool old_suppress_value = m_suppress_stop_hooks;
1503 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001504
1505 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001506
1507 const size_t expr_cstr_len = ::strlen (expr_cstr);
1508
Greg Clayton427f2902010-12-14 02:59:59 +00001509 if (frame)
1510 {
1511 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001512 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001513 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001514 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1515 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001516 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001517
1518 // Make sure we don't have any things that we know a variable expression
1519 // won't be able to deal with before calling into it
1520 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1521 {
1522 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1523 use_dynamic,
1524 expr_path_options,
1525 var_sp,
1526 error);
1527 }
Greg Clayton427f2902010-12-14 02:59:59 +00001528 }
1529 else if (m_process_sp)
1530 {
1531 m_process_sp->CalculateExecutionContext(exe_ctx);
1532 }
1533 else
1534 {
1535 CalculateExecutionContext(exe_ctx);
1536 }
1537
1538 if (result_valobj_sp)
1539 {
1540 execution_results = eExecutionCompleted;
1541 // We got a result from the frame variable expression path above...
1542 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1543
1544 lldb::ValueObjectSP const_valobj_sp;
1545
1546 // Check in case our value is already a constant value
1547 if (result_valobj_sp->GetIsConstant())
1548 {
1549 const_valobj_sp = result_valobj_sp;
1550 const_valobj_sp->SetName (persistent_variable_name);
1551 }
1552 else
Jim Inghame41494a2011-04-16 00:01:13 +00001553 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001554 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001555 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001556 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001557 if (dynamic_sp)
1558 result_valobj_sp = dynamic_sp;
1559 }
1560
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001561 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001562 }
Greg Clayton427f2902010-12-14 02:59:59 +00001563
Sean Callanan6a925532011-01-13 08:53:35 +00001564 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1565
Greg Clayton427f2902010-12-14 02:59:59 +00001566 result_valobj_sp = const_valobj_sp;
1567
Sean Callanan6a925532011-01-13 08:53:35 +00001568 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1569 assert (clang_expr_variable_sp.get());
1570
1571 // Set flags and live data as appropriate
1572
1573 const Value &result_value = live_valobj_sp->GetValue();
1574
1575 switch (result_value.GetValueType())
1576 {
1577 case Value::eValueTypeHostAddress:
1578 case Value::eValueTypeFileAddress:
1579 // we don't do anything with these for now
1580 break;
1581 case Value::eValueTypeScalar:
1582 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1583 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1584 break;
1585 case Value::eValueTypeLoadAddress:
1586 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1587 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1588 break;
1589 }
Greg Clayton427f2902010-12-14 02:59:59 +00001590 }
1591 else
1592 {
1593 // Make sure we aren't just trying to see the value of a persistent
1594 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001595 lldb::ClangExpressionVariableSP persistent_var_sp;
1596 // Only check for persistent variables the expression starts with a '$'
1597 if (expr_cstr[0] == '$')
1598 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1599
Greg Clayton427f2902010-12-14 02:59:59 +00001600 if (persistent_var_sp)
1601 {
1602 result_valobj_sp = persistent_var_sp->GetValueObject ();
1603 execution_results = eExecutionCompleted;
1604 }
1605 else
1606 {
1607 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001608
Greg Clayton427f2902010-12-14 02:59:59 +00001609 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001610 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001611 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001612 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan6a925532011-01-13 08:53:35 +00001613 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001614 expr_cstr,
1615 prefix,
1616 result_valobj_sp);
1617 }
1618 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001619
1620 m_suppress_stop_hooks = old_suppress_value;
1621
Greg Clayton427f2902010-12-14 02:59:59 +00001622 return execution_results;
1623}
1624
Greg Claytonc0fa5332011-05-22 22:46:53 +00001625lldb::addr_t
1626Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1627{
1628 addr_t code_addr = load_addr;
1629 switch (m_arch.GetMachine())
1630 {
1631 case llvm::Triple::arm:
1632 case llvm::Triple::thumb:
1633 switch (addr_class)
1634 {
1635 case eAddressClassData:
1636 case eAddressClassDebug:
1637 return LLDB_INVALID_ADDRESS;
1638
1639 case eAddressClassUnknown:
1640 case eAddressClassInvalid:
1641 case eAddressClassCode:
1642 case eAddressClassCodeAlternateISA:
1643 case eAddressClassRuntime:
1644 // Check if bit zero it no set?
1645 if ((code_addr & 1ull) == 0)
1646 {
1647 // Bit zero isn't set, check if the address is a multiple of 2?
1648 if (code_addr & 2ull)
1649 {
1650 // The address is a multiple of 2 so it must be thumb, set bit zero
1651 code_addr |= 1ull;
1652 }
1653 else if (addr_class == eAddressClassCodeAlternateISA)
1654 {
1655 // We checked the address and the address claims to be the alternate ISA
1656 // which means thumb, so set bit zero.
1657 code_addr |= 1ull;
1658 }
1659 }
1660 break;
1661 }
1662 break;
1663
1664 default:
1665 break;
1666 }
1667 return code_addr;
1668}
1669
1670lldb::addr_t
1671Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1672{
1673 addr_t opcode_addr = load_addr;
1674 switch (m_arch.GetMachine())
1675 {
1676 case llvm::Triple::arm:
1677 case llvm::Triple::thumb:
1678 switch (addr_class)
1679 {
1680 case eAddressClassData:
1681 case eAddressClassDebug:
1682 return LLDB_INVALID_ADDRESS;
1683
1684 case eAddressClassInvalid:
1685 case eAddressClassUnknown:
1686 case eAddressClassCode:
1687 case eAddressClassCodeAlternateISA:
1688 case eAddressClassRuntime:
1689 opcode_addr &= ~(1ull);
1690 break;
1691 }
1692 break;
1693
1694 default:
1695 break;
1696 }
1697 return opcode_addr;
1698}
1699
Jim Inghamd60d94a2011-03-11 03:53:59 +00001700lldb::user_id_t
1701Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1702{
1703 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001704 new_hook_sp.reset (new StopHook(shared_from_this(), new_uid));
Jim Inghamd60d94a2011-03-11 03:53:59 +00001705 m_stop_hooks[new_uid] = new_hook_sp;
1706 return new_uid;
1707}
1708
1709bool
1710Target::RemoveStopHookByID (lldb::user_id_t user_id)
1711{
1712 size_t num_removed;
1713 num_removed = m_stop_hooks.erase (user_id);
1714 if (num_removed == 0)
1715 return false;
1716 else
1717 return true;
1718}
1719
1720void
1721Target::RemoveAllStopHooks ()
1722{
1723 m_stop_hooks.clear();
1724}
1725
1726Target::StopHookSP
1727Target::GetStopHookByID (lldb::user_id_t user_id)
1728{
1729 StopHookSP found_hook;
1730
1731 StopHookCollection::iterator specified_hook_iter;
1732 specified_hook_iter = m_stop_hooks.find (user_id);
1733 if (specified_hook_iter != m_stop_hooks.end())
1734 found_hook = (*specified_hook_iter).second;
1735 return found_hook;
1736}
1737
1738bool
1739Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1740{
1741 StopHookCollection::iterator specified_hook_iter;
1742 specified_hook_iter = m_stop_hooks.find (user_id);
1743 if (specified_hook_iter == m_stop_hooks.end())
1744 return false;
1745
1746 (*specified_hook_iter).second->SetIsActive (active_state);
1747 return true;
1748}
1749
1750void
1751Target::SetAllStopHooksActiveState (bool active_state)
1752{
1753 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1754 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1755 {
1756 (*pos).second->SetIsActive (active_state);
1757 }
1758}
1759
1760void
1761Target::RunStopHooks ()
1762{
Jim Ingham3613ae12011-05-12 02:06:14 +00001763 if (m_suppress_stop_hooks)
1764 return;
1765
Jim Inghamd60d94a2011-03-11 03:53:59 +00001766 if (!m_process_sp)
1767 return;
1768
1769 if (m_stop_hooks.empty())
1770 return;
1771
1772 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1773
1774 // If there aren't any active stop hooks, don't bother either:
1775 bool any_active_hooks = false;
1776 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1777 {
1778 if ((*pos).second->IsActive())
1779 {
1780 any_active_hooks = true;
1781 break;
1782 }
1783 }
1784 if (!any_active_hooks)
1785 return;
1786
1787 CommandReturnObject result;
1788
1789 std::vector<ExecutionContext> exc_ctx_with_reasons;
1790 std::vector<SymbolContext> sym_ctx_with_reasons;
1791
1792 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1793 size_t num_threads = cur_threadlist.GetSize();
1794 for (size_t i = 0; i < num_threads; i++)
1795 {
1796 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1797 if (cur_thread_sp->ThreadStoppedForAReason())
1798 {
1799 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1800 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1801 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1802 }
1803 }
1804
1805 // If no threads stopped for a reason, don't run the stop-hooks.
1806 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1807 if (num_exe_ctx == 0)
1808 return;
1809
Jim Inghame5ed8e92011-06-02 23:58:26 +00001810 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1811 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001812
1813 bool keep_going = true;
1814 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001815 bool print_hook_header;
1816 bool print_thread_header;
1817
1818 if (num_exe_ctx == 1)
1819 print_thread_header = false;
1820 else
1821 print_thread_header = true;
1822
1823 if (m_stop_hooks.size() == 1)
1824 print_hook_header = false;
1825 else
1826 print_hook_header = true;
1827
Jim Inghamd60d94a2011-03-11 03:53:59 +00001828 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1829 {
1830 // result.Clear();
1831 StopHookSP cur_hook_sp = (*pos).second;
1832 if (!cur_hook_sp->IsActive())
1833 continue;
1834
1835 bool any_thread_matched = false;
1836 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1837 {
1838 if ((cur_hook_sp->GetSpecifier () == NULL
1839 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1840 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Clayton567e7f32011-09-22 04:58:26 +00001841 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001842 {
1843 if (!hooks_ran)
1844 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001845 hooks_ran = true;
1846 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001847 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001848 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001849 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1850 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1851 NULL);
1852 if (cmd)
1853 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1854 else
1855 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001856 any_thread_matched = true;
1857 }
1858
Jim Inghamc54840c2011-03-22 01:47:27 +00001859 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001860 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001861
1862 bool stop_on_continue = true;
1863 bool stop_on_error = true;
1864 bool echo_commands = false;
1865 bool print_results = true;
1866 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001867 &exc_ctx_with_reasons[i],
1868 stop_on_continue,
1869 stop_on_error,
1870 echo_commands,
1871 print_results,
1872 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001873
1874 // If the command started the target going again, we should bag out of
1875 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001876 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1877 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001878 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001879 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001880 keep_going = false;
1881 }
1882 }
1883 }
1884 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00001885
Caroline Tice4a348082011-05-02 20:41:46 +00001886 result.GetImmediateOutputStream()->Flush();
1887 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001888}
1889
Greg Claytonbbea1332011-07-08 00:48:09 +00001890bool
1891Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1892{
1893 bool changed = false;
1894 if (module)
1895 {
1896 ObjectFile *object_file = module->GetObjectFile();
1897 if (object_file)
1898 {
1899 SectionList *section_list = object_file->GetSectionList ();
1900 if (section_list)
1901 {
1902 // All sections listed in the dyld image info structure will all
1903 // either be fixed up already, or they will all be off by a single
1904 // slide amount that is determined by finding the first segment
1905 // that is at file offset zero which also has bytes (a file size
1906 // that is greater than zero) in the object file.
1907
1908 // Determine the slide amount (if any)
1909 const size_t num_sections = section_list->GetSize();
1910 size_t sect_idx = 0;
1911 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1912 {
1913 // Iterate through the object file sections to find the
1914 // first section that starts of file offset zero and that
1915 // has bytes in the file...
1916 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1917 if (section)
1918 {
1919 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1920 changed = true;
1921 }
1922 }
1923 }
1924 }
1925 }
1926 return changed;
1927}
1928
1929
Jim Inghamd60d94a2011-03-11 03:53:59 +00001930//--------------------------------------------------------------
1931// class Target::StopHook
1932//--------------------------------------------------------------
1933
1934
1935Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1936 UserID (uid),
1937 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001938 m_commands (),
1939 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001940 m_thread_spec_ap(NULL),
1941 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001942{
1943}
1944
1945Target::StopHook::StopHook (const StopHook &rhs) :
1946 UserID (rhs.GetID()),
1947 m_target_sp (rhs.m_target_sp),
1948 m_commands (rhs.m_commands),
1949 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001950 m_thread_spec_ap (NULL),
1951 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001952{
1953 if (rhs.m_thread_spec_ap.get() != NULL)
1954 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1955}
1956
1957
1958Target::StopHook::~StopHook ()
1959{
1960}
1961
1962void
1963Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1964{
1965 m_thread_spec_ap.reset (specifier);
1966}
1967
1968
1969void
1970Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1971{
1972 int indent_level = s->GetIndentLevel();
1973
1974 s->SetIndentLevel(indent_level + 2);
1975
Greg Clayton444e35b2011-10-19 18:09:39 +00001976 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001977 if (m_active)
1978 s->Indent ("State: enabled\n");
1979 else
1980 s->Indent ("State: disabled\n");
1981
1982 if (m_specifier_sp)
1983 {
1984 s->Indent();
1985 s->PutCString ("Specifier:\n");
1986 s->SetIndentLevel (indent_level + 4);
1987 m_specifier_sp->GetDescription (s, level);
1988 s->SetIndentLevel (indent_level + 2);
1989 }
1990
1991 if (m_thread_spec_ap.get() != NULL)
1992 {
1993 StreamString tmp;
1994 s->Indent("Thread:\n");
1995 m_thread_spec_ap->GetDescription (&tmp, level);
1996 s->SetIndentLevel (indent_level + 4);
1997 s->Indent (tmp.GetData());
1998 s->PutCString ("\n");
1999 s->SetIndentLevel (indent_level + 2);
2000 }
2001
2002 s->Indent ("Commands: \n");
2003 s->SetIndentLevel (indent_level + 4);
2004 uint32_t num_commands = m_commands.GetSize();
2005 for (uint32_t i = 0; i < num_commands; i++)
2006 {
2007 s->Indent(m_commands.GetStringAtIndex(i));
2008 s->PutCString ("\n");
2009 }
2010 s->SetIndentLevel (indent_level);
2011}
2012
2013
Caroline Tice5bc8c972010-09-20 20:44:43 +00002014//--------------------------------------------------------------
2015// class Target::SettingsController
2016//--------------------------------------------------------------
2017
2018Target::SettingsController::SettingsController () :
2019 UserSettingsController ("target", Debugger::GetSettingsController()),
2020 m_default_architecture ()
2021{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002022}
2023
2024Target::SettingsController::~SettingsController ()
2025{
2026}
2027
2028lldb::InstanceSettingsSP
2029Target::SettingsController::CreateInstanceSettings (const char *instance_name)
2030{
Greg Clayton334d33a2012-01-30 07:41:31 +00002031 lldb::InstanceSettingsSP new_settings_sp (new TargetInstanceSettings (GetSettingsController(),
2032 false,
2033 instance_name));
Caroline Tice5bc8c972010-09-20 20:44:43 +00002034 return new_settings_sp;
2035}
2036
Caroline Tice5bc8c972010-09-20 20:44:43 +00002037
Greg Claytonabb33022011-11-08 02:43:13 +00002038#define TSC_DEFAULT_ARCH "default-arch"
2039#define TSC_EXPR_PREFIX "expr-prefix"
2040#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
2041#define TSC_SKIP_PROLOGUE "skip-prologue"
2042#define TSC_SOURCE_MAP "source-map"
Greg Clayton9ce95382012-02-13 23:10:39 +00002043#define TSC_EXE_SEARCH_PATHS "exec-search-paths"
Greg Claytonabb33022011-11-08 02:43:13 +00002044#define TSC_MAX_CHILDREN "max-children-count"
2045#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
2046#define TSC_PLATFORM_AVOID "breakpoints-use-platform-avoid-list"
2047#define TSC_RUN_ARGS "run-args"
2048#define TSC_ENV_VARS "env-vars"
2049#define TSC_INHERIT_ENV "inherit-env"
2050#define TSC_STDIN_PATH "input-path"
2051#define TSC_STDOUT_PATH "output-path"
2052#define TSC_STDERR_PATH "error-path"
2053#define TSC_DISABLE_ASLR "disable-aslr"
2054#define TSC_DISABLE_STDIO "disable-stdio"
Greg Claytond284b662011-02-18 01:44:25 +00002055
2056
2057static const ConstString &
2058GetSettingNameForDefaultArch ()
2059{
2060 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00002061 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002062}
2063
Greg Claytond284b662011-02-18 01:44:25 +00002064static const ConstString &
2065GetSettingNameForExpressionPrefix ()
2066{
2067 static ConstString g_const_string (TSC_EXPR_PREFIX);
2068 return g_const_string;
2069}
2070
2071static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00002072GetSettingNameForPreferDynamicValue ()
2073{
2074 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
2075 return g_const_string;
2076}
2077
Greg Claytonff44ab42011-04-23 02:04:55 +00002078static const ConstString &
2079GetSettingNameForSourcePathMap ()
2080{
2081 static ConstString g_const_string (TSC_SOURCE_MAP);
2082 return g_const_string;
2083}
Greg Claytond284b662011-02-18 01:44:25 +00002084
Greg Clayton17cd9952011-04-22 03:55:06 +00002085static const ConstString &
Greg Clayton9ce95382012-02-13 23:10:39 +00002086GetSettingNameForExecutableSearchPaths ()
2087{
2088 static ConstString g_const_string (TSC_EXE_SEARCH_PATHS);
2089 return g_const_string;
2090}
2091
2092static const ConstString &
Greg Clayton17cd9952011-04-22 03:55:06 +00002093GetSettingNameForSkipPrologue ()
2094{
2095 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
2096 return g_const_string;
2097}
2098
Enrico Granata018921d2011-08-12 02:00:06 +00002099static const ConstString &
2100GetSettingNameForMaxChildren ()
2101{
2102 static ConstString g_const_string (TSC_MAX_CHILDREN);
2103 return g_const_string;
2104}
Greg Clayton17cd9952011-04-22 03:55:06 +00002105
Enrico Granata91544802011-09-06 19:20:51 +00002106static const ConstString &
2107GetSettingNameForMaxStringSummaryLength ()
2108{
2109 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2110 return g_const_string;
2111}
Greg Clayton17cd9952011-04-22 03:55:06 +00002112
Jim Ingham7089d8a2011-10-28 23:14:11 +00002113static const ConstString &
2114GetSettingNameForPlatformAvoid ()
2115{
2116 static ConstString g_const_string (TSC_PLATFORM_AVOID);
2117 return g_const_string;
2118}
2119
Greg Claytonabb33022011-11-08 02:43:13 +00002120const ConstString &
2121GetSettingNameForRunArgs ()
2122{
2123 static ConstString g_const_string (TSC_RUN_ARGS);
2124 return g_const_string;
2125}
2126
2127const ConstString &
2128GetSettingNameForEnvVars ()
2129{
2130 static ConstString g_const_string (TSC_ENV_VARS);
2131 return g_const_string;
2132}
2133
2134const ConstString &
2135GetSettingNameForInheritHostEnv ()
2136{
2137 static ConstString g_const_string (TSC_INHERIT_ENV);
2138 return g_const_string;
2139}
2140
2141const ConstString &
2142GetSettingNameForInputPath ()
2143{
2144 static ConstString g_const_string (TSC_STDIN_PATH);
2145 return g_const_string;
2146}
2147
2148const ConstString &
2149GetSettingNameForOutputPath ()
2150{
2151 static ConstString g_const_string (TSC_STDOUT_PATH);
2152 return g_const_string;
2153}
2154
2155const ConstString &
2156GetSettingNameForErrorPath ()
2157{
2158 static ConstString g_const_string (TSC_STDERR_PATH);
2159 return g_const_string;
2160}
2161
2162const ConstString &
2163GetSettingNameForDisableASLR ()
2164{
2165 static ConstString g_const_string (TSC_DISABLE_ASLR);
2166 return g_const_string;
2167}
2168
2169const ConstString &
2170GetSettingNameForDisableSTDIO ()
2171{
2172 static ConstString g_const_string (TSC_DISABLE_STDIO);
2173 return g_const_string;
2174}
Jim Ingham7089d8a2011-10-28 23:14:11 +00002175
Caroline Tice5bc8c972010-09-20 20:44:43 +00002176bool
2177Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2178 const char *index_value,
2179 const char *value,
2180 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002181 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002182 Error&err)
2183{
Greg Claytond284b662011-02-18 01:44:25 +00002184 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002185 {
Greg Claytonf15996e2011-04-07 22:46:35 +00002186 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00002187 if (!m_default_architecture.IsValid())
2188 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002189 }
2190 return true;
2191}
2192
2193
2194bool
2195Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2196 StringList &value,
2197 Error &err)
2198{
Greg Claytond284b662011-02-18 01:44:25 +00002199 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002200 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002201 // If the arch is invalid (the default), don't show a string for it
2202 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002203 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002204 return true;
2205 }
2206 else
2207 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2208
2209 return false;
2210}
2211
2212//--------------------------------------------------------------
2213// class TargetInstanceSettings
2214//--------------------------------------------------------------
2215
Greg Clayton638351a2010-12-04 00:10:17 +00002216TargetInstanceSettings::TargetInstanceSettings
2217(
Greg Clayton334d33a2012-01-30 07:41:31 +00002218 const lldb::UserSettingsControllerSP &owner_sp,
Greg Clayton638351a2010-12-04 00:10:17 +00002219 bool live_instance,
2220 const char *name
2221) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002222 InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002223 m_expr_prefix_file (),
Sean Callanane0b7f942011-11-16 01:54:57 +00002224 m_expr_prefix_contents (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002225 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00002226 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002227 m_source_map (NULL, NULL),
Greg Clayton9ce95382012-02-13 23:10:39 +00002228 m_exe_search_paths (),
Enrico Granata91544802011-09-06 19:20:51 +00002229 m_max_children_display(256),
Jim Ingham7089d8a2011-10-28 23:14:11 +00002230 m_max_strlen_length(1024),
Greg Claytonabb33022011-11-08 02:43:13 +00002231 m_breakpoints_use_platform_avoid (true, true),
2232 m_run_args (),
2233 m_env_vars (),
2234 m_input_path (),
2235 m_output_path (),
2236 m_error_path (),
2237 m_disable_aslr (true),
2238 m_disable_stdio (false),
2239 m_inherit_host_env (true),
2240 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002241{
2242 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2243 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2244 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2245 // This is true for CreateInstanceName() too.
2246
2247 if (GetInstanceName () == InstanceSettings::InvalidName())
2248 {
2249 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
Greg Clayton334d33a2012-01-30 07:41:31 +00002250 owner_sp->RegisterInstanceSettings (this);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002251 }
2252
2253 if (live_instance)
2254 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002255 const lldb::InstanceSettingsSP &pending_settings = owner_sp->FindPendingSettings (m_instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002256 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002257 }
2258}
2259
2260TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton334d33a2012-01-30 07:41:31 +00002261 InstanceSettings (Target::GetSettingsController(), CreateInstanceName().AsCString()),
Greg Claytonff44ab42011-04-23 02:04:55 +00002262 m_expr_prefix_file (rhs.m_expr_prefix_file),
Sean Callanane0b7f942011-11-16 01:54:57 +00002263 m_expr_prefix_contents (rhs.m_expr_prefix_contents),
Greg Claytonff44ab42011-04-23 02:04:55 +00002264 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2265 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002266 m_source_map (rhs.m_source_map),
Greg Clayton9ce95382012-02-13 23:10:39 +00002267 m_exe_search_paths (rhs.m_exe_search_paths),
Greg Claytonabb33022011-11-08 02:43:13 +00002268 m_max_children_display (rhs.m_max_children_display),
2269 m_max_strlen_length (rhs.m_max_strlen_length),
2270 m_breakpoints_use_platform_avoid (rhs.m_breakpoints_use_platform_avoid),
2271 m_run_args (rhs.m_run_args),
2272 m_env_vars (rhs.m_env_vars),
2273 m_input_path (rhs.m_input_path),
2274 m_output_path (rhs.m_output_path),
2275 m_error_path (rhs.m_error_path),
2276 m_disable_aslr (rhs.m_disable_aslr),
2277 m_disable_stdio (rhs.m_disable_stdio),
2278 m_inherit_host_env (rhs.m_inherit_host_env)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002279{
2280 if (m_instance_name != InstanceSettings::GetDefaultName())
2281 {
Greg Clayton334d33a2012-01-30 07:41:31 +00002282 UserSettingsControllerSP owner_sp (m_owner_wp.lock());
2283 if (owner_sp)
2284 CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name),false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002285 }
2286}
2287
2288TargetInstanceSettings::~TargetInstanceSettings ()
2289{
2290}
2291
2292TargetInstanceSettings&
2293TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2294{
2295 if (this != &rhs)
2296 {
Greg Claytonabb33022011-11-08 02:43:13 +00002297 m_expr_prefix_file = rhs.m_expr_prefix_file;
Sean Callanane0b7f942011-11-16 01:54:57 +00002298 m_expr_prefix_contents = rhs.m_expr_prefix_contents;
Greg Claytonabb33022011-11-08 02:43:13 +00002299 m_prefer_dynamic_value = rhs.m_prefer_dynamic_value;
2300 m_skip_prologue = rhs.m_skip_prologue;
2301 m_source_map = rhs.m_source_map;
Greg Clayton9ce95382012-02-13 23:10:39 +00002302 m_exe_search_paths = rhs.m_exe_search_paths;
Greg Claytonabb33022011-11-08 02:43:13 +00002303 m_max_children_display = rhs.m_max_children_display;
2304 m_max_strlen_length = rhs.m_max_strlen_length;
2305 m_breakpoints_use_platform_avoid = rhs.m_breakpoints_use_platform_avoid;
2306 m_run_args = rhs.m_run_args;
2307 m_env_vars = rhs.m_env_vars;
2308 m_input_path = rhs.m_input_path;
2309 m_output_path = rhs.m_output_path;
2310 m_error_path = rhs.m_error_path;
2311 m_disable_aslr = rhs.m_disable_aslr;
2312 m_disable_stdio = rhs.m_disable_stdio;
2313 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002314 }
2315
2316 return *this;
2317}
2318
Caroline Tice5bc8c972010-09-20 20:44:43 +00002319void
2320TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2321 const char *index_value,
2322 const char *value,
2323 const ConstString &instance_name,
2324 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002325 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002326 Error &err,
2327 bool pending)
2328{
Greg Claytond284b662011-02-18 01:44:25 +00002329 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002330 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002331 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2332 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002333 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002334 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002335 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002336 default:
2337 break;
2338 case eVarSetOperationAssign:
2339 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002340 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002341 m_expr_prefix_contents.clear();
2342
Greg Claytonff44ab42011-04-23 02:04:55 +00002343 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2344 {
2345 err.SetErrorToGenericError ();
Greg Clayton9c236732011-10-26 00:56:27 +00002346 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002347 return;
2348 }
2349
Greg Clayton4b23ab32012-01-06 02:01:06 +00002350 DataBufferSP file_data_sp (m_expr_prefix_file.GetCurrentValue().ReadFileContents(0, SIZE_MAX, &err));
Sean Callanane0b7f942011-11-16 01:54:57 +00002351
Greg Clayton4b23ab32012-01-06 02:01:06 +00002352 if (err.Success())
Greg Claytonff44ab42011-04-23 02:04:55 +00002353 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002354 if (file_data_sp && file_data_sp->GetByteSize() > 0)
2355 {
2356 m_expr_prefix_contents.assign((const char*)file_data_sp->GetBytes(), file_data_sp->GetByteSize());
2357 }
2358 else
2359 {
2360 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
2361 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002362 }
Sean Callanan77e93942010-10-29 00:29:03 +00002363 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002364 break;
2365 case eVarSetOperationClear:
Sean Callanane0b7f942011-11-16 01:54:57 +00002366 m_expr_prefix_contents.clear();
Sean Callanan77e93942010-10-29 00:29:03 +00002367 }
Sean Callanan77e93942010-10-29 00:29:03 +00002368 }
2369 }
Jim Inghame41494a2011-04-16 00:01:13 +00002370 else if (var_name == GetSettingNameForPreferDynamicValue())
2371 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002372 int new_value;
2373 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2374 if (err.Success())
2375 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002376 }
2377 else if (var_name == GetSettingNameForSkipPrologue())
2378 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002379 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2380 }
Enrico Granata018921d2011-08-12 02:00:06 +00002381 else if (var_name == GetSettingNameForMaxChildren())
2382 {
2383 bool ok;
2384 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2385 if (ok)
2386 m_max_children_display = new_value;
2387 }
Enrico Granata91544802011-09-06 19:20:51 +00002388 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2389 {
2390 bool ok;
2391 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2392 if (ok)
2393 m_max_strlen_length = new_value;
2394 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002395 else if (var_name == GetSettingNameForExecutableSearchPaths())
2396 {
2397 switch (op)
2398 {
2399 case eVarSetOperationReplace:
2400 case eVarSetOperationInsertBefore:
2401 case eVarSetOperationInsertAfter:
2402 case eVarSetOperationRemove:
2403 default:
2404 break;
2405 case eVarSetOperationAssign:
2406 m_exe_search_paths.Clear();
2407 // Fall through to append....
2408 case eVarSetOperationAppend:
2409 {
2410 Args args(value);
2411 const uint32_t argc = args.GetArgumentCount();
2412 if (argc > 0)
2413 {
2414 const char *exe_search_path_dir;
2415 for (uint32_t idx = 0; (exe_search_path_dir = args.GetArgumentAtIndex(idx)) != NULL; ++idx)
2416 {
2417 FileSpec file_spec;
2418 file_spec.GetDirectory().SetCString(exe_search_path_dir);
2419 FileSpec::FileType file_type = file_spec.GetFileType();
2420 if (file_type == FileSpec::eFileTypeDirectory || file_type == FileSpec::eFileTypeInvalid)
2421 {
2422 m_exe_search_paths.Append(file_spec);
2423 }
2424 else
2425 {
2426 err.SetErrorStringWithFormat("executable search path '%s' exists, but it does not resolve to a directory", exe_search_path_dir);
2427 }
2428 }
2429 }
2430 }
2431 break;
2432
2433 case eVarSetOperationClear:
2434 m_exe_search_paths.Clear();
2435 break;
2436 }
2437 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002438 else if (var_name == GetSettingNameForSourcePathMap ())
2439 {
2440 switch (op)
2441 {
2442 case eVarSetOperationReplace:
2443 case eVarSetOperationInsertBefore:
2444 case eVarSetOperationInsertAfter:
2445 case eVarSetOperationRemove:
2446 default:
2447 break;
2448 case eVarSetOperationAssign:
2449 m_source_map.Clear(true);
2450 // Fall through to append....
2451 case eVarSetOperationAppend:
2452 {
2453 Args args(value);
2454 const uint32_t argc = args.GetArgumentCount();
2455 if (argc & 1 || argc == 0)
2456 {
2457 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2458 }
2459 else
2460 {
2461 char resolved_new_path[PATH_MAX];
2462 FileSpec file_spec;
2463 const char *old_path;
2464 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2465 {
2466 const char *new_path = args.GetArgumentAtIndex(idx+1);
2467 assert (new_path); // We have an even number of paths, this shouldn't happen!
2468
2469 file_spec.SetFile(new_path, true);
2470 if (file_spec.Exists())
2471 {
2472 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2473 {
2474 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2475 return;
2476 }
2477 }
2478 else
2479 {
2480 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2481 return;
2482 }
2483 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2484 }
2485 }
2486 }
2487 break;
2488
2489 case eVarSetOperationClear:
2490 m_source_map.Clear(true);
2491 break;
2492 }
Jim Inghame41494a2011-04-16 00:01:13 +00002493 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002494 else if (var_name == GetSettingNameForPlatformAvoid ())
2495 {
2496 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_breakpoints_use_platform_avoid);
2497 }
Greg Claytonabb33022011-11-08 02:43:13 +00002498 else if (var_name == GetSettingNameForRunArgs())
2499 {
2500 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2501 }
2502 else if (var_name == GetSettingNameForEnvVars())
2503 {
2504 // This is nice for local debugging, but it is isn't correct for
2505 // remote debugging. We need to stop process.env-vars from being
2506 // populated with the host environment and add this as a launch option
2507 // and get the correct environment from the Target's platform.
2508 // GetHostEnvironmentIfNeeded ();
2509 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2510 }
2511 else if (var_name == GetSettingNameForInputPath())
2512 {
2513 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2514 }
2515 else if (var_name == GetSettingNameForOutputPath())
2516 {
2517 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2518 }
2519 else if (var_name == GetSettingNameForErrorPath())
2520 {
2521 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2522 }
2523 else if (var_name == GetSettingNameForDisableASLR())
2524 {
2525 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
2526 }
2527 else if (var_name == GetSettingNameForDisableSTDIO ())
2528 {
2529 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
2530 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002531}
2532
2533void
Greg Claytond284b662011-02-18 01:44:25 +00002534TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002535{
Sean Callanan77e93942010-10-29 00:29:03 +00002536 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2537
2538 if (!new_settings_ptr)
2539 return;
2540
Greg Claytonabb33022011-11-08 02:43:13 +00002541 *this = *new_settings_ptr;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002542}
2543
Caroline Ticebcb5b452010-09-20 21:37:42 +00002544bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002545TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2546 const ConstString &var_name,
2547 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002548 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002549{
Greg Claytond284b662011-02-18 01:44:25 +00002550 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002551 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002552 char path[PATH_MAX];
2553 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2554 if (path_len > 0)
2555 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002556 }
Jim Inghame41494a2011-04-16 00:01:13 +00002557 else if (var_name == GetSettingNameForPreferDynamicValue())
2558 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002559 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002560 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002561 else if (var_name == GetSettingNameForSkipPrologue())
2562 {
2563 if (m_skip_prologue)
2564 value.AppendString ("true");
2565 else
2566 value.AppendString ("false");
2567 }
Greg Clayton9ce95382012-02-13 23:10:39 +00002568 else if (var_name == GetSettingNameForExecutableSearchPaths())
2569 {
2570 if (m_exe_search_paths.GetSize())
2571 {
2572 for (size_t i = 0, n = m_exe_search_paths.GetSize(); i < n; ++i)
2573 {
2574 value.AppendString(m_exe_search_paths.GetFileSpecAtIndex (i).GetDirectory().AsCString());
2575 }
2576 }
2577 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002578 else if (var_name == GetSettingNameForSourcePathMap ())
2579 {
Johnny Chen931449e2011-12-12 21:59:28 +00002580 if (m_source_map.GetSize())
2581 {
2582 size_t i;
2583 for (i = 0; i < m_source_map.GetSize(); ++i) {
2584 StreamString sstr;
2585 m_source_map.Dump(&sstr, i);
2586 value.AppendString(sstr.GetData());
2587 }
2588 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002589 }
Enrico Granata018921d2011-08-12 02:00:06 +00002590 else if (var_name == GetSettingNameForMaxChildren())
2591 {
2592 StreamString count_str;
2593 count_str.Printf ("%d", m_max_children_display);
2594 value.AppendString (count_str.GetData());
2595 }
Enrico Granata91544802011-09-06 19:20:51 +00002596 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2597 {
2598 StreamString count_str;
2599 count_str.Printf ("%d", m_max_strlen_length);
2600 value.AppendString (count_str.GetData());
2601 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002602 else if (var_name == GetSettingNameForPlatformAvoid())
2603 {
2604 if (m_breakpoints_use_platform_avoid)
2605 value.AppendString ("true");
2606 else
2607 value.AppendString ("false");
2608 }
Greg Claytonabb33022011-11-08 02:43:13 +00002609 else if (var_name == GetSettingNameForRunArgs())
2610 {
2611 if (m_run_args.GetArgumentCount() > 0)
2612 {
2613 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2614 value.AppendString (m_run_args.GetArgumentAtIndex (i));
2615 }
2616 }
2617 else if (var_name == GetSettingNameForEnvVars())
2618 {
2619 GetHostEnvironmentIfNeeded ();
2620
2621 if (m_env_vars.size() > 0)
2622 {
2623 std::map<std::string, std::string>::iterator pos;
2624 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2625 {
2626 StreamString value_str;
2627 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2628 value.AppendString (value_str.GetData());
2629 }
2630 }
2631 }
2632 else if (var_name == GetSettingNameForInputPath())
2633 {
2634 value.AppendString (m_input_path.c_str());
2635 }
2636 else if (var_name == GetSettingNameForOutputPath())
2637 {
2638 value.AppendString (m_output_path.c_str());
2639 }
2640 else if (var_name == GetSettingNameForErrorPath())
2641 {
2642 value.AppendString (m_error_path.c_str());
2643 }
2644 else if (var_name == GetSettingNameForInheritHostEnv())
2645 {
2646 if (m_inherit_host_env)
2647 value.AppendString ("true");
2648 else
2649 value.AppendString ("false");
2650 }
2651 else if (var_name == GetSettingNameForDisableASLR())
2652 {
2653 if (m_disable_aslr)
2654 value.AppendString ("true");
2655 else
2656 value.AppendString ("false");
2657 }
2658 else if (var_name == GetSettingNameForDisableSTDIO())
2659 {
2660 if (m_disable_stdio)
2661 value.AppendString ("true");
2662 else
2663 value.AppendString ("false");
2664 }
Sean Callanan77e93942010-10-29 00:29:03 +00002665 else
2666 {
2667 if (err)
2668 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2669 return false;
2670 }
Sean Callanan77e93942010-10-29 00:29:03 +00002671 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002672}
2673
Greg Claytonabb33022011-11-08 02:43:13 +00002674void
2675Target::TargetInstanceSettings::GetHostEnvironmentIfNeeded ()
2676{
2677 if (m_inherit_host_env && !m_got_host_env)
2678 {
2679 m_got_host_env = true;
2680 StringList host_env;
2681 const size_t host_env_count = Host::GetEnvironment (host_env);
2682 for (size_t idx=0; idx<host_env_count; idx++)
2683 {
2684 const char *env_entry = host_env.GetStringAtIndex (idx);
2685 if (env_entry)
2686 {
2687 const char *equal_pos = ::strchr(env_entry, '=');
2688 if (equal_pos)
2689 {
2690 std::string key (env_entry, equal_pos - env_entry);
2691 std::string value (equal_pos + 1);
2692 if (m_env_vars.find (key) == m_env_vars.end())
2693 m_env_vars[key] = value;
2694 }
2695 }
2696 }
2697 }
2698}
2699
2700
2701size_t
2702Target::TargetInstanceSettings::GetEnvironmentAsArgs (Args &env)
2703{
2704 GetHostEnvironmentIfNeeded ();
2705
2706 dictionary::const_iterator pos, end = m_env_vars.end();
2707 for (pos = m_env_vars.begin(); pos != end; ++pos)
2708 {
2709 std::string env_var_equal_value (pos->first);
2710 env_var_equal_value.append(1, '=');
2711 env_var_equal_value.append (pos->second);
2712 env.AppendArgument (env_var_equal_value.c_str());
2713 }
2714 return env.GetArgumentCount();
2715}
2716
2717
Caroline Tice5bc8c972010-09-20 20:44:43 +00002718const ConstString
2719TargetInstanceSettings::CreateInstanceName ()
2720{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002721 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002722 static int instance_count = 1;
2723
Caroline Tice5bc8c972010-09-20 20:44:43 +00002724 sstr.Printf ("target_%d", instance_count);
2725 ++instance_count;
2726
2727 const ConstString ret_val (sstr.GetData());
2728 return ret_val;
2729}
2730
2731//--------------------------------------------------
2732// Target::SettingsController Variable Tables
2733//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002734OptionEnumValueElement
2735TargetInstanceSettings::g_dynamic_value_types[] =
2736{
Greg Clayton577fbc32011-05-30 00:39:48 +00002737{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2738{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2739{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002740{ 0, NULL, NULL }
2741};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002742
2743SettingEntry
2744Target::SettingsController::global_settings_table[] =
2745{
Greg Claytond284b662011-02-18 01:44:25 +00002746 // var-name var-type default enum init'd hidden help-text
2747 // ================= ================== =========== ==== ====== ====== =========================================================================
2748 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2749 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2750};
2751
Caroline Tice5bc8c972010-09-20 20:44:43 +00002752SettingEntry
2753Target::SettingsController::instance_settings_table[] =
2754{
Enrico Granata91544802011-09-06 19:20:51 +00002755 // var-name var-type default enum init'd hidden help-text
2756 // ================= ================== =============== ======================= ====== ====== =========================================================================
2757 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2758 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2759 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2760 { 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 +00002761 { 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 +00002762 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2763 { 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 +00002764 { 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 +00002765 { TSC_RUN_ARGS , eSetVarTypeArray , NULL , NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2766 { 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." },
2767 { TSC_INHERIT_ENV , eSetVarTypeBoolean, "true" , NULL, false, false, "Inherit the environment from the process that is running LLDB." },
2768 { TSC_STDIN_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for reading its standard input." },
2769 { TSC_STDOUT_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard output." },
2770 { TSC_STDERR_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard error." },
2771// { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
2772 { TSC_DISABLE_ASLR , eSetVarTypeBoolean, "true" , NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2773 { 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 +00002774 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002775};