blob: 46ce1d32caf9853dbeaf0ef0a25e118fe42f8615 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- 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 Ingham969795f2011-09-21 01:17:1319#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:2420#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:2521#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5922#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2423#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Caroline Tice969ed3d12011-05-02 20:41:4625#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner30fdc8d2010-06-08 16:52:2426#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5927#include "lldb/Core/Timer.h"
28#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:1929#include "lldb/Expression/ClangASTSource.h"
Greg Claytoneb0103f2011-04-07 22:46:3530#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:2431#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:5932#include "lldb/Interpreter/CommandInterpreter.h"
33#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2434#include "lldb/lldb-private-log.h"
35#include "lldb/Symbol/ObjectFile.h"
36#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5937#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:5938#include "lldb/Target/Thread.h"
39#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:2440
41using namespace lldb;
42using namespace lldb_private;
43
44//----------------------------------------------------------------------
45// Target constructor
46//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:5147Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
48 Broadcaster ("lldb.target"),
49 ExecutionContextScope (),
Greg Claytondbe54502010-11-19 03:46:0150 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2951 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:5152 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:2353 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:5154 m_arch (target_arch),
55 m_images (),
Greg Claytonf5e56de2010-09-14 23:36:4056 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2457 m_breakpoint_list (false),
58 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:2559 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:5160 m_process_sp (),
61 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:2462 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5963 m_scratch_ast_context_ap (NULL),
Sean Callanan686b2312011-11-16 18:20:4764 m_scratch_ast_source_ap (NULL),
65 m_ast_importer_ap (NULL),
Jim Ingham9575d842011-03-11 03:53:5966 m_persistent_variables (),
Jim Inghame37d6052011-09-13 00:29:5667 m_source_manager(*this),
Greg Clayton32e0a752011-03-30 18:16:5168 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:1469 m_stop_hook_next_id (0),
70 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:2471{
Greg Claytoncfd1ace2010-10-31 03:01:0672 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
73 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
74 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
75
Greg Clayton2d4edfb2010-11-06 01:53:3076 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2477 if (log)
78 log->Printf ("%p Target::Target()", this);
79}
80
81//----------------------------------------------------------------------
82// Destructor
83//----------------------------------------------------------------------
84Target::~Target()
85{
Greg Clayton2d4edfb2010-11-06 01:53:3086 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2487 if (log)
88 log->Printf ("%p Target::~Target()", this);
89 DeleteCurrentProcess ();
90}
91
92void
Caroline Ticeceb6b132010-10-26 03:11:1393Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2494{
Greg Clayton89411422010-10-08 00:21:0595// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1396 if (description_level != lldb::eDescriptionLevelBrief)
97 {
98 s->Indent();
99 s->PutCString("Target\n");
100 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35101 m_images.Dump(s);
102 m_breakpoint_list.Dump(s);
103 m_internal_breakpoint_list.Dump(s);
104 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13105 }
106 else
107 {
Greg Claytonaa149cb2011-08-11 02:48:45108 Module *exe_module = GetExecutableModulePointer();
109 if (exe_module)
110 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28111 else
112 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13113 }
Chris Lattner30fdc8d2010-06-08 16:52:24114}
115
116void
117Target::DeleteCurrentProcess ()
118{
119 if (m_process_sp.get())
120 {
Greg Clayton17f69202010-09-14 23:52:43121 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24122 if (m_process_sp->IsAlive())
123 m_process_sp->Destroy();
Jim Inghamd0a3e12b2011-02-16 17:54:55124
125 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24126
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 Chen01a67862011-10-14 00:42:25132 // Disable watchpoints just on the debugger side.
133 DisableAllWatchpoints(false);
Chris Lattner30fdc8d2010-06-08 16:52:24134 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 Clayton4d122c42011-09-17 08:33:22155 // 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 Lattner30fdc8d2010-06-08 16:52:24158}
159
Greg Clayton3418c852011-08-10 02:10:13160void
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 Chen01a67862011-10-14 00:42:25173 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13174 m_search_filter_sp.reset();
175 m_image_search_paths.Clear(notify);
176 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19177 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47178 m_ast_importer_ap.reset();
Greg Clayton3418c852011-08-10 02:10:13179 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 Lattner30fdc8d2010-06-08 16:52:24186BreakpointList &
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 Ingham87df91b2011-09-23 00:54:11218Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
219 const FileSpecList *source_file_spec_list,
Jim Ingham969795f2011-09-21 01:17:13220 RegularExpression &source_regex,
221 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24222{
Jim Ingham87df91b2011-09-23 00:54:11223 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
224 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham969795f2011-09-21 01:17:13225 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 Lattner30fdc8d2010-06-08 16:52:24233 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
234 return CreateBreakpoint (filter_sp, resolver_sp, internal);
235}
236
237
238BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41239Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24240{
Chris Lattner30fdc8d2010-06-08 16:52:24241 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 Clayton1b72fcb2010-08-24 00:45:41245 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40246 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41247 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 Lattner30fdc8d2010-06-08 16:52:24253 return bp_sp;
254}
255
256BreakpointSP
257Target::CreateBreakpoint (Address &addr, bool internal)
258{
259 TargetSP target_sp = this->GetSP();
Jim Inghamc6674fd2011-10-28 23:14:11260 SearchFilterSP filter_sp(new SearchFilterForNonModuleSpecificSearches (target_sp));
Chris Lattner30fdc8d2010-06-08 16:52:24261 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
262 return CreateBreakpoint (filter_sp, resolver_sp, internal);
263}
264
265BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11266Target::CreateBreakpoint (const FileSpecList *containingModules,
267 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17268 const char *func_name,
269 uint32_t func_name_type_mask,
270 bool internal,
271 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24272{
Greg Clayton0c5cd902010-06-28 21:30:43273 BreakpointSP bp_sp;
274 if (func_name)
275 {
Jim Ingham87df91b2011-09-23 00:54:11276 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17277
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 Clayton0c5cd902010-06-28 21:30:43283 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
284 }
285 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24286}
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 Inghamc6674fd2011-10-28 23:14:11303 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (target_sp));
Chris Lattner30fdc8d2010-06-08 16:52:24304 filter_sp = m_search_filter_sp;
305 }
306 return filter_sp;
307}
308
Jim Ingham969795f2011-09-21 01:17:13309SearchFilterSP
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 Inghamc6674fd2011-10-28 23:14:11323 m_search_filter_sp.reset (new SearchFilterForNonModuleSpecificSearches (target_sp));
Jim Ingham969795f2011-09-21 01:17:13324 filter_sp = m_search_filter_sp;
325 }
326 return filter_sp;
327}
328
Jim Ingham87df91b2011-09-23 00:54:11329SearchFilterSP
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 Lattner30fdc8d2010-06-08 16:52:24351BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11352Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
353 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17354 RegularExpression &func_regex,
355 bool internal,
356 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24357{
Jim Ingham87df91b2011-09-23 00:54:11358 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17359 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
360 func_regex,
361 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24362
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 Clayton9fed0d82010-07-23 23:33:17376 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24377 else
Greg Clayton9fed0d82010-07-23 23:33:17378 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24379
Greg Clayton2d4edfb2010-11-06 01:53:30380 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24381 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 Lattner30fdc8d2010-06-08 16:52:24388 bp_sp->ResolveBreakpoint();
389 }
Jim Ingham36f3b362010-10-14 23:45:03390
391 if (!internal && bp_sp)
392 {
393 m_last_created_breakpoint = bp_sp;
394 }
395
Chris Lattner30fdc8d2010-06-08 16:52:24396 return bp_sp;
397}
398
Johnny Chen86364b42011-09-20 23:28:55399bool
400Target::ProcessIsValid()
401{
402 return (m_process_sp && m_process_sp->IsAlive());
403}
404
Johnny Chen01a67862011-10-14 00:42:25405// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03406// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25407WatchpointSP
408Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type)
Johnny Chen887062a2011-09-12 23:38:44409{
Johnny Chen0c406372011-09-14 20:23:45410 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 Chen01a67862011-10-14 00:42:25415 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55416 if (!ProcessIsValid())
Johnny Chen01a67862011-10-14 00:42:25417 return wp_sp;
Johnny Chen45e541f2011-09-14 22:20:15418 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen01a67862011-10-14 00:42:25419 return wp_sp;
Johnny Chen7313a642011-09-13 01:15:36420
Johnny Chen01a67862011-10-14 00:42:25421 // 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 Chen3c532582011-09-13 23:29:31424 if (matched_sp)
425 {
Johnny Chen0c406372011-09-14 20:23:45426 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31427 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45428 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
429 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25430 // Return the existing watchpoint if both size and type match.
Johnny Chen45e541f2011-09-14 22:20:15431 if (size == old_size && type == old_type) {
Johnny Chen01a67862011-10-14 00:42:25432 wp_sp = matched_sp;
433 wp_sp->SetEnabled(false);
Johnny Chen45e541f2011-09-14 22:20:15434 } else {
Johnny Chen01a67862011-10-14 00:42:25435 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen45e541f2011-09-14 22:20:15436 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chen01a67862011-10-14 00:42:25437 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen45e541f2011-09-14 22:20:15438 }
Johnny Chen3c532582011-09-13 23:29:31439 }
440
Johnny Chen01a67862011-10-14 00:42:25441 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 Chen45e541f2011-09-14 22:20:15446 }
Johnny Chen01a67862011-10-14 00:42:25447 new_wp->SetWatchpointType(type);
448 new_wp->SetTarget(this);
449 wp_sp.reset(new_wp);
450 m_watchpoint_list.Add(wp_sp);
Johnny Chen45e541f2011-09-14 22:20:15451 }
Johnny Chen0c406372011-09-14 20:23:45452
Johnny Chen01a67862011-10-14 00:42:25453 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen0c406372011-09-14 20:23:45454 if (log)
455 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
456 __FUNCTION__,
457 rc.Success() ? "succeeded" : "failed",
Johnny Chen01a67862011-10-14 00:42:25458 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45459
Johnny Chen9d954d82011-09-27 20:29:45460 if (rc.Fail())
Johnny Chen01a67862011-10-14 00:42:25461 wp_sp.reset();
Johnny Chen9d954d82011-09-27 20:29:45462 else
Johnny Chen01a67862011-10-14 00:42:25463 m_last_created_watchpoint = wp_sp;
464 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44465}
466
Chris Lattner30fdc8d2010-06-08 16:52:24467void
468Target::RemoveAllBreakpoints (bool internal_also)
469{
Greg Clayton2d4edfb2010-11-06 01:53:30470 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24471 if (log)
472 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
473
Greg Clayton9fed0d82010-07-23 23:33:17474 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24475 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17476 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03477
478 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24479}
480
481void
482Target::DisableAllBreakpoints (bool internal_also)
483{
Greg Clayton2d4edfb2010-11-06 01:53:30484 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24485 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 Clayton2d4edfb2010-11-06 01:53:30496 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24497 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 Clayton2d4edfb2010-11-06 01:53:30508 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24509 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 Clayton9fed0d82010-07-23 23:33:17515 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24516 else
Jim Ingham36f3b362010-10-14 23:45:03517 {
Greg Claytonaa1c5872011-01-24 23:35:47518 if (m_last_created_breakpoint)
519 {
520 if (m_last_created_breakpoint->GetID() == break_id)
521 m_last_created_breakpoint.reset();
522 }
Greg Clayton9fed0d82010-07-23 23:33:17523 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03524 }
Chris Lattner30fdc8d2010-06-08 16:52:24525 return true;
526 }
527 return false;
528}
529
530bool
531Target::DisableBreakpointByID (break_id_t break_id)
532{
Greg Clayton2d4edfb2010-11-06 01:53:30533 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24534 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 Clayton2d4edfb2010-11-06 01:53:30554 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24555 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 Chenedf50372011-09-23 21:21:43576// 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 Chen01a67862011-10-14 00:42:25579// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
580// to end operations.
Johnny Chen86364b42011-09-20 23:28:55581bool
Johnny Chen01a67862011-10-14 00:42:25582Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55583{
584 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
585 if (log)
586 log->Printf ("Target::%s\n", __FUNCTION__);
587
Johnny Chenedf50372011-09-23 21:21:43588 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25589 m_watchpoint_list.RemoveAll();
Johnny Chenedf50372011-09-23 21:21:43590 return true;
591 }
592
593 // Otherwise, it's an end to end operation.
594
Johnny Chen86364b42011-09-20 23:28:55595 if (!ProcessIsValid())
596 return false;
597
Johnny Chen01a67862011-10-14 00:42:25598 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55599 for (size_t i = 0; i < num_watchpoints; ++i)
600 {
Johnny Chen01a67862011-10-14 00:42:25601 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
602 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55603 return false;
604
Johnny Chen01a67862011-10-14 00:42:25605 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55606 if (rc.Fail())
607 return false;
608 }
Johnny Chen01a67862011-10-14 00:42:25609 m_watchpoint_list.RemoveAll ();
Johnny Chen86364b42011-09-20 23:28:55610 return true; // Success!
611}
612
Johnny Chen01a67862011-10-14 00:42:25613// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
614// end operations.
Johnny Chen86364b42011-09-20 23:28:55615bool
Johnny Chen01a67862011-10-14 00:42:25616Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55617{
618 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
619 if (log)
620 log->Printf ("Target::%s\n", __FUNCTION__);
621
Johnny Chenedf50372011-09-23 21:21:43622 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25623 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43624 return true;
625 }
626
627 // Otherwise, it's an end to end operation.
628
Johnny Chen86364b42011-09-20 23:28:55629 if (!ProcessIsValid())
630 return false;
631
Johnny Chen01a67862011-10-14 00:42:25632 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55633 for (size_t i = 0; i < num_watchpoints; ++i)
634 {
Johnny Chen01a67862011-10-14 00:42:25635 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
636 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55637 return false;
638
Johnny Chen01a67862011-10-14 00:42:25639 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55640 if (rc.Fail())
641 return false;
642 }
Johnny Chen86364b42011-09-20 23:28:55643 return true; // Success!
644}
645
Johnny Chen01a67862011-10-14 00:42:25646// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
647// end operations.
Johnny Chen86364b42011-09-20 23:28:55648bool
Johnny Chen01a67862011-10-14 00:42:25649Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55650{
651 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
652 if (log)
653 log->Printf ("Target::%s\n", __FUNCTION__);
654
Johnny Chenedf50372011-09-23 21:21:43655 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25656 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43657 return true;
658 }
659
660 // Otherwise, it's an end to end operation.
661
Johnny Chen86364b42011-09-20 23:28:55662 if (!ProcessIsValid())
663 return false;
664
Johnny Chen01a67862011-10-14 00:42:25665 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55666 for (size_t i = 0; i < num_watchpoints; ++i)
667 {
Johnny Chen01a67862011-10-14 00:42:25668 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
669 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55670 return false;
671
Johnny Chen01a67862011-10-14 00:42:25672 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55673 if (rc.Fail())
674 return false;
675 }
Johnny Chen86364b42011-09-20 23:28:55676 return true; // Success!
677}
678
Johnny Chen01a67862011-10-14 00:42:25679// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46680// during these operations.
681bool
Johnny Chen01a67862011-10-14 00:42:25682Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46683{
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 Chen01a67862011-10-14 00:42:25691 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46692 for (size_t i = 0; i < num_watchpoints; ++i)
693 {
Johnny Chen01a67862011-10-14 00:42:25694 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
695 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46696 return false;
697
Johnny Chen01a67862011-10-14 00:42:25698 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46699 }
700 return true; // Success!
701}
702
Johnny Chen01a67862011-10-14 00:42:25703// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55704bool
Johnny Chen01a67862011-10-14 00:42:25705Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55706{
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 Chen01a67862011-10-14 00:42:25714 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
715 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55716 {
Johnny Chen01a67862011-10-14 00:42:25717 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58718 if (rc.Success())
719 return true;
Johnny Chen86364b42011-09-20 23:28:55720
Johnny Chenf04ee932011-09-22 18:04:58721 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55722 }
723 return false;
724}
725
Johnny Chen01a67862011-10-14 00:42:25726// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55727bool
Johnny Chen01a67862011-10-14 00:42:25728Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55729{
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 Chen01a67862011-10-14 00:42:25737 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
738 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55739 {
Johnny Chen01a67862011-10-14 00:42:25740 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58741 if (rc.Success())
742 return true;
Johnny Chen86364b42011-09-20 23:28:55743
Johnny Chenf04ee932011-09-22 18:04:58744 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55745 }
746 return false;
747}
748
Johnny Chen01a67862011-10-14 00:42:25749// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55750bool
Johnny Chen01a67862011-10-14 00:42:25751Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55752{
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 Chen01a67862011-10-14 00:42:25757 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55758 {
Johnny Chen01a67862011-10-14 00:42:25759 m_watchpoint_list.Remove(watch_id);
Johnny Chen86364b42011-09-20 23:28:55760 return true;
761 }
762 return false;
763}
764
Johnny Chen01a67862011-10-14 00:42:25765// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46766bool
Johnny Chen01a67862011-10-14 00:42:25767Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46768{
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 Chen01a67862011-10-14 00:42:25776 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
777 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46778 {
Johnny Chen01a67862011-10-14 00:42:25779 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46780 return true;
781 }
782 return false;
783}
784
Chris Lattner30fdc8d2010-06-08 16:52:24785ModuleSP
786Target::GetExecutableModule ()
787{
Greg Claytonaa149cb2011-08-11 02:48:45788 return m_images.GetModuleAtIndex(0);
789}
790
791Module*
792Target::GetExecutableModulePointer ()
793{
794 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24795}
796
797void
798Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
799{
800 m_images.Clear();
801 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19802 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47803 m_ast_importer_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24804
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 Ingham5aee1622010-08-09 23:31:02814 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton32e0a752011-03-30 18:16:51815 if (!m_arch.IsValid())
816 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02817
Chris Lattner30fdc8d2010-06-08 16:52:24818 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15819 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24820
Greg Claytoncac9c5f2011-09-24 00:52:29821 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24822 {
823 executable_objfile->GetDependentModules(dependent_files);
824 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
825 {
Greg Claytonded470d2011-03-19 01:12:21826 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
827 FileSpec platform_dependent_file_spec;
828 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55829 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21830 else
831 platform_dependent_file_spec = dependent_file_spec;
832
833 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton32e0a752011-03-30 18:16:51834 m_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24835 if (image_module_sp.get())
836 {
Chris Lattner30fdc8d2010-06-08 16:52:24837 ObjectFile *objfile = image_module_sp->GetObjectFile();
838 if (objfile)
839 objfile->GetDependentModules(dependent_files);
840 }
841 }
842 }
Chris Lattner30fdc8d2010-06-08 16:52:24843 }
Caroline Tice1559a462010-09-27 00:30:10844
845 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24846}
847
848
Jim Ingham5aee1622010-08-09 23:31:02849bool
850Target::SetArchitecture (const ArchSpec &arch_spec)
851{
Greg Clayton32e0a752011-03-30 18:16:51852 if (m_arch == arch_spec)
Jim Ingham5aee1622010-08-09 23:31:02853 {
854 // If we're setting the architecture to our current architecture, we
855 // don't need to do anything.
856 return true;
857 }
Greg Clayton32e0a752011-03-30 18:16:51858 else if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02859 {
860 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton32e0a752011-03-30 18:16:51861 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02862 return true;
863 }
864 else
865 {
866 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51867 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02868 ModuleSP executable_sp = GetExecutableModule ();
869 m_images.Clear();
870 m_scratch_ast_context_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47871 m_scratch_ast_source_ap.reset();
872 m_ast_importer_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02873 // Need to do something about unsetting breakpoints.
874
875 if (executable_sp)
876 {
877 FileSpec exec_file_spec = executable_sp->GetFileSpec();
878 Error error = ModuleList::GetSharedModule(exec_file_spec,
879 arch_spec,
880 NULL,
881 NULL,
882 0,
883 executable_sp,
884 NULL,
885 NULL);
886
887 if (!error.Fail() && executable_sp)
888 {
889 SetExecutableModule (executable_sp, true);
890 return true;
891 }
892 else
893 {
894 return false;
895 }
896 }
897 else
898 {
899 return false;
900 }
901 }
902}
Chris Lattner30fdc8d2010-06-08 16:52:24903
Chris Lattner30fdc8d2010-06-08 16:52:24904void
905Target::ModuleAdded (ModuleSP &module_sp)
906{
907 // A module is being added to this target for the first time
908 ModuleList module_list;
909 module_list.Append(module_sp);
910 ModulesDidLoad (module_list);
911}
912
913void
914Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
915{
Jim Inghame716ae02011-08-03 01:00:06916 // A module is replacing an already added module
Chris Lattner30fdc8d2010-06-08 16:52:24917 ModuleList module_list;
918 module_list.Append (old_module_sp);
919 ModulesDidUnload (module_list);
920 module_list.Clear ();
921 module_list.Append (new_module_sp);
922 ModulesDidLoad (module_list);
923}
924
925void
926Target::ModulesDidLoad (ModuleList &module_list)
927{
928 m_breakpoint_list.UpdateBreakpoints (module_list, true);
929 // TODO: make event data that packages up the module_list
930 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
931}
932
933void
934Target::ModulesDidUnload (ModuleList &module_list)
935{
936 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26937
938 // Remove the images from the target image list
939 m_images.Remove(module_list);
940
Chris Lattner30fdc8d2010-06-08 16:52:24941 // TODO: make event data that packages up the module_list
942 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
943}
944
Jim Inghamc6674fd2011-10-28 23:14:11945
Daniel Dunbarf9f70322011-10-31 22:50:37946bool
Jim Inghamc6674fd2011-10-28 23:14:11947Target::ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_spec)
948{
949
950 if (!m_breakpoints_use_platform_avoid)
951 return false;
952 else
953 {
954 ModuleList matchingModules;
955 const ArchSpec *arch_ptr = NULL;
956 const lldb_private::UUID *uuid_ptr= NULL;
957 const ConstString *object_name = NULL;
958 size_t num_modules = GetImages().FindModules(&module_spec, arch_ptr, uuid_ptr, object_name, matchingModules);
959
960 // If there is more than one module for this file spec, only return true if ALL the modules are on the
961 // black list.
962 if (num_modules > 0)
963 {
964 for (int i = 0; i < num_modules; i++)
965 {
966 if (!ModuleIsExcludedForNonModuleSpecificSearches (matchingModules.GetModuleAtIndex(i)))
967 return false;
968 }
969 return true;
970 }
971 else
972 return false;
973 }
974}
975
Daniel Dunbarf9f70322011-10-31 22:50:37976bool
Jim Inghamc6674fd2011-10-28 23:14:11977Target::ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp)
978{
979 if (!m_breakpoints_use_platform_avoid)
980 return false;
981 else if (GetPlatform())
982 {
983 return GetPlatform()->ModuleIsExcludedForNonModuleSpecificSearches (*this, module_sp);
984 }
985 else
986 return false;
987}
988
Chris Lattner30fdc8d2010-06-08 16:52:24989size_t
Greg Claytondb598232011-01-07 01:57:07990Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
991{
992 const Section *section = addr.GetSection();
993 if (section && section->GetModule())
994 {
995 ObjectFile *objfile = section->GetModule()->GetObjectFile();
996 if (objfile)
997 {
998 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
999 addr.GetOffset(),
1000 dst,
1001 dst_len);
1002 if (bytes_read > 0)
1003 return bytes_read;
1004 else
1005 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
1006 }
1007 else
1008 {
1009 error.SetErrorString("address isn't from a object file");
1010 }
1011 }
1012 else
1013 {
1014 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
1015 }
1016 return 0;
1017}
1018
1019size_t
Enrico Granata9128ee2f2011-09-06 19:20:511020Target::ReadMemory (const Address& addr,
1021 bool prefer_file_cache,
1022 void *dst,
1023 size_t dst_len,
1024 Error &error,
1025 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:241026{
Chris Lattner30fdc8d2010-06-08 16:52:241027 error.Clear();
Greg Claytondb598232011-01-07 01:57:071028
Enrico Granata9128ee2f2011-09-06 19:20:511029 // if we end up reading this from process memory, we will fill this
1030 // with the actual load address
1031 if (load_addr_ptr)
1032 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1033
Greg Claytondb598232011-01-07 01:57:071034 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:021035
1036 addr_t load_addr = LLDB_INVALID_ADDRESS;
1037 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:581038 Address resolved_addr;
1039 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:031040 {
Greg Claytond16e1e52011-07-12 17:06:171041 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:021042 {
Greg Claytond16e1e52011-07-12 17:06:171043 // No sections are loaded, so we must assume we are not running
1044 // yet and anything we are given is a file address.
1045 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1046 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:021047 }
Greg Claytondda4f7b2010-06-30 23:03:031048 else
Greg Claytonc749eb82011-07-11 05:12:021049 {
Greg Claytond16e1e52011-07-12 17:06:171050 // We have at least one section loaded. This can be becuase
1051 // we have manually loaded some sections with "target modules load ..."
1052 // or because we have have a live process that has sections loaded
1053 // through the dynamic loader
1054 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1055 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:021056 }
Greg Claytondda4f7b2010-06-30 23:03:031057 }
Greg Clayton357132e2011-03-26 19:14:581058 if (!resolved_addr.IsValid())
1059 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:031060
Greg Claytonc749eb82011-07-11 05:12:021061
Greg Claytondb598232011-01-07 01:57:071062 if (prefer_file_cache)
1063 {
1064 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1065 if (bytes_read > 0)
1066 return bytes_read;
1067 }
Greg Claytondda4f7b2010-06-30 23:03:031068
Johnny Chen86364b42011-09-20 23:28:551069 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:031070 {
Greg Claytonc749eb82011-07-11 05:12:021071 if (load_addr == LLDB_INVALID_ADDRESS)
1072 load_addr = resolved_addr.GetLoadAddress (this);
1073
Greg Claytondda4f7b2010-06-30 23:03:031074 if (load_addr == LLDB_INVALID_ADDRESS)
1075 {
1076 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
Greg Clayton86edbf42011-10-26 00:56:271077 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Claytondda4f7b2010-06-30 23:03:031078 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:081079 resolved_addr.GetFileAddress(),
1080 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:031081 else
Greg Clayton86edbf42011-10-26 00:56:271082 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:031083 }
1084 else
1085 {
Greg Claytondb598232011-01-07 01:57:071086 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:241087 if (bytes_read != dst_len)
1088 {
1089 if (error.Success())
1090 {
1091 if (bytes_read == 0)
Greg Clayton86edbf42011-10-26 00:56:271092 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:241093 else
Greg Clayton86edbf42011-10-26 00:56:271094 error.SetErrorStringWithFormat("only %zu of %zu bytes were read from memory at 0x%llx", bytes_read, dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:241095 }
1096 }
Greg Claytondda4f7b2010-06-30 23:03:031097 if (bytes_read)
Enrico Granata9128ee2f2011-09-06 19:20:511098 {
1099 if (load_addr_ptr)
1100 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:031101 return bytes_read;
Enrico Granata9128ee2f2011-09-06 19:20:511102 }
Greg Claytondda4f7b2010-06-30 23:03:031103 // If the address is not section offset we have an address that
1104 // doesn't resolve to any address in any currently loaded shared
1105 // libaries and we failed to read memory so there isn't anything
1106 // more we can do. If it is section offset, we might be able to
1107 // read cached memory from the object file.
1108 if (!resolved_addr.IsSectionOffset())
1109 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:241110 }
Chris Lattner30fdc8d2010-06-08 16:52:241111 }
Greg Claytondda4f7b2010-06-30 23:03:031112
Greg Claytonc749eb82011-07-11 05:12:021113 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:031114 {
Greg Claytondb598232011-01-07 01:57:071115 // If we didn't already try and read from the object file cache, then
1116 // try it after failing to read from the process.
1117 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:031118 }
1119 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:241120}
1121
Greg Claytond16e1e52011-07-12 17:06:171122size_t
1123Target::ReadScalarIntegerFromMemory (const Address& addr,
1124 bool prefer_file_cache,
1125 uint32_t byte_size,
1126 bool is_signed,
1127 Scalar &scalar,
1128 Error &error)
1129{
1130 uint64_t uval;
1131
1132 if (byte_size <= sizeof(uval))
1133 {
1134 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1135 if (bytes_read == byte_size)
1136 {
1137 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1138 uint32_t offset = 0;
1139 if (byte_size <= 4)
1140 scalar = data.GetMaxU32 (&offset, byte_size);
1141 else
1142 scalar = data.GetMaxU64 (&offset, byte_size);
1143
1144 if (is_signed)
1145 scalar.SignExtend(byte_size * 8);
1146 return bytes_read;
1147 }
1148 }
1149 else
1150 {
1151 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1152 }
1153 return 0;
1154}
1155
1156uint64_t
1157Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1158 bool prefer_file_cache,
1159 size_t integer_byte_size,
1160 uint64_t fail_value,
1161 Error &error)
1162{
1163 Scalar scalar;
1164 if (ReadScalarIntegerFromMemory (addr,
1165 prefer_file_cache,
1166 integer_byte_size,
1167 false,
1168 scalar,
1169 error))
1170 return scalar.ULongLong(fail_value);
1171 return fail_value;
1172}
1173
1174bool
1175Target::ReadPointerFromMemory (const Address& addr,
1176 bool prefer_file_cache,
1177 Error &error,
1178 Address &pointer_addr)
1179{
1180 Scalar scalar;
1181 if (ReadScalarIntegerFromMemory (addr,
1182 prefer_file_cache,
1183 m_arch.GetAddressByteSize(),
1184 false,
1185 scalar,
1186 error))
1187 {
1188 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1189 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1190 {
1191 if (m_section_load_list.IsEmpty())
1192 {
1193 // No sections are loaded, so we must assume we are not running
1194 // yet and anything we are given is a file address.
1195 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1196 }
1197 else
1198 {
1199 // We have at least one section loaded. This can be becuase
1200 // we have manually loaded some sections with "target modules load ..."
1201 // or because we have have a live process that has sections loaded
1202 // through the dynamic loader
1203 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1204 }
1205 // We weren't able to resolve the pointer value, so just return
1206 // an address with no section
1207 if (!pointer_addr.IsValid())
1208 pointer_addr.SetOffset (pointer_vm_addr);
1209 return true;
1210
1211 }
1212 }
1213 return false;
1214}
Chris Lattner30fdc8d2010-06-08 16:52:241215
1216ModuleSP
1217Target::GetSharedModule
1218(
1219 const FileSpec& file_spec,
1220 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:101221 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:241222 const ConstString *object_name,
1223 off_t object_offset,
1224 Error *error_ptr
1225)
1226{
1227 // Don't pass in the UUID so we can tell if we have a stale value in our list
1228 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1229 bool did_create_module = false;
1230 ModuleSP module_sp;
1231
Chris Lattner30fdc8d2010-06-08 16:52:241232 Error error;
1233
Greg Clayton32e0a752011-03-30 18:16:511234 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner30fdc8d2010-06-08 16:52:241235 if (m_image_search_paths.GetSize())
1236 {
1237 FileSpec transformed_spec;
1238 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1239 {
1240 transformed_spec.GetFilename() = file_spec.GetFilename();
1241 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1242 }
1243 }
1244
Greg Clayton32e0a752011-03-30 18:16:511245 // The platform is responsible for finding and caching an appropriate
1246 // module in the shared module cache.
1247 if (m_platform_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241248 {
Greg Clayton32e0a752011-03-30 18:16:511249 FileSpec platform_file_spec;
1250 error = m_platform_sp->GetSharedModule (file_spec,
1251 arch,
1252 uuid_ptr,
1253 object_name,
1254 object_offset,
1255 module_sp,
1256 &old_module_sp,
1257 &did_create_module);
1258 }
1259 else
1260 {
1261 error.SetErrorString("no platform is currently set");
Chris Lattner30fdc8d2010-06-08 16:52:241262 }
1263
Greg Clayton32e0a752011-03-30 18:16:511264 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner30fdc8d2010-06-08 16:52:241265 if (module_sp)
1266 {
1267 m_images.Append (module_sp);
1268 if (did_create_module)
1269 {
1270 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1271 ModuleUpdated(old_module_sp, module_sp);
1272 else
1273 ModuleAdded(module_sp);
1274 }
1275 }
1276 if (error_ptr)
1277 *error_ptr = error;
1278 return module_sp;
1279}
1280
1281
1282Target *
1283Target::CalculateTarget ()
1284{
1285 return this;
1286}
1287
1288Process *
1289Target::CalculateProcess ()
1290{
1291 return NULL;
1292}
1293
1294Thread *
1295Target::CalculateThread ()
1296{
1297 return NULL;
1298}
1299
1300StackFrame *
1301Target::CalculateStackFrame ()
1302{
1303 return NULL;
1304}
1305
1306void
Greg Clayton0603aa92010-10-04 01:05:561307Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:241308{
Greg Claytonc14ee322011-09-22 04:58:261309 exe_ctx.Clear();
1310 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:241311}
1312
1313PathMappingList &
1314Target::GetImageSearchPathList ()
1315{
1316 return m_image_search_paths;
1317}
1318
1319void
1320Target::ImageSearchPathsChanged
1321(
1322 const PathMappingList &path_list,
1323 void *baton
1324)
1325{
1326 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:451327 ModuleSP exe_module_sp (target->GetExecutableModule());
1328 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241329 {
Greg Claytonaa149cb2011-08-11 02:48:451330 target->m_images.Clear();
1331 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:241332 }
1333}
1334
1335ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:531336Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:241337{
Greg Clayton73da2442011-08-03 01:23:551338 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:531339 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:191340 {
Greg Clayton73da2442011-08-03 01:23:551341 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Sean Callanan4bf80d52011-11-15 22:27:191342 m_scratch_ast_source_ap.reset (new ClangASTSource(GetSP()));
1343 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
1344 llvm::OwningPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
1345 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1346 }
Chris Lattner30fdc8d2010-06-08 16:52:241347 return m_scratch_ast_context_ap.get();
1348}
Caroline Ticedaccaa92010-09-20 20:44:431349
Sean Callanan686b2312011-11-16 18:20:471350ClangASTImporter *
1351Target::GetClangASTImporter()
1352{
1353 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1354
1355 if (!ast_importer)
1356 {
1357 ast_importer = new ClangASTImporter();
1358 m_ast_importer_ap.reset(ast_importer);
1359 }
1360
1361 return ast_importer;
1362}
1363
Greg Clayton99d0faf2010-11-18 23:32:351364void
Caroline Tice20bd37f2011-03-10 22:14:101365Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:431366{
Greg Clayton99d0faf2010-11-18 23:32:351367 UserSettingsControllerSP &usc = GetSettingsController();
1368 usc.reset (new SettingsController);
1369 UserSettingsController::InitializeSettingsController (usc,
1370 SettingsController::global_settings_table,
1371 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:101372
1373 // Now call SettingsInitialize() on each 'child' setting of Target
1374 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:351375}
Caroline Ticedaccaa92010-09-20 20:44:431376
Greg Clayton99d0faf2010-11-18 23:32:351377void
Caroline Tice20bd37f2011-03-10 22:14:101378Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:351379{
Caroline Tice20bd37f2011-03-10 22:14:101380
1381 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1382
1383 Process::SettingsTerminate ();
1384
1385 // Now terminate Target Settings.
1386
Greg Clayton99d0faf2010-11-18 23:32:351387 UserSettingsControllerSP &usc = GetSettingsController();
1388 UserSettingsController::FinalizeSettingsController (usc);
1389 usc.reset();
1390}
Caroline Ticedaccaa92010-09-20 20:44:431391
Greg Clayton99d0faf2010-11-18 23:32:351392UserSettingsControllerSP &
1393Target::GetSettingsController ()
1394{
1395 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:431396 return g_settings_controller;
1397}
1398
1399ArchSpec
1400Target::GetDefaultArchitecture ()
1401{
Greg Clayton64195a22011-02-23 00:35:021402 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1403
1404 if (settings_controller_sp)
1405 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1406 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:431407}
1408
1409void
Greg Clayton64195a22011-02-23 00:35:021410Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:431411{
Greg Clayton64195a22011-02-23 00:35:021412 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1413
1414 if (settings_controller_sp)
1415 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:431416}
1417
Greg Clayton0603aa92010-10-04 01:05:561418Target *
1419Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1420{
1421 // The target can either exist in the "process" of ExecutionContext, or in
1422 // the "target_sp" member of SymbolContext. This accessor helper function
1423 // will get the target from one of these locations.
1424
1425 Target *target = NULL;
1426 if (sc_ptr != NULL)
1427 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:261428 if (target == NULL && exe_ctx_ptr)
1429 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:561430 return target;
1431}
1432
1433
Caroline Tice1559a462010-09-27 00:30:101434void
1435Target::UpdateInstanceName ()
1436{
1437 StreamString sstr;
1438
Greg Claytonaa149cb2011-08-11 02:48:451439 Module *exe_module = GetExecutableModulePointer();
1440 if (exe_module)
Caroline Tice1559a462010-09-27 00:30:101441 {
Greg Clayton307de252010-10-27 02:06:371442 sstr.Printf ("%s_%s",
Greg Claytonaa149cb2011-08-11 02:48:451443 exe_module->GetFileSpec().GetFilename().AsCString(),
1444 exe_module->GetArchitecture().GetArchitectureName());
1445 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:101446 }
1447}
1448
Sean Callanan322f5292010-10-29 00:29:031449const char *
1450Target::GetExpressionPrefixContentsAsCString ()
1451{
Sean Callanan6d6acc82011-11-16 01:54:571452 if (!m_expr_prefix_contents.empty())
1453 return m_expr_prefix_contents.c_str();
Greg Clayton7e14f912011-04-23 02:04:551454 return NULL;
Sean Callanan322f5292010-10-29 00:29:031455}
1456
Greg Clayton8b2fe6d2010-12-14 02:59:591457ExecutionResults
1458Target::EvaluateExpression
1459(
1460 const char *expr_cstr,
1461 StackFrame *frame,
Sean Callanan3bfdaa22011-09-15 02:13:071462 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan20bb3aa2011-12-21 22:22:581463 bool coerce_to_id,
Greg Clayton8b2fe6d2010-12-14 02:59:591464 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:351465 bool keep_in_memory,
Jim Ingham2837b762011-05-04 03:43:181466 lldb::DynamicValueType use_dynamic,
Greg Clayton8b2fe6d2010-12-14 02:59:591467 lldb::ValueObjectSP &result_valobj_sp
1468)
1469{
1470 ExecutionResults execution_results = eExecutionSetupError;
1471
1472 result_valobj_sp.reset();
Greg Claytond1767f02011-12-08 02:13:161473
1474 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1475 return execution_results;
1476
Jim Ingham6026ca32011-05-12 02:06:141477 // We shouldn't run stop hooks in expressions.
1478 // Be sure to reset this if you return anywhere within this function.
1479 bool old_suppress_value = m_suppress_stop_hooks;
1480 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:591481
1482 ExecutionContext exe_ctx;
Greg Claytond1767f02011-12-08 02:13:161483
1484 const size_t expr_cstr_len = ::strlen (expr_cstr);
1485
Greg Clayton8b2fe6d2010-12-14 02:59:591486 if (frame)
1487 {
1488 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:081489 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:181490 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granata27b625e2011-08-09 01:04:561491 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1492 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham2837b762011-05-04 03:43:181493 lldb::VariableSP var_sp;
Greg Claytond1767f02011-12-08 02:13:161494
1495 // Make sure we don't have any things that we know a variable expression
1496 // won't be able to deal with before calling into it
1497 if (::strcspn (expr_cstr, "()+*&|!~<=/^%,?") == expr_cstr_len)
1498 {
1499 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1500 use_dynamic,
1501 expr_path_options,
1502 var_sp,
1503 error);
1504 }
Greg Clayton8b2fe6d2010-12-14 02:59:591505 }
1506 else if (m_process_sp)
1507 {
1508 m_process_sp->CalculateExecutionContext(exe_ctx);
1509 }
1510 else
1511 {
1512 CalculateExecutionContext(exe_ctx);
1513 }
1514
1515 if (result_valobj_sp)
1516 {
1517 execution_results = eExecutionCompleted;
1518 // We got a result from the frame variable expression path above...
1519 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1520
1521 lldb::ValueObjectSP const_valobj_sp;
1522
1523 // Check in case our value is already a constant value
1524 if (result_valobj_sp->GetIsConstant())
1525 {
1526 const_valobj_sp = result_valobj_sp;
1527 const_valobj_sp->SetName (persistent_variable_name);
1528 }
1529 else
Jim Ingham78a685a2011-04-16 00:01:131530 {
Jim Ingham2837b762011-05-04 03:43:181531 if (use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:131532 {
Jim Ingham2837b762011-05-04 03:43:181533 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:131534 if (dynamic_sp)
1535 result_valobj_sp = dynamic_sp;
1536 }
1537
Jim Ingham6035b672011-03-31 00:19:251538 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:131539 }
Greg Clayton8b2fe6d2010-12-14 02:59:591540
Sean Callanan92adcac2011-01-13 08:53:351541 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1542
Greg Clayton8b2fe6d2010-12-14 02:59:591543 result_valobj_sp = const_valobj_sp;
1544
Sean Callanan92adcac2011-01-13 08:53:351545 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1546 assert (clang_expr_variable_sp.get());
1547
1548 // Set flags and live data as appropriate
1549
1550 const Value &result_value = live_valobj_sp->GetValue();
1551
1552 switch (result_value.GetValueType())
1553 {
1554 case Value::eValueTypeHostAddress:
1555 case Value::eValueTypeFileAddress:
1556 // we don't do anything with these for now
1557 break;
1558 case Value::eValueTypeScalar:
1559 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1560 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1561 break;
1562 case Value::eValueTypeLoadAddress:
1563 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1564 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1565 break;
1566 }
Greg Clayton8b2fe6d2010-12-14 02:59:591567 }
1568 else
1569 {
1570 // Make sure we aren't just trying to see the value of a persistent
1571 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:351572 lldb::ClangExpressionVariableSP persistent_var_sp;
1573 // Only check for persistent variables the expression starts with a '$'
1574 if (expr_cstr[0] == '$')
1575 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1576
Greg Clayton8b2fe6d2010-12-14 02:59:591577 if (persistent_var_sp)
1578 {
1579 result_valobj_sp = persistent_var_sp->GetValueObject ();
1580 execution_results = eExecutionCompleted;
1581 }
1582 else
1583 {
1584 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan3bfdaa22011-09-15 02:13:071585
Greg Clayton8b2fe6d2010-12-14 02:59:591586 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:071587 execution_policy,
Sean Callananc7b65062011-11-07 23:35:401588 lldb::eLanguageTypeUnknown,
Sean Callanan20bb3aa2011-12-21 22:22:581589 coerce_to_id ? ClangUserExpression::eResultTypeId : ClangUserExpression::eResultTypeAny,
Sean Callanan92adcac2011-01-13 08:53:351590 unwind_on_error,
Greg Clayton8b2fe6d2010-12-14 02:59:591591 expr_cstr,
1592 prefix,
1593 result_valobj_sp);
1594 }
1595 }
Jim Ingham6026ca32011-05-12 02:06:141596
1597 m_suppress_stop_hooks = old_suppress_value;
1598
Greg Clayton8b2fe6d2010-12-14 02:59:591599 return execution_results;
1600}
1601
Greg Claytonf3ef3d22011-05-22 22:46:531602lldb::addr_t
1603Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1604{
1605 addr_t code_addr = load_addr;
1606 switch (m_arch.GetMachine())
1607 {
1608 case llvm::Triple::arm:
1609 case llvm::Triple::thumb:
1610 switch (addr_class)
1611 {
1612 case eAddressClassData:
1613 case eAddressClassDebug:
1614 return LLDB_INVALID_ADDRESS;
1615
1616 case eAddressClassUnknown:
1617 case eAddressClassInvalid:
1618 case eAddressClassCode:
1619 case eAddressClassCodeAlternateISA:
1620 case eAddressClassRuntime:
1621 // Check if bit zero it no set?
1622 if ((code_addr & 1ull) == 0)
1623 {
1624 // Bit zero isn't set, check if the address is a multiple of 2?
1625 if (code_addr & 2ull)
1626 {
1627 // The address is a multiple of 2 so it must be thumb, set bit zero
1628 code_addr |= 1ull;
1629 }
1630 else if (addr_class == eAddressClassCodeAlternateISA)
1631 {
1632 // We checked the address and the address claims to be the alternate ISA
1633 // which means thumb, so set bit zero.
1634 code_addr |= 1ull;
1635 }
1636 }
1637 break;
1638 }
1639 break;
1640
1641 default:
1642 break;
1643 }
1644 return code_addr;
1645}
1646
1647lldb::addr_t
1648Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1649{
1650 addr_t opcode_addr = load_addr;
1651 switch (m_arch.GetMachine())
1652 {
1653 case llvm::Triple::arm:
1654 case llvm::Triple::thumb:
1655 switch (addr_class)
1656 {
1657 case eAddressClassData:
1658 case eAddressClassDebug:
1659 return LLDB_INVALID_ADDRESS;
1660
1661 case eAddressClassInvalid:
1662 case eAddressClassUnknown:
1663 case eAddressClassCode:
1664 case eAddressClassCodeAlternateISA:
1665 case eAddressClassRuntime:
1666 opcode_addr &= ~(1ull);
1667 break;
1668 }
1669 break;
1670
1671 default:
1672 break;
1673 }
1674 return opcode_addr;
1675}
1676
Jim Ingham9575d842011-03-11 03:53:591677lldb::user_id_t
1678Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1679{
1680 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1681 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1682 m_stop_hooks[new_uid] = new_hook_sp;
1683 return new_uid;
1684}
1685
1686bool
1687Target::RemoveStopHookByID (lldb::user_id_t user_id)
1688{
1689 size_t num_removed;
1690 num_removed = m_stop_hooks.erase (user_id);
1691 if (num_removed == 0)
1692 return false;
1693 else
1694 return true;
1695}
1696
1697void
1698Target::RemoveAllStopHooks ()
1699{
1700 m_stop_hooks.clear();
1701}
1702
1703Target::StopHookSP
1704Target::GetStopHookByID (lldb::user_id_t user_id)
1705{
1706 StopHookSP found_hook;
1707
1708 StopHookCollection::iterator specified_hook_iter;
1709 specified_hook_iter = m_stop_hooks.find (user_id);
1710 if (specified_hook_iter != m_stop_hooks.end())
1711 found_hook = (*specified_hook_iter).second;
1712 return found_hook;
1713}
1714
1715bool
1716Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1717{
1718 StopHookCollection::iterator specified_hook_iter;
1719 specified_hook_iter = m_stop_hooks.find (user_id);
1720 if (specified_hook_iter == m_stop_hooks.end())
1721 return false;
1722
1723 (*specified_hook_iter).second->SetIsActive (active_state);
1724 return true;
1725}
1726
1727void
1728Target::SetAllStopHooksActiveState (bool active_state)
1729{
1730 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1731 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1732 {
1733 (*pos).second->SetIsActive (active_state);
1734 }
1735}
1736
1737void
1738Target::RunStopHooks ()
1739{
Jim Ingham6026ca32011-05-12 02:06:141740 if (m_suppress_stop_hooks)
1741 return;
1742
Jim Ingham9575d842011-03-11 03:53:591743 if (!m_process_sp)
1744 return;
1745
1746 if (m_stop_hooks.empty())
1747 return;
1748
1749 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1750
1751 // If there aren't any active stop hooks, don't bother either:
1752 bool any_active_hooks = false;
1753 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1754 {
1755 if ((*pos).second->IsActive())
1756 {
1757 any_active_hooks = true;
1758 break;
1759 }
1760 }
1761 if (!any_active_hooks)
1762 return;
1763
1764 CommandReturnObject result;
1765
1766 std::vector<ExecutionContext> exc_ctx_with_reasons;
1767 std::vector<SymbolContext> sym_ctx_with_reasons;
1768
1769 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1770 size_t num_threads = cur_threadlist.GetSize();
1771 for (size_t i = 0; i < num_threads; i++)
1772 {
1773 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1774 if (cur_thread_sp->ThreadStoppedForAReason())
1775 {
1776 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1777 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1778 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1779 }
1780 }
1781
1782 // If no threads stopped for a reason, don't run the stop-hooks.
1783 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1784 if (num_exe_ctx == 0)
1785 return;
1786
Jim Ingham5b52f0c2011-06-02 23:58:261787 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1788 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:591789
1790 bool keep_going = true;
1791 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271792 bool print_hook_header;
1793 bool print_thread_header;
1794
1795 if (num_exe_ctx == 1)
1796 print_thread_header = false;
1797 else
1798 print_thread_header = true;
1799
1800 if (m_stop_hooks.size() == 1)
1801 print_hook_header = false;
1802 else
1803 print_hook_header = true;
1804
Jim Ingham9575d842011-03-11 03:53:591805 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1806 {
1807 // result.Clear();
1808 StopHookSP cur_hook_sp = (*pos).second;
1809 if (!cur_hook_sp->IsActive())
1810 continue;
1811
1812 bool any_thread_matched = false;
1813 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1814 {
1815 if ((cur_hook_sp->GetSpecifier () == NULL
1816 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1817 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Claytonc14ee322011-09-22 04:58:261818 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Ingham9575d842011-03-11 03:53:591819 {
1820 if (!hooks_ran)
1821 {
Jim Ingham9575d842011-03-11 03:53:591822 hooks_ran = true;
1823 }
Jim Ingham381e25b2011-03-22 01:47:271824 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591825 {
Johnny Chenaeab25c2011-10-24 23:01:061826 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1827 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1828 NULL);
1829 if (cmd)
1830 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1831 else
1832 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:591833 any_thread_matched = true;
1834 }
1835
Jim Ingham381e25b2011-03-22 01:47:271836 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:261837 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591838
1839 bool stop_on_continue = true;
1840 bool stop_on_error = true;
1841 bool echo_commands = false;
1842 bool print_results = true;
1843 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:511844 &exc_ctx_with_reasons[i],
1845 stop_on_continue,
1846 stop_on_error,
1847 echo_commands,
1848 print_results,
1849 result);
Jim Ingham9575d842011-03-11 03:53:591850
1851 // If the command started the target going again, we should bag out of
1852 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:511853 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1854 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:591855 {
Greg Clayton81c22f62011-10-19 18:09:391856 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:591857 keep_going = false;
1858 }
1859 }
1860 }
1861 }
Jason Molenda879cf772011-09-23 00:42:551862
Caroline Tice969ed3d12011-05-02 20:41:461863 result.GetImmediateOutputStream()->Flush();
1864 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:591865}
1866
Greg Clayton7b242382011-07-08 00:48:091867bool
1868Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1869{
1870 bool changed = false;
1871 if (module)
1872 {
1873 ObjectFile *object_file = module->GetObjectFile();
1874 if (object_file)
1875 {
1876 SectionList *section_list = object_file->GetSectionList ();
1877 if (section_list)
1878 {
1879 // All sections listed in the dyld image info structure will all
1880 // either be fixed up already, or they will all be off by a single
1881 // slide amount that is determined by finding the first segment
1882 // that is at file offset zero which also has bytes (a file size
1883 // that is greater than zero) in the object file.
1884
1885 // Determine the slide amount (if any)
1886 const size_t num_sections = section_list->GetSize();
1887 size_t sect_idx = 0;
1888 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1889 {
1890 // Iterate through the object file sections to find the
1891 // first section that starts of file offset zero and that
1892 // has bytes in the file...
1893 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1894 if (section)
1895 {
1896 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1897 changed = true;
1898 }
1899 }
1900 }
1901 }
1902 }
1903 return changed;
1904}
1905
1906
Jim Ingham9575d842011-03-11 03:53:591907//--------------------------------------------------------------
1908// class Target::StopHook
1909//--------------------------------------------------------------
1910
1911
1912Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1913 UserID (uid),
1914 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:591915 m_commands (),
1916 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:401917 m_thread_spec_ap(NULL),
1918 m_active (true)
Jim Ingham9575d842011-03-11 03:53:591919{
1920}
1921
1922Target::StopHook::StopHook (const StopHook &rhs) :
1923 UserID (rhs.GetID()),
1924 m_target_sp (rhs.m_target_sp),
1925 m_commands (rhs.m_commands),
1926 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:401927 m_thread_spec_ap (NULL),
1928 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:591929{
1930 if (rhs.m_thread_spec_ap.get() != NULL)
1931 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1932}
1933
1934
1935Target::StopHook::~StopHook ()
1936{
1937}
1938
1939void
1940Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1941{
1942 m_thread_spec_ap.reset (specifier);
1943}
1944
1945
1946void
1947Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1948{
1949 int indent_level = s->GetIndentLevel();
1950
1951 s->SetIndentLevel(indent_level + 2);
1952
Greg Clayton81c22f62011-10-19 18:09:391953 s->Printf ("Hook: %llu\n", GetID());
Jim Ingham9575d842011-03-11 03:53:591954 if (m_active)
1955 s->Indent ("State: enabled\n");
1956 else
1957 s->Indent ("State: disabled\n");
1958
1959 if (m_specifier_sp)
1960 {
1961 s->Indent();
1962 s->PutCString ("Specifier:\n");
1963 s->SetIndentLevel (indent_level + 4);
1964 m_specifier_sp->GetDescription (s, level);
1965 s->SetIndentLevel (indent_level + 2);
1966 }
1967
1968 if (m_thread_spec_ap.get() != NULL)
1969 {
1970 StreamString tmp;
1971 s->Indent("Thread:\n");
1972 m_thread_spec_ap->GetDescription (&tmp, level);
1973 s->SetIndentLevel (indent_level + 4);
1974 s->Indent (tmp.GetData());
1975 s->PutCString ("\n");
1976 s->SetIndentLevel (indent_level + 2);
1977 }
1978
1979 s->Indent ("Commands: \n");
1980 s->SetIndentLevel (indent_level + 4);
1981 uint32_t num_commands = m_commands.GetSize();
1982 for (uint32_t i = 0; i < num_commands; i++)
1983 {
1984 s->Indent(m_commands.GetStringAtIndex(i));
1985 s->PutCString ("\n");
1986 }
1987 s->SetIndentLevel (indent_level);
1988}
1989
1990
Caroline Ticedaccaa92010-09-20 20:44:431991//--------------------------------------------------------------
1992// class Target::SettingsController
1993//--------------------------------------------------------------
1994
1995Target::SettingsController::SettingsController () :
1996 UserSettingsController ("target", Debugger::GetSettingsController()),
1997 m_default_architecture ()
1998{
1999 m_default_settings.reset (new TargetInstanceSettings (*this, false,
2000 InstanceSettings::GetDefaultName().AsCString()));
2001}
2002
2003Target::SettingsController::~SettingsController ()
2004{
2005}
2006
2007lldb::InstanceSettingsSP
2008Target::SettingsController::CreateInstanceSettings (const char *instance_name)
2009{
Greg Claytondbe54502010-11-19 03:46:012010 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
2011 false,
2012 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:432013 lldb::InstanceSettingsSP new_settings_sp (new_settings);
2014 return new_settings_sp;
2015}
2016
Caroline Ticedaccaa92010-09-20 20:44:432017
Greg Clayton1d885962011-11-08 02:43:132018#define TSC_DEFAULT_ARCH "default-arch"
2019#define TSC_EXPR_PREFIX "expr-prefix"
2020#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
2021#define TSC_SKIP_PROLOGUE "skip-prologue"
2022#define TSC_SOURCE_MAP "source-map"
2023#define TSC_MAX_CHILDREN "max-children-count"
2024#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
2025#define TSC_PLATFORM_AVOID "breakpoints-use-platform-avoid-list"
2026#define TSC_RUN_ARGS "run-args"
2027#define TSC_ENV_VARS "env-vars"
2028#define TSC_INHERIT_ENV "inherit-env"
2029#define TSC_STDIN_PATH "input-path"
2030#define TSC_STDOUT_PATH "output-path"
2031#define TSC_STDERR_PATH "error-path"
2032#define TSC_DISABLE_ASLR "disable-aslr"
2033#define TSC_DISABLE_STDIO "disable-stdio"
Greg Claytonbfe5f3b2011-02-18 01:44:252034
2035
2036static const ConstString &
2037GetSettingNameForDefaultArch ()
2038{
2039 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytonbfe5f3b2011-02-18 01:44:252040 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:432041}
2042
Greg Claytonbfe5f3b2011-02-18 01:44:252043static const ConstString &
2044GetSettingNameForExpressionPrefix ()
2045{
2046 static ConstString g_const_string (TSC_EXPR_PREFIX);
2047 return g_const_string;
2048}
2049
2050static const ConstString &
Jim Ingham78a685a2011-04-16 00:01:132051GetSettingNameForPreferDynamicValue ()
2052{
2053 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
2054 return g_const_string;
2055}
2056
Greg Clayton7e14f912011-04-23 02:04:552057static const ConstString &
2058GetSettingNameForSourcePathMap ()
2059{
2060 static ConstString g_const_string (TSC_SOURCE_MAP);
2061 return g_const_string;
2062}
Greg Claytonbfe5f3b2011-02-18 01:44:252063
Greg Clayton385aa282011-04-22 03:55:062064static const ConstString &
2065GetSettingNameForSkipPrologue ()
2066{
2067 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
2068 return g_const_string;
2069}
2070
Enrico Granata22c55d12011-08-12 02:00:062071static const ConstString &
2072GetSettingNameForMaxChildren ()
2073{
2074 static ConstString g_const_string (TSC_MAX_CHILDREN);
2075 return g_const_string;
2076}
Greg Clayton385aa282011-04-22 03:55:062077
Enrico Granata9128ee2f2011-09-06 19:20:512078static const ConstString &
2079GetSettingNameForMaxStringSummaryLength ()
2080{
2081 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2082 return g_const_string;
2083}
Greg Clayton385aa282011-04-22 03:55:062084
Jim Inghamc6674fd2011-10-28 23:14:112085static const ConstString &
2086GetSettingNameForPlatformAvoid ()
2087{
2088 static ConstString g_const_string (TSC_PLATFORM_AVOID);
2089 return g_const_string;
2090}
2091
Greg Clayton1d885962011-11-08 02:43:132092const ConstString &
2093GetSettingNameForRunArgs ()
2094{
2095 static ConstString g_const_string (TSC_RUN_ARGS);
2096 return g_const_string;
2097}
2098
2099const ConstString &
2100GetSettingNameForEnvVars ()
2101{
2102 static ConstString g_const_string (TSC_ENV_VARS);
2103 return g_const_string;
2104}
2105
2106const ConstString &
2107GetSettingNameForInheritHostEnv ()
2108{
2109 static ConstString g_const_string (TSC_INHERIT_ENV);
2110 return g_const_string;
2111}
2112
2113const ConstString &
2114GetSettingNameForInputPath ()
2115{
2116 static ConstString g_const_string (TSC_STDIN_PATH);
2117 return g_const_string;
2118}
2119
2120const ConstString &
2121GetSettingNameForOutputPath ()
2122{
2123 static ConstString g_const_string (TSC_STDOUT_PATH);
2124 return g_const_string;
2125}
2126
2127const ConstString &
2128GetSettingNameForErrorPath ()
2129{
2130 static ConstString g_const_string (TSC_STDERR_PATH);
2131 return g_const_string;
2132}
2133
2134const ConstString &
2135GetSettingNameForDisableASLR ()
2136{
2137 static ConstString g_const_string (TSC_DISABLE_ASLR);
2138 return g_const_string;
2139}
2140
2141const ConstString &
2142GetSettingNameForDisableSTDIO ()
2143{
2144 static ConstString g_const_string (TSC_DISABLE_STDIO);
2145 return g_const_string;
2146}
Jim Inghamc6674fd2011-10-28 23:14:112147
Caroline Ticedaccaa92010-09-20 20:44:432148bool
2149Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2150 const char *index_value,
2151 const char *value,
2152 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:542153 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:432154 Error&err)
2155{
Greg Claytonbfe5f3b2011-02-18 01:44:252156 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:432157 {
Greg Claytoneb0103f2011-04-07 22:46:352158 m_default_architecture.SetTriple (value, NULL);
Greg Clayton64195a22011-02-23 00:35:022159 if (!m_default_architecture.IsValid())
2160 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:432161 }
2162 return true;
2163}
2164
2165
2166bool
2167Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2168 StringList &value,
2169 Error &err)
2170{
Greg Claytonbfe5f3b2011-02-18 01:44:252171 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:432172 {
Greg Clayton307de252010-10-27 02:06:372173 // If the arch is invalid (the default), don't show a string for it
2174 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:022175 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:432176 return true;
2177 }
2178 else
2179 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2180
2181 return false;
2182}
2183
2184//--------------------------------------------------------------
2185// class TargetInstanceSettings
2186//--------------------------------------------------------------
2187
Greg Clayton85851dd2010-12-04 00:10:172188TargetInstanceSettings::TargetInstanceSettings
2189(
2190 UserSettingsController &owner,
2191 bool live_instance,
2192 const char *name
2193) :
Greg Claytonbfe5f3b2011-02-18 01:44:252194 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Clayton7e14f912011-04-23 02:04:552195 m_expr_prefix_file (),
Sean Callanan6d6acc82011-11-16 01:54:572196 m_expr_prefix_contents (),
Jim Ingham2837b762011-05-04 03:43:182197 m_prefer_dynamic_value (2),
Greg Clayton7e14f912011-04-23 02:04:552198 m_skip_prologue (true, true),
Enrico Granata22c55d12011-08-12 02:00:062199 m_source_map (NULL, NULL),
Enrico Granata9128ee2f2011-09-06 19:20:512200 m_max_children_display(256),
Jim Inghamc6674fd2011-10-28 23:14:112201 m_max_strlen_length(1024),
Greg Clayton1d885962011-11-08 02:43:132202 m_breakpoints_use_platform_avoid (true, true),
2203 m_run_args (),
2204 m_env_vars (),
2205 m_input_path (),
2206 m_output_path (),
2207 m_error_path (),
2208 m_disable_aslr (true),
2209 m_disable_stdio (false),
2210 m_inherit_host_env (true),
2211 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:432212{
2213 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2214 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2215 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2216 // This is true for CreateInstanceName() too.
2217
2218 if (GetInstanceName () == InstanceSettings::InvalidName())
2219 {
2220 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2221 m_owner.RegisterInstanceSettings (this);
2222 }
2223
2224 if (live_instance)
2225 {
2226 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2227 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:432228 }
2229}
2230
2231TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton7e14f912011-04-23 02:04:552232 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
2233 m_expr_prefix_file (rhs.m_expr_prefix_file),
Sean Callanan6d6acc82011-11-16 01:54:572234 m_expr_prefix_contents (rhs.m_expr_prefix_contents),
Greg Clayton7e14f912011-04-23 02:04:552235 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2236 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata22c55d12011-08-12 02:00:062237 m_source_map (rhs.m_source_map),
Greg Clayton1d885962011-11-08 02:43:132238 m_max_children_display (rhs.m_max_children_display),
2239 m_max_strlen_length (rhs.m_max_strlen_length),
2240 m_breakpoints_use_platform_avoid (rhs.m_breakpoints_use_platform_avoid),
2241 m_run_args (rhs.m_run_args),
2242 m_env_vars (rhs.m_env_vars),
2243 m_input_path (rhs.m_input_path),
2244 m_output_path (rhs.m_output_path),
2245 m_error_path (rhs.m_error_path),
2246 m_disable_aslr (rhs.m_disable_aslr),
2247 m_disable_stdio (rhs.m_disable_stdio),
2248 m_inherit_host_env (rhs.m_inherit_host_env)
Caroline Ticedaccaa92010-09-20 20:44:432249{
2250 if (m_instance_name != InstanceSettings::GetDefaultName())
2251 {
2252 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2253 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:432254 }
2255}
2256
2257TargetInstanceSettings::~TargetInstanceSettings ()
2258{
2259}
2260
2261TargetInstanceSettings&
2262TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2263{
2264 if (this != &rhs)
2265 {
Greg Clayton1d885962011-11-08 02:43:132266 m_expr_prefix_file = rhs.m_expr_prefix_file;
Sean Callanan6d6acc82011-11-16 01:54:572267 m_expr_prefix_contents = rhs.m_expr_prefix_contents;
Greg Clayton1d885962011-11-08 02:43:132268 m_prefer_dynamic_value = rhs.m_prefer_dynamic_value;
2269 m_skip_prologue = rhs.m_skip_prologue;
2270 m_source_map = rhs.m_source_map;
2271 m_max_children_display = rhs.m_max_children_display;
2272 m_max_strlen_length = rhs.m_max_strlen_length;
2273 m_breakpoints_use_platform_avoid = rhs.m_breakpoints_use_platform_avoid;
2274 m_run_args = rhs.m_run_args;
2275 m_env_vars = rhs.m_env_vars;
2276 m_input_path = rhs.m_input_path;
2277 m_output_path = rhs.m_output_path;
2278 m_error_path = rhs.m_error_path;
2279 m_disable_aslr = rhs.m_disable_aslr;
2280 m_disable_stdio = rhs.m_disable_stdio;
2281 m_inherit_host_env = rhs.m_inherit_host_env;
Caroline Ticedaccaa92010-09-20 20:44:432282 }
2283
2284 return *this;
2285}
2286
Caroline Ticedaccaa92010-09-20 20:44:432287void
2288TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2289 const char *index_value,
2290 const char *value,
2291 const ConstString &instance_name,
2292 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:542293 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:432294 Error &err,
2295 bool pending)
2296{
Greg Claytonbfe5f3b2011-02-18 01:44:252297 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:032298 {
Greg Clayton7e14f912011-04-23 02:04:552299 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2300 if (err.Success())
Sean Callanan322f5292010-10-29 00:29:032301 {
Greg Clayton7e14f912011-04-23 02:04:552302 switch (op)
Sean Callanan322f5292010-10-29 00:29:032303 {
Greg Clayton7e14f912011-04-23 02:04:552304 default:
2305 break;
2306 case eVarSetOperationAssign:
2307 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:032308 {
Greg Clayton4017fa32012-01-06 02:01:062309 m_expr_prefix_contents.clear();
2310
Greg Clayton7e14f912011-04-23 02:04:552311 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2312 {
2313 err.SetErrorToGenericError ();
Greg Clayton86edbf42011-10-26 00:56:272314 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Clayton7e14f912011-04-23 02:04:552315 return;
2316 }
2317
Greg Clayton4017fa32012-01-06 02:01:062318 DataBufferSP file_data_sp (m_expr_prefix_file.GetCurrentValue().ReadFileContents(0, SIZE_MAX, &err));
Sean Callanan6d6acc82011-11-16 01:54:572319
Greg Clayton4017fa32012-01-06 02:01:062320 if (err.Success())
Greg Clayton7e14f912011-04-23 02:04:552321 {
Greg Clayton4017fa32012-01-06 02:01:062322 if (file_data_sp && file_data_sp->GetByteSize() > 0)
2323 {
2324 m_expr_prefix_contents.assign((const char*)file_data_sp->GetBytes(), file_data_sp->GetByteSize());
2325 }
2326 else
2327 {
2328 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
2329 }
Greg Clayton7e14f912011-04-23 02:04:552330 }
Sean Callanan322f5292010-10-29 00:29:032331 }
Greg Clayton7e14f912011-04-23 02:04:552332 break;
2333 case eVarSetOperationClear:
Sean Callanan6d6acc82011-11-16 01:54:572334 m_expr_prefix_contents.clear();
Sean Callanan322f5292010-10-29 00:29:032335 }
Sean Callanan322f5292010-10-29 00:29:032336 }
2337 }
Jim Ingham78a685a2011-04-16 00:01:132338 else if (var_name == GetSettingNameForPreferDynamicValue())
2339 {
Jim Ingham2837b762011-05-04 03:43:182340 int new_value;
2341 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2342 if (err.Success())
2343 m_prefer_dynamic_value = new_value;
Greg Clayton385aa282011-04-22 03:55:062344 }
2345 else if (var_name == GetSettingNameForSkipPrologue())
2346 {
Greg Clayton7e14f912011-04-23 02:04:552347 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2348 }
Enrico Granata22c55d12011-08-12 02:00:062349 else if (var_name == GetSettingNameForMaxChildren())
2350 {
2351 bool ok;
2352 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2353 if (ok)
2354 m_max_children_display = new_value;
2355 }
Enrico Granata9128ee2f2011-09-06 19:20:512356 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2357 {
2358 bool ok;
2359 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2360 if (ok)
2361 m_max_strlen_length = new_value;
2362 }
Greg Clayton7e14f912011-04-23 02:04:552363 else if (var_name == GetSettingNameForSourcePathMap ())
2364 {
2365 switch (op)
2366 {
2367 case eVarSetOperationReplace:
2368 case eVarSetOperationInsertBefore:
2369 case eVarSetOperationInsertAfter:
2370 case eVarSetOperationRemove:
2371 default:
2372 break;
2373 case eVarSetOperationAssign:
2374 m_source_map.Clear(true);
2375 // Fall through to append....
2376 case eVarSetOperationAppend:
2377 {
2378 Args args(value);
2379 const uint32_t argc = args.GetArgumentCount();
2380 if (argc & 1 || argc == 0)
2381 {
2382 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2383 }
2384 else
2385 {
2386 char resolved_new_path[PATH_MAX];
2387 FileSpec file_spec;
2388 const char *old_path;
2389 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2390 {
2391 const char *new_path = args.GetArgumentAtIndex(idx+1);
2392 assert (new_path); // We have an even number of paths, this shouldn't happen!
2393
2394 file_spec.SetFile(new_path, true);
2395 if (file_spec.Exists())
2396 {
2397 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2398 {
2399 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2400 return;
2401 }
2402 }
2403 else
2404 {
2405 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2406 return;
2407 }
2408 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2409 }
2410 }
2411 }
2412 break;
2413
2414 case eVarSetOperationClear:
2415 m_source_map.Clear(true);
2416 break;
2417 }
Jim Ingham78a685a2011-04-16 00:01:132418 }
Jim Inghamc6674fd2011-10-28 23:14:112419 else if (var_name == GetSettingNameForPlatformAvoid ())
2420 {
2421 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_breakpoints_use_platform_avoid);
2422 }
Greg Clayton1d885962011-11-08 02:43:132423 else if (var_name == GetSettingNameForRunArgs())
2424 {
2425 UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err);
2426 }
2427 else if (var_name == GetSettingNameForEnvVars())
2428 {
2429 // This is nice for local debugging, but it is isn't correct for
2430 // remote debugging. We need to stop process.env-vars from being
2431 // populated with the host environment and add this as a launch option
2432 // and get the correct environment from the Target's platform.
2433 // GetHostEnvironmentIfNeeded ();
2434 UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err);
2435 }
2436 else if (var_name == GetSettingNameForInputPath())
2437 {
2438 UserSettingsController::UpdateStringVariable (op, m_input_path, value, err);
2439 }
2440 else if (var_name == GetSettingNameForOutputPath())
2441 {
2442 UserSettingsController::UpdateStringVariable (op, m_output_path, value, err);
2443 }
2444 else if (var_name == GetSettingNameForErrorPath())
2445 {
2446 UserSettingsController::UpdateStringVariable (op, m_error_path, value, err);
2447 }
2448 else if (var_name == GetSettingNameForDisableASLR())
2449 {
2450 UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, true, err);
2451 }
2452 else if (var_name == GetSettingNameForDisableSTDIO ())
2453 {
2454 UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, false, err);
2455 }
Caroline Ticedaccaa92010-09-20 20:44:432456}
2457
2458void
Greg Claytonbfe5f3b2011-02-18 01:44:252459TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:432460{
Sean Callanan322f5292010-10-29 00:29:032461 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2462
2463 if (!new_settings_ptr)
2464 return;
2465
Greg Clayton1d885962011-11-08 02:43:132466 *this = *new_settings_ptr;
Caroline Ticedaccaa92010-09-20 20:44:432467}
2468
Caroline Tice12cecd72010-09-20 21:37:422469bool
Caroline Ticedaccaa92010-09-20 20:44:432470TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2471 const ConstString &var_name,
2472 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:422473 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:432474{
Greg Claytonbfe5f3b2011-02-18 01:44:252475 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:032476 {
Greg Clayton7e14f912011-04-23 02:04:552477 char path[PATH_MAX];
2478 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2479 if (path_len > 0)
2480 value.AppendString (path, path_len);
Sean Callanan322f5292010-10-29 00:29:032481 }
Jim Ingham78a685a2011-04-16 00:01:132482 else if (var_name == GetSettingNameForPreferDynamicValue())
2483 {
Jim Ingham2837b762011-05-04 03:43:182484 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Ingham78a685a2011-04-16 00:01:132485 }
Greg Clayton385aa282011-04-22 03:55:062486 else if (var_name == GetSettingNameForSkipPrologue())
2487 {
2488 if (m_skip_prologue)
2489 value.AppendString ("true");
2490 else
2491 value.AppendString ("false");
2492 }
Greg Clayton7e14f912011-04-23 02:04:552493 else if (var_name == GetSettingNameForSourcePathMap ())
2494 {
Johnny Chen64bab482011-12-12 21:59:282495 if (m_source_map.GetSize())
2496 {
2497 size_t i;
2498 for (i = 0; i < m_source_map.GetSize(); ++i) {
2499 StreamString sstr;
2500 m_source_map.Dump(&sstr, i);
2501 value.AppendString(sstr.GetData());
2502 }
2503 }
Greg Clayton7e14f912011-04-23 02:04:552504 }
Enrico Granata22c55d12011-08-12 02:00:062505 else if (var_name == GetSettingNameForMaxChildren())
2506 {
2507 StreamString count_str;
2508 count_str.Printf ("%d", m_max_children_display);
2509 value.AppendString (count_str.GetData());
2510 }
Enrico Granata9128ee2f2011-09-06 19:20:512511 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2512 {
2513 StreamString count_str;
2514 count_str.Printf ("%d", m_max_strlen_length);
2515 value.AppendString (count_str.GetData());
2516 }
Jim Inghamc6674fd2011-10-28 23:14:112517 else if (var_name == GetSettingNameForPlatformAvoid())
2518 {
2519 if (m_breakpoints_use_platform_avoid)
2520 value.AppendString ("true");
2521 else
2522 value.AppendString ("false");
2523 }
Greg Clayton1d885962011-11-08 02:43:132524 else if (var_name == GetSettingNameForRunArgs())
2525 {
2526 if (m_run_args.GetArgumentCount() > 0)
2527 {
2528 for (int i = 0; i < m_run_args.GetArgumentCount(); ++i)
2529 value.AppendString (m_run_args.GetArgumentAtIndex (i));
2530 }
2531 }
2532 else if (var_name == GetSettingNameForEnvVars())
2533 {
2534 GetHostEnvironmentIfNeeded ();
2535
2536 if (m_env_vars.size() > 0)
2537 {
2538 std::map<std::string, std::string>::iterator pos;
2539 for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos)
2540 {
2541 StreamString value_str;
2542 value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str());
2543 value.AppendString (value_str.GetData());
2544 }
2545 }
2546 }
2547 else if (var_name == GetSettingNameForInputPath())
2548 {
2549 value.AppendString (m_input_path.c_str());
2550 }
2551 else if (var_name == GetSettingNameForOutputPath())
2552 {
2553 value.AppendString (m_output_path.c_str());
2554 }
2555 else if (var_name == GetSettingNameForErrorPath())
2556 {
2557 value.AppendString (m_error_path.c_str());
2558 }
2559 else if (var_name == GetSettingNameForInheritHostEnv())
2560 {
2561 if (m_inherit_host_env)
2562 value.AppendString ("true");
2563 else
2564 value.AppendString ("false");
2565 }
2566 else if (var_name == GetSettingNameForDisableASLR())
2567 {
2568 if (m_disable_aslr)
2569 value.AppendString ("true");
2570 else
2571 value.AppendString ("false");
2572 }
2573 else if (var_name == GetSettingNameForDisableSTDIO())
2574 {
2575 if (m_disable_stdio)
2576 value.AppendString ("true");
2577 else
2578 value.AppendString ("false");
2579 }
Sean Callanan322f5292010-10-29 00:29:032580 else
2581 {
2582 if (err)
2583 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2584 return false;
2585 }
Sean Callanan322f5292010-10-29 00:29:032586 return true;
Caroline Ticedaccaa92010-09-20 20:44:432587}
2588
Greg Clayton1d885962011-11-08 02:43:132589void
2590Target::TargetInstanceSettings::GetHostEnvironmentIfNeeded ()
2591{
2592 if (m_inherit_host_env && !m_got_host_env)
2593 {
2594 m_got_host_env = true;
2595 StringList host_env;
2596 const size_t host_env_count = Host::GetEnvironment (host_env);
2597 for (size_t idx=0; idx<host_env_count; idx++)
2598 {
2599 const char *env_entry = host_env.GetStringAtIndex (idx);
2600 if (env_entry)
2601 {
2602 const char *equal_pos = ::strchr(env_entry, '=');
2603 if (equal_pos)
2604 {
2605 std::string key (env_entry, equal_pos - env_entry);
2606 std::string value (equal_pos + 1);
2607 if (m_env_vars.find (key) == m_env_vars.end())
2608 m_env_vars[key] = value;
2609 }
2610 }
2611 }
2612 }
2613}
2614
2615
2616size_t
2617Target::TargetInstanceSettings::GetEnvironmentAsArgs (Args &env)
2618{
2619 GetHostEnvironmentIfNeeded ();
2620
2621 dictionary::const_iterator pos, end = m_env_vars.end();
2622 for (pos = m_env_vars.begin(); pos != end; ++pos)
2623 {
2624 std::string env_var_equal_value (pos->first);
2625 env_var_equal_value.append(1, '=');
2626 env_var_equal_value.append (pos->second);
2627 env.AppendArgument (env_var_equal_value.c_str());
2628 }
2629 return env.GetArgumentCount();
2630}
2631
2632
Caroline Ticedaccaa92010-09-20 20:44:432633const ConstString
2634TargetInstanceSettings::CreateInstanceName ()
2635{
Caroline Ticedaccaa92010-09-20 20:44:432636 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:102637 static int instance_count = 1;
2638
Caroline Ticedaccaa92010-09-20 20:44:432639 sstr.Printf ("target_%d", instance_count);
2640 ++instance_count;
2641
2642 const ConstString ret_val (sstr.GetData());
2643 return ret_val;
2644}
2645
2646//--------------------------------------------------
2647// Target::SettingsController Variable Tables
2648//--------------------------------------------------
Jim Ingham2837b762011-05-04 03:43:182649OptionEnumValueElement
2650TargetInstanceSettings::g_dynamic_value_types[] =
2651{
Greg Clayton5d2fbfe2011-05-30 00:39:482652{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2653{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2654{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham2837b762011-05-04 03:43:182655{ 0, NULL, NULL }
2656};
Caroline Ticedaccaa92010-09-20 20:44:432657
2658SettingEntry
2659Target::SettingsController::global_settings_table[] =
2660{
Greg Claytonbfe5f3b2011-02-18 01:44:252661 // var-name var-type default enum init'd hidden help-text
2662 // ================= ================== =========== ==== ====== ====== =========================================================================
2663 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2664 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2665};
2666
Caroline Ticedaccaa92010-09-20 20:44:432667SettingEntry
2668Target::SettingsController::instance_settings_table[] =
2669{
Enrico Granata9128ee2f2011-09-06 19:20:512670 // var-name var-type default enum init'd hidden help-text
2671 // ================= ================== =============== ======================= ====== ====== =========================================================================
2672 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2673 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2674 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2675 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2676 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2677 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
Jim Inghamc6674fd2011-10-28 23:14:112678 { TSC_PLATFORM_AVOID , eSetVarTypeBoolean, "true" , NULL, false, false, "Consult the platform module avoid list when setting non-module specific breakpoints." },
Greg Clayton1d885962011-11-08 02:43:132679 { TSC_RUN_ARGS , eSetVarTypeArray , NULL , NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." },
2680 { 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." },
2681 { TSC_INHERIT_ENV , eSetVarTypeBoolean, "true" , NULL, false, false, "Inherit the environment from the process that is running LLDB." },
2682 { TSC_STDIN_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for reading its standard input." },
2683 { TSC_STDOUT_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard output." },
2684 { TSC_STDERR_PATH , eSetVarTypeString , NULL , NULL, false, false, "The file/path to be used by the executable program for writing its standard error." },
2685// { "plugin", eSetVarTypeEnum, NULL, NULL, false, false, "The plugin to be used to run the process." },
2686 { TSC_DISABLE_ASLR , eSetVarTypeBoolean, "true" , NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" },
2687 { TSC_DISABLE_STDIO , eSetVarTypeBoolean, "false" , NULL, false, false, "Disable stdin/stdout for process (e.g. for a GUI application)" },
Enrico Granata9128ee2f2011-09-06 19:20:512688 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:432689};