blob: 6a742c28e598d2acc41b5f12ef5a4b59f48ed90d [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"
Jim Ingham969795f2011-09-21 01:17:1319#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:2420#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen5d043462011-09-26 22:40:5021#include "lldb/Breakpoint/WatchpointLocation.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5922#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2423#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Caroline Tice969ed3d12011-05-02 20:41:4625#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner30fdc8d2010-06-08 16:52:2426#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5927#include "lldb/Core/Timer.h"
28#include "lldb/Core/ValueObject.h"
Greg Claytoneb0103f2011-04-07 22:46:3529#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:2430#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:5931#include "lldb/Interpreter/CommandInterpreter.h"
32#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2433#include "lldb/lldb-private-log.h"
34#include "lldb/Symbol/ObjectFile.h"
35#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5936#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:5937#include "lldb/Target/Thread.h"
38#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:2439
40using namespace lldb;
41using namespace lldb_private;
42
43//----------------------------------------------------------------------
44// Target constructor
45//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:5146Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
47 Broadcaster ("lldb.target"),
48 ExecutionContextScope (),
Greg Claytondbe54502010-11-19 03:46:0149 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2950 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:5151 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:2352 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:5153 m_arch (target_arch),
54 m_images (),
Greg Claytonf5e56de2010-09-14 23:36:4055 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2456 m_breakpoint_list (false),
57 m_internal_breakpoint_list (true),
Johnny Chen1d6bad02011-09-06 20:05:2558 m_watchpoint_location_list (),
Greg Clayton32e0a752011-03-30 18:16:5159 m_process_sp (),
60 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:2461 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5962 m_scratch_ast_context_ap (NULL),
Jim Ingham9575d842011-03-11 03:53:5963 m_persistent_variables (),
Jim Inghame37d6052011-09-13 00:29:5664 m_source_manager(*this),
Greg Clayton32e0a752011-03-30 18:16:5165 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:1466 m_stop_hook_next_id (0),
67 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:2468{
Greg Claytoncfd1ace2010-10-31 03:01:0669 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
70 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
71 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
72
Greg Clayton2d4edfb2010-11-06 01:53:3073 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2474 if (log)
75 log->Printf ("%p Target::Target()", this);
76}
77
78//----------------------------------------------------------------------
79// Destructor
80//----------------------------------------------------------------------
81Target::~Target()
82{
Greg Clayton2d4edfb2010-11-06 01:53:3083 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2484 if (log)
85 log->Printf ("%p Target::~Target()", this);
86 DeleteCurrentProcess ();
87}
88
89void
Caroline Ticeceb6b132010-10-26 03:11:1390Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2491{
Greg Clayton89411422010-10-08 00:21:0592// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1393 if (description_level != lldb::eDescriptionLevelBrief)
94 {
95 s->Indent();
96 s->PutCString("Target\n");
97 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:3598 m_images.Dump(s);
99 m_breakpoint_list.Dump(s);
100 m_internal_breakpoint_list.Dump(s);
101 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13102 }
103 else
104 {
Greg Claytonaa149cb2011-08-11 02:48:45105 Module *exe_module = GetExecutableModulePointer();
106 if (exe_module)
107 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28108 else
109 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13110 }
Chris Lattner30fdc8d2010-06-08 16:52:24111}
112
113void
114Target::DeleteCurrentProcess ()
115{
116 if (m_process_sp.get())
117 {
Greg Clayton17f69202010-09-14 23:52:43118 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24119 if (m_process_sp->IsAlive())
120 m_process_sp->Destroy();
Jim Inghamd0a3e12b2011-02-16 17:54:55121
122 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24123
124 // Do any cleanup of the target we need to do between process instances.
125 // NB It is better to do this before destroying the process in case the
126 // clean up needs some help from the process.
127 m_breakpoint_list.ClearAllBreakpointSites();
128 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenedf50372011-09-23 21:21:43129 // Disable watchpoint locations just on the debugger side.
130 DisableAllWatchpointLocations(false);
Chris Lattner30fdc8d2010-06-08 16:52:24131 m_process_sp.reset();
132 }
133}
134
135const lldb::ProcessSP &
136Target::CreateProcess (Listener &listener, const char *plugin_name)
137{
138 DeleteCurrentProcess ();
139 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
140 return m_process_sp;
141}
142
143const lldb::ProcessSP &
144Target::GetProcessSP () const
145{
146 return m_process_sp;
147}
148
149lldb::TargetSP
150Target::GetSP()
151{
Greg Clayton4d122c42011-09-17 08:33:22152 // This object contains an instrusive ref count base class so we can
153 // easily make a shared pointer to this object
154 return TargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24155}
156
Greg Clayton3418c852011-08-10 02:10:13157void
158Target::Destroy()
159{
160 Mutex::Locker locker (m_mutex);
161 DeleteCurrentProcess ();
162 m_platform_sp.reset();
163 m_arch.Clear();
164 m_images.Clear();
165 m_section_load_list.Clear();
166 const bool notify = false;
167 m_breakpoint_list.RemoveAll(notify);
168 m_internal_breakpoint_list.RemoveAll(notify);
169 m_last_created_breakpoint.reset();
170 m_search_filter_sp.reset();
171 m_image_search_paths.Clear(notify);
172 m_scratch_ast_context_ap.reset();
173 m_persistent_variables.Clear();
174 m_stop_hooks.clear();
175 m_stop_hook_next_id = 0;
176 m_suppress_stop_hooks = false;
177}
178
179
Chris Lattner30fdc8d2010-06-08 16:52:24180BreakpointList &
181Target::GetBreakpointList(bool internal)
182{
183 if (internal)
184 return m_internal_breakpoint_list;
185 else
186 return m_breakpoint_list;
187}
188
189const BreakpointList &
190Target::GetBreakpointList(bool internal) const
191{
192 if (internal)
193 return m_internal_breakpoint_list;
194 else
195 return m_breakpoint_list;
196}
197
198BreakpointSP
199Target::GetBreakpointByID (break_id_t break_id)
200{
201 BreakpointSP bp_sp;
202
203 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
204 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
205 else
206 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
207
208 return bp_sp;
209}
210
211BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11212Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
213 const FileSpecList *source_file_spec_list,
Jim Ingham969795f2011-09-21 01:17:13214 RegularExpression &source_regex,
215 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24216{
Jim Ingham87df91b2011-09-23 00:54:11217 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
218 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham969795f2011-09-21 01:17:13219 return CreateBreakpoint (filter_sp, resolver_sp, internal);
220}
221
222
223BreakpointSP
224Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
225{
226 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner30fdc8d2010-06-08 16:52:24227 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
228 return CreateBreakpoint (filter_sp, resolver_sp, internal);
229}
230
231
232BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41233Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24234{
Chris Lattner30fdc8d2010-06-08 16:52:24235 Address so_addr;
236 // Attempt to resolve our load address if possible, though it is ok if
237 // it doesn't resolve to section/offset.
238
Greg Clayton1b72fcb2010-08-24 00:45:41239 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40240 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41241 if (!so_addr.IsValid())
242 {
243 // The address didn't resolve, so just set this as an absolute address
244 so_addr.SetOffset (addr);
245 }
246 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24247 return bp_sp;
248}
249
250BreakpointSP
251Target::CreateBreakpoint (Address &addr, bool internal)
252{
253 TargetSP target_sp = this->GetSP();
254 SearchFilterSP filter_sp(new SearchFilter (target_sp));
255 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
256 return CreateBreakpoint (filter_sp, resolver_sp, internal);
257}
258
259BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11260Target::CreateBreakpoint (const FileSpecList *containingModules,
261 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17262 const char *func_name,
263 uint32_t func_name_type_mask,
264 bool internal,
265 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24266{
Greg Clayton0c5cd902010-06-28 21:30:43267 BreakpointSP bp_sp;
268 if (func_name)
269 {
Jim Ingham87df91b2011-09-23 00:54:11270 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17271
272 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
273 func_name,
274 func_name_type_mask,
275 Breakpoint::Exact,
276 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton0c5cd902010-06-28 21:30:43277 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
278 }
279 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24280}
281
282
283SearchFilterSP
284Target::GetSearchFilterForModule (const FileSpec *containingModule)
285{
286 SearchFilterSP filter_sp;
287 lldb::TargetSP target_sp = this->GetSP();
288 if (containingModule != NULL)
289 {
290 // TODO: We should look into sharing module based search filters
291 // across many breakpoints like we do for the simple target based one
292 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
293 }
294 else
295 {
296 if (m_search_filter_sp.get() == NULL)
297 m_search_filter_sp.reset (new SearchFilter (target_sp));
298 filter_sp = m_search_filter_sp;
299 }
300 return filter_sp;
301}
302
Jim Ingham969795f2011-09-21 01:17:13303SearchFilterSP
304Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
305{
306 SearchFilterSP filter_sp;
307 lldb::TargetSP target_sp = this->GetSP();
308 if (containingModules && containingModules->GetSize() != 0)
309 {
310 // TODO: We should look into sharing module based search filters
311 // across many breakpoints like we do for the simple target based one
312 filter_sp.reset (new SearchFilterByModuleList (target_sp, *containingModules));
313 }
314 else
315 {
316 if (m_search_filter_sp.get() == NULL)
317 m_search_filter_sp.reset (new SearchFilter (target_sp));
318 filter_sp = m_search_filter_sp;
319 }
320 return filter_sp;
321}
322
Jim Ingham87df91b2011-09-23 00:54:11323SearchFilterSP
324Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles)
325{
326 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
327 return GetSearchFilterForModuleList(containingModules);
328
329 SearchFilterSP filter_sp;
330 lldb::TargetSP target_sp = this->GetSP();
331 if (containingModules == NULL)
332 {
333 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
334 // but that will take a little reworking.
335
336 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, FileSpecList(), *containingSourceFiles));
337 }
338 else
339 {
340 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, *containingModules, *containingSourceFiles));
341 }
342 return filter_sp;
343}
344
Chris Lattner30fdc8d2010-06-08 16:52:24345BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11346Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
347 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17348 RegularExpression &func_regex,
349 bool internal,
350 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24351{
Jim Ingham87df91b2011-09-23 00:54:11352 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17353 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
354 func_regex,
355 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24356
357 return CreateBreakpoint (filter_sp, resolver_sp, internal);
358}
359
360BreakpointSP
361Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
362{
363 BreakpointSP bp_sp;
364 if (filter_sp && resolver_sp)
365 {
366 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
367 resolver_sp->SetBreakpoint (bp_sp.get());
368
369 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17370 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24371 else
Greg Clayton9fed0d82010-07-23 23:33:17372 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24373
Greg Clayton2d4edfb2010-11-06 01:53:30374 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24375 if (log)
376 {
377 StreamString s;
378 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
379 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
380 }
381
Chris Lattner30fdc8d2010-06-08 16:52:24382 bp_sp->ResolveBreakpoint();
383 }
Jim Ingham36f3b362010-10-14 23:45:03384
385 if (!internal && bp_sp)
386 {
387 m_last_created_breakpoint = bp_sp;
388 }
389
Chris Lattner30fdc8d2010-06-08 16:52:24390 return bp_sp;
391}
392
Johnny Chen86364b42011-09-20 23:28:55393bool
394Target::ProcessIsValid()
395{
396 return (m_process_sp && m_process_sp->IsAlive());
397}
398
Johnny Chenab9ee762011-09-14 00:26:03399// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
400// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen887062a2011-09-12 23:38:44401WatchpointLocationSP
402Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
403{
Johnny Chen0c406372011-09-14 20:23:45404 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
405 if (log)
406 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
407 __FUNCTION__, addr, size, type);
408
Johnny Chen887062a2011-09-12 23:38:44409 WatchpointLocationSP wp_loc_sp;
Johnny Chen86364b42011-09-20 23:28:55410 if (!ProcessIsValid())
Johnny Chen2fd89a02011-09-13 18:30:59411 return wp_loc_sp;
Johnny Chen45e541f2011-09-14 22:20:15412 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen887062a2011-09-12 23:38:44413 return wp_loc_sp;
Johnny Chen7313a642011-09-13 01:15:36414
Johnny Chenab9ee762011-09-14 00:26:03415 // Currently we only support one watchpoint location per address, with total
416 // number of watchpoint locations limited by the hardware which the inferior
417 // is running on.
Johnny Chen3c532582011-09-13 23:29:31418 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
419 if (matched_sp)
420 {
Johnny Chen0c406372011-09-14 20:23:45421 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31422 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45423 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
424 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen45e541f2011-09-14 22:20:15425 // Return the existing watchpoint location if both size and type match.
426 if (size == old_size && type == old_type) {
427 wp_loc_sp = matched_sp;
428 wp_loc_sp->SetEnabled(false);
429 } else {
430 // Nil the matched watchpoint location; we will be creating a new one.
431 m_process_sp->DisableWatchpoint(matched_sp.get());
432 m_watchpoint_location_list.Remove(matched_sp->GetID());
433 }
Johnny Chen3c532582011-09-13 23:29:31434 }
435
Johnny Chen45e541f2011-09-14 22:20:15436 if (!wp_loc_sp) {
437 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
438 if (!new_loc) {
439 printf("WatchpointLocation ctor failed, out of memory?\n");
440 return wp_loc_sp;
441 }
442 new_loc->SetWatchpointType(type);
Johnny Chen5d043462011-09-26 22:40:50443 new_loc->SetTarget(this);
Johnny Chen45e541f2011-09-14 22:20:15444 wp_loc_sp.reset(new_loc);
445 m_watchpoint_location_list.Add(wp_loc_sp);
446 }
Johnny Chen0c406372011-09-14 20:23:45447
Johnny Chen0c406372011-09-14 20:23:45448 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen0c406372011-09-14 20:23:45449 if (log)
450 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
451 __FUNCTION__,
452 rc.Success() ? "succeeded" : "failed",
453 wp_loc_sp->GetID());
454
Johnny Chen45e541f2011-09-14 22:20:15455 if (rc.Fail()) wp_loc_sp.reset();
Johnny Chen7313a642011-09-13 01:15:36456 return wp_loc_sp;
Johnny Chen887062a2011-09-12 23:38:44457}
458
Chris Lattner30fdc8d2010-06-08 16:52:24459void
460Target::RemoveAllBreakpoints (bool internal_also)
461{
Greg Clayton2d4edfb2010-11-06 01:53:30462 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24463 if (log)
464 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
465
Greg Clayton9fed0d82010-07-23 23:33:17466 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24467 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17468 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03469
470 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24471}
472
473void
474Target::DisableAllBreakpoints (bool internal_also)
475{
Greg Clayton2d4edfb2010-11-06 01:53:30476 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24477 if (log)
478 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
479
480 m_breakpoint_list.SetEnabledAll (false);
481 if (internal_also)
482 m_internal_breakpoint_list.SetEnabledAll (false);
483}
484
485void
486Target::EnableAllBreakpoints (bool internal_also)
487{
Greg Clayton2d4edfb2010-11-06 01:53:30488 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24489 if (log)
490 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
491
492 m_breakpoint_list.SetEnabledAll (true);
493 if (internal_also)
494 m_internal_breakpoint_list.SetEnabledAll (true);
495}
496
497bool
498Target::RemoveBreakpointByID (break_id_t break_id)
499{
Greg Clayton2d4edfb2010-11-06 01:53:30500 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24501 if (log)
502 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
503
504 if (DisableBreakpointByID (break_id))
505 {
506 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17507 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24508 else
Jim Ingham36f3b362010-10-14 23:45:03509 {
Greg Claytonaa1c5872011-01-24 23:35:47510 if (m_last_created_breakpoint)
511 {
512 if (m_last_created_breakpoint->GetID() == break_id)
513 m_last_created_breakpoint.reset();
514 }
Greg Clayton9fed0d82010-07-23 23:33:17515 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03516 }
Chris Lattner30fdc8d2010-06-08 16:52:24517 return true;
518 }
519 return false;
520}
521
522bool
523Target::DisableBreakpointByID (break_id_t break_id)
524{
Greg Clayton2d4edfb2010-11-06 01:53:30525 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24526 if (log)
527 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
528
529 BreakpointSP bp_sp;
530
531 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
532 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
533 else
534 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
535 if (bp_sp)
536 {
537 bp_sp->SetEnabled (false);
538 return true;
539 }
540 return false;
541}
542
543bool
544Target::EnableBreakpointByID (break_id_t break_id)
545{
Greg Clayton2d4edfb2010-11-06 01:53:30546 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24547 if (log)
548 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
549 __FUNCTION__,
550 break_id,
551 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
552
553 BreakpointSP bp_sp;
554
555 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
556 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
557 else
558 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
559
560 if (bp_sp)
561 {
562 bp_sp->SetEnabled (true);
563 return true;
564 }
565 return false;
566}
567
Johnny Chenedf50372011-09-23 21:21:43568// The flag 'end_to_end', default to true, signifies that the operation is
569// performed end to end, for both the debugger and the debuggee.
570
571// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
572// for end to end operations.
Johnny Chen86364b42011-09-20 23:28:55573bool
Johnny Chenedf50372011-09-23 21:21:43574Target::RemoveAllWatchpointLocations (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55575{
576 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
577 if (log)
578 log->Printf ("Target::%s\n", __FUNCTION__);
579
Johnny Chenedf50372011-09-23 21:21:43580 if (!end_to_end) {
581 m_watchpoint_location_list.RemoveAll();
582 return true;
583 }
584
585 // Otherwise, it's an end to end operation.
586
Johnny Chen86364b42011-09-20 23:28:55587 if (!ProcessIsValid())
588 return false;
589
590 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
591 for (size_t i = 0; i < num_watchpoints; ++i)
592 {
593 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
594 if (!wp_loc_sp)
595 return false;
596
597 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
598 if (rc.Fail())
599 return false;
600 }
601 m_watchpoint_location_list.RemoveAll ();
602 return true; // Success!
603}
604
Johnny Chenedf50372011-09-23 21:21:43605// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
606// for end to end operations.
Johnny Chen86364b42011-09-20 23:28:55607bool
Johnny Chenedf50372011-09-23 21:21:43608Target::DisableAllWatchpointLocations (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55609{
610 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
611 if (log)
612 log->Printf ("Target::%s\n", __FUNCTION__);
613
Johnny Chenedf50372011-09-23 21:21:43614 if (!end_to_end) {
615 m_watchpoint_location_list.SetEnabledAll(false);
616 return true;
617 }
618
619 // Otherwise, it's an end to end operation.
620
Johnny Chen86364b42011-09-20 23:28:55621 if (!ProcessIsValid())
622 return false;
623
624 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
625 for (size_t i = 0; i < num_watchpoints; ++i)
626 {
627 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
628 if (!wp_loc_sp)
629 return false;
630
631 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
632 if (rc.Fail())
633 return false;
634 }
Johnny Chen86364b42011-09-20 23:28:55635 return true; // Success!
636}
637
Johnny Chenedf50372011-09-23 21:21:43638// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
639// for end to end operations.
Johnny Chen86364b42011-09-20 23:28:55640bool
Johnny Chenedf50372011-09-23 21:21:43641Target::EnableAllWatchpointLocations (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55642{
643 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
644 if (log)
645 log->Printf ("Target::%s\n", __FUNCTION__);
646
Johnny Chenedf50372011-09-23 21:21:43647 if (!end_to_end) {
648 m_watchpoint_location_list.SetEnabledAll(true);
649 return true;
650 }
651
652 // Otherwise, it's an end to end operation.
653
Johnny Chen86364b42011-09-20 23:28:55654 if (!ProcessIsValid())
655 return false;
656
657 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
658 for (size_t i = 0; i < num_watchpoints; ++i)
659 {
660 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
661 if (!wp_loc_sp)
662 return false;
663
664 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
665 if (rc.Fail())
666 return false;
667 }
Johnny Chen86364b42011-09-20 23:28:55668 return true; // Success!
669}
670
Johnny Chenedf50372011-09-23 21:21:43671// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chen86364b42011-09-20 23:28:55672bool
673Target::DisableWatchpointLocationByID (lldb::watch_id_t watch_id)
674{
675 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
676 if (log)
677 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
678
679 if (!ProcessIsValid())
680 return false;
681
682 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
683 if (wp_loc_sp)
684 {
685 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58686 if (rc.Success())
687 return true;
Johnny Chen86364b42011-09-20 23:28:55688
Johnny Chenf04ee932011-09-22 18:04:58689 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55690 }
691 return false;
692}
693
Johnny Chenedf50372011-09-23 21:21:43694// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chen86364b42011-09-20 23:28:55695bool
696Target::EnableWatchpointLocationByID (lldb::watch_id_t watch_id)
697{
698 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
699 if (log)
700 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
701
702 if (!ProcessIsValid())
703 return false;
704
705 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
706 if (wp_loc_sp)
707 {
708 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58709 if (rc.Success())
710 return true;
Johnny Chen86364b42011-09-20 23:28:55711
Johnny Chenf04ee932011-09-22 18:04:58712 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55713 }
714 return false;
715}
716
Johnny Chenedf50372011-09-23 21:21:43717// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chen86364b42011-09-20 23:28:55718bool
719Target::RemoveWatchpointLocationByID (lldb::watch_id_t watch_id)
720{
721 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
722 if (log)
723 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
724
725 if (DisableWatchpointLocationByID (watch_id))
726 {
727 m_watchpoint_location_list.Remove(watch_id);
728 return true;
729 }
730 return false;
731}
732
Chris Lattner30fdc8d2010-06-08 16:52:24733ModuleSP
734Target::GetExecutableModule ()
735{
Greg Claytonaa149cb2011-08-11 02:48:45736 return m_images.GetModuleAtIndex(0);
737}
738
739Module*
740Target::GetExecutableModulePointer ()
741{
742 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24743}
744
745void
746Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
747{
748 m_images.Clear();
749 m_scratch_ast_context_ap.reset();
750
751 if (executable_sp.get())
752 {
753 Timer scoped_timer (__PRETTY_FUNCTION__,
754 "Target::SetExecutableModule (executable = '%s/%s')",
755 executable_sp->GetFileSpec().GetDirectory().AsCString(),
756 executable_sp->GetFileSpec().GetFilename().AsCString());
757
758 m_images.Append(executable_sp); // The first image is our exectuable file
759
Jim Ingham5aee1622010-08-09 23:31:02760 // 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:51761 if (!m_arch.IsValid())
762 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02763
Chris Lattner30fdc8d2010-06-08 16:52:24764 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15765 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamb7f6b2f2011-09-08 22:13:49766 // Let's find the file & line for main and set the default source file from there.
767 if (!m_source_manager.DefaultFileAndLineSet())
768 {
769 SymbolContextList sc_list;
770 uint32_t num_matches;
771 ConstString main_name("main");
772 bool symbols_okay = false; // Force it to be a debug symbol.
773 bool append = false;
774 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
775 for (uint32_t idx = 0; idx < num_matches; idx++)
776 {
777 SymbolContext sc;
778 sc_list.GetContextAtIndex(idx, sc);
779 if (sc.line_entry.file)
780 {
781 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
782 break;
783 }
784 }
785 }
Chris Lattner30fdc8d2010-06-08 16:52:24786
Greg Claytoncac9c5f2011-09-24 00:52:29787 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24788 {
789 executable_objfile->GetDependentModules(dependent_files);
790 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
791 {
Greg Claytonded470d2011-03-19 01:12:21792 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
793 FileSpec platform_dependent_file_spec;
794 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55795 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21796 else
797 platform_dependent_file_spec = dependent_file_spec;
798
799 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton32e0a752011-03-30 18:16:51800 m_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24801 if (image_module_sp.get())
802 {
Chris Lattner30fdc8d2010-06-08 16:52:24803 ObjectFile *objfile = image_module_sp->GetObjectFile();
804 if (objfile)
805 objfile->GetDependentModules(dependent_files);
806 }
807 }
808 }
809
Chris Lattner30fdc8d2010-06-08 16:52:24810 }
Caroline Tice1559a462010-09-27 00:30:10811
812 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24813}
814
815
Jim Ingham5aee1622010-08-09 23:31:02816bool
817Target::SetArchitecture (const ArchSpec &arch_spec)
818{
Greg Clayton32e0a752011-03-30 18:16:51819 if (m_arch == arch_spec)
Jim Ingham5aee1622010-08-09 23:31:02820 {
821 // If we're setting the architecture to our current architecture, we
822 // don't need to do anything.
823 return true;
824 }
Greg Clayton32e0a752011-03-30 18:16:51825 else if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02826 {
827 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton32e0a752011-03-30 18:16:51828 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02829 return true;
830 }
831 else
832 {
833 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51834 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02835 ModuleSP executable_sp = GetExecutableModule ();
836 m_images.Clear();
837 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02838 // Need to do something about unsetting breakpoints.
839
840 if (executable_sp)
841 {
842 FileSpec exec_file_spec = executable_sp->GetFileSpec();
843 Error error = ModuleList::GetSharedModule(exec_file_spec,
844 arch_spec,
845 NULL,
846 NULL,
847 0,
848 executable_sp,
849 NULL,
850 NULL);
851
852 if (!error.Fail() && executable_sp)
853 {
854 SetExecutableModule (executable_sp, true);
855 return true;
856 }
857 else
858 {
859 return false;
860 }
861 }
862 else
863 {
864 return false;
865 }
866 }
867}
Chris Lattner30fdc8d2010-06-08 16:52:24868
Chris Lattner30fdc8d2010-06-08 16:52:24869void
870Target::ModuleAdded (ModuleSP &module_sp)
871{
872 // A module is being added to this target for the first time
873 ModuleList module_list;
874 module_list.Append(module_sp);
875 ModulesDidLoad (module_list);
876}
877
878void
879Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
880{
Jim Inghame716ae02011-08-03 01:00:06881 // A module is replacing an already added module
Chris Lattner30fdc8d2010-06-08 16:52:24882 ModuleList module_list;
883 module_list.Append (old_module_sp);
884 ModulesDidUnload (module_list);
885 module_list.Clear ();
886 module_list.Append (new_module_sp);
887 ModulesDidLoad (module_list);
888}
889
890void
891Target::ModulesDidLoad (ModuleList &module_list)
892{
893 m_breakpoint_list.UpdateBreakpoints (module_list, true);
894 // TODO: make event data that packages up the module_list
895 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
896}
897
898void
899Target::ModulesDidUnload (ModuleList &module_list)
900{
901 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26902
903 // Remove the images from the target image list
904 m_images.Remove(module_list);
905
Chris Lattner30fdc8d2010-06-08 16:52:24906 // TODO: make event data that packages up the module_list
907 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
908}
909
910size_t
Greg Claytondb598232011-01-07 01:57:07911Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
912{
913 const Section *section = addr.GetSection();
914 if (section && section->GetModule())
915 {
916 ObjectFile *objfile = section->GetModule()->GetObjectFile();
917 if (objfile)
918 {
919 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
920 addr.GetOffset(),
921 dst,
922 dst_len);
923 if (bytes_read > 0)
924 return bytes_read;
925 else
926 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
927 }
928 else
929 {
930 error.SetErrorString("address isn't from a object file");
931 }
932 }
933 else
934 {
935 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
936 }
937 return 0;
938}
939
940size_t
Enrico Granata9128ee2f2011-09-06 19:20:51941Target::ReadMemory (const Address& addr,
942 bool prefer_file_cache,
943 void *dst,
944 size_t dst_len,
945 Error &error,
946 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24947{
Chris Lattner30fdc8d2010-06-08 16:52:24948 error.Clear();
Greg Claytondb598232011-01-07 01:57:07949
Enrico Granata9128ee2f2011-09-06 19:20:51950 // if we end up reading this from process memory, we will fill this
951 // with the actual load address
952 if (load_addr_ptr)
953 *load_addr_ptr = LLDB_INVALID_ADDRESS;
954
Greg Claytondb598232011-01-07 01:57:07955 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02956
957 addr_t load_addr = LLDB_INVALID_ADDRESS;
958 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58959 Address resolved_addr;
960 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03961 {
Greg Claytond16e1e52011-07-12 17:06:17962 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02963 {
Greg Claytond16e1e52011-07-12 17:06:17964 // No sections are loaded, so we must assume we are not running
965 // yet and anything we are given is a file address.
966 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
967 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02968 }
Greg Claytondda4f7b2010-06-30 23:03:03969 else
Greg Claytonc749eb82011-07-11 05:12:02970 {
Greg Claytond16e1e52011-07-12 17:06:17971 // We have at least one section loaded. This can be becuase
972 // we have manually loaded some sections with "target modules load ..."
973 // or because we have have a live process that has sections loaded
974 // through the dynamic loader
975 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
976 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02977 }
Greg Claytondda4f7b2010-06-30 23:03:03978 }
Greg Clayton357132e2011-03-26 19:14:58979 if (!resolved_addr.IsValid())
980 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03981
Greg Claytonc749eb82011-07-11 05:12:02982
Greg Claytondb598232011-01-07 01:57:07983 if (prefer_file_cache)
984 {
985 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
986 if (bytes_read > 0)
987 return bytes_read;
988 }
Greg Claytondda4f7b2010-06-30 23:03:03989
Johnny Chen86364b42011-09-20 23:28:55990 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03991 {
Greg Claytonc749eb82011-07-11 05:12:02992 if (load_addr == LLDB_INVALID_ADDRESS)
993 load_addr = resolved_addr.GetLoadAddress (this);
994
Greg Claytondda4f7b2010-06-30 23:03:03995 if (load_addr == LLDB_INVALID_ADDRESS)
996 {
997 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
998 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
999 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:081000 resolved_addr.GetFileAddress(),
1001 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:031002 else
1003 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
1004 }
1005 else
1006 {
Greg Claytondb598232011-01-07 01:57:071007 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:241008 if (bytes_read != dst_len)
1009 {
1010 if (error.Success())
1011 {
1012 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:031013 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:241014 else
Greg Claytondda4f7b2010-06-30 23:03:031015 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:241016 }
1017 }
Greg Claytondda4f7b2010-06-30 23:03:031018 if (bytes_read)
Enrico Granata9128ee2f2011-09-06 19:20:511019 {
1020 if (load_addr_ptr)
1021 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:031022 return bytes_read;
Enrico Granata9128ee2f2011-09-06 19:20:511023 }
Greg Claytondda4f7b2010-06-30 23:03:031024 // If the address is not section offset we have an address that
1025 // doesn't resolve to any address in any currently loaded shared
1026 // libaries and we failed to read memory so there isn't anything
1027 // more we can do. If it is section offset, we might be able to
1028 // read cached memory from the object file.
1029 if (!resolved_addr.IsSectionOffset())
1030 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:241031 }
Chris Lattner30fdc8d2010-06-08 16:52:241032 }
Greg Claytondda4f7b2010-06-30 23:03:031033
Greg Claytonc749eb82011-07-11 05:12:021034 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:031035 {
Greg Claytondb598232011-01-07 01:57:071036 // If we didn't already try and read from the object file cache, then
1037 // try it after failing to read from the process.
1038 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:031039 }
1040 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:241041}
1042
Greg Claytond16e1e52011-07-12 17:06:171043size_t
1044Target::ReadScalarIntegerFromMemory (const Address& addr,
1045 bool prefer_file_cache,
1046 uint32_t byte_size,
1047 bool is_signed,
1048 Scalar &scalar,
1049 Error &error)
1050{
1051 uint64_t uval;
1052
1053 if (byte_size <= sizeof(uval))
1054 {
1055 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1056 if (bytes_read == byte_size)
1057 {
1058 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1059 uint32_t offset = 0;
1060 if (byte_size <= 4)
1061 scalar = data.GetMaxU32 (&offset, byte_size);
1062 else
1063 scalar = data.GetMaxU64 (&offset, byte_size);
1064
1065 if (is_signed)
1066 scalar.SignExtend(byte_size * 8);
1067 return bytes_read;
1068 }
1069 }
1070 else
1071 {
1072 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1073 }
1074 return 0;
1075}
1076
1077uint64_t
1078Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1079 bool prefer_file_cache,
1080 size_t integer_byte_size,
1081 uint64_t fail_value,
1082 Error &error)
1083{
1084 Scalar scalar;
1085 if (ReadScalarIntegerFromMemory (addr,
1086 prefer_file_cache,
1087 integer_byte_size,
1088 false,
1089 scalar,
1090 error))
1091 return scalar.ULongLong(fail_value);
1092 return fail_value;
1093}
1094
1095bool
1096Target::ReadPointerFromMemory (const Address& addr,
1097 bool prefer_file_cache,
1098 Error &error,
1099 Address &pointer_addr)
1100{
1101 Scalar scalar;
1102 if (ReadScalarIntegerFromMemory (addr,
1103 prefer_file_cache,
1104 m_arch.GetAddressByteSize(),
1105 false,
1106 scalar,
1107 error))
1108 {
1109 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1110 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1111 {
1112 if (m_section_load_list.IsEmpty())
1113 {
1114 // No sections are loaded, so we must assume we are not running
1115 // yet and anything we are given is a file address.
1116 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1117 }
1118 else
1119 {
1120 // We have at least one section loaded. This can be becuase
1121 // we have manually loaded some sections with "target modules load ..."
1122 // or because we have have a live process that has sections loaded
1123 // through the dynamic loader
1124 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1125 }
1126 // We weren't able to resolve the pointer value, so just return
1127 // an address with no section
1128 if (!pointer_addr.IsValid())
1129 pointer_addr.SetOffset (pointer_vm_addr);
1130 return true;
1131
1132 }
1133 }
1134 return false;
1135}
Chris Lattner30fdc8d2010-06-08 16:52:241136
1137ModuleSP
1138Target::GetSharedModule
1139(
1140 const FileSpec& file_spec,
1141 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:101142 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:241143 const ConstString *object_name,
1144 off_t object_offset,
1145 Error *error_ptr
1146)
1147{
1148 // Don't pass in the UUID so we can tell if we have a stale value in our list
1149 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1150 bool did_create_module = false;
1151 ModuleSP module_sp;
1152
Chris Lattner30fdc8d2010-06-08 16:52:241153 Error error;
1154
Greg Clayton32e0a752011-03-30 18:16:511155 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner30fdc8d2010-06-08 16:52:241156 if (m_image_search_paths.GetSize())
1157 {
1158 FileSpec transformed_spec;
1159 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1160 {
1161 transformed_spec.GetFilename() = file_spec.GetFilename();
1162 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1163 }
1164 }
1165
Greg Clayton32e0a752011-03-30 18:16:511166 // The platform is responsible for finding and caching an appropriate
1167 // module in the shared module cache.
1168 if (m_platform_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241169 {
Greg Clayton32e0a752011-03-30 18:16:511170 FileSpec platform_file_spec;
1171 error = m_platform_sp->GetSharedModule (file_spec,
1172 arch,
1173 uuid_ptr,
1174 object_name,
1175 object_offset,
1176 module_sp,
1177 &old_module_sp,
1178 &did_create_module);
1179 }
1180 else
1181 {
1182 error.SetErrorString("no platform is currently set");
Chris Lattner30fdc8d2010-06-08 16:52:241183 }
1184
Greg Clayton32e0a752011-03-30 18:16:511185 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner30fdc8d2010-06-08 16:52:241186 if (module_sp)
1187 {
1188 m_images.Append (module_sp);
1189 if (did_create_module)
1190 {
1191 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1192 ModuleUpdated(old_module_sp, module_sp);
1193 else
1194 ModuleAdded(module_sp);
1195 }
1196 }
1197 if (error_ptr)
1198 *error_ptr = error;
1199 return module_sp;
1200}
1201
1202
1203Target *
1204Target::CalculateTarget ()
1205{
1206 return this;
1207}
1208
1209Process *
1210Target::CalculateProcess ()
1211{
1212 return NULL;
1213}
1214
1215Thread *
1216Target::CalculateThread ()
1217{
1218 return NULL;
1219}
1220
1221StackFrame *
1222Target::CalculateStackFrame ()
1223{
1224 return NULL;
1225}
1226
1227void
Greg Clayton0603aa92010-10-04 01:05:561228Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:241229{
Greg Claytonc14ee322011-09-22 04:58:261230 exe_ctx.Clear();
1231 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:241232}
1233
1234PathMappingList &
1235Target::GetImageSearchPathList ()
1236{
1237 return m_image_search_paths;
1238}
1239
1240void
1241Target::ImageSearchPathsChanged
1242(
1243 const PathMappingList &path_list,
1244 void *baton
1245)
1246{
1247 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:451248 ModuleSP exe_module_sp (target->GetExecutableModule());
1249 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241250 {
Greg Claytonaa149cb2011-08-11 02:48:451251 target->m_images.Clear();
1252 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:241253 }
1254}
1255
1256ClangASTContext *
1257Target::GetScratchClangASTContext()
1258{
Greg Clayton73da2442011-08-03 01:23:551259 // Now see if we know the target triple, and if so, create our scratch AST context:
1260 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1261 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:241262 return m_scratch_ast_context_ap.get();
1263}
Caroline Ticedaccaa92010-09-20 20:44:431264
Greg Clayton99d0faf2010-11-18 23:32:351265void
Caroline Tice20bd37f2011-03-10 22:14:101266Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:431267{
Greg Clayton99d0faf2010-11-18 23:32:351268 UserSettingsControllerSP &usc = GetSettingsController();
1269 usc.reset (new SettingsController);
1270 UserSettingsController::InitializeSettingsController (usc,
1271 SettingsController::global_settings_table,
1272 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:101273
1274 // Now call SettingsInitialize() on each 'child' setting of Target
1275 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:351276}
Caroline Ticedaccaa92010-09-20 20:44:431277
Greg Clayton99d0faf2010-11-18 23:32:351278void
Caroline Tice20bd37f2011-03-10 22:14:101279Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:351280{
Caroline Tice20bd37f2011-03-10 22:14:101281
1282 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1283
1284 Process::SettingsTerminate ();
1285
1286 // Now terminate Target Settings.
1287
Greg Clayton99d0faf2010-11-18 23:32:351288 UserSettingsControllerSP &usc = GetSettingsController();
1289 UserSettingsController::FinalizeSettingsController (usc);
1290 usc.reset();
1291}
Caroline Ticedaccaa92010-09-20 20:44:431292
Greg Clayton99d0faf2010-11-18 23:32:351293UserSettingsControllerSP &
1294Target::GetSettingsController ()
1295{
1296 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:431297 return g_settings_controller;
1298}
1299
1300ArchSpec
1301Target::GetDefaultArchitecture ()
1302{
Greg Clayton64195a22011-02-23 00:35:021303 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1304
1305 if (settings_controller_sp)
1306 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1307 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:431308}
1309
1310void
Greg Clayton64195a22011-02-23 00:35:021311Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:431312{
Greg Clayton64195a22011-02-23 00:35:021313 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1314
1315 if (settings_controller_sp)
1316 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:431317}
1318
Greg Clayton0603aa92010-10-04 01:05:561319Target *
1320Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1321{
1322 // The target can either exist in the "process" of ExecutionContext, or in
1323 // the "target_sp" member of SymbolContext. This accessor helper function
1324 // will get the target from one of these locations.
1325
1326 Target *target = NULL;
1327 if (sc_ptr != NULL)
1328 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:261329 if (target == NULL && exe_ctx_ptr)
1330 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:561331 return target;
1332}
1333
1334
Caroline Tice1559a462010-09-27 00:30:101335void
1336Target::UpdateInstanceName ()
1337{
1338 StreamString sstr;
1339
Greg Claytonaa149cb2011-08-11 02:48:451340 Module *exe_module = GetExecutableModulePointer();
1341 if (exe_module)
Caroline Tice1559a462010-09-27 00:30:101342 {
Greg Clayton307de252010-10-27 02:06:371343 sstr.Printf ("%s_%s",
Greg Claytonaa149cb2011-08-11 02:48:451344 exe_module->GetFileSpec().GetFilename().AsCString(),
1345 exe_module->GetArchitecture().GetArchitectureName());
1346 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:101347 }
1348}
1349
Sean Callanan322f5292010-10-29 00:29:031350const char *
1351Target::GetExpressionPrefixContentsAsCString ()
1352{
Greg Clayton7e14f912011-04-23 02:04:551353 if (m_expr_prefix_contents_sp)
1354 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1355 return NULL;
Sean Callanan322f5292010-10-29 00:29:031356}
1357
Greg Clayton8b2fe6d2010-12-14 02:59:591358ExecutionResults
1359Target::EvaluateExpression
1360(
1361 const char *expr_cstr,
1362 StackFrame *frame,
Sean Callanan3bfdaa22011-09-15 02:13:071363 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton8b2fe6d2010-12-14 02:59:591364 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:351365 bool keep_in_memory,
Jim Ingham2837b762011-05-04 03:43:181366 lldb::DynamicValueType use_dynamic,
Greg Clayton8b2fe6d2010-12-14 02:59:591367 lldb::ValueObjectSP &result_valobj_sp
1368)
1369{
1370 ExecutionResults execution_results = eExecutionSetupError;
1371
1372 result_valobj_sp.reset();
Jim Ingham6026ca32011-05-12 02:06:141373
1374 // We shouldn't run stop hooks in expressions.
1375 // Be sure to reset this if you return anywhere within this function.
1376 bool old_suppress_value = m_suppress_stop_hooks;
1377 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:591378
1379 ExecutionContext exe_ctx;
1380 if (frame)
1381 {
1382 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:081383 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:181384 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granata27b625e2011-08-09 01:04:561385 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1386 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham2837b762011-05-04 03:43:181387 lldb::VariableSP var_sp;
1388 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1389 use_dynamic,
1390 expr_path_options,
1391 var_sp,
1392 error);
Greg Clayton8b2fe6d2010-12-14 02:59:591393 }
1394 else if (m_process_sp)
1395 {
1396 m_process_sp->CalculateExecutionContext(exe_ctx);
1397 }
1398 else
1399 {
1400 CalculateExecutionContext(exe_ctx);
1401 }
1402
1403 if (result_valobj_sp)
1404 {
1405 execution_results = eExecutionCompleted;
1406 // We got a result from the frame variable expression path above...
1407 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1408
1409 lldb::ValueObjectSP const_valobj_sp;
1410
1411 // Check in case our value is already a constant value
1412 if (result_valobj_sp->GetIsConstant())
1413 {
1414 const_valobj_sp = result_valobj_sp;
1415 const_valobj_sp->SetName (persistent_variable_name);
1416 }
1417 else
Jim Ingham78a685a2011-04-16 00:01:131418 {
Jim Ingham2837b762011-05-04 03:43:181419 if (use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:131420 {
Jim Ingham2837b762011-05-04 03:43:181421 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:131422 if (dynamic_sp)
1423 result_valobj_sp = dynamic_sp;
1424 }
1425
Jim Ingham6035b672011-03-31 00:19:251426 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:131427 }
Greg Clayton8b2fe6d2010-12-14 02:59:591428
Sean Callanan92adcac2011-01-13 08:53:351429 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1430
Greg Clayton8b2fe6d2010-12-14 02:59:591431 result_valobj_sp = const_valobj_sp;
1432
Sean Callanan92adcac2011-01-13 08:53:351433 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1434 assert (clang_expr_variable_sp.get());
1435
1436 // Set flags and live data as appropriate
1437
1438 const Value &result_value = live_valobj_sp->GetValue();
1439
1440 switch (result_value.GetValueType())
1441 {
1442 case Value::eValueTypeHostAddress:
1443 case Value::eValueTypeFileAddress:
1444 // we don't do anything with these for now
1445 break;
1446 case Value::eValueTypeScalar:
1447 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1448 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1449 break;
1450 case Value::eValueTypeLoadAddress:
1451 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1452 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1453 break;
1454 }
Greg Clayton8b2fe6d2010-12-14 02:59:591455 }
1456 else
1457 {
1458 // Make sure we aren't just trying to see the value of a persistent
1459 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:351460 lldb::ClangExpressionVariableSP persistent_var_sp;
1461 // Only check for persistent variables the expression starts with a '$'
1462 if (expr_cstr[0] == '$')
1463 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1464
Greg Clayton8b2fe6d2010-12-14 02:59:591465 if (persistent_var_sp)
1466 {
1467 result_valobj_sp = persistent_var_sp->GetValueObject ();
1468 execution_results = eExecutionCompleted;
1469 }
1470 else
1471 {
1472 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan3bfdaa22011-09-15 02:13:071473
Greg Clayton8b2fe6d2010-12-14 02:59:591474 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:071475 execution_policy,
Sean Callanan92adcac2011-01-13 08:53:351476 unwind_on_error,
Greg Clayton8b2fe6d2010-12-14 02:59:591477 expr_cstr,
1478 prefix,
1479 result_valobj_sp);
1480 }
1481 }
Jim Ingham6026ca32011-05-12 02:06:141482
1483 m_suppress_stop_hooks = old_suppress_value;
1484
Greg Clayton8b2fe6d2010-12-14 02:59:591485 return execution_results;
1486}
1487
Greg Claytonf3ef3d22011-05-22 22:46:531488lldb::addr_t
1489Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1490{
1491 addr_t code_addr = load_addr;
1492 switch (m_arch.GetMachine())
1493 {
1494 case llvm::Triple::arm:
1495 case llvm::Triple::thumb:
1496 switch (addr_class)
1497 {
1498 case eAddressClassData:
1499 case eAddressClassDebug:
1500 return LLDB_INVALID_ADDRESS;
1501
1502 case eAddressClassUnknown:
1503 case eAddressClassInvalid:
1504 case eAddressClassCode:
1505 case eAddressClassCodeAlternateISA:
1506 case eAddressClassRuntime:
1507 // Check if bit zero it no set?
1508 if ((code_addr & 1ull) == 0)
1509 {
1510 // Bit zero isn't set, check if the address is a multiple of 2?
1511 if (code_addr & 2ull)
1512 {
1513 // The address is a multiple of 2 so it must be thumb, set bit zero
1514 code_addr |= 1ull;
1515 }
1516 else if (addr_class == eAddressClassCodeAlternateISA)
1517 {
1518 // We checked the address and the address claims to be the alternate ISA
1519 // which means thumb, so set bit zero.
1520 code_addr |= 1ull;
1521 }
1522 }
1523 break;
1524 }
1525 break;
1526
1527 default:
1528 break;
1529 }
1530 return code_addr;
1531}
1532
1533lldb::addr_t
1534Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1535{
1536 addr_t opcode_addr = load_addr;
1537 switch (m_arch.GetMachine())
1538 {
1539 case llvm::Triple::arm:
1540 case llvm::Triple::thumb:
1541 switch (addr_class)
1542 {
1543 case eAddressClassData:
1544 case eAddressClassDebug:
1545 return LLDB_INVALID_ADDRESS;
1546
1547 case eAddressClassInvalid:
1548 case eAddressClassUnknown:
1549 case eAddressClassCode:
1550 case eAddressClassCodeAlternateISA:
1551 case eAddressClassRuntime:
1552 opcode_addr &= ~(1ull);
1553 break;
1554 }
1555 break;
1556
1557 default:
1558 break;
1559 }
1560 return opcode_addr;
1561}
1562
Jim Ingham9575d842011-03-11 03:53:591563lldb::user_id_t
1564Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1565{
1566 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1567 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1568 m_stop_hooks[new_uid] = new_hook_sp;
1569 return new_uid;
1570}
1571
1572bool
1573Target::RemoveStopHookByID (lldb::user_id_t user_id)
1574{
1575 size_t num_removed;
1576 num_removed = m_stop_hooks.erase (user_id);
1577 if (num_removed == 0)
1578 return false;
1579 else
1580 return true;
1581}
1582
1583void
1584Target::RemoveAllStopHooks ()
1585{
1586 m_stop_hooks.clear();
1587}
1588
1589Target::StopHookSP
1590Target::GetStopHookByID (lldb::user_id_t user_id)
1591{
1592 StopHookSP found_hook;
1593
1594 StopHookCollection::iterator specified_hook_iter;
1595 specified_hook_iter = m_stop_hooks.find (user_id);
1596 if (specified_hook_iter != m_stop_hooks.end())
1597 found_hook = (*specified_hook_iter).second;
1598 return found_hook;
1599}
1600
1601bool
1602Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1603{
1604 StopHookCollection::iterator specified_hook_iter;
1605 specified_hook_iter = m_stop_hooks.find (user_id);
1606 if (specified_hook_iter == m_stop_hooks.end())
1607 return false;
1608
1609 (*specified_hook_iter).second->SetIsActive (active_state);
1610 return true;
1611}
1612
1613void
1614Target::SetAllStopHooksActiveState (bool active_state)
1615{
1616 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1617 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1618 {
1619 (*pos).second->SetIsActive (active_state);
1620 }
1621}
1622
1623void
1624Target::RunStopHooks ()
1625{
Jim Ingham6026ca32011-05-12 02:06:141626 if (m_suppress_stop_hooks)
1627 return;
1628
Jim Ingham9575d842011-03-11 03:53:591629 if (!m_process_sp)
1630 return;
1631
1632 if (m_stop_hooks.empty())
1633 return;
1634
1635 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1636
1637 // If there aren't any active stop hooks, don't bother either:
1638 bool any_active_hooks = false;
1639 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1640 {
1641 if ((*pos).second->IsActive())
1642 {
1643 any_active_hooks = true;
1644 break;
1645 }
1646 }
1647 if (!any_active_hooks)
1648 return;
1649
1650 CommandReturnObject result;
1651
1652 std::vector<ExecutionContext> exc_ctx_with_reasons;
1653 std::vector<SymbolContext> sym_ctx_with_reasons;
1654
1655 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1656 size_t num_threads = cur_threadlist.GetSize();
1657 for (size_t i = 0; i < num_threads; i++)
1658 {
1659 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1660 if (cur_thread_sp->ThreadStoppedForAReason())
1661 {
1662 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1663 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1664 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1665 }
1666 }
1667
1668 // If no threads stopped for a reason, don't run the stop-hooks.
1669 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1670 if (num_exe_ctx == 0)
1671 return;
1672
Jim Ingham5b52f0c2011-06-02 23:58:261673 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1674 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:591675
1676 bool keep_going = true;
1677 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271678 bool print_hook_header;
1679 bool print_thread_header;
1680
1681 if (num_exe_ctx == 1)
1682 print_thread_header = false;
1683 else
1684 print_thread_header = true;
1685
1686 if (m_stop_hooks.size() == 1)
1687 print_hook_header = false;
1688 else
1689 print_hook_header = true;
1690
Jim Ingham9575d842011-03-11 03:53:591691 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1692 {
1693 // result.Clear();
1694 StopHookSP cur_hook_sp = (*pos).second;
1695 if (!cur_hook_sp->IsActive())
1696 continue;
1697
1698 bool any_thread_matched = false;
1699 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1700 {
1701 if ((cur_hook_sp->GetSpecifier () == NULL
1702 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1703 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Claytonc14ee322011-09-22 04:58:261704 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Ingham9575d842011-03-11 03:53:591705 {
1706 if (!hooks_ran)
1707 {
Jim Ingham9575d842011-03-11 03:53:591708 hooks_ran = true;
1709 }
Jim Ingham381e25b2011-03-22 01:47:271710 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591711 {
1712 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1713 any_thread_matched = true;
1714 }
1715
Jim Ingham381e25b2011-03-22 01:47:271716 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:261717 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591718
1719 bool stop_on_continue = true;
1720 bool stop_on_error = true;
1721 bool echo_commands = false;
1722 bool print_results = true;
1723 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:511724 &exc_ctx_with_reasons[i],
1725 stop_on_continue,
1726 stop_on_error,
1727 echo_commands,
1728 print_results,
1729 result);
Jim Ingham9575d842011-03-11 03:53:591730
1731 // If the command started the target going again, we should bag out of
1732 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:511733 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1734 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:591735 {
1736 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1737 keep_going = false;
1738 }
1739 }
1740 }
1741 }
Jason Molenda879cf772011-09-23 00:42:551742
Caroline Tice969ed3d12011-05-02 20:41:461743 result.GetImmediateOutputStream()->Flush();
1744 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:591745}
1746
Greg Clayton7b242382011-07-08 00:48:091747bool
1748Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1749{
1750 bool changed = false;
1751 if (module)
1752 {
1753 ObjectFile *object_file = module->GetObjectFile();
1754 if (object_file)
1755 {
1756 SectionList *section_list = object_file->GetSectionList ();
1757 if (section_list)
1758 {
1759 // All sections listed in the dyld image info structure will all
1760 // either be fixed up already, or they will all be off by a single
1761 // slide amount that is determined by finding the first segment
1762 // that is at file offset zero which also has bytes (a file size
1763 // that is greater than zero) in the object file.
1764
1765 // Determine the slide amount (if any)
1766 const size_t num_sections = section_list->GetSize();
1767 size_t sect_idx = 0;
1768 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1769 {
1770 // Iterate through the object file sections to find the
1771 // first section that starts of file offset zero and that
1772 // has bytes in the file...
1773 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1774 if (section)
1775 {
1776 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1777 changed = true;
1778 }
1779 }
1780 }
1781 }
1782 }
1783 return changed;
1784}
1785
1786
Jim Ingham9575d842011-03-11 03:53:591787//--------------------------------------------------------------
1788// class Target::StopHook
1789//--------------------------------------------------------------
1790
1791
1792Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1793 UserID (uid),
1794 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:591795 m_commands (),
1796 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:401797 m_thread_spec_ap(NULL),
1798 m_active (true)
Jim Ingham9575d842011-03-11 03:53:591799{
1800}
1801
1802Target::StopHook::StopHook (const StopHook &rhs) :
1803 UserID (rhs.GetID()),
1804 m_target_sp (rhs.m_target_sp),
1805 m_commands (rhs.m_commands),
1806 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:401807 m_thread_spec_ap (NULL),
1808 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:591809{
1810 if (rhs.m_thread_spec_ap.get() != NULL)
1811 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1812}
1813
1814
1815Target::StopHook::~StopHook ()
1816{
1817}
1818
1819void
1820Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1821{
1822 m_thread_spec_ap.reset (specifier);
1823}
1824
1825
1826void
1827Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1828{
1829 int indent_level = s->GetIndentLevel();
1830
1831 s->SetIndentLevel(indent_level + 2);
1832
1833 s->Printf ("Hook: %d\n", GetID());
1834 if (m_active)
1835 s->Indent ("State: enabled\n");
1836 else
1837 s->Indent ("State: disabled\n");
1838
1839 if (m_specifier_sp)
1840 {
1841 s->Indent();
1842 s->PutCString ("Specifier:\n");
1843 s->SetIndentLevel (indent_level + 4);
1844 m_specifier_sp->GetDescription (s, level);
1845 s->SetIndentLevel (indent_level + 2);
1846 }
1847
1848 if (m_thread_spec_ap.get() != NULL)
1849 {
1850 StreamString tmp;
1851 s->Indent("Thread:\n");
1852 m_thread_spec_ap->GetDescription (&tmp, level);
1853 s->SetIndentLevel (indent_level + 4);
1854 s->Indent (tmp.GetData());
1855 s->PutCString ("\n");
1856 s->SetIndentLevel (indent_level + 2);
1857 }
1858
1859 s->Indent ("Commands: \n");
1860 s->SetIndentLevel (indent_level + 4);
1861 uint32_t num_commands = m_commands.GetSize();
1862 for (uint32_t i = 0; i < num_commands; i++)
1863 {
1864 s->Indent(m_commands.GetStringAtIndex(i));
1865 s->PutCString ("\n");
1866 }
1867 s->SetIndentLevel (indent_level);
1868}
1869
1870
Caroline Ticedaccaa92010-09-20 20:44:431871//--------------------------------------------------------------
1872// class Target::SettingsController
1873//--------------------------------------------------------------
1874
1875Target::SettingsController::SettingsController () :
1876 UserSettingsController ("target", Debugger::GetSettingsController()),
1877 m_default_architecture ()
1878{
1879 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1880 InstanceSettings::GetDefaultName().AsCString()));
1881}
1882
1883Target::SettingsController::~SettingsController ()
1884{
1885}
1886
1887lldb::InstanceSettingsSP
1888Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1889{
Greg Claytondbe54502010-11-19 03:46:011890 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1891 false,
1892 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431893 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1894 return new_settings_sp;
1895}
1896
Caroline Ticedaccaa92010-09-20 20:44:431897
Jim Ingham78a685a2011-04-16 00:01:131898#define TSC_DEFAULT_ARCH "default-arch"
1899#define TSC_EXPR_PREFIX "expr-prefix"
Jim Ingham78a685a2011-04-16 00:01:131900#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton385aa282011-04-22 03:55:061901#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Clayton7e14f912011-04-23 02:04:551902#define TSC_SOURCE_MAP "source-map"
Enrico Granata22c55d12011-08-12 02:00:061903#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata9128ee2f2011-09-06 19:20:511904#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytonbfe5f3b2011-02-18 01:44:251905
1906
1907static const ConstString &
1908GetSettingNameForDefaultArch ()
1909{
1910 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytonbfe5f3b2011-02-18 01:44:251911 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431912}
1913
Greg Claytonbfe5f3b2011-02-18 01:44:251914static const ConstString &
1915GetSettingNameForExpressionPrefix ()
1916{
1917 static ConstString g_const_string (TSC_EXPR_PREFIX);
1918 return g_const_string;
1919}
1920
1921static const ConstString &
Jim Ingham78a685a2011-04-16 00:01:131922GetSettingNameForPreferDynamicValue ()
1923{
1924 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1925 return g_const_string;
1926}
1927
Greg Clayton7e14f912011-04-23 02:04:551928static const ConstString &
1929GetSettingNameForSourcePathMap ()
1930{
1931 static ConstString g_const_string (TSC_SOURCE_MAP);
1932 return g_const_string;
1933}
Greg Claytonbfe5f3b2011-02-18 01:44:251934
Greg Clayton385aa282011-04-22 03:55:061935static const ConstString &
1936GetSettingNameForSkipPrologue ()
1937{
1938 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1939 return g_const_string;
1940}
1941
Enrico Granata22c55d12011-08-12 02:00:061942static const ConstString &
1943GetSettingNameForMaxChildren ()
1944{
1945 static ConstString g_const_string (TSC_MAX_CHILDREN);
1946 return g_const_string;
1947}
Greg Clayton385aa282011-04-22 03:55:061948
Enrico Granata9128ee2f2011-09-06 19:20:511949static const ConstString &
1950GetSettingNameForMaxStringSummaryLength ()
1951{
1952 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1953 return g_const_string;
1954}
Greg Clayton385aa282011-04-22 03:55:061955
Caroline Ticedaccaa92010-09-20 20:44:431956bool
1957Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1958 const char *index_value,
1959 const char *value,
1960 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541961 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431962 Error&err)
1963{
Greg Claytonbfe5f3b2011-02-18 01:44:251964 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431965 {
Greg Claytoneb0103f2011-04-07 22:46:351966 m_default_architecture.SetTriple (value, NULL);
Greg Clayton64195a22011-02-23 00:35:021967 if (!m_default_architecture.IsValid())
1968 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431969 }
1970 return true;
1971}
1972
1973
1974bool
1975Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1976 StringList &value,
1977 Error &err)
1978{
Greg Claytonbfe5f3b2011-02-18 01:44:251979 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431980 {
Greg Clayton307de252010-10-27 02:06:371981 // If the arch is invalid (the default), don't show a string for it
1982 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021983 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431984 return true;
1985 }
1986 else
1987 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1988
1989 return false;
1990}
1991
1992//--------------------------------------------------------------
1993// class TargetInstanceSettings
1994//--------------------------------------------------------------
1995
Greg Clayton85851dd2010-12-04 00:10:171996TargetInstanceSettings::TargetInstanceSettings
1997(
1998 UserSettingsController &owner,
1999 bool live_instance,
2000 const char *name
2001) :
Greg Claytonbfe5f3b2011-02-18 01:44:252002 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Clayton7e14f912011-04-23 02:04:552003 m_expr_prefix_file (),
2004 m_expr_prefix_contents_sp (),
Jim Ingham2837b762011-05-04 03:43:182005 m_prefer_dynamic_value (2),
Greg Clayton7e14f912011-04-23 02:04:552006 m_skip_prologue (true, true),
Enrico Granata22c55d12011-08-12 02:00:062007 m_source_map (NULL, NULL),
Enrico Granata9128ee2f2011-09-06 19:20:512008 m_max_children_display(256),
2009 m_max_strlen_length(1024)
Caroline Ticedaccaa92010-09-20 20:44:432010{
2011 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2012 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2013 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2014 // This is true for CreateInstanceName() too.
2015
2016 if (GetInstanceName () == InstanceSettings::InvalidName())
2017 {
2018 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2019 m_owner.RegisterInstanceSettings (this);
2020 }
2021
2022 if (live_instance)
2023 {
2024 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2025 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:432026 }
2027}
2028
2029TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton7e14f912011-04-23 02:04:552030 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
2031 m_expr_prefix_file (rhs.m_expr_prefix_file),
2032 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
2033 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2034 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata22c55d12011-08-12 02:00:062035 m_source_map (rhs.m_source_map),
Enrico Granata9128ee2f2011-09-06 19:20:512036 m_max_children_display(rhs.m_max_children_display),
2037 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Ticedaccaa92010-09-20 20:44:432038{
2039 if (m_instance_name != InstanceSettings::GetDefaultName())
2040 {
2041 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2042 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:432043 }
2044}
2045
2046TargetInstanceSettings::~TargetInstanceSettings ()
2047{
2048}
2049
2050TargetInstanceSettings&
2051TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2052{
2053 if (this != &rhs)
2054 {
2055 }
2056
2057 return *this;
2058}
2059
Caroline Ticedaccaa92010-09-20 20:44:432060void
2061TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2062 const char *index_value,
2063 const char *value,
2064 const ConstString &instance_name,
2065 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:542066 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:432067 Error &err,
2068 bool pending)
2069{
Greg Claytonbfe5f3b2011-02-18 01:44:252070 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:032071 {
Greg Clayton7e14f912011-04-23 02:04:552072 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2073 if (err.Success())
Sean Callanan322f5292010-10-29 00:29:032074 {
Greg Clayton7e14f912011-04-23 02:04:552075 switch (op)
Sean Callanan322f5292010-10-29 00:29:032076 {
Greg Clayton7e14f912011-04-23 02:04:552077 default:
2078 break;
2079 case eVarSetOperationAssign:
2080 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:032081 {
Greg Clayton7e14f912011-04-23 02:04:552082 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2083 {
2084 err.SetErrorToGenericError ();
2085 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
2086 return;
2087 }
2088
2089 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
2090
2091 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
2092 {
2093 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
2094 m_expr_prefix_contents_sp.reset();
2095 }
Sean Callanan322f5292010-10-29 00:29:032096 }
Greg Clayton7e14f912011-04-23 02:04:552097 break;
2098 case eVarSetOperationClear:
2099 m_expr_prefix_contents_sp.reset();
Sean Callanan322f5292010-10-29 00:29:032100 }
Sean Callanan322f5292010-10-29 00:29:032101 }
2102 }
Jim Ingham78a685a2011-04-16 00:01:132103 else if (var_name == GetSettingNameForPreferDynamicValue())
2104 {
Jim Ingham2837b762011-05-04 03:43:182105 int new_value;
2106 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2107 if (err.Success())
2108 m_prefer_dynamic_value = new_value;
Greg Clayton385aa282011-04-22 03:55:062109 }
2110 else if (var_name == GetSettingNameForSkipPrologue())
2111 {
Greg Clayton7e14f912011-04-23 02:04:552112 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2113 }
Enrico Granata22c55d12011-08-12 02:00:062114 else if (var_name == GetSettingNameForMaxChildren())
2115 {
2116 bool ok;
2117 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2118 if (ok)
2119 m_max_children_display = new_value;
2120 }
Enrico Granata9128ee2f2011-09-06 19:20:512121 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2122 {
2123 bool ok;
2124 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2125 if (ok)
2126 m_max_strlen_length = new_value;
2127 }
Greg Clayton7e14f912011-04-23 02:04:552128 else if (var_name == GetSettingNameForSourcePathMap ())
2129 {
2130 switch (op)
2131 {
2132 case eVarSetOperationReplace:
2133 case eVarSetOperationInsertBefore:
2134 case eVarSetOperationInsertAfter:
2135 case eVarSetOperationRemove:
2136 default:
2137 break;
2138 case eVarSetOperationAssign:
2139 m_source_map.Clear(true);
2140 // Fall through to append....
2141 case eVarSetOperationAppend:
2142 {
2143 Args args(value);
2144 const uint32_t argc = args.GetArgumentCount();
2145 if (argc & 1 || argc == 0)
2146 {
2147 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2148 }
2149 else
2150 {
2151 char resolved_new_path[PATH_MAX];
2152 FileSpec file_spec;
2153 const char *old_path;
2154 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2155 {
2156 const char *new_path = args.GetArgumentAtIndex(idx+1);
2157 assert (new_path); // We have an even number of paths, this shouldn't happen!
2158
2159 file_spec.SetFile(new_path, true);
2160 if (file_spec.Exists())
2161 {
2162 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2163 {
2164 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2165 return;
2166 }
2167 }
2168 else
2169 {
2170 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2171 return;
2172 }
2173 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2174 }
2175 }
2176 }
2177 break;
2178
2179 case eVarSetOperationClear:
2180 m_source_map.Clear(true);
2181 break;
2182 }
Jim Ingham78a685a2011-04-16 00:01:132183 }
Caroline Ticedaccaa92010-09-20 20:44:432184}
2185
2186void
Greg Claytonbfe5f3b2011-02-18 01:44:252187TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:432188{
Sean Callanan322f5292010-10-29 00:29:032189 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2190
2191 if (!new_settings_ptr)
2192 return;
2193
Greg Clayton7e14f912011-04-23 02:04:552194 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
2195 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
2196 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
2197 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata22c55d12011-08-12 02:00:062198 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata9128ee2f2011-09-06 19:20:512199 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Ticedaccaa92010-09-20 20:44:432200}
2201
Caroline Tice12cecd72010-09-20 21:37:422202bool
Caroline Ticedaccaa92010-09-20 20:44:432203TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2204 const ConstString &var_name,
2205 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:422206 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:432207{
Greg Claytonbfe5f3b2011-02-18 01:44:252208 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:032209 {
Greg Clayton7e14f912011-04-23 02:04:552210 char path[PATH_MAX];
2211 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2212 if (path_len > 0)
2213 value.AppendString (path, path_len);
Sean Callanan322f5292010-10-29 00:29:032214 }
Jim Ingham78a685a2011-04-16 00:01:132215 else if (var_name == GetSettingNameForPreferDynamicValue())
2216 {
Jim Ingham2837b762011-05-04 03:43:182217 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Ingham78a685a2011-04-16 00:01:132218 }
Greg Clayton385aa282011-04-22 03:55:062219 else if (var_name == GetSettingNameForSkipPrologue())
2220 {
2221 if (m_skip_prologue)
2222 value.AppendString ("true");
2223 else
2224 value.AppendString ("false");
2225 }
Greg Clayton7e14f912011-04-23 02:04:552226 else if (var_name == GetSettingNameForSourcePathMap ())
2227 {
2228 }
Enrico Granata22c55d12011-08-12 02:00:062229 else if (var_name == GetSettingNameForMaxChildren())
2230 {
2231 StreamString count_str;
2232 count_str.Printf ("%d", m_max_children_display);
2233 value.AppendString (count_str.GetData());
2234 }
Enrico Granata9128ee2f2011-09-06 19:20:512235 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2236 {
2237 StreamString count_str;
2238 count_str.Printf ("%d", m_max_strlen_length);
2239 value.AppendString (count_str.GetData());
2240 }
Sean Callanan322f5292010-10-29 00:29:032241 else
2242 {
2243 if (err)
2244 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2245 return false;
2246 }
2247
2248 return true;
Caroline Ticedaccaa92010-09-20 20:44:432249}
2250
2251const ConstString
2252TargetInstanceSettings::CreateInstanceName ()
2253{
Caroline Ticedaccaa92010-09-20 20:44:432254 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:102255 static int instance_count = 1;
2256
Caroline Ticedaccaa92010-09-20 20:44:432257 sstr.Printf ("target_%d", instance_count);
2258 ++instance_count;
2259
2260 const ConstString ret_val (sstr.GetData());
2261 return ret_val;
2262}
2263
2264//--------------------------------------------------
2265// Target::SettingsController Variable Tables
2266//--------------------------------------------------
Jim Ingham2837b762011-05-04 03:43:182267OptionEnumValueElement
2268TargetInstanceSettings::g_dynamic_value_types[] =
2269{
Greg Clayton5d2fbfe2011-05-30 00:39:482270{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2271{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2272{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham2837b762011-05-04 03:43:182273{ 0, NULL, NULL }
2274};
Caroline Ticedaccaa92010-09-20 20:44:432275
2276SettingEntry
2277Target::SettingsController::global_settings_table[] =
2278{
Greg Claytonbfe5f3b2011-02-18 01:44:252279 // var-name var-type default enum init'd hidden help-text
2280 // ================= ================== =========== ==== ====== ====== =========================================================================
2281 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2282 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2283};
2284
Caroline Ticedaccaa92010-09-20 20:44:432285SettingEntry
2286Target::SettingsController::instance_settings_table[] =
2287{
Enrico Granata9128ee2f2011-09-06 19:20:512288 // var-name var-type default enum init'd hidden help-text
2289 // ================= ================== =============== ======================= ====== ====== =========================================================================
2290 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2291 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2292 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2293 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2294 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2295 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2296 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:432297};