blob: 3ff461d6173a637a40900fc18cbb6c446109f08f [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),
Sean Callanan4938bd62011-11-16 18:20:47 +000064 m_scratch_ast_source_ap (NULL),
65 m_ast_importer_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000066 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000067 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000068 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000069 m_stop_hook_next_id (0),
70 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000071{
Greg Clayton49ce6822010-10-31 03:01:06 +000072 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
73 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
74 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
75
Greg Claytone005f2c2010-11-06 01:53:30 +000076 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000077 if (log)
78 log->Printf ("%p Target::Target()", this);
79}
80
81//----------------------------------------------------------------------
82// Destructor
83//----------------------------------------------------------------------
84Target::~Target()
85{
Greg Claytone005f2c2010-11-06 01:53:30 +000086 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000087 if (log)
88 log->Printf ("%p Target::~Target()", this);
89 DeleteCurrentProcess ();
90}
91
92void
Caroline Tice7826c882010-10-26 03:11:13 +000093Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000094{
Greg Clayton3fed8b92010-10-08 00:21:05 +000095// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000096 if (description_level != lldb::eDescriptionLevelBrief)
97 {
98 s->Indent();
99 s->PutCString("Target\n");
100 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000101 m_images.Dump(s);
102 m_breakpoint_list.Dump(s);
103 m_internal_breakpoint_list.Dump(s);
104 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000105 }
106 else
107 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000108 Module *exe_module = GetExecutableModulePointer();
109 if (exe_module)
110 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000111 else
112 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000113 }
Chris Lattner24943d22010-06-08 16:52:24 +0000114}
115
116void
117Target::DeleteCurrentProcess ()
118{
119 if (m_process_sp.get())
120 {
Greg Clayton49480b12010-09-14 23:52:43 +0000121 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000122 if (m_process_sp->IsAlive())
123 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000124
125 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000126
127 // Do any cleanup of the target we need to do between process instances.
128 // NB It is better to do this before destroying the process in case the
129 // clean up needs some help from the process.
130 m_breakpoint_list.ClearAllBreakpointSites();
131 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000132 // Disable watchpoints just on the debugger side.
133 DisableAllWatchpoints(false);
Chris Lattner24943d22010-06-08 16:52:24 +0000134 m_process_sp.reset();
135 }
136}
137
138const lldb::ProcessSP &
139Target::CreateProcess (Listener &listener, const char *plugin_name)
140{
141 DeleteCurrentProcess ();
142 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
143 return m_process_sp;
144}
145
146const lldb::ProcessSP &
147Target::GetProcessSP () const
148{
149 return m_process_sp;
150}
151
152lldb::TargetSP
153Target::GetSP()
154{
Greg Clayton987c7eb2011-09-17 08:33:22 +0000155 // This object contains an instrusive ref count base class so we can
156 // easily make a shared pointer to this object
157 return TargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000158}
159
Greg Clayton153ccd72011-08-10 02:10:13 +0000160void
161Target::Destroy()
162{
163 Mutex::Locker locker (m_mutex);
164 DeleteCurrentProcess ();
165 m_platform_sp.reset();
166 m_arch.Clear();
167 m_images.Clear();
168 m_section_load_list.Clear();
169 const bool notify = false;
170 m_breakpoint_list.RemoveAll(notify);
171 m_internal_breakpoint_list.RemoveAll(notify);
172 m_last_created_breakpoint.reset();
Johnny Chenecd4feb2011-10-14 00:42:25 +0000173 m_last_created_watchpoint.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000174 m_search_filter_sp.reset();
175 m_image_search_paths.Clear(notify);
176 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000177 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000178 m_ast_importer_ap.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000179 m_persistent_variables.Clear();
180 m_stop_hooks.clear();
181 m_stop_hook_next_id = 0;
182 m_suppress_stop_hooks = false;
183}
184
185
Chris Lattner24943d22010-06-08 16:52:24 +0000186BreakpointList &
187Target::GetBreakpointList(bool internal)
188{
189 if (internal)
190 return m_internal_breakpoint_list;
191 else
192 return m_breakpoint_list;
193}
194
195const BreakpointList &
196Target::GetBreakpointList(bool internal) const
197{
198 if (internal)
199 return m_internal_breakpoint_list;
200 else
201 return m_breakpoint_list;
202}
203
204BreakpointSP
205Target::GetBreakpointByID (break_id_t break_id)
206{
207 BreakpointSP bp_sp;
208
209 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
210 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
211 else
212 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
213
214 return bp_sp;
215}
216
217BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000218Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
219 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000220 RegularExpression &source_regex,
221 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000222{
Jim Inghamd6d47972011-09-23 00:54:11 +0000223 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
224 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000225 return CreateBreakpoint (filter_sp, resolver_sp, internal);
226}
227
228
229BreakpointSP
230Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
231{
232 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner24943d22010-06-08 16:52:24 +0000233 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
234 return CreateBreakpoint (filter_sp, resolver_sp, internal);
235}
236
237
238BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000239Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000240{
Chris Lattner24943d22010-06-08 16:52:24 +0000241 Address so_addr;
242 // Attempt to resolve our load address if possible, though it is ok if
243 // it doesn't resolve to section/offset.
244
Greg Clayton33ed1702010-08-24 00:45:41 +0000245 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000246 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000247 if (!so_addr.IsValid())
248 {
249 // The address didn't resolve, so just set this as an absolute address
250 so_addr.SetOffset (addr);
251 }
252 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000253 return bp_sp;
254}
255
256BreakpointSP
257Target::CreateBreakpoint (Address &addr, bool internal)
258{
259 TargetSP target_sp = this->GetSP();
Jim Ingham7089d8a2011-10-28 23:14:11 +0000260 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (target_sp));
Chris Lattner24943d22010-06-08 16:52:24 +0000261 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
262 return CreateBreakpoint (filter_sp, resolver_sp, internal);
263}
264
265BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000266Target::CreateBreakpoint (const FileSpecList *containingModules,
267 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000268 const char *func_name,
269 uint32_t func_name_type_mask,
270 bool internal,
271 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000272{
Greg Clayton12bec712010-06-28 21:30:43 +0000273 BreakpointSP bp_sp;
274 if (func_name)
275 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000276 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000277
278 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
279 func_name,
280 func_name_type_mask,
281 Breakpoint::Exact,
282 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000283 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
284 }
285 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000286}
287
288
289SearchFilterSP
290Target::GetSearchFilterForModule (const FileSpec *containingModule)
291{
292 SearchFilterSP filter_sp;
293 lldb::TargetSP target_sp = this->GetSP();
294 if (containingModule != NULL)
295 {
296 // TODO: We should look into sharing module based search filters
297 // across many breakpoints like we do for the simple target based one
298 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
299 }
300 else
301 {
302 if (m_search_filter_sp.get() == NULL)
Jim Ingham7089d8a2011-10-28 23:14:11 +0000303 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (target_sp));
Chris Lattner24943d22010-06-08 16:52:24 +0000304 filter_sp = m_search_filter_sp;
305 }
306 return filter_sp;
307}
308
Jim Ingham03c8ee52011-09-21 01:17:13 +0000309SearchFilterSP
310Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
311{
312 SearchFilterSP filter_sp;
313 lldb::TargetSP target_sp = this->GetSP();
314 if (containingModules && containingModules->GetSize() != 0)
315 {
316 // TODO: We should look into sharing module based search filters
317 // across many breakpoints like we do for the simple target based one
318 filter_sp.reset (new SearchFilterByModuleList (target_sp, *containingModules));
319 }
320 else
321 {
322 if (m_search_filter_sp.get() == NULL)
Jim Ingham7089d8a2011-10-28 23:14:11 +0000323 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (target_sp));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000324 filter_sp = m_search_filter_sp;
325 }
326 return filter_sp;
327}
328
Jim Inghamd6d47972011-09-23 00:54:11 +0000329SearchFilterSP
330Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles)
331{
332 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
333 return GetSearchFilterForModuleList(containingModules);
334
335 SearchFilterSP filter_sp;
336 lldb::TargetSP target_sp = this->GetSP();
337 if (containingModules == NULL)
338 {
339 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
340 // but that will take a little reworking.
341
342 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, FileSpecList(), *containingSourceFiles));
343 }
344 else
345 {
346 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, *containingModules, *containingSourceFiles));
347 }
348 return filter_sp;
349}
350
Chris Lattner24943d22010-06-08 16:52:24 +0000351BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000352Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
353 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000354 RegularExpression &func_regex,
355 bool internal,
356 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000357{
Jim Inghamd6d47972011-09-23 00:54:11 +0000358 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000359 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
360 func_regex,
361 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000362
363 return CreateBreakpoint (filter_sp, resolver_sp, internal);
364}
365
366BreakpointSP
367Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
368{
369 BreakpointSP bp_sp;
370 if (filter_sp && resolver_sp)
371 {
372 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
373 resolver_sp->SetBreakpoint (bp_sp.get());
374
375 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000376 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000377 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000378 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000379
Greg Claytone005f2c2010-11-06 01:53:30 +0000380 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000381 if (log)
382 {
383 StreamString s;
384 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
385 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
386 }
387
Chris Lattner24943d22010-06-08 16:52:24 +0000388 bp_sp->ResolveBreakpoint();
389 }
Jim Inghamd1686902010-10-14 23:45:03 +0000390
391 if (!internal && bp_sp)
392 {
393 m_last_created_breakpoint = bp_sp;
394 }
395
Chris Lattner24943d22010-06-08 16:52:24 +0000396 return bp_sp;
397}
398
Johnny Chenda5a8022011-09-20 23:28:55 +0000399bool
400Target::ProcessIsValid()
401{
402 return (m_process_sp && m_process_sp->IsAlive());
403}
404
Johnny Chenecd4feb2011-10-14 00:42:25 +0000405// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000406// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000407WatchpointSP
408Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type)
Johnny Chen34bbf852011-09-12 23:38:44 +0000409{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000410 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
411 if (log)
412 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
413 __FUNCTION__, addr, size, type);
414
Johnny Chenecd4feb2011-10-14 00:42:25 +0000415 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000416 if (!ProcessIsValid())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000417 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000418 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000419 return wp_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000420
Johnny Chenecd4feb2011-10-14 00:42:25 +0000421 // Currently we only support one watchpoint per address, with total number
422 // of watchpoints limited by the hardware which the inferior is running on.
423 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000424 if (matched_sp)
425 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000426 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000427 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000428 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
429 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000430 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000431 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000432 wp_sp = matched_sp;
433 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000434 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000435 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000436 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000437 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000438 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000439 }
440
Johnny Chenecd4feb2011-10-14 00:42:25 +0000441 if (!wp_sp) {
442 Watchpoint *new_wp = new Watchpoint(addr, size);
443 if (!new_wp) {
444 printf("Watchpoint ctor failed, out of memory?\n");
445 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000446 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000447 new_wp->SetWatchpointType(type);
448 new_wp->SetTarget(this);
449 wp_sp.reset(new_wp);
450 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000451 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000452
Johnny Chenecd4feb2011-10-14 00:42:25 +0000453 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000454 if (log)
455 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
456 __FUNCTION__,
457 rc.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000458 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000459
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000460 if (rc.Fail())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000461 wp_sp.reset();
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000462 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000463 m_last_created_watchpoint = wp_sp;
464 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000465}
466
Chris Lattner24943d22010-06-08 16:52:24 +0000467void
468Target::RemoveAllBreakpoints (bool internal_also)
469{
Greg Claytone005f2c2010-11-06 01:53:30 +0000470 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000471 if (log)
472 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
473
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000474 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000475 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000476 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000477
478 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000479}
480
481void
482Target::DisableAllBreakpoints (bool internal_also)
483{
Greg Claytone005f2c2010-11-06 01:53:30 +0000484 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000485 if (log)
486 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
487
488 m_breakpoint_list.SetEnabledAll (false);
489 if (internal_also)
490 m_internal_breakpoint_list.SetEnabledAll (false);
491}
492
493void
494Target::EnableAllBreakpoints (bool internal_also)
495{
Greg Claytone005f2c2010-11-06 01:53:30 +0000496 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000497 if (log)
498 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
499
500 m_breakpoint_list.SetEnabledAll (true);
501 if (internal_also)
502 m_internal_breakpoint_list.SetEnabledAll (true);
503}
504
505bool
506Target::RemoveBreakpointByID (break_id_t break_id)
507{
Greg Claytone005f2c2010-11-06 01:53:30 +0000508 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000509 if (log)
510 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
511
512 if (DisableBreakpointByID (break_id))
513 {
514 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000515 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000516 else
Jim Inghamd1686902010-10-14 23:45:03 +0000517 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000518 if (m_last_created_breakpoint)
519 {
520 if (m_last_created_breakpoint->GetID() == break_id)
521 m_last_created_breakpoint.reset();
522 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000523 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000524 }
Chris Lattner24943d22010-06-08 16:52:24 +0000525 return true;
526 }
527 return false;
528}
529
530bool
531Target::DisableBreakpointByID (break_id_t break_id)
532{
Greg Claytone005f2c2010-11-06 01:53:30 +0000533 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000534 if (log)
535 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
536
537 BreakpointSP bp_sp;
538
539 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
540 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
541 else
542 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
543 if (bp_sp)
544 {
545 bp_sp->SetEnabled (false);
546 return true;
547 }
548 return false;
549}
550
551bool
552Target::EnableBreakpointByID (break_id_t break_id)
553{
Greg Claytone005f2c2010-11-06 01:53:30 +0000554 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000555 if (log)
556 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
557 __FUNCTION__,
558 break_id,
559 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
560
561 BreakpointSP bp_sp;
562
563 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
564 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
565 else
566 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
567
568 if (bp_sp)
569 {
570 bp_sp->SetEnabled (true);
571 return true;
572 }
573 return false;
574}
575
Johnny Chenc86582f2011-09-23 21:21:43 +0000576// The flag 'end_to_end', default to true, signifies that the operation is
577// performed end to end, for both the debugger and the debuggee.
578
Johnny Chenecd4feb2011-10-14 00:42:25 +0000579// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
580// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000581bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000582Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000583{
584 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
585 if (log)
586 log->Printf ("Target::%s\n", __FUNCTION__);
587
Johnny Chenc86582f2011-09-23 21:21:43 +0000588 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000589 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000590 return true;
591 }
592
593 // Otherwise, it's an end to end operation.
594
Johnny Chenda5a8022011-09-20 23:28:55 +0000595 if (!ProcessIsValid())
596 return false;
597
Johnny Chenecd4feb2011-10-14 00:42:25 +0000598 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000599 for (size_t i = 0; i < num_watchpoints; ++i)
600 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000601 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
602 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000603 return false;
604
Johnny Chenecd4feb2011-10-14 00:42:25 +0000605 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000606 if (rc.Fail())
607 return false;
608 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000609 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000610 return true; // Success!
611}
612
Johnny Chenecd4feb2011-10-14 00:42:25 +0000613// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
614// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000615bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000616Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000617{
618 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
619 if (log)
620 log->Printf ("Target::%s\n", __FUNCTION__);
621
Johnny Chenc86582f2011-09-23 21:21:43 +0000622 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000623 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000624 return true;
625 }
626
627 // Otherwise, it's an end to end operation.
628
Johnny Chenda5a8022011-09-20 23:28:55 +0000629 if (!ProcessIsValid())
630 return false;
631
Johnny Chenecd4feb2011-10-14 00:42:25 +0000632 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000633 for (size_t i = 0; i < num_watchpoints; ++i)
634 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000635 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
636 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000637 return false;
638
Johnny Chenecd4feb2011-10-14 00:42:25 +0000639 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000640 if (rc.Fail())
641 return false;
642 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000643 return true; // Success!
644}
645
Johnny Chenecd4feb2011-10-14 00:42:25 +0000646// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
647// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000648bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000649Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000650{
651 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
652 if (log)
653 log->Printf ("Target::%s\n", __FUNCTION__);
654
Johnny Chenc86582f2011-09-23 21:21:43 +0000655 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000656 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000657 return true;
658 }
659
660 // Otherwise, it's an end to end operation.
661
Johnny Chenda5a8022011-09-20 23:28:55 +0000662 if (!ProcessIsValid())
663 return false;
664
Johnny Chenecd4feb2011-10-14 00:42:25 +0000665 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000666 for (size_t i = 0; i < num_watchpoints; ++i)
667 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000668 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
669 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000670 return false;
671
Johnny Chenecd4feb2011-10-14 00:42:25 +0000672 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000673 if (rc.Fail())
674 return false;
675 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000676 return true; // Success!
677}
678
Johnny Chenecd4feb2011-10-14 00:42:25 +0000679// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000680// during these operations.
681bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000682Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000683{
684 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
685 if (log)
686 log->Printf ("Target::%s\n", __FUNCTION__);
687
688 if (!ProcessIsValid())
689 return false;
690
Johnny Chenecd4feb2011-10-14 00:42:25 +0000691 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000692 for (size_t i = 0; i < num_watchpoints; ++i)
693 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000694 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
695 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000696 return false;
697
Johnny Chenecd4feb2011-10-14 00:42:25 +0000698 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000699 }
700 return true; // Success!
701}
702
Johnny Chenecd4feb2011-10-14 00:42:25 +0000703// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000704bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000705Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000706{
707 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
708 if (log)
709 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
710
711 if (!ProcessIsValid())
712 return false;
713
Johnny Chenecd4feb2011-10-14 00:42:25 +0000714 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
715 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000716 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000717 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000718 if (rc.Success())
719 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000720
Johnny Chen01acfa72011-09-22 18:04:58 +0000721 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000722 }
723 return false;
724}
725
Johnny Chenecd4feb2011-10-14 00:42:25 +0000726// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000727bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000728Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000729{
730 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
731 if (log)
732 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
733
734 if (!ProcessIsValid())
735 return false;
736
Johnny Chenecd4feb2011-10-14 00:42:25 +0000737 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
738 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000739 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000740 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000741 if (rc.Success())
742 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000743
Johnny Chen01acfa72011-09-22 18:04:58 +0000744 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000745 }
746 return false;
747}
748
Johnny Chenecd4feb2011-10-14 00:42:25 +0000749// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000750bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000751Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000752{
753 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
754 if (log)
755 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
756
Johnny Chenecd4feb2011-10-14 00:42:25 +0000757 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000758 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000759 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000760 return true;
761 }
762 return false;
763}
764
Johnny Chenecd4feb2011-10-14 00:42:25 +0000765// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000766bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000767Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000768{
769 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
770 if (log)
771 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
772
773 if (!ProcessIsValid())
774 return false;
775
Johnny Chenecd4feb2011-10-14 00:42:25 +0000776 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
777 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000778 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000779 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000780 return true;
781 }
782 return false;
783}
784
Chris Lattner24943d22010-06-08 16:52:24 +0000785ModuleSP
786Target::GetExecutableModule ()
787{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000788 return m_images.GetModuleAtIndex(0);
789}
790
791Module*
792Target::GetExecutableModulePointer ()
793{
794 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000795}
796
797void
798Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
799{
800 m_images.Clear();
801 m_scratch_ast_context_ap.reset();
Sean Callanandcf03f82011-11-15 22:27:19 +0000802 m_scratch_ast_source_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000803 m_ast_importer_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000804
805 if (executable_sp.get())
806 {
807 Timer scoped_timer (__PRETTY_FUNCTION__,
808 "Target::SetExecutableModule (executable = '%s/%s')",
809 executable_sp->GetFileSpec().GetDirectory().AsCString(),
810 executable_sp->GetFileSpec().GetFilename().AsCString());
811
812 m_images.Append(executable_sp); // The first image is our exectuable file
813
Jim Ingham7508e732010-08-09 23:31:02 +0000814 // 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 +0000815 if (!m_arch.IsValid())
816 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000817
Chris Lattner24943d22010-06-08 16:52:24 +0000818 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000819 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000820
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000821 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000822 {
823 executable_objfile->GetDependentModules(dependent_files);
824 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
825 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000826 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
827 FileSpec platform_dependent_file_spec;
828 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000829 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000830 else
831 platform_dependent_file_spec = dependent_file_spec;
832
833 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000834 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000835 if (image_module_sp.get())
836 {
Chris Lattner24943d22010-06-08 16:52:24 +0000837 ObjectFile *objfile = image_module_sp->GetObjectFile();
838 if (objfile)
839 objfile->GetDependentModules(dependent_files);
840 }
841 }
842 }
843
Sean Callanan4938bd62011-11-16 18:20:47 +0000844 m_ast_importer_ap.reset(new ClangASTImporter());
Chris Lattner24943d22010-06-08 16:52:24 +0000845 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000846
847 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000848}
849
850
Jim Ingham7508e732010-08-09 23:31:02 +0000851bool
852Target::SetArchitecture (const ArchSpec &arch_spec)
853{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000854 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000855 {
856 // If we're setting the architecture to our current architecture, we
857 // don't need to do anything.
858 return true;
859 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000860 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000861 {
862 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000863 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000864 return true;
865 }
866 else
867 {
868 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000869 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000870 ModuleSP executable_sp = GetExecutableModule ();
871 m_images.Clear();
872 m_scratch_ast_context_ap.reset();
Sean Callanan4938bd62011-11-16 18:20:47 +0000873 m_scratch_ast_source_ap.reset();
874 m_ast_importer_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000875 // Need to do something about unsetting breakpoints.
876
877 if (executable_sp)
878 {
879 FileSpec exec_file_spec = executable_sp->GetFileSpec();
880 Error error = ModuleList::GetSharedModule(exec_file_spec,
881 arch_spec,
882 NULL,
883 NULL,
884 0,
885 executable_sp,
886 NULL,
887 NULL);
888
889 if (!error.Fail() && executable_sp)
890 {
891 SetExecutableModule (executable_sp, true);
892 return true;
893 }
894 else
895 {
896 return false;
897 }
898 }
899 else
900 {
901 return false;
902 }
903 }
904}
Chris Lattner24943d22010-06-08 16:52:24 +0000905
Chris Lattner24943d22010-06-08 16:52:24 +0000906void
907Target::ModuleAdded (ModuleSP &module_sp)
908{
909 // A module is being added to this target for the first time
910 ModuleList module_list;
911 module_list.Append(module_sp);
912 ModulesDidLoad (module_list);
913}
914
915void
916Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
917{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000918 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000919 ModuleList module_list;
920 module_list.Append (old_module_sp);
921 ModulesDidUnload (module_list);
922 module_list.Clear ();
923 module_list.Append (new_module_sp);
924 ModulesDidLoad (module_list);
925}
926
927void
928Target::ModulesDidLoad (ModuleList &module_list)
929{
930 m_breakpoint_list.UpdateBreakpoints (module_list, true);
931 // TODO: make event data that packages up the module_list
932 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
933}
934
935void
936Target::ModulesDidUnload (ModuleList &module_list)
937{
938 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000939
940 // Remove the images from the target image list
941 m_images.Remove(module_list);
942
Chris Lattner24943d22010-06-08 16:52:24 +0000943 // TODO: make event data that packages up the module_list
944 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
945}
946
Jim Ingham7089d8a2011-10-28 23:14:11 +0000947
Daniel Dunbar705a0982011-10-31 22:50:37 +0000948bool
Jim Ingham7089d8a2011-10-28 23:14:11 +0000949Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_spec)
950{
951
952 if (!m_breakpoints_use_platform_avoid)
953 return false;
954 else
955 {
956 ModuleList matchingModules;
957 const ArchSpec *arch_ptr = NULL;
958 const lldb_private::UUID *uuid_ptr= NULL;
959 const ConstString *object_name = NULL;
960 size_t num_modules = GetImages().FindModules(&module_spec, arch_ptr, uuid_ptr, object_name, matchingModules);
961
962 // If there is more than one module for this file spec, only return true if ALL the modules are on the
963 // black list.
964 if (num_modules > 0)
965 {
966 for (int i = 0; i < num_modules; i++)
967 {
968 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
969 return false;
970 }
971 return true;
972 }
973 else
974 return false;
975 }
976}
977
Daniel Dunbar705a0982011-10-31 22:50:37 +0000978bool
Jim Ingham7089d8a2011-10-28 23:14:11 +0000979Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
980{
981 if (!m_breakpoints_use_platform_avoid)
982 return false;
983 else if (GetPlatform())
984 {
985 return GetPlatform()->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
986 }
987 else
988 return false;
989}
990
Chris Lattner24943d22010-06-08 16:52:24 +0000991size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000992Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
993{
994 const Section *section = addr.GetSection();
995 if (section && section->GetModule())
996 {
997 ObjectFile *objfile = section->GetModule()->GetObjectFile();
998 if (objfile)
999 {
1000 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
1001 addr.GetOffset(),
1002 dst,
1003 dst_len);
1004 if (bytes_read > 0)
1005 return bytes_read;
1006 else
1007 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
1008 }
1009 else
1010 {
1011 error.SetErrorString("address isn't from a object file");
1012 }
1013 }
1014 else
1015 {
1016 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
1017 }
1018 return 0;
1019}
1020
1021size_t
Enrico Granata91544802011-09-06 19:20:51 +00001022Target::ReadMemory (const Address& addr,
1023 bool prefer_file_cache,
1024 void *dst,
1025 size_t dst_len,
1026 Error &error,
1027 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +00001028{
Chris Lattner24943d22010-06-08 16:52:24 +00001029 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +00001030
Enrico Granata91544802011-09-06 19:20:51 +00001031 // if we end up reading this from process memory, we will fill this
1032 // with the actual load address
1033 if (load_addr_ptr)
1034 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1035
Greg Clayton26100dc2011-01-07 01:57:07 +00001036 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001037
1038 addr_t load_addr = LLDB_INVALID_ADDRESS;
1039 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001040 Address resolved_addr;
1041 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001042 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001043 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001044 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001045 // No sections are loaded, so we must assume we are not running
1046 // yet and anything we are given is a file address.
1047 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1048 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001049 }
Greg Clayton70436352010-06-30 23:03:03 +00001050 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001051 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001052 // We have at least one section loaded. This can be becuase
1053 // we have manually loaded some sections with "target modules load ..."
1054 // or because we have have a live process that has sections loaded
1055 // through the dynamic loader
1056 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1057 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001058 }
Greg Clayton70436352010-06-30 23:03:03 +00001059 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001060 if (!resolved_addr.IsValid())
1061 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001062
Greg Clayton9b82f862011-07-11 05:12:02 +00001063
Greg Clayton26100dc2011-01-07 01:57:07 +00001064 if (prefer_file_cache)
1065 {
1066 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1067 if (bytes_read > 0)
1068 return bytes_read;
1069 }
Greg Clayton70436352010-06-30 23:03:03 +00001070
Johnny Chenda5a8022011-09-20 23:28:55 +00001071 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001072 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001073 if (load_addr == LLDB_INVALID_ADDRESS)
1074 load_addr = resolved_addr.GetLoadAddress (this);
1075
Greg Clayton70436352010-06-30 23:03:03 +00001076 if (load_addr == LLDB_INVALID_ADDRESS)
1077 {
1078 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001079 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton70436352010-06-30 23:03:03 +00001080 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001081 resolved_addr.GetFileAddress(),
1082 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001083 else
Greg Clayton9c236732011-10-26 00:56:27 +00001084 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001085 }
1086 else
1087 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001088 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001089 if (bytes_read != dst_len)
1090 {
1091 if (error.Success())
1092 {
1093 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001094 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001095 else
Greg Clayton9c236732011-10-26 00:56:27 +00001096 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 +00001097 }
1098 }
Greg Clayton70436352010-06-30 23:03:03 +00001099 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001100 {
1101 if (load_addr_ptr)
1102 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001103 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001104 }
Greg Clayton70436352010-06-30 23:03:03 +00001105 // If the address is not section offset we have an address that
1106 // doesn't resolve to any address in any currently loaded shared
1107 // libaries and we failed to read memory so there isn't anything
1108 // more we can do. If it is section offset, we might be able to
1109 // read cached memory from the object file.
1110 if (!resolved_addr.IsSectionOffset())
1111 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001112 }
Chris Lattner24943d22010-06-08 16:52:24 +00001113 }
Greg Clayton70436352010-06-30 23:03:03 +00001114
Greg Clayton9b82f862011-07-11 05:12:02 +00001115 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001116 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001117 // If we didn't already try and read from the object file cache, then
1118 // try it after failing to read from the process.
1119 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001120 }
1121 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001122}
1123
Greg Clayton7dd98df2011-07-12 17:06:17 +00001124size_t
1125Target::ReadScalarIntegerFromMemory (const Address& addr,
1126 bool prefer_file_cache,
1127 uint32_t byte_size,
1128 bool is_signed,
1129 Scalar &scalar,
1130 Error &error)
1131{
1132 uint64_t uval;
1133
1134 if (byte_size <= sizeof(uval))
1135 {
1136 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1137 if (bytes_read == byte_size)
1138 {
1139 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1140 uint32_t offset = 0;
1141 if (byte_size <= 4)
1142 scalar = data.GetMaxU32 (&offset, byte_size);
1143 else
1144 scalar = data.GetMaxU64 (&offset, byte_size);
1145
1146 if (is_signed)
1147 scalar.SignExtend(byte_size * 8);
1148 return bytes_read;
1149 }
1150 }
1151 else
1152 {
1153 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1154 }
1155 return 0;
1156}
1157
1158uint64_t
1159Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1160 bool prefer_file_cache,
1161 size_t integer_byte_size,
1162 uint64_t fail_value,
1163 Error &error)
1164{
1165 Scalar scalar;
1166 if (ReadScalarIntegerFromMemory (addr,
1167 prefer_file_cache,
1168 integer_byte_size,
1169 false,
1170 scalar,
1171 error))
1172 return scalar.ULongLong(fail_value);
1173 return fail_value;
1174}
1175
1176bool
1177Target::ReadPointerFromMemory (const Address& addr,
1178 bool prefer_file_cache,
1179 Error &error,
1180 Address &pointer_addr)
1181{
1182 Scalar scalar;
1183 if (ReadScalarIntegerFromMemory (addr,
1184 prefer_file_cache,
1185 m_arch.GetAddressByteSize(),
1186 false,
1187 scalar,
1188 error))
1189 {
1190 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1191 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1192 {
1193 if (m_section_load_list.IsEmpty())
1194 {
1195 // No sections are loaded, so we must assume we are not running
1196 // yet and anything we are given is a file address.
1197 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1198 }
1199 else
1200 {
1201 // We have at least one section loaded. This can be becuase
1202 // we have manually loaded some sections with "target modules load ..."
1203 // or because we have have a live process that has sections loaded
1204 // through the dynamic loader
1205 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1206 }
1207 // We weren't able to resolve the pointer value, so just return
1208 // an address with no section
1209 if (!pointer_addr.IsValid())
1210 pointer_addr.SetOffset (pointer_vm_addr);
1211 return true;
1212
1213 }
1214 }
1215 return false;
1216}
Chris Lattner24943d22010-06-08 16:52:24 +00001217
1218ModuleSP
1219Target::GetSharedModule
1220(
1221 const FileSpec& file_spec,
1222 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +00001223 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +00001224 const ConstString *object_name,
1225 off_t object_offset,
1226 Error *error_ptr
1227)
1228{
1229 // Don't pass in the UUID so we can tell if we have a stale value in our list
1230 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1231 bool did_create_module = false;
1232 ModuleSP module_sp;
1233
Chris Lattner24943d22010-06-08 16:52:24 +00001234 Error error;
1235
Greg Clayton24bc5d92011-03-30 18:16:51 +00001236 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +00001237 if (m_image_search_paths.GetSize())
1238 {
1239 FileSpec transformed_spec;
1240 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1241 {
1242 transformed_spec.GetFilename() = file_spec.GetFilename();
1243 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1244 }
1245 }
1246
Greg Clayton24bc5d92011-03-30 18:16:51 +00001247 // The platform is responsible for finding and caching an appropriate
1248 // module in the shared module cache.
1249 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001250 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001251 FileSpec platform_file_spec;
1252 error = m_platform_sp->GetSharedModule (file_spec,
1253 arch,
1254 uuid_ptr,
1255 object_name,
1256 object_offset,
1257 module_sp,
1258 &old_module_sp,
1259 &did_create_module);
1260 }
1261 else
1262 {
1263 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +00001264 }
1265
Greg Clayton24bc5d92011-03-30 18:16:51 +00001266 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +00001267 if (module_sp)
1268 {
1269 m_images.Append (module_sp);
1270 if (did_create_module)
1271 {
1272 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1273 ModuleUpdated(old_module_sp, module_sp);
1274 else
1275 ModuleAdded(module_sp);
1276 }
1277 }
1278 if (error_ptr)
1279 *error_ptr = error;
1280 return module_sp;
1281}
1282
1283
1284Target *
1285Target::CalculateTarget ()
1286{
1287 return this;
1288}
1289
1290Process *
1291Target::CalculateProcess ()
1292{
1293 return NULL;
1294}
1295
1296Thread *
1297Target::CalculateThread ()
1298{
1299 return NULL;
1300}
1301
1302StackFrame *
1303Target::CalculateStackFrame ()
1304{
1305 return NULL;
1306}
1307
1308void
Greg Claytona830adb2010-10-04 01:05:56 +00001309Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001310{
Greg Clayton567e7f32011-09-22 04:58:26 +00001311 exe_ctx.Clear();
1312 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001313}
1314
1315PathMappingList &
1316Target::GetImageSearchPathList ()
1317{
1318 return m_image_search_paths;
1319}
1320
1321void
1322Target::ImageSearchPathsChanged
1323(
1324 const PathMappingList &path_list,
1325 void *baton
1326)
1327{
1328 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001329 ModuleSP exe_module_sp (target->GetExecutableModule());
1330 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001331 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001332 target->m_images.Clear();
1333 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001334 }
1335}
1336
1337ClangASTContext *
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001338Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner24943d22010-06-08 16:52:24 +00001339{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001340 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chenfa21ffd2011-11-30 23:18:53 +00001341 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanandcf03f82011-11-15 22:27:19 +00001342 {
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001343 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Sean Callanandcf03f82011-11-15 22:27:19 +00001344 m_scratch_ast_source_ap.reset (new ClangASTSource(GetSP()));
1345 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1346 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1347 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1348 }
Chris Lattner24943d22010-06-08 16:52:24 +00001349 return m_scratch_ast_context_ap.get();
1350}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001351
Sean Callanan4938bd62011-11-16 18:20:47 +00001352ClangASTImporter *
1353Target::GetClangASTImporter()
1354{
1355 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1356
1357 if (!ast_importer)
1358 {
1359 ast_importer = new ClangASTImporter();
1360 m_ast_importer_ap.reset(ast_importer);
1361 }
1362
1363 return ast_importer;
1364}
1365
Greg Clayton990de7b2010-11-18 23:32:35 +00001366void
Caroline Tice2a456812011-03-10 22:14:10 +00001367Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001368{
Greg Clayton990de7b2010-11-18 23:32:35 +00001369 UserSettingsControllerSP &usc = GetSettingsController();
1370 usc.reset (new SettingsController);
1371 UserSettingsController::InitializeSettingsController (usc,
1372 SettingsController::global_settings_table,
1373 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001374
1375 // Now call SettingsInitialize() on each 'child' setting of Target
1376 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001377}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001378
Greg Clayton990de7b2010-11-18 23:32:35 +00001379void
Caroline Tice2a456812011-03-10 22:14:10 +00001380Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001381{
Caroline Tice2a456812011-03-10 22:14:10 +00001382
1383 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1384
1385 Process::SettingsTerminate ();
1386
1387 // Now terminate Target Settings.
1388
Greg Clayton990de7b2010-11-18 23:32:35 +00001389 UserSettingsControllerSP &usc = GetSettingsController();
1390 UserSettingsController::FinalizeSettingsController (usc);
1391 usc.reset();
1392}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001393
Greg Clayton990de7b2010-11-18 23:32:35 +00001394UserSettingsControllerSP &
1395Target::GetSettingsController ()
1396{
1397 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001398 return g_settings_controller;
1399}
1400
1401ArchSpec
1402Target::GetDefaultArchitecture ()
1403{
Greg Clayton940b1032011-02-23 00:35:02 +00001404 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1405
1406 if (settings_controller_sp)
1407 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1408 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001409}
1410
1411void
Greg Clayton940b1032011-02-23 00:35:02 +00001412Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001413{
Greg Clayton940b1032011-02-23 00:35:02 +00001414 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1415
1416 if (settings_controller_sp)
1417 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001418}
1419
Greg Claytona830adb2010-10-04 01:05:56 +00001420Target *
1421Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1422{
1423 // The target can either exist in the "process" of ExecutionContext, or in
1424 // the "target_sp" member of SymbolContext. This accessor helper function
1425 // will get the target from one of these locations.
1426
1427 Target *target = NULL;
1428 if (sc_ptr != NULL)
1429 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001430 if (target == NULL && exe_ctx_ptr)
1431 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001432 return target;
1433}
1434
1435
Caroline Tice1ebef442010-09-27 00:30:10 +00001436void
1437Target::UpdateInstanceName ()
1438{
1439 StreamString sstr;
1440
Greg Clayton5beb99d2011-08-11 02:48:45 +00001441 Module *exe_module = GetExecutableModulePointer();
1442 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001443 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001444 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001445 exe_module->GetFileSpec().GetFilename().AsCString(),
1446 exe_module->GetArchitecture().GetArchitectureName());
1447 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001448 }
1449}
1450
Sean Callanan77e93942010-10-29 00:29:03 +00001451const char *
1452Target::GetExpressionPrefixContentsAsCString ()
1453{
Sean Callanane0b7f942011-11-16 01:54:57 +00001454 if (!m_expr_prefix_contents.empty())
1455 return m_expr_prefix_contents.c_str();
Greg Claytonff44ab42011-04-23 02:04:55 +00001456 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001457}
1458
Greg Clayton427f2902010-12-14 02:59:59 +00001459ExecutionResults
1460Target::EvaluateExpression
1461(
1462 const char *expr_cstr,
1463 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001464 lldb_private::ExecutionPolicy execution_policy,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001465 bool coerce_to_id,
Greg Clayton427f2902010-12-14 02:59:59 +00001466 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001467 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001468 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001469 lldb::ValueObjectSP &result_valobj_sp
1470)
1471{
1472 ExecutionResults execution_results = eExecutionSetupError;
1473
1474 result_valobj_sp.reset();
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001475
1476 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1477 return execution_results;
1478
Jim Ingham3613ae12011-05-12 02:06:14 +00001479 // We shouldn't run stop hooks in expressions.
1480 // Be sure to reset this if you return anywhere within this function.
1481 bool old_suppress_value = m_suppress_stop_hooks;
1482 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001483
1484 ExecutionContext exe_ctx;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001485
1486 const size_t expr_cstr_len = ::strlen (expr_cstr);
1487
Greg Clayton427f2902010-12-14 02:59:59 +00001488 if (frame)
1489 {
1490 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001491 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001492 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001493 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1494 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001495 lldb::VariableSP var_sp;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001496
1497 // Make sure we don't have any things that we know a variable expression
1498 // won't be able to deal with before calling into it
1499 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1500 {
1501 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1502 use_dynamic,
1503 expr_path_options,
1504 var_sp,
1505 error);
1506 }
Greg Clayton427f2902010-12-14 02:59:59 +00001507 }
1508 else if (m_process_sp)
1509 {
1510 m_process_sp->CalculateExecutionContext(exe_ctx);
1511 }
1512 else
1513 {
1514 CalculateExecutionContext(exe_ctx);
1515 }
1516
1517 if (result_valobj_sp)
1518 {
1519 execution_results = eExecutionCompleted;
1520 // We got a result from the frame variable expression path above...
1521 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1522
1523 lldb::ValueObjectSP const_valobj_sp;
1524
1525 // Check in case our value is already a constant value
1526 if (result_valobj_sp->GetIsConstant())
1527 {
1528 const_valobj_sp = result_valobj_sp;
1529 const_valobj_sp->SetName (persistent_variable_name);
1530 }
1531 else
Jim Inghame41494a2011-04-16 00:01:13 +00001532 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001533 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001534 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001535 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001536 if (dynamic_sp)
1537 result_valobj_sp = dynamic_sp;
1538 }
1539
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001540 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001541 }
Greg Clayton427f2902010-12-14 02:59:59 +00001542
Sean Callanan6a925532011-01-13 08:53:35 +00001543 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1544
Greg Clayton427f2902010-12-14 02:59:59 +00001545 result_valobj_sp = const_valobj_sp;
1546
Sean Callanan6a925532011-01-13 08:53:35 +00001547 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1548 assert (clang_expr_variable_sp.get());
1549
1550 // Set flags and live data as appropriate
1551
1552 const Value &result_value = live_valobj_sp->GetValue();
1553
1554 switch (result_value.GetValueType())
1555 {
1556 case Value::eValueTypeHostAddress:
1557 case Value::eValueTypeFileAddress:
1558 // we don't do anything with these for now
1559 break;
1560 case Value::eValueTypeScalar:
1561 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1562 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1563 break;
1564 case Value::eValueTypeLoadAddress:
1565 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1566 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1567 break;
1568 }
Greg Clayton427f2902010-12-14 02:59:59 +00001569 }
1570 else
1571 {
1572 // Make sure we aren't just trying to see the value of a persistent
1573 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001574 lldb::ClangExpressionVariableSP persistent_var_sp;
1575 // Only check for persistent variables the expression starts with a '$'
1576 if (expr_cstr[0] == '$')
1577 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1578
Greg Clayton427f2902010-12-14 02:59:59 +00001579 if (persistent_var_sp)
1580 {
1581 result_valobj_sp = persistent_var_sp->GetValueObject ();
1582 execution_results = eExecutionCompleted;
1583 }
1584 else
1585 {
1586 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001587
Greg Clayton427f2902010-12-14 02:59:59 +00001588 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001589 execution_policy,
Sean Callanan5b658cc2011-11-07 23:35:40 +00001590 lldb::eLanguageTypeUnknown,
Sean Callanandaa6efe2011-12-21 22:22:58 +00001591 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan6a925532011-01-13 08:53:35 +00001592 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001593 expr_cstr,
1594 prefix,
1595 result_valobj_sp);
1596 }
1597 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001598
1599 m_suppress_stop_hooks = old_suppress_value;
1600
Greg Clayton427f2902010-12-14 02:59:59 +00001601 return execution_results;
1602}
1603
Greg Claytonc0fa5332011-05-22 22:46:53 +00001604lldb::addr_t
1605Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1606{
1607 addr_t code_addr = load_addr;
1608 switch (m_arch.GetMachine())
1609 {
1610 case llvm::Triple::arm:
1611 case llvm::Triple::thumb:
1612 switch (addr_class)
1613 {
1614 case eAddressClassData:
1615 case eAddressClassDebug:
1616 return LLDB_INVALID_ADDRESS;
1617
1618 case eAddressClassUnknown:
1619 case eAddressClassInvalid:
1620 case eAddressClassCode:
1621 case eAddressClassCodeAlternateISA:
1622 case eAddressClassRuntime:
1623 // Check if bit zero it no set?
1624 if ((code_addr & 1ull) == 0)
1625 {
1626 // Bit zero isn't set, check if the address is a multiple of 2?
1627 if (code_addr & 2ull)
1628 {
1629 // The address is a multiple of 2 so it must be thumb, set bit zero
1630 code_addr |= 1ull;
1631 }
1632 else if (addr_class == eAddressClassCodeAlternateISA)
1633 {
1634 // We checked the address and the address claims to be the alternate ISA
1635 // which means thumb, so set bit zero.
1636 code_addr |= 1ull;
1637 }
1638 }
1639 break;
1640 }
1641 break;
1642
1643 default:
1644 break;
1645 }
1646 return code_addr;
1647}
1648
1649lldb::addr_t
1650Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1651{
1652 addr_t opcode_addr = load_addr;
1653 switch (m_arch.GetMachine())
1654 {
1655 case llvm::Triple::arm:
1656 case llvm::Triple::thumb:
1657 switch (addr_class)
1658 {
1659 case eAddressClassData:
1660 case eAddressClassDebug:
1661 return LLDB_INVALID_ADDRESS;
1662
1663 case eAddressClassInvalid:
1664 case eAddressClassUnknown:
1665 case eAddressClassCode:
1666 case eAddressClassCodeAlternateISA:
1667 case eAddressClassRuntime:
1668 opcode_addr &= ~(1ull);
1669 break;
1670 }
1671 break;
1672
1673 default:
1674 break;
1675 }
1676 return opcode_addr;
1677}
1678
Jim Inghamd60d94a2011-03-11 03:53:59 +00001679lldb::user_id_t
1680Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1681{
1682 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1683 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1684 m_stop_hooks[new_uid] = new_hook_sp;
1685 return new_uid;
1686}
1687
1688bool
1689Target::RemoveStopHookByID (lldb::user_id_t user_id)
1690{
1691 size_t num_removed;
1692 num_removed = m_stop_hooks.erase (user_id);
1693 if (num_removed == 0)
1694 return false;
1695 else
1696 return true;
1697}
1698
1699void
1700Target::RemoveAllStopHooks ()
1701{
1702 m_stop_hooks.clear();
1703}
1704
1705Target::StopHookSP
1706Target::GetStopHookByID (lldb::user_id_t user_id)
1707{
1708 StopHookSP found_hook;
1709
1710 StopHookCollection::iterator specified_hook_iter;
1711 specified_hook_iter = m_stop_hooks.find (user_id);
1712 if (specified_hook_iter != m_stop_hooks.end())
1713 found_hook = (*specified_hook_iter).second;
1714 return found_hook;
1715}
1716
1717bool
1718Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1719{
1720 StopHookCollection::iterator specified_hook_iter;
1721 specified_hook_iter = m_stop_hooks.find (user_id);
1722 if (specified_hook_iter == m_stop_hooks.end())
1723 return false;
1724
1725 (*specified_hook_iter).second->SetIsActive (active_state);
1726 return true;
1727}
1728
1729void
1730Target::SetAllStopHooksActiveState (bool active_state)
1731{
1732 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1733 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1734 {
1735 (*pos).second->SetIsActive (active_state);
1736 }
1737}
1738
1739void
1740Target::RunStopHooks ()
1741{
Jim Ingham3613ae12011-05-12 02:06:14 +00001742 if (m_suppress_stop_hooks)
1743 return;
1744
Jim Inghamd60d94a2011-03-11 03:53:59 +00001745 if (!m_process_sp)
1746 return;
1747
1748 if (m_stop_hooks.empty())
1749 return;
1750
1751 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1752
1753 // If there aren't any active stop hooks, don't bother either:
1754 bool any_active_hooks = false;
1755 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1756 {
1757 if ((*pos).second->IsActive())
1758 {
1759 any_active_hooks = true;
1760 break;
1761 }
1762 }
1763 if (!any_active_hooks)
1764 return;
1765
1766 CommandReturnObject result;
1767
1768 std::vector<ExecutionContext> exc_ctx_with_reasons;
1769 std::vector<SymbolContext> sym_ctx_with_reasons;
1770
1771 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1772 size_t num_threads = cur_threadlist.GetSize();
1773 for (size_t i = 0; i < num_threads; i++)
1774 {
1775 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1776 if (cur_thread_sp->ThreadStoppedForAReason())
1777 {
1778 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1779 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1780 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1781 }
1782 }
1783
1784 // If no threads stopped for a reason, don't run the stop-hooks.
1785 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1786 if (num_exe_ctx == 0)
1787 return;
1788
Jim Inghame5ed8e92011-06-02 23:58:26 +00001789 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1790 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001791
1792 bool keep_going = true;
1793 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001794 bool print_hook_header;
1795 bool print_thread_header;
1796
1797 if (num_exe_ctx == 1)
1798 print_thread_header = false;
1799 else
1800 print_thread_header = true;
1801
1802 if (m_stop_hooks.size() == 1)
1803 print_hook_header = false;
1804 else
1805 print_hook_header = true;
1806
Jim Inghamd60d94a2011-03-11 03:53:59 +00001807 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1808 {
1809 // result.Clear();
1810 StopHookSP cur_hook_sp = (*pos).second;
1811 if (!cur_hook_sp->IsActive())
1812 continue;
1813
1814 bool any_thread_matched = false;
1815 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1816 {
1817 if ((cur_hook_sp->GetSpecifier () == NULL
1818 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1819 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Clayton567e7f32011-09-22 04:58:26 +00001820 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001821 {
1822 if (!hooks_ran)
1823 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001824 hooks_ran = true;
1825 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001826 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001827 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001828 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1829 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1830 NULL);
1831 if (cmd)
1832 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1833 else
1834 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001835 any_thread_matched = true;
1836 }
1837
Jim Inghamc54840c2011-03-22 01:47:27 +00001838 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001839 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001840
1841 bool stop_on_continue = true;
1842 bool stop_on_error = true;
1843 bool echo_commands = false;
1844 bool print_results = true;
1845 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001846 &exc_ctx_with_reasons[i],
1847 stop_on_continue,
1848 stop_on_error,
1849 echo_commands,
1850 print_results,
1851 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001852
1853 // If the command started the target going again, we should bag out of
1854 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001855 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1856 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001857 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001858 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001859 keep_going = false;
1860 }
1861 }
1862 }
1863 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00001864
Caroline Tice4a348082011-05-02 20:41:46 +00001865 result.GetImmediateOutputStream()->Flush();
1866 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001867}
1868
Greg Claytonbbea1332011-07-08 00:48:09 +00001869bool
1870Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1871{
1872 bool changed = false;
1873 if (module)
1874 {
1875 ObjectFile *object_file = module->GetObjectFile();
1876 if (object_file)
1877 {
1878 SectionList *section_list = object_file->GetSectionList ();
1879 if (section_list)
1880 {
1881 // All sections listed in the dyld image info structure will all
1882 // either be fixed up already, or they will all be off by a single
1883 // slide amount that is determined by finding the first segment
1884 // that is at file offset zero which also has bytes (a file size
1885 // that is greater than zero) in the object file.
1886
1887 // Determine the slide amount (if any)
1888 const size_t num_sections = section_list->GetSize();
1889 size_t sect_idx = 0;
1890 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1891 {
1892 // Iterate through the object file sections to find the
1893 // first section that starts of file offset zero and that
1894 // has bytes in the file...
1895 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1896 if (section)
1897 {
1898 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1899 changed = true;
1900 }
1901 }
1902 }
1903 }
1904 }
1905 return changed;
1906}
1907
1908
Jim Inghamd60d94a2011-03-11 03:53:59 +00001909//--------------------------------------------------------------
1910// class Target::StopHook
1911//--------------------------------------------------------------
1912
1913
1914Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1915 UserID (uid),
1916 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001917 m_commands (),
1918 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001919 m_thread_spec_ap(NULL),
1920 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001921{
1922}
1923
1924Target::StopHook::StopHook (const StopHook &rhs) :
1925 UserID (rhs.GetID()),
1926 m_target_sp (rhs.m_target_sp),
1927 m_commands (rhs.m_commands),
1928 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001929 m_thread_spec_ap (NULL),
1930 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001931{
1932 if (rhs.m_thread_spec_ap.get() != NULL)
1933 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1934}
1935
1936
1937Target::StopHook::~StopHook ()
1938{
1939}
1940
1941void
1942Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1943{
1944 m_thread_spec_ap.reset (specifier);
1945}
1946
1947
1948void
1949Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1950{
1951 int indent_level = s->GetIndentLevel();
1952
1953 s->SetIndentLevel(indent_level + 2);
1954
Greg Clayton444e35b2011-10-19 18:09:39 +00001955 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001956 if (m_active)
1957 s->Indent ("State: enabled\n");
1958 else
1959 s->Indent ("State: disabled\n");
1960
1961 if (m_specifier_sp)
1962 {
1963 s->Indent();
1964 s->PutCString ("Specifier:\n");
1965 s->SetIndentLevel (indent_level + 4);
1966 m_specifier_sp->GetDescription (s, level);
1967 s->SetIndentLevel (indent_level + 2);
1968 }
1969
1970 if (m_thread_spec_ap.get() != NULL)
1971 {
1972 StreamString tmp;
1973 s->Indent("Thread:\n");
1974 m_thread_spec_ap->GetDescription (&tmp, level);
1975 s->SetIndentLevel (indent_level + 4);
1976 s->Indent (tmp.GetData());
1977 s->PutCString ("\n");
1978 s->SetIndentLevel (indent_level + 2);
1979 }
1980
1981 s->Indent ("Commands: \n");
1982 s->SetIndentLevel (indent_level + 4);
1983 uint32_t num_commands = m_commands.GetSize();
1984 for (uint32_t i = 0; i < num_commands; i++)
1985 {
1986 s->Indent(m_commands.GetStringAtIndex(i));
1987 s->PutCString ("\n");
1988 }
1989 s->SetIndentLevel (indent_level);
1990}
1991
1992
Caroline Tice5bc8c972010-09-20 20:44:43 +00001993//--------------------------------------------------------------
1994// class Target::SettingsController
1995//--------------------------------------------------------------
1996
1997Target::SettingsController::SettingsController () :
1998 UserSettingsController ("target", Debugger::GetSettingsController()),
1999 m_default_architecture ()
2000{
2001 m_default_settings.reset (new TargetInstanceSettings (*this, false,
2002 InstanceSettings::GetDefaultName().AsCString()));
2003}
2004
2005Target::SettingsController::~SettingsController ()
2006{
2007}
2008
2009lldb::InstanceSettingsSP
2010Target::SettingsController::CreateInstanceSettings (const char *instance_name)
2011{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00002012 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
2013 false,
2014 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002015 lldb::InstanceSettingsSP new_settings_sp (new_settings);
2016 return new_settings_sp;
2017}
2018
Caroline Tice5bc8c972010-09-20 20:44:43 +00002019
Greg Claytonabb33022011-11-08 02:43:13 +00002020#define TSC_DEFAULT_ARCH "default-arch"
2021#define TSC_EXPR_PREFIX "expr-prefix"
2022#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
2023#define TSC_SKIP_PROLOGUE "skip-prologue"
2024#define TSC_SOURCE_MAP "source-map"
2025#define TSC_MAX_CHILDREN "max-children-count"
2026#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
2027#define TSC_PLATFORM_AVOID "breakpoints-use-platform-avoid-list"
2028#define TSC_RUN_ARGS "run-args"
2029#define TSC_ENV_VARS "env-vars"
2030#define TSC_INHERIT_ENV "inherit-env"
2031#define TSC_STDIN_PATH "input-path"
2032#define TSC_STDOUT_PATH "output-path"
2033#define TSC_STDERR_PATH "error-path"
2034#define TSC_DISABLE_ASLR "disable-aslr"
2035#define TSC_DISABLE_STDIO "disable-stdio"
Greg Claytond284b662011-02-18 01:44:25 +00002036
2037
2038static const ConstString &
2039GetSettingNameForDefaultArch ()
2040{
2041 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00002042 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002043}
2044
Greg Claytond284b662011-02-18 01:44:25 +00002045static const ConstString &
2046GetSettingNameForExpressionPrefix ()
2047{
2048 static ConstString g_const_string (TSC_EXPR_PREFIX);
2049 return g_const_string;
2050}
2051
2052static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00002053GetSettingNameForPreferDynamicValue ()
2054{
2055 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
2056 return g_const_string;
2057}
2058
Greg Claytonff44ab42011-04-23 02:04:55 +00002059static const ConstString &
2060GetSettingNameForSourcePathMap ()
2061{
2062 static ConstString g_const_string (TSC_SOURCE_MAP);
2063 return g_const_string;
2064}
Greg Claytond284b662011-02-18 01:44:25 +00002065
Greg Clayton17cd9952011-04-22 03:55:06 +00002066static const ConstString &
2067GetSettingNameForSkipPrologue ()
2068{
2069 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
2070 return g_const_string;
2071}
2072
Enrico Granata018921d2011-08-12 02:00:06 +00002073static const ConstString &
2074GetSettingNameForMaxChildren ()
2075{
2076 static ConstString g_const_string (TSC_MAX_CHILDREN);
2077 return g_const_string;
2078}
Greg Clayton17cd9952011-04-22 03:55:06 +00002079
Enrico Granata91544802011-09-06 19:20:51 +00002080static const ConstString &
2081GetSettingNameForMaxStringSummaryLength ()
2082{
2083 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2084 return g_const_string;
2085}
Greg Clayton17cd9952011-04-22 03:55:06 +00002086
Jim Ingham7089d8a2011-10-28 23:14:11 +00002087static const ConstString &
2088GetSettingNameForPlatformAvoid ()
2089{
2090 static ConstString g_const_string (TSC_PLATFORM_AVOID);
2091 return g_const_string;
2092}
2093
Greg Claytonabb33022011-11-08 02:43:13 +00002094const ConstString &
2095GetSettingNameForRunArgs ()
2096{
2097 static ConstString g_const_string (TSC_RUN_ARGS);
2098 return g_const_string;
2099}
2100
2101const ConstString &
2102GetSettingNameForEnvVars ()
2103{
2104 static ConstString g_const_string (TSC_ENV_VARS);
2105 return g_const_string;
2106}
2107
2108const ConstString &
2109GetSettingNameForInheritHostEnv ()
2110{
2111 static ConstString g_const_string (TSC_INHERIT_ENV);
2112 return g_const_string;
2113}
2114
2115const ConstString &
2116GetSettingNameForInputPath ()
2117{
2118 static ConstString g_const_string (TSC_STDIN_PATH);
2119 return g_const_string;
2120}
2121
2122const ConstString &
2123GetSettingNameForOutputPath ()
2124{
2125 static ConstString g_const_string (TSC_STDOUT_PATH);
2126 return g_const_string;
2127}
2128
2129const ConstString &
2130GetSettingNameForErrorPath ()
2131{
2132 static ConstString g_const_string (TSC_STDERR_PATH);
2133 return g_const_string;
2134}
2135
2136const ConstString &
2137GetSettingNameForDisableASLR ()
2138{
2139 static ConstString g_const_string (TSC_DISABLE_ASLR);
2140 return g_const_string;
2141}
2142
2143const ConstString &
2144GetSettingNameForDisableSTDIO ()
2145{
2146 static ConstString g_const_string (TSC_DISABLE_STDIO);
2147 return g_const_string;
2148}
Jim Ingham7089d8a2011-10-28 23:14:11 +00002149
Caroline Tice5bc8c972010-09-20 20:44:43 +00002150bool
2151Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2152 const char *index_value,
2153 const char *value,
2154 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002155 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002156 Error&err)
2157{
Greg Claytond284b662011-02-18 01:44:25 +00002158 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002159 {
Greg Claytonf15996e2011-04-07 22:46:35 +00002160 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00002161 if (!m_default_architecture.IsValid())
2162 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002163 }
2164 return true;
2165}
2166
2167
2168bool
2169Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2170 StringList &value,
2171 Error &err)
2172{
Greg Claytond284b662011-02-18 01:44:25 +00002173 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002174 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002175 // If the arch is invalid (the default), don't show a string for it
2176 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002177 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002178 return true;
2179 }
2180 else
2181 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2182
2183 return false;
2184}
2185
2186//--------------------------------------------------------------
2187// class TargetInstanceSettings
2188//--------------------------------------------------------------
2189
Greg Clayton638351a2010-12-04 00:10:17 +00002190TargetInstanceSettings::TargetInstanceSettings
2191(
2192 UserSettingsController &owner,
2193 bool live_instance,
2194 const char *name
2195) :
Greg Claytond284b662011-02-18 01:44:25 +00002196 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002197 m_expr_prefix_file (),
Sean Callanane0b7f942011-11-16 01:54:57 +00002198 m_expr_prefix_contents (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002199 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00002200 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002201 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00002202 m_max_children_display(256),
Jim Ingham7089d8a2011-10-28 23:14:11 +00002203 m_max_strlen_length(1024),
Greg Claytonabb33022011-11-08 02:43:13 +00002204 m_breakpoints_use_platform_avoid (true, true),
2205 m_run_args (),
2206 m_env_vars (),
2207 m_input_path (),
2208 m_output_path (),
2209 m_error_path (),
2210 m_disable_aslr (true),
2211 m_disable_stdio (false),
2212 m_inherit_host_env (true),
2213 m_got_host_env (false)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002214{
2215 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2216 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2217 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2218 // This is true for CreateInstanceName() too.
2219
2220 if (GetInstanceName () == InstanceSettings::InvalidName())
2221 {
2222 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2223 m_owner.RegisterInstanceSettings (this);
2224 }
2225
2226 if (live_instance)
2227 {
2228 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2229 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002230 }
2231}
2232
2233TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00002234 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
2235 m_expr_prefix_file (rhs.m_expr_prefix_file),
Sean Callanane0b7f942011-11-16 01:54:57 +00002236 m_expr_prefix_contents (rhs.m_expr_prefix_contents),
Greg Claytonff44ab42011-04-23 02:04:55 +00002237 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2238 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002239 m_source_map (rhs.m_source_map),
Greg Claytonabb33022011-11-08 02:43:13 +00002240 m_max_children_display (rhs.m_max_children_display),
2241 m_max_strlen_length (rhs.m_max_strlen_length),
2242 m_breakpoints_use_platform_avoid (rhs.m_breakpoints_use_platform_avoid),
2243 m_run_args (rhs.m_run_args),
2244 m_env_vars (rhs.m_env_vars),
2245 m_input_path (rhs.m_input_path),
2246 m_output_path (rhs.m_output_path),
2247 m_error_path (rhs.m_error_path),
2248 m_disable_aslr (rhs.m_disable_aslr),
2249 m_disable_stdio (rhs.m_disable_stdio),
2250 m_inherit_host_env (rhs.m_inherit_host_env)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002251{
2252 if (m_instance_name != InstanceSettings::GetDefaultName())
2253 {
2254 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2255 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002256 }
2257}
2258
2259TargetInstanceSettings::~TargetInstanceSettings ()
2260{
2261}
2262
2263TargetInstanceSettings&
2264TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2265{
2266 if (this != &rhs)
2267 {
Greg Claytonabb33022011-11-08 02:43:13 +00002268 m_expr_prefix_file = rhs.m_expr_prefix_file;
Sean Callanane0b7f942011-11-16 01:54:57 +00002269 m_expr_prefix_contents = rhs.m_expr_prefix_contents;
Greg Claytonabb33022011-11-08 02:43:13 +00002270 m_prefer_dynamic_value = rhs.m_prefer_dynamic_value;
2271 m_skip_prologue = rhs.m_skip_prologue;
2272 m_source_map = rhs.m_source_map;
2273 m_max_children_display = rhs.m_max_children_display;
2274 m_max_strlen_length = rhs.m_max_strlen_length;
2275 m_breakpoints_use_platform_avoid = rhs.m_breakpoints_use_platform_avoid;
2276 m_run_args = rhs.m_run_args;
2277 m_env_vars = rhs.m_env_vars;
2278 m_input_path = rhs.m_input_path;
2279 m_output_path = rhs.m_output_path;
2280 m_error_path = rhs.m_error_path;
2281 m_disable_aslr = rhs.m_disable_aslr;
2282 m_disable_stdio = rhs.m_disable_stdio;
2283 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002284 }
2285
2286 return *this;
2287}
2288
Caroline Tice5bc8c972010-09-20 20:44:43 +00002289void
2290TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2291 const char *index_value,
2292 const char *value,
2293 const ConstString &instance_name,
2294 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002295 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002296 Error &err,
2297 bool pending)
2298{
Greg Claytond284b662011-02-18 01:44:25 +00002299 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002300 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002301 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2302 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002303 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002304 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002305 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002306 default:
2307 break;
2308 case eVarSetOperationAssign:
2309 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002310 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002311 m_expr_prefix_contents.clear();
2312
Greg Claytonff44ab42011-04-23 02:04:55 +00002313 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2314 {
2315 err.SetErrorToGenericError ();
Greg Clayton9c236732011-10-26 00:56:27 +00002316 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002317 return;
2318 }
2319
Greg Clayton4b23ab32012-01-06 02:01:06 +00002320 DataBufferSP file_data_sp (m_expr_prefix_file.GetCurrentValue().ReadFileContents(0, SIZE_MAX, &err));
Sean Callanane0b7f942011-11-16 01:54:57 +00002321
Greg Clayton4b23ab32012-01-06 02:01:06 +00002322 if (err.Success())
Greg Claytonff44ab42011-04-23 02:04:55 +00002323 {
Greg Clayton4b23ab32012-01-06 02:01:06 +00002324 if (file_data_sp && file_data_sp->GetByteSize() > 0)
2325 {
2326 m_expr_prefix_contents.assign((const char*)file_data_sp->GetBytes(), file_data_sp->GetByteSize());
2327 }
2328 else
2329 {
2330 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
2331 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002332 }
Sean Callanan77e93942010-10-29 00:29:03 +00002333 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002334 break;
2335 case eVarSetOperationClear:
Sean Callanane0b7f942011-11-16 01:54:57 +00002336 m_expr_prefix_contents.clear();
Sean Callanan77e93942010-10-29 00:29:03 +00002337 }
Sean Callanan77e93942010-10-29 00:29:03 +00002338 }
2339 }
Jim Inghame41494a2011-04-16 00:01:13 +00002340 else if (var_name == GetSettingNameForPreferDynamicValue())
2341 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002342 int new_value;
2343 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2344 if (err.Success())
2345 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002346 }
2347 else if (var_name == GetSettingNameForSkipPrologue())
2348 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002349 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2350 }
Enrico Granata018921d2011-08-12 02:00:06 +00002351 else if (var_name == GetSettingNameForMaxChildren())
2352 {
2353 bool ok;
2354 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2355 if (ok)
2356 m_max_children_display = new_value;
2357 }
Enrico Granata91544802011-09-06 19:20:51 +00002358 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2359 {
2360 bool ok;
2361 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2362 if (ok)
2363 m_max_strlen_length = new_value;
2364 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002365 else if (var_name == GetSettingNameForSourcePathMap ())
2366 {
2367 switch (op)
2368 {
2369 case eVarSetOperationReplace:
2370 case eVarSetOperationInsertBefore:
2371 case eVarSetOperationInsertAfter:
2372 case eVarSetOperationRemove:
2373 default:
2374 break;
2375 case eVarSetOperationAssign:
2376 m_source_map.Clear(true);
2377 // Fall through to append....
2378 case eVarSetOperationAppend:
2379 {
2380 Args args(value);
2381 const uint32_t argc = args.GetArgumentCount();
2382 if (argc & 1 || argc == 0)
2383 {
2384 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2385 }
2386 else
2387 {
2388 char resolved_new_path[PATH_MAX];
2389 FileSpec file_spec;
2390 const char *old_path;
2391 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2392 {
2393 const char *new_path = args.GetArgumentAtIndex(idx+1);
2394 assert (new_path); // We have an even number of paths, this shouldn't happen!
2395
2396 file_spec.SetFile(new_path, true);
2397 if (file_spec.Exists())
2398 {
2399 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2400 {
2401 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2402 return;
2403 }
2404 }
2405 else
2406 {
2407 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2408 return;
2409 }
2410 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2411 }
2412 }
2413 }
2414 break;
2415
2416 case eVarSetOperationClear:
2417 m_source_map.Clear(true);
2418 break;
2419 }
Jim Inghame41494a2011-04-16 00:01:13 +00002420 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002421 else if (var_name == GetSettingNameForPlatformAvoid ())
2422 {
2423 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_breakpoints_use_platform_avoid);
2424 }
Greg Claytonabb33022011-11-08 02:43:13 +00002425 else if (var_name == GetSettingNameForRunArgs())
2426 {
2427 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2428 }
2429 else if (var_name == GetSettingNameForEnvVars())
2430 {
2431 // This is nice for local debugging, but it is isn't correct for
2432 // remote debugging. We need to stop process.env-vars from being
2433 // populated with the host environment and add this as a launch option
2434 // and get the correct environment from the Target's platform.
2435 // GetHostEnvironmentIfNeeded ();
2436 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2437 }
2438 else if (var_name == GetSettingNameForInputPath())
2439 {
2440 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2441 }
2442 else if (var_name == GetSettingNameForOutputPath())
2443 {
2444 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2445 }
2446 else if (var_name == GetSettingNameForErrorPath())
2447 {
2448 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2449 }
2450 else if (var_name == GetSettingNameForDisableASLR())
2451 {
2452 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
2453 }
2454 else if (var_name == GetSettingNameForDisableSTDIO ())
2455 {
2456 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
2457 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002458}
2459
2460void
Greg Claytond284b662011-02-18 01:44:25 +00002461TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002462{
Sean Callanan77e93942010-10-29 00:29:03 +00002463 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2464
2465 if (!new_settings_ptr)
2466 return;
2467
Greg Claytonabb33022011-11-08 02:43:13 +00002468 *this = *new_settings_ptr;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002469}
2470
Caroline Ticebcb5b452010-09-20 21:37:42 +00002471bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002472TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2473 const ConstString &var_name,
2474 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002475 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002476{
Greg Claytond284b662011-02-18 01:44:25 +00002477 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002478 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002479 char path[PATH_MAX];
2480 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2481 if (path_len > 0)
2482 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002483 }
Jim Inghame41494a2011-04-16 00:01:13 +00002484 else if (var_name == GetSettingNameForPreferDynamicValue())
2485 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002486 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002487 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002488 else if (var_name == GetSettingNameForSkipPrologue())
2489 {
2490 if (m_skip_prologue)
2491 value.AppendString ("true");
2492 else
2493 value.AppendString ("false");
2494 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002495 else if (var_name == GetSettingNameForSourcePathMap ())
2496 {
Johnny Chen931449e2011-12-12 21:59:28 +00002497 if (m_source_map.GetSize())
2498 {
2499 size_t i;
2500 for (i = 0; i < m_source_map.GetSize(); ++i) {
2501 StreamString sstr;
2502 m_source_map.Dump(&sstr, i);
2503 value.AppendString(sstr.GetData());
2504 }
2505 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002506 }
Enrico Granata018921d2011-08-12 02:00:06 +00002507 else if (var_name == GetSettingNameForMaxChildren())
2508 {
2509 StreamString count_str;
2510 count_str.Printf ("%d", m_max_children_display);
2511 value.AppendString (count_str.GetData());
2512 }
Enrico Granata91544802011-09-06 19:20:51 +00002513 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2514 {
2515 StreamString count_str;
2516 count_str.Printf ("%d", m_max_strlen_length);
2517 value.AppendString (count_str.GetData());
2518 }
Jim Ingham7089d8a2011-10-28 23:14:11 +00002519 else if (var_name == GetSettingNameForPlatformAvoid())
2520 {
2521 if (m_breakpoints_use_platform_avoid)
2522 value.AppendString ("true");
2523 else
2524 value.AppendString ("false");
2525 }
Greg Claytonabb33022011-11-08 02:43:13 +00002526 else if (var_name == GetSettingNameForRunArgs())
2527 {
2528 if (m_run_args.GetArgumentCount() > 0)
2529 {
2530 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2531 value.AppendString (m_run_args.GetArgumentAtIndex (i));
2532 }
2533 }
2534 else if (var_name == GetSettingNameForEnvVars())
2535 {
2536 GetHostEnvironmentIfNeeded ();
2537
2538 if (m_env_vars.size() > 0)
2539 {
2540 std::map<std::string, std::string>::iterator pos;
2541 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2542 {
2543 StreamString value_str;
2544 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2545 value.AppendString (value_str.GetData());
2546 }
2547 }
2548 }
2549 else if (var_name == GetSettingNameForInputPath())
2550 {
2551 value.AppendString (m_input_path.c_str());
2552 }
2553 else if (var_name == GetSettingNameForOutputPath())
2554 {
2555 value.AppendString (m_output_path.c_str());
2556 }
2557 else if (var_name == GetSettingNameForErrorPath())
2558 {
2559 value.AppendString (m_error_path.c_str());
2560 }
2561 else if (var_name == GetSettingNameForInheritHostEnv())
2562 {
2563 if (m_inherit_host_env)
2564 value.AppendString ("true");
2565 else
2566 value.AppendString ("false");
2567 }
2568 else if (var_name == GetSettingNameForDisableASLR())
2569 {
2570 if (m_disable_aslr)
2571 value.AppendString ("true");
2572 else
2573 value.AppendString ("false");
2574 }
2575 else if (var_name == GetSettingNameForDisableSTDIO())
2576 {
2577 if (m_disable_stdio)
2578 value.AppendString ("true");
2579 else
2580 value.AppendString ("false");
2581 }
Sean Callanan77e93942010-10-29 00:29:03 +00002582 else
2583 {
2584 if (err)
2585 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2586 return false;
2587 }
Sean Callanan77e93942010-10-29 00:29:03 +00002588 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002589}
2590
Greg Claytonabb33022011-11-08 02:43:13 +00002591void
2592Target::TargetInstanceSettings::GetHostEnvironmentIfNeeded ()
2593{
2594 if (m_inherit_host_env && !m_got_host_env)
2595 {
2596 m_got_host_env = true;
2597 StringList host_env;
2598 const size_t host_env_count = Host::GetEnvironment (host_env);
2599 for (size_t idx=0; idx<host_env_count; idx++)
2600 {
2601 const char *env_entry = host_env.GetStringAtIndex (idx);
2602 if (env_entry)
2603 {
2604 const char *equal_pos = ::strchr(env_entry, '=');
2605 if (equal_pos)
2606 {
2607 std::string key (env_entry, equal_pos - env_entry);
2608 std::string value (equal_pos + 1);
2609 if (m_env_vars.find (key) == m_env_vars.end())
2610 m_env_vars[key] = value;
2611 }
2612 }
2613 }
2614 }
2615}
2616
2617
2618size_t
2619Target::TargetInstanceSettings::GetEnvironmentAsArgs (Args &env)
2620{
2621 GetHostEnvironmentIfNeeded ();
2622
2623 dictionary::const_iterator pos, end = m_env_vars.end();
2624 for (pos = m_env_vars.begin(); pos != end; ++pos)
2625 {
2626 std::string env_var_equal_value (pos->first);
2627 env_var_equal_value.append(1, '=');
2628 env_var_equal_value.append (pos->second);
2629 env.AppendArgument (env_var_equal_value.c_str());
2630 }
2631 return env.GetArgumentCount();
2632}
2633
2634
Caroline Tice5bc8c972010-09-20 20:44:43 +00002635const ConstString
2636TargetInstanceSettings::CreateInstanceName ()
2637{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002638 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002639 static int instance_count = 1;
2640
Caroline Tice5bc8c972010-09-20 20:44:43 +00002641 sstr.Printf ("target_%d", instance_count);
2642 ++instance_count;
2643
2644 const ConstString ret_val (sstr.GetData());
2645 return ret_val;
2646}
2647
2648//--------------------------------------------------
2649// Target::SettingsController Variable Tables
2650//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002651OptionEnumValueElement
2652TargetInstanceSettings::g_dynamic_value_types[] =
2653{
Greg Clayton577fbc32011-05-30 00:39:48 +00002654{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2655{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2656{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002657{ 0, NULL, NULL }
2658};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002659
2660SettingEntry
2661Target::SettingsController::global_settings_table[] =
2662{
Greg Claytond284b662011-02-18 01:44:25 +00002663 // var-name var-type default enum init'd hidden help-text
2664 // ================= ================== =========== ==== ====== ====== =========================================================================
2665 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2666 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2667};
2668
Caroline Tice5bc8c972010-09-20 20:44:43 +00002669SettingEntry
2670Target::SettingsController::instance_settings_table[] =
2671{
Enrico Granata91544802011-09-06 19:20:51 +00002672 // var-name var-type default enum init'd hidden help-text
2673 // ================= ================== =============== ======================= ====== ====== =========================================================================
2674 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2675 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2676 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2677 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2678 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2679 { 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 +00002680 { 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 +00002681 { TSC_RUN_ARGS , eSetVarTypeArray , NULL , NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2682 { 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." },
2683 { TSC_INHERIT_ENV , eSetVarTypeBoolean, "true" , NULL, false, false, "Inherit the environment from the process that is running LLDB." },
2684 { TSC_STDIN_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for reading its standard input." },
2685 { TSC_STDOUT_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard output." },
2686 { TSC_STDERR_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard error." },
2687// { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
2688 { TSC_DISABLE_ASLR , eSetVarTypeBoolean, "true" , NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2689 { 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 +00002690 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002691};