blob: 022cdb3e5069941e927e3975bfe68d0cae9a79dc [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 Claytonc0c1b0c2010-11-19 03:46:01 +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),
Jim Inghamd60d94a2011-03-11 03:53:59 +000064 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000065 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000066 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000067 m_stop_hook_next_id (0),
68 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000069{
Greg Clayton49ce6822010-10-31 03:01:06 +000070 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
71 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
72 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
73
Greg Claytone005f2c2010-11-06 01:53:30 +000074 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000075 if (log)
76 log->Printf ("%p Target::Target()", this);
77}
78
79//----------------------------------------------------------------------
80// Destructor
81//----------------------------------------------------------------------
82Target::~Target()
83{
Greg Claytone005f2c2010-11-06 01:53:30 +000084 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000085 if (log)
86 log->Printf ("%p Target::~Target()", this);
87 DeleteCurrentProcess ();
88}
89
90void
Caroline Tice7826c882010-10-26 03:11:13 +000091Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000092{
Greg Clayton3fed8b92010-10-08 00:21:05 +000093// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000094 if (description_level != lldb::eDescriptionLevelBrief)
95 {
96 s->Indent();
97 s->PutCString("Target\n");
98 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000099 m_images.Dump(s);
100 m_breakpoint_list.Dump(s);
101 m_internal_breakpoint_list.Dump(s);
102 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000103 }
104 else
105 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000106 Module *exe_module = GetExecutableModulePointer();
107 if (exe_module)
108 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000109 else
110 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000111 }
Chris Lattner24943d22010-06-08 16:52:24 +0000112}
113
114void
115Target::DeleteCurrentProcess ()
116{
117 if (m_process_sp.get())
118 {
Greg Clayton49480b12010-09-14 23:52:43 +0000119 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000120 if (m_process_sp->IsAlive())
121 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000122
123 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000124
125 // Do any cleanup of the target we need to do between process instances.
126 // NB It is better to do this before destroying the process in case the
127 // clean up needs some help from the process.
128 m_breakpoint_list.ClearAllBreakpointSites();
129 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000130 // Disable watchpoints just on the debugger side.
131 DisableAllWatchpoints(false);
Chris Lattner24943d22010-06-08 16:52:24 +0000132 m_process_sp.reset();
133 }
134}
135
136const lldb::ProcessSP &
137Target::CreateProcess (Listener &listener, const char *plugin_name)
138{
139 DeleteCurrentProcess ();
140 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
141 return m_process_sp;
142}
143
144const lldb::ProcessSP &
145Target::GetProcessSP () const
146{
147 return m_process_sp;
148}
149
150lldb::TargetSP
151Target::GetSP()
152{
Greg Clayton987c7eb2011-09-17 08:33:22 +0000153 // This object contains an instrusive ref count base class so we can
154 // easily make a shared pointer to this object
155 return TargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000156}
157
Greg Clayton153ccd72011-08-10 02:10:13 +0000158void
159Target::Destroy()
160{
161 Mutex::Locker locker (m_mutex);
162 DeleteCurrentProcess ();
163 m_platform_sp.reset();
164 m_arch.Clear();
165 m_images.Clear();
166 m_section_load_list.Clear();
167 const bool notify = false;
168 m_breakpoint_list.RemoveAll(notify);
169 m_internal_breakpoint_list.RemoveAll(notify);
170 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000171 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000172 m_search_filter_sp.reset();
173 m_image_search_paths.Clear(notify);
174 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000175 m_scratch_ast_source_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000176 m_persistent_variables.Clear();
177 m_stop_hooks.clear();
178 m_stop_hook_next_id = 0;
179 m_suppress_stop_hooks = false;
180}
181
182
Chris Lattner24943d22010-06-08 16:52:24 +0000183BreakpointList &
184Target::GetBreakpointList(bool internal)
185{
186 if (internal)
187 return m_internal_breakpoint_list;
188 else
189 return m_breakpoint_list;
190}
191
192const BreakpointList &
193Target::GetBreakpointList(bool internal) const
194{
195 if (internal)
196 return m_internal_breakpoint_list;
197 else
198 return m_breakpoint_list;
199}
200
201BreakpointSP
202Target::GetBreakpointByID (break_id_t break_id)
203{
204 BreakpointSP bp_sp;
205
206 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
207 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
208 else
209 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
210
211 return bp_sp;
212}
213
214BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000215Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
216 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000217 RegularExpression &source_regex,
218 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000219{
Jim Inghamd6d47972011-09-23 00:54:11 +0000220 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
221 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000222 return CreateBreakpoint (filter_sp, resolver_sp, internal);
223}
224
225
226BreakpointSP
227Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
228{
229 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner24943d22010-06-08 16:52:24 +0000230 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
231 return CreateBreakpoint (filter_sp, resolver_sp, internal);
232}
233
234
235BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000236Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000237{
Chris Lattner24943d22010-06-08 16:52:24 +0000238 Address so_addr;
239 // Attempt to resolve our load address if possible, though it is ok if
240 // it doesn't resolve to section/offset.
241
Greg Clayton33ed1702010-08-24 00:45:41 +0000242 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000243 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000244 if (!so_addr.IsValid())
245 {
246 // The address didn't resolve, so just set this as an absolute address
247 so_addr.SetOffset (addr);
248 }
249 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000250 return bp_sp;
251}
252
253BreakpointSP
254Target::CreateBreakpoint (Address &addr, bool internal)
255{
256 TargetSP target_sp = this->GetSP();
Jim Ingham7089d8a2011-10-28 23:14:11 +0000257 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (target_sp));
Chris Lattner24943d22010-06-08 16:52:24 +0000258 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
259 return CreateBreakpoint (filter_sp, resolver_sp, internal);
260}
261
262BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000263Target::CreateBreakpoint (const FileSpecList *containingModules,
264 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000265 const char *func_name,
266 uint32_t func_name_type_mask,
267 bool internal,
268 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000269{
Greg Clayton12bec712010-06-28 21:30:43 +0000270 BreakpointSP bp_sp;
271 if (func_name)
272 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000273 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000274
275 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
276 func_name,
277 func_name_type_mask,
278 Breakpoint::Exact,
279 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000280 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
281 }
282 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000283}
284
285
286SearchFilterSP
287Target::GetSearchFilterForModule (const FileSpec *containingModule)
288{
289 SearchFilterSP filter_sp;
290 lldb::TargetSP target_sp = this->GetSP();
291 if (containingModule != NULL)
292 {
293 // TODO: We should look into sharing module based search filters
294 // across many breakpoints like we do for the simple target based one
295 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
296 }
297 else
298 {
299 if (m_search_filter_sp.get() == NULL)
Jim Ingham7089d8a2011-10-28 23:14:11 +0000300 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (target_sp));
Chris Lattner24943d22010-06-08 16:52:24 +0000301 filter_sp = m_search_filter_sp;
302 }
303 return filter_sp;
304}
305
Jim Ingham03c8ee52011-09-21 01:17:13 +0000306SearchFilterSP
307Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
308{
309 SearchFilterSP filter_sp;
310 lldb::TargetSP target_sp = this->GetSP();
311 if (containingModules && containingModules->GetSize() != 0)
312 {
313 // TODO: We should look into sharing module based search filters
314 // across many breakpoints like we do for the simple target based one
315 filter_sp.reset (new SearchFilterByModuleList (target_sp, *containingModules));
316 }
317 else
318 {
319 if (m_search_filter_sp.get() == NULL)
Jim Ingham7089d8a2011-10-28 23:14:11 +0000320 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (target_sp));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000321 filter_sp = m_search_filter_sp;
322 }
323 return filter_sp;
324}
325
Jim Inghamd6d47972011-09-23 00:54:11 +0000326SearchFilterSP
327Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles)
328{
329 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
330 return GetSearchFilterForModuleList(containingModules);
331
332 SearchFilterSP filter_sp;
333 lldb::TargetSP target_sp = this->GetSP();
334 if (containingModules == NULL)
335 {
336 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
337 // but that will take a little reworking.
338
339 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, FileSpecList(), *containingSourceFiles));
340 }
341 else
342 {
343 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, *containingModules, *containingSourceFiles));
344 }
345 return filter_sp;
346}
347
Chris Lattner24943d22010-06-08 16:52:24 +0000348BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000349Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
350 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000351 RegularExpression &func_regex,
352 bool internal,
353 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000354{
Jim Inghamd6d47972011-09-23 00:54:11 +0000355 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000356 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
357 func_regex,
358 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000359
360 return CreateBreakpoint (filter_sp, resolver_sp, internal);
361}
362
363BreakpointSP
364Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
365{
366 BreakpointSP bp_sp;
367 if (filter_sp && resolver_sp)
368 {
369 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
370 resolver_sp->SetBreakpoint (bp_sp.get());
371
372 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000373 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000374 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000375 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000376
Greg Claytone005f2c2010-11-06 01:53:30 +0000377 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000378 if (log)
379 {
380 StreamString s;
381 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
382 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
383 }
384
Chris Lattner24943d22010-06-08 16:52:24 +0000385 bp_sp->ResolveBreakpoint();
386 }
Jim Inghamd1686902010-10-14 23:45:03 +0000387
388 if (!internal && bp_sp)
389 {
390 m_last_created_breakpoint = bp_sp;
391 }
392
Chris Lattner24943d22010-06-08 16:52:24 +0000393 return bp_sp;
394}
395
Johnny Chenda5a8022011-09-20 23:28:55 +0000396bool
397Target::ProcessIsValid()
398{
399 return (m_process_sp && m_process_sp->IsAlive());
400}
401
Johnny Chenecd4feb2011-10-14 00:42:25 +0000402// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000403// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000404WatchpointSP
405Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type)
Johnny Chen34bbf852011-09-12 23:38:44 +0000406{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000407 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
408 if (log)
409 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
410 __FUNCTION__, addr, size, type);
411
Johnny Chenecd4feb2011-10-14 00:42:25 +0000412 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000413 if (!ProcessIsValid())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000414 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000415 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000416 return wp_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000417
Johnny Chenecd4feb2011-10-14 00:42:25 +0000418 // Currently we only support one watchpoint per address, with total number
419 // of watchpoints limited by the hardware which the inferior is running on.
420 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000421 if (matched_sp)
422 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000423 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000424 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000425 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
426 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000427 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000428 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000429 wp_sp = matched_sp;
430 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000431 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000432 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000433 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000434 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000435 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000436 }
437
Johnny Chenecd4feb2011-10-14 00:42:25 +0000438 if (!wp_sp) {
439 Watchpoint *new_wp = new Watchpoint(addr, size);
440 if (!new_wp) {
441 printf("Watchpoint ctor failed, out of memory?\n");
442 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000443 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000444 new_wp->SetWatchpointType(type);
445 new_wp->SetTarget(this);
446 wp_sp.reset(new_wp);
447 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000448 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000449
Johnny Chenecd4feb2011-10-14 00:42:25 +0000450 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000451 if (log)
452 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
453 __FUNCTION__,
454 rc.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000455 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000456
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000457 if (rc.Fail())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000458 wp_sp.reset();
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000459 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000460 m_last_created_watchpoint = wp_sp;
461 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000462}
463
Chris Lattner24943d22010-06-08 16:52:24 +0000464void
465Target::RemoveAllBreakpoints (bool internal_also)
466{
Greg Claytone005f2c2010-11-06 01:53:30 +0000467 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000468 if (log)
469 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
470
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000471 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000472 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000473 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000474
475 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000476}
477
478void
479Target::DisableAllBreakpoints (bool internal_also)
480{
Greg Claytone005f2c2010-11-06 01:53:30 +0000481 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000482 if (log)
483 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
484
485 m_breakpoint_list.SetEnabledAll (false);
486 if (internal_also)
487 m_internal_breakpoint_list.SetEnabledAll (false);
488}
489
490void
491Target::EnableAllBreakpoints (bool internal_also)
492{
Greg Claytone005f2c2010-11-06 01:53:30 +0000493 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000494 if (log)
495 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
496
497 m_breakpoint_list.SetEnabledAll (true);
498 if (internal_also)
499 m_internal_breakpoint_list.SetEnabledAll (true);
500}
501
502bool
503Target::RemoveBreakpointByID (break_id_t break_id)
504{
Greg Claytone005f2c2010-11-06 01:53:30 +0000505 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000506 if (log)
507 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
508
509 if (DisableBreakpointByID (break_id))
510 {
511 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000512 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000513 else
Jim Inghamd1686902010-10-14 23:45:03 +0000514 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000515 if (m_last_created_breakpoint)
516 {
517 if (m_last_created_breakpoint->GetID() == break_id)
518 m_last_created_breakpoint.reset();
519 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000520 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000521 }
Chris Lattner24943d22010-06-08 16:52:24 +0000522 return true;
523 }
524 return false;
525}
526
527bool
528Target::DisableBreakpointByID (break_id_t break_id)
529{
Greg Claytone005f2c2010-11-06 01:53:30 +0000530 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000531 if (log)
532 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
533
534 BreakpointSP bp_sp;
535
536 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
537 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
538 else
539 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
540 if (bp_sp)
541 {
542 bp_sp->SetEnabled (false);
543 return true;
544 }
545 return false;
546}
547
548bool
549Target::EnableBreakpointByID (break_id_t break_id)
550{
Greg Claytone005f2c2010-11-06 01:53:30 +0000551 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000552 if (log)
553 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
554 __FUNCTION__,
555 break_id,
556 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
557
558 BreakpointSP bp_sp;
559
560 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
561 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
562 else
563 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
564
565 if (bp_sp)
566 {
567 bp_sp->SetEnabled (true);
568 return true;
569 }
570 return false;
571}
572
Johnny Chenc86582f2011-09-23 21:21:43 +0000573// The flag 'end_to_end', default to true, signifies that the operation is
574// performed end to end, for both the debugger and the debuggee.
575
Johnny Chenecd4feb2011-10-14 00:42:25 +0000576// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
577// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000578bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000579Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000580{
581 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
582 if (log)
583 log->Printf ("Target::%s\n", __FUNCTION__);
584
Johnny Chenc86582f2011-09-23 21:21:43 +0000585 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000586 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000587 return true;
588 }
589
590 // Otherwise, it's an end to end operation.
591
Johnny Chenda5a8022011-09-20 23:28:55 +0000592 if (!ProcessIsValid())
593 return false;
594
Johnny Chenecd4feb2011-10-14 00:42:25 +0000595 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000596 for (size_t i = 0; i < num_watchpoints; ++i)
597 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000598 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
599 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000600 return false;
601
Johnny Chenecd4feb2011-10-14 00:42:25 +0000602 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000603 if (rc.Fail())
604 return false;
605 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000606 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000607 return true; // Success!
608}
609
Johnny Chenecd4feb2011-10-14 00:42:25 +0000610// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
611// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000612bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000613Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000614{
615 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
616 if (log)
617 log->Printf ("Target::%s\n", __FUNCTION__);
618
Johnny Chenc86582f2011-09-23 21:21:43 +0000619 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000620 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000621 return true;
622 }
623
624 // Otherwise, it's an end to end operation.
625
Johnny Chenda5a8022011-09-20 23:28:55 +0000626 if (!ProcessIsValid())
627 return false;
628
Johnny Chenecd4feb2011-10-14 00:42:25 +0000629 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000630 for (size_t i = 0; i < num_watchpoints; ++i)
631 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000632 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
633 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000634 return false;
635
Johnny Chenecd4feb2011-10-14 00:42:25 +0000636 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000637 if (rc.Fail())
638 return false;
639 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000640 return true; // Success!
641}
642
Johnny Chenecd4feb2011-10-14 00:42:25 +0000643// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
644// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000645bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000646Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000647{
648 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
649 if (log)
650 log->Printf ("Target::%s\n", __FUNCTION__);
651
Johnny Chenc86582f2011-09-23 21:21:43 +0000652 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000653 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000654 return true;
655 }
656
657 // Otherwise, it's an end to end operation.
658
Johnny Chenda5a8022011-09-20 23:28:55 +0000659 if (!ProcessIsValid())
660 return false;
661
Johnny Chenecd4feb2011-10-14 00:42:25 +0000662 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000663 for (size_t i = 0; i < num_watchpoints; ++i)
664 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000665 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
666 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000667 return false;
668
Johnny Chenecd4feb2011-10-14 00:42:25 +0000669 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000670 if (rc.Fail())
671 return false;
672 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000673 return true; // Success!
674}
675
Johnny Chenecd4feb2011-10-14 00:42:25 +0000676// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000677// during these operations.
678bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000679Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000680{
681 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
682 if (log)
683 log->Printf ("Target::%s\n", __FUNCTION__);
684
685 if (!ProcessIsValid())
686 return false;
687
Johnny Chenecd4feb2011-10-14 00:42:25 +0000688 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000689 for (size_t i = 0; i < num_watchpoints; ++i)
690 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000691 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
692 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000693 return false;
694
Johnny Chenecd4feb2011-10-14 00:42:25 +0000695 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000696 }
697 return true; // Success!
698}
699
Johnny Chenecd4feb2011-10-14 00:42:25 +0000700// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000701bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000702Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000703{
704 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
705 if (log)
706 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
707
708 if (!ProcessIsValid())
709 return false;
710
Johnny Chenecd4feb2011-10-14 00:42:25 +0000711 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
712 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000713 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000714 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000715 if (rc.Success())
716 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000717
Johnny Chen01acfa72011-09-22 18:04:58 +0000718 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000719 }
720 return false;
721}
722
Johnny Chenecd4feb2011-10-14 00:42:25 +0000723// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000724bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000725Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000726{
727 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
728 if (log)
729 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
730
731 if (!ProcessIsValid())
732 return false;
733
Johnny Chenecd4feb2011-10-14 00:42:25 +0000734 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
735 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000736 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000737 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000738 if (rc.Success())
739 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000740
Johnny Chen01acfa72011-09-22 18:04:58 +0000741 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000742 }
743 return false;
744}
745
Johnny Chenecd4feb2011-10-14 00:42:25 +0000746// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000747bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000748Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000749{
750 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
751 if (log)
752 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
753
Johnny Chenecd4feb2011-10-14 00:42:25 +0000754 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000755 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000756 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000757 return true;
758 }
759 return false;
760}
761
Johnny Chenecd4feb2011-10-14 00:42:25 +0000762// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000763bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000764Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000765{
766 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
767 if (log)
768 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
769
770 if (!ProcessIsValid())
771 return false;
772
Johnny Chenecd4feb2011-10-14 00:42:25 +0000773 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
774 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000775 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000776 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000777 return true;
778 }
779 return false;
780}
781
Chris Lattner24943d22010-06-08 16:52:24 +0000782ModuleSP
783Target::GetExecutableModule ()
784{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000785 return m_images.GetModuleAtIndex(0);
786}
787
788Module*
789Target::GetExecutableModulePointer ()
790{
791 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000792}
793
794void
795Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
796{
797 m_images.Clear();
798 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000799 m_scratch_ast_source_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000800
801 if (executable_sp.get())
802 {
803 Timer scoped_timer (__PRETTY_FUNCTION__,
804 "Target::SetExecutableModule (executable = '%s/%s')",
805 executable_sp->GetFileSpec().GetDirectory().AsCString(),
806 executable_sp->GetFileSpec().GetFilename().AsCString());
807
808 m_images.Append(executable_sp); // The first image is our exectuable file
809
Jim Ingham7508e732010-08-09 23:31:02 +0000810 // 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 +0000811 if (!m_arch.IsValid())
812 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000813
Chris Lattner24943d22010-06-08 16:52:24 +0000814 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000815 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000816
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000817 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000818 {
819 executable_objfile->GetDependentModules(dependent_files);
820 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
821 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000822 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
823 FileSpec platform_dependent_file_spec;
824 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000825 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000826 else
827 platform_dependent_file_spec = dependent_file_spec;
828
829 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000830 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000831 if (image_module_sp.get())
832 {
Chris Lattner24943d22010-06-08 16:52:24 +0000833 ObjectFile *objfile = image_module_sp->GetObjectFile();
834 if (objfile)
835 objfile->GetDependentModules(dependent_files);
836 }
837 }
838 }
839
Chris Lattner24943d22010-06-08 16:52:24 +0000840 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000841
842 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000843}
844
845
Jim Ingham7508e732010-08-09 23:31:02 +0000846bool
847Target::SetArchitecture (const ArchSpec &arch_spec)
848{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000849 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000850 {
851 // If we're setting the architecture to our current architecture, we
852 // don't need to do anything.
853 return true;
854 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000855 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000856 {
857 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000858 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000859 return true;
860 }
861 else
862 {
863 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000864 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000865 ModuleSP executable_sp = GetExecutableModule ();
866 m_images.Clear();
867 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000868 // Need to do something about unsetting breakpoints.
869
870 if (executable_sp)
871 {
872 FileSpec exec_file_spec = executable_sp->GetFileSpec();
873 Error error = ModuleList::GetSharedModule(exec_file_spec,
874 arch_spec,
875 NULL,
876 NULL,
877 0,
878 executable_sp,
879 NULL,
880 NULL);
881
882 if (!error.Fail() && executable_sp)
883 {
884 SetExecutableModule (executable_sp, true);
885 return true;
886 }
887 else
888 {
889 return false;
890 }
891 }
892 else
893 {
894 return false;
895 }
896 }
897}
Chris Lattner24943d22010-06-08 16:52:24 +0000898
Chris Lattner24943d22010-06-08 16:52:24 +0000899void
900Target::ModuleAdded (ModuleSP &module_sp)
901{
902 // A module is being added to this target for the first time
903 ModuleList module_list;
904 module_list.Append(module_sp);
905 ModulesDidLoad (module_list);
906}
907
908void
909Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
910{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000911 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000912 ModuleList module_list;
913 module_list.Append (old_module_sp);
914 ModulesDidUnload (module_list);
915 module_list.Clear ();
916 module_list.Append (new_module_sp);
917 ModulesDidLoad (module_list);
918}
919
920void
921Target::ModulesDidLoad (ModuleList &module_list)
922{
923 m_breakpoint_list.UpdateBreakpoints (module_list, true);
924 // TODO: make event data that packages up the module_list
925 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
926}
927
928void
929Target::ModulesDidUnload (ModuleList &module_list)
930{
931 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000932
933 // Remove the images from the target image list
934 m_images.Remove(module_list);
935
Chris Lattner24943d22010-06-08 16:52:24 +0000936 // TODO: make event data that packages up the module_list
937 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
938}
939
Jim Ingham7089d8a2011-10-28 23:14:11 +0000940
Daniel Dunbar705a0982011-10-31 22:50:37 +0000941bool
Jim Ingham7089d8a2011-10-28 23:14:11 +0000942Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_spec)
943{
944
945 if (!m_breakpoints_use_platform_avoid)
946 return false;
947 else
948 {
949 ModuleList matchingModules;
950 const ArchSpec *arch_ptr = NULL;
951 const lldb_private::UUID *uuid_ptr= NULL;
952 const ConstString *object_name = NULL;
953 size_t num_modules = GetImages().FindModules(&module_spec, arch_ptr, uuid_ptr, object_name, matchingModules);
954
955 // If there is more than one module for this file spec, only return true if ALL the modules are on the
956 // black list.
957 if (num_modules > 0)
958 {
959 for (int i = 0; i < num_modules; i++)
960 {
961 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
962 return false;
963 }
964 return true;
965 }
966 else
967 return false;
968 }
969}
970
Daniel Dunbar705a0982011-10-31 22:50:37 +0000971bool
Jim Ingham7089d8a2011-10-28 23:14:11 +0000972Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
973{
974 if (!m_breakpoints_use_platform_avoid)
975 return false;
976 else if (GetPlatform())
977 {
978 return GetPlatform()->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
979 }
980 else
981 return false;
982}
983
Chris Lattner24943d22010-06-08 16:52:24 +0000984size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000985Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
986{
987 const Section *section = addr.GetSection();
988 if (section && section->GetModule())
989 {
990 ObjectFile *objfile = section->GetModule()->GetObjectFile();
991 if (objfile)
992 {
993 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
994 addr.GetOffset(),
995 dst,
996 dst_len);
997 if (bytes_read > 0)
998 return bytes_read;
999 else
1000 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
1001 }
1002 else
1003 {
1004 error.SetErrorString("address isn't from a object file");
1005 }
1006 }
1007 else
1008 {
1009 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
1010 }
1011 return 0;
1012}
1013
1014size_t
Enrico Granata91544802011-09-06 19:20:51 +00001015Target::ReadMemory (const Address& addr,
1016 bool prefer_file_cache,
1017 void *dst,
1018 size_t dst_len,
1019 Error &error,
1020 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001021{
Chris Lattner24943d22010-06-08 16:52:24 +00001022 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001023
Enrico Granata91544802011-09-06 19:20:51 +00001024 // if we end up reading this from process memory, we will fill this
1025 // with the actual load address
1026 if (load_addr_ptr)
1027 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1028
Greg Clayton26100dc2011-01-07 01:57:07 +00001029 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001030
1031 addr_t load_addr = LLDB_INVALID_ADDRESS;
1032 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001033 Address resolved_addr;
1034 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001035 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001036 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001037 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001038 // No sections are loaded, so we must assume we are not running
1039 // yet and anything we are given is a file address.
1040 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1041 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001042 }
Greg Clayton70436352010-06-30 23:03:03 +00001043 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001044 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001045 // We have at least one section loaded. This can be becuase
1046 // we have manually loaded some sections with "target modules load ..."
1047 // or because we have have a live process that has sections loaded
1048 // through the dynamic loader
1049 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1050 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001051 }
Greg Clayton70436352010-06-30 23:03:03 +00001052 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001053 if (!resolved_addr.IsValid())
1054 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001055
Greg Clayton9b82f862011-07-11 05:12:02 +00001056
Greg Clayton26100dc2011-01-07 01:57:07 +00001057 if (prefer_file_cache)
1058 {
1059 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1060 if (bytes_read > 0)
1061 return bytes_read;
1062 }
Greg Clayton70436352010-06-30 23:03:03 +00001063
Johnny Chenda5a8022011-09-20 23:28:55 +00001064 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001065 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001066 if (load_addr == LLDB_INVALID_ADDRESS)
1067 load_addr = resolved_addr.GetLoadAddress (this);
1068
Greg Clayton70436352010-06-30 23:03:03 +00001069 if (load_addr == LLDB_INVALID_ADDRESS)
1070 {
1071 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001072 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton70436352010-06-30 23:03:03 +00001073 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001074 resolved_addr.GetFileAddress(),
1075 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001076 else
Greg Clayton9c236732011-10-26 00:56:27 +00001077 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001078 }
1079 else
1080 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001081 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001082 if (bytes_read != dst_len)
1083 {
1084 if (error.Success())
1085 {
1086 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001087 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001088 else
Greg Clayton9c236732011-10-26 00:56:27 +00001089 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 +00001090 }
1091 }
Greg Clayton70436352010-06-30 23:03:03 +00001092 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001093 {
1094 if (load_addr_ptr)
1095 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001096 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001097 }
Greg Clayton70436352010-06-30 23:03:03 +00001098 // If the address is not section offset we have an address that
1099 // doesn't resolve to any address in any currently loaded shared
1100 // libaries and we failed to read memory so there isn't anything
1101 // more we can do. If it is section offset, we might be able to
1102 // read cached memory from the object file.
1103 if (!resolved_addr.IsSectionOffset())
1104 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001105 }
Chris Lattner24943d22010-06-08 16:52:24 +00001106 }
Greg Clayton70436352010-06-30 23:03:03 +00001107
Greg Clayton9b82f862011-07-11 05:12:02 +00001108 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001109 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001110 // If we didn't already try and read from the object file cache, then
1111 // try it after failing to read from the process.
1112 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001113 }
1114 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001115}
1116
Greg Clayton7dd98df2011-07-12 17:06:17 +00001117size_t
1118Target::ReadScalarIntegerFromMemory (const Address& addr,
1119 bool prefer_file_cache,
1120 uint32_t byte_size,
1121 bool is_signed,
1122 Scalar &scalar,
1123 Error &error)
1124{
1125 uint64_t uval;
1126
1127 if (byte_size <= sizeof(uval))
1128 {
1129 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1130 if (bytes_read == byte_size)
1131 {
1132 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1133 uint32_t offset = 0;
1134 if (byte_size <= 4)
1135 scalar = data.GetMaxU32 (&offset, byte_size);
1136 else
1137 scalar = data.GetMaxU64 (&offset, byte_size);
1138
1139 if (is_signed)
1140 scalar.SignExtend(byte_size * 8);
1141 return bytes_read;
1142 }
1143 }
1144 else
1145 {
1146 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1147 }
1148 return 0;
1149}
1150
1151uint64_t
1152Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1153 bool prefer_file_cache,
1154 size_t integer_byte_size,
1155 uint64_t fail_value,
1156 Error &error)
1157{
1158 Scalar scalar;
1159 if (ReadScalarIntegerFromMemory (addr,
1160 prefer_file_cache,
1161 integer_byte_size,
1162 false,
1163 scalar,
1164 error))
1165 return scalar.ULongLong(fail_value);
1166 return fail_value;
1167}
1168
1169bool
1170Target::ReadPointerFromMemory (const Address& addr,
1171 bool prefer_file_cache,
1172 Error &error,
1173 Address &pointer_addr)
1174{
1175 Scalar scalar;
1176 if (ReadScalarIntegerFromMemory (addr,
1177 prefer_file_cache,
1178 m_arch.GetAddressByteSize(),
1179 false,
1180 scalar,
1181 error))
1182 {
1183 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1184 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1185 {
1186 if (m_section_load_list.IsEmpty())
1187 {
1188 // No sections are loaded, so we must assume we are not running
1189 // yet and anything we are given is a file address.
1190 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1191 }
1192 else
1193 {
1194 // We have at least one section loaded. This can be becuase
1195 // we have manually loaded some sections with "target modules load ..."
1196 // or because we have have a live process that has sections loaded
1197 // through the dynamic loader
1198 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1199 }
1200 // We weren't able to resolve the pointer value, so just return
1201 // an address with no section
1202 if (!pointer_addr.IsValid())
1203 pointer_addr.SetOffset (pointer_vm_addr);
1204 return true;
1205
1206 }
1207 }
1208 return false;
1209}
Chris Lattner24943d22010-06-08 16:52:24 +00001210
1211ModuleSP
1212Target::GetSharedModule
1213(
1214 const FileSpec& file_spec,
1215 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +00001216 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +00001217 const ConstString *object_name,
1218 off_t object_offset,
1219 Error *error_ptr
1220)
1221{
1222 // Don't pass in the UUID so we can tell if we have a stale value in our list
1223 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1224 bool did_create_module = false;
1225 ModuleSP module_sp;
1226
Chris Lattner24943d22010-06-08 16:52:24 +00001227 Error error;
1228
Greg Clayton24bc5d92011-03-30 18:16:51 +00001229 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +00001230 if (m_image_search_paths.GetSize())
1231 {
1232 FileSpec transformed_spec;
1233 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1234 {
1235 transformed_spec.GetFilename() = file_spec.GetFilename();
1236 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1237 }
1238 }
1239
Greg Clayton24bc5d92011-03-30 18:16:51 +00001240 // The platform is responsible for finding and caching an appropriate
1241 // module in the shared module cache.
1242 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001243 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001244 FileSpec platform_file_spec;
1245 error = m_platform_sp->GetSharedModule (file_spec,
1246 arch,
1247 uuid_ptr,
1248 object_name,
1249 object_offset,
1250 module_sp,
1251 &old_module_sp,
1252 &did_create_module);
1253 }
1254 else
1255 {
1256 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +00001257 }
1258
Greg Clayton24bc5d92011-03-30 18:16:51 +00001259 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +00001260 if (module_sp)
1261 {
1262 m_images.Append (module_sp);
1263 if (did_create_module)
1264 {
1265 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1266 ModuleUpdated(old_module_sp, module_sp);
1267 else
1268 ModuleAdded(module_sp);
1269 }
1270 }
1271 if (error_ptr)
1272 *error_ptr = error;
1273 return module_sp;
1274}
1275
1276
1277Target *
1278Target::CalculateTarget ()
1279{
1280 return this;
1281}
1282
1283Process *
1284Target::CalculateProcess ()
1285{
1286 return NULL;
1287}
1288
1289Thread *
1290Target::CalculateThread ()
1291{
1292 return NULL;
1293}
1294
1295StackFrame *
1296Target::CalculateStackFrame ()
1297{
1298 return NULL;
1299}
1300
1301void
Greg Claytona830adb2010-10-04 01:05:56 +00001302Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001303{
Greg Clayton567e7f32011-09-22 04:58:26 +00001304 exe_ctx.Clear();
1305 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001306}
1307
1308PathMappingList &
1309Target::GetImageSearchPathList ()
1310{
1311 return m_image_search_paths;
1312}
1313
1314void
1315Target::ImageSearchPathsChanged
1316(
1317 const PathMappingList &path_list,
1318 void *baton
1319)
1320{
1321 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001322 ModuleSP exe_module_sp (target->GetExecutableModule());
1323 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001324 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001325 target->m_images.Clear();
1326 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001327 }
1328}
1329
1330ClangASTContext *
1331Target::GetScratchClangASTContext()
1332{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001333 // Now see if we know the target triple, and if so, create our scratch AST context:
1334 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
Sean Callanandcf03f82011-11-15 22:27:19 +00001335 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001336 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001337 m_scratch_ast_source_ap.reset (new ClangASTSource(GetSP()));
1338 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1339 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1340 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1341 }
Chris Lattner24943d22010-06-08 16:52:24 +00001342 return m_scratch_ast_context_ap.get();
1343}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001344
Greg Clayton990de7b2010-11-18 23:32:35 +00001345void
Caroline Tice2a456812011-03-10 22:14:10 +00001346Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001347{
Greg Clayton990de7b2010-11-18 23:32:35 +00001348 UserSettingsControllerSP &usc = GetSettingsController();
1349 usc.reset (new SettingsController);
1350 UserSettingsController::InitializeSettingsController (usc,
1351 SettingsController::global_settings_table,
1352 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001353
1354 // Now call SettingsInitialize() on each 'child' setting of Target
1355 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001356}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001357
Greg Clayton990de7b2010-11-18 23:32:35 +00001358void
Caroline Tice2a456812011-03-10 22:14:10 +00001359Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001360{
Caroline Tice2a456812011-03-10 22:14:10 +00001361
1362 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1363
1364 Process::SettingsTerminate ();
1365
1366 // Now terminate Target Settings.
1367
Greg Clayton990de7b2010-11-18 23:32:35 +00001368 UserSettingsControllerSP &usc = GetSettingsController();
1369 UserSettingsController::FinalizeSettingsController (usc);
1370 usc.reset();
1371}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001372
Greg Clayton990de7b2010-11-18 23:32:35 +00001373UserSettingsControllerSP &
1374Target::GetSettingsController ()
1375{
1376 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001377 return g_settings_controller;
1378}
1379
1380ArchSpec
1381Target::GetDefaultArchitecture ()
1382{
Greg Clayton940b1032011-02-23 00:35:02 +00001383 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1384
1385 if (settings_controller_sp)
1386 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1387 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001388}
1389
1390void
Greg Clayton940b1032011-02-23 00:35:02 +00001391Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001392{
Greg Clayton940b1032011-02-23 00:35:02 +00001393 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1394
1395 if (settings_controller_sp)
1396 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001397}
1398
Greg Claytona830adb2010-10-04 01:05:56 +00001399Target *
1400Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1401{
1402 // The target can either exist in the "process" of ExecutionContext, or in
1403 // the "target_sp" member of SymbolContext. This accessor helper function
1404 // will get the target from one of these locations.
1405
1406 Target *target = NULL;
1407 if (sc_ptr != NULL)
1408 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001409 if (target == NULL && exe_ctx_ptr)
1410 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001411 return target;
1412}
1413
1414
Caroline Tice1ebef442010-09-27 00:30:10 +00001415void
1416Target::UpdateInstanceName ()
1417{
1418 StreamString sstr;
1419
Greg Clayton5beb99d2011-08-11 02:48:45 +00001420 Module *exe_module = GetExecutableModulePointer();
1421 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001422 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001423 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001424 exe_module->GetFileSpec().GetFilename().AsCString(),
1425 exe_module->GetArchitecture().GetArchitectureName());
1426 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001427 }
1428}
1429
Sean Callanan77e93942010-10-29 00:29:03 +00001430const char *
1431Target::GetExpressionPrefixContentsAsCString ()
1432{
Greg Claytonff44ab42011-04-23 02:04:55 +00001433 if (m_expr_prefix_contents_sp)
1434 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1435 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001436}
1437
Greg Clayton427f2902010-12-14 02:59:59 +00001438ExecutionResults
1439Target::EvaluateExpression
1440(
1441 const char *expr_cstr,
1442 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001443 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton427f2902010-12-14 02:59:59 +00001444 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001445 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001446 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001447 lldb::ValueObjectSP &result_valobj_sp
1448)
1449{
1450 ExecutionResults execution_results = eExecutionSetupError;
1451
1452 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001453
1454 // We shouldn't run stop hooks in expressions.
1455 // Be sure to reset this if you return anywhere within this function.
1456 bool old_suppress_value = m_suppress_stop_hooks;
1457 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001458
1459 ExecutionContext exe_ctx;
1460 if (frame)
1461 {
1462 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001463 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001464 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001465 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1466 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001467 lldb::VariableSP var_sp;
1468 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1469 use_dynamic,
1470 expr_path_options,
1471 var_sp,
1472 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001473 }
1474 else if (m_process_sp)
1475 {
1476 m_process_sp->CalculateExecutionContext(exe_ctx);
1477 }
1478 else
1479 {
1480 CalculateExecutionContext(exe_ctx);
1481 }
1482
1483 if (result_valobj_sp)
1484 {
1485 execution_results = eExecutionCompleted;
1486 // We got a result from the frame variable expression path above...
1487 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1488
1489 lldb::ValueObjectSP const_valobj_sp;
1490
1491 // Check in case our value is already a constant value
1492 if (result_valobj_sp->GetIsConstant())
1493 {
1494 const_valobj_sp = result_valobj_sp;
1495 const_valobj_sp->SetName (persistent_variable_name);
1496 }
1497 else
Jim Inghame41494a2011-04-16 00:01:13 +00001498 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001499 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001500 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001501 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001502 if (dynamic_sp)
1503 result_valobj_sp = dynamic_sp;
1504 }
1505
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001506 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001507 }
Greg Clayton427f2902010-12-14 02:59:59 +00001508
Sean Callanan6a925532011-01-13 08:53:35 +00001509 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1510
Greg Clayton427f2902010-12-14 02:59:59 +00001511 result_valobj_sp = const_valobj_sp;
1512
Sean Callanan6a925532011-01-13 08:53:35 +00001513 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1514 assert (clang_expr_variable_sp.get());
1515
1516 // Set flags and live data as appropriate
1517
1518 const Value &result_value = live_valobj_sp->GetValue();
1519
1520 switch (result_value.GetValueType())
1521 {
1522 case Value::eValueTypeHostAddress:
1523 case Value::eValueTypeFileAddress:
1524 // we don't do anything with these for now
1525 break;
1526 case Value::eValueTypeScalar:
1527 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1528 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1529 break;
1530 case Value::eValueTypeLoadAddress:
1531 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1532 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1533 break;
1534 }
Greg Clayton427f2902010-12-14 02:59:59 +00001535 }
1536 else
1537 {
1538 // Make sure we aren't just trying to see the value of a persistent
1539 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001540 lldb::ClangExpressionVariableSP persistent_var_sp;
1541 // Only check for persistent variables the expression starts with a '$'
1542 if (expr_cstr[0] == '$')
1543 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1544
Greg Clayton427f2902010-12-14 02:59:59 +00001545 if (persistent_var_sp)
1546 {
1547 result_valobj_sp = persistent_var_sp->GetValueObject ();
1548 execution_results = eExecutionCompleted;
1549 }
1550 else
1551 {
1552 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001553
Greg Clayton427f2902010-12-14 02:59:59 +00001554 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001555 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001556 lldb::eLanguageTypeUnknown,
Sean Callanan6a925532011-01-13 08:53:35 +00001557 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001558 expr_cstr,
1559 prefix,
1560 result_valobj_sp);
1561 }
1562 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001563
1564 m_suppress_stop_hooks = old_suppress_value;
1565
Greg Clayton427f2902010-12-14 02:59:59 +00001566 return execution_results;
1567}
1568
Greg Claytonc0fa5332011-05-22 22:46:53 +00001569lldb::addr_t
1570Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1571{
1572 addr_t code_addr = load_addr;
1573 switch (m_arch.GetMachine())
1574 {
1575 case llvm::Triple::arm:
1576 case llvm::Triple::thumb:
1577 switch (addr_class)
1578 {
1579 case eAddressClassData:
1580 case eAddressClassDebug:
1581 return LLDB_INVALID_ADDRESS;
1582
1583 case eAddressClassUnknown:
1584 case eAddressClassInvalid:
1585 case eAddressClassCode:
1586 case eAddressClassCodeAlternateISA:
1587 case eAddressClassRuntime:
1588 // Check if bit zero it no set?
1589 if ((code_addr & 1ull) == 0)
1590 {
1591 // Bit zero isn't set, check if the address is a multiple of 2?
1592 if (code_addr & 2ull)
1593 {
1594 // The address is a multiple of 2 so it must be thumb, set bit zero
1595 code_addr |= 1ull;
1596 }
1597 else if (addr_class == eAddressClassCodeAlternateISA)
1598 {
1599 // We checked the address and the address claims to be the alternate ISA
1600 // which means thumb, so set bit zero.
1601 code_addr |= 1ull;
1602 }
1603 }
1604 break;
1605 }
1606 break;
1607
1608 default:
1609 break;
1610 }
1611 return code_addr;
1612}
1613
1614lldb::addr_t
1615Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1616{
1617 addr_t opcode_addr = load_addr;
1618 switch (m_arch.GetMachine())
1619 {
1620 case llvm::Triple::arm:
1621 case llvm::Triple::thumb:
1622 switch (addr_class)
1623 {
1624 case eAddressClassData:
1625 case eAddressClassDebug:
1626 return LLDB_INVALID_ADDRESS;
1627
1628 case eAddressClassInvalid:
1629 case eAddressClassUnknown:
1630 case eAddressClassCode:
1631 case eAddressClassCodeAlternateISA:
1632 case eAddressClassRuntime:
1633 opcode_addr &= ~(1ull);
1634 break;
1635 }
1636 break;
1637
1638 default:
1639 break;
1640 }
1641 return opcode_addr;
1642}
1643
Jim Inghamd60d94a2011-03-11 03:53:59 +00001644lldb::user_id_t
1645Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1646{
1647 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1648 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1649 m_stop_hooks[new_uid] = new_hook_sp;
1650 return new_uid;
1651}
1652
1653bool
1654Target::RemoveStopHookByID (lldb::user_id_t user_id)
1655{
1656 size_t num_removed;
1657 num_removed = m_stop_hooks.erase (user_id);
1658 if (num_removed == 0)
1659 return false;
1660 else
1661 return true;
1662}
1663
1664void
1665Target::RemoveAllStopHooks ()
1666{
1667 m_stop_hooks.clear();
1668}
1669
1670Target::StopHookSP
1671Target::GetStopHookByID (lldb::user_id_t user_id)
1672{
1673 StopHookSP found_hook;
1674
1675 StopHookCollection::iterator specified_hook_iter;
1676 specified_hook_iter = m_stop_hooks.find (user_id);
1677 if (specified_hook_iter != m_stop_hooks.end())
1678 found_hook = (*specified_hook_iter).second;
1679 return found_hook;
1680}
1681
1682bool
1683Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1684{
1685 StopHookCollection::iterator specified_hook_iter;
1686 specified_hook_iter = m_stop_hooks.find (user_id);
1687 if (specified_hook_iter == m_stop_hooks.end())
1688 return false;
1689
1690 (*specified_hook_iter).second->SetIsActive (active_state);
1691 return true;
1692}
1693
1694void
1695Target::SetAllStopHooksActiveState (bool active_state)
1696{
1697 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1698 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1699 {
1700 (*pos).second->SetIsActive (active_state);
1701 }
1702}
1703
1704void
1705Target::RunStopHooks ()
1706{
Jim Ingham3613ae12011-05-12 02:06:14 +00001707 if (m_suppress_stop_hooks)
1708 return;
1709
Jim Inghamd60d94a2011-03-11 03:53:59 +00001710 if (!m_process_sp)
1711 return;
1712
1713 if (m_stop_hooks.empty())
1714 return;
1715
1716 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1717
1718 // If there aren't any active stop hooks, don't bother either:
1719 bool any_active_hooks = false;
1720 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1721 {
1722 if ((*pos).second->IsActive())
1723 {
1724 any_active_hooks = true;
1725 break;
1726 }
1727 }
1728 if (!any_active_hooks)
1729 return;
1730
1731 CommandReturnObject result;
1732
1733 std::vector<ExecutionContext> exc_ctx_with_reasons;
1734 std::vector<SymbolContext> sym_ctx_with_reasons;
1735
1736 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1737 size_t num_threads = cur_threadlist.GetSize();
1738 for (size_t i = 0; i < num_threads; i++)
1739 {
1740 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1741 if (cur_thread_sp->ThreadStoppedForAReason())
1742 {
1743 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1744 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1745 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1746 }
1747 }
1748
1749 // If no threads stopped for a reason, don't run the stop-hooks.
1750 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1751 if (num_exe_ctx == 0)
1752 return;
1753
Jim Inghame5ed8e92011-06-02 23:58:26 +00001754 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1755 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001756
1757 bool keep_going = true;
1758 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001759 bool print_hook_header;
1760 bool print_thread_header;
1761
1762 if (num_exe_ctx == 1)
1763 print_thread_header = false;
1764 else
1765 print_thread_header = true;
1766
1767 if (m_stop_hooks.size() == 1)
1768 print_hook_header = false;
1769 else
1770 print_hook_header = true;
1771
Jim Inghamd60d94a2011-03-11 03:53:59 +00001772 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1773 {
1774 // result.Clear();
1775 StopHookSP cur_hook_sp = (*pos).second;
1776 if (!cur_hook_sp->IsActive())
1777 continue;
1778
1779 bool any_thread_matched = false;
1780 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1781 {
1782 if ((cur_hook_sp->GetSpecifier () == NULL
1783 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1784 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Clayton567e7f32011-09-22 04:58:26 +00001785 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001786 {
1787 if (!hooks_ran)
1788 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001789 hooks_ran = true;
1790 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001791 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001792 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001793 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1794 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1795 NULL);
1796 if (cmd)
1797 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1798 else
1799 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001800 any_thread_matched = true;
1801 }
1802
Jim Inghamc54840c2011-03-22 01:47:27 +00001803 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001804 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001805
1806 bool stop_on_continue = true;
1807 bool stop_on_error = true;
1808 bool echo_commands = false;
1809 bool print_results = true;
1810 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001811 &exc_ctx_with_reasons[i],
1812 stop_on_continue,
1813 stop_on_error,
1814 echo_commands,
1815 print_results,
1816 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001817
1818 // If the command started the target going again, we should bag out of
1819 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001820 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1821 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001822 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001823 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001824 keep_going = false;
1825 }
1826 }
1827 }
1828 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00001829
Caroline Tice4a348082011-05-02 20:41:46 +00001830 result.GetImmediateOutputStream()->Flush();
1831 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001832}
1833
Greg Claytonbbea1332011-07-08 00:48:09 +00001834bool
1835Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1836{
1837 bool changed = false;
1838 if (module)
1839 {
1840 ObjectFile *object_file = module->GetObjectFile();
1841 if (object_file)
1842 {
1843 SectionList *section_list = object_file->GetSectionList ();
1844 if (section_list)
1845 {
1846 // All sections listed in the dyld image info structure will all
1847 // either be fixed up already, or they will all be off by a single
1848 // slide amount that is determined by finding the first segment
1849 // that is at file offset zero which also has bytes (a file size
1850 // that is greater than zero) in the object file.
1851
1852 // Determine the slide amount (if any)
1853 const size_t num_sections = section_list->GetSize();
1854 size_t sect_idx = 0;
1855 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1856 {
1857 // Iterate through the object file sections to find the
1858 // first section that starts of file offset zero and that
1859 // has bytes in the file...
1860 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1861 if (section)
1862 {
1863 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1864 changed = true;
1865 }
1866 }
1867 }
1868 }
1869 }
1870 return changed;
1871}
1872
1873
Jim Inghamd60d94a2011-03-11 03:53:59 +00001874//--------------------------------------------------------------
1875// class Target::StopHook
1876//--------------------------------------------------------------
1877
1878
1879Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1880 UserID (uid),
1881 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001882 m_commands (),
1883 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001884 m_thread_spec_ap(NULL),
1885 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001886{
1887}
1888
1889Target::StopHook::StopHook (const StopHook &rhs) :
1890 UserID (rhs.GetID()),
1891 m_target_sp (rhs.m_target_sp),
1892 m_commands (rhs.m_commands),
1893 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001894 m_thread_spec_ap (NULL),
1895 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001896{
1897 if (rhs.m_thread_spec_ap.get() != NULL)
1898 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1899}
1900
1901
1902Target::StopHook::~StopHook ()
1903{
1904}
1905
1906void
1907Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1908{
1909 m_thread_spec_ap.reset (specifier);
1910}
1911
1912
1913void
1914Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1915{
1916 int indent_level = s->GetIndentLevel();
1917
1918 s->SetIndentLevel(indent_level + 2);
1919
Greg Clayton444e35b2011-10-19 18:09:39 +00001920 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001921 if (m_active)
1922 s->Indent ("State: enabled\n");
1923 else
1924 s->Indent ("State: disabled\n");
1925
1926 if (m_specifier_sp)
1927 {
1928 s->Indent();
1929 s->PutCString ("Specifier:\n");
1930 s->SetIndentLevel (indent_level + 4);
1931 m_specifier_sp->GetDescription (s, level);
1932 s->SetIndentLevel (indent_level + 2);
1933 }
1934
1935 if (m_thread_spec_ap.get() != NULL)
1936 {
1937 StreamString tmp;
1938 s->Indent("Thread:\n");
1939 m_thread_spec_ap->GetDescription (&tmp, level);
1940 s->SetIndentLevel (indent_level + 4);
1941 s->Indent (tmp.GetData());
1942 s->PutCString ("\n");
1943 s->SetIndentLevel (indent_level + 2);
1944 }
1945
1946 s->Indent ("Commands: \n");
1947 s->SetIndentLevel (indent_level + 4);
1948 uint32_t num_commands = m_commands.GetSize();
1949 for (uint32_t i = 0; i < num_commands; i++)
1950 {
1951 s->Indent(m_commands.GetStringAtIndex(i));
1952 s->PutCString ("\n");
1953 }
1954 s->SetIndentLevel (indent_level);
1955}
1956
1957
Caroline Tice5bc8c972010-09-20 20:44:43 +00001958//--------------------------------------------------------------
1959// class Target::SettingsController
1960//--------------------------------------------------------------
1961
1962Target::SettingsController::SettingsController () :
1963 UserSettingsController ("target", Debugger::GetSettingsController()),
1964 m_default_architecture ()
1965{
1966 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1967 InstanceSettings::GetDefaultName().AsCString()));
1968}
1969
1970Target::SettingsController::~SettingsController ()
1971{
1972}
1973
1974lldb::InstanceSettingsSP
1975Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1976{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001977 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1978 false,
1979 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001980 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1981 return new_settings_sp;
1982}
1983
Caroline Tice5bc8c972010-09-20 20:44:43 +00001984
Greg Claytonabb33022011-11-08 02:43:13 +00001985#define TSC_DEFAULT_ARCH "default-arch"
1986#define TSC_EXPR_PREFIX "expr-prefix"
1987#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
1988#define TSC_SKIP_PROLOGUE "skip-prologue"
1989#define TSC_SOURCE_MAP "source-map"
1990#define TSC_MAX_CHILDREN "max-children-count"
1991#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
1992#define TSC_PLATFORM_AVOID "breakpoints-use-platform-avoid-list"
1993#define TSC_RUN_ARGS "run-args"
1994#define TSC_ENV_VARS "env-vars"
1995#define TSC_INHERIT_ENV "inherit-env"
1996#define TSC_STDIN_PATH "input-path"
1997#define TSC_STDOUT_PATH "output-path"
1998#define TSC_STDERR_PATH "error-path"
1999#define TSC_DISABLE_ASLR "disable-aslr"
2000#define TSC_DISABLE_STDIO "disable-stdio"
Greg Claytond284b662011-02-18 01:44:25 +00002001
2002
2003static const ConstString &
2004GetSettingNameForDefaultArch ()
2005{
2006 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00002007 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002008}
2009
Greg Claytond284b662011-02-18 01:44:25 +00002010static const ConstString &
2011GetSettingNameForExpressionPrefix ()
2012{
2013 static ConstString g_const_string (TSC_EXPR_PREFIX);
2014 return g_const_string;
2015}
2016
2017static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00002018GetSettingNameForPreferDynamicValue ()
2019{
2020 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
2021 return g_const_string;
2022}
2023
Greg Claytonff44ab42011-04-23 02:04:55 +00002024static const ConstString &
2025GetSettingNameForSourcePathMap ()
2026{
2027 static ConstString g_const_string (TSC_SOURCE_MAP);
2028 return g_const_string;
2029}
Greg Claytond284b662011-02-18 01:44:25 +00002030
Greg Clayton17cd9952011-04-22 03:55:06 +00002031static const ConstString &
2032GetSettingNameForSkipPrologue ()
2033{
2034 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
2035 return g_const_string;
2036}
2037
Enrico Granata018921d2011-08-12 02:00:06 +00002038static const ConstString &
2039GetSettingNameForMaxChildren ()
2040{
2041 static ConstString g_const_string (TSC_MAX_CHILDREN);
2042 return g_const_string;
2043}
Greg Clayton17cd9952011-04-22 03:55:06 +00002044
Enrico Granata91544802011-09-06 19:20:51 +00002045static const ConstString &
2046GetSettingNameForMaxStringSummaryLength ()
2047{
2048 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2049 return g_const_string;
2050}
Greg Clayton17cd9952011-04-22 03:55:06 +00002051
Jim Ingham7089d8a2011-10-28 23:14:11 +00002052static const ConstString &
2053GetSettingNameForPlatformAvoid ()
2054{
2055 static ConstString g_const_string (TSC_PLATFORM_AVOID);
2056 return g_const_string;
2057}
2058
Greg Claytonabb33022011-11-08 02:43:13 +00002059const ConstString &
2060GetSettingNameForRunArgs ()
2061{
2062 static ConstString g_const_string (TSC_RUN_ARGS);
2063 return g_const_string;
2064}
2065
2066const ConstString &
2067GetSettingNameForEnvVars ()
2068{
2069 static ConstString g_const_string (TSC_ENV_VARS);
2070 return g_const_string;
2071}
2072
2073const ConstString &
2074GetSettingNameForInheritHostEnv ()
2075{
2076 static ConstString g_const_string (TSC_INHERIT_ENV);
2077 return g_const_string;
2078}
2079
2080const ConstString &
2081GetSettingNameForInputPath ()
2082{
2083 static ConstString g_const_string (TSC_STDIN_PATH);
2084 return g_const_string;
2085}
2086
2087const ConstString &
2088GetSettingNameForOutputPath ()
2089{
2090 static ConstString g_const_string (TSC_STDOUT_PATH);
2091 return g_const_string;
2092}
2093
2094const ConstString &
2095GetSettingNameForErrorPath ()
2096{
2097 static ConstString g_const_string (TSC_STDERR_PATH);
2098 return g_const_string;
2099}
2100
2101const ConstString &
2102GetSettingNameForDisableASLR ()
2103{
2104 static ConstString g_const_string (TSC_DISABLE_ASLR);
2105 return g_const_string;
2106}
2107
2108const ConstString &
2109GetSettingNameForDisableSTDIO ()
2110{
2111 static ConstString g_const_string (TSC_DISABLE_STDIO);
2112 return g_const_string;
2113}
Jim Ingham7089d8a2011-10-28 23:14:11 +00002114
Caroline Tice5bc8c972010-09-20 20:44:43 +00002115bool
2116Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2117 const char *index_value,
2118 const char *value,
2119 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002120 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002121 Error&err)
2122{
Greg Claytond284b662011-02-18 01:44:25 +00002123 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002124 {
Greg Claytonf15996e2011-04-07 22:46:35 +00002125 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00002126 if (!m_default_architecture.IsValid())
2127 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002128 }
2129 return true;
2130}
2131
2132
2133bool
2134Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2135 StringList &value,
2136 Error &err)
2137{
Greg Claytond284b662011-02-18 01:44:25 +00002138 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002139 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002140 // If the arch is invalid (the default), don't show a string for it
2141 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002142 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002143 return true;
2144 }
2145 else
2146 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2147
2148 return false;
2149}
2150
2151//--------------------------------------------------------------
2152// class TargetInstanceSettings
2153//--------------------------------------------------------------
2154
Greg Clayton638351a2010-12-04 00:10:17 +00002155TargetInstanceSettings::TargetInstanceSettings
2156(
2157 UserSettingsController &owner,
2158 bool live_instance,
2159 const char *name
2160) :
Greg Claytond284b662011-02-18 01:44:25 +00002161 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002162 m_expr_prefix_file (),
2163 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002164 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00002165 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002166 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00002167 m_max_children_display(256),
Jim Ingham7089d8a2011-10-28 23:14:11 +00002168 m_max_strlen_length(1024),
Greg Claytonabb33022011-11-08 02:43:13 +00002169 m_breakpoints_use_platform_avoid (true, true),
2170 m_run_args (),
2171 m_env_vars (),
2172 m_input_path (),
2173 m_output_path (),
2174 m_error_path (),
2175 m_disable_aslr (true),
2176 m_disable_stdio (false),
2177 m_inherit_host_env (true),
2178 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002179{
2180 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2181 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2182 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2183 // This is true for CreateInstanceName() too.
2184
2185 if (GetInstanceName () == InstanceSettings::InvalidName())
2186 {
2187 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2188 m_owner.RegisterInstanceSettings (this);
2189 }
2190
2191 if (live_instance)
2192 {
2193 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2194 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002195 }
2196}
2197
2198TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00002199 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
2200 m_expr_prefix_file (rhs.m_expr_prefix_file),
2201 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
2202 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2203 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002204 m_source_map (rhs.m_source_map),
Greg Claytonabb33022011-11-08 02:43:13 +00002205 m_max_children_display (rhs.m_max_children_display),
2206 m_max_strlen_length (rhs.m_max_strlen_length),
2207 m_breakpoints_use_platform_avoid (rhs.m_breakpoints_use_platform_avoid),
2208 m_run_args (rhs.m_run_args),
2209 m_env_vars (rhs.m_env_vars),
2210 m_input_path (rhs.m_input_path),
2211 m_output_path (rhs.m_output_path),
2212 m_error_path (rhs.m_error_path),
2213 m_disable_aslr (rhs.m_disable_aslr),
2214 m_disable_stdio (rhs.m_disable_stdio),
2215 m_inherit_host_env (rhs.m_inherit_host_env)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002216{
2217 if (m_instance_name != InstanceSettings::GetDefaultName())
2218 {
2219 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2220 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002221 }
2222}
2223
2224TargetInstanceSettings::~TargetInstanceSettings ()
2225{
2226}
2227
2228TargetInstanceSettings&
2229TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2230{
2231 if (this != &rhs)
2232 {
Greg Claytonabb33022011-11-08 02:43:13 +00002233 m_expr_prefix_file = rhs.m_expr_prefix_file;
2234 m_expr_prefix_contents_sp = rhs.m_expr_prefix_contents_sp;
2235 m_prefer_dynamic_value = rhs.m_prefer_dynamic_value;
2236 m_skip_prologue = rhs.m_skip_prologue;
2237 m_source_map = rhs.m_source_map;
2238 m_max_children_display = rhs.m_max_children_display;
2239 m_max_strlen_length = rhs.m_max_strlen_length;
2240 m_breakpoints_use_platform_avoid = rhs.m_breakpoints_use_platform_avoid;
2241 m_run_args = rhs.m_run_args;
2242 m_env_vars = rhs.m_env_vars;
2243 m_input_path = rhs.m_input_path;
2244 m_output_path = rhs.m_output_path;
2245 m_error_path = rhs.m_error_path;
2246 m_disable_aslr = rhs.m_disable_aslr;
2247 m_disable_stdio = rhs.m_disable_stdio;
2248 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002249 }
2250
2251 return *this;
2252}
2253
Caroline Tice5bc8c972010-09-20 20:44:43 +00002254void
2255TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2256 const char *index_value,
2257 const char *value,
2258 const ConstString &instance_name,
2259 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002260 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002261 Error &err,
2262 bool pending)
2263{
Greg Claytond284b662011-02-18 01:44:25 +00002264 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002265 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002266 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2267 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002268 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002269 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002270 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002271 default:
2272 break;
2273 case eVarSetOperationAssign:
2274 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002275 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002276 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2277 {
2278 err.SetErrorToGenericError ();
Greg Clayton9c236732011-10-26 00:56:27 +00002279 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002280 return;
2281 }
2282
2283 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
2284
2285 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
2286 {
Greg Clayton9c236732011-10-26 00:56:27 +00002287 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002288 m_expr_prefix_contents_sp.reset();
2289 }
Sean Callanan77e93942010-10-29 00:29:03 +00002290 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002291 break;
2292 case eVarSetOperationClear:
2293 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00002294 }
Sean Callanan77e93942010-10-29 00:29:03 +00002295 }
2296 }
Jim Inghame41494a2011-04-16 00:01:13 +00002297 else if (var_name == GetSettingNameForPreferDynamicValue())
2298 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002299 int new_value;
2300 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2301 if (err.Success())
2302 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002303 }
2304 else if (var_name == GetSettingNameForSkipPrologue())
2305 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002306 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2307 }
Enrico Granata018921d2011-08-12 02:00:06 +00002308 else if (var_name == GetSettingNameForMaxChildren())
2309 {
2310 bool ok;
2311 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2312 if (ok)
2313 m_max_children_display = new_value;
2314 }
Enrico Granata91544802011-09-06 19:20:51 +00002315 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2316 {
2317 bool ok;
2318 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2319 if (ok)
2320 m_max_strlen_length = new_value;
2321 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002322 else if (var_name == GetSettingNameForSourcePathMap ())
2323 {
2324 switch (op)
2325 {
2326 case eVarSetOperationReplace:
2327 case eVarSetOperationInsertBefore:
2328 case eVarSetOperationInsertAfter:
2329 case eVarSetOperationRemove:
2330 default:
2331 break;
2332 case eVarSetOperationAssign:
2333 m_source_map.Clear(true);
2334 // Fall through to append....
2335 case eVarSetOperationAppend:
2336 {
2337 Args args(value);
2338 const uint32_t argc = args.GetArgumentCount();
2339 if (argc & 1 || argc == 0)
2340 {
2341 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2342 }
2343 else
2344 {
2345 char resolved_new_path[PATH_MAX];
2346 FileSpec file_spec;
2347 const char *old_path;
2348 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2349 {
2350 const char *new_path = args.GetArgumentAtIndex(idx+1);
2351 assert (new_path); // We have an even number of paths, this shouldn't happen!
2352
2353 file_spec.SetFile(new_path, true);
2354 if (file_spec.Exists())
2355 {
2356 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2357 {
2358 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2359 return;
2360 }
2361 }
2362 else
2363 {
2364 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2365 return;
2366 }
2367 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2368 }
2369 }
2370 }
2371 break;
2372
2373 case eVarSetOperationClear:
2374 m_source_map.Clear(true);
2375 break;
2376 }
Jim Inghame41494a2011-04-16 00:01:13 +00002377 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002378 else if (var_name == GetSettingNameForPlatformAvoid ())
2379 {
2380 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_breakpoints_use_platform_avoid);
2381 }
Greg Claytonabb33022011-11-08 02:43:13 +00002382 else if (var_name == GetSettingNameForRunArgs())
2383 {
2384 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2385 }
2386 else if (var_name == GetSettingNameForEnvVars())
2387 {
2388 // This is nice for local debugging, but it is isn't correct for
2389 // remote debugging. We need to stop process.env-vars from being
2390 // populated with the host environment and add this as a launch option
2391 // and get the correct environment from the Target's platform.
2392 // GetHostEnvironmentIfNeeded ();
2393 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2394 }
2395 else if (var_name == GetSettingNameForInputPath())
2396 {
2397 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2398 }
2399 else if (var_name == GetSettingNameForOutputPath())
2400 {
2401 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2402 }
2403 else if (var_name == GetSettingNameForErrorPath())
2404 {
2405 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2406 }
2407 else if (var_name == GetSettingNameForDisableASLR())
2408 {
2409 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
2410 }
2411 else if (var_name == GetSettingNameForDisableSTDIO ())
2412 {
2413 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
2414 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002415}
2416
2417void
Greg Claytond284b662011-02-18 01:44:25 +00002418TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002419{
Sean Callanan77e93942010-10-29 00:29:03 +00002420 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2421
2422 if (!new_settings_ptr)
2423 return;
2424
Greg Claytonabb33022011-11-08 02:43:13 +00002425 *this = *new_settings_ptr;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002426}
2427
Caroline Ticebcb5b452010-09-20 21:37:42 +00002428bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002429TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2430 const ConstString &var_name,
2431 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002432 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002433{
Greg Claytond284b662011-02-18 01:44:25 +00002434 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002435 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002436 char path[PATH_MAX];
2437 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2438 if (path_len > 0)
2439 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002440 }
Jim Inghame41494a2011-04-16 00:01:13 +00002441 else if (var_name == GetSettingNameForPreferDynamicValue())
2442 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002443 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002444 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002445 else if (var_name == GetSettingNameForSkipPrologue())
2446 {
2447 if (m_skip_prologue)
2448 value.AppendString ("true");
2449 else
2450 value.AppendString ("false");
2451 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002452 else if (var_name == GetSettingNameForSourcePathMap ())
2453 {
2454 }
Enrico Granata018921d2011-08-12 02:00:06 +00002455 else if (var_name == GetSettingNameForMaxChildren())
2456 {
2457 StreamString count_str;
2458 count_str.Printf ("%d", m_max_children_display);
2459 value.AppendString (count_str.GetData());
2460 }
Enrico Granata91544802011-09-06 19:20:51 +00002461 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2462 {
2463 StreamString count_str;
2464 count_str.Printf ("%d", m_max_strlen_length);
2465 value.AppendString (count_str.GetData());
2466 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002467 else if (var_name == GetSettingNameForPlatformAvoid())
2468 {
2469 if (m_breakpoints_use_platform_avoid)
2470 value.AppendString ("true");
2471 else
2472 value.AppendString ("false");
2473 }
Greg Claytonabb33022011-11-08 02:43:13 +00002474 else if (var_name == GetSettingNameForRunArgs())
2475 {
2476 if (m_run_args.GetArgumentCount() > 0)
2477 {
2478 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2479 value.AppendString (m_run_args.GetArgumentAtIndex (i));
2480 }
2481 }
2482 else if (var_name == GetSettingNameForEnvVars())
2483 {
2484 GetHostEnvironmentIfNeeded ();
2485
2486 if (m_env_vars.size() > 0)
2487 {
2488 std::map<std::string, std::string>::iterator pos;
2489 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2490 {
2491 StreamString value_str;
2492 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2493 value.AppendString (value_str.GetData());
2494 }
2495 }
2496 }
2497 else if (var_name == GetSettingNameForInputPath())
2498 {
2499 value.AppendString (m_input_path.c_str());
2500 }
2501 else if (var_name == GetSettingNameForOutputPath())
2502 {
2503 value.AppendString (m_output_path.c_str());
2504 }
2505 else if (var_name == GetSettingNameForErrorPath())
2506 {
2507 value.AppendString (m_error_path.c_str());
2508 }
2509 else if (var_name == GetSettingNameForInheritHostEnv())
2510 {
2511 if (m_inherit_host_env)
2512 value.AppendString ("true");
2513 else
2514 value.AppendString ("false");
2515 }
2516 else if (var_name == GetSettingNameForDisableASLR())
2517 {
2518 if (m_disable_aslr)
2519 value.AppendString ("true");
2520 else
2521 value.AppendString ("false");
2522 }
2523 else if (var_name == GetSettingNameForDisableSTDIO())
2524 {
2525 if (m_disable_stdio)
2526 value.AppendString ("true");
2527 else
2528 value.AppendString ("false");
2529 }
Sean Callanan77e93942010-10-29 00:29:03 +00002530 else
2531 {
2532 if (err)
2533 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2534 return false;
2535 }
Sean Callanan77e93942010-10-29 00:29:03 +00002536 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002537}
2538
Greg Claytonabb33022011-11-08 02:43:13 +00002539void
2540Target::TargetInstanceSettings::GetHostEnvironmentIfNeeded ()
2541{
2542 if (m_inherit_host_env && !m_got_host_env)
2543 {
2544 m_got_host_env = true;
2545 StringList host_env;
2546 const size_t host_env_count = Host::GetEnvironment (host_env);
2547 for (size_t idx=0; idx<host_env_count; idx++)
2548 {
2549 const char *env_entry = host_env.GetStringAtIndex (idx);
2550 if (env_entry)
2551 {
2552 const char *equal_pos = ::strchr(env_entry, '=');
2553 if (equal_pos)
2554 {
2555 std::string key (env_entry, equal_pos - env_entry);
2556 std::string value (equal_pos + 1);
2557 if (m_env_vars.find (key) == m_env_vars.end())
2558 m_env_vars[key] = value;
2559 }
2560 }
2561 }
2562 }
2563}
2564
2565
2566size_t
2567Target::TargetInstanceSettings::GetEnvironmentAsArgs (Args &env)
2568{
2569 GetHostEnvironmentIfNeeded ();
2570
2571 dictionary::const_iterator pos, end = m_env_vars.end();
2572 for (pos = m_env_vars.begin(); pos != end; ++pos)
2573 {
2574 std::string env_var_equal_value (pos->first);
2575 env_var_equal_value.append(1, '=');
2576 env_var_equal_value.append (pos->second);
2577 env.AppendArgument (env_var_equal_value.c_str());
2578 }
2579 return env.GetArgumentCount();
2580}
2581
2582
Caroline Tice5bc8c972010-09-20 20:44:43 +00002583const ConstString
2584TargetInstanceSettings::CreateInstanceName ()
2585{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002586 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002587 static int instance_count = 1;
2588
Caroline Tice5bc8c972010-09-20 20:44:43 +00002589 sstr.Printf ("target_%d", instance_count);
2590 ++instance_count;
2591
2592 const ConstString ret_val (sstr.GetData());
2593 return ret_val;
2594}
2595
2596//--------------------------------------------------
2597// Target::SettingsController Variable Tables
2598//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002599OptionEnumValueElement
2600TargetInstanceSettings::g_dynamic_value_types[] =
2601{
Greg Clayton577fbc32011-05-30 00:39:48 +00002602{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2603{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2604{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002605{ 0, NULL, NULL }
2606};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002607
2608SettingEntry
2609Target::SettingsController::global_settings_table[] =
2610{
Greg Claytond284b662011-02-18 01:44:25 +00002611 // var-name var-type default enum init'd hidden help-text
2612 // ================= ================== =========== ==== ====== ====== =========================================================================
2613 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2614 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2615};
2616
Caroline Tice5bc8c972010-09-20 20:44:43 +00002617SettingEntry
2618Target::SettingsController::instance_settings_table[] =
2619{
Enrico Granata91544802011-09-06 19:20:51 +00002620 // var-name var-type default enum init'd hidden help-text
2621 // ================= ================== =============== ======================= ====== ====== =========================================================================
2622 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2623 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2624 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2625 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2626 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2627 { 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 +00002628 { 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 +00002629 { TSC_RUN_ARGS , eSetVarTypeArray , NULL , NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2630 { 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." },
2631 { TSC_INHERIT_ENV , eSetVarTypeBoolean, "true" , NULL, false, false, "Inherit the environment from the process that is running LLDB." },
2632 { TSC_STDIN_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for reading its standard input." },
2633 { TSC_STDOUT_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard output." },
2634 { TSC_STDERR_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard error." },
2635// { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
2636 { TSC_DISABLE_ASLR , eSetVarTypeBoolean, "true" , NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2637 { 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 +00002638 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002639};