blob: 7b6dcf75cb971ceeb21725eb5ca0ce95c6c06304 [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
344 // FIXME: Add implmenetation.
345 return wp_loc_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000346}
347
Chris Lattner24943d22010-06-08 16:52:24 +0000348void
349Target::RemoveAllBreakpoints (bool internal_also)
350{
Greg Claytone005f2c2010-11-06 01:53:30 +0000351 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000352 if (log)
353 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
354
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000355 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000356 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000357 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000358
359 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000360}
361
362void
363Target::DisableAllBreakpoints (bool internal_also)
364{
Greg Claytone005f2c2010-11-06 01:53:30 +0000365 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000366 if (log)
367 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
368
369 m_breakpoint_list.SetEnabledAll (false);
370 if (internal_also)
371 m_internal_breakpoint_list.SetEnabledAll (false);
372}
373
374void
375Target::EnableAllBreakpoints (bool internal_also)
376{
Greg Claytone005f2c2010-11-06 01:53:30 +0000377 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000378 if (log)
379 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
380
381 m_breakpoint_list.SetEnabledAll (true);
382 if (internal_also)
383 m_internal_breakpoint_list.SetEnabledAll (true);
384}
385
386bool
387Target::RemoveBreakpointByID (break_id_t break_id)
388{
Greg Claytone005f2c2010-11-06 01:53:30 +0000389 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000390 if (log)
391 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
392
393 if (DisableBreakpointByID (break_id))
394 {
395 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000396 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000397 else
Jim Inghamd1686902010-10-14 23:45:03 +0000398 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000399 if (m_last_created_breakpoint)
400 {
401 if (m_last_created_breakpoint->GetID() == break_id)
402 m_last_created_breakpoint.reset();
403 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000404 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000405 }
Chris Lattner24943d22010-06-08 16:52:24 +0000406 return true;
407 }
408 return false;
409}
410
411bool
412Target::DisableBreakpointByID (break_id_t break_id)
413{
Greg Claytone005f2c2010-11-06 01:53:30 +0000414 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000415 if (log)
416 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
417
418 BreakpointSP bp_sp;
419
420 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
421 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
422 else
423 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
424 if (bp_sp)
425 {
426 bp_sp->SetEnabled (false);
427 return true;
428 }
429 return false;
430}
431
432bool
433Target::EnableBreakpointByID (break_id_t break_id)
434{
Greg Claytone005f2c2010-11-06 01:53:30 +0000435 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000436 if (log)
437 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
438 __FUNCTION__,
439 break_id,
440 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
441
442 BreakpointSP bp_sp;
443
444 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
445 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
446 else
447 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
448
449 if (bp_sp)
450 {
451 bp_sp->SetEnabled (true);
452 return true;
453 }
454 return false;
455}
456
457ModuleSP
458Target::GetExecutableModule ()
459{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000460 return m_images.GetModuleAtIndex(0);
461}
462
463Module*
464Target::GetExecutableModulePointer ()
465{
466 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000467}
468
469void
470Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
471{
472 m_images.Clear();
473 m_scratch_ast_context_ap.reset();
474
475 if (executable_sp.get())
476 {
477 Timer scoped_timer (__PRETTY_FUNCTION__,
478 "Target::SetExecutableModule (executable = '%s/%s')",
479 executable_sp->GetFileSpec().GetDirectory().AsCString(),
480 executable_sp->GetFileSpec().GetFilename().AsCString());
481
482 m_images.Append(executable_sp); // The first image is our exectuable file
483
Jim Ingham7508e732010-08-09 23:31:02 +0000484 // 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 +0000485 if (!m_arch.IsValid())
486 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000487
Chris Lattner24943d22010-06-08 16:52:24 +0000488 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000489 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000490 // Let's find the file & line for main and set the default source file from there.
491 if (!m_source_manager.DefaultFileAndLineSet())
492 {
493 SymbolContextList sc_list;
494 uint32_t num_matches;
495 ConstString main_name("main");
496 bool symbols_okay = false; // Force it to be a debug symbol.
497 bool append = false;
498 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
499 for (uint32_t idx = 0; idx < num_matches; idx++)
500 {
501 SymbolContext sc;
502 sc_list.GetContextAtIndex(idx, sc);
503 if (sc.line_entry.file)
504 {
505 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
506 break;
507 }
508 }
509 }
Chris Lattner24943d22010-06-08 16:52:24 +0000510
511 if (executable_objfile)
512 {
513 executable_objfile->GetDependentModules(dependent_files);
514 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
515 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000516 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
517 FileSpec platform_dependent_file_spec;
518 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000519 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000520 else
521 platform_dependent_file_spec = dependent_file_spec;
522
523 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000524 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000525 if (image_module_sp.get())
526 {
Chris Lattner24943d22010-06-08 16:52:24 +0000527 ObjectFile *objfile = image_module_sp->GetObjectFile();
528 if (objfile)
529 objfile->GetDependentModules(dependent_files);
530 }
531 }
532 }
533
Chris Lattner24943d22010-06-08 16:52:24 +0000534 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000535
536 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000537}
538
539
Jim Ingham7508e732010-08-09 23:31:02 +0000540bool
541Target::SetArchitecture (const ArchSpec &arch_spec)
542{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000543 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000544 {
545 // If we're setting the architecture to our current architecture, we
546 // don't need to do anything.
547 return true;
548 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000549 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000550 {
551 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000552 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000553 return true;
554 }
555 else
556 {
557 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000558 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000559 ModuleSP executable_sp = GetExecutableModule ();
560 m_images.Clear();
561 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000562 // Need to do something about unsetting breakpoints.
563
564 if (executable_sp)
565 {
566 FileSpec exec_file_spec = executable_sp->GetFileSpec();
567 Error error = ModuleList::GetSharedModule(exec_file_spec,
568 arch_spec,
569 NULL,
570 NULL,
571 0,
572 executable_sp,
573 NULL,
574 NULL);
575
576 if (!error.Fail() && executable_sp)
577 {
578 SetExecutableModule (executable_sp, true);
579 return true;
580 }
581 else
582 {
583 return false;
584 }
585 }
586 else
587 {
588 return false;
589 }
590 }
591}
Chris Lattner24943d22010-06-08 16:52:24 +0000592
Chris Lattner24943d22010-06-08 16:52:24 +0000593void
594Target::ModuleAdded (ModuleSP &module_sp)
595{
596 // A module is being added to this target for the first time
597 ModuleList module_list;
598 module_list.Append(module_sp);
599 ModulesDidLoad (module_list);
600}
601
602void
603Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
604{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000605 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000606 ModuleList module_list;
607 module_list.Append (old_module_sp);
608 ModulesDidUnload (module_list);
609 module_list.Clear ();
610 module_list.Append (new_module_sp);
611 ModulesDidLoad (module_list);
612}
613
614void
615Target::ModulesDidLoad (ModuleList &module_list)
616{
617 m_breakpoint_list.UpdateBreakpoints (module_list, true);
618 // TODO: make event data that packages up the module_list
619 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
620}
621
622void
623Target::ModulesDidUnload (ModuleList &module_list)
624{
625 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000626
627 // Remove the images from the target image list
628 m_images.Remove(module_list);
629
Chris Lattner24943d22010-06-08 16:52:24 +0000630 // TODO: make event data that packages up the module_list
631 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
632}
633
634size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000635Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
636{
637 const Section *section = addr.GetSection();
638 if (section && section->GetModule())
639 {
640 ObjectFile *objfile = section->GetModule()->GetObjectFile();
641 if (objfile)
642 {
643 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
644 addr.GetOffset(),
645 dst,
646 dst_len);
647 if (bytes_read > 0)
648 return bytes_read;
649 else
650 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
651 }
652 else
653 {
654 error.SetErrorString("address isn't from a object file");
655 }
656 }
657 else
658 {
659 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
660 }
661 return 0;
662}
663
664size_t
Enrico Granata91544802011-09-06 19:20:51 +0000665Target::ReadMemory (const Address& addr,
666 bool prefer_file_cache,
667 void *dst,
668 size_t dst_len,
669 Error &error,
670 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000671{
Chris Lattner24943d22010-06-08 16:52:24 +0000672 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000673
Enrico Granata91544802011-09-06 19:20:51 +0000674 // if we end up reading this from process memory, we will fill this
675 // with the actual load address
676 if (load_addr_ptr)
677 *load_addr_ptr = LLDB_INVALID_ADDRESS;
678
Greg Clayton70436352010-06-30 23:03:03 +0000679 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
680
Greg Clayton26100dc2011-01-07 01:57:07 +0000681 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000682
683 addr_t load_addr = LLDB_INVALID_ADDRESS;
684 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000685 Address resolved_addr;
686 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000687 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000688 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000689 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000690 // No sections are loaded, so we must assume we are not running
691 // yet and anything we are given is a file address.
692 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
693 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000694 }
Greg Clayton70436352010-06-30 23:03:03 +0000695 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000696 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000697 // We have at least one section loaded. This can be becuase
698 // we have manually loaded some sections with "target modules load ..."
699 // or because we have have a live process that has sections loaded
700 // through the dynamic loader
701 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
702 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000703 }
Greg Clayton70436352010-06-30 23:03:03 +0000704 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000705 if (!resolved_addr.IsValid())
706 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000707
Greg Clayton9b82f862011-07-11 05:12:02 +0000708
Greg Clayton26100dc2011-01-07 01:57:07 +0000709 if (prefer_file_cache)
710 {
711 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
712 if (bytes_read > 0)
713 return bytes_read;
714 }
Greg Clayton70436352010-06-30 23:03:03 +0000715
716 if (process_is_valid)
717 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000718 if (load_addr == LLDB_INVALID_ADDRESS)
719 load_addr = resolved_addr.GetLoadAddress (this);
720
Greg Clayton70436352010-06-30 23:03:03 +0000721 if (load_addr == LLDB_INVALID_ADDRESS)
722 {
723 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
724 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
725 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
726 resolved_addr.GetFileAddress());
727 else
728 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
729 }
730 else
731 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000732 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000733 if (bytes_read != dst_len)
734 {
735 if (error.Success())
736 {
737 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000738 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000739 else
Greg Clayton70436352010-06-30 23:03:03 +0000740 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 +0000741 }
742 }
Greg Clayton70436352010-06-30 23:03:03 +0000743 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +0000744 {
745 if (load_addr_ptr)
746 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +0000747 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +0000748 }
Greg Clayton70436352010-06-30 23:03:03 +0000749 // If the address is not section offset we have an address that
750 // doesn't resolve to any address in any currently loaded shared
751 // libaries and we failed to read memory so there isn't anything
752 // more we can do. If it is section offset, we might be able to
753 // read cached memory from the object file.
754 if (!resolved_addr.IsSectionOffset())
755 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000756 }
Chris Lattner24943d22010-06-08 16:52:24 +0000757 }
Greg Clayton70436352010-06-30 23:03:03 +0000758
Greg Clayton9b82f862011-07-11 05:12:02 +0000759 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000760 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000761 // If we didn't already try and read from the object file cache, then
762 // try it after failing to read from the process.
763 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000764 }
765 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000766}
767
Greg Clayton7dd98df2011-07-12 17:06:17 +0000768size_t
769Target::ReadScalarIntegerFromMemory (const Address& addr,
770 bool prefer_file_cache,
771 uint32_t byte_size,
772 bool is_signed,
773 Scalar &scalar,
774 Error &error)
775{
776 uint64_t uval;
777
778 if (byte_size <= sizeof(uval))
779 {
780 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
781 if (bytes_read == byte_size)
782 {
783 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
784 uint32_t offset = 0;
785 if (byte_size <= 4)
786 scalar = data.GetMaxU32 (&offset, byte_size);
787 else
788 scalar = data.GetMaxU64 (&offset, byte_size);
789
790 if (is_signed)
791 scalar.SignExtend(byte_size * 8);
792 return bytes_read;
793 }
794 }
795 else
796 {
797 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
798 }
799 return 0;
800}
801
802uint64_t
803Target::ReadUnsignedIntegerFromMemory (const Address& addr,
804 bool prefer_file_cache,
805 size_t integer_byte_size,
806 uint64_t fail_value,
807 Error &error)
808{
809 Scalar scalar;
810 if (ReadScalarIntegerFromMemory (addr,
811 prefer_file_cache,
812 integer_byte_size,
813 false,
814 scalar,
815 error))
816 return scalar.ULongLong(fail_value);
817 return fail_value;
818}
819
820bool
821Target::ReadPointerFromMemory (const Address& addr,
822 bool prefer_file_cache,
823 Error &error,
824 Address &pointer_addr)
825{
826 Scalar scalar;
827 if (ReadScalarIntegerFromMemory (addr,
828 prefer_file_cache,
829 m_arch.GetAddressByteSize(),
830 false,
831 scalar,
832 error))
833 {
834 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
835 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
836 {
837 if (m_section_load_list.IsEmpty())
838 {
839 // No sections are loaded, so we must assume we are not running
840 // yet and anything we are given is a file address.
841 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
842 }
843 else
844 {
845 // We have at least one section loaded. This can be becuase
846 // we have manually loaded some sections with "target modules load ..."
847 // or because we have have a live process that has sections loaded
848 // through the dynamic loader
849 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
850 }
851 // We weren't able to resolve the pointer value, so just return
852 // an address with no section
853 if (!pointer_addr.IsValid())
854 pointer_addr.SetOffset (pointer_vm_addr);
855 return true;
856
857 }
858 }
859 return false;
860}
Chris Lattner24943d22010-06-08 16:52:24 +0000861
862ModuleSP
863Target::GetSharedModule
864(
865 const FileSpec& file_spec,
866 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000867 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000868 const ConstString *object_name,
869 off_t object_offset,
870 Error *error_ptr
871)
872{
873 // Don't pass in the UUID so we can tell if we have a stale value in our list
874 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
875 bool did_create_module = false;
876 ModuleSP module_sp;
877
Chris Lattner24943d22010-06-08 16:52:24 +0000878 Error error;
879
Greg Clayton24bc5d92011-03-30 18:16:51 +0000880 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +0000881 if (m_image_search_paths.GetSize())
882 {
883 FileSpec transformed_spec;
884 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
885 {
886 transformed_spec.GetFilename() = file_spec.GetFilename();
887 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
888 }
889 }
890
Greg Clayton24bc5d92011-03-30 18:16:51 +0000891 // The platform is responsible for finding and caching an appropriate
892 // module in the shared module cache.
893 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000894 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000895 FileSpec platform_file_spec;
896 error = m_platform_sp->GetSharedModule (file_spec,
897 arch,
898 uuid_ptr,
899 object_name,
900 object_offset,
901 module_sp,
902 &old_module_sp,
903 &did_create_module);
904 }
905 else
906 {
907 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +0000908 }
909
Greg Clayton24bc5d92011-03-30 18:16:51 +0000910 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +0000911 if (module_sp)
912 {
913 m_images.Append (module_sp);
914 if (did_create_module)
915 {
916 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
917 ModuleUpdated(old_module_sp, module_sp);
918 else
919 ModuleAdded(module_sp);
920 }
921 }
922 if (error_ptr)
923 *error_ptr = error;
924 return module_sp;
925}
926
927
928Target *
929Target::CalculateTarget ()
930{
931 return this;
932}
933
934Process *
935Target::CalculateProcess ()
936{
937 return NULL;
938}
939
940Thread *
941Target::CalculateThread ()
942{
943 return NULL;
944}
945
946StackFrame *
947Target::CalculateStackFrame ()
948{
949 return NULL;
950}
951
952void
Greg Claytona830adb2010-10-04 01:05:56 +0000953Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000954{
955 exe_ctx.target = this;
956 exe_ctx.process = NULL; // Do NOT fill in process...
957 exe_ctx.thread = NULL;
958 exe_ctx.frame = NULL;
959}
960
961PathMappingList &
962Target::GetImageSearchPathList ()
963{
964 return m_image_search_paths;
965}
966
967void
968Target::ImageSearchPathsChanged
969(
970 const PathMappingList &path_list,
971 void *baton
972)
973{
974 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +0000975 ModuleSP exe_module_sp (target->GetExecutableModule());
976 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000977 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000978 target->m_images.Clear();
979 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000980 }
981}
982
983ClangASTContext *
984Target::GetScratchClangASTContext()
985{
Greg Clayton34ce4ea2011-08-03 01:23:55 +0000986 // Now see if we know the target triple, and if so, create our scratch AST context:
987 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
988 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000989 return m_scratch_ast_context_ap.get();
990}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000991
Greg Clayton990de7b2010-11-18 23:32:35 +0000992void
Caroline Tice2a456812011-03-10 22:14:10 +0000993Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000994{
Greg Clayton990de7b2010-11-18 23:32:35 +0000995 UserSettingsControllerSP &usc = GetSettingsController();
996 usc.reset (new SettingsController);
997 UserSettingsController::InitializeSettingsController (usc,
998 SettingsController::global_settings_table,
999 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001000
1001 // Now call SettingsInitialize() on each 'child' setting of Target
1002 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001003}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001004
Greg Clayton990de7b2010-11-18 23:32:35 +00001005void
Caroline Tice2a456812011-03-10 22:14:10 +00001006Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001007{
Caroline Tice2a456812011-03-10 22:14:10 +00001008
1009 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1010
1011 Process::SettingsTerminate ();
1012
1013 // Now terminate Target Settings.
1014
Greg Clayton990de7b2010-11-18 23:32:35 +00001015 UserSettingsControllerSP &usc = GetSettingsController();
1016 UserSettingsController::FinalizeSettingsController (usc);
1017 usc.reset();
1018}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001019
Greg Clayton990de7b2010-11-18 23:32:35 +00001020UserSettingsControllerSP &
1021Target::GetSettingsController ()
1022{
1023 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001024 return g_settings_controller;
1025}
1026
1027ArchSpec
1028Target::GetDefaultArchitecture ()
1029{
Greg Clayton940b1032011-02-23 00:35:02 +00001030 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1031
1032 if (settings_controller_sp)
1033 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1034 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001035}
1036
1037void
Greg Clayton940b1032011-02-23 00:35:02 +00001038Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001039{
Greg Clayton940b1032011-02-23 00:35:02 +00001040 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1041
1042 if (settings_controller_sp)
1043 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001044}
1045
Greg Claytona830adb2010-10-04 01:05:56 +00001046Target *
1047Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1048{
1049 // The target can either exist in the "process" of ExecutionContext, or in
1050 // the "target_sp" member of SymbolContext. This accessor helper function
1051 // will get the target from one of these locations.
1052
1053 Target *target = NULL;
1054 if (sc_ptr != NULL)
1055 target = sc_ptr->target_sp.get();
1056 if (target == NULL)
1057 {
1058 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
1059 target = &exe_ctx_ptr->process->GetTarget();
1060 }
1061 return target;
1062}
1063
1064
Caroline Tice1ebef442010-09-27 00:30:10 +00001065void
1066Target::UpdateInstanceName ()
1067{
1068 StreamString sstr;
1069
Greg Clayton5beb99d2011-08-11 02:48:45 +00001070 Module *exe_module = GetExecutableModulePointer();
1071 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001072 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001073 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001074 exe_module->GetFileSpec().GetFilename().AsCString(),
1075 exe_module->GetArchitecture().GetArchitectureName());
1076 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001077 }
1078}
1079
Sean Callanan77e93942010-10-29 00:29:03 +00001080const char *
1081Target::GetExpressionPrefixContentsAsCString ()
1082{
Greg Claytonff44ab42011-04-23 02:04:55 +00001083 if (m_expr_prefix_contents_sp)
1084 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1085 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001086}
1087
Greg Clayton427f2902010-12-14 02:59:59 +00001088ExecutionResults
1089Target::EvaluateExpression
1090(
1091 const char *expr_cstr,
1092 StackFrame *frame,
1093 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001094 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001095 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001096 lldb::ValueObjectSP &result_valobj_sp
1097)
1098{
1099 ExecutionResults execution_results = eExecutionSetupError;
1100
1101 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001102
1103 // We shouldn't run stop hooks in expressions.
1104 // Be sure to reset this if you return anywhere within this function.
1105 bool old_suppress_value = m_suppress_stop_hooks;
1106 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001107
1108 ExecutionContext exe_ctx;
1109 if (frame)
1110 {
1111 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001112 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001113 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001114 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1115 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001116 lldb::VariableSP var_sp;
1117 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1118 use_dynamic,
1119 expr_path_options,
1120 var_sp,
1121 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001122 }
1123 else if (m_process_sp)
1124 {
1125 m_process_sp->CalculateExecutionContext(exe_ctx);
1126 }
1127 else
1128 {
1129 CalculateExecutionContext(exe_ctx);
1130 }
1131
1132 if (result_valobj_sp)
1133 {
1134 execution_results = eExecutionCompleted;
1135 // We got a result from the frame variable expression path above...
1136 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1137
1138 lldb::ValueObjectSP const_valobj_sp;
1139
1140 // Check in case our value is already a constant value
1141 if (result_valobj_sp->GetIsConstant())
1142 {
1143 const_valobj_sp = result_valobj_sp;
1144 const_valobj_sp->SetName (persistent_variable_name);
1145 }
1146 else
Jim Inghame41494a2011-04-16 00:01:13 +00001147 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001148 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001149 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001150 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001151 if (dynamic_sp)
1152 result_valobj_sp = dynamic_sp;
1153 }
1154
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001155 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001156 }
Greg Clayton427f2902010-12-14 02:59:59 +00001157
Sean Callanan6a925532011-01-13 08:53:35 +00001158 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1159
Greg Clayton427f2902010-12-14 02:59:59 +00001160 result_valobj_sp = const_valobj_sp;
1161
Sean Callanan6a925532011-01-13 08:53:35 +00001162 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1163 assert (clang_expr_variable_sp.get());
1164
1165 // Set flags and live data as appropriate
1166
1167 const Value &result_value = live_valobj_sp->GetValue();
1168
1169 switch (result_value.GetValueType())
1170 {
1171 case Value::eValueTypeHostAddress:
1172 case Value::eValueTypeFileAddress:
1173 // we don't do anything with these for now
1174 break;
1175 case Value::eValueTypeScalar:
1176 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1177 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1178 break;
1179 case Value::eValueTypeLoadAddress:
1180 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1181 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1182 break;
1183 }
Greg Clayton427f2902010-12-14 02:59:59 +00001184 }
1185 else
1186 {
1187 // Make sure we aren't just trying to see the value of a persistent
1188 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001189 lldb::ClangExpressionVariableSP persistent_var_sp;
1190 // Only check for persistent variables the expression starts with a '$'
1191 if (expr_cstr[0] == '$')
1192 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1193
Greg Clayton427f2902010-12-14 02:59:59 +00001194 if (persistent_var_sp)
1195 {
1196 result_valobj_sp = persistent_var_sp->GetValueObject ();
1197 execution_results = eExecutionCompleted;
1198 }
1199 else
1200 {
1201 const char *prefix = GetExpressionPrefixContentsAsCString();
1202
1203 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +00001204 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001205 expr_cstr,
1206 prefix,
1207 result_valobj_sp);
1208 }
1209 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001210
1211 m_suppress_stop_hooks = old_suppress_value;
1212
Greg Clayton427f2902010-12-14 02:59:59 +00001213 return execution_results;
1214}
1215
Greg Claytonc0fa5332011-05-22 22:46:53 +00001216lldb::addr_t
1217Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1218{
1219 addr_t code_addr = load_addr;
1220 switch (m_arch.GetMachine())
1221 {
1222 case llvm::Triple::arm:
1223 case llvm::Triple::thumb:
1224 switch (addr_class)
1225 {
1226 case eAddressClassData:
1227 case eAddressClassDebug:
1228 return LLDB_INVALID_ADDRESS;
1229
1230 case eAddressClassUnknown:
1231 case eAddressClassInvalid:
1232 case eAddressClassCode:
1233 case eAddressClassCodeAlternateISA:
1234 case eAddressClassRuntime:
1235 // Check if bit zero it no set?
1236 if ((code_addr & 1ull) == 0)
1237 {
1238 // Bit zero isn't set, check if the address is a multiple of 2?
1239 if (code_addr & 2ull)
1240 {
1241 // The address is a multiple of 2 so it must be thumb, set bit zero
1242 code_addr |= 1ull;
1243 }
1244 else if (addr_class == eAddressClassCodeAlternateISA)
1245 {
1246 // We checked the address and the address claims to be the alternate ISA
1247 // which means thumb, so set bit zero.
1248 code_addr |= 1ull;
1249 }
1250 }
1251 break;
1252 }
1253 break;
1254
1255 default:
1256 break;
1257 }
1258 return code_addr;
1259}
1260
1261lldb::addr_t
1262Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1263{
1264 addr_t opcode_addr = load_addr;
1265 switch (m_arch.GetMachine())
1266 {
1267 case llvm::Triple::arm:
1268 case llvm::Triple::thumb:
1269 switch (addr_class)
1270 {
1271 case eAddressClassData:
1272 case eAddressClassDebug:
1273 return LLDB_INVALID_ADDRESS;
1274
1275 case eAddressClassInvalid:
1276 case eAddressClassUnknown:
1277 case eAddressClassCode:
1278 case eAddressClassCodeAlternateISA:
1279 case eAddressClassRuntime:
1280 opcode_addr &= ~(1ull);
1281 break;
1282 }
1283 break;
1284
1285 default:
1286 break;
1287 }
1288 return opcode_addr;
1289}
1290
Jim Inghamd60d94a2011-03-11 03:53:59 +00001291lldb::user_id_t
1292Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1293{
1294 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1295 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1296 m_stop_hooks[new_uid] = new_hook_sp;
1297 return new_uid;
1298}
1299
1300bool
1301Target::RemoveStopHookByID (lldb::user_id_t user_id)
1302{
1303 size_t num_removed;
1304 num_removed = m_stop_hooks.erase (user_id);
1305 if (num_removed == 0)
1306 return false;
1307 else
1308 return true;
1309}
1310
1311void
1312Target::RemoveAllStopHooks ()
1313{
1314 m_stop_hooks.clear();
1315}
1316
1317Target::StopHookSP
1318Target::GetStopHookByID (lldb::user_id_t user_id)
1319{
1320 StopHookSP found_hook;
1321
1322 StopHookCollection::iterator specified_hook_iter;
1323 specified_hook_iter = m_stop_hooks.find (user_id);
1324 if (specified_hook_iter != m_stop_hooks.end())
1325 found_hook = (*specified_hook_iter).second;
1326 return found_hook;
1327}
1328
1329bool
1330Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1331{
1332 StopHookCollection::iterator specified_hook_iter;
1333 specified_hook_iter = m_stop_hooks.find (user_id);
1334 if (specified_hook_iter == m_stop_hooks.end())
1335 return false;
1336
1337 (*specified_hook_iter).second->SetIsActive (active_state);
1338 return true;
1339}
1340
1341void
1342Target::SetAllStopHooksActiveState (bool active_state)
1343{
1344 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1345 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1346 {
1347 (*pos).second->SetIsActive (active_state);
1348 }
1349}
1350
1351void
1352Target::RunStopHooks ()
1353{
Jim Ingham3613ae12011-05-12 02:06:14 +00001354 if (m_suppress_stop_hooks)
1355 return;
1356
Jim Inghamd60d94a2011-03-11 03:53:59 +00001357 if (!m_process_sp)
1358 return;
1359
1360 if (m_stop_hooks.empty())
1361 return;
1362
1363 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1364
1365 // If there aren't any active stop hooks, don't bother either:
1366 bool any_active_hooks = false;
1367 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1368 {
1369 if ((*pos).second->IsActive())
1370 {
1371 any_active_hooks = true;
1372 break;
1373 }
1374 }
1375 if (!any_active_hooks)
1376 return;
1377
1378 CommandReturnObject result;
1379
1380 std::vector<ExecutionContext> exc_ctx_with_reasons;
1381 std::vector<SymbolContext> sym_ctx_with_reasons;
1382
1383 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1384 size_t num_threads = cur_threadlist.GetSize();
1385 for (size_t i = 0; i < num_threads; i++)
1386 {
1387 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1388 if (cur_thread_sp->ThreadStoppedForAReason())
1389 {
1390 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1391 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1392 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1393 }
1394 }
1395
1396 // If no threads stopped for a reason, don't run the stop-hooks.
1397 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1398 if (num_exe_ctx == 0)
1399 return;
1400
Jim Inghame5ed8e92011-06-02 23:58:26 +00001401 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1402 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001403
1404 bool keep_going = true;
1405 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001406 bool print_hook_header;
1407 bool print_thread_header;
1408
1409 if (num_exe_ctx == 1)
1410 print_thread_header = false;
1411 else
1412 print_thread_header = true;
1413
1414 if (m_stop_hooks.size() == 1)
1415 print_hook_header = false;
1416 else
1417 print_hook_header = true;
1418
Jim Inghamd60d94a2011-03-11 03:53:59 +00001419 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1420 {
1421 // result.Clear();
1422 StopHookSP cur_hook_sp = (*pos).second;
1423 if (!cur_hook_sp->IsActive())
1424 continue;
1425
1426 bool any_thread_matched = false;
1427 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1428 {
1429 if ((cur_hook_sp->GetSpecifier () == NULL
1430 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1431 && (cur_hook_sp->GetThreadSpecifier() == NULL
1432 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1433 {
1434 if (!hooks_ran)
1435 {
Jim Inghamc54840c2011-03-22 01:47:27 +00001436 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001437 hooks_ran = true;
1438 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001439 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001440 {
1441 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1442 any_thread_matched = true;
1443 }
1444
Jim Inghamc54840c2011-03-22 01:47:27 +00001445 if (print_thread_header)
1446 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001447
1448 bool stop_on_continue = true;
1449 bool stop_on_error = true;
1450 bool echo_commands = false;
1451 bool print_results = true;
1452 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001453 &exc_ctx_with_reasons[i],
1454 stop_on_continue,
1455 stop_on_error,
1456 echo_commands,
1457 print_results,
1458 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001459
1460 // If the command started the target going again, we should bag out of
1461 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001462 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1463 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001464 {
1465 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1466 keep_going = false;
1467 }
1468 }
1469 }
1470 }
1471 if (hooks_ran)
1472 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice4a348082011-05-02 20:41:46 +00001473
1474 result.GetImmediateOutputStream()->Flush();
1475 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001476}
1477
Greg Claytonbbea1332011-07-08 00:48:09 +00001478bool
1479Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1480{
1481 bool changed = false;
1482 if (module)
1483 {
1484 ObjectFile *object_file = module->GetObjectFile();
1485 if (object_file)
1486 {
1487 SectionList *section_list = object_file->GetSectionList ();
1488 if (section_list)
1489 {
1490 // All sections listed in the dyld image info structure will all
1491 // either be fixed up already, or they will all be off by a single
1492 // slide amount that is determined by finding the first segment
1493 // that is at file offset zero which also has bytes (a file size
1494 // that is greater than zero) in the object file.
1495
1496 // Determine the slide amount (if any)
1497 const size_t num_sections = section_list->GetSize();
1498 size_t sect_idx = 0;
1499 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1500 {
1501 // Iterate through the object file sections to find the
1502 // first section that starts of file offset zero and that
1503 // has bytes in the file...
1504 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1505 if (section)
1506 {
1507 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1508 changed = true;
1509 }
1510 }
1511 }
1512 }
1513 }
1514 return changed;
1515}
1516
1517
Jim Inghamd60d94a2011-03-11 03:53:59 +00001518//--------------------------------------------------------------
1519// class Target::StopHook
1520//--------------------------------------------------------------
1521
1522
1523Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1524 UserID (uid),
1525 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001526 m_commands (),
1527 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001528 m_thread_spec_ap(NULL),
1529 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001530{
1531}
1532
1533Target::StopHook::StopHook (const StopHook &rhs) :
1534 UserID (rhs.GetID()),
1535 m_target_sp (rhs.m_target_sp),
1536 m_commands (rhs.m_commands),
1537 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001538 m_thread_spec_ap (NULL),
1539 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001540{
1541 if (rhs.m_thread_spec_ap.get() != NULL)
1542 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1543}
1544
1545
1546Target::StopHook::~StopHook ()
1547{
1548}
1549
1550void
1551Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1552{
1553 m_thread_spec_ap.reset (specifier);
1554}
1555
1556
1557void
1558Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1559{
1560 int indent_level = s->GetIndentLevel();
1561
1562 s->SetIndentLevel(indent_level + 2);
1563
1564 s->Printf ("Hook: %d\n", GetID());
1565 if (m_active)
1566 s->Indent ("State: enabled\n");
1567 else
1568 s->Indent ("State: disabled\n");
1569
1570 if (m_specifier_sp)
1571 {
1572 s->Indent();
1573 s->PutCString ("Specifier:\n");
1574 s->SetIndentLevel (indent_level + 4);
1575 m_specifier_sp->GetDescription (s, level);
1576 s->SetIndentLevel (indent_level + 2);
1577 }
1578
1579 if (m_thread_spec_ap.get() != NULL)
1580 {
1581 StreamString tmp;
1582 s->Indent("Thread:\n");
1583 m_thread_spec_ap->GetDescription (&tmp, level);
1584 s->SetIndentLevel (indent_level + 4);
1585 s->Indent (tmp.GetData());
1586 s->PutCString ("\n");
1587 s->SetIndentLevel (indent_level + 2);
1588 }
1589
1590 s->Indent ("Commands: \n");
1591 s->SetIndentLevel (indent_level + 4);
1592 uint32_t num_commands = m_commands.GetSize();
1593 for (uint32_t i = 0; i < num_commands; i++)
1594 {
1595 s->Indent(m_commands.GetStringAtIndex(i));
1596 s->PutCString ("\n");
1597 }
1598 s->SetIndentLevel (indent_level);
1599}
1600
1601
Caroline Tice5bc8c972010-09-20 20:44:43 +00001602//--------------------------------------------------------------
1603// class Target::SettingsController
1604//--------------------------------------------------------------
1605
1606Target::SettingsController::SettingsController () :
1607 UserSettingsController ("target", Debugger::GetSettingsController()),
1608 m_default_architecture ()
1609{
1610 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1611 InstanceSettings::GetDefaultName().AsCString()));
1612}
1613
1614Target::SettingsController::~SettingsController ()
1615{
1616}
1617
1618lldb::InstanceSettingsSP
1619Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1620{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001621 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1622 false,
1623 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001624 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1625 return new_settings_sp;
1626}
1627
Caroline Tice5bc8c972010-09-20 20:44:43 +00001628
Jim Inghame41494a2011-04-16 00:01:13 +00001629#define TSC_DEFAULT_ARCH "default-arch"
1630#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001631#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001632#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001633#define TSC_SOURCE_MAP "source-map"
Enrico Granata018921d2011-08-12 02:00:06 +00001634#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata91544802011-09-06 19:20:51 +00001635#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytond284b662011-02-18 01:44:25 +00001636
1637
1638static const ConstString &
1639GetSettingNameForDefaultArch ()
1640{
1641 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001642 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001643}
1644
Greg Claytond284b662011-02-18 01:44:25 +00001645static const ConstString &
1646GetSettingNameForExpressionPrefix ()
1647{
1648 static ConstString g_const_string (TSC_EXPR_PREFIX);
1649 return g_const_string;
1650}
1651
1652static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001653GetSettingNameForPreferDynamicValue ()
1654{
1655 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1656 return g_const_string;
1657}
1658
Greg Claytonff44ab42011-04-23 02:04:55 +00001659static const ConstString &
1660GetSettingNameForSourcePathMap ()
1661{
1662 static ConstString g_const_string (TSC_SOURCE_MAP);
1663 return g_const_string;
1664}
Greg Claytond284b662011-02-18 01:44:25 +00001665
Greg Clayton17cd9952011-04-22 03:55:06 +00001666static const ConstString &
1667GetSettingNameForSkipPrologue ()
1668{
1669 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1670 return g_const_string;
1671}
1672
Enrico Granata018921d2011-08-12 02:00:06 +00001673static const ConstString &
1674GetSettingNameForMaxChildren ()
1675{
1676 static ConstString g_const_string (TSC_MAX_CHILDREN);
1677 return g_const_string;
1678}
Greg Clayton17cd9952011-04-22 03:55:06 +00001679
Enrico Granata91544802011-09-06 19:20:51 +00001680static const ConstString &
1681GetSettingNameForMaxStringSummaryLength ()
1682{
1683 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1684 return g_const_string;
1685}
Greg Clayton17cd9952011-04-22 03:55:06 +00001686
Caroline Tice5bc8c972010-09-20 20:44:43 +00001687bool
1688Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1689 const char *index_value,
1690 const char *value,
1691 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001692 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001693 Error&err)
1694{
Greg Claytond284b662011-02-18 01:44:25 +00001695 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001696 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001697 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001698 if (!m_default_architecture.IsValid())
1699 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001700 }
1701 return true;
1702}
1703
1704
1705bool
1706Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1707 StringList &value,
1708 Error &err)
1709{
Greg Claytond284b662011-02-18 01:44:25 +00001710 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001711 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001712 // If the arch is invalid (the default), don't show a string for it
1713 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001714 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001715 return true;
1716 }
1717 else
1718 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1719
1720 return false;
1721}
1722
1723//--------------------------------------------------------------
1724// class TargetInstanceSettings
1725//--------------------------------------------------------------
1726
Greg Clayton638351a2010-12-04 00:10:17 +00001727TargetInstanceSettings::TargetInstanceSettings
1728(
1729 UserSettingsController &owner,
1730 bool live_instance,
1731 const char *name
1732) :
Greg Claytond284b662011-02-18 01:44:25 +00001733 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001734 m_expr_prefix_file (),
1735 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001736 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001737 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00001738 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00001739 m_max_children_display(256),
1740 m_max_strlen_length(1024)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001741{
1742 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1743 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1744 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1745 // This is true for CreateInstanceName() too.
1746
1747 if (GetInstanceName () == InstanceSettings::InvalidName())
1748 {
1749 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1750 m_owner.RegisterInstanceSettings (this);
1751 }
1752
1753 if (live_instance)
1754 {
1755 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1756 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001757 }
1758}
1759
1760TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001761 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1762 m_expr_prefix_file (rhs.m_expr_prefix_file),
1763 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1764 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1765 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00001766 m_source_map (rhs.m_source_map),
Enrico Granata91544802011-09-06 19:20:51 +00001767 m_max_children_display(rhs.m_max_children_display),
1768 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001769{
1770 if (m_instance_name != InstanceSettings::GetDefaultName())
1771 {
1772 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1773 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001774 }
1775}
1776
1777TargetInstanceSettings::~TargetInstanceSettings ()
1778{
1779}
1780
1781TargetInstanceSettings&
1782TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1783{
1784 if (this != &rhs)
1785 {
1786 }
1787
1788 return *this;
1789}
1790
Caroline Tice5bc8c972010-09-20 20:44:43 +00001791void
1792TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1793 const char *index_value,
1794 const char *value,
1795 const ConstString &instance_name,
1796 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001797 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001798 Error &err,
1799 bool pending)
1800{
Greg Claytond284b662011-02-18 01:44:25 +00001801 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001802 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001803 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1804 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00001805 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001806 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00001807 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001808 default:
1809 break;
1810 case eVarSetOperationAssign:
1811 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00001812 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001813 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1814 {
1815 err.SetErrorToGenericError ();
1816 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1817 return;
1818 }
1819
1820 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1821
1822 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1823 {
1824 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1825 m_expr_prefix_contents_sp.reset();
1826 }
Sean Callanan77e93942010-10-29 00:29:03 +00001827 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001828 break;
1829 case eVarSetOperationClear:
1830 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00001831 }
Sean Callanan77e93942010-10-29 00:29:03 +00001832 }
1833 }
Jim Inghame41494a2011-04-16 00:01:13 +00001834 else if (var_name == GetSettingNameForPreferDynamicValue())
1835 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001836 int new_value;
1837 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1838 if (err.Success())
1839 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00001840 }
1841 else if (var_name == GetSettingNameForSkipPrologue())
1842 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001843 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1844 }
Enrico Granata018921d2011-08-12 02:00:06 +00001845 else if (var_name == GetSettingNameForMaxChildren())
1846 {
1847 bool ok;
1848 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1849 if (ok)
1850 m_max_children_display = new_value;
1851 }
Enrico Granata91544802011-09-06 19:20:51 +00001852 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1853 {
1854 bool ok;
1855 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1856 if (ok)
1857 m_max_strlen_length = new_value;
1858 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001859 else if (var_name == GetSettingNameForSourcePathMap ())
1860 {
1861 switch (op)
1862 {
1863 case eVarSetOperationReplace:
1864 case eVarSetOperationInsertBefore:
1865 case eVarSetOperationInsertAfter:
1866 case eVarSetOperationRemove:
1867 default:
1868 break;
1869 case eVarSetOperationAssign:
1870 m_source_map.Clear(true);
1871 // Fall through to append....
1872 case eVarSetOperationAppend:
1873 {
1874 Args args(value);
1875 const uint32_t argc = args.GetArgumentCount();
1876 if (argc & 1 || argc == 0)
1877 {
1878 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1879 }
1880 else
1881 {
1882 char resolved_new_path[PATH_MAX];
1883 FileSpec file_spec;
1884 const char *old_path;
1885 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1886 {
1887 const char *new_path = args.GetArgumentAtIndex(idx+1);
1888 assert (new_path); // We have an even number of paths, this shouldn't happen!
1889
1890 file_spec.SetFile(new_path, true);
1891 if (file_spec.Exists())
1892 {
1893 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1894 {
1895 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1896 return;
1897 }
1898 }
1899 else
1900 {
1901 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1902 return;
1903 }
1904 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1905 }
1906 }
1907 }
1908 break;
1909
1910 case eVarSetOperationClear:
1911 m_source_map.Clear(true);
1912 break;
1913 }
Jim Inghame41494a2011-04-16 00:01:13 +00001914 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001915}
1916
1917void
Greg Claytond284b662011-02-18 01:44:25 +00001918TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001919{
Sean Callanan77e93942010-10-29 00:29:03 +00001920 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1921
1922 if (!new_settings_ptr)
1923 return;
1924
Greg Claytonff44ab42011-04-23 02:04:55 +00001925 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1926 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1927 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1928 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata018921d2011-08-12 02:00:06 +00001929 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata91544802011-09-06 19:20:51 +00001930 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001931}
1932
Caroline Ticebcb5b452010-09-20 21:37:42 +00001933bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001934TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1935 const ConstString &var_name,
1936 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001937 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001938{
Greg Claytond284b662011-02-18 01:44:25 +00001939 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001940 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001941 char path[PATH_MAX];
1942 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1943 if (path_len > 0)
1944 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00001945 }
Jim Inghame41494a2011-04-16 00:01:13 +00001946 else if (var_name == GetSettingNameForPreferDynamicValue())
1947 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001948 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00001949 }
Greg Clayton17cd9952011-04-22 03:55:06 +00001950 else if (var_name == GetSettingNameForSkipPrologue())
1951 {
1952 if (m_skip_prologue)
1953 value.AppendString ("true");
1954 else
1955 value.AppendString ("false");
1956 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001957 else if (var_name == GetSettingNameForSourcePathMap ())
1958 {
1959 }
Enrico Granata018921d2011-08-12 02:00:06 +00001960 else if (var_name == GetSettingNameForMaxChildren())
1961 {
1962 StreamString count_str;
1963 count_str.Printf ("%d", m_max_children_display);
1964 value.AppendString (count_str.GetData());
1965 }
Enrico Granata91544802011-09-06 19:20:51 +00001966 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1967 {
1968 StreamString count_str;
1969 count_str.Printf ("%d", m_max_strlen_length);
1970 value.AppendString (count_str.GetData());
1971 }
Sean Callanan77e93942010-10-29 00:29:03 +00001972 else
1973 {
1974 if (err)
1975 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1976 return false;
1977 }
1978
1979 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001980}
1981
1982const ConstString
1983TargetInstanceSettings::CreateInstanceName ()
1984{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001985 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001986 static int instance_count = 1;
1987
Caroline Tice5bc8c972010-09-20 20:44:43 +00001988 sstr.Printf ("target_%d", instance_count);
1989 ++instance_count;
1990
1991 const ConstString ret_val (sstr.GetData());
1992 return ret_val;
1993}
1994
1995//--------------------------------------------------
1996// Target::SettingsController Variable Tables
1997//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00001998OptionEnumValueElement
1999TargetInstanceSettings::g_dynamic_value_types[] =
2000{
Greg Clayton577fbc32011-05-30 00:39:48 +00002001{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2002{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2003{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002004{ 0, NULL, NULL }
2005};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002006
2007SettingEntry
2008Target::SettingsController::global_settings_table[] =
2009{
Greg Claytond284b662011-02-18 01:44:25 +00002010 // var-name var-type default enum init'd hidden help-text
2011 // ================= ================== =========== ==== ====== ====== =========================================================================
2012 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2013 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2014};
2015
Caroline Tice5bc8c972010-09-20 20:44:43 +00002016SettingEntry
2017Target::SettingsController::instance_settings_table[] =
2018{
Enrico Granata91544802011-09-06 19:20:51 +00002019 // var-name var-type default enum init'd hidden help-text
2020 // ================= ================== =============== ======================= ====== ====== =========================================================================
2021 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2022 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2023 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2024 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2025 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2026 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2027 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002028};