blob: 11934e0a201a4bc35e976294347d650c1674eb56 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5920#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2421#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Caroline Tice969ed3d12011-05-02 20:41:4623#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner30fdc8d2010-06-08 16:52:2424#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5925#include "lldb/Core/Timer.h"
26#include "lldb/Core/ValueObject.h"
Greg Claytoneb0103f2011-04-07 22:46:3527#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:2428#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:5929#include "lldb/Interpreter/CommandInterpreter.h"
30#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2431#include "lldb/lldb-private-log.h"
32#include "lldb/Symbol/ObjectFile.h"
33#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5934#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:5935#include "lldb/Target/Thread.h"
36#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:2437
38using namespace lldb;
39using namespace lldb_private;
40
41//----------------------------------------------------------------------
42// Target constructor
43//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:5144Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
45 Broadcaster ("lldb.target"),
46 ExecutionContextScope (),
Greg Claytondbe54502010-11-19 03:46:0147 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2948 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:5149 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:2350 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:5151 m_arch (target_arch),
52 m_images (),
Greg Claytonf5e56de2010-09-14 23:36:4053 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2454 m_breakpoint_list (false),
55 m_internal_breakpoint_list (true),
Johnny Chen1d6bad02011-09-06 20:05:2556 m_watchpoint_location_list (),
Greg Clayton32e0a752011-03-30 18:16:5157 m_process_sp (),
58 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:2459 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5960 m_scratch_ast_context_ap (NULL),
Jim Ingham9575d842011-03-11 03:53:5961 m_persistent_variables (),
Jim Inghame37d6052011-09-13 00:29:5662 m_source_manager(*this),
Greg Clayton32e0a752011-03-30 18:16:5163 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:1464 m_stop_hook_next_id (0),
65 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:2466{
Greg Claytoncfd1ace2010-10-31 03:01:0667 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
68 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
69 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
70
Greg Clayton2d4edfb2010-11-06 01:53:3071 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2472 if (log)
73 log->Printf ("%p Target::Target()", this);
74}
75
76//----------------------------------------------------------------------
77// Destructor
78//----------------------------------------------------------------------
79Target::~Target()
80{
Greg Clayton2d4edfb2010-11-06 01:53:3081 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2482 if (log)
83 log->Printf ("%p Target::~Target()", this);
84 DeleteCurrentProcess ();
85}
86
87void
Caroline Ticeceb6b132010-10-26 03:11:1388Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2489{
Greg Clayton89411422010-10-08 00:21:0590// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1391 if (description_level != lldb::eDescriptionLevelBrief)
92 {
93 s->Indent();
94 s->PutCString("Target\n");
95 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:3596 m_images.Dump(s);
97 m_breakpoint_list.Dump(s);
98 m_internal_breakpoint_list.Dump(s);
99 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13100 }
101 else
102 {
Greg Claytonaa149cb2011-08-11 02:48:45103 Module *exe_module = GetExecutableModulePointer();
104 if (exe_module)
105 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28106 else
107 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13108 }
Chris Lattner30fdc8d2010-06-08 16:52:24109}
110
111void
112Target::DeleteCurrentProcess ()
113{
114 if (m_process_sp.get())
115 {
Greg Clayton17f69202010-09-14 23:52:43116 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24117 if (m_process_sp->IsAlive())
118 m_process_sp->Destroy();
Jim Inghamd0a3e12b2011-02-16 17:54:55119
120 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24121
122 // Do any cleanup of the target we need to do between process instances.
123 // NB It is better to do this before destroying the process in case the
124 // clean up needs some help from the process.
125 m_breakpoint_list.ClearAllBreakpointSites();
126 m_internal_breakpoint_list.ClearAllBreakpointSites();
127 m_process_sp.reset();
128 }
129}
130
131const lldb::ProcessSP &
132Target::CreateProcess (Listener &listener, const char *plugin_name)
133{
134 DeleteCurrentProcess ();
135 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
136 return m_process_sp;
137}
138
139const lldb::ProcessSP &
140Target::GetProcessSP () const
141{
142 return m_process_sp;
143}
144
145lldb::TargetSP
146Target::GetSP()
147{
Greg Clayton66111032010-06-23 01:19:29148 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24149}
150
Greg Clayton3418c852011-08-10 02:10:13151void
152Target::Destroy()
153{
154 Mutex::Locker locker (m_mutex);
155 DeleteCurrentProcess ();
156 m_platform_sp.reset();
157 m_arch.Clear();
158 m_images.Clear();
159 m_section_load_list.Clear();
160 const bool notify = false;
161 m_breakpoint_list.RemoveAll(notify);
162 m_internal_breakpoint_list.RemoveAll(notify);
163 m_last_created_breakpoint.reset();
164 m_search_filter_sp.reset();
165 m_image_search_paths.Clear(notify);
166 m_scratch_ast_context_ap.reset();
167 m_persistent_variables.Clear();
168 m_stop_hooks.clear();
169 m_stop_hook_next_id = 0;
170 m_suppress_stop_hooks = false;
171}
172
173
Chris Lattner30fdc8d2010-06-08 16:52:24174BreakpointList &
175Target::GetBreakpointList(bool internal)
176{
177 if (internal)
178 return m_internal_breakpoint_list;
179 else
180 return m_breakpoint_list;
181}
182
183const BreakpointList &
184Target::GetBreakpointList(bool internal) const
185{
186 if (internal)
187 return m_internal_breakpoint_list;
188 else
189 return m_breakpoint_list;
190}
191
192BreakpointSP
193Target::GetBreakpointByID (break_id_t break_id)
194{
195 BreakpointSP bp_sp;
196
197 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
198 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
199 else
200 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
201
202 return bp_sp;
203}
204
205BreakpointSP
206Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
207{
208 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
209 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
210 return CreateBreakpoint (filter_sp, resolver_sp, internal);
211}
212
213
214BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41215Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24216{
Chris Lattner30fdc8d2010-06-08 16:52:24217 Address so_addr;
218 // Attempt to resolve our load address if possible, though it is ok if
219 // it doesn't resolve to section/offset.
220
Greg Clayton1b72fcb2010-08-24 00:45:41221 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40222 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41223 if (!so_addr.IsValid())
224 {
225 // The address didn't resolve, so just set this as an absolute address
226 so_addr.SetOffset (addr);
227 }
228 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24229 return bp_sp;
230}
231
232BreakpointSP
233Target::CreateBreakpoint (Address &addr, bool internal)
234{
235 TargetSP target_sp = this->GetSP();
236 SearchFilterSP filter_sp(new SearchFilter (target_sp));
237 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
238 return CreateBreakpoint (filter_sp, resolver_sp, internal);
239}
240
241BreakpointSP
Greg Claytond16e1e52011-07-12 17:06:17242Target::CreateBreakpoint (const FileSpec *containingModule,
243 const char *func_name,
244 uint32_t func_name_type_mask,
245 bool internal,
246 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24247{
Greg Clayton0c5cd902010-06-28 21:30:43248 BreakpointSP bp_sp;
249 if (func_name)
250 {
251 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Claytond16e1e52011-07-12 17:06:17252
253 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
254 func_name,
255 func_name_type_mask,
256 Breakpoint::Exact,
257 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton0c5cd902010-06-28 21:30:43258 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
259 }
260 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24261}
262
263
264SearchFilterSP
265Target::GetSearchFilterForModule (const FileSpec *containingModule)
266{
267 SearchFilterSP filter_sp;
268 lldb::TargetSP target_sp = this->GetSP();
269 if (containingModule != NULL)
270 {
271 // TODO: We should look into sharing module based search filters
272 // across many breakpoints like we do for the simple target based one
273 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
274 }
275 else
276 {
277 if (m_search_filter_sp.get() == NULL)
278 m_search_filter_sp.reset (new SearchFilter (target_sp));
279 filter_sp = m_search_filter_sp;
280 }
281 return filter_sp;
282}
283
284BreakpointSP
Greg Claytond16e1e52011-07-12 17:06:17285Target::CreateBreakpoint (const FileSpec *containingModule,
286 RegularExpression &func_regex,
287 bool internal,
288 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24289{
290 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Claytond16e1e52011-07-12 17:06:17291 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
292 func_regex,
293 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24294
295 return CreateBreakpoint (filter_sp, resolver_sp, internal);
296}
297
298BreakpointSP
299Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
300{
301 BreakpointSP bp_sp;
302 if (filter_sp && resolver_sp)
303 {
304 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
305 resolver_sp->SetBreakpoint (bp_sp.get());
306
307 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17308 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24309 else
Greg Clayton9fed0d82010-07-23 23:33:17310 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24311
Greg Clayton2d4edfb2010-11-06 01:53:30312 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24313 if (log)
314 {
315 StreamString s;
316 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
317 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
318 }
319
Chris Lattner30fdc8d2010-06-08 16:52:24320 bp_sp->ResolveBreakpoint();
321 }
Jim Ingham36f3b362010-10-14 23:45:03322
323 if (!internal && bp_sp)
324 {
325 m_last_created_breakpoint = bp_sp;
326 }
327
Chris Lattner30fdc8d2010-06-08 16:52:24328 return bp_sp;
329}
330
Johnny Chenab9ee762011-09-14 00:26:03331// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
332// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen887062a2011-09-12 23:38:44333WatchpointLocationSP
334Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
335{
336 WatchpointLocationSP wp_loc_sp;
Johnny Chen2fd89a02011-09-13 18:30:59337 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
338 if (!process_is_valid)
339 return wp_loc_sp;
340 if (addr == LLDB_INVALID_ADDRESS)
341 return wp_loc_sp;
342 if (size == 0)
Johnny Chen887062a2011-09-12 23:38:44343 return wp_loc_sp;
Johnny Chen7313a642011-09-13 01:15:36344
Johnny Chenab9ee762011-09-14 00:26:03345 // Currently we only support one watchpoint location per address, with total
346 // number of watchpoint locations limited by the hardware which the inferior
347 // is running on.
Johnny Chen3c532582011-09-13 23:29:31348 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
349 if (matched_sp)
350 {
351 size_t old_size = wp_loc_sp->GetByteSize();
352 uint32_t old_type =
353 (wp_loc_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
354 (wp_loc_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
355 // Return an empty watchpoint location if the same one exists already.
356 if (size == old_size && type == old_type)
357 return wp_loc_sp;
358
359 // Nil the matched watchpoint location; we will be creating a new one.
360 m_process_sp->DisableWatchpoint(matched_sp.get());
361 m_watchpoint_location_list.Remove(matched_sp->GetID());
362 }
363
364 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
365 new_loc->SetWatchpointType(type);
366 wp_loc_sp.reset(new_loc);
367 m_watchpoint_location_list.Add(wp_loc_sp);
368 m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen7313a642011-09-13 01:15:36369 return wp_loc_sp;
Johnny Chen887062a2011-09-12 23:38:44370}
371
Chris Lattner30fdc8d2010-06-08 16:52:24372void
373Target::RemoveAllBreakpoints (bool internal_also)
374{
Greg Clayton2d4edfb2010-11-06 01:53:30375 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24376 if (log)
377 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
378
Greg Clayton9fed0d82010-07-23 23:33:17379 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24380 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17381 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03382
383 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24384}
385
386void
387Target::DisableAllBreakpoints (bool internal_also)
388{
Greg Clayton2d4edfb2010-11-06 01:53:30389 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24390 if (log)
391 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
392
393 m_breakpoint_list.SetEnabledAll (false);
394 if (internal_also)
395 m_internal_breakpoint_list.SetEnabledAll (false);
396}
397
398void
399Target::EnableAllBreakpoints (bool internal_also)
400{
Greg Clayton2d4edfb2010-11-06 01:53:30401 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24402 if (log)
403 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
404
405 m_breakpoint_list.SetEnabledAll (true);
406 if (internal_also)
407 m_internal_breakpoint_list.SetEnabledAll (true);
408}
409
410bool
411Target::RemoveBreakpointByID (break_id_t break_id)
412{
Greg Clayton2d4edfb2010-11-06 01:53:30413 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24414 if (log)
415 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
416
417 if (DisableBreakpointByID (break_id))
418 {
419 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17420 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24421 else
Jim Ingham36f3b362010-10-14 23:45:03422 {
Greg Claytonaa1c5872011-01-24 23:35:47423 if (m_last_created_breakpoint)
424 {
425 if (m_last_created_breakpoint->GetID() == break_id)
426 m_last_created_breakpoint.reset();
427 }
Greg Clayton9fed0d82010-07-23 23:33:17428 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03429 }
Chris Lattner30fdc8d2010-06-08 16:52:24430 return true;
431 }
432 return false;
433}
434
435bool
436Target::DisableBreakpointByID (break_id_t break_id)
437{
Greg Clayton2d4edfb2010-11-06 01:53:30438 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24439 if (log)
440 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
441
442 BreakpointSP bp_sp;
443
444 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
445 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
446 else
447 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
448 if (bp_sp)
449 {
450 bp_sp->SetEnabled (false);
451 return true;
452 }
453 return false;
454}
455
456bool
457Target::EnableBreakpointByID (break_id_t break_id)
458{
Greg Clayton2d4edfb2010-11-06 01:53:30459 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24460 if (log)
461 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
462 __FUNCTION__,
463 break_id,
464 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
465
466 BreakpointSP bp_sp;
467
468 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
469 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
470 else
471 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
472
473 if (bp_sp)
474 {
475 bp_sp->SetEnabled (true);
476 return true;
477 }
478 return false;
479}
480
481ModuleSP
482Target::GetExecutableModule ()
483{
Greg Claytonaa149cb2011-08-11 02:48:45484 return m_images.GetModuleAtIndex(0);
485}
486
487Module*
488Target::GetExecutableModulePointer ()
489{
490 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24491}
492
493void
494Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
495{
496 m_images.Clear();
497 m_scratch_ast_context_ap.reset();
498
499 if (executable_sp.get())
500 {
501 Timer scoped_timer (__PRETTY_FUNCTION__,
502 "Target::SetExecutableModule (executable = '%s/%s')",
503 executable_sp->GetFileSpec().GetDirectory().AsCString(),
504 executable_sp->GetFileSpec().GetFilename().AsCString());
505
506 m_images.Append(executable_sp); // The first image is our exectuable file
507
Jim Ingham5aee1622010-08-09 23:31:02508 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton32e0a752011-03-30 18:16:51509 if (!m_arch.IsValid())
510 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02511
Chris Lattner30fdc8d2010-06-08 16:52:24512 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15513 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamb7f6b2f2011-09-08 22:13:49514 // Let's find the file & line for main and set the default source file from there.
515 if (!m_source_manager.DefaultFileAndLineSet())
516 {
517 SymbolContextList sc_list;
518 uint32_t num_matches;
519 ConstString main_name("main");
520 bool symbols_okay = false; // Force it to be a debug symbol.
521 bool append = false;
522 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
523 for (uint32_t idx = 0; idx < num_matches; idx++)
524 {
525 SymbolContext sc;
526 sc_list.GetContextAtIndex(idx, sc);
527 if (sc.line_entry.file)
528 {
529 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
530 break;
531 }
532 }
533 }
Chris Lattner30fdc8d2010-06-08 16:52:24534
535 if (executable_objfile)
536 {
537 executable_objfile->GetDependentModules(dependent_files);
538 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
539 {
Greg Claytonded470d2011-03-19 01:12:21540 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
541 FileSpec platform_dependent_file_spec;
542 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55543 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21544 else
545 platform_dependent_file_spec = dependent_file_spec;
546
547 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton32e0a752011-03-30 18:16:51548 m_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24549 if (image_module_sp.get())
550 {
Chris Lattner30fdc8d2010-06-08 16:52:24551 ObjectFile *objfile = image_module_sp->GetObjectFile();
552 if (objfile)
553 objfile->GetDependentModules(dependent_files);
554 }
555 }
556 }
557
Chris Lattner30fdc8d2010-06-08 16:52:24558 }
Caroline Tice1559a462010-09-27 00:30:10559
560 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24561}
562
563
Jim Ingham5aee1622010-08-09 23:31:02564bool
565Target::SetArchitecture (const ArchSpec &arch_spec)
566{
Greg Clayton32e0a752011-03-30 18:16:51567 if (m_arch == arch_spec)
Jim Ingham5aee1622010-08-09 23:31:02568 {
569 // If we're setting the architecture to our current architecture, we
570 // don't need to do anything.
571 return true;
572 }
Greg Clayton32e0a752011-03-30 18:16:51573 else if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02574 {
575 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton32e0a752011-03-30 18:16:51576 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02577 return true;
578 }
579 else
580 {
581 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51582 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02583 ModuleSP executable_sp = GetExecutableModule ();
584 m_images.Clear();
585 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02586 // Need to do something about unsetting breakpoints.
587
588 if (executable_sp)
589 {
590 FileSpec exec_file_spec = executable_sp->GetFileSpec();
591 Error error = ModuleList::GetSharedModule(exec_file_spec,
592 arch_spec,
593 NULL,
594 NULL,
595 0,
596 executable_sp,
597 NULL,
598 NULL);
599
600 if (!error.Fail() && executable_sp)
601 {
602 SetExecutableModule (executable_sp, true);
603 return true;
604 }
605 else
606 {
607 return false;
608 }
609 }
610 else
611 {
612 return false;
613 }
614 }
615}
Chris Lattner30fdc8d2010-06-08 16:52:24616
Chris Lattner30fdc8d2010-06-08 16:52:24617void
618Target::ModuleAdded (ModuleSP &module_sp)
619{
620 // A module is being added to this target for the first time
621 ModuleList module_list;
622 module_list.Append(module_sp);
623 ModulesDidLoad (module_list);
624}
625
626void
627Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
628{
Jim Inghame716ae02011-08-03 01:00:06629 // A module is replacing an already added module
Chris Lattner30fdc8d2010-06-08 16:52:24630 ModuleList module_list;
631 module_list.Append (old_module_sp);
632 ModulesDidUnload (module_list);
633 module_list.Clear ();
634 module_list.Append (new_module_sp);
635 ModulesDidLoad (module_list);
636}
637
638void
639Target::ModulesDidLoad (ModuleList &module_list)
640{
641 m_breakpoint_list.UpdateBreakpoints (module_list, true);
642 // TODO: make event data that packages up the module_list
643 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
644}
645
646void
647Target::ModulesDidUnload (ModuleList &module_list)
648{
649 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26650
651 // Remove the images from the target image list
652 m_images.Remove(module_list);
653
Chris Lattner30fdc8d2010-06-08 16:52:24654 // TODO: make event data that packages up the module_list
655 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
656}
657
658size_t
Greg Claytondb598232011-01-07 01:57:07659Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
660{
661 const Section *section = addr.GetSection();
662 if (section && section->GetModule())
663 {
664 ObjectFile *objfile = section->GetModule()->GetObjectFile();
665 if (objfile)
666 {
667 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
668 addr.GetOffset(),
669 dst,
670 dst_len);
671 if (bytes_read > 0)
672 return bytes_read;
673 else
674 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
675 }
676 else
677 {
678 error.SetErrorString("address isn't from a object file");
679 }
680 }
681 else
682 {
683 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
684 }
685 return 0;
686}
687
688size_t
Enrico Granata9128ee2f2011-09-06 19:20:51689Target::ReadMemory (const Address& addr,
690 bool prefer_file_cache,
691 void *dst,
692 size_t dst_len,
693 Error &error,
694 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24695{
Chris Lattner30fdc8d2010-06-08 16:52:24696 error.Clear();
Greg Claytondb598232011-01-07 01:57:07697
Enrico Granata9128ee2f2011-09-06 19:20:51698 // if we end up reading this from process memory, we will fill this
699 // with the actual load address
700 if (load_addr_ptr)
701 *load_addr_ptr = LLDB_INVALID_ADDRESS;
702
Greg Claytondda4f7b2010-06-30 23:03:03703 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
704
Greg Claytondb598232011-01-07 01:57:07705 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02706
707 addr_t load_addr = LLDB_INVALID_ADDRESS;
708 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58709 Address resolved_addr;
710 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03711 {
Greg Claytond16e1e52011-07-12 17:06:17712 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02713 {
Greg Claytond16e1e52011-07-12 17:06:17714 // No sections are loaded, so we must assume we are not running
715 // yet and anything we are given is a file address.
716 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
717 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02718 }
Greg Claytondda4f7b2010-06-30 23:03:03719 else
Greg Claytonc749eb82011-07-11 05:12:02720 {
Greg Claytond16e1e52011-07-12 17:06:17721 // We have at least one section loaded. This can be becuase
722 // we have manually loaded some sections with "target modules load ..."
723 // or because we have have a live process that has sections loaded
724 // through the dynamic loader
725 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
726 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02727 }
Greg Claytondda4f7b2010-06-30 23:03:03728 }
Greg Clayton357132e2011-03-26 19:14:58729 if (!resolved_addr.IsValid())
730 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03731
Greg Claytonc749eb82011-07-11 05:12:02732
Greg Claytondb598232011-01-07 01:57:07733 if (prefer_file_cache)
734 {
735 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
736 if (bytes_read > 0)
737 return bytes_read;
738 }
Greg Claytondda4f7b2010-06-30 23:03:03739
740 if (process_is_valid)
741 {
Greg Claytonc749eb82011-07-11 05:12:02742 if (load_addr == LLDB_INVALID_ADDRESS)
743 load_addr = resolved_addr.GetLoadAddress (this);
744
Greg Claytondda4f7b2010-06-30 23:03:03745 if (load_addr == LLDB_INVALID_ADDRESS)
746 {
747 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
748 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
749 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
750 resolved_addr.GetFileAddress());
751 else
752 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
753 }
754 else
755 {
Greg Claytondb598232011-01-07 01:57:07756 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24757 if (bytes_read != dst_len)
758 {
759 if (error.Success())
760 {
761 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03762 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24763 else
Greg Claytondda4f7b2010-06-30 23:03:03764 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24765 }
766 }
Greg Claytondda4f7b2010-06-30 23:03:03767 if (bytes_read)
Enrico Granata9128ee2f2011-09-06 19:20:51768 {
769 if (load_addr_ptr)
770 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03771 return bytes_read;
Enrico Granata9128ee2f2011-09-06 19:20:51772 }
Greg Claytondda4f7b2010-06-30 23:03:03773 // If the address is not section offset we have an address that
774 // doesn't resolve to any address in any currently loaded shared
775 // libaries and we failed to read memory so there isn't anything
776 // more we can do. If it is section offset, we might be able to
777 // read cached memory from the object file.
778 if (!resolved_addr.IsSectionOffset())
779 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24780 }
Chris Lattner30fdc8d2010-06-08 16:52:24781 }
Greg Claytondda4f7b2010-06-30 23:03:03782
Greg Claytonc749eb82011-07-11 05:12:02783 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03784 {
Greg Claytondb598232011-01-07 01:57:07785 // If we didn't already try and read from the object file cache, then
786 // try it after failing to read from the process.
787 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03788 }
789 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24790}
791
Greg Claytond16e1e52011-07-12 17:06:17792size_t
793Target::ReadScalarIntegerFromMemory (const Address& addr,
794 bool prefer_file_cache,
795 uint32_t byte_size,
796 bool is_signed,
797 Scalar &scalar,
798 Error &error)
799{
800 uint64_t uval;
801
802 if (byte_size <= sizeof(uval))
803 {
804 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
805 if (bytes_read == byte_size)
806 {
807 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
808 uint32_t offset = 0;
809 if (byte_size <= 4)
810 scalar = data.GetMaxU32 (&offset, byte_size);
811 else
812 scalar = data.GetMaxU64 (&offset, byte_size);
813
814 if (is_signed)
815 scalar.SignExtend(byte_size * 8);
816 return bytes_read;
817 }
818 }
819 else
820 {
821 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
822 }
823 return 0;
824}
825
826uint64_t
827Target::ReadUnsignedIntegerFromMemory (const Address& addr,
828 bool prefer_file_cache,
829 size_t integer_byte_size,
830 uint64_t fail_value,
831 Error &error)
832{
833 Scalar scalar;
834 if (ReadScalarIntegerFromMemory (addr,
835 prefer_file_cache,
836 integer_byte_size,
837 false,
838 scalar,
839 error))
840 return scalar.ULongLong(fail_value);
841 return fail_value;
842}
843
844bool
845Target::ReadPointerFromMemory (const Address& addr,
846 bool prefer_file_cache,
847 Error &error,
848 Address &pointer_addr)
849{
850 Scalar scalar;
851 if (ReadScalarIntegerFromMemory (addr,
852 prefer_file_cache,
853 m_arch.GetAddressByteSize(),
854 false,
855 scalar,
856 error))
857 {
858 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
859 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
860 {
861 if (m_section_load_list.IsEmpty())
862 {
863 // No sections are loaded, so we must assume we are not running
864 // yet and anything we are given is a file address.
865 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
866 }
867 else
868 {
869 // We have at least one section loaded. This can be becuase
870 // we have manually loaded some sections with "target modules load ..."
871 // or because we have have a live process that has sections loaded
872 // through the dynamic loader
873 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
874 }
875 // We weren't able to resolve the pointer value, so just return
876 // an address with no section
877 if (!pointer_addr.IsValid())
878 pointer_addr.SetOffset (pointer_vm_addr);
879 return true;
880
881 }
882 }
883 return false;
884}
Chris Lattner30fdc8d2010-06-08 16:52:24885
886ModuleSP
887Target::GetSharedModule
888(
889 const FileSpec& file_spec,
890 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:10891 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:24892 const ConstString *object_name,
893 off_t object_offset,
894 Error *error_ptr
895)
896{
897 // Don't pass in the UUID so we can tell if we have a stale value in our list
898 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
899 bool did_create_module = false;
900 ModuleSP module_sp;
901
Chris Lattner30fdc8d2010-06-08 16:52:24902 Error error;
903
Greg Clayton32e0a752011-03-30 18:16:51904 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner30fdc8d2010-06-08 16:52:24905 if (m_image_search_paths.GetSize())
906 {
907 FileSpec transformed_spec;
908 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
909 {
910 transformed_spec.GetFilename() = file_spec.GetFilename();
911 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
912 }
913 }
914
Greg Clayton32e0a752011-03-30 18:16:51915 // The platform is responsible for finding and caching an appropriate
916 // module in the shared module cache.
917 if (m_platform_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24918 {
Greg Clayton32e0a752011-03-30 18:16:51919 FileSpec platform_file_spec;
920 error = m_platform_sp->GetSharedModule (file_spec,
921 arch,
922 uuid_ptr,
923 object_name,
924 object_offset,
925 module_sp,
926 &old_module_sp,
927 &did_create_module);
928 }
929 else
930 {
931 error.SetErrorString("no platform is currently set");
Chris Lattner30fdc8d2010-06-08 16:52:24932 }
933
Greg Clayton32e0a752011-03-30 18:16:51934 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner30fdc8d2010-06-08 16:52:24935 if (module_sp)
936 {
937 m_images.Append (module_sp);
938 if (did_create_module)
939 {
940 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
941 ModuleUpdated(old_module_sp, module_sp);
942 else
943 ModuleAdded(module_sp);
944 }
945 }
946 if (error_ptr)
947 *error_ptr = error;
948 return module_sp;
949}
950
951
952Target *
953Target::CalculateTarget ()
954{
955 return this;
956}
957
958Process *
959Target::CalculateProcess ()
960{
961 return NULL;
962}
963
964Thread *
965Target::CalculateThread ()
966{
967 return NULL;
968}
969
970StackFrame *
971Target::CalculateStackFrame ()
972{
973 return NULL;
974}
975
976void
Greg Clayton0603aa92010-10-04 01:05:56977Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24978{
979 exe_ctx.target = this;
980 exe_ctx.process = NULL; // Do NOT fill in process...
981 exe_ctx.thread = NULL;
982 exe_ctx.frame = NULL;
983}
984
985PathMappingList &
986Target::GetImageSearchPathList ()
987{
988 return m_image_search_paths;
989}
990
991void
992Target::ImageSearchPathsChanged
993(
994 const PathMappingList &path_list,
995 void *baton
996)
997{
998 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45999 ModuleSP exe_module_sp (target->GetExecutableModule());
1000 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241001 {
Greg Claytonaa149cb2011-08-11 02:48:451002 target->m_images.Clear();
1003 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:241004 }
1005}
1006
1007ClangASTContext *
1008Target::GetScratchClangASTContext()
1009{
Greg Clayton73da2442011-08-03 01:23:551010 // Now see if we know the target triple, and if so, create our scratch AST context:
1011 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1012 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:241013 return m_scratch_ast_context_ap.get();
1014}
Caroline Ticedaccaa92010-09-20 20:44:431015
Greg Clayton99d0faf2010-11-18 23:32:351016void
Caroline Tice20bd37f2011-03-10 22:14:101017Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:431018{
Greg Clayton99d0faf2010-11-18 23:32:351019 UserSettingsControllerSP &usc = GetSettingsController();
1020 usc.reset (new SettingsController);
1021 UserSettingsController::InitializeSettingsController (usc,
1022 SettingsController::global_settings_table,
1023 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:101024
1025 // Now call SettingsInitialize() on each 'child' setting of Target
1026 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:351027}
Caroline Ticedaccaa92010-09-20 20:44:431028
Greg Clayton99d0faf2010-11-18 23:32:351029void
Caroline Tice20bd37f2011-03-10 22:14:101030Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:351031{
Caroline Tice20bd37f2011-03-10 22:14:101032
1033 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1034
1035 Process::SettingsTerminate ();
1036
1037 // Now terminate Target Settings.
1038
Greg Clayton99d0faf2010-11-18 23:32:351039 UserSettingsControllerSP &usc = GetSettingsController();
1040 UserSettingsController::FinalizeSettingsController (usc);
1041 usc.reset();
1042}
Caroline Ticedaccaa92010-09-20 20:44:431043
Greg Clayton99d0faf2010-11-18 23:32:351044UserSettingsControllerSP &
1045Target::GetSettingsController ()
1046{
1047 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:431048 return g_settings_controller;
1049}
1050
1051ArchSpec
1052Target::GetDefaultArchitecture ()
1053{
Greg Clayton64195a22011-02-23 00:35:021054 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1055
1056 if (settings_controller_sp)
1057 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1058 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:431059}
1060
1061void
Greg Clayton64195a22011-02-23 00:35:021062Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:431063{
Greg Clayton64195a22011-02-23 00:35:021064 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1065
1066 if (settings_controller_sp)
1067 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:431068}
1069
Greg Clayton0603aa92010-10-04 01:05:561070Target *
1071Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1072{
1073 // The target can either exist in the "process" of ExecutionContext, or in
1074 // the "target_sp" member of SymbolContext. This accessor helper function
1075 // will get the target from one of these locations.
1076
1077 Target *target = NULL;
1078 if (sc_ptr != NULL)
1079 target = sc_ptr->target_sp.get();
1080 if (target == NULL)
1081 {
1082 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
1083 target = &exe_ctx_ptr->process->GetTarget();
1084 }
1085 return target;
1086}
1087
1088
Caroline Tice1559a462010-09-27 00:30:101089void
1090Target::UpdateInstanceName ()
1091{
1092 StreamString sstr;
1093
Greg Claytonaa149cb2011-08-11 02:48:451094 Module *exe_module = GetExecutableModulePointer();
1095 if (exe_module)
Caroline Tice1559a462010-09-27 00:30:101096 {
Greg Clayton307de252010-10-27 02:06:371097 sstr.Printf ("%s_%s",
Greg Claytonaa149cb2011-08-11 02:48:451098 exe_module->GetFileSpec().GetFilename().AsCString(),
1099 exe_module->GetArchitecture().GetArchitectureName());
1100 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:101101 }
1102}
1103
Sean Callanan322f5292010-10-29 00:29:031104const char *
1105Target::GetExpressionPrefixContentsAsCString ()
1106{
Greg Clayton7e14f912011-04-23 02:04:551107 if (m_expr_prefix_contents_sp)
1108 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1109 return NULL;
Sean Callanan322f5292010-10-29 00:29:031110}
1111
Greg Clayton8b2fe6d2010-12-14 02:59:591112ExecutionResults
1113Target::EvaluateExpression
1114(
1115 const char *expr_cstr,
1116 StackFrame *frame,
1117 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:351118 bool keep_in_memory,
Jim Ingham2837b762011-05-04 03:43:181119 lldb::DynamicValueType use_dynamic,
Greg Clayton8b2fe6d2010-12-14 02:59:591120 lldb::ValueObjectSP &result_valobj_sp
1121)
1122{
1123 ExecutionResults execution_results = eExecutionSetupError;
1124
1125 result_valobj_sp.reset();
Jim Ingham6026ca32011-05-12 02:06:141126
1127 // We shouldn't run stop hooks in expressions.
1128 // Be sure to reset this if you return anywhere within this function.
1129 bool old_suppress_value = m_suppress_stop_hooks;
1130 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:591131
1132 ExecutionContext exe_ctx;
1133 if (frame)
1134 {
1135 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:081136 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:181137 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granata27b625e2011-08-09 01:04:561138 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1139 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham2837b762011-05-04 03:43:181140 lldb::VariableSP var_sp;
1141 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1142 use_dynamic,
1143 expr_path_options,
1144 var_sp,
1145 error);
Greg Clayton8b2fe6d2010-12-14 02:59:591146 }
1147 else if (m_process_sp)
1148 {
1149 m_process_sp->CalculateExecutionContext(exe_ctx);
1150 }
1151 else
1152 {
1153 CalculateExecutionContext(exe_ctx);
1154 }
1155
1156 if (result_valobj_sp)
1157 {
1158 execution_results = eExecutionCompleted;
1159 // We got a result from the frame variable expression path above...
1160 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1161
1162 lldb::ValueObjectSP const_valobj_sp;
1163
1164 // Check in case our value is already a constant value
1165 if (result_valobj_sp->GetIsConstant())
1166 {
1167 const_valobj_sp = result_valobj_sp;
1168 const_valobj_sp->SetName (persistent_variable_name);
1169 }
1170 else
Jim Ingham78a685a2011-04-16 00:01:131171 {
Jim Ingham2837b762011-05-04 03:43:181172 if (use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:131173 {
Jim Ingham2837b762011-05-04 03:43:181174 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:131175 if (dynamic_sp)
1176 result_valobj_sp = dynamic_sp;
1177 }
1178
Jim Ingham6035b672011-03-31 00:19:251179 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:131180 }
Greg Clayton8b2fe6d2010-12-14 02:59:591181
Sean Callanan92adcac2011-01-13 08:53:351182 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1183
Greg Clayton8b2fe6d2010-12-14 02:59:591184 result_valobj_sp = const_valobj_sp;
1185
Sean Callanan92adcac2011-01-13 08:53:351186 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1187 assert (clang_expr_variable_sp.get());
1188
1189 // Set flags and live data as appropriate
1190
1191 const Value &result_value = live_valobj_sp->GetValue();
1192
1193 switch (result_value.GetValueType())
1194 {
1195 case Value::eValueTypeHostAddress:
1196 case Value::eValueTypeFileAddress:
1197 // we don't do anything with these for now
1198 break;
1199 case Value::eValueTypeScalar:
1200 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1201 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1202 break;
1203 case Value::eValueTypeLoadAddress:
1204 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1205 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1206 break;
1207 }
Greg Clayton8b2fe6d2010-12-14 02:59:591208 }
1209 else
1210 {
1211 // Make sure we aren't just trying to see the value of a persistent
1212 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:351213 lldb::ClangExpressionVariableSP persistent_var_sp;
1214 // Only check for persistent variables the expression starts with a '$'
1215 if (expr_cstr[0] == '$')
1216 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1217
Greg Clayton8b2fe6d2010-12-14 02:59:591218 if (persistent_var_sp)
1219 {
1220 result_valobj_sp = persistent_var_sp->GetValueObject ();
1221 execution_results = eExecutionCompleted;
1222 }
1223 else
1224 {
1225 const char *prefix = GetExpressionPrefixContentsAsCString();
1226
1227 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:351228 unwind_on_error,
Greg Clayton8b2fe6d2010-12-14 02:59:591229 expr_cstr,
1230 prefix,
1231 result_valobj_sp);
1232 }
1233 }
Jim Ingham6026ca32011-05-12 02:06:141234
1235 m_suppress_stop_hooks = old_suppress_value;
1236
Greg Clayton8b2fe6d2010-12-14 02:59:591237 return execution_results;
1238}
1239
Greg Claytonf3ef3d22011-05-22 22:46:531240lldb::addr_t
1241Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1242{
1243 addr_t code_addr = load_addr;
1244 switch (m_arch.GetMachine())
1245 {
1246 case llvm::Triple::arm:
1247 case llvm::Triple::thumb:
1248 switch (addr_class)
1249 {
1250 case eAddressClassData:
1251 case eAddressClassDebug:
1252 return LLDB_INVALID_ADDRESS;
1253
1254 case eAddressClassUnknown:
1255 case eAddressClassInvalid:
1256 case eAddressClassCode:
1257 case eAddressClassCodeAlternateISA:
1258 case eAddressClassRuntime:
1259 // Check if bit zero it no set?
1260 if ((code_addr & 1ull) == 0)
1261 {
1262 // Bit zero isn't set, check if the address is a multiple of 2?
1263 if (code_addr & 2ull)
1264 {
1265 // The address is a multiple of 2 so it must be thumb, set bit zero
1266 code_addr |= 1ull;
1267 }
1268 else if (addr_class == eAddressClassCodeAlternateISA)
1269 {
1270 // We checked the address and the address claims to be the alternate ISA
1271 // which means thumb, so set bit zero.
1272 code_addr |= 1ull;
1273 }
1274 }
1275 break;
1276 }
1277 break;
1278
1279 default:
1280 break;
1281 }
1282 return code_addr;
1283}
1284
1285lldb::addr_t
1286Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1287{
1288 addr_t opcode_addr = load_addr;
1289 switch (m_arch.GetMachine())
1290 {
1291 case llvm::Triple::arm:
1292 case llvm::Triple::thumb:
1293 switch (addr_class)
1294 {
1295 case eAddressClassData:
1296 case eAddressClassDebug:
1297 return LLDB_INVALID_ADDRESS;
1298
1299 case eAddressClassInvalid:
1300 case eAddressClassUnknown:
1301 case eAddressClassCode:
1302 case eAddressClassCodeAlternateISA:
1303 case eAddressClassRuntime:
1304 opcode_addr &= ~(1ull);
1305 break;
1306 }
1307 break;
1308
1309 default:
1310 break;
1311 }
1312 return opcode_addr;
1313}
1314
Jim Ingham9575d842011-03-11 03:53:591315lldb::user_id_t
1316Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1317{
1318 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1319 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1320 m_stop_hooks[new_uid] = new_hook_sp;
1321 return new_uid;
1322}
1323
1324bool
1325Target::RemoveStopHookByID (lldb::user_id_t user_id)
1326{
1327 size_t num_removed;
1328 num_removed = m_stop_hooks.erase (user_id);
1329 if (num_removed == 0)
1330 return false;
1331 else
1332 return true;
1333}
1334
1335void
1336Target::RemoveAllStopHooks ()
1337{
1338 m_stop_hooks.clear();
1339}
1340
1341Target::StopHookSP
1342Target::GetStopHookByID (lldb::user_id_t user_id)
1343{
1344 StopHookSP found_hook;
1345
1346 StopHookCollection::iterator specified_hook_iter;
1347 specified_hook_iter = m_stop_hooks.find (user_id);
1348 if (specified_hook_iter != m_stop_hooks.end())
1349 found_hook = (*specified_hook_iter).second;
1350 return found_hook;
1351}
1352
1353bool
1354Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1355{
1356 StopHookCollection::iterator specified_hook_iter;
1357 specified_hook_iter = m_stop_hooks.find (user_id);
1358 if (specified_hook_iter == m_stop_hooks.end())
1359 return false;
1360
1361 (*specified_hook_iter).second->SetIsActive (active_state);
1362 return true;
1363}
1364
1365void
1366Target::SetAllStopHooksActiveState (bool active_state)
1367{
1368 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1369 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1370 {
1371 (*pos).second->SetIsActive (active_state);
1372 }
1373}
1374
1375void
1376Target::RunStopHooks ()
1377{
Jim Ingham6026ca32011-05-12 02:06:141378 if (m_suppress_stop_hooks)
1379 return;
1380
Jim Ingham9575d842011-03-11 03:53:591381 if (!m_process_sp)
1382 return;
1383
1384 if (m_stop_hooks.empty())
1385 return;
1386
1387 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1388
1389 // If there aren't any active stop hooks, don't bother either:
1390 bool any_active_hooks = false;
1391 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1392 {
1393 if ((*pos).second->IsActive())
1394 {
1395 any_active_hooks = true;
1396 break;
1397 }
1398 }
1399 if (!any_active_hooks)
1400 return;
1401
1402 CommandReturnObject result;
1403
1404 std::vector<ExecutionContext> exc_ctx_with_reasons;
1405 std::vector<SymbolContext> sym_ctx_with_reasons;
1406
1407 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1408 size_t num_threads = cur_threadlist.GetSize();
1409 for (size_t i = 0; i < num_threads; i++)
1410 {
1411 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1412 if (cur_thread_sp->ThreadStoppedForAReason())
1413 {
1414 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1415 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1416 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1417 }
1418 }
1419
1420 // If no threads stopped for a reason, don't run the stop-hooks.
1421 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1422 if (num_exe_ctx == 0)
1423 return;
1424
Jim Ingham5b52f0c2011-06-02 23:58:261425 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1426 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:591427
1428 bool keep_going = true;
1429 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271430 bool print_hook_header;
1431 bool print_thread_header;
1432
1433 if (num_exe_ctx == 1)
1434 print_thread_header = false;
1435 else
1436 print_thread_header = true;
1437
1438 if (m_stop_hooks.size() == 1)
1439 print_hook_header = false;
1440 else
1441 print_hook_header = true;
1442
Jim Ingham9575d842011-03-11 03:53:591443 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1444 {
1445 // result.Clear();
1446 StopHookSP cur_hook_sp = (*pos).second;
1447 if (!cur_hook_sp->IsActive())
1448 continue;
1449
1450 bool any_thread_matched = false;
1451 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1452 {
1453 if ((cur_hook_sp->GetSpecifier () == NULL
1454 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1455 && (cur_hook_sp->GetThreadSpecifier() == NULL
1456 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1457 {
1458 if (!hooks_ran)
1459 {
Jim Ingham381e25b2011-03-22 01:47:271460 result.AppendMessage("\n** Stop Hooks **");
Jim Ingham9575d842011-03-11 03:53:591461 hooks_ran = true;
1462 }
Jim Ingham381e25b2011-03-22 01:47:271463 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591464 {
1465 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1466 any_thread_matched = true;
1467 }
1468
Jim Ingham381e25b2011-03-22 01:47:271469 if (print_thread_header)
1470 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591471
1472 bool stop_on_continue = true;
1473 bool stop_on_error = true;
1474 bool echo_commands = false;
1475 bool print_results = true;
1476 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:511477 &exc_ctx_with_reasons[i],
1478 stop_on_continue,
1479 stop_on_error,
1480 echo_commands,
1481 print_results,
1482 result);
Jim Ingham9575d842011-03-11 03:53:591483
1484 // If the command started the target going again, we should bag out of
1485 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:511486 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1487 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:591488 {
1489 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1490 keep_going = false;
1491 }
1492 }
1493 }
1494 }
1495 if (hooks_ran)
1496 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice969ed3d12011-05-02 20:41:461497
1498 result.GetImmediateOutputStream()->Flush();
1499 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:591500}
1501
Greg Clayton7b242382011-07-08 00:48:091502bool
1503Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1504{
1505 bool changed = false;
1506 if (module)
1507 {
1508 ObjectFile *object_file = module->GetObjectFile();
1509 if (object_file)
1510 {
1511 SectionList *section_list = object_file->GetSectionList ();
1512 if (section_list)
1513 {
1514 // All sections listed in the dyld image info structure will all
1515 // either be fixed up already, or they will all be off by a single
1516 // slide amount that is determined by finding the first segment
1517 // that is at file offset zero which also has bytes (a file size
1518 // that is greater than zero) in the object file.
1519
1520 // Determine the slide amount (if any)
1521 const size_t num_sections = section_list->GetSize();
1522 size_t sect_idx = 0;
1523 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1524 {
1525 // Iterate through the object file sections to find the
1526 // first section that starts of file offset zero and that
1527 // has bytes in the file...
1528 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1529 if (section)
1530 {
1531 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1532 changed = true;
1533 }
1534 }
1535 }
1536 }
1537 }
1538 return changed;
1539}
1540
1541
Jim Ingham9575d842011-03-11 03:53:591542//--------------------------------------------------------------
1543// class Target::StopHook
1544//--------------------------------------------------------------
1545
1546
1547Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1548 UserID (uid),
1549 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:591550 m_commands (),
1551 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:401552 m_thread_spec_ap(NULL),
1553 m_active (true)
Jim Ingham9575d842011-03-11 03:53:591554{
1555}
1556
1557Target::StopHook::StopHook (const StopHook &rhs) :
1558 UserID (rhs.GetID()),
1559 m_target_sp (rhs.m_target_sp),
1560 m_commands (rhs.m_commands),
1561 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:401562 m_thread_spec_ap (NULL),
1563 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:591564{
1565 if (rhs.m_thread_spec_ap.get() != NULL)
1566 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1567}
1568
1569
1570Target::StopHook::~StopHook ()
1571{
1572}
1573
1574void
1575Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1576{
1577 m_thread_spec_ap.reset (specifier);
1578}
1579
1580
1581void
1582Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1583{
1584 int indent_level = s->GetIndentLevel();
1585
1586 s->SetIndentLevel(indent_level + 2);
1587
1588 s->Printf ("Hook: %d\n", GetID());
1589 if (m_active)
1590 s->Indent ("State: enabled\n");
1591 else
1592 s->Indent ("State: disabled\n");
1593
1594 if (m_specifier_sp)
1595 {
1596 s->Indent();
1597 s->PutCString ("Specifier:\n");
1598 s->SetIndentLevel (indent_level + 4);
1599 m_specifier_sp->GetDescription (s, level);
1600 s->SetIndentLevel (indent_level + 2);
1601 }
1602
1603 if (m_thread_spec_ap.get() != NULL)
1604 {
1605 StreamString tmp;
1606 s->Indent("Thread:\n");
1607 m_thread_spec_ap->GetDescription (&tmp, level);
1608 s->SetIndentLevel (indent_level + 4);
1609 s->Indent (tmp.GetData());
1610 s->PutCString ("\n");
1611 s->SetIndentLevel (indent_level + 2);
1612 }
1613
1614 s->Indent ("Commands: \n");
1615 s->SetIndentLevel (indent_level + 4);
1616 uint32_t num_commands = m_commands.GetSize();
1617 for (uint32_t i = 0; i < num_commands; i++)
1618 {
1619 s->Indent(m_commands.GetStringAtIndex(i));
1620 s->PutCString ("\n");
1621 }
1622 s->SetIndentLevel (indent_level);
1623}
1624
1625
Caroline Ticedaccaa92010-09-20 20:44:431626//--------------------------------------------------------------
1627// class Target::SettingsController
1628//--------------------------------------------------------------
1629
1630Target::SettingsController::SettingsController () :
1631 UserSettingsController ("target", Debugger::GetSettingsController()),
1632 m_default_architecture ()
1633{
1634 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1635 InstanceSettings::GetDefaultName().AsCString()));
1636}
1637
1638Target::SettingsController::~SettingsController ()
1639{
1640}
1641
1642lldb::InstanceSettingsSP
1643Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1644{
Greg Claytondbe54502010-11-19 03:46:011645 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1646 false,
1647 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431648 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1649 return new_settings_sp;
1650}
1651
Caroline Ticedaccaa92010-09-20 20:44:431652
Jim Ingham78a685a2011-04-16 00:01:131653#define TSC_DEFAULT_ARCH "default-arch"
1654#define TSC_EXPR_PREFIX "expr-prefix"
Jim Ingham78a685a2011-04-16 00:01:131655#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton385aa282011-04-22 03:55:061656#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Clayton7e14f912011-04-23 02:04:551657#define TSC_SOURCE_MAP "source-map"
Enrico Granata22c55d12011-08-12 02:00:061658#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata9128ee2f2011-09-06 19:20:511659#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytonbfe5f3b2011-02-18 01:44:251660
1661
1662static const ConstString &
1663GetSettingNameForDefaultArch ()
1664{
1665 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytonbfe5f3b2011-02-18 01:44:251666 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431667}
1668
Greg Claytonbfe5f3b2011-02-18 01:44:251669static const ConstString &
1670GetSettingNameForExpressionPrefix ()
1671{
1672 static ConstString g_const_string (TSC_EXPR_PREFIX);
1673 return g_const_string;
1674}
1675
1676static const ConstString &
Jim Ingham78a685a2011-04-16 00:01:131677GetSettingNameForPreferDynamicValue ()
1678{
1679 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1680 return g_const_string;
1681}
1682
Greg Clayton7e14f912011-04-23 02:04:551683static const ConstString &
1684GetSettingNameForSourcePathMap ()
1685{
1686 static ConstString g_const_string (TSC_SOURCE_MAP);
1687 return g_const_string;
1688}
Greg Claytonbfe5f3b2011-02-18 01:44:251689
Greg Clayton385aa282011-04-22 03:55:061690static const ConstString &
1691GetSettingNameForSkipPrologue ()
1692{
1693 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1694 return g_const_string;
1695}
1696
Enrico Granata22c55d12011-08-12 02:00:061697static const ConstString &
1698GetSettingNameForMaxChildren ()
1699{
1700 static ConstString g_const_string (TSC_MAX_CHILDREN);
1701 return g_const_string;
1702}
Greg Clayton385aa282011-04-22 03:55:061703
Enrico Granata9128ee2f2011-09-06 19:20:511704static const ConstString &
1705GetSettingNameForMaxStringSummaryLength ()
1706{
1707 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1708 return g_const_string;
1709}
Greg Clayton385aa282011-04-22 03:55:061710
Caroline Ticedaccaa92010-09-20 20:44:431711bool
1712Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1713 const char *index_value,
1714 const char *value,
1715 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541716 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431717 Error&err)
1718{
Greg Claytonbfe5f3b2011-02-18 01:44:251719 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431720 {
Greg Claytoneb0103f2011-04-07 22:46:351721 m_default_architecture.SetTriple (value, NULL);
Greg Clayton64195a22011-02-23 00:35:021722 if (!m_default_architecture.IsValid())
1723 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431724 }
1725 return true;
1726}
1727
1728
1729bool
1730Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1731 StringList &value,
1732 Error &err)
1733{
Greg Claytonbfe5f3b2011-02-18 01:44:251734 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431735 {
Greg Clayton307de252010-10-27 02:06:371736 // If the arch is invalid (the default), don't show a string for it
1737 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021738 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431739 return true;
1740 }
1741 else
1742 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1743
1744 return false;
1745}
1746
1747//--------------------------------------------------------------
1748// class TargetInstanceSettings
1749//--------------------------------------------------------------
1750
Greg Clayton85851dd2010-12-04 00:10:171751TargetInstanceSettings::TargetInstanceSettings
1752(
1753 UserSettingsController &owner,
1754 bool live_instance,
1755 const char *name
1756) :
Greg Claytonbfe5f3b2011-02-18 01:44:251757 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Clayton7e14f912011-04-23 02:04:551758 m_expr_prefix_file (),
1759 m_expr_prefix_contents_sp (),
Jim Ingham2837b762011-05-04 03:43:181760 m_prefer_dynamic_value (2),
Greg Clayton7e14f912011-04-23 02:04:551761 m_skip_prologue (true, true),
Enrico Granata22c55d12011-08-12 02:00:061762 m_source_map (NULL, NULL),
Enrico Granata9128ee2f2011-09-06 19:20:511763 m_max_children_display(256),
1764 m_max_strlen_length(1024)
Caroline Ticedaccaa92010-09-20 20:44:431765{
1766 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1767 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1768 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1769 // This is true for CreateInstanceName() too.
1770
1771 if (GetInstanceName () == InstanceSettings::InvalidName())
1772 {
1773 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1774 m_owner.RegisterInstanceSettings (this);
1775 }
1776
1777 if (live_instance)
1778 {
1779 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1780 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:431781 }
1782}
1783
1784TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton7e14f912011-04-23 02:04:551785 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1786 m_expr_prefix_file (rhs.m_expr_prefix_file),
1787 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1788 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1789 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata22c55d12011-08-12 02:00:061790 m_source_map (rhs.m_source_map),
Enrico Granata9128ee2f2011-09-06 19:20:511791 m_max_children_display(rhs.m_max_children_display),
1792 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Ticedaccaa92010-09-20 20:44:431793{
1794 if (m_instance_name != InstanceSettings::GetDefaultName())
1795 {
1796 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1797 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:431798 }
1799}
1800
1801TargetInstanceSettings::~TargetInstanceSettings ()
1802{
1803}
1804
1805TargetInstanceSettings&
1806TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1807{
1808 if (this != &rhs)
1809 {
1810 }
1811
1812 return *this;
1813}
1814
Caroline Ticedaccaa92010-09-20 20:44:431815void
1816TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1817 const char *index_value,
1818 const char *value,
1819 const ConstString &instance_name,
1820 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541821 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431822 Error &err,
1823 bool pending)
1824{
Greg Claytonbfe5f3b2011-02-18 01:44:251825 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031826 {
Greg Clayton7e14f912011-04-23 02:04:551827 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1828 if (err.Success())
Sean Callanan322f5292010-10-29 00:29:031829 {
Greg Clayton7e14f912011-04-23 02:04:551830 switch (op)
Sean Callanan322f5292010-10-29 00:29:031831 {
Greg Clayton7e14f912011-04-23 02:04:551832 default:
1833 break;
1834 case eVarSetOperationAssign:
1835 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:031836 {
Greg Clayton7e14f912011-04-23 02:04:551837 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1838 {
1839 err.SetErrorToGenericError ();
1840 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1841 return;
1842 }
1843
1844 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1845
1846 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1847 {
1848 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1849 m_expr_prefix_contents_sp.reset();
1850 }
Sean Callanan322f5292010-10-29 00:29:031851 }
Greg Clayton7e14f912011-04-23 02:04:551852 break;
1853 case eVarSetOperationClear:
1854 m_expr_prefix_contents_sp.reset();
Sean Callanan322f5292010-10-29 00:29:031855 }
Sean Callanan322f5292010-10-29 00:29:031856 }
1857 }
Jim Ingham78a685a2011-04-16 00:01:131858 else if (var_name == GetSettingNameForPreferDynamicValue())
1859 {
Jim Ingham2837b762011-05-04 03:43:181860 int new_value;
1861 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1862 if (err.Success())
1863 m_prefer_dynamic_value = new_value;
Greg Clayton385aa282011-04-22 03:55:061864 }
1865 else if (var_name == GetSettingNameForSkipPrologue())
1866 {
Greg Clayton7e14f912011-04-23 02:04:551867 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1868 }
Enrico Granata22c55d12011-08-12 02:00:061869 else if (var_name == GetSettingNameForMaxChildren())
1870 {
1871 bool ok;
1872 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1873 if (ok)
1874 m_max_children_display = new_value;
1875 }
Enrico Granata9128ee2f2011-09-06 19:20:511876 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1877 {
1878 bool ok;
1879 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1880 if (ok)
1881 m_max_strlen_length = new_value;
1882 }
Greg Clayton7e14f912011-04-23 02:04:551883 else if (var_name == GetSettingNameForSourcePathMap ())
1884 {
1885 switch (op)
1886 {
1887 case eVarSetOperationReplace:
1888 case eVarSetOperationInsertBefore:
1889 case eVarSetOperationInsertAfter:
1890 case eVarSetOperationRemove:
1891 default:
1892 break;
1893 case eVarSetOperationAssign:
1894 m_source_map.Clear(true);
1895 // Fall through to append....
1896 case eVarSetOperationAppend:
1897 {
1898 Args args(value);
1899 const uint32_t argc = args.GetArgumentCount();
1900 if (argc & 1 || argc == 0)
1901 {
1902 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1903 }
1904 else
1905 {
1906 char resolved_new_path[PATH_MAX];
1907 FileSpec file_spec;
1908 const char *old_path;
1909 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1910 {
1911 const char *new_path = args.GetArgumentAtIndex(idx+1);
1912 assert (new_path); // We have an even number of paths, this shouldn't happen!
1913
1914 file_spec.SetFile(new_path, true);
1915 if (file_spec.Exists())
1916 {
1917 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1918 {
1919 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1920 return;
1921 }
1922 }
1923 else
1924 {
1925 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1926 return;
1927 }
1928 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1929 }
1930 }
1931 }
1932 break;
1933
1934 case eVarSetOperationClear:
1935 m_source_map.Clear(true);
1936 break;
1937 }
Jim Ingham78a685a2011-04-16 00:01:131938 }
Caroline Ticedaccaa92010-09-20 20:44:431939}
1940
1941void
Greg Claytonbfe5f3b2011-02-18 01:44:251942TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431943{
Sean Callanan322f5292010-10-29 00:29:031944 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1945
1946 if (!new_settings_ptr)
1947 return;
1948
Greg Clayton7e14f912011-04-23 02:04:551949 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1950 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1951 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1952 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata22c55d12011-08-12 02:00:061953 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata9128ee2f2011-09-06 19:20:511954 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Ticedaccaa92010-09-20 20:44:431955}
1956
Caroline Tice12cecd72010-09-20 21:37:421957bool
Caroline Ticedaccaa92010-09-20 20:44:431958TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1959 const ConstString &var_name,
1960 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421961 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431962{
Greg Claytonbfe5f3b2011-02-18 01:44:251963 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031964 {
Greg Clayton7e14f912011-04-23 02:04:551965 char path[PATH_MAX];
1966 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1967 if (path_len > 0)
1968 value.AppendString (path, path_len);
Sean Callanan322f5292010-10-29 00:29:031969 }
Jim Ingham78a685a2011-04-16 00:01:131970 else if (var_name == GetSettingNameForPreferDynamicValue())
1971 {
Jim Ingham2837b762011-05-04 03:43:181972 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Ingham78a685a2011-04-16 00:01:131973 }
Greg Clayton385aa282011-04-22 03:55:061974 else if (var_name == GetSettingNameForSkipPrologue())
1975 {
1976 if (m_skip_prologue)
1977 value.AppendString ("true");
1978 else
1979 value.AppendString ("false");
1980 }
Greg Clayton7e14f912011-04-23 02:04:551981 else if (var_name == GetSettingNameForSourcePathMap ())
1982 {
1983 }
Enrico Granata22c55d12011-08-12 02:00:061984 else if (var_name == GetSettingNameForMaxChildren())
1985 {
1986 StreamString count_str;
1987 count_str.Printf ("%d", m_max_children_display);
1988 value.AppendString (count_str.GetData());
1989 }
Enrico Granata9128ee2f2011-09-06 19:20:511990 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1991 {
1992 StreamString count_str;
1993 count_str.Printf ("%d", m_max_strlen_length);
1994 value.AppendString (count_str.GetData());
1995 }
Sean Callanan322f5292010-10-29 00:29:031996 else
1997 {
1998 if (err)
1999 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2000 return false;
2001 }
2002
2003 return true;
Caroline Ticedaccaa92010-09-20 20:44:432004}
2005
2006const ConstString
2007TargetInstanceSettings::CreateInstanceName ()
2008{
Caroline Ticedaccaa92010-09-20 20:44:432009 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:102010 static int instance_count = 1;
2011
Caroline Ticedaccaa92010-09-20 20:44:432012 sstr.Printf ("target_%d", instance_count);
2013 ++instance_count;
2014
2015 const ConstString ret_val (sstr.GetData());
2016 return ret_val;
2017}
2018
2019//--------------------------------------------------
2020// Target::SettingsController Variable Tables
2021//--------------------------------------------------
Jim Ingham2837b762011-05-04 03:43:182022OptionEnumValueElement
2023TargetInstanceSettings::g_dynamic_value_types[] =
2024{
Greg Clayton5d2fbfe2011-05-30 00:39:482025{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2026{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2027{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham2837b762011-05-04 03:43:182028{ 0, NULL, NULL }
2029};
Caroline Ticedaccaa92010-09-20 20:44:432030
2031SettingEntry
2032Target::SettingsController::global_settings_table[] =
2033{
Greg Claytonbfe5f3b2011-02-18 01:44:252034 // var-name var-type default enum init'd hidden help-text
2035 // ================= ================== =========== ==== ====== ====== =========================================================================
2036 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2037 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2038};
2039
Caroline Ticedaccaa92010-09-20 20:44:432040SettingEntry
2041Target::SettingsController::instance_settings_table[] =
2042{
Enrico Granata9128ee2f2011-09-06 19:20:512043 // var-name var-type default enum init'd hidden help-text
2044 // ================= ================== =============== ======================= ====== ====== =========================================================================
2045 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2046 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2047 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2048 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2049 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2050 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2051 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:432052};