blob: 1a4a7b04741badc1947d3d3b35c3cd763789bf48 [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"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5920#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2421#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Caroline Tice969ed3d12011-05-02 20:41:4623#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner30fdc8d2010-06-08 16:52:2424#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5925#include "lldb/Core/Timer.h"
26#include "lldb/Core/ValueObject.h"
Greg Claytoneb0103f2011-04-07 22:46:3527#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:2428#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:5929#include "lldb/Interpreter/CommandInterpreter.h"
30#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2431#include "lldb/lldb-private-log.h"
32#include "lldb/Symbol/ObjectFile.h"
33#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5934#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:5935#include "lldb/Target/Thread.h"
36#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:2437
38using namespace lldb;
39using namespace lldb_private;
40
41//----------------------------------------------------------------------
42// Target constructor
43//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:5144Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
45 Broadcaster ("lldb.target"),
46 ExecutionContextScope (),
Greg Claytondbe54502010-11-19 03:46:0147 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2948 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:5149 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:2350 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:5151 m_arch (target_arch),
52 m_images (),
Greg Claytonf5e56de2010-09-14 23:36:4053 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2454 m_breakpoint_list (false),
55 m_internal_breakpoint_list (true),
Johnny Chen1d6bad02011-09-06 20:05:2556 m_watchpoint_location_list (),
Greg Clayton32e0a752011-03-30 18:16:5157 m_process_sp (),
58 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:2459 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5960 m_scratch_ast_context_ap (NULL),
Jim Ingham9575d842011-03-11 03:53:5961 m_persistent_variables (),
Jim Inghame37d6052011-09-13 00:29:5662 m_source_manager(*this),
Greg Clayton32e0a752011-03-30 18:16:5163 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:1464 m_stop_hook_next_id (0),
65 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:2466{
Greg Claytoncfd1ace2010-10-31 03:01:0667 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
68 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
69 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
70
Greg Clayton2d4edfb2010-11-06 01:53:3071 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2472 if (log)
73 log->Printf ("%p Target::Target()", this);
74}
75
76//----------------------------------------------------------------------
77// Destructor
78//----------------------------------------------------------------------
79Target::~Target()
80{
Greg Clayton2d4edfb2010-11-06 01:53:3081 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2482 if (log)
83 log->Printf ("%p Target::~Target()", this);
84 DeleteCurrentProcess ();
85}
86
87void
Caroline Ticeceb6b132010-10-26 03:11:1388Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2489{
Greg Clayton89411422010-10-08 00:21:0590// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1391 if (description_level != lldb::eDescriptionLevelBrief)
92 {
93 s->Indent();
94 s->PutCString("Target\n");
95 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:3596 m_images.Dump(s);
97 m_breakpoint_list.Dump(s);
98 m_internal_breakpoint_list.Dump(s);
99 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13100 }
101 else
102 {
Greg Claytonaa149cb2011-08-11 02:48:45103 Module *exe_module = GetExecutableModulePointer();
104 if (exe_module)
105 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28106 else
107 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13108 }
Chris Lattner30fdc8d2010-06-08 16:52:24109}
110
111void
112Target::DeleteCurrentProcess ()
113{
114 if (m_process_sp.get())
115 {
Greg Clayton17f69202010-09-14 23:52:43116 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24117 if (m_process_sp->IsAlive())
118 m_process_sp->Destroy();
Jim Inghamd0a3e12b2011-02-16 17:54:55119
120 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24121
122 // Do any cleanup of the target we need to do between process instances.
123 // NB It is better to do this before destroying the process in case the
124 // clean up needs some help from the process.
125 m_breakpoint_list.ClearAllBreakpointSites();
126 m_internal_breakpoint_list.ClearAllBreakpointSites();
127 m_process_sp.reset();
128 }
129}
130
131const lldb::ProcessSP &
132Target::CreateProcess (Listener &listener, const char *plugin_name)
133{
134 DeleteCurrentProcess ();
135 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
136 return m_process_sp;
137}
138
139const lldb::ProcessSP &
140Target::GetProcessSP () const
141{
142 return m_process_sp;
143}
144
145lldb::TargetSP
146Target::GetSP()
147{
Greg Clayton66111032010-06-23 01:19:29148 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24149}
150
Greg Clayton3418c852011-08-10 02:10:13151void
152Target::Destroy()
153{
154 Mutex::Locker locker (m_mutex);
155 DeleteCurrentProcess ();
156 m_platform_sp.reset();
157 m_arch.Clear();
158 m_images.Clear();
159 m_section_load_list.Clear();
160 const bool notify = false;
161 m_breakpoint_list.RemoveAll(notify);
162 m_internal_breakpoint_list.RemoveAll(notify);
163 m_last_created_breakpoint.reset();
164 m_search_filter_sp.reset();
165 m_image_search_paths.Clear(notify);
166 m_scratch_ast_context_ap.reset();
167 m_persistent_variables.Clear();
168 m_stop_hooks.clear();
169 m_stop_hook_next_id = 0;
170 m_suppress_stop_hooks = false;
171}
172
173
Chris Lattner30fdc8d2010-06-08 16:52:24174BreakpointList &
175Target::GetBreakpointList(bool internal)
176{
177 if (internal)
178 return m_internal_breakpoint_list;
179 else
180 return m_breakpoint_list;
181}
182
183const BreakpointList &
184Target::GetBreakpointList(bool internal) const
185{
186 if (internal)
187 return m_internal_breakpoint_list;
188 else
189 return m_breakpoint_list;
190}
191
192BreakpointSP
193Target::GetBreakpointByID (break_id_t break_id)
194{
195 BreakpointSP bp_sp;
196
197 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
198 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
199 else
200 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
201
202 return bp_sp;
203}
204
205BreakpointSP
206Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
207{
208 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
209 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
210 return CreateBreakpoint (filter_sp, resolver_sp, internal);
211}
212
213
214BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41215Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24216{
Chris Lattner30fdc8d2010-06-08 16:52:24217 Address so_addr;
218 // Attempt to resolve our load address if possible, though it is ok if
219 // it doesn't resolve to section/offset.
220
Greg Clayton1b72fcb2010-08-24 00:45:41221 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40222 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41223 if (!so_addr.IsValid())
224 {
225 // The address didn't resolve, so just set this as an absolute address
226 so_addr.SetOffset (addr);
227 }
228 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24229 return bp_sp;
230}
231
232BreakpointSP
233Target::CreateBreakpoint (Address &addr, bool internal)
234{
235 TargetSP target_sp = this->GetSP();
236 SearchFilterSP filter_sp(new SearchFilter (target_sp));
237 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
238 return CreateBreakpoint (filter_sp, resolver_sp, internal);
239}
240
241BreakpointSP
Greg Claytond16e1e52011-07-12 17:06:17242Target::CreateBreakpoint (const FileSpec *containingModule,
243 const char *func_name,
244 uint32_t func_name_type_mask,
245 bool internal,
246 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24247{
Greg Clayton0c5cd902010-06-28 21:30:43248 BreakpointSP bp_sp;
249 if (func_name)
250 {
251 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Claytond16e1e52011-07-12 17:06:17252
253 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
254 func_name,
255 func_name_type_mask,
256 Breakpoint::Exact,
257 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton0c5cd902010-06-28 21:30:43258 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
259 }
260 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24261}
262
263
264SearchFilterSP
265Target::GetSearchFilterForModule (const FileSpec *containingModule)
266{
267 SearchFilterSP filter_sp;
268 lldb::TargetSP target_sp = this->GetSP();
269 if (containingModule != NULL)
270 {
271 // TODO: We should look into sharing module based search filters
272 // across many breakpoints like we do for the simple target based one
273 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
274 }
275 else
276 {
277 if (m_search_filter_sp.get() == NULL)
278 m_search_filter_sp.reset (new SearchFilter (target_sp));
279 filter_sp = m_search_filter_sp;
280 }
281 return filter_sp;
282}
283
284BreakpointSP
Greg Claytond16e1e52011-07-12 17:06:17285Target::CreateBreakpoint (const FileSpec *containingModule,
286 RegularExpression &func_regex,
287 bool internal,
288 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24289{
290 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Claytond16e1e52011-07-12 17:06:17291 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
292 func_regex,
293 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24294
295 return CreateBreakpoint (filter_sp, resolver_sp, internal);
296}
297
298BreakpointSP
299Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
300{
301 BreakpointSP bp_sp;
302 if (filter_sp && resolver_sp)
303 {
304 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
305 resolver_sp->SetBreakpoint (bp_sp.get());
306
307 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17308 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24309 else
Greg Clayton9fed0d82010-07-23 23:33:17310 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24311
Greg Clayton2d4edfb2010-11-06 01:53:30312 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24313 if (log)
314 {
315 StreamString s;
316 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
317 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
318 }
319
Chris Lattner30fdc8d2010-06-08 16:52:24320 bp_sp->ResolveBreakpoint();
321 }
Jim Ingham36f3b362010-10-14 23:45:03322
323 if (!internal && bp_sp)
324 {
325 m_last_created_breakpoint = bp_sp;
326 }
327
Chris Lattner30fdc8d2010-06-08 16:52:24328 return bp_sp;
329}
330
Johnny Chen887062a2011-09-12 23:38:44331// See also WatchpointLocation::SetWatchpointType() and OptionGroupWatchpoint::WatchType.
332WatchpointLocationSP
333Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
334{
335 WatchpointLocationSP wp_loc_sp;
Johnny Chen2fd89a02011-09-13 18:30:59336 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
337 if (!process_is_valid)
338 return wp_loc_sp;
339 if (addr == LLDB_INVALID_ADDRESS)
340 return wp_loc_sp;
341 if (size == 0)
Johnny Chen887062a2011-09-12 23:38:44342 return wp_loc_sp;
Johnny Chen7313a642011-09-13 01:15:36343
Johnny Chen3c532582011-09-13 23:29:31344 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
345 if (matched_sp)
346 {
347 size_t old_size = wp_loc_sp->GetByteSize();
348 uint32_t old_type =
349 (wp_loc_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
350 (wp_loc_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
351 // Return an empty watchpoint location if the same one exists already.
352 if (size == old_size && type == old_type)
353 return wp_loc_sp;
354
355 // Nil the matched watchpoint location; we will be creating a new one.
356 m_process_sp->DisableWatchpoint(matched_sp.get());
357 m_watchpoint_location_list.Remove(matched_sp->GetID());
358 }
359
360 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
361 new_loc->SetWatchpointType(type);
362 wp_loc_sp.reset(new_loc);
363 m_watchpoint_location_list.Add(wp_loc_sp);
364 m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen7313a642011-09-13 01:15:36365 return wp_loc_sp;
Johnny Chen887062a2011-09-12 23:38:44366}
367
Chris Lattner30fdc8d2010-06-08 16:52:24368void
369Target::RemoveAllBreakpoints (bool internal_also)
370{
Greg Clayton2d4edfb2010-11-06 01:53:30371 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24372 if (log)
373 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
374
Greg Clayton9fed0d82010-07-23 23:33:17375 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24376 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17377 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03378
379 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24380}
381
382void
383Target::DisableAllBreakpoints (bool internal_also)
384{
Greg Clayton2d4edfb2010-11-06 01:53:30385 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24386 if (log)
387 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
388
389 m_breakpoint_list.SetEnabledAll (false);
390 if (internal_also)
391 m_internal_breakpoint_list.SetEnabledAll (false);
392}
393
394void
395Target::EnableAllBreakpoints (bool internal_also)
396{
Greg Clayton2d4edfb2010-11-06 01:53:30397 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24398 if (log)
399 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
400
401 m_breakpoint_list.SetEnabledAll (true);
402 if (internal_also)
403 m_internal_breakpoint_list.SetEnabledAll (true);
404}
405
406bool
407Target::RemoveBreakpointByID (break_id_t break_id)
408{
Greg Clayton2d4edfb2010-11-06 01:53:30409 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24410 if (log)
411 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
412
413 if (DisableBreakpointByID (break_id))
414 {
415 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17416 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24417 else
Jim Ingham36f3b362010-10-14 23:45:03418 {
Greg Claytonaa1c5872011-01-24 23:35:47419 if (m_last_created_breakpoint)
420 {
421 if (m_last_created_breakpoint->GetID() == break_id)
422 m_last_created_breakpoint.reset();
423 }
Greg Clayton9fed0d82010-07-23 23:33:17424 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03425 }
Chris Lattner30fdc8d2010-06-08 16:52:24426 return true;
427 }
428 return false;
429}
430
431bool
432Target::DisableBreakpointByID (break_id_t break_id)
433{
Greg Clayton2d4edfb2010-11-06 01:53:30434 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24435 if (log)
436 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
437
438 BreakpointSP bp_sp;
439
440 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
441 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
442 else
443 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
444 if (bp_sp)
445 {
446 bp_sp->SetEnabled (false);
447 return true;
448 }
449 return false;
450}
451
452bool
453Target::EnableBreakpointByID (break_id_t break_id)
454{
Greg Clayton2d4edfb2010-11-06 01:53:30455 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24456 if (log)
457 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
458 __FUNCTION__,
459 break_id,
460 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
461
462 BreakpointSP bp_sp;
463
464 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
465 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
466 else
467 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
468
469 if (bp_sp)
470 {
471 bp_sp->SetEnabled (true);
472 return true;
473 }
474 return false;
475}
476
477ModuleSP
478Target::GetExecutableModule ()
479{
Greg Claytonaa149cb2011-08-11 02:48:45480 return m_images.GetModuleAtIndex(0);
481}
482
483Module*
484Target::GetExecutableModulePointer ()
485{
486 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24487}
488
489void
490Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
491{
492 m_images.Clear();
493 m_scratch_ast_context_ap.reset();
494
495 if (executable_sp.get())
496 {
497 Timer scoped_timer (__PRETTY_FUNCTION__,
498 "Target::SetExecutableModule (executable = '%s/%s')",
499 executable_sp->GetFileSpec().GetDirectory().AsCString(),
500 executable_sp->GetFileSpec().GetFilename().AsCString());
501
502 m_images.Append(executable_sp); // The first image is our exectuable file
503
Jim Ingham5aee1622010-08-09 23:31:02504 // 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:51505 if (!m_arch.IsValid())
506 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02507
Chris Lattner30fdc8d2010-06-08 16:52:24508 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15509 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamb7f6b2f2011-09-08 22:13:49510 // Let's find the file & line for main and set the default source file from there.
511 if (!m_source_manager.DefaultFileAndLineSet())
512 {
513 SymbolContextList sc_list;
514 uint32_t num_matches;
515 ConstString main_name("main");
516 bool symbols_okay = false; // Force it to be a debug symbol.
517 bool append = false;
518 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
519 for (uint32_t idx = 0; idx < num_matches; idx++)
520 {
521 SymbolContext sc;
522 sc_list.GetContextAtIndex(idx, sc);
523 if (sc.line_entry.file)
524 {
525 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
526 break;
527 }
528 }
529 }
Chris Lattner30fdc8d2010-06-08 16:52:24530
531 if (executable_objfile)
532 {
533 executable_objfile->GetDependentModules(dependent_files);
534 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
535 {
Greg Claytonded470d2011-03-19 01:12:21536 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
537 FileSpec platform_dependent_file_spec;
538 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55539 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21540 else
541 platform_dependent_file_spec = dependent_file_spec;
542
543 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton32e0a752011-03-30 18:16:51544 m_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24545 if (image_module_sp.get())
546 {
Chris Lattner30fdc8d2010-06-08 16:52:24547 ObjectFile *objfile = image_module_sp->GetObjectFile();
548 if (objfile)
549 objfile->GetDependentModules(dependent_files);
550 }
551 }
552 }
553
Chris Lattner30fdc8d2010-06-08 16:52:24554 }
Caroline Tice1559a462010-09-27 00:30:10555
556 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24557}
558
559
Jim Ingham5aee1622010-08-09 23:31:02560bool
561Target::SetArchitecture (const ArchSpec &arch_spec)
562{
Greg Clayton32e0a752011-03-30 18:16:51563 if (m_arch == arch_spec)
Jim Ingham5aee1622010-08-09 23:31:02564 {
565 // If we're setting the architecture to our current architecture, we
566 // don't need to do anything.
567 return true;
568 }
Greg Clayton32e0a752011-03-30 18:16:51569 else if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02570 {
571 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton32e0a752011-03-30 18:16:51572 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02573 return true;
574 }
575 else
576 {
577 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51578 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02579 ModuleSP executable_sp = GetExecutableModule ();
580 m_images.Clear();
581 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02582 // Need to do something about unsetting breakpoints.
583
584 if (executable_sp)
585 {
586 FileSpec exec_file_spec = executable_sp->GetFileSpec();
587 Error error = ModuleList::GetSharedModule(exec_file_spec,
588 arch_spec,
589 NULL,
590 NULL,
591 0,
592 executable_sp,
593 NULL,
594 NULL);
595
596 if (!error.Fail() && executable_sp)
597 {
598 SetExecutableModule (executable_sp, true);
599 return true;
600 }
601 else
602 {
603 return false;
604 }
605 }
606 else
607 {
608 return false;
609 }
610 }
611}
Chris Lattner30fdc8d2010-06-08 16:52:24612
Chris Lattner30fdc8d2010-06-08 16:52:24613void
614Target::ModuleAdded (ModuleSP &module_sp)
615{
616 // A module is being added to this target for the first time
617 ModuleList module_list;
618 module_list.Append(module_sp);
619 ModulesDidLoad (module_list);
620}
621
622void
623Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
624{
Jim Inghame716ae02011-08-03 01:00:06625 // A module is replacing an already added module
Chris Lattner30fdc8d2010-06-08 16:52:24626 ModuleList module_list;
627 module_list.Append (old_module_sp);
628 ModulesDidUnload (module_list);
629 module_list.Clear ();
630 module_list.Append (new_module_sp);
631 ModulesDidLoad (module_list);
632}
633
634void
635Target::ModulesDidLoad (ModuleList &module_list)
636{
637 m_breakpoint_list.UpdateBreakpoints (module_list, true);
638 // TODO: make event data that packages up the module_list
639 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
640}
641
642void
643Target::ModulesDidUnload (ModuleList &module_list)
644{
645 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26646
647 // Remove the images from the target image list
648 m_images.Remove(module_list);
649
Chris Lattner30fdc8d2010-06-08 16:52:24650 // TODO: make event data that packages up the module_list
651 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
652}
653
654size_t
Greg Claytondb598232011-01-07 01:57:07655Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
656{
657 const Section *section = addr.GetSection();
658 if (section && section->GetModule())
659 {
660 ObjectFile *objfile = section->GetModule()->GetObjectFile();
661 if (objfile)
662 {
663 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
664 addr.GetOffset(),
665 dst,
666 dst_len);
667 if (bytes_read > 0)
668 return bytes_read;
669 else
670 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
671 }
672 else
673 {
674 error.SetErrorString("address isn't from a object file");
675 }
676 }
677 else
678 {
679 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
680 }
681 return 0;
682}
683
684size_t
Enrico Granata9128ee2f2011-09-06 19:20:51685Target::ReadMemory (const Address& addr,
686 bool prefer_file_cache,
687 void *dst,
688 size_t dst_len,
689 Error &error,
690 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24691{
Chris Lattner30fdc8d2010-06-08 16:52:24692 error.Clear();
Greg Claytondb598232011-01-07 01:57:07693
Enrico Granata9128ee2f2011-09-06 19:20:51694 // if we end up reading this from process memory, we will fill this
695 // with the actual load address
696 if (load_addr_ptr)
697 *load_addr_ptr = LLDB_INVALID_ADDRESS;
698
Greg Claytondda4f7b2010-06-30 23:03:03699 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
700
Greg Claytondb598232011-01-07 01:57:07701 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02702
703 addr_t load_addr = LLDB_INVALID_ADDRESS;
704 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58705 Address resolved_addr;
706 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03707 {
Greg Claytond16e1e52011-07-12 17:06:17708 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02709 {
Greg Claytond16e1e52011-07-12 17:06:17710 // No sections are loaded, so we must assume we are not running
711 // yet and anything we are given is a file address.
712 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
713 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02714 }
Greg Claytondda4f7b2010-06-30 23:03:03715 else
Greg Claytonc749eb82011-07-11 05:12:02716 {
Greg Claytond16e1e52011-07-12 17:06:17717 // We have at least one section loaded. This can be becuase
718 // we have manually loaded some sections with "target modules load ..."
719 // or because we have have a live process that has sections loaded
720 // through the dynamic loader
721 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
722 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02723 }
Greg Claytondda4f7b2010-06-30 23:03:03724 }
Greg Clayton357132e2011-03-26 19:14:58725 if (!resolved_addr.IsValid())
726 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03727
Greg Claytonc749eb82011-07-11 05:12:02728
Greg Claytondb598232011-01-07 01:57:07729 if (prefer_file_cache)
730 {
731 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
732 if (bytes_read > 0)
733 return bytes_read;
734 }
Greg Claytondda4f7b2010-06-30 23:03:03735
736 if (process_is_valid)
737 {
Greg Claytonc749eb82011-07-11 05:12:02738 if (load_addr == LLDB_INVALID_ADDRESS)
739 load_addr = resolved_addr.GetLoadAddress (this);
740
Greg Claytondda4f7b2010-06-30 23:03:03741 if (load_addr == LLDB_INVALID_ADDRESS)
742 {
743 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
744 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
745 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
746 resolved_addr.GetFileAddress());
747 else
748 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
749 }
750 else
751 {
Greg Claytondb598232011-01-07 01:57:07752 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24753 if (bytes_read != dst_len)
754 {
755 if (error.Success())
756 {
757 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03758 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24759 else
Greg Claytondda4f7b2010-06-30 23:03:03760 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24761 }
762 }
Greg Claytondda4f7b2010-06-30 23:03:03763 if (bytes_read)
Enrico Granata9128ee2f2011-09-06 19:20:51764 {
765 if (load_addr_ptr)
766 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03767 return bytes_read;
Enrico Granata9128ee2f2011-09-06 19:20:51768 }
Greg Claytondda4f7b2010-06-30 23:03:03769 // If the address is not section offset we have an address that
770 // doesn't resolve to any address in any currently loaded shared
771 // libaries and we failed to read memory so there isn't anything
772 // more we can do. If it is section offset, we might be able to
773 // read cached memory from the object file.
774 if (!resolved_addr.IsSectionOffset())
775 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24776 }
Chris Lattner30fdc8d2010-06-08 16:52:24777 }
Greg Claytondda4f7b2010-06-30 23:03:03778
Greg Claytonc749eb82011-07-11 05:12:02779 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03780 {
Greg Claytondb598232011-01-07 01:57:07781 // If we didn't already try and read from the object file cache, then
782 // try it after failing to read from the process.
783 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03784 }
785 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24786}
787
Greg Claytond16e1e52011-07-12 17:06:17788size_t
789Target::ReadScalarIntegerFromMemory (const Address& addr,
790 bool prefer_file_cache,
791 uint32_t byte_size,
792 bool is_signed,
793 Scalar &scalar,
794 Error &error)
795{
796 uint64_t uval;
797
798 if (byte_size <= sizeof(uval))
799 {
800 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
801 if (bytes_read == byte_size)
802 {
803 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
804 uint32_t offset = 0;
805 if (byte_size <= 4)
806 scalar = data.GetMaxU32 (&offset, byte_size);
807 else
808 scalar = data.GetMaxU64 (&offset, byte_size);
809
810 if (is_signed)
811 scalar.SignExtend(byte_size * 8);
812 return bytes_read;
813 }
814 }
815 else
816 {
817 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
818 }
819 return 0;
820}
821
822uint64_t
823Target::ReadUnsignedIntegerFromMemory (const Address& addr,
824 bool prefer_file_cache,
825 size_t integer_byte_size,
826 uint64_t fail_value,
827 Error &error)
828{
829 Scalar scalar;
830 if (ReadScalarIntegerFromMemory (addr,
831 prefer_file_cache,
832 integer_byte_size,
833 false,
834 scalar,
835 error))
836 return scalar.ULongLong(fail_value);
837 return fail_value;
838}
839
840bool
841Target::ReadPointerFromMemory (const Address& addr,
842 bool prefer_file_cache,
843 Error &error,
844 Address &pointer_addr)
845{
846 Scalar scalar;
847 if (ReadScalarIntegerFromMemory (addr,
848 prefer_file_cache,
849 m_arch.GetAddressByteSize(),
850 false,
851 scalar,
852 error))
853 {
854 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
855 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
856 {
857 if (m_section_load_list.IsEmpty())
858 {
859 // No sections are loaded, so we must assume we are not running
860 // yet and anything we are given is a file address.
861 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
862 }
863 else
864 {
865 // We have at least one section loaded. This can be becuase
866 // we have manually loaded some sections with "target modules load ..."
867 // or because we have have a live process that has sections loaded
868 // through the dynamic loader
869 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
870 }
871 // We weren't able to resolve the pointer value, so just return
872 // an address with no section
873 if (!pointer_addr.IsValid())
874 pointer_addr.SetOffset (pointer_vm_addr);
875 return true;
876
877 }
878 }
879 return false;
880}
Chris Lattner30fdc8d2010-06-08 16:52:24881
882ModuleSP
883Target::GetSharedModule
884(
885 const FileSpec& file_spec,
886 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:10887 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:24888 const ConstString *object_name,
889 off_t object_offset,
890 Error *error_ptr
891)
892{
893 // Don't pass in the UUID so we can tell if we have a stale value in our list
894 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
895 bool did_create_module = false;
896 ModuleSP module_sp;
897
Chris Lattner30fdc8d2010-06-08 16:52:24898 Error error;
899
Greg Clayton32e0a752011-03-30 18:16:51900 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner30fdc8d2010-06-08 16:52:24901 if (m_image_search_paths.GetSize())
902 {
903 FileSpec transformed_spec;
904 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
905 {
906 transformed_spec.GetFilename() = file_spec.GetFilename();
907 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
908 }
909 }
910
Greg Clayton32e0a752011-03-30 18:16:51911 // The platform is responsible for finding and caching an appropriate
912 // module in the shared module cache.
913 if (m_platform_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24914 {
Greg Clayton32e0a752011-03-30 18:16:51915 FileSpec platform_file_spec;
916 error = m_platform_sp->GetSharedModule (file_spec,
917 arch,
918 uuid_ptr,
919 object_name,
920 object_offset,
921 module_sp,
922 &old_module_sp,
923 &did_create_module);
924 }
925 else
926 {
927 error.SetErrorString("no platform is currently set");
Chris Lattner30fdc8d2010-06-08 16:52:24928 }
929
Greg Clayton32e0a752011-03-30 18:16:51930 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner30fdc8d2010-06-08 16:52:24931 if (module_sp)
932 {
933 m_images.Append (module_sp);
934 if (did_create_module)
935 {
936 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
937 ModuleUpdated(old_module_sp, module_sp);
938 else
939 ModuleAdded(module_sp);
940 }
941 }
942 if (error_ptr)
943 *error_ptr = error;
944 return module_sp;
945}
946
947
948Target *
949Target::CalculateTarget ()
950{
951 return this;
952}
953
954Process *
955Target::CalculateProcess ()
956{
957 return NULL;
958}
959
960Thread *
961Target::CalculateThread ()
962{
963 return NULL;
964}
965
966StackFrame *
967Target::CalculateStackFrame ()
968{
969 return NULL;
970}
971
972void
Greg Clayton0603aa92010-10-04 01:05:56973Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24974{
975 exe_ctx.target = this;
976 exe_ctx.process = NULL; // Do NOT fill in process...
977 exe_ctx.thread = NULL;
978 exe_ctx.frame = NULL;
979}
980
981PathMappingList &
982Target::GetImageSearchPathList ()
983{
984 return m_image_search_paths;
985}
986
987void
988Target::ImageSearchPathsChanged
989(
990 const PathMappingList &path_list,
991 void *baton
992)
993{
994 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45995 ModuleSP exe_module_sp (target->GetExecutableModule());
996 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24997 {
Greg Claytonaa149cb2011-08-11 02:48:45998 target->m_images.Clear();
999 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:241000 }
1001}
1002
1003ClangASTContext *
1004Target::GetScratchClangASTContext()
1005{
Greg Clayton73da2442011-08-03 01:23:551006 // Now see if we know the target triple, and if so, create our scratch AST context:
1007 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1008 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:241009 return m_scratch_ast_context_ap.get();
1010}
Caroline Ticedaccaa92010-09-20 20:44:431011
Greg Clayton99d0faf2010-11-18 23:32:351012void
Caroline Tice20bd37f2011-03-10 22:14:101013Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:431014{
Greg Clayton99d0faf2010-11-18 23:32:351015 UserSettingsControllerSP &usc = GetSettingsController();
1016 usc.reset (new SettingsController);
1017 UserSettingsController::InitializeSettingsController (usc,
1018 SettingsController::global_settings_table,
1019 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:101020
1021 // Now call SettingsInitialize() on each 'child' setting of Target
1022 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:351023}
Caroline Ticedaccaa92010-09-20 20:44:431024
Greg Clayton99d0faf2010-11-18 23:32:351025void
Caroline Tice20bd37f2011-03-10 22:14:101026Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:351027{
Caroline Tice20bd37f2011-03-10 22:14:101028
1029 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1030
1031 Process::SettingsTerminate ();
1032
1033 // Now terminate Target Settings.
1034
Greg Clayton99d0faf2010-11-18 23:32:351035 UserSettingsControllerSP &usc = GetSettingsController();
1036 UserSettingsController::FinalizeSettingsController (usc);
1037 usc.reset();
1038}
Caroline Ticedaccaa92010-09-20 20:44:431039
Greg Clayton99d0faf2010-11-18 23:32:351040UserSettingsControllerSP &
1041Target::GetSettingsController ()
1042{
1043 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:431044 return g_settings_controller;
1045}
1046
1047ArchSpec
1048Target::GetDefaultArchitecture ()
1049{
Greg Clayton64195a22011-02-23 00:35:021050 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1051
1052 if (settings_controller_sp)
1053 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1054 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:431055}
1056
1057void
Greg Clayton64195a22011-02-23 00:35:021058Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:431059{
Greg Clayton64195a22011-02-23 00:35:021060 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1061
1062 if (settings_controller_sp)
1063 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:431064}
1065
Greg Clayton0603aa92010-10-04 01:05:561066Target *
1067Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1068{
1069 // The target can either exist in the "process" of ExecutionContext, or in
1070 // the "target_sp" member of SymbolContext. This accessor helper function
1071 // will get the target from one of these locations.
1072
1073 Target *target = NULL;
1074 if (sc_ptr != NULL)
1075 target = sc_ptr->target_sp.get();
1076 if (target == NULL)
1077 {
1078 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
1079 target = &exe_ctx_ptr->process->GetTarget();
1080 }
1081 return target;
1082}
1083
1084
Caroline Tice1559a462010-09-27 00:30:101085void
1086Target::UpdateInstanceName ()
1087{
1088 StreamString sstr;
1089
Greg Claytonaa149cb2011-08-11 02:48:451090 Module *exe_module = GetExecutableModulePointer();
1091 if (exe_module)
Caroline Tice1559a462010-09-27 00:30:101092 {
Greg Clayton307de252010-10-27 02:06:371093 sstr.Printf ("%s_%s",
Greg Claytonaa149cb2011-08-11 02:48:451094 exe_module->GetFileSpec().GetFilename().AsCString(),
1095 exe_module->GetArchitecture().GetArchitectureName());
1096 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:101097 }
1098}
1099
Sean Callanan322f5292010-10-29 00:29:031100const char *
1101Target::GetExpressionPrefixContentsAsCString ()
1102{
Greg Clayton7e14f912011-04-23 02:04:551103 if (m_expr_prefix_contents_sp)
1104 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1105 return NULL;
Sean Callanan322f5292010-10-29 00:29:031106}
1107
Greg Clayton8b2fe6d2010-12-14 02:59:591108ExecutionResults
1109Target::EvaluateExpression
1110(
1111 const char *expr_cstr,
1112 StackFrame *frame,
1113 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:351114 bool keep_in_memory,
Jim Ingham2837b762011-05-04 03:43:181115 lldb::DynamicValueType use_dynamic,
Greg Clayton8b2fe6d2010-12-14 02:59:591116 lldb::ValueObjectSP &result_valobj_sp
1117)
1118{
1119 ExecutionResults execution_results = eExecutionSetupError;
1120
1121 result_valobj_sp.reset();
Jim Ingham6026ca32011-05-12 02:06:141122
1123 // We shouldn't run stop hooks in expressions.
1124 // Be sure to reset this if you return anywhere within this function.
1125 bool old_suppress_value = m_suppress_stop_hooks;
1126 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:591127
1128 ExecutionContext exe_ctx;
1129 if (frame)
1130 {
1131 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:081132 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:181133 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granata27b625e2011-08-09 01:04:561134 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1135 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham2837b762011-05-04 03:43:181136 lldb::VariableSP var_sp;
1137 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1138 use_dynamic,
1139 expr_path_options,
1140 var_sp,
1141 error);
Greg Clayton8b2fe6d2010-12-14 02:59:591142 }
1143 else if (m_process_sp)
1144 {
1145 m_process_sp->CalculateExecutionContext(exe_ctx);
1146 }
1147 else
1148 {
1149 CalculateExecutionContext(exe_ctx);
1150 }
1151
1152 if (result_valobj_sp)
1153 {
1154 execution_results = eExecutionCompleted;
1155 // We got a result from the frame variable expression path above...
1156 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1157
1158 lldb::ValueObjectSP const_valobj_sp;
1159
1160 // Check in case our value is already a constant value
1161 if (result_valobj_sp->GetIsConstant())
1162 {
1163 const_valobj_sp = result_valobj_sp;
1164 const_valobj_sp->SetName (persistent_variable_name);
1165 }
1166 else
Jim Ingham78a685a2011-04-16 00:01:131167 {
Jim Ingham2837b762011-05-04 03:43:181168 if (use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:131169 {
Jim Ingham2837b762011-05-04 03:43:181170 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:131171 if (dynamic_sp)
1172 result_valobj_sp = dynamic_sp;
1173 }
1174
Jim Ingham6035b672011-03-31 00:19:251175 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:131176 }
Greg Clayton8b2fe6d2010-12-14 02:59:591177
Sean Callanan92adcac2011-01-13 08:53:351178 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1179
Greg Clayton8b2fe6d2010-12-14 02:59:591180 result_valobj_sp = const_valobj_sp;
1181
Sean Callanan92adcac2011-01-13 08:53:351182 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1183 assert (clang_expr_variable_sp.get());
1184
1185 // Set flags and live data as appropriate
1186
1187 const Value &result_value = live_valobj_sp->GetValue();
1188
1189 switch (result_value.GetValueType())
1190 {
1191 case Value::eValueTypeHostAddress:
1192 case Value::eValueTypeFileAddress:
1193 // we don't do anything with these for now
1194 break;
1195 case Value::eValueTypeScalar:
1196 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1197 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1198 break;
1199 case Value::eValueTypeLoadAddress:
1200 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1201 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1202 break;
1203 }
Greg Clayton8b2fe6d2010-12-14 02:59:591204 }
1205 else
1206 {
1207 // Make sure we aren't just trying to see the value of a persistent
1208 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:351209 lldb::ClangExpressionVariableSP persistent_var_sp;
1210 // Only check for persistent variables the expression starts with a '$'
1211 if (expr_cstr[0] == '$')
1212 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1213
Greg Clayton8b2fe6d2010-12-14 02:59:591214 if (persistent_var_sp)
1215 {
1216 result_valobj_sp = persistent_var_sp->GetValueObject ();
1217 execution_results = eExecutionCompleted;
1218 }
1219 else
1220 {
1221 const char *prefix = GetExpressionPrefixContentsAsCString();
1222
1223 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:351224 unwind_on_error,
Greg Clayton8b2fe6d2010-12-14 02:59:591225 expr_cstr,
1226 prefix,
1227 result_valobj_sp);
1228 }
1229 }
Jim Ingham6026ca32011-05-12 02:06:141230
1231 m_suppress_stop_hooks = old_suppress_value;
1232
Greg Clayton8b2fe6d2010-12-14 02:59:591233 return execution_results;
1234}
1235
Greg Claytonf3ef3d22011-05-22 22:46:531236lldb::addr_t
1237Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1238{
1239 addr_t code_addr = load_addr;
1240 switch (m_arch.GetMachine())
1241 {
1242 case llvm::Triple::arm:
1243 case llvm::Triple::thumb:
1244 switch (addr_class)
1245 {
1246 case eAddressClassData:
1247 case eAddressClassDebug:
1248 return LLDB_INVALID_ADDRESS;
1249
1250 case eAddressClassUnknown:
1251 case eAddressClassInvalid:
1252 case eAddressClassCode:
1253 case eAddressClassCodeAlternateISA:
1254 case eAddressClassRuntime:
1255 // Check if bit zero it no set?
1256 if ((code_addr & 1ull) == 0)
1257 {
1258 // Bit zero isn't set, check if the address is a multiple of 2?
1259 if (code_addr & 2ull)
1260 {
1261 // The address is a multiple of 2 so it must be thumb, set bit zero
1262 code_addr |= 1ull;
1263 }
1264 else if (addr_class == eAddressClassCodeAlternateISA)
1265 {
1266 // We checked the address and the address claims to be the alternate ISA
1267 // which means thumb, so set bit zero.
1268 code_addr |= 1ull;
1269 }
1270 }
1271 break;
1272 }
1273 break;
1274
1275 default:
1276 break;
1277 }
1278 return code_addr;
1279}
1280
1281lldb::addr_t
1282Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1283{
1284 addr_t opcode_addr = load_addr;
1285 switch (m_arch.GetMachine())
1286 {
1287 case llvm::Triple::arm:
1288 case llvm::Triple::thumb:
1289 switch (addr_class)
1290 {
1291 case eAddressClassData:
1292 case eAddressClassDebug:
1293 return LLDB_INVALID_ADDRESS;
1294
1295 case eAddressClassInvalid:
1296 case eAddressClassUnknown:
1297 case eAddressClassCode:
1298 case eAddressClassCodeAlternateISA:
1299 case eAddressClassRuntime:
1300 opcode_addr &= ~(1ull);
1301 break;
1302 }
1303 break;
1304
1305 default:
1306 break;
1307 }
1308 return opcode_addr;
1309}
1310
Jim Ingham9575d842011-03-11 03:53:591311lldb::user_id_t
1312Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1313{
1314 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1315 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1316 m_stop_hooks[new_uid] = new_hook_sp;
1317 return new_uid;
1318}
1319
1320bool
1321Target::RemoveStopHookByID (lldb::user_id_t user_id)
1322{
1323 size_t num_removed;
1324 num_removed = m_stop_hooks.erase (user_id);
1325 if (num_removed == 0)
1326 return false;
1327 else
1328 return true;
1329}
1330
1331void
1332Target::RemoveAllStopHooks ()
1333{
1334 m_stop_hooks.clear();
1335}
1336
1337Target::StopHookSP
1338Target::GetStopHookByID (lldb::user_id_t user_id)
1339{
1340 StopHookSP found_hook;
1341
1342 StopHookCollection::iterator specified_hook_iter;
1343 specified_hook_iter = m_stop_hooks.find (user_id);
1344 if (specified_hook_iter != m_stop_hooks.end())
1345 found_hook = (*specified_hook_iter).second;
1346 return found_hook;
1347}
1348
1349bool
1350Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1351{
1352 StopHookCollection::iterator specified_hook_iter;
1353 specified_hook_iter = m_stop_hooks.find (user_id);
1354 if (specified_hook_iter == m_stop_hooks.end())
1355 return false;
1356
1357 (*specified_hook_iter).second->SetIsActive (active_state);
1358 return true;
1359}
1360
1361void
1362Target::SetAllStopHooksActiveState (bool active_state)
1363{
1364 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1365 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1366 {
1367 (*pos).second->SetIsActive (active_state);
1368 }
1369}
1370
1371void
1372Target::RunStopHooks ()
1373{
Jim Ingham6026ca32011-05-12 02:06:141374 if (m_suppress_stop_hooks)
1375 return;
1376
Jim Ingham9575d842011-03-11 03:53:591377 if (!m_process_sp)
1378 return;
1379
1380 if (m_stop_hooks.empty())
1381 return;
1382
1383 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1384
1385 // If there aren't any active stop hooks, don't bother either:
1386 bool any_active_hooks = false;
1387 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1388 {
1389 if ((*pos).second->IsActive())
1390 {
1391 any_active_hooks = true;
1392 break;
1393 }
1394 }
1395 if (!any_active_hooks)
1396 return;
1397
1398 CommandReturnObject result;
1399
1400 std::vector<ExecutionContext> exc_ctx_with_reasons;
1401 std::vector<SymbolContext> sym_ctx_with_reasons;
1402
1403 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1404 size_t num_threads = cur_threadlist.GetSize();
1405 for (size_t i = 0; i < num_threads; i++)
1406 {
1407 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1408 if (cur_thread_sp->ThreadStoppedForAReason())
1409 {
1410 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1411 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1412 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1413 }
1414 }
1415
1416 // If no threads stopped for a reason, don't run the stop-hooks.
1417 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1418 if (num_exe_ctx == 0)
1419 return;
1420
Jim Ingham5b52f0c2011-06-02 23:58:261421 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1422 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:591423
1424 bool keep_going = true;
1425 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271426 bool print_hook_header;
1427 bool print_thread_header;
1428
1429 if (num_exe_ctx == 1)
1430 print_thread_header = false;
1431 else
1432 print_thread_header = true;
1433
1434 if (m_stop_hooks.size() == 1)
1435 print_hook_header = false;
1436 else
1437 print_hook_header = true;
1438
Jim Ingham9575d842011-03-11 03:53:591439 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1440 {
1441 // result.Clear();
1442 StopHookSP cur_hook_sp = (*pos).second;
1443 if (!cur_hook_sp->IsActive())
1444 continue;
1445
1446 bool any_thread_matched = false;
1447 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1448 {
1449 if ((cur_hook_sp->GetSpecifier () == NULL
1450 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1451 && (cur_hook_sp->GetThreadSpecifier() == NULL
1452 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1453 {
1454 if (!hooks_ran)
1455 {
Jim Ingham381e25b2011-03-22 01:47:271456 result.AppendMessage("\n** Stop Hooks **");
Jim Ingham9575d842011-03-11 03:53:591457 hooks_ran = true;
1458 }
Jim Ingham381e25b2011-03-22 01:47:271459 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591460 {
1461 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1462 any_thread_matched = true;
1463 }
1464
Jim Ingham381e25b2011-03-22 01:47:271465 if (print_thread_header)
1466 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591467
1468 bool stop_on_continue = true;
1469 bool stop_on_error = true;
1470 bool echo_commands = false;
1471 bool print_results = true;
1472 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:511473 &exc_ctx_with_reasons[i],
1474 stop_on_continue,
1475 stop_on_error,
1476 echo_commands,
1477 print_results,
1478 result);
Jim Ingham9575d842011-03-11 03:53:591479
1480 // If the command started the target going again, we should bag out of
1481 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:511482 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1483 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:591484 {
1485 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1486 keep_going = false;
1487 }
1488 }
1489 }
1490 }
1491 if (hooks_ran)
1492 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice969ed3d12011-05-02 20:41:461493
1494 result.GetImmediateOutputStream()->Flush();
1495 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:591496}
1497
Greg Clayton7b242382011-07-08 00:48:091498bool
1499Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1500{
1501 bool changed = false;
1502 if (module)
1503 {
1504 ObjectFile *object_file = module->GetObjectFile();
1505 if (object_file)
1506 {
1507 SectionList *section_list = object_file->GetSectionList ();
1508 if (section_list)
1509 {
1510 // All sections listed in the dyld image info structure will all
1511 // either be fixed up already, or they will all be off by a single
1512 // slide amount that is determined by finding the first segment
1513 // that is at file offset zero which also has bytes (a file size
1514 // that is greater than zero) in the object file.
1515
1516 // Determine the slide amount (if any)
1517 const size_t num_sections = section_list->GetSize();
1518 size_t sect_idx = 0;
1519 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1520 {
1521 // Iterate through the object file sections to find the
1522 // first section that starts of file offset zero and that
1523 // has bytes in the file...
1524 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1525 if (section)
1526 {
1527 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1528 changed = true;
1529 }
1530 }
1531 }
1532 }
1533 }
1534 return changed;
1535}
1536
1537
Jim Ingham9575d842011-03-11 03:53:591538//--------------------------------------------------------------
1539// class Target::StopHook
1540//--------------------------------------------------------------
1541
1542
1543Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1544 UserID (uid),
1545 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:591546 m_commands (),
1547 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:401548 m_thread_spec_ap(NULL),
1549 m_active (true)
Jim Ingham9575d842011-03-11 03:53:591550{
1551}
1552
1553Target::StopHook::StopHook (const StopHook &rhs) :
1554 UserID (rhs.GetID()),
1555 m_target_sp (rhs.m_target_sp),
1556 m_commands (rhs.m_commands),
1557 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:401558 m_thread_spec_ap (NULL),
1559 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:591560{
1561 if (rhs.m_thread_spec_ap.get() != NULL)
1562 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1563}
1564
1565
1566Target::StopHook::~StopHook ()
1567{
1568}
1569
1570void
1571Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1572{
1573 m_thread_spec_ap.reset (specifier);
1574}
1575
1576
1577void
1578Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1579{
1580 int indent_level = s->GetIndentLevel();
1581
1582 s->SetIndentLevel(indent_level + 2);
1583
1584 s->Printf ("Hook: %d\n", GetID());
1585 if (m_active)
1586 s->Indent ("State: enabled\n");
1587 else
1588 s->Indent ("State: disabled\n");
1589
1590 if (m_specifier_sp)
1591 {
1592 s->Indent();
1593 s->PutCString ("Specifier:\n");
1594 s->SetIndentLevel (indent_level + 4);
1595 m_specifier_sp->GetDescription (s, level);
1596 s->SetIndentLevel (indent_level + 2);
1597 }
1598
1599 if (m_thread_spec_ap.get() != NULL)
1600 {
1601 StreamString tmp;
1602 s->Indent("Thread:\n");
1603 m_thread_spec_ap->GetDescription (&tmp, level);
1604 s->SetIndentLevel (indent_level + 4);
1605 s->Indent (tmp.GetData());
1606 s->PutCString ("\n");
1607 s->SetIndentLevel (indent_level + 2);
1608 }
1609
1610 s->Indent ("Commands: \n");
1611 s->SetIndentLevel (indent_level + 4);
1612 uint32_t num_commands = m_commands.GetSize();
1613 for (uint32_t i = 0; i < num_commands; i++)
1614 {
1615 s->Indent(m_commands.GetStringAtIndex(i));
1616 s->PutCString ("\n");
1617 }
1618 s->SetIndentLevel (indent_level);
1619}
1620
1621
Caroline Ticedaccaa92010-09-20 20:44:431622//--------------------------------------------------------------
1623// class Target::SettingsController
1624//--------------------------------------------------------------
1625
1626Target::SettingsController::SettingsController () :
1627 UserSettingsController ("target", Debugger::GetSettingsController()),
1628 m_default_architecture ()
1629{
1630 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1631 InstanceSettings::GetDefaultName().AsCString()));
1632}
1633
1634Target::SettingsController::~SettingsController ()
1635{
1636}
1637
1638lldb::InstanceSettingsSP
1639Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1640{
Greg Claytondbe54502010-11-19 03:46:011641 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1642 false,
1643 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431644 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1645 return new_settings_sp;
1646}
1647
Caroline Ticedaccaa92010-09-20 20:44:431648
Jim Ingham78a685a2011-04-16 00:01:131649#define TSC_DEFAULT_ARCH "default-arch"
1650#define TSC_EXPR_PREFIX "expr-prefix"
Jim Ingham78a685a2011-04-16 00:01:131651#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton385aa282011-04-22 03:55:061652#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Clayton7e14f912011-04-23 02:04:551653#define TSC_SOURCE_MAP "source-map"
Enrico Granata22c55d12011-08-12 02:00:061654#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata9128ee2f2011-09-06 19:20:511655#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytonbfe5f3b2011-02-18 01:44:251656
1657
1658static const ConstString &
1659GetSettingNameForDefaultArch ()
1660{
1661 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytonbfe5f3b2011-02-18 01:44:251662 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431663}
1664
Greg Claytonbfe5f3b2011-02-18 01:44:251665static const ConstString &
1666GetSettingNameForExpressionPrefix ()
1667{
1668 static ConstString g_const_string (TSC_EXPR_PREFIX);
1669 return g_const_string;
1670}
1671
1672static const ConstString &
Jim Ingham78a685a2011-04-16 00:01:131673GetSettingNameForPreferDynamicValue ()
1674{
1675 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1676 return g_const_string;
1677}
1678
Greg Clayton7e14f912011-04-23 02:04:551679static const ConstString &
1680GetSettingNameForSourcePathMap ()
1681{
1682 static ConstString g_const_string (TSC_SOURCE_MAP);
1683 return g_const_string;
1684}
Greg Claytonbfe5f3b2011-02-18 01:44:251685
Greg Clayton385aa282011-04-22 03:55:061686static const ConstString &
1687GetSettingNameForSkipPrologue ()
1688{
1689 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1690 return g_const_string;
1691}
1692
Enrico Granata22c55d12011-08-12 02:00:061693static const ConstString &
1694GetSettingNameForMaxChildren ()
1695{
1696 static ConstString g_const_string (TSC_MAX_CHILDREN);
1697 return g_const_string;
1698}
Greg Clayton385aa282011-04-22 03:55:061699
Enrico Granata9128ee2f2011-09-06 19:20:511700static const ConstString &
1701GetSettingNameForMaxStringSummaryLength ()
1702{
1703 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1704 return g_const_string;
1705}
Greg Clayton385aa282011-04-22 03:55:061706
Caroline Ticedaccaa92010-09-20 20:44:431707bool
1708Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1709 const char *index_value,
1710 const char *value,
1711 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541712 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431713 Error&err)
1714{
Greg Claytonbfe5f3b2011-02-18 01:44:251715 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431716 {
Greg Claytoneb0103f2011-04-07 22:46:351717 m_default_architecture.SetTriple (value, NULL);
Greg Clayton64195a22011-02-23 00:35:021718 if (!m_default_architecture.IsValid())
1719 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431720 }
1721 return true;
1722}
1723
1724
1725bool
1726Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1727 StringList &value,
1728 Error &err)
1729{
Greg Claytonbfe5f3b2011-02-18 01:44:251730 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431731 {
Greg Clayton307de252010-10-27 02:06:371732 // If the arch is invalid (the default), don't show a string for it
1733 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021734 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431735 return true;
1736 }
1737 else
1738 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1739
1740 return false;
1741}
1742
1743//--------------------------------------------------------------
1744// class TargetInstanceSettings
1745//--------------------------------------------------------------
1746
Greg Clayton85851dd2010-12-04 00:10:171747TargetInstanceSettings::TargetInstanceSettings
1748(
1749 UserSettingsController &owner,
1750 bool live_instance,
1751 const char *name
1752) :
Greg Claytonbfe5f3b2011-02-18 01:44:251753 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Clayton7e14f912011-04-23 02:04:551754 m_expr_prefix_file (),
1755 m_expr_prefix_contents_sp (),
Jim Ingham2837b762011-05-04 03:43:181756 m_prefer_dynamic_value (2),
Greg Clayton7e14f912011-04-23 02:04:551757 m_skip_prologue (true, true),
Enrico Granata22c55d12011-08-12 02:00:061758 m_source_map (NULL, NULL),
Enrico Granata9128ee2f2011-09-06 19:20:511759 m_max_children_display(256),
1760 m_max_strlen_length(1024)
Caroline Ticedaccaa92010-09-20 20:44:431761{
1762 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1763 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1764 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1765 // This is true for CreateInstanceName() too.
1766
1767 if (GetInstanceName () == InstanceSettings::InvalidName())
1768 {
1769 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1770 m_owner.RegisterInstanceSettings (this);
1771 }
1772
1773 if (live_instance)
1774 {
1775 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1776 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:431777 }
1778}
1779
1780TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton7e14f912011-04-23 02:04:551781 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1782 m_expr_prefix_file (rhs.m_expr_prefix_file),
1783 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1784 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1785 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata22c55d12011-08-12 02:00:061786 m_source_map (rhs.m_source_map),
Enrico Granata9128ee2f2011-09-06 19:20:511787 m_max_children_display(rhs.m_max_children_display),
1788 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Ticedaccaa92010-09-20 20:44:431789{
1790 if (m_instance_name != InstanceSettings::GetDefaultName())
1791 {
1792 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1793 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:431794 }
1795}
1796
1797TargetInstanceSettings::~TargetInstanceSettings ()
1798{
1799}
1800
1801TargetInstanceSettings&
1802TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1803{
1804 if (this != &rhs)
1805 {
1806 }
1807
1808 return *this;
1809}
1810
Caroline Ticedaccaa92010-09-20 20:44:431811void
1812TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1813 const char *index_value,
1814 const char *value,
1815 const ConstString &instance_name,
1816 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541817 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431818 Error &err,
1819 bool pending)
1820{
Greg Claytonbfe5f3b2011-02-18 01:44:251821 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031822 {
Greg Clayton7e14f912011-04-23 02:04:551823 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1824 if (err.Success())
Sean Callanan322f5292010-10-29 00:29:031825 {
Greg Clayton7e14f912011-04-23 02:04:551826 switch (op)
Sean Callanan322f5292010-10-29 00:29:031827 {
Greg Clayton7e14f912011-04-23 02:04:551828 default:
1829 break;
1830 case eVarSetOperationAssign:
1831 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:031832 {
Greg Clayton7e14f912011-04-23 02:04:551833 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1834 {
1835 err.SetErrorToGenericError ();
1836 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1837 return;
1838 }
1839
1840 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1841
1842 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1843 {
1844 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1845 m_expr_prefix_contents_sp.reset();
1846 }
Sean Callanan322f5292010-10-29 00:29:031847 }
Greg Clayton7e14f912011-04-23 02:04:551848 break;
1849 case eVarSetOperationClear:
1850 m_expr_prefix_contents_sp.reset();
Sean Callanan322f5292010-10-29 00:29:031851 }
Sean Callanan322f5292010-10-29 00:29:031852 }
1853 }
Jim Ingham78a685a2011-04-16 00:01:131854 else if (var_name == GetSettingNameForPreferDynamicValue())
1855 {
Jim Ingham2837b762011-05-04 03:43:181856 int new_value;
1857 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1858 if (err.Success())
1859 m_prefer_dynamic_value = new_value;
Greg Clayton385aa282011-04-22 03:55:061860 }
1861 else if (var_name == GetSettingNameForSkipPrologue())
1862 {
Greg Clayton7e14f912011-04-23 02:04:551863 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1864 }
Enrico Granata22c55d12011-08-12 02:00:061865 else if (var_name == GetSettingNameForMaxChildren())
1866 {
1867 bool ok;
1868 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1869 if (ok)
1870 m_max_children_display = new_value;
1871 }
Enrico Granata9128ee2f2011-09-06 19:20:511872 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1873 {
1874 bool ok;
1875 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1876 if (ok)
1877 m_max_strlen_length = new_value;
1878 }
Greg Clayton7e14f912011-04-23 02:04:551879 else if (var_name == GetSettingNameForSourcePathMap ())
1880 {
1881 switch (op)
1882 {
1883 case eVarSetOperationReplace:
1884 case eVarSetOperationInsertBefore:
1885 case eVarSetOperationInsertAfter:
1886 case eVarSetOperationRemove:
1887 default:
1888 break;
1889 case eVarSetOperationAssign:
1890 m_source_map.Clear(true);
1891 // Fall through to append....
1892 case eVarSetOperationAppend:
1893 {
1894 Args args(value);
1895 const uint32_t argc = args.GetArgumentCount();
1896 if (argc & 1 || argc == 0)
1897 {
1898 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1899 }
1900 else
1901 {
1902 char resolved_new_path[PATH_MAX];
1903 FileSpec file_spec;
1904 const char *old_path;
1905 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1906 {
1907 const char *new_path = args.GetArgumentAtIndex(idx+1);
1908 assert (new_path); // We have an even number of paths, this shouldn't happen!
1909
1910 file_spec.SetFile(new_path, true);
1911 if (file_spec.Exists())
1912 {
1913 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1914 {
1915 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1916 return;
1917 }
1918 }
1919 else
1920 {
1921 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1922 return;
1923 }
1924 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1925 }
1926 }
1927 }
1928 break;
1929
1930 case eVarSetOperationClear:
1931 m_source_map.Clear(true);
1932 break;
1933 }
Jim Ingham78a685a2011-04-16 00:01:131934 }
Caroline Ticedaccaa92010-09-20 20:44:431935}
1936
1937void
Greg Claytonbfe5f3b2011-02-18 01:44:251938TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431939{
Sean Callanan322f5292010-10-29 00:29:031940 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1941
1942 if (!new_settings_ptr)
1943 return;
1944
Greg Clayton7e14f912011-04-23 02:04:551945 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1946 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1947 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1948 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata22c55d12011-08-12 02:00:061949 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata9128ee2f2011-09-06 19:20:511950 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Ticedaccaa92010-09-20 20:44:431951}
1952
Caroline Tice12cecd72010-09-20 21:37:421953bool
Caroline Ticedaccaa92010-09-20 20:44:431954TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1955 const ConstString &var_name,
1956 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421957 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431958{
Greg Claytonbfe5f3b2011-02-18 01:44:251959 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031960 {
Greg Clayton7e14f912011-04-23 02:04:551961 char path[PATH_MAX];
1962 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1963 if (path_len > 0)
1964 value.AppendString (path, path_len);
Sean Callanan322f5292010-10-29 00:29:031965 }
Jim Ingham78a685a2011-04-16 00:01:131966 else if (var_name == GetSettingNameForPreferDynamicValue())
1967 {
Jim Ingham2837b762011-05-04 03:43:181968 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Ingham78a685a2011-04-16 00:01:131969 }
Greg Clayton385aa282011-04-22 03:55:061970 else if (var_name == GetSettingNameForSkipPrologue())
1971 {
1972 if (m_skip_prologue)
1973 value.AppendString ("true");
1974 else
1975 value.AppendString ("false");
1976 }
Greg Clayton7e14f912011-04-23 02:04:551977 else if (var_name == GetSettingNameForSourcePathMap ())
1978 {
1979 }
Enrico Granata22c55d12011-08-12 02:00:061980 else if (var_name == GetSettingNameForMaxChildren())
1981 {
1982 StreamString count_str;
1983 count_str.Printf ("%d", m_max_children_display);
1984 value.AppendString (count_str.GetData());
1985 }
Enrico Granata9128ee2f2011-09-06 19:20:511986 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1987 {
1988 StreamString count_str;
1989 count_str.Printf ("%d", m_max_strlen_length);
1990 value.AppendString (count_str.GetData());
1991 }
Sean Callanan322f5292010-10-29 00:29:031992 else
1993 {
1994 if (err)
1995 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1996 return false;
1997 }
1998
1999 return true;
Caroline Ticedaccaa92010-09-20 20:44:432000}
2001
2002const ConstString
2003TargetInstanceSettings::CreateInstanceName ()
2004{
Caroline Ticedaccaa92010-09-20 20:44:432005 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:102006 static int instance_count = 1;
2007
Caroline Ticedaccaa92010-09-20 20:44:432008 sstr.Printf ("target_%d", instance_count);
2009 ++instance_count;
2010
2011 const ConstString ret_val (sstr.GetData());
2012 return ret_val;
2013}
2014
2015//--------------------------------------------------
2016// Target::SettingsController Variable Tables
2017//--------------------------------------------------
Jim Ingham2837b762011-05-04 03:43:182018OptionEnumValueElement
2019TargetInstanceSettings::g_dynamic_value_types[] =
2020{
Greg Clayton5d2fbfe2011-05-30 00:39:482021{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2022{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2023{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham2837b762011-05-04 03:43:182024{ 0, NULL, NULL }
2025};
Caroline Ticedaccaa92010-09-20 20:44:432026
2027SettingEntry
2028Target::SettingsController::global_settings_table[] =
2029{
Greg Claytonbfe5f3b2011-02-18 01:44:252030 // var-name var-type default enum init'd hidden help-text
2031 // ================= ================== =========== ==== ====== ====== =========================================================================
2032 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2033 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2034};
2035
Caroline Ticedaccaa92010-09-20 20:44:432036SettingEntry
2037Target::SettingsController::instance_settings_table[] =
2038{
Enrico Granata9128ee2f2011-09-06 19:20:512039 // var-name var-type default enum init'd hidden help-text
2040 // ================= ================== =============== ======================= ====== ====== =========================================================================
2041 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2042 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2043 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2044 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2045 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2046 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2047 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:432048};