blob: ac50ef1cd673e4ade29081262d26dd6686a1e81b [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;
336 if (addr == LLDB_INVALID_ADDRESS || size == 0 || GetProcessSP())
337 return wp_loc_sp;
338}
339
Chris Lattner24943d22010-06-08 16:52:24 +0000340void
341Target::RemoveAllBreakpoints (bool internal_also)
342{
Greg Claytone005f2c2010-11-06 01:53:30 +0000343 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000344 if (log)
345 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
346
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000347 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000348 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000349 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000350
351 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000352}
353
354void
355Target::DisableAllBreakpoints (bool internal_also)
356{
Greg Claytone005f2c2010-11-06 01:53:30 +0000357 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000358 if (log)
359 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
360
361 m_breakpoint_list.SetEnabledAll (false);
362 if (internal_also)
363 m_internal_breakpoint_list.SetEnabledAll (false);
364}
365
366void
367Target::EnableAllBreakpoints (bool internal_also)
368{
Greg Claytone005f2c2010-11-06 01:53:30 +0000369 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000370 if (log)
371 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
372
373 m_breakpoint_list.SetEnabledAll (true);
374 if (internal_also)
375 m_internal_breakpoint_list.SetEnabledAll (true);
376}
377
378bool
379Target::RemoveBreakpointByID (break_id_t break_id)
380{
Greg Claytone005f2c2010-11-06 01:53:30 +0000381 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000382 if (log)
383 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
384
385 if (DisableBreakpointByID (break_id))
386 {
387 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000388 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000389 else
Jim Inghamd1686902010-10-14 23:45:03 +0000390 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000391 if (m_last_created_breakpoint)
392 {
393 if (m_last_created_breakpoint->GetID() == break_id)
394 m_last_created_breakpoint.reset();
395 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000396 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000397 }
Chris Lattner24943d22010-06-08 16:52:24 +0000398 return true;
399 }
400 return false;
401}
402
403bool
404Target::DisableBreakpointByID (break_id_t break_id)
405{
Greg Claytone005f2c2010-11-06 01:53:30 +0000406 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000407 if (log)
408 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
409
410 BreakpointSP bp_sp;
411
412 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
413 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
414 else
415 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
416 if (bp_sp)
417 {
418 bp_sp->SetEnabled (false);
419 return true;
420 }
421 return false;
422}
423
424bool
425Target::EnableBreakpointByID (break_id_t break_id)
426{
Greg Claytone005f2c2010-11-06 01:53:30 +0000427 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000428 if (log)
429 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
430 __FUNCTION__,
431 break_id,
432 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
433
434 BreakpointSP bp_sp;
435
436 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
437 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
438 else
439 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
440
441 if (bp_sp)
442 {
443 bp_sp->SetEnabled (true);
444 return true;
445 }
446 return false;
447}
448
449ModuleSP
450Target::GetExecutableModule ()
451{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000452 return m_images.GetModuleAtIndex(0);
453}
454
455Module*
456Target::GetExecutableModulePointer ()
457{
458 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000459}
460
461void
462Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
463{
464 m_images.Clear();
465 m_scratch_ast_context_ap.reset();
466
467 if (executable_sp.get())
468 {
469 Timer scoped_timer (__PRETTY_FUNCTION__,
470 "Target::SetExecutableModule (executable = '%s/%s')",
471 executable_sp->GetFileSpec().GetDirectory().AsCString(),
472 executable_sp->GetFileSpec().GetFilename().AsCString());
473
474 m_images.Append(executable_sp); // The first image is our exectuable file
475
Jim Ingham7508e732010-08-09 23:31:02 +0000476 // 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 +0000477 if (!m_arch.IsValid())
478 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000479
Chris Lattner24943d22010-06-08 16:52:24 +0000480 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000481 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000482 // Let's find the file & line for main and set the default source file from there.
483 if (!m_source_manager.DefaultFileAndLineSet())
484 {
485 SymbolContextList sc_list;
486 uint32_t num_matches;
487 ConstString main_name("main");
488 bool symbols_okay = false; // Force it to be a debug symbol.
489 bool append = false;
490 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
491 for (uint32_t idx = 0; idx < num_matches; idx++)
492 {
493 SymbolContext sc;
494 sc_list.GetContextAtIndex(idx, sc);
495 if (sc.line_entry.file)
496 {
497 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
498 break;
499 }
500 }
501 }
Chris Lattner24943d22010-06-08 16:52:24 +0000502
503 if (executable_objfile)
504 {
505 executable_objfile->GetDependentModules(dependent_files);
506 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
507 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000508 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
509 FileSpec platform_dependent_file_spec;
510 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000511 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000512 else
513 platform_dependent_file_spec = dependent_file_spec;
514
515 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000516 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000517 if (image_module_sp.get())
518 {
Chris Lattner24943d22010-06-08 16:52:24 +0000519 ObjectFile *objfile = image_module_sp->GetObjectFile();
520 if (objfile)
521 objfile->GetDependentModules(dependent_files);
522 }
523 }
524 }
525
Chris Lattner24943d22010-06-08 16:52:24 +0000526 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000527
528 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000529}
530
531
Jim Ingham7508e732010-08-09 23:31:02 +0000532bool
533Target::SetArchitecture (const ArchSpec &arch_spec)
534{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000535 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000536 {
537 // If we're setting the architecture to our current architecture, we
538 // don't need to do anything.
539 return true;
540 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000541 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000542 {
543 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000544 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000545 return true;
546 }
547 else
548 {
549 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000550 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000551 ModuleSP executable_sp = GetExecutableModule ();
552 m_images.Clear();
553 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000554 // Need to do something about unsetting breakpoints.
555
556 if (executable_sp)
557 {
558 FileSpec exec_file_spec = executable_sp->GetFileSpec();
559 Error error = ModuleList::GetSharedModule(exec_file_spec,
560 arch_spec,
561 NULL,
562 NULL,
563 0,
564 executable_sp,
565 NULL,
566 NULL);
567
568 if (!error.Fail() && executable_sp)
569 {
570 SetExecutableModule (executable_sp, true);
571 return true;
572 }
573 else
574 {
575 return false;
576 }
577 }
578 else
579 {
580 return false;
581 }
582 }
583}
Chris Lattner24943d22010-06-08 16:52:24 +0000584
Chris Lattner24943d22010-06-08 16:52:24 +0000585void
586Target::ModuleAdded (ModuleSP &module_sp)
587{
588 // A module is being added to this target for the first time
589 ModuleList module_list;
590 module_list.Append(module_sp);
591 ModulesDidLoad (module_list);
592}
593
594void
595Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
596{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000597 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000598 ModuleList module_list;
599 module_list.Append (old_module_sp);
600 ModulesDidUnload (module_list);
601 module_list.Clear ();
602 module_list.Append (new_module_sp);
603 ModulesDidLoad (module_list);
604}
605
606void
607Target::ModulesDidLoad (ModuleList &module_list)
608{
609 m_breakpoint_list.UpdateBreakpoints (module_list, true);
610 // TODO: make event data that packages up the module_list
611 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
612}
613
614void
615Target::ModulesDidUnload (ModuleList &module_list)
616{
617 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000618
619 // Remove the images from the target image list
620 m_images.Remove(module_list);
621
Chris Lattner24943d22010-06-08 16:52:24 +0000622 // TODO: make event data that packages up the module_list
623 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
624}
625
626size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000627Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
628{
629 const Section *section = addr.GetSection();
630 if (section && section->GetModule())
631 {
632 ObjectFile *objfile = section->GetModule()->GetObjectFile();
633 if (objfile)
634 {
635 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
636 addr.GetOffset(),
637 dst,
638 dst_len);
639 if (bytes_read > 0)
640 return bytes_read;
641 else
642 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
643 }
644 else
645 {
646 error.SetErrorString("address isn't from a object file");
647 }
648 }
649 else
650 {
651 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
652 }
653 return 0;
654}
655
656size_t
Enrico Granata91544802011-09-06 19:20:51 +0000657Target::ReadMemory (const Address& addr,
658 bool prefer_file_cache,
659 void *dst,
660 size_t dst_len,
661 Error &error,
662 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000663{
Chris Lattner24943d22010-06-08 16:52:24 +0000664 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000665
Enrico Granata91544802011-09-06 19:20:51 +0000666 // if we end up reading this from process memory, we will fill this
667 // with the actual load address
668 if (load_addr_ptr)
669 *load_addr_ptr = LLDB_INVALID_ADDRESS;
670
Greg Clayton70436352010-06-30 23:03:03 +0000671 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
672
Greg Clayton26100dc2011-01-07 01:57:07 +0000673 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000674
675 addr_t load_addr = LLDB_INVALID_ADDRESS;
676 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000677 Address resolved_addr;
678 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000679 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000680 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000681 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000682 // No sections are loaded, so we must assume we are not running
683 // yet and anything we are given is a file address.
684 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
685 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000686 }
Greg Clayton70436352010-06-30 23:03:03 +0000687 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000688 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000689 // We have at least one section loaded. This can be becuase
690 // we have manually loaded some sections with "target modules load ..."
691 // or because we have have a live process that has sections loaded
692 // through the dynamic loader
693 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
694 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000695 }
Greg Clayton70436352010-06-30 23:03:03 +0000696 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000697 if (!resolved_addr.IsValid())
698 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000699
Greg Clayton9b82f862011-07-11 05:12:02 +0000700
Greg Clayton26100dc2011-01-07 01:57:07 +0000701 if (prefer_file_cache)
702 {
703 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
704 if (bytes_read > 0)
705 return bytes_read;
706 }
Greg Clayton70436352010-06-30 23:03:03 +0000707
708 if (process_is_valid)
709 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000710 if (load_addr == LLDB_INVALID_ADDRESS)
711 load_addr = resolved_addr.GetLoadAddress (this);
712
Greg Clayton70436352010-06-30 23:03:03 +0000713 if (load_addr == LLDB_INVALID_ADDRESS)
714 {
715 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
716 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
717 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
718 resolved_addr.GetFileAddress());
719 else
720 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
721 }
722 else
723 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000724 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000725 if (bytes_read != dst_len)
726 {
727 if (error.Success())
728 {
729 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000730 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000731 else
Greg Clayton70436352010-06-30 23:03:03 +0000732 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 +0000733 }
734 }
Greg Clayton70436352010-06-30 23:03:03 +0000735 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +0000736 {
737 if (load_addr_ptr)
738 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +0000739 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +0000740 }
Greg Clayton70436352010-06-30 23:03:03 +0000741 // If the address is not section offset we have an address that
742 // doesn't resolve to any address in any currently loaded shared
743 // libaries and we failed to read memory so there isn't anything
744 // more we can do. If it is section offset, we might be able to
745 // read cached memory from the object file.
746 if (!resolved_addr.IsSectionOffset())
747 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000748 }
Chris Lattner24943d22010-06-08 16:52:24 +0000749 }
Greg Clayton70436352010-06-30 23:03:03 +0000750
Greg Clayton9b82f862011-07-11 05:12:02 +0000751 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000752 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000753 // If we didn't already try and read from the object file cache, then
754 // try it after failing to read from the process.
755 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000756 }
757 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000758}
759
Greg Clayton7dd98df2011-07-12 17:06:17 +0000760size_t
761Target::ReadScalarIntegerFromMemory (const Address& addr,
762 bool prefer_file_cache,
763 uint32_t byte_size,
764 bool is_signed,
765 Scalar &scalar,
766 Error &error)
767{
768 uint64_t uval;
769
770 if (byte_size <= sizeof(uval))
771 {
772 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
773 if (bytes_read == byte_size)
774 {
775 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
776 uint32_t offset = 0;
777 if (byte_size <= 4)
778 scalar = data.GetMaxU32 (&offset, byte_size);
779 else
780 scalar = data.GetMaxU64 (&offset, byte_size);
781
782 if (is_signed)
783 scalar.SignExtend(byte_size * 8);
784 return bytes_read;
785 }
786 }
787 else
788 {
789 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
790 }
791 return 0;
792}
793
794uint64_t
795Target::ReadUnsignedIntegerFromMemory (const Address& addr,
796 bool prefer_file_cache,
797 size_t integer_byte_size,
798 uint64_t fail_value,
799 Error &error)
800{
801 Scalar scalar;
802 if (ReadScalarIntegerFromMemory (addr,
803 prefer_file_cache,
804 integer_byte_size,
805 false,
806 scalar,
807 error))
808 return scalar.ULongLong(fail_value);
809 return fail_value;
810}
811
812bool
813Target::ReadPointerFromMemory (const Address& addr,
814 bool prefer_file_cache,
815 Error &error,
816 Address &pointer_addr)
817{
818 Scalar scalar;
819 if (ReadScalarIntegerFromMemory (addr,
820 prefer_file_cache,
821 m_arch.GetAddressByteSize(),
822 false,
823 scalar,
824 error))
825 {
826 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
827 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
828 {
829 if (m_section_load_list.IsEmpty())
830 {
831 // No sections are loaded, so we must assume we are not running
832 // yet and anything we are given is a file address.
833 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
834 }
835 else
836 {
837 // We have at least one section loaded. This can be becuase
838 // we have manually loaded some sections with "target modules load ..."
839 // or because we have have a live process that has sections loaded
840 // through the dynamic loader
841 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
842 }
843 // We weren't able to resolve the pointer value, so just return
844 // an address with no section
845 if (!pointer_addr.IsValid())
846 pointer_addr.SetOffset (pointer_vm_addr);
847 return true;
848
849 }
850 }
851 return false;
852}
Chris Lattner24943d22010-06-08 16:52:24 +0000853
854ModuleSP
855Target::GetSharedModule
856(
857 const FileSpec& file_spec,
858 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000859 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000860 const ConstString *object_name,
861 off_t object_offset,
862 Error *error_ptr
863)
864{
865 // Don't pass in the UUID so we can tell if we have a stale value in our list
866 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
867 bool did_create_module = false;
868 ModuleSP module_sp;
869
Chris Lattner24943d22010-06-08 16:52:24 +0000870 Error error;
871
Greg Clayton24bc5d92011-03-30 18:16:51 +0000872 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +0000873 if (m_image_search_paths.GetSize())
874 {
875 FileSpec transformed_spec;
876 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
877 {
878 transformed_spec.GetFilename() = file_spec.GetFilename();
879 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
880 }
881 }
882
Greg Clayton24bc5d92011-03-30 18:16:51 +0000883 // The platform is responsible for finding and caching an appropriate
884 // module in the shared module cache.
885 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000886 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000887 FileSpec platform_file_spec;
888 error = m_platform_sp->GetSharedModule (file_spec,
889 arch,
890 uuid_ptr,
891 object_name,
892 object_offset,
893 module_sp,
894 &old_module_sp,
895 &did_create_module);
896 }
897 else
898 {
899 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +0000900 }
901
Greg Clayton24bc5d92011-03-30 18:16:51 +0000902 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +0000903 if (module_sp)
904 {
905 m_images.Append (module_sp);
906 if (did_create_module)
907 {
908 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
909 ModuleUpdated(old_module_sp, module_sp);
910 else
911 ModuleAdded(module_sp);
912 }
913 }
914 if (error_ptr)
915 *error_ptr = error;
916 return module_sp;
917}
918
919
920Target *
921Target::CalculateTarget ()
922{
923 return this;
924}
925
926Process *
927Target::CalculateProcess ()
928{
929 return NULL;
930}
931
932Thread *
933Target::CalculateThread ()
934{
935 return NULL;
936}
937
938StackFrame *
939Target::CalculateStackFrame ()
940{
941 return NULL;
942}
943
944void
Greg Claytona830adb2010-10-04 01:05:56 +0000945Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000946{
947 exe_ctx.target = this;
948 exe_ctx.process = NULL; // Do NOT fill in process...
949 exe_ctx.thread = NULL;
950 exe_ctx.frame = NULL;
951}
952
953PathMappingList &
954Target::GetImageSearchPathList ()
955{
956 return m_image_search_paths;
957}
958
959void
960Target::ImageSearchPathsChanged
961(
962 const PathMappingList &path_list,
963 void *baton
964)
965{
966 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +0000967 ModuleSP exe_module_sp (target->GetExecutableModule());
968 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000969 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000970 target->m_images.Clear();
971 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000972 }
973}
974
975ClangASTContext *
976Target::GetScratchClangASTContext()
977{
Greg Clayton34ce4ea2011-08-03 01:23:55 +0000978 // Now see if we know the target triple, and if so, create our scratch AST context:
979 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
980 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000981 return m_scratch_ast_context_ap.get();
982}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000983
Greg Clayton990de7b2010-11-18 23:32:35 +0000984void
Caroline Tice2a456812011-03-10 22:14:10 +0000985Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000986{
Greg Clayton990de7b2010-11-18 23:32:35 +0000987 UserSettingsControllerSP &usc = GetSettingsController();
988 usc.reset (new SettingsController);
989 UserSettingsController::InitializeSettingsController (usc,
990 SettingsController::global_settings_table,
991 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000992
993 // Now call SettingsInitialize() on each 'child' setting of Target
994 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000995}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000996
Greg Clayton990de7b2010-11-18 23:32:35 +0000997void
Caroline Tice2a456812011-03-10 22:14:10 +0000998Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000999{
Caroline Tice2a456812011-03-10 22:14:10 +00001000
1001 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1002
1003 Process::SettingsTerminate ();
1004
1005 // Now terminate Target Settings.
1006
Greg Clayton990de7b2010-11-18 23:32:35 +00001007 UserSettingsControllerSP &usc = GetSettingsController();
1008 UserSettingsController::FinalizeSettingsController (usc);
1009 usc.reset();
1010}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001011
Greg Clayton990de7b2010-11-18 23:32:35 +00001012UserSettingsControllerSP &
1013Target::GetSettingsController ()
1014{
1015 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001016 return g_settings_controller;
1017}
1018
1019ArchSpec
1020Target::GetDefaultArchitecture ()
1021{
Greg Clayton940b1032011-02-23 00:35:02 +00001022 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1023
1024 if (settings_controller_sp)
1025 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1026 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001027}
1028
1029void
Greg Clayton940b1032011-02-23 00:35:02 +00001030Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001031{
Greg Clayton940b1032011-02-23 00:35:02 +00001032 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1033
1034 if (settings_controller_sp)
1035 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001036}
1037
Greg Claytona830adb2010-10-04 01:05:56 +00001038Target *
1039Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1040{
1041 // The target can either exist in the "process" of ExecutionContext, or in
1042 // the "target_sp" member of SymbolContext. This accessor helper function
1043 // will get the target from one of these locations.
1044
1045 Target *target = NULL;
1046 if (sc_ptr != NULL)
1047 target = sc_ptr->target_sp.get();
1048 if (target == NULL)
1049 {
1050 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
1051 target = &exe_ctx_ptr->process->GetTarget();
1052 }
1053 return target;
1054}
1055
1056
Caroline Tice1ebef442010-09-27 00:30:10 +00001057void
1058Target::UpdateInstanceName ()
1059{
1060 StreamString sstr;
1061
Greg Clayton5beb99d2011-08-11 02:48:45 +00001062 Module *exe_module = GetExecutableModulePointer();
1063 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001064 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001065 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001066 exe_module->GetFileSpec().GetFilename().AsCString(),
1067 exe_module->GetArchitecture().GetArchitectureName());
1068 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001069 }
1070}
1071
Sean Callanan77e93942010-10-29 00:29:03 +00001072const char *
1073Target::GetExpressionPrefixContentsAsCString ()
1074{
Greg Claytonff44ab42011-04-23 02:04:55 +00001075 if (m_expr_prefix_contents_sp)
1076 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1077 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001078}
1079
Greg Clayton427f2902010-12-14 02:59:59 +00001080ExecutionResults
1081Target::EvaluateExpression
1082(
1083 const char *expr_cstr,
1084 StackFrame *frame,
1085 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001086 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001087 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001088 lldb::ValueObjectSP &result_valobj_sp
1089)
1090{
1091 ExecutionResults execution_results = eExecutionSetupError;
1092
1093 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001094
1095 // We shouldn't run stop hooks in expressions.
1096 // Be sure to reset this if you return anywhere within this function.
1097 bool old_suppress_value = m_suppress_stop_hooks;
1098 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001099
1100 ExecutionContext exe_ctx;
1101 if (frame)
1102 {
1103 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001104 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001105 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001106 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1107 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001108 lldb::VariableSP var_sp;
1109 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1110 use_dynamic,
1111 expr_path_options,
1112 var_sp,
1113 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001114 }
1115 else if (m_process_sp)
1116 {
1117 m_process_sp->CalculateExecutionContext(exe_ctx);
1118 }
1119 else
1120 {
1121 CalculateExecutionContext(exe_ctx);
1122 }
1123
1124 if (result_valobj_sp)
1125 {
1126 execution_results = eExecutionCompleted;
1127 // We got a result from the frame variable expression path above...
1128 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1129
1130 lldb::ValueObjectSP const_valobj_sp;
1131
1132 // Check in case our value is already a constant value
1133 if (result_valobj_sp->GetIsConstant())
1134 {
1135 const_valobj_sp = result_valobj_sp;
1136 const_valobj_sp->SetName (persistent_variable_name);
1137 }
1138 else
Jim Inghame41494a2011-04-16 00:01:13 +00001139 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001140 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001141 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001142 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001143 if (dynamic_sp)
1144 result_valobj_sp = dynamic_sp;
1145 }
1146
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001147 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001148 }
Greg Clayton427f2902010-12-14 02:59:59 +00001149
Sean Callanan6a925532011-01-13 08:53:35 +00001150 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1151
Greg Clayton427f2902010-12-14 02:59:59 +00001152 result_valobj_sp = const_valobj_sp;
1153
Sean Callanan6a925532011-01-13 08:53:35 +00001154 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1155 assert (clang_expr_variable_sp.get());
1156
1157 // Set flags and live data as appropriate
1158
1159 const Value &result_value = live_valobj_sp->GetValue();
1160
1161 switch (result_value.GetValueType())
1162 {
1163 case Value::eValueTypeHostAddress:
1164 case Value::eValueTypeFileAddress:
1165 // we don't do anything with these for now
1166 break;
1167 case Value::eValueTypeScalar:
1168 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1169 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1170 break;
1171 case Value::eValueTypeLoadAddress:
1172 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1173 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1174 break;
1175 }
Greg Clayton427f2902010-12-14 02:59:59 +00001176 }
1177 else
1178 {
1179 // Make sure we aren't just trying to see the value of a persistent
1180 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001181 lldb::ClangExpressionVariableSP persistent_var_sp;
1182 // Only check for persistent variables the expression starts with a '$'
1183 if (expr_cstr[0] == '$')
1184 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1185
Greg Clayton427f2902010-12-14 02:59:59 +00001186 if (persistent_var_sp)
1187 {
1188 result_valobj_sp = persistent_var_sp->GetValueObject ();
1189 execution_results = eExecutionCompleted;
1190 }
1191 else
1192 {
1193 const char *prefix = GetExpressionPrefixContentsAsCString();
1194
1195 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +00001196 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001197 expr_cstr,
1198 prefix,
1199 result_valobj_sp);
1200 }
1201 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001202
1203 m_suppress_stop_hooks = old_suppress_value;
1204
Greg Clayton427f2902010-12-14 02:59:59 +00001205 return execution_results;
1206}
1207
Greg Claytonc0fa5332011-05-22 22:46:53 +00001208lldb::addr_t
1209Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1210{
1211 addr_t code_addr = load_addr;
1212 switch (m_arch.GetMachine())
1213 {
1214 case llvm::Triple::arm:
1215 case llvm::Triple::thumb:
1216 switch (addr_class)
1217 {
1218 case eAddressClassData:
1219 case eAddressClassDebug:
1220 return LLDB_INVALID_ADDRESS;
1221
1222 case eAddressClassUnknown:
1223 case eAddressClassInvalid:
1224 case eAddressClassCode:
1225 case eAddressClassCodeAlternateISA:
1226 case eAddressClassRuntime:
1227 // Check if bit zero it no set?
1228 if ((code_addr & 1ull) == 0)
1229 {
1230 // Bit zero isn't set, check if the address is a multiple of 2?
1231 if (code_addr & 2ull)
1232 {
1233 // The address is a multiple of 2 so it must be thumb, set bit zero
1234 code_addr |= 1ull;
1235 }
1236 else if (addr_class == eAddressClassCodeAlternateISA)
1237 {
1238 // We checked the address and the address claims to be the alternate ISA
1239 // which means thumb, so set bit zero.
1240 code_addr |= 1ull;
1241 }
1242 }
1243 break;
1244 }
1245 break;
1246
1247 default:
1248 break;
1249 }
1250 return code_addr;
1251}
1252
1253lldb::addr_t
1254Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1255{
1256 addr_t opcode_addr = load_addr;
1257 switch (m_arch.GetMachine())
1258 {
1259 case llvm::Triple::arm:
1260 case llvm::Triple::thumb:
1261 switch (addr_class)
1262 {
1263 case eAddressClassData:
1264 case eAddressClassDebug:
1265 return LLDB_INVALID_ADDRESS;
1266
1267 case eAddressClassInvalid:
1268 case eAddressClassUnknown:
1269 case eAddressClassCode:
1270 case eAddressClassCodeAlternateISA:
1271 case eAddressClassRuntime:
1272 opcode_addr &= ~(1ull);
1273 break;
1274 }
1275 break;
1276
1277 default:
1278 break;
1279 }
1280 return opcode_addr;
1281}
1282
Jim Inghamd60d94a2011-03-11 03:53:59 +00001283lldb::user_id_t
1284Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1285{
1286 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1287 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1288 m_stop_hooks[new_uid] = new_hook_sp;
1289 return new_uid;
1290}
1291
1292bool
1293Target::RemoveStopHookByID (lldb::user_id_t user_id)
1294{
1295 size_t num_removed;
1296 num_removed = m_stop_hooks.erase (user_id);
1297 if (num_removed == 0)
1298 return false;
1299 else
1300 return true;
1301}
1302
1303void
1304Target::RemoveAllStopHooks ()
1305{
1306 m_stop_hooks.clear();
1307}
1308
1309Target::StopHookSP
1310Target::GetStopHookByID (lldb::user_id_t user_id)
1311{
1312 StopHookSP found_hook;
1313
1314 StopHookCollection::iterator specified_hook_iter;
1315 specified_hook_iter = m_stop_hooks.find (user_id);
1316 if (specified_hook_iter != m_stop_hooks.end())
1317 found_hook = (*specified_hook_iter).second;
1318 return found_hook;
1319}
1320
1321bool
1322Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1323{
1324 StopHookCollection::iterator specified_hook_iter;
1325 specified_hook_iter = m_stop_hooks.find (user_id);
1326 if (specified_hook_iter == m_stop_hooks.end())
1327 return false;
1328
1329 (*specified_hook_iter).second->SetIsActive (active_state);
1330 return true;
1331}
1332
1333void
1334Target::SetAllStopHooksActiveState (bool active_state)
1335{
1336 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1337 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1338 {
1339 (*pos).second->SetIsActive (active_state);
1340 }
1341}
1342
1343void
1344Target::RunStopHooks ()
1345{
Jim Ingham3613ae12011-05-12 02:06:14 +00001346 if (m_suppress_stop_hooks)
1347 return;
1348
Jim Inghamd60d94a2011-03-11 03:53:59 +00001349 if (!m_process_sp)
1350 return;
1351
1352 if (m_stop_hooks.empty())
1353 return;
1354
1355 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1356
1357 // If there aren't any active stop hooks, don't bother either:
1358 bool any_active_hooks = false;
1359 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1360 {
1361 if ((*pos).second->IsActive())
1362 {
1363 any_active_hooks = true;
1364 break;
1365 }
1366 }
1367 if (!any_active_hooks)
1368 return;
1369
1370 CommandReturnObject result;
1371
1372 std::vector<ExecutionContext> exc_ctx_with_reasons;
1373 std::vector<SymbolContext> sym_ctx_with_reasons;
1374
1375 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1376 size_t num_threads = cur_threadlist.GetSize();
1377 for (size_t i = 0; i < num_threads; i++)
1378 {
1379 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1380 if (cur_thread_sp->ThreadStoppedForAReason())
1381 {
1382 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1383 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1384 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1385 }
1386 }
1387
1388 // If no threads stopped for a reason, don't run the stop-hooks.
1389 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1390 if (num_exe_ctx == 0)
1391 return;
1392
Jim Inghame5ed8e92011-06-02 23:58:26 +00001393 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1394 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001395
1396 bool keep_going = true;
1397 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001398 bool print_hook_header;
1399 bool print_thread_header;
1400
1401 if (num_exe_ctx == 1)
1402 print_thread_header = false;
1403 else
1404 print_thread_header = true;
1405
1406 if (m_stop_hooks.size() == 1)
1407 print_hook_header = false;
1408 else
1409 print_hook_header = true;
1410
Jim Inghamd60d94a2011-03-11 03:53:59 +00001411 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1412 {
1413 // result.Clear();
1414 StopHookSP cur_hook_sp = (*pos).second;
1415 if (!cur_hook_sp->IsActive())
1416 continue;
1417
1418 bool any_thread_matched = false;
1419 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1420 {
1421 if ((cur_hook_sp->GetSpecifier () == NULL
1422 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1423 && (cur_hook_sp->GetThreadSpecifier() == NULL
1424 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1425 {
1426 if (!hooks_ran)
1427 {
Jim Inghamc54840c2011-03-22 01:47:27 +00001428 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001429 hooks_ran = true;
1430 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001431 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001432 {
1433 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1434 any_thread_matched = true;
1435 }
1436
Jim Inghamc54840c2011-03-22 01:47:27 +00001437 if (print_thread_header)
1438 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001439
1440 bool stop_on_continue = true;
1441 bool stop_on_error = true;
1442 bool echo_commands = false;
1443 bool print_results = true;
1444 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001445 &exc_ctx_with_reasons[i],
1446 stop_on_continue,
1447 stop_on_error,
1448 echo_commands,
1449 print_results,
1450 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001451
1452 // If the command started the target going again, we should bag out of
1453 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001454 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1455 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001456 {
1457 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1458 keep_going = false;
1459 }
1460 }
1461 }
1462 }
1463 if (hooks_ran)
1464 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice4a348082011-05-02 20:41:46 +00001465
1466 result.GetImmediateOutputStream()->Flush();
1467 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001468}
1469
Greg Claytonbbea1332011-07-08 00:48:09 +00001470bool
1471Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1472{
1473 bool changed = false;
1474 if (module)
1475 {
1476 ObjectFile *object_file = module->GetObjectFile();
1477 if (object_file)
1478 {
1479 SectionList *section_list = object_file->GetSectionList ();
1480 if (section_list)
1481 {
1482 // All sections listed in the dyld image info structure will all
1483 // either be fixed up already, or they will all be off by a single
1484 // slide amount that is determined by finding the first segment
1485 // that is at file offset zero which also has bytes (a file size
1486 // that is greater than zero) in the object file.
1487
1488 // Determine the slide amount (if any)
1489 const size_t num_sections = section_list->GetSize();
1490 size_t sect_idx = 0;
1491 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1492 {
1493 // Iterate through the object file sections to find the
1494 // first section that starts of file offset zero and that
1495 // has bytes in the file...
1496 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1497 if (section)
1498 {
1499 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1500 changed = true;
1501 }
1502 }
1503 }
1504 }
1505 }
1506 return changed;
1507}
1508
1509
Jim Inghamd60d94a2011-03-11 03:53:59 +00001510//--------------------------------------------------------------
1511// class Target::StopHook
1512//--------------------------------------------------------------
1513
1514
1515Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1516 UserID (uid),
1517 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001518 m_commands (),
1519 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001520 m_thread_spec_ap(NULL),
1521 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001522{
1523}
1524
1525Target::StopHook::StopHook (const StopHook &rhs) :
1526 UserID (rhs.GetID()),
1527 m_target_sp (rhs.m_target_sp),
1528 m_commands (rhs.m_commands),
1529 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001530 m_thread_spec_ap (NULL),
1531 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001532{
1533 if (rhs.m_thread_spec_ap.get() != NULL)
1534 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1535}
1536
1537
1538Target::StopHook::~StopHook ()
1539{
1540}
1541
1542void
1543Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1544{
1545 m_thread_spec_ap.reset (specifier);
1546}
1547
1548
1549void
1550Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1551{
1552 int indent_level = s->GetIndentLevel();
1553
1554 s->SetIndentLevel(indent_level + 2);
1555
1556 s->Printf ("Hook: %d\n", GetID());
1557 if (m_active)
1558 s->Indent ("State: enabled\n");
1559 else
1560 s->Indent ("State: disabled\n");
1561
1562 if (m_specifier_sp)
1563 {
1564 s->Indent();
1565 s->PutCString ("Specifier:\n");
1566 s->SetIndentLevel (indent_level + 4);
1567 m_specifier_sp->GetDescription (s, level);
1568 s->SetIndentLevel (indent_level + 2);
1569 }
1570
1571 if (m_thread_spec_ap.get() != NULL)
1572 {
1573 StreamString tmp;
1574 s->Indent("Thread:\n");
1575 m_thread_spec_ap->GetDescription (&tmp, level);
1576 s->SetIndentLevel (indent_level + 4);
1577 s->Indent (tmp.GetData());
1578 s->PutCString ("\n");
1579 s->SetIndentLevel (indent_level + 2);
1580 }
1581
1582 s->Indent ("Commands: \n");
1583 s->SetIndentLevel (indent_level + 4);
1584 uint32_t num_commands = m_commands.GetSize();
1585 for (uint32_t i = 0; i < num_commands; i++)
1586 {
1587 s->Indent(m_commands.GetStringAtIndex(i));
1588 s->PutCString ("\n");
1589 }
1590 s->SetIndentLevel (indent_level);
1591}
1592
1593
Caroline Tice5bc8c972010-09-20 20:44:43 +00001594//--------------------------------------------------------------
1595// class Target::SettingsController
1596//--------------------------------------------------------------
1597
1598Target::SettingsController::SettingsController () :
1599 UserSettingsController ("target", Debugger::GetSettingsController()),
1600 m_default_architecture ()
1601{
1602 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1603 InstanceSettings::GetDefaultName().AsCString()));
1604}
1605
1606Target::SettingsController::~SettingsController ()
1607{
1608}
1609
1610lldb::InstanceSettingsSP
1611Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1612{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001613 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1614 false,
1615 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001616 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1617 return new_settings_sp;
1618}
1619
Caroline Tice5bc8c972010-09-20 20:44:43 +00001620
Jim Inghame41494a2011-04-16 00:01:13 +00001621#define TSC_DEFAULT_ARCH "default-arch"
1622#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001623#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001624#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001625#define TSC_SOURCE_MAP "source-map"
Enrico Granata018921d2011-08-12 02:00:06 +00001626#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata91544802011-09-06 19:20:51 +00001627#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytond284b662011-02-18 01:44:25 +00001628
1629
1630static const ConstString &
1631GetSettingNameForDefaultArch ()
1632{
1633 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001634 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001635}
1636
Greg Claytond284b662011-02-18 01:44:25 +00001637static const ConstString &
1638GetSettingNameForExpressionPrefix ()
1639{
1640 static ConstString g_const_string (TSC_EXPR_PREFIX);
1641 return g_const_string;
1642}
1643
1644static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001645GetSettingNameForPreferDynamicValue ()
1646{
1647 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1648 return g_const_string;
1649}
1650
Greg Claytonff44ab42011-04-23 02:04:55 +00001651static const ConstString &
1652GetSettingNameForSourcePathMap ()
1653{
1654 static ConstString g_const_string (TSC_SOURCE_MAP);
1655 return g_const_string;
1656}
Greg Claytond284b662011-02-18 01:44:25 +00001657
Greg Clayton17cd9952011-04-22 03:55:06 +00001658static const ConstString &
1659GetSettingNameForSkipPrologue ()
1660{
1661 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1662 return g_const_string;
1663}
1664
Enrico Granata018921d2011-08-12 02:00:06 +00001665static const ConstString &
1666GetSettingNameForMaxChildren ()
1667{
1668 static ConstString g_const_string (TSC_MAX_CHILDREN);
1669 return g_const_string;
1670}
Greg Clayton17cd9952011-04-22 03:55:06 +00001671
Enrico Granata91544802011-09-06 19:20:51 +00001672static const ConstString &
1673GetSettingNameForMaxStringSummaryLength ()
1674{
1675 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1676 return g_const_string;
1677}
Greg Clayton17cd9952011-04-22 03:55:06 +00001678
Caroline Tice5bc8c972010-09-20 20:44:43 +00001679bool
1680Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1681 const char *index_value,
1682 const char *value,
1683 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001684 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001685 Error&err)
1686{
Greg Claytond284b662011-02-18 01:44:25 +00001687 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001688 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001689 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001690 if (!m_default_architecture.IsValid())
1691 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001692 }
1693 return true;
1694}
1695
1696
1697bool
1698Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1699 StringList &value,
1700 Error &err)
1701{
Greg Claytond284b662011-02-18 01:44:25 +00001702 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001703 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001704 // If the arch is invalid (the default), don't show a string for it
1705 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001706 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001707 return true;
1708 }
1709 else
1710 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1711
1712 return false;
1713}
1714
1715//--------------------------------------------------------------
1716// class TargetInstanceSettings
1717//--------------------------------------------------------------
1718
Greg Clayton638351a2010-12-04 00:10:17 +00001719TargetInstanceSettings::TargetInstanceSettings
1720(
1721 UserSettingsController &owner,
1722 bool live_instance,
1723 const char *name
1724) :
Greg Claytond284b662011-02-18 01:44:25 +00001725 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001726 m_expr_prefix_file (),
1727 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001728 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001729 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00001730 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00001731 m_max_children_display(256),
1732 m_max_strlen_length(1024)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001733{
1734 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1735 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1736 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1737 // This is true for CreateInstanceName() too.
1738
1739 if (GetInstanceName () == InstanceSettings::InvalidName())
1740 {
1741 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1742 m_owner.RegisterInstanceSettings (this);
1743 }
1744
1745 if (live_instance)
1746 {
1747 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1748 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001749 }
1750}
1751
1752TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001753 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1754 m_expr_prefix_file (rhs.m_expr_prefix_file),
1755 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1756 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1757 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00001758 m_source_map (rhs.m_source_map),
Enrico Granata91544802011-09-06 19:20:51 +00001759 m_max_children_display(rhs.m_max_children_display),
1760 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001761{
1762 if (m_instance_name != InstanceSettings::GetDefaultName())
1763 {
1764 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1765 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001766 }
1767}
1768
1769TargetInstanceSettings::~TargetInstanceSettings ()
1770{
1771}
1772
1773TargetInstanceSettings&
1774TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1775{
1776 if (this != &rhs)
1777 {
1778 }
1779
1780 return *this;
1781}
1782
Caroline Tice5bc8c972010-09-20 20:44:43 +00001783void
1784TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1785 const char *index_value,
1786 const char *value,
1787 const ConstString &instance_name,
1788 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001789 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001790 Error &err,
1791 bool pending)
1792{
Greg Claytond284b662011-02-18 01:44:25 +00001793 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001794 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001795 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1796 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00001797 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001798 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00001799 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001800 default:
1801 break;
1802 case eVarSetOperationAssign:
1803 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00001804 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001805 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1806 {
1807 err.SetErrorToGenericError ();
1808 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1809 return;
1810 }
1811
1812 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1813
1814 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1815 {
1816 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1817 m_expr_prefix_contents_sp.reset();
1818 }
Sean Callanan77e93942010-10-29 00:29:03 +00001819 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001820 break;
1821 case eVarSetOperationClear:
1822 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00001823 }
Sean Callanan77e93942010-10-29 00:29:03 +00001824 }
1825 }
Jim Inghame41494a2011-04-16 00:01:13 +00001826 else if (var_name == GetSettingNameForPreferDynamicValue())
1827 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001828 int new_value;
1829 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1830 if (err.Success())
1831 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00001832 }
1833 else if (var_name == GetSettingNameForSkipPrologue())
1834 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001835 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1836 }
Enrico Granata018921d2011-08-12 02:00:06 +00001837 else if (var_name == GetSettingNameForMaxChildren())
1838 {
1839 bool ok;
1840 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1841 if (ok)
1842 m_max_children_display = new_value;
1843 }
Enrico Granata91544802011-09-06 19:20:51 +00001844 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1845 {
1846 bool ok;
1847 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1848 if (ok)
1849 m_max_strlen_length = new_value;
1850 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001851 else if (var_name == GetSettingNameForSourcePathMap ())
1852 {
1853 switch (op)
1854 {
1855 case eVarSetOperationReplace:
1856 case eVarSetOperationInsertBefore:
1857 case eVarSetOperationInsertAfter:
1858 case eVarSetOperationRemove:
1859 default:
1860 break;
1861 case eVarSetOperationAssign:
1862 m_source_map.Clear(true);
1863 // Fall through to append....
1864 case eVarSetOperationAppend:
1865 {
1866 Args args(value);
1867 const uint32_t argc = args.GetArgumentCount();
1868 if (argc & 1 || argc == 0)
1869 {
1870 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1871 }
1872 else
1873 {
1874 char resolved_new_path[PATH_MAX];
1875 FileSpec file_spec;
1876 const char *old_path;
1877 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1878 {
1879 const char *new_path = args.GetArgumentAtIndex(idx+1);
1880 assert (new_path); // We have an even number of paths, this shouldn't happen!
1881
1882 file_spec.SetFile(new_path, true);
1883 if (file_spec.Exists())
1884 {
1885 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1886 {
1887 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1888 return;
1889 }
1890 }
1891 else
1892 {
1893 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1894 return;
1895 }
1896 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1897 }
1898 }
1899 }
1900 break;
1901
1902 case eVarSetOperationClear:
1903 m_source_map.Clear(true);
1904 break;
1905 }
Jim Inghame41494a2011-04-16 00:01:13 +00001906 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001907}
1908
1909void
Greg Claytond284b662011-02-18 01:44:25 +00001910TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001911{
Sean Callanan77e93942010-10-29 00:29:03 +00001912 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1913
1914 if (!new_settings_ptr)
1915 return;
1916
Greg Claytonff44ab42011-04-23 02:04:55 +00001917 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1918 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1919 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1920 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata018921d2011-08-12 02:00:06 +00001921 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata91544802011-09-06 19:20:51 +00001922 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001923}
1924
Caroline Ticebcb5b452010-09-20 21:37:42 +00001925bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001926TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1927 const ConstString &var_name,
1928 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001929 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001930{
Greg Claytond284b662011-02-18 01:44:25 +00001931 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001932 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001933 char path[PATH_MAX];
1934 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1935 if (path_len > 0)
1936 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00001937 }
Jim Inghame41494a2011-04-16 00:01:13 +00001938 else if (var_name == GetSettingNameForPreferDynamicValue())
1939 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001940 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00001941 }
Greg Clayton17cd9952011-04-22 03:55:06 +00001942 else if (var_name == GetSettingNameForSkipPrologue())
1943 {
1944 if (m_skip_prologue)
1945 value.AppendString ("true");
1946 else
1947 value.AppendString ("false");
1948 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001949 else if (var_name == GetSettingNameForSourcePathMap ())
1950 {
1951 }
Enrico Granata018921d2011-08-12 02:00:06 +00001952 else if (var_name == GetSettingNameForMaxChildren())
1953 {
1954 StreamString count_str;
1955 count_str.Printf ("%d", m_max_children_display);
1956 value.AppendString (count_str.GetData());
1957 }
Enrico Granata91544802011-09-06 19:20:51 +00001958 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1959 {
1960 StreamString count_str;
1961 count_str.Printf ("%d", m_max_strlen_length);
1962 value.AppendString (count_str.GetData());
1963 }
Sean Callanan77e93942010-10-29 00:29:03 +00001964 else
1965 {
1966 if (err)
1967 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1968 return false;
1969 }
1970
1971 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001972}
1973
1974const ConstString
1975TargetInstanceSettings::CreateInstanceName ()
1976{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001977 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001978 static int instance_count = 1;
1979
Caroline Tice5bc8c972010-09-20 20:44:43 +00001980 sstr.Printf ("target_%d", instance_count);
1981 ++instance_count;
1982
1983 const ConstString ret_val (sstr.GetData());
1984 return ret_val;
1985}
1986
1987//--------------------------------------------------
1988// Target::SettingsController Variable Tables
1989//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00001990OptionEnumValueElement
1991TargetInstanceSettings::g_dynamic_value_types[] =
1992{
Greg Clayton577fbc32011-05-30 00:39:48 +00001993{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
1994{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
1995{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00001996{ 0, NULL, NULL }
1997};
Caroline Tice5bc8c972010-09-20 20:44:43 +00001998
1999SettingEntry
2000Target::SettingsController::global_settings_table[] =
2001{
Greg Claytond284b662011-02-18 01:44:25 +00002002 // var-name var-type default enum init'd hidden help-text
2003 // ================= ================== =========== ==== ====== ====== =========================================================================
2004 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2005 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2006};
2007
Caroline Tice5bc8c972010-09-20 20:44:43 +00002008SettingEntry
2009Target::SettingsController::instance_settings_table[] =
2010{
Enrico Granata91544802011-09-06 19:20:51 +00002011 // var-name var-type default enum init'd hidden help-text
2012 // ================= ================== =============== ======================= ====== ====== =========================================================================
2013 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2014 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2015 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2016 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2017 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2018 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2019 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002020};