blob: 78f16bf9d839e1716bcdcfe4c030b08eb37b76fa [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;
Johnny Chen9bf11992011-09-13 01:15:36 +0000338
339 // FIXME: Add implmenetation.
340 return wp_loc_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000341}
342
Chris Lattner24943d22010-06-08 16:52:24 +0000343void
344Target::RemoveAllBreakpoints (bool internal_also)
345{
Greg Claytone005f2c2010-11-06 01:53:30 +0000346 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000347 if (log)
348 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
349
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000350 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000351 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000352 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000353
354 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000355}
356
357void
358Target::DisableAllBreakpoints (bool internal_also)
359{
Greg Claytone005f2c2010-11-06 01:53:30 +0000360 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000361 if (log)
362 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
363
364 m_breakpoint_list.SetEnabledAll (false);
365 if (internal_also)
366 m_internal_breakpoint_list.SetEnabledAll (false);
367}
368
369void
370Target::EnableAllBreakpoints (bool internal_also)
371{
Greg Claytone005f2c2010-11-06 01:53:30 +0000372 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000373 if (log)
374 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
375
376 m_breakpoint_list.SetEnabledAll (true);
377 if (internal_also)
378 m_internal_breakpoint_list.SetEnabledAll (true);
379}
380
381bool
382Target::RemoveBreakpointByID (break_id_t break_id)
383{
Greg Claytone005f2c2010-11-06 01:53:30 +0000384 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000385 if (log)
386 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
387
388 if (DisableBreakpointByID (break_id))
389 {
390 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000391 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000392 else
Jim Inghamd1686902010-10-14 23:45:03 +0000393 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000394 if (m_last_created_breakpoint)
395 {
396 if (m_last_created_breakpoint->GetID() == break_id)
397 m_last_created_breakpoint.reset();
398 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000399 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000400 }
Chris Lattner24943d22010-06-08 16:52:24 +0000401 return true;
402 }
403 return false;
404}
405
406bool
407Target::DisableBreakpointByID (break_id_t break_id)
408{
Greg Claytone005f2c2010-11-06 01:53:30 +0000409 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000410 if (log)
411 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
412
413 BreakpointSP bp_sp;
414
415 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
416 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
417 else
418 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
419 if (bp_sp)
420 {
421 bp_sp->SetEnabled (false);
422 return true;
423 }
424 return false;
425}
426
427bool
428Target::EnableBreakpointByID (break_id_t break_id)
429{
Greg Claytone005f2c2010-11-06 01:53:30 +0000430 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000431 if (log)
432 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
433 __FUNCTION__,
434 break_id,
435 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
436
437 BreakpointSP bp_sp;
438
439 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
440 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
441 else
442 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
443
444 if (bp_sp)
445 {
446 bp_sp->SetEnabled (true);
447 return true;
448 }
449 return false;
450}
451
452ModuleSP
453Target::GetExecutableModule ()
454{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000455 return m_images.GetModuleAtIndex(0);
456}
457
458Module*
459Target::GetExecutableModulePointer ()
460{
461 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000462}
463
464void
465Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
466{
467 m_images.Clear();
468 m_scratch_ast_context_ap.reset();
469
470 if (executable_sp.get())
471 {
472 Timer scoped_timer (__PRETTY_FUNCTION__,
473 "Target::SetExecutableModule (executable = '%s/%s')",
474 executable_sp->GetFileSpec().GetDirectory().AsCString(),
475 executable_sp->GetFileSpec().GetFilename().AsCString());
476
477 m_images.Append(executable_sp); // The first image is our exectuable file
478
Jim Ingham7508e732010-08-09 23:31:02 +0000479 // 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 +0000480 if (!m_arch.IsValid())
481 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000482
Chris Lattner24943d22010-06-08 16:52:24 +0000483 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000484 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000485 // Let's find the file & line for main and set the default source file from there.
486 if (!m_source_manager.DefaultFileAndLineSet())
487 {
488 SymbolContextList sc_list;
489 uint32_t num_matches;
490 ConstString main_name("main");
491 bool symbols_okay = false; // Force it to be a debug symbol.
492 bool append = false;
493 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
494 for (uint32_t idx = 0; idx < num_matches; idx++)
495 {
496 SymbolContext sc;
497 sc_list.GetContextAtIndex(idx, sc);
498 if (sc.line_entry.file)
499 {
500 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
501 break;
502 }
503 }
504 }
Chris Lattner24943d22010-06-08 16:52:24 +0000505
506 if (executable_objfile)
507 {
508 executable_objfile->GetDependentModules(dependent_files);
509 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
510 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000511 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
512 FileSpec platform_dependent_file_spec;
513 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000514 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000515 else
516 platform_dependent_file_spec = dependent_file_spec;
517
518 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000519 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000520 if (image_module_sp.get())
521 {
Chris Lattner24943d22010-06-08 16:52:24 +0000522 ObjectFile *objfile = image_module_sp->GetObjectFile();
523 if (objfile)
524 objfile->GetDependentModules(dependent_files);
525 }
526 }
527 }
528
Chris Lattner24943d22010-06-08 16:52:24 +0000529 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000530
531 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000532}
533
534
Jim Ingham7508e732010-08-09 23:31:02 +0000535bool
536Target::SetArchitecture (const ArchSpec &arch_spec)
537{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000538 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000539 {
540 // If we're setting the architecture to our current architecture, we
541 // don't need to do anything.
542 return true;
543 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000544 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000545 {
546 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000547 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000548 return true;
549 }
550 else
551 {
552 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000553 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000554 ModuleSP executable_sp = GetExecutableModule ();
555 m_images.Clear();
556 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000557 // Need to do something about unsetting breakpoints.
558
559 if (executable_sp)
560 {
561 FileSpec exec_file_spec = executable_sp->GetFileSpec();
562 Error error = ModuleList::GetSharedModule(exec_file_spec,
563 arch_spec,
564 NULL,
565 NULL,
566 0,
567 executable_sp,
568 NULL,
569 NULL);
570
571 if (!error.Fail() && executable_sp)
572 {
573 SetExecutableModule (executable_sp, true);
574 return true;
575 }
576 else
577 {
578 return false;
579 }
580 }
581 else
582 {
583 return false;
584 }
585 }
586}
Chris Lattner24943d22010-06-08 16:52:24 +0000587
Chris Lattner24943d22010-06-08 16:52:24 +0000588void
589Target::ModuleAdded (ModuleSP &module_sp)
590{
591 // A module is being added to this target for the first time
592 ModuleList module_list;
593 module_list.Append(module_sp);
594 ModulesDidLoad (module_list);
595}
596
597void
598Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
599{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000600 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000601 ModuleList module_list;
602 module_list.Append (old_module_sp);
603 ModulesDidUnload (module_list);
604 module_list.Clear ();
605 module_list.Append (new_module_sp);
606 ModulesDidLoad (module_list);
607}
608
609void
610Target::ModulesDidLoad (ModuleList &module_list)
611{
612 m_breakpoint_list.UpdateBreakpoints (module_list, true);
613 // TODO: make event data that packages up the module_list
614 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
615}
616
617void
618Target::ModulesDidUnload (ModuleList &module_list)
619{
620 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000621
622 // Remove the images from the target image list
623 m_images.Remove(module_list);
624
Chris Lattner24943d22010-06-08 16:52:24 +0000625 // TODO: make event data that packages up the module_list
626 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
627}
628
629size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000630Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
631{
632 const Section *section = addr.GetSection();
633 if (section && section->GetModule())
634 {
635 ObjectFile *objfile = section->GetModule()->GetObjectFile();
636 if (objfile)
637 {
638 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
639 addr.GetOffset(),
640 dst,
641 dst_len);
642 if (bytes_read > 0)
643 return bytes_read;
644 else
645 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
646 }
647 else
648 {
649 error.SetErrorString("address isn't from a object file");
650 }
651 }
652 else
653 {
654 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
655 }
656 return 0;
657}
658
659size_t
Enrico Granata91544802011-09-06 19:20:51 +0000660Target::ReadMemory (const Address& addr,
661 bool prefer_file_cache,
662 void *dst,
663 size_t dst_len,
664 Error &error,
665 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000666{
Chris Lattner24943d22010-06-08 16:52:24 +0000667 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000668
Enrico Granata91544802011-09-06 19:20:51 +0000669 // if we end up reading this from process memory, we will fill this
670 // with the actual load address
671 if (load_addr_ptr)
672 *load_addr_ptr = LLDB_INVALID_ADDRESS;
673
Greg Clayton70436352010-06-30 23:03:03 +0000674 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
675
Greg Clayton26100dc2011-01-07 01:57:07 +0000676 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000677
678 addr_t load_addr = LLDB_INVALID_ADDRESS;
679 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000680 Address resolved_addr;
681 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000682 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000683 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000684 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000685 // No sections are loaded, so we must assume we are not running
686 // yet and anything we are given is a file address.
687 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
688 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000689 }
Greg Clayton70436352010-06-30 23:03:03 +0000690 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000691 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000692 // We have at least one section loaded. This can be becuase
693 // we have manually loaded some sections with "target modules load ..."
694 // or because we have have a live process that has sections loaded
695 // through the dynamic loader
696 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
697 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000698 }
Greg Clayton70436352010-06-30 23:03:03 +0000699 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000700 if (!resolved_addr.IsValid())
701 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000702
Greg Clayton9b82f862011-07-11 05:12:02 +0000703
Greg Clayton26100dc2011-01-07 01:57:07 +0000704 if (prefer_file_cache)
705 {
706 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
707 if (bytes_read > 0)
708 return bytes_read;
709 }
Greg Clayton70436352010-06-30 23:03:03 +0000710
711 if (process_is_valid)
712 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000713 if (load_addr == LLDB_INVALID_ADDRESS)
714 load_addr = resolved_addr.GetLoadAddress (this);
715
Greg Clayton70436352010-06-30 23:03:03 +0000716 if (load_addr == LLDB_INVALID_ADDRESS)
717 {
718 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
719 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
720 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
721 resolved_addr.GetFileAddress());
722 else
723 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
724 }
725 else
726 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000727 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000728 if (bytes_read != dst_len)
729 {
730 if (error.Success())
731 {
732 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000733 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000734 else
Greg Clayton70436352010-06-30 23:03:03 +0000735 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 +0000736 }
737 }
Greg Clayton70436352010-06-30 23:03:03 +0000738 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +0000739 {
740 if (load_addr_ptr)
741 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +0000742 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +0000743 }
Greg Clayton70436352010-06-30 23:03:03 +0000744 // If the address is not section offset we have an address that
745 // doesn't resolve to any address in any currently loaded shared
746 // libaries and we failed to read memory so there isn't anything
747 // more we can do. If it is section offset, we might be able to
748 // read cached memory from the object file.
749 if (!resolved_addr.IsSectionOffset())
750 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000751 }
Chris Lattner24943d22010-06-08 16:52:24 +0000752 }
Greg Clayton70436352010-06-30 23:03:03 +0000753
Greg Clayton9b82f862011-07-11 05:12:02 +0000754 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000755 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000756 // If we didn't already try and read from the object file cache, then
757 // try it after failing to read from the process.
758 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000759 }
760 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000761}
762
Greg Clayton7dd98df2011-07-12 17:06:17 +0000763size_t
764Target::ReadScalarIntegerFromMemory (const Address& addr,
765 bool prefer_file_cache,
766 uint32_t byte_size,
767 bool is_signed,
768 Scalar &scalar,
769 Error &error)
770{
771 uint64_t uval;
772
773 if (byte_size <= sizeof(uval))
774 {
775 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
776 if (bytes_read == byte_size)
777 {
778 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
779 uint32_t offset = 0;
780 if (byte_size <= 4)
781 scalar = data.GetMaxU32 (&offset, byte_size);
782 else
783 scalar = data.GetMaxU64 (&offset, byte_size);
784
785 if (is_signed)
786 scalar.SignExtend(byte_size * 8);
787 return bytes_read;
788 }
789 }
790 else
791 {
792 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
793 }
794 return 0;
795}
796
797uint64_t
798Target::ReadUnsignedIntegerFromMemory (const Address& addr,
799 bool prefer_file_cache,
800 size_t integer_byte_size,
801 uint64_t fail_value,
802 Error &error)
803{
804 Scalar scalar;
805 if (ReadScalarIntegerFromMemory (addr,
806 prefer_file_cache,
807 integer_byte_size,
808 false,
809 scalar,
810 error))
811 return scalar.ULongLong(fail_value);
812 return fail_value;
813}
814
815bool
816Target::ReadPointerFromMemory (const Address& addr,
817 bool prefer_file_cache,
818 Error &error,
819 Address &pointer_addr)
820{
821 Scalar scalar;
822 if (ReadScalarIntegerFromMemory (addr,
823 prefer_file_cache,
824 m_arch.GetAddressByteSize(),
825 false,
826 scalar,
827 error))
828 {
829 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
830 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
831 {
832 if (m_section_load_list.IsEmpty())
833 {
834 // No sections are loaded, so we must assume we are not running
835 // yet and anything we are given is a file address.
836 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
837 }
838 else
839 {
840 // We have at least one section loaded. This can be becuase
841 // we have manually loaded some sections with "target modules load ..."
842 // or because we have have a live process that has sections loaded
843 // through the dynamic loader
844 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
845 }
846 // We weren't able to resolve the pointer value, so just return
847 // an address with no section
848 if (!pointer_addr.IsValid())
849 pointer_addr.SetOffset (pointer_vm_addr);
850 return true;
851
852 }
853 }
854 return false;
855}
Chris Lattner24943d22010-06-08 16:52:24 +0000856
857ModuleSP
858Target::GetSharedModule
859(
860 const FileSpec& file_spec,
861 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000862 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000863 const ConstString *object_name,
864 off_t object_offset,
865 Error *error_ptr
866)
867{
868 // Don't pass in the UUID so we can tell if we have a stale value in our list
869 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
870 bool did_create_module = false;
871 ModuleSP module_sp;
872
Chris Lattner24943d22010-06-08 16:52:24 +0000873 Error error;
874
Greg Clayton24bc5d92011-03-30 18:16:51 +0000875 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +0000876 if (m_image_search_paths.GetSize())
877 {
878 FileSpec transformed_spec;
879 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
880 {
881 transformed_spec.GetFilename() = file_spec.GetFilename();
882 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
883 }
884 }
885
Greg Clayton24bc5d92011-03-30 18:16:51 +0000886 // The platform is responsible for finding and caching an appropriate
887 // module in the shared module cache.
888 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000889 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000890 FileSpec platform_file_spec;
891 error = m_platform_sp->GetSharedModule (file_spec,
892 arch,
893 uuid_ptr,
894 object_name,
895 object_offset,
896 module_sp,
897 &old_module_sp,
898 &did_create_module);
899 }
900 else
901 {
902 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +0000903 }
904
Greg Clayton24bc5d92011-03-30 18:16:51 +0000905 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +0000906 if (module_sp)
907 {
908 m_images.Append (module_sp);
909 if (did_create_module)
910 {
911 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
912 ModuleUpdated(old_module_sp, module_sp);
913 else
914 ModuleAdded(module_sp);
915 }
916 }
917 if (error_ptr)
918 *error_ptr = error;
919 return module_sp;
920}
921
922
923Target *
924Target::CalculateTarget ()
925{
926 return this;
927}
928
929Process *
930Target::CalculateProcess ()
931{
932 return NULL;
933}
934
935Thread *
936Target::CalculateThread ()
937{
938 return NULL;
939}
940
941StackFrame *
942Target::CalculateStackFrame ()
943{
944 return NULL;
945}
946
947void
Greg Claytona830adb2010-10-04 01:05:56 +0000948Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000949{
950 exe_ctx.target = this;
951 exe_ctx.process = NULL; // Do NOT fill in process...
952 exe_ctx.thread = NULL;
953 exe_ctx.frame = NULL;
954}
955
956PathMappingList &
957Target::GetImageSearchPathList ()
958{
959 return m_image_search_paths;
960}
961
962void
963Target::ImageSearchPathsChanged
964(
965 const PathMappingList &path_list,
966 void *baton
967)
968{
969 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +0000970 ModuleSP exe_module_sp (target->GetExecutableModule());
971 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000972 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000973 target->m_images.Clear();
974 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000975 }
976}
977
978ClangASTContext *
979Target::GetScratchClangASTContext()
980{
Greg Clayton34ce4ea2011-08-03 01:23:55 +0000981 // Now see if we know the target triple, and if so, create our scratch AST context:
982 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
983 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000984 return m_scratch_ast_context_ap.get();
985}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000986
Greg Clayton990de7b2010-11-18 23:32:35 +0000987void
Caroline Tice2a456812011-03-10 22:14:10 +0000988Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000989{
Greg Clayton990de7b2010-11-18 23:32:35 +0000990 UserSettingsControllerSP &usc = GetSettingsController();
991 usc.reset (new SettingsController);
992 UserSettingsController::InitializeSettingsController (usc,
993 SettingsController::global_settings_table,
994 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000995
996 // Now call SettingsInitialize() on each 'child' setting of Target
997 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000998}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000999
Greg Clayton990de7b2010-11-18 23:32:35 +00001000void
Caroline Tice2a456812011-03-10 22:14:10 +00001001Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001002{
Caroline Tice2a456812011-03-10 22:14:10 +00001003
1004 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1005
1006 Process::SettingsTerminate ();
1007
1008 // Now terminate Target Settings.
1009
Greg Clayton990de7b2010-11-18 23:32:35 +00001010 UserSettingsControllerSP &usc = GetSettingsController();
1011 UserSettingsController::FinalizeSettingsController (usc);
1012 usc.reset();
1013}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001014
Greg Clayton990de7b2010-11-18 23:32:35 +00001015UserSettingsControllerSP &
1016Target::GetSettingsController ()
1017{
1018 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001019 return g_settings_controller;
1020}
1021
1022ArchSpec
1023Target::GetDefaultArchitecture ()
1024{
Greg Clayton940b1032011-02-23 00:35:02 +00001025 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1026
1027 if (settings_controller_sp)
1028 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1029 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001030}
1031
1032void
Greg Clayton940b1032011-02-23 00:35:02 +00001033Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001034{
Greg Clayton940b1032011-02-23 00:35:02 +00001035 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1036
1037 if (settings_controller_sp)
1038 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001039}
1040
Greg Claytona830adb2010-10-04 01:05:56 +00001041Target *
1042Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1043{
1044 // The target can either exist in the "process" of ExecutionContext, or in
1045 // the "target_sp" member of SymbolContext. This accessor helper function
1046 // will get the target from one of these locations.
1047
1048 Target *target = NULL;
1049 if (sc_ptr != NULL)
1050 target = sc_ptr->target_sp.get();
1051 if (target == NULL)
1052 {
1053 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
1054 target = &exe_ctx_ptr->process->GetTarget();
1055 }
1056 return target;
1057}
1058
1059
Caroline Tice1ebef442010-09-27 00:30:10 +00001060void
1061Target::UpdateInstanceName ()
1062{
1063 StreamString sstr;
1064
Greg Clayton5beb99d2011-08-11 02:48:45 +00001065 Module *exe_module = GetExecutableModulePointer();
1066 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001067 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001068 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001069 exe_module->GetFileSpec().GetFilename().AsCString(),
1070 exe_module->GetArchitecture().GetArchitectureName());
1071 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001072 }
1073}
1074
Sean Callanan77e93942010-10-29 00:29:03 +00001075const char *
1076Target::GetExpressionPrefixContentsAsCString ()
1077{
Greg Claytonff44ab42011-04-23 02:04:55 +00001078 if (m_expr_prefix_contents_sp)
1079 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1080 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001081}
1082
Greg Clayton427f2902010-12-14 02:59:59 +00001083ExecutionResults
1084Target::EvaluateExpression
1085(
1086 const char *expr_cstr,
1087 StackFrame *frame,
1088 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001089 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001090 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001091 lldb::ValueObjectSP &result_valobj_sp
1092)
1093{
1094 ExecutionResults execution_results = eExecutionSetupError;
1095
1096 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001097
1098 // We shouldn't run stop hooks in expressions.
1099 // Be sure to reset this if you return anywhere within this function.
1100 bool old_suppress_value = m_suppress_stop_hooks;
1101 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001102
1103 ExecutionContext exe_ctx;
1104 if (frame)
1105 {
1106 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001107 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001108 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001109 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1110 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001111 lldb::VariableSP var_sp;
1112 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1113 use_dynamic,
1114 expr_path_options,
1115 var_sp,
1116 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001117 }
1118 else if (m_process_sp)
1119 {
1120 m_process_sp->CalculateExecutionContext(exe_ctx);
1121 }
1122 else
1123 {
1124 CalculateExecutionContext(exe_ctx);
1125 }
1126
1127 if (result_valobj_sp)
1128 {
1129 execution_results = eExecutionCompleted;
1130 // We got a result from the frame variable expression path above...
1131 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1132
1133 lldb::ValueObjectSP const_valobj_sp;
1134
1135 // Check in case our value is already a constant value
1136 if (result_valobj_sp->GetIsConstant())
1137 {
1138 const_valobj_sp = result_valobj_sp;
1139 const_valobj_sp->SetName (persistent_variable_name);
1140 }
1141 else
Jim Inghame41494a2011-04-16 00:01:13 +00001142 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001143 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001144 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001145 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001146 if (dynamic_sp)
1147 result_valobj_sp = dynamic_sp;
1148 }
1149
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001150 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001151 }
Greg Clayton427f2902010-12-14 02:59:59 +00001152
Sean Callanan6a925532011-01-13 08:53:35 +00001153 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1154
Greg Clayton427f2902010-12-14 02:59:59 +00001155 result_valobj_sp = const_valobj_sp;
1156
Sean Callanan6a925532011-01-13 08:53:35 +00001157 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1158 assert (clang_expr_variable_sp.get());
1159
1160 // Set flags and live data as appropriate
1161
1162 const Value &result_value = live_valobj_sp->GetValue();
1163
1164 switch (result_value.GetValueType())
1165 {
1166 case Value::eValueTypeHostAddress:
1167 case Value::eValueTypeFileAddress:
1168 // we don't do anything with these for now
1169 break;
1170 case Value::eValueTypeScalar:
1171 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1172 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1173 break;
1174 case Value::eValueTypeLoadAddress:
1175 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1176 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1177 break;
1178 }
Greg Clayton427f2902010-12-14 02:59:59 +00001179 }
1180 else
1181 {
1182 // Make sure we aren't just trying to see the value of a persistent
1183 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001184 lldb::ClangExpressionVariableSP persistent_var_sp;
1185 // Only check for persistent variables the expression starts with a '$'
1186 if (expr_cstr[0] == '$')
1187 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1188
Greg Clayton427f2902010-12-14 02:59:59 +00001189 if (persistent_var_sp)
1190 {
1191 result_valobj_sp = persistent_var_sp->GetValueObject ();
1192 execution_results = eExecutionCompleted;
1193 }
1194 else
1195 {
1196 const char *prefix = GetExpressionPrefixContentsAsCString();
1197
1198 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +00001199 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001200 expr_cstr,
1201 prefix,
1202 result_valobj_sp);
1203 }
1204 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001205
1206 m_suppress_stop_hooks = old_suppress_value;
1207
Greg Clayton427f2902010-12-14 02:59:59 +00001208 return execution_results;
1209}
1210
Greg Claytonc0fa5332011-05-22 22:46:53 +00001211lldb::addr_t
1212Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1213{
1214 addr_t code_addr = load_addr;
1215 switch (m_arch.GetMachine())
1216 {
1217 case llvm::Triple::arm:
1218 case llvm::Triple::thumb:
1219 switch (addr_class)
1220 {
1221 case eAddressClassData:
1222 case eAddressClassDebug:
1223 return LLDB_INVALID_ADDRESS;
1224
1225 case eAddressClassUnknown:
1226 case eAddressClassInvalid:
1227 case eAddressClassCode:
1228 case eAddressClassCodeAlternateISA:
1229 case eAddressClassRuntime:
1230 // Check if bit zero it no set?
1231 if ((code_addr & 1ull) == 0)
1232 {
1233 // Bit zero isn't set, check if the address is a multiple of 2?
1234 if (code_addr & 2ull)
1235 {
1236 // The address is a multiple of 2 so it must be thumb, set bit zero
1237 code_addr |= 1ull;
1238 }
1239 else if (addr_class == eAddressClassCodeAlternateISA)
1240 {
1241 // We checked the address and the address claims to be the alternate ISA
1242 // which means thumb, so set bit zero.
1243 code_addr |= 1ull;
1244 }
1245 }
1246 break;
1247 }
1248 break;
1249
1250 default:
1251 break;
1252 }
1253 return code_addr;
1254}
1255
1256lldb::addr_t
1257Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1258{
1259 addr_t opcode_addr = load_addr;
1260 switch (m_arch.GetMachine())
1261 {
1262 case llvm::Triple::arm:
1263 case llvm::Triple::thumb:
1264 switch (addr_class)
1265 {
1266 case eAddressClassData:
1267 case eAddressClassDebug:
1268 return LLDB_INVALID_ADDRESS;
1269
1270 case eAddressClassInvalid:
1271 case eAddressClassUnknown:
1272 case eAddressClassCode:
1273 case eAddressClassCodeAlternateISA:
1274 case eAddressClassRuntime:
1275 opcode_addr &= ~(1ull);
1276 break;
1277 }
1278 break;
1279
1280 default:
1281 break;
1282 }
1283 return opcode_addr;
1284}
1285
Jim Inghamd60d94a2011-03-11 03:53:59 +00001286lldb::user_id_t
1287Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1288{
1289 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1290 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1291 m_stop_hooks[new_uid] = new_hook_sp;
1292 return new_uid;
1293}
1294
1295bool
1296Target::RemoveStopHookByID (lldb::user_id_t user_id)
1297{
1298 size_t num_removed;
1299 num_removed = m_stop_hooks.erase (user_id);
1300 if (num_removed == 0)
1301 return false;
1302 else
1303 return true;
1304}
1305
1306void
1307Target::RemoveAllStopHooks ()
1308{
1309 m_stop_hooks.clear();
1310}
1311
1312Target::StopHookSP
1313Target::GetStopHookByID (lldb::user_id_t user_id)
1314{
1315 StopHookSP found_hook;
1316
1317 StopHookCollection::iterator specified_hook_iter;
1318 specified_hook_iter = m_stop_hooks.find (user_id);
1319 if (specified_hook_iter != m_stop_hooks.end())
1320 found_hook = (*specified_hook_iter).second;
1321 return found_hook;
1322}
1323
1324bool
1325Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1326{
1327 StopHookCollection::iterator specified_hook_iter;
1328 specified_hook_iter = m_stop_hooks.find (user_id);
1329 if (specified_hook_iter == m_stop_hooks.end())
1330 return false;
1331
1332 (*specified_hook_iter).second->SetIsActive (active_state);
1333 return true;
1334}
1335
1336void
1337Target::SetAllStopHooksActiveState (bool active_state)
1338{
1339 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1340 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1341 {
1342 (*pos).second->SetIsActive (active_state);
1343 }
1344}
1345
1346void
1347Target::RunStopHooks ()
1348{
Jim Ingham3613ae12011-05-12 02:06:14 +00001349 if (m_suppress_stop_hooks)
1350 return;
1351
Jim Inghamd60d94a2011-03-11 03:53:59 +00001352 if (!m_process_sp)
1353 return;
1354
1355 if (m_stop_hooks.empty())
1356 return;
1357
1358 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1359
1360 // If there aren't any active stop hooks, don't bother either:
1361 bool any_active_hooks = false;
1362 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1363 {
1364 if ((*pos).second->IsActive())
1365 {
1366 any_active_hooks = true;
1367 break;
1368 }
1369 }
1370 if (!any_active_hooks)
1371 return;
1372
1373 CommandReturnObject result;
1374
1375 std::vector<ExecutionContext> exc_ctx_with_reasons;
1376 std::vector<SymbolContext> sym_ctx_with_reasons;
1377
1378 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1379 size_t num_threads = cur_threadlist.GetSize();
1380 for (size_t i = 0; i < num_threads; i++)
1381 {
1382 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1383 if (cur_thread_sp->ThreadStoppedForAReason())
1384 {
1385 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1386 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1387 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1388 }
1389 }
1390
1391 // If no threads stopped for a reason, don't run the stop-hooks.
1392 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1393 if (num_exe_ctx == 0)
1394 return;
1395
Jim Inghame5ed8e92011-06-02 23:58:26 +00001396 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1397 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001398
1399 bool keep_going = true;
1400 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001401 bool print_hook_header;
1402 bool print_thread_header;
1403
1404 if (num_exe_ctx == 1)
1405 print_thread_header = false;
1406 else
1407 print_thread_header = true;
1408
1409 if (m_stop_hooks.size() == 1)
1410 print_hook_header = false;
1411 else
1412 print_hook_header = true;
1413
Jim Inghamd60d94a2011-03-11 03:53:59 +00001414 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1415 {
1416 // result.Clear();
1417 StopHookSP cur_hook_sp = (*pos).second;
1418 if (!cur_hook_sp->IsActive())
1419 continue;
1420
1421 bool any_thread_matched = false;
1422 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1423 {
1424 if ((cur_hook_sp->GetSpecifier () == NULL
1425 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1426 && (cur_hook_sp->GetThreadSpecifier() == NULL
1427 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1428 {
1429 if (!hooks_ran)
1430 {
Jim Inghamc54840c2011-03-22 01:47:27 +00001431 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001432 hooks_ran = true;
1433 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001434 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001435 {
1436 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1437 any_thread_matched = true;
1438 }
1439
Jim Inghamc54840c2011-03-22 01:47:27 +00001440 if (print_thread_header)
1441 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001442
1443 bool stop_on_continue = true;
1444 bool stop_on_error = true;
1445 bool echo_commands = false;
1446 bool print_results = true;
1447 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001448 &exc_ctx_with_reasons[i],
1449 stop_on_continue,
1450 stop_on_error,
1451 echo_commands,
1452 print_results,
1453 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001454
1455 // If the command started the target going again, we should bag out of
1456 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001457 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1458 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001459 {
1460 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1461 keep_going = false;
1462 }
1463 }
1464 }
1465 }
1466 if (hooks_ran)
1467 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice4a348082011-05-02 20:41:46 +00001468
1469 result.GetImmediateOutputStream()->Flush();
1470 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001471}
1472
Greg Claytonbbea1332011-07-08 00:48:09 +00001473bool
1474Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1475{
1476 bool changed = false;
1477 if (module)
1478 {
1479 ObjectFile *object_file = module->GetObjectFile();
1480 if (object_file)
1481 {
1482 SectionList *section_list = object_file->GetSectionList ();
1483 if (section_list)
1484 {
1485 // All sections listed in the dyld image info structure will all
1486 // either be fixed up already, or they will all be off by a single
1487 // slide amount that is determined by finding the first segment
1488 // that is at file offset zero which also has bytes (a file size
1489 // that is greater than zero) in the object file.
1490
1491 // Determine the slide amount (if any)
1492 const size_t num_sections = section_list->GetSize();
1493 size_t sect_idx = 0;
1494 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1495 {
1496 // Iterate through the object file sections to find the
1497 // first section that starts of file offset zero and that
1498 // has bytes in the file...
1499 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1500 if (section)
1501 {
1502 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1503 changed = true;
1504 }
1505 }
1506 }
1507 }
1508 }
1509 return changed;
1510}
1511
1512
Jim Inghamd60d94a2011-03-11 03:53:59 +00001513//--------------------------------------------------------------
1514// class Target::StopHook
1515//--------------------------------------------------------------
1516
1517
1518Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1519 UserID (uid),
1520 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001521 m_commands (),
1522 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001523 m_thread_spec_ap(NULL),
1524 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001525{
1526}
1527
1528Target::StopHook::StopHook (const StopHook &rhs) :
1529 UserID (rhs.GetID()),
1530 m_target_sp (rhs.m_target_sp),
1531 m_commands (rhs.m_commands),
1532 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001533 m_thread_spec_ap (NULL),
1534 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001535{
1536 if (rhs.m_thread_spec_ap.get() != NULL)
1537 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1538}
1539
1540
1541Target::StopHook::~StopHook ()
1542{
1543}
1544
1545void
1546Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1547{
1548 m_thread_spec_ap.reset (specifier);
1549}
1550
1551
1552void
1553Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1554{
1555 int indent_level = s->GetIndentLevel();
1556
1557 s->SetIndentLevel(indent_level + 2);
1558
1559 s->Printf ("Hook: %d\n", GetID());
1560 if (m_active)
1561 s->Indent ("State: enabled\n");
1562 else
1563 s->Indent ("State: disabled\n");
1564
1565 if (m_specifier_sp)
1566 {
1567 s->Indent();
1568 s->PutCString ("Specifier:\n");
1569 s->SetIndentLevel (indent_level + 4);
1570 m_specifier_sp->GetDescription (s, level);
1571 s->SetIndentLevel (indent_level + 2);
1572 }
1573
1574 if (m_thread_spec_ap.get() != NULL)
1575 {
1576 StreamString tmp;
1577 s->Indent("Thread:\n");
1578 m_thread_spec_ap->GetDescription (&tmp, level);
1579 s->SetIndentLevel (indent_level + 4);
1580 s->Indent (tmp.GetData());
1581 s->PutCString ("\n");
1582 s->SetIndentLevel (indent_level + 2);
1583 }
1584
1585 s->Indent ("Commands: \n");
1586 s->SetIndentLevel (indent_level + 4);
1587 uint32_t num_commands = m_commands.GetSize();
1588 for (uint32_t i = 0; i < num_commands; i++)
1589 {
1590 s->Indent(m_commands.GetStringAtIndex(i));
1591 s->PutCString ("\n");
1592 }
1593 s->SetIndentLevel (indent_level);
1594}
1595
1596
Caroline Tice5bc8c972010-09-20 20:44:43 +00001597//--------------------------------------------------------------
1598// class Target::SettingsController
1599//--------------------------------------------------------------
1600
1601Target::SettingsController::SettingsController () :
1602 UserSettingsController ("target", Debugger::GetSettingsController()),
1603 m_default_architecture ()
1604{
1605 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1606 InstanceSettings::GetDefaultName().AsCString()));
1607}
1608
1609Target::SettingsController::~SettingsController ()
1610{
1611}
1612
1613lldb::InstanceSettingsSP
1614Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1615{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001616 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1617 false,
1618 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001619 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1620 return new_settings_sp;
1621}
1622
Caroline Tice5bc8c972010-09-20 20:44:43 +00001623
Jim Inghame41494a2011-04-16 00:01:13 +00001624#define TSC_DEFAULT_ARCH "default-arch"
1625#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001626#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001627#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001628#define TSC_SOURCE_MAP "source-map"
Enrico Granata018921d2011-08-12 02:00:06 +00001629#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata91544802011-09-06 19:20:51 +00001630#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytond284b662011-02-18 01:44:25 +00001631
1632
1633static const ConstString &
1634GetSettingNameForDefaultArch ()
1635{
1636 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001637 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001638}
1639
Greg Claytond284b662011-02-18 01:44:25 +00001640static const ConstString &
1641GetSettingNameForExpressionPrefix ()
1642{
1643 static ConstString g_const_string (TSC_EXPR_PREFIX);
1644 return g_const_string;
1645}
1646
1647static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001648GetSettingNameForPreferDynamicValue ()
1649{
1650 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1651 return g_const_string;
1652}
1653
Greg Claytonff44ab42011-04-23 02:04:55 +00001654static const ConstString &
1655GetSettingNameForSourcePathMap ()
1656{
1657 static ConstString g_const_string (TSC_SOURCE_MAP);
1658 return g_const_string;
1659}
Greg Claytond284b662011-02-18 01:44:25 +00001660
Greg Clayton17cd9952011-04-22 03:55:06 +00001661static const ConstString &
1662GetSettingNameForSkipPrologue ()
1663{
1664 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1665 return g_const_string;
1666}
1667
Enrico Granata018921d2011-08-12 02:00:06 +00001668static const ConstString &
1669GetSettingNameForMaxChildren ()
1670{
1671 static ConstString g_const_string (TSC_MAX_CHILDREN);
1672 return g_const_string;
1673}
Greg Clayton17cd9952011-04-22 03:55:06 +00001674
Enrico Granata91544802011-09-06 19:20:51 +00001675static const ConstString &
1676GetSettingNameForMaxStringSummaryLength ()
1677{
1678 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1679 return g_const_string;
1680}
Greg Clayton17cd9952011-04-22 03:55:06 +00001681
Caroline Tice5bc8c972010-09-20 20:44:43 +00001682bool
1683Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1684 const char *index_value,
1685 const char *value,
1686 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001687 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001688 Error&err)
1689{
Greg Claytond284b662011-02-18 01:44:25 +00001690 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001691 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001692 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001693 if (!m_default_architecture.IsValid())
1694 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001695 }
1696 return true;
1697}
1698
1699
1700bool
1701Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1702 StringList &value,
1703 Error &err)
1704{
Greg Claytond284b662011-02-18 01:44:25 +00001705 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001706 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001707 // If the arch is invalid (the default), don't show a string for it
1708 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001709 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001710 return true;
1711 }
1712 else
1713 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1714
1715 return false;
1716}
1717
1718//--------------------------------------------------------------
1719// class TargetInstanceSettings
1720//--------------------------------------------------------------
1721
Greg Clayton638351a2010-12-04 00:10:17 +00001722TargetInstanceSettings::TargetInstanceSettings
1723(
1724 UserSettingsController &owner,
1725 bool live_instance,
1726 const char *name
1727) :
Greg Claytond284b662011-02-18 01:44:25 +00001728 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001729 m_expr_prefix_file (),
1730 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001731 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001732 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00001733 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00001734 m_max_children_display(256),
1735 m_max_strlen_length(1024)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001736{
1737 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1738 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1739 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1740 // This is true for CreateInstanceName() too.
1741
1742 if (GetInstanceName () == InstanceSettings::InvalidName())
1743 {
1744 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1745 m_owner.RegisterInstanceSettings (this);
1746 }
1747
1748 if (live_instance)
1749 {
1750 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1751 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001752 }
1753}
1754
1755TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001756 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1757 m_expr_prefix_file (rhs.m_expr_prefix_file),
1758 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1759 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1760 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00001761 m_source_map (rhs.m_source_map),
Enrico Granata91544802011-09-06 19:20:51 +00001762 m_max_children_display(rhs.m_max_children_display),
1763 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001764{
1765 if (m_instance_name != InstanceSettings::GetDefaultName())
1766 {
1767 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1768 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001769 }
1770}
1771
1772TargetInstanceSettings::~TargetInstanceSettings ()
1773{
1774}
1775
1776TargetInstanceSettings&
1777TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1778{
1779 if (this != &rhs)
1780 {
1781 }
1782
1783 return *this;
1784}
1785
Caroline Tice5bc8c972010-09-20 20:44:43 +00001786void
1787TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1788 const char *index_value,
1789 const char *value,
1790 const ConstString &instance_name,
1791 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001792 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001793 Error &err,
1794 bool pending)
1795{
Greg Claytond284b662011-02-18 01:44:25 +00001796 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001797 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001798 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1799 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00001800 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001801 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00001802 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001803 default:
1804 break;
1805 case eVarSetOperationAssign:
1806 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00001807 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001808 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1809 {
1810 err.SetErrorToGenericError ();
1811 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1812 return;
1813 }
1814
1815 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1816
1817 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1818 {
1819 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1820 m_expr_prefix_contents_sp.reset();
1821 }
Sean Callanan77e93942010-10-29 00:29:03 +00001822 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001823 break;
1824 case eVarSetOperationClear:
1825 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00001826 }
Sean Callanan77e93942010-10-29 00:29:03 +00001827 }
1828 }
Jim Inghame41494a2011-04-16 00:01:13 +00001829 else if (var_name == GetSettingNameForPreferDynamicValue())
1830 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001831 int new_value;
1832 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1833 if (err.Success())
1834 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00001835 }
1836 else if (var_name == GetSettingNameForSkipPrologue())
1837 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001838 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1839 }
Enrico Granata018921d2011-08-12 02:00:06 +00001840 else if (var_name == GetSettingNameForMaxChildren())
1841 {
1842 bool ok;
1843 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1844 if (ok)
1845 m_max_children_display = new_value;
1846 }
Enrico Granata91544802011-09-06 19:20:51 +00001847 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1848 {
1849 bool ok;
1850 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1851 if (ok)
1852 m_max_strlen_length = new_value;
1853 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001854 else if (var_name == GetSettingNameForSourcePathMap ())
1855 {
1856 switch (op)
1857 {
1858 case eVarSetOperationReplace:
1859 case eVarSetOperationInsertBefore:
1860 case eVarSetOperationInsertAfter:
1861 case eVarSetOperationRemove:
1862 default:
1863 break;
1864 case eVarSetOperationAssign:
1865 m_source_map.Clear(true);
1866 // Fall through to append....
1867 case eVarSetOperationAppend:
1868 {
1869 Args args(value);
1870 const uint32_t argc = args.GetArgumentCount();
1871 if (argc & 1 || argc == 0)
1872 {
1873 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1874 }
1875 else
1876 {
1877 char resolved_new_path[PATH_MAX];
1878 FileSpec file_spec;
1879 const char *old_path;
1880 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1881 {
1882 const char *new_path = args.GetArgumentAtIndex(idx+1);
1883 assert (new_path); // We have an even number of paths, this shouldn't happen!
1884
1885 file_spec.SetFile(new_path, true);
1886 if (file_spec.Exists())
1887 {
1888 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1889 {
1890 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1891 return;
1892 }
1893 }
1894 else
1895 {
1896 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1897 return;
1898 }
1899 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1900 }
1901 }
1902 }
1903 break;
1904
1905 case eVarSetOperationClear:
1906 m_source_map.Clear(true);
1907 break;
1908 }
Jim Inghame41494a2011-04-16 00:01:13 +00001909 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001910}
1911
1912void
Greg Claytond284b662011-02-18 01:44:25 +00001913TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001914{
Sean Callanan77e93942010-10-29 00:29:03 +00001915 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1916
1917 if (!new_settings_ptr)
1918 return;
1919
Greg Claytonff44ab42011-04-23 02:04:55 +00001920 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1921 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1922 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1923 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata018921d2011-08-12 02:00:06 +00001924 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata91544802011-09-06 19:20:51 +00001925 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001926}
1927
Caroline Ticebcb5b452010-09-20 21:37:42 +00001928bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001929TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1930 const ConstString &var_name,
1931 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001932 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001933{
Greg Claytond284b662011-02-18 01:44:25 +00001934 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001935 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001936 char path[PATH_MAX];
1937 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1938 if (path_len > 0)
1939 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00001940 }
Jim Inghame41494a2011-04-16 00:01:13 +00001941 else if (var_name == GetSettingNameForPreferDynamicValue())
1942 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001943 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00001944 }
Greg Clayton17cd9952011-04-22 03:55:06 +00001945 else if (var_name == GetSettingNameForSkipPrologue())
1946 {
1947 if (m_skip_prologue)
1948 value.AppendString ("true");
1949 else
1950 value.AppendString ("false");
1951 }
Greg Claytonff44ab42011-04-23 02:04:55 +00001952 else if (var_name == GetSettingNameForSourcePathMap ())
1953 {
1954 }
Enrico Granata018921d2011-08-12 02:00:06 +00001955 else if (var_name == GetSettingNameForMaxChildren())
1956 {
1957 StreamString count_str;
1958 count_str.Printf ("%d", m_max_children_display);
1959 value.AppendString (count_str.GetData());
1960 }
Enrico Granata91544802011-09-06 19:20:51 +00001961 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1962 {
1963 StreamString count_str;
1964 count_str.Printf ("%d", m_max_strlen_length);
1965 value.AppendString (count_str.GetData());
1966 }
Sean Callanan77e93942010-10-29 00:29:03 +00001967 else
1968 {
1969 if (err)
1970 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1971 return false;
1972 }
1973
1974 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001975}
1976
1977const ConstString
1978TargetInstanceSettings::CreateInstanceName ()
1979{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001980 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001981 static int instance_count = 1;
1982
Caroline Tice5bc8c972010-09-20 20:44:43 +00001983 sstr.Printf ("target_%d", instance_count);
1984 ++instance_count;
1985
1986 const ConstString ret_val (sstr.GetData());
1987 return ret_val;
1988}
1989
1990//--------------------------------------------------
1991// Target::SettingsController Variable Tables
1992//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00001993OptionEnumValueElement
1994TargetInstanceSettings::g_dynamic_value_types[] =
1995{
Greg Clayton577fbc32011-05-30 00:39:48 +00001996{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
1997{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
1998{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00001999{ 0, NULL, NULL }
2000};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002001
2002SettingEntry
2003Target::SettingsController::global_settings_table[] =
2004{
Greg Claytond284b662011-02-18 01:44:25 +00002005 // var-name var-type default enum init'd hidden help-text
2006 // ================= ================== =========== ==== ====== ====== =========================================================================
2007 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2008 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2009};
2010
Caroline Tice5bc8c972010-09-20 20:44:43 +00002011SettingEntry
2012Target::SettingsController::instance_settings_table[] =
2013{
Enrico Granata91544802011-09-06 19:20:51 +00002014 // var-name var-type default enum init'd hidden help-text
2015 // ================= ================== =============== ======================= ====== ====== =========================================================================
2016 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2017 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2018 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2019 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2020 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2021 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2022 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002023};