blob: 1a4a7b04741badc1947d3d3b35c3cd763789bf48 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton427f2902010-12-14 02:59:59 +000020#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Caroline Tice4a348082011-05-02 20:41:46 +000023#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000025#include "lldb/Core/Timer.h"
26#include "lldb/Core/ValueObject.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000027#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000029#include "lldb/Interpreter/CommandInterpreter.h"
30#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/lldb-private-log.h"
32#include "lldb/Symbol/ObjectFile.h"
33#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000034#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000035#include "lldb/Target/Thread.h"
36#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
41//----------------------------------------------------------------------
42// Target constructor
43//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000044Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
45 Broadcaster ("lldb.target"),
46 ExecutionContextScope (),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000047 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000048 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000049 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000050 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000051 m_arch (target_arch),
52 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000053 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000054 m_breakpoint_list (false),
55 m_internal_breakpoint_list (true),
Johnny Chen9a3c2a52011-09-06 20:05:25 +000056 m_watchpoint_location_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000057 m_process_sp (),
58 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000059 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000060 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000061 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000062 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000063 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000064 m_stop_hook_next_id (0),
65 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000066{
Greg Clayton49ce6822010-10-31 03:01:06 +000067 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
68 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
69 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
70
Greg Claytone005f2c2010-11-06 01:53:30 +000071 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000072 if (log)
73 log->Printf ("%p Target::Target()", this);
74}
75
76//----------------------------------------------------------------------
77// Destructor
78//----------------------------------------------------------------------
79Target::~Target()
80{
Greg Claytone005f2c2010-11-06 01:53:30 +000081 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000082 if (log)
83 log->Printf ("%p Target::~Target()", this);
84 DeleteCurrentProcess ();
85}
86
87void
Caroline Tice7826c882010-10-26 03:11:13 +000088Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000089{
Greg Clayton3fed8b92010-10-08 00:21:05 +000090// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000091 if (description_level != lldb::eDescriptionLevelBrief)
92 {
93 s->Indent();
94 s->PutCString("Target\n");
95 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000096 m_images.Dump(s);
97 m_breakpoint_list.Dump(s);
98 m_internal_breakpoint_list.Dump(s);
99 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000100 }
101 else
102 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000103 Module *exe_module = GetExecutableModulePointer();
104 if (exe_module)
105 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000106 else
107 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000108 }
Chris Lattner24943d22010-06-08 16:52:24 +0000109}
110
111void
112Target::DeleteCurrentProcess ()
113{
114 if (m_process_sp.get())
115 {
Greg Clayton49480b12010-09-14 23:52:43 +0000116 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000117 if (m_process_sp->IsAlive())
118 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000119
120 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000121
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 Clayton63094e02010-06-23 01:19:29 +0000148 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000149}
150
Greg Clayton153ccd72011-08-10 02:10:13 +0000151void
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 Lattner24943d22010-06-08 16:52:24 +0000174BreakpointList &
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 Clayton33ed1702010-08-24 00:45:41 +0000215Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000216{
Chris Lattner24943d22010-06-08 16:52:24 +0000217 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 Clayton33ed1702010-08-24 00:45:41 +0000221 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000222 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000223 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 Lattner24943d22010-06-08 16:52:24 +0000229 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 Clayton7dd98df2011-07-12 17:06:17 +0000242Target::CreateBreakpoint (const FileSpec *containingModule,
243 const char *func_name,
244 uint32_t func_name_type_mask,
245 bool internal,
246 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000247{
Greg Clayton12bec712010-06-28 21:30:43 +0000248 BreakpointSP bp_sp;
249 if (func_name)
250 {
251 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000252
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 Clayton12bec712010-06-28 21:30:43 +0000258 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
259 }
260 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000261}
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 Clayton7dd98df2011-07-12 17:06:17 +0000285Target::CreateBreakpoint (const FileSpec *containingModule,
286 RegularExpression &func_regex,
287 bool internal,
288 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000289{
290 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000291 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
292 func_regex,
293 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000294
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 Claytonc7f5d5c2010-07-23 23:33:17 +0000308 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000309 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000310 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000311
Greg Claytone005f2c2010-11-06 01:53:30 +0000312 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000313 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 Lattner24943d22010-06-08 16:52:24 +0000320 bp_sp->ResolveBreakpoint();
321 }
Jim Inghamd1686902010-10-14 23:45:03 +0000322
323 if (!internal && bp_sp)
324 {
325 m_last_created_breakpoint = bp_sp;
326 }
327
Chris Lattner24943d22010-06-08 16:52:24 +0000328 return bp_sp;
329}
330
Johnny Chen34bbf852011-09-12 23:38:44 +0000331// 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 Chen61286a52011-09-13 18:30:59 +0000336 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 Chen34bbf852011-09-12 23:38:44 +0000342 return wp_loc_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000343
Johnny Chen69b6ec82011-09-13 23:29:31 +0000344 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 Chen9bf11992011-09-13 01:15:36 +0000365 return wp_loc_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000366}
367
Chris Lattner24943d22010-06-08 16:52:24 +0000368void
369Target::RemoveAllBreakpoints (bool internal_also)
370{
Greg Claytone005f2c2010-11-06 01:53:30 +0000371 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000372 if (log)
373 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
374
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000375 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000376 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000377 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000378
379 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000380}
381
382void
383Target::DisableAllBreakpoints (bool internal_also)
384{
Greg Claytone005f2c2010-11-06 01:53:30 +0000385 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000386 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 Claytone005f2c2010-11-06 01:53:30 +0000397 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000398 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 Claytone005f2c2010-11-06 01:53:30 +0000409 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000410 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 Claytonc7f5d5c2010-07-23 23:33:17 +0000416 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000417 else
Jim Inghamd1686902010-10-14 23:45:03 +0000418 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000419 if (m_last_created_breakpoint)
420 {
421 if (m_last_created_breakpoint->GetID() == break_id)
422 m_last_created_breakpoint.reset();
423 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000424 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000425 }
Chris Lattner24943d22010-06-08 16:52:24 +0000426 return true;
427 }
428 return false;
429}
430
431bool
432Target::DisableBreakpointByID (break_id_t break_id)
433{
Greg Claytone005f2c2010-11-06 01:53:30 +0000434 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000435 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 Claytone005f2c2010-11-06 01:53:30 +0000455 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000456 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 Clayton5beb99d2011-08-11 02:48:45 +0000480 return m_images.GetModuleAtIndex(0);
481}
482
483Module*
484Target::GetExecutableModulePointer ()
485{
486 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000487}
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 Ingham7508e732010-08-09 23:31:02 +0000504 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000505 if (!m_arch.IsValid())
506 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000507
Chris Lattner24943d22010-06-08 16:52:24 +0000508 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000509 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000510 // 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 Lattner24943d22010-06-08 16:52:24 +0000530
531 if (executable_objfile)
532 {
533 executable_objfile->GetDependentModules(dependent_files);
534 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
535 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000536 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
537 FileSpec platform_dependent_file_spec;
538 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000539 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000540 else
541 platform_dependent_file_spec = dependent_file_spec;
542
543 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000544 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000545 if (image_module_sp.get())
546 {
Chris Lattner24943d22010-06-08 16:52:24 +0000547 ObjectFile *objfile = image_module_sp->GetObjectFile();
548 if (objfile)
549 objfile->GetDependentModules(dependent_files);
550 }
551 }
552 }
553
Chris Lattner24943d22010-06-08 16:52:24 +0000554 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000555
556 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000557}
558
559
Jim Ingham7508e732010-08-09 23:31:02 +0000560bool
561Target::SetArchitecture (const ArchSpec &arch_spec)
562{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000563 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000564 {
565 // If we're setting the architecture to our current architecture, we
566 // don't need to do anything.
567 return true;
568 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000569 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000570 {
571 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000572 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000573 return true;
574 }
575 else
576 {
577 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000578 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000579 ModuleSP executable_sp = GetExecutableModule ();
580 m_images.Clear();
581 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000582 // 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 Lattner24943d22010-06-08 16:52:24 +0000612
Chris Lattner24943d22010-06-08 16:52:24 +0000613void
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 Ingham3b8a6052011-08-03 01:00:06 +0000625 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000626 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 Clayton7b9fcc02010-12-06 23:51:26 +0000646
647 // Remove the images from the target image list
648 m_images.Remove(module_list);
649
Chris Lattner24943d22010-06-08 16:52:24 +0000650 // TODO: make event data that packages up the module_list
651 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
652}
653
654size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000655Target::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 Granata91544802011-09-06 19:20:51 +0000685Target::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 Lattner24943d22010-06-08 16:52:24 +0000691{
Chris Lattner24943d22010-06-08 16:52:24 +0000692 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000693
Enrico Granata91544802011-09-06 19:20:51 +0000694 // 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 Clayton70436352010-06-30 23:03:03 +0000699 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
700
Greg Clayton26100dc2011-01-07 01:57:07 +0000701 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000702
703 addr_t load_addr = LLDB_INVALID_ADDRESS;
704 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000705 Address resolved_addr;
706 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000707 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000708 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000709 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000710 // 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 Clayton9b82f862011-07-11 05:12:02 +0000714 }
Greg Clayton70436352010-06-30 23:03:03 +0000715 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000716 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000717 // 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 Clayton9b82f862011-07-11 05:12:02 +0000723 }
Greg Clayton70436352010-06-30 23:03:03 +0000724 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000725 if (!resolved_addr.IsValid())
726 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000727
Greg Clayton9b82f862011-07-11 05:12:02 +0000728
Greg Clayton26100dc2011-01-07 01:57:07 +0000729 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 Clayton70436352010-06-30 23:03:03 +0000735
736 if (process_is_valid)
737 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000738 if (load_addr == LLDB_INVALID_ADDRESS)
739 load_addr = resolved_addr.GetLoadAddress (this);
740
Greg Clayton70436352010-06-30 23:03:03 +0000741 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 Clayton26100dc2011-01-07 01:57:07 +0000752 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000753 if (bytes_read != dst_len)
754 {
755 if (error.Success())
756 {
757 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000758 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000759 else
Greg Clayton70436352010-06-30 23:03:03 +0000760 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000761 }
762 }
Greg Clayton70436352010-06-30 23:03:03 +0000763 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +0000764 {
765 if (load_addr_ptr)
766 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +0000767 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +0000768 }
Greg Clayton70436352010-06-30 23:03:03 +0000769 // 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 Lattner24943d22010-06-08 16:52:24 +0000776 }
Chris Lattner24943d22010-06-08 16:52:24 +0000777 }
Greg Clayton70436352010-06-30 23:03:03 +0000778
Greg Clayton9b82f862011-07-11 05:12:02 +0000779 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000780 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000781 // 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 Clayton70436352010-06-30 23:03:03 +0000784 }
785 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000786}
787
Greg Clayton7dd98df2011-07-12 17:06:17 +0000788size_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 Lattner24943d22010-06-08 16:52:24 +0000881
882ModuleSP
883Target::GetSharedModule
884(
885 const FileSpec& file_spec,
886 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000887 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000888 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 Lattner24943d22010-06-08 16:52:24 +0000898 Error error;
899
Greg Clayton24bc5d92011-03-30 18:16:51 +0000900 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +0000901 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 Clayton24bc5d92011-03-30 18:16:51 +0000911 // The platform is responsible for finding and caching an appropriate
912 // module in the shared module cache.
913 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000914 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000915 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 Lattner24943d22010-06-08 16:52:24 +0000928 }
929
Greg Clayton24bc5d92011-03-30 18:16:51 +0000930 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +0000931 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 Claytona830adb2010-10-04 01:05:56 +0000973Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000974{
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 Clayton5beb99d2011-08-11 02:48:45 +0000995 ModuleSP exe_module_sp (target->GetExecutableModule());
996 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000997 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000998 target->m_images.Clear();
999 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001000 }
1001}
1002
1003ClangASTContext *
1004Target::GetScratchClangASTContext()
1005{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001006 // 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 Lattner24943d22010-06-08 16:52:24 +00001009 return m_scratch_ast_context_ap.get();
1010}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001011
Greg Clayton990de7b2010-11-18 23:32:35 +00001012void
Caroline Tice2a456812011-03-10 22:14:10 +00001013Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001014{
Greg Clayton990de7b2010-11-18 23:32:35 +00001015 UserSettingsControllerSP &usc = GetSettingsController();
1016 usc.reset (new SettingsController);
1017 UserSettingsController::InitializeSettingsController (usc,
1018 SettingsController::global_settings_table,
1019 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001020
1021 // Now call SettingsInitialize() on each 'child' setting of Target
1022 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001023}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001024
Greg Clayton990de7b2010-11-18 23:32:35 +00001025void
Caroline Tice2a456812011-03-10 22:14:10 +00001026Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001027{
Caroline Tice2a456812011-03-10 22:14:10 +00001028
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 Clayton990de7b2010-11-18 23:32:35 +00001035 UserSettingsControllerSP &usc = GetSettingsController();
1036 UserSettingsController::FinalizeSettingsController (usc);
1037 usc.reset();
1038}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001039
Greg Clayton990de7b2010-11-18 23:32:35 +00001040UserSettingsControllerSP &
1041Target::GetSettingsController ()
1042{
1043 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001044 return g_settings_controller;
1045}
1046
1047ArchSpec
1048Target::GetDefaultArchitecture ()
1049{
Greg Clayton940b1032011-02-23 00:35:02 +00001050 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 Tice5bc8c972010-09-20 20:44:43 +00001055}
1056
1057void
Greg Clayton940b1032011-02-23 00:35:02 +00001058Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001059{
Greg Clayton940b1032011-02-23 00:35:02 +00001060 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1061
1062 if (settings_controller_sp)
1063 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001064}
1065
Greg Claytona830adb2010-10-04 01:05:56 +00001066Target *
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 Tice1ebef442010-09-27 00:30:10 +00001085void
1086Target::UpdateInstanceName ()
1087{
1088 StreamString sstr;
1089
Greg Clayton5beb99d2011-08-11 02:48:45 +00001090 Module *exe_module = GetExecutableModulePointer();
1091 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001092 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001093 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001094 exe_module->GetFileSpec().GetFilename().AsCString(),
1095 exe_module->GetArchitecture().GetArchitectureName());
1096 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001097 }
1098}
1099
Sean Callanan77e93942010-10-29 00:29:03 +00001100const char *
1101Target::GetExpressionPrefixContentsAsCString ()
1102{
Greg Claytonff44ab42011-04-23 02:04:55 +00001103 if (m_expr_prefix_contents_sp)
1104 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1105 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001106}
1107
Greg Clayton427f2902010-12-14 02:59:59 +00001108ExecutionResults
1109Target::EvaluateExpression
1110(
1111 const char *expr_cstr,
1112 StackFrame *frame,
1113 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001114 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001115 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001116 lldb::ValueObjectSP &result_valobj_sp
1117)
1118{
1119 ExecutionResults execution_results = eExecutionSetupError;
1120
1121 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001122
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 Clayton427f2902010-12-14 02:59:59 +00001127
1128 ExecutionContext exe_ctx;
1129 if (frame)
1130 {
1131 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001132 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001133 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001134 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1135 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001136 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 Clayton427f2902010-12-14 02:59:59 +00001142 }
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 Inghame41494a2011-04-16 00:01:13 +00001167 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001168 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001169 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001170 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001171 if (dynamic_sp)
1172 result_valobj_sp = dynamic_sp;
1173 }
1174
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001175 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001176 }
Greg Clayton427f2902010-12-14 02:59:59 +00001177
Sean Callanan6a925532011-01-13 08:53:35 +00001178 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1179
Greg Clayton427f2902010-12-14 02:59:59 +00001180 result_valobj_sp = const_valobj_sp;
1181
Sean Callanan6a925532011-01-13 08:53:35 +00001182 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 Clayton427f2902010-12-14 02:59:59 +00001204 }
1205 else
1206 {
1207 // Make sure we aren't just trying to see the value of a persistent
1208 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001209 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 Clayton427f2902010-12-14 02:59:59 +00001214 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 Callanan6a925532011-01-13 08:53:35 +00001224 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001225 expr_cstr,
1226 prefix,
1227 result_valobj_sp);
1228 }
1229 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001230
1231 m_suppress_stop_hooks = old_suppress_value;
1232
Greg Clayton427f2902010-12-14 02:59:59 +00001233 return execution_results;
1234}
1235
Greg Claytonc0fa5332011-05-22 22:46:53 +00001236lldb::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 Inghamd60d94a2011-03-11 03:53:59 +00001311lldb::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 Ingham3613ae12011-05-12 02:06:14 +00001374 if (m_suppress_stop_hooks)
1375 return;
1376
Jim Inghamd60d94a2011-03-11 03:53:59 +00001377 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 Inghame5ed8e92011-06-02 23:58:26 +00001421 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1422 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001423
1424 bool keep_going = true;
1425 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001426 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 Inghamd60d94a2011-03-11 03:53:59 +00001439 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 Inghamc54840c2011-03-22 01:47:27 +00001456 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001457 hooks_ran = true;
1458 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001459 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001460 {
1461 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1462 any_thread_matched = true;
1463 }
1464
Jim Inghamc54840c2011-03-22 01:47:27 +00001465 if (print_thread_header)
1466 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001467
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 Clayton24bc5d92011-03-30 18:16:51 +00001473 &exc_ctx_with_reasons[i],
1474 stop_on_continue,
1475 stop_on_error,
1476 echo_commands,
1477 print_results,
1478 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001479
1480 // If the command started the target going again, we should bag out of
1481 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001482 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1483 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001484 {
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 Tice4a348082011-05-02 20:41:46 +00001493
1494 result.GetImmediateOutputStream()->Flush();
1495 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001496}
1497
Greg Claytonbbea1332011-07-08 00:48:09 +00001498bool
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 Inghamd60d94a2011-03-11 03:53:59 +00001538//--------------------------------------------------------------
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 Inghamd60d94a2011-03-11 03:53:59 +00001546 m_commands (),
1547 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001548 m_thread_spec_ap(NULL),
1549 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001550{
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 Wilsondbeb3e12011-04-11 19:41:40 +00001558 m_thread_spec_ap (NULL),
1559 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001560{
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 Tice5bc8c972010-09-20 20:44:43 +00001622//--------------------------------------------------------------
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 Claytonc0c1b0c2010-11-19 03:46:01 +00001641 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1642 false,
1643 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001644 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1645 return new_settings_sp;
1646}
1647
Caroline Tice5bc8c972010-09-20 20:44:43 +00001648
Jim Inghame41494a2011-04-16 00:01:13 +00001649#define TSC_DEFAULT_ARCH "default-arch"
1650#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001651#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001652#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001653#define TSC_SOURCE_MAP "source-map"
Enrico Granata018921d2011-08-12 02:00:06 +00001654#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata91544802011-09-06 19:20:51 +00001655#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytond284b662011-02-18 01:44:25 +00001656
1657
1658static const ConstString &
1659GetSettingNameForDefaultArch ()
1660{
1661 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001662 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001663}
1664
Greg Claytond284b662011-02-18 01:44:25 +00001665static const ConstString &
1666GetSettingNameForExpressionPrefix ()
1667{
1668 static ConstString g_const_string (TSC_EXPR_PREFIX);
1669 return g_const_string;
1670}
1671
1672static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001673GetSettingNameForPreferDynamicValue ()
1674{
1675 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1676 return g_const_string;
1677}
1678
Greg Claytonff44ab42011-04-23 02:04:55 +00001679static const ConstString &
1680GetSettingNameForSourcePathMap ()
1681{
1682 static ConstString g_const_string (TSC_SOURCE_MAP);
1683 return g_const_string;
1684}
Greg Claytond284b662011-02-18 01:44:25 +00001685
Greg Clayton17cd9952011-04-22 03:55:06 +00001686static const ConstString &
1687GetSettingNameForSkipPrologue ()
1688{
1689 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1690 return g_const_string;
1691}
1692
Enrico Granata018921d2011-08-12 02:00:06 +00001693static const ConstString &
1694GetSettingNameForMaxChildren ()
1695{
1696 static ConstString g_const_string (TSC_MAX_CHILDREN);
1697 return g_const_string;
1698}
Greg Clayton17cd9952011-04-22 03:55:06 +00001699
Enrico Granata91544802011-09-06 19:20:51 +00001700static const ConstString &
1701GetSettingNameForMaxStringSummaryLength ()
1702{
1703 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1704 return g_const_string;
1705}
Greg Clayton17cd9952011-04-22 03:55:06 +00001706
Caroline Tice5bc8c972010-09-20 20:44:43 +00001707bool
1708Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1709 const char *index_value,
1710 const char *value,
1711 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001712 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001713 Error&err)
1714{
Greg Claytond284b662011-02-18 01:44:25 +00001715 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001716 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001717 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001718 if (!m_default_architecture.IsValid())
1719 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001720 }
1721 return true;
1722}
1723
1724
1725bool
1726Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1727 StringList &value,
1728 Error &err)
1729{
Greg Claytond284b662011-02-18 01:44:25 +00001730 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001731 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001732 // If the arch is invalid (the default), don't show a string for it
1733 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001734 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001735 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 Clayton638351a2010-12-04 00:10:17 +00001747TargetInstanceSettings::TargetInstanceSettings
1748(
1749 UserSettingsController &owner,
1750 bool live_instance,
1751 const char *name
1752) :
Greg Claytond284b662011-02-18 01:44:25 +00001753 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001754 m_expr_prefix_file (),
1755 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001756 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001757 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00001758 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00001759 m_max_children_display(256),
1760 m_max_strlen_length(1024)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001761{
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 Tice5bc8c972010-09-20 20:44:43 +00001777 }
1778}
1779
1780TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001781 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 Granata018921d2011-08-12 02:00:06 +00001786 m_source_map (rhs.m_source_map),
Enrico Granata91544802011-09-06 19:20:51 +00001787 m_max_children_display(rhs.m_max_children_display),
1788 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001789{
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 Tice5bc8c972010-09-20 20:44:43 +00001794 }
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 Tice5bc8c972010-09-20 20:44:43 +00001811void
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 Claytonb3448432011-03-24 21:19:54 +00001817 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001818 Error &err,
1819 bool pending)
1820{
Greg Claytond284b662011-02-18 01:44:25 +00001821 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001822 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001823 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1824 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00001825 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001826 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00001827 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001828 default:
1829 break;
1830 case eVarSetOperationAssign:
1831 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00001832 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001833 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 Callanan77e93942010-10-29 00:29:03 +00001847 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001848 break;
1849 case eVarSetOperationClear:
1850 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00001851 }
Sean Callanan77e93942010-10-29 00:29:03 +00001852 }
1853 }
Jim Inghame41494a2011-04-16 00:01:13 +00001854 else if (var_name == GetSettingNameForPreferDynamicValue())
1855 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001856 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 Clayton17cd9952011-04-22 03:55:06 +00001860 }
1861 else if (var_name == GetSettingNameForSkipPrologue())
1862 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001863 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1864 }
Enrico Granata018921d2011-08-12 02:00:06 +00001865 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 Granata91544802011-09-06 19:20:51 +00001872 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 Claytonff44ab42011-04-23 02:04:55 +00001879 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 Inghame41494a2011-04-16 00:01:13 +00001934 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001935}
1936
1937void
Greg Claytond284b662011-02-18 01:44:25 +00001938TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001939{
Sean Callanan77e93942010-10-29 00:29:03 +00001940 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1941
1942 if (!new_settings_ptr)
1943 return;
1944
Greg Claytonff44ab42011-04-23 02:04:55 +00001945 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 Granata018921d2011-08-12 02:00:06 +00001949 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata91544802011-09-06 19:20:51 +00001950 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001951}
1952
Caroline Ticebcb5b452010-09-20 21:37:42 +00001953bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001954TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1955 const ConstString &var_name,
1956 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001957 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001958{
Greg Claytond284b662011-02-18 01:44:25 +00001959 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001960 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001961 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 Callanan77e93942010-10-29 00:29:03 +00001965 }
Jim Inghame41494a2011-04-16 00:01:13 +00001966 else if (var_name == GetSettingNameForPreferDynamicValue())
1967 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001968 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00001969 }
Greg Clayton17cd9952011-04-22 03:55:06 +00001970 else if (var_name == GetSettingNameForSkipPrologue())
1971 {
1972 if (m_skip_prologue)
1973 value.AppendString ("true");
1974 else
1975 value.AppendString ("false");
1976 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001977 else if (var_name == GetSettingNameForSourcePathMap ())
1978 {
1979 }
Enrico Granata018921d2011-08-12 02:00:06 +00001980 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 Granata91544802011-09-06 19:20:51 +00001986 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 Callanan77e93942010-10-29 00:29:03 +00001992 else
1993 {
1994 if (err)
1995 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1996 return false;
1997 }
1998
1999 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002000}
2001
2002const ConstString
2003TargetInstanceSettings::CreateInstanceName ()
2004{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002005 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002006 static int instance_count = 1;
2007
Caroline Tice5bc8c972010-09-20 20:44:43 +00002008 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 Ingham10de7d12011-05-04 03:43:18 +00002018OptionEnumValueElement
2019TargetInstanceSettings::g_dynamic_value_types[] =
2020{
Greg Clayton577fbc32011-05-30 00:39:48 +00002021{ 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 Ingham10de7d12011-05-04 03:43:18 +00002024{ 0, NULL, NULL }
2025};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002026
2027SettingEntry
2028Target::SettingsController::global_settings_table[] =
2029{
Greg Claytond284b662011-02-18 01:44:25 +00002030 // 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 Tice5bc8c972010-09-20 20:44:43 +00002036SettingEntry
2037Target::SettingsController::instance_settings_table[] =
2038{
Enrico Granata91544802011-09-06 19:20:51 +00002039 // 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 Tice5bc8c972010-09-20 20:44:43 +00002048};