blob: 60831baae058872f03d236bfd1fc51a3bbe6f475 [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"
Greg Clayton8b2fe6d2010-12-14 02:59:5921#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2422#include "lldb/Core/Event.h"
23#include "lldb/Core/Log.h"
Caroline Tice969ed3d12011-05-02 20:41:4624#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner30fdc8d2010-06-08 16:52:2425#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5926#include "lldb/Core/Timer.h"
27#include "lldb/Core/ValueObject.h"
Greg Claytoneb0103f2011-04-07 22:46:3528#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:2429#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:5930#include "lldb/Interpreter/CommandInterpreter.h"
31#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2432#include "lldb/lldb-private-log.h"
33#include "lldb/Symbol/ObjectFile.h"
34#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5935#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:5936#include "lldb/Target/Thread.h"
37#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:2438
39using namespace lldb;
40using namespace lldb_private;
41
42//----------------------------------------------------------------------
43// Target constructor
44//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:5145Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
46 Broadcaster ("lldb.target"),
47 ExecutionContextScope (),
Greg Claytondbe54502010-11-19 03:46:0148 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2949 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:5150 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:2351 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:5152 m_arch (target_arch),
53 m_images (),
Greg Claytonf5e56de2010-09-14 23:36:4054 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2455 m_breakpoint_list (false),
56 m_internal_breakpoint_list (true),
Johnny Chen1d6bad02011-09-06 20:05:2557 m_watchpoint_location_list (),
Greg Clayton32e0a752011-03-30 18:16:5158 m_process_sp (),
59 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:2460 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5961 m_scratch_ast_context_ap (NULL),
Jim Ingham9575d842011-03-11 03:53:5962 m_persistent_variables (),
Jim Inghame37d6052011-09-13 00:29:5663 m_source_manager(*this),
Greg Clayton32e0a752011-03-30 18:16:5164 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:1465 m_stop_hook_next_id (0),
66 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:2467{
Greg Claytoncfd1ace2010-10-31 03:01:0668 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
69 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
70 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
71
Greg Clayton2d4edfb2010-11-06 01:53:3072 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2473 if (log)
74 log->Printf ("%p Target::Target()", this);
75}
76
77//----------------------------------------------------------------------
78// Destructor
79//----------------------------------------------------------------------
80Target::~Target()
81{
Greg Clayton2d4edfb2010-11-06 01:53:3082 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2483 if (log)
84 log->Printf ("%p Target::~Target()", this);
85 DeleteCurrentProcess ();
86}
87
88void
Caroline Ticeceb6b132010-10-26 03:11:1389Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2490{
Greg Clayton89411422010-10-08 00:21:0591// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1392 if (description_level != lldb::eDescriptionLevelBrief)
93 {
94 s->Indent();
95 s->PutCString("Target\n");
96 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:3597 m_images.Dump(s);
98 m_breakpoint_list.Dump(s);
99 m_internal_breakpoint_list.Dump(s);
100 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13101 }
102 else
103 {
Greg Claytonaa149cb2011-08-11 02:48:45104 Module *exe_module = GetExecutableModulePointer();
105 if (exe_module)
106 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28107 else
108 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13109 }
Chris Lattner30fdc8d2010-06-08 16:52:24110}
111
112void
113Target::DeleteCurrentProcess ()
114{
115 if (m_process_sp.get())
116 {
Greg Clayton17f69202010-09-14 23:52:43117 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24118 if (m_process_sp->IsAlive())
119 m_process_sp->Destroy();
Jim Inghamd0a3e12b2011-02-16 17:54:55120
121 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24122
123 // Do any cleanup of the target we need to do between process instances.
124 // NB It is better to do this before destroying the process in case the
125 // clean up needs some help from the process.
126 m_breakpoint_list.ClearAllBreakpointSites();
127 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenedf50372011-09-23 21:21:43128 // Disable watchpoint locations just on the debugger side.
129 DisableAllWatchpointLocations(false);
Chris Lattner30fdc8d2010-06-08 16:52:24130 m_process_sp.reset();
131 }
132}
133
134const lldb::ProcessSP &
135Target::CreateProcess (Listener &listener, const char *plugin_name)
136{
137 DeleteCurrentProcess ();
138 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
139 return m_process_sp;
140}
141
142const lldb::ProcessSP &
143Target::GetProcessSP () const
144{
145 return m_process_sp;
146}
147
148lldb::TargetSP
149Target::GetSP()
150{
Greg Clayton4d122c42011-09-17 08:33:22151 // This object contains an instrusive ref count base class so we can
152 // easily make a shared pointer to this object
153 return TargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24154}
155
Greg Clayton3418c852011-08-10 02:10:13156void
157Target::Destroy()
158{
159 Mutex::Locker locker (m_mutex);
160 DeleteCurrentProcess ();
161 m_platform_sp.reset();
162 m_arch.Clear();
163 m_images.Clear();
164 m_section_load_list.Clear();
165 const bool notify = false;
166 m_breakpoint_list.RemoveAll(notify);
167 m_internal_breakpoint_list.RemoveAll(notify);
168 m_last_created_breakpoint.reset();
169 m_search_filter_sp.reset();
170 m_image_search_paths.Clear(notify);
171 m_scratch_ast_context_ap.reset();
172 m_persistent_variables.Clear();
173 m_stop_hooks.clear();
174 m_stop_hook_next_id = 0;
175 m_suppress_stop_hooks = false;
176}
177
178
Chris Lattner30fdc8d2010-06-08 16:52:24179BreakpointList &
180Target::GetBreakpointList(bool internal)
181{
182 if (internal)
183 return m_internal_breakpoint_list;
184 else
185 return m_breakpoint_list;
186}
187
188const BreakpointList &
189Target::GetBreakpointList(bool internal) const
190{
191 if (internal)
192 return m_internal_breakpoint_list;
193 else
194 return m_breakpoint_list;
195}
196
197BreakpointSP
198Target::GetBreakpointByID (break_id_t break_id)
199{
200 BreakpointSP bp_sp;
201
202 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
203 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
204 else
205 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
206
207 return bp_sp;
208}
209
210BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11211Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
212 const FileSpecList *source_file_spec_list,
Jim Ingham969795f2011-09-21 01:17:13213 RegularExpression &source_regex,
214 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24215{
Jim Ingham87df91b2011-09-23 00:54:11216 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
217 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham969795f2011-09-21 01:17:13218 return CreateBreakpoint (filter_sp, resolver_sp, internal);
219}
220
221
222BreakpointSP
223Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
224{
225 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner30fdc8d2010-06-08 16:52:24226 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
227 return CreateBreakpoint (filter_sp, resolver_sp, internal);
228}
229
230
231BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41232Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24233{
Chris Lattner30fdc8d2010-06-08 16:52:24234 Address so_addr;
235 // Attempt to resolve our load address if possible, though it is ok if
236 // it doesn't resolve to section/offset.
237
Greg Clayton1b72fcb2010-08-24 00:45:41238 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40239 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41240 if (!so_addr.IsValid())
241 {
242 // The address didn't resolve, so just set this as an absolute address
243 so_addr.SetOffset (addr);
244 }
245 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24246 return bp_sp;
247}
248
249BreakpointSP
250Target::CreateBreakpoint (Address &addr, bool internal)
251{
252 TargetSP target_sp = this->GetSP();
253 SearchFilterSP filter_sp(new SearchFilter (target_sp));
254 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
255 return CreateBreakpoint (filter_sp, resolver_sp, internal);
256}
257
258BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11259Target::CreateBreakpoint (const FileSpecList *containingModules,
260 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17261 const char *func_name,
262 uint32_t func_name_type_mask,
263 bool internal,
264 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24265{
Greg Clayton0c5cd902010-06-28 21:30:43266 BreakpointSP bp_sp;
267 if (func_name)
268 {
Jim Ingham87df91b2011-09-23 00:54:11269 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17270
271 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
272 func_name,
273 func_name_type_mask,
274 Breakpoint::Exact,
275 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton0c5cd902010-06-28 21:30:43276 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
277 }
278 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24279}
280
281
282SearchFilterSP
283Target::GetSearchFilterForModule (const FileSpec *containingModule)
284{
285 SearchFilterSP filter_sp;
286 lldb::TargetSP target_sp = this->GetSP();
287 if (containingModule != NULL)
288 {
289 // TODO: We should look into sharing module based search filters
290 // across many breakpoints like we do for the simple target based one
291 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
292 }
293 else
294 {
295 if (m_search_filter_sp.get() == NULL)
296 m_search_filter_sp.reset (new SearchFilter (target_sp));
297 filter_sp = m_search_filter_sp;
298 }
299 return filter_sp;
300}
301
Jim Ingham969795f2011-09-21 01:17:13302SearchFilterSP
303Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
304{
305 SearchFilterSP filter_sp;
306 lldb::TargetSP target_sp = this->GetSP();
307 if (containingModules && containingModules->GetSize() != 0)
308 {
309 // TODO: We should look into sharing module based search filters
310 // across many breakpoints like we do for the simple target based one
311 filter_sp.reset (new SearchFilterByModuleList (target_sp, *containingModules));
312 }
313 else
314 {
315 if (m_search_filter_sp.get() == NULL)
316 m_search_filter_sp.reset (new SearchFilter (target_sp));
317 filter_sp = m_search_filter_sp;
318 }
319 return filter_sp;
320}
321
Jim Ingham87df91b2011-09-23 00:54:11322SearchFilterSP
323Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles)
324{
325 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
326 return GetSearchFilterForModuleList(containingModules);
327
328 SearchFilterSP filter_sp;
329 lldb::TargetSP target_sp = this->GetSP();
330 if (containingModules == NULL)
331 {
332 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
333 // but that will take a little reworking.
334
335 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, FileSpecList(), *containingSourceFiles));
336 }
337 else
338 {
339 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, *containingModules, *containingSourceFiles));
340 }
341 return filter_sp;
342}
343
Chris Lattner30fdc8d2010-06-08 16:52:24344BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11345Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
346 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17347 RegularExpression &func_regex,
348 bool internal,
349 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24350{
Jim Ingham87df91b2011-09-23 00:54:11351 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17352 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
353 func_regex,
354 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24355
356 return CreateBreakpoint (filter_sp, resolver_sp, internal);
357}
358
359BreakpointSP
360Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
361{
362 BreakpointSP bp_sp;
363 if (filter_sp && resolver_sp)
364 {
365 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
366 resolver_sp->SetBreakpoint (bp_sp.get());
367
368 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17369 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24370 else
Greg Clayton9fed0d82010-07-23 23:33:17371 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24372
Greg Clayton2d4edfb2010-11-06 01:53:30373 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24374 if (log)
375 {
376 StreamString s;
377 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
378 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
379 }
380
Chris Lattner30fdc8d2010-06-08 16:52:24381 bp_sp->ResolveBreakpoint();
382 }
Jim Ingham36f3b362010-10-14 23:45:03383
384 if (!internal && bp_sp)
385 {
386 m_last_created_breakpoint = bp_sp;
387 }
388
Chris Lattner30fdc8d2010-06-08 16:52:24389 return bp_sp;
390}
391
Johnny Chen86364b42011-09-20 23:28:55392bool
393Target::ProcessIsValid()
394{
395 return (m_process_sp && m_process_sp->IsAlive());
396}
397
Johnny Chenab9ee762011-09-14 00:26:03398// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
399// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen887062a2011-09-12 23:38:44400WatchpointLocationSP
401Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
402{
Johnny Chen0c406372011-09-14 20:23:45403 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
404 if (log)
405 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
406 __FUNCTION__, addr, size, type);
407
Johnny Chen887062a2011-09-12 23:38:44408 WatchpointLocationSP wp_loc_sp;
Johnny Chen86364b42011-09-20 23:28:55409 if (!ProcessIsValid())
Johnny Chen2fd89a02011-09-13 18:30:59410 return wp_loc_sp;
Johnny Chen45e541f2011-09-14 22:20:15411 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen887062a2011-09-12 23:38:44412 return wp_loc_sp;
Johnny Chen7313a642011-09-13 01:15:36413
Johnny Chenab9ee762011-09-14 00:26:03414 // Currently we only support one watchpoint location per address, with total
415 // number of watchpoint locations limited by the hardware which the inferior
416 // is running on.
Johnny Chen3c532582011-09-13 23:29:31417 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
418 if (matched_sp)
419 {
Johnny Chen0c406372011-09-14 20:23:45420 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31421 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45422 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
423 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen45e541f2011-09-14 22:20:15424 // Return the existing watchpoint location if both size and type match.
425 if (size == old_size && type == old_type) {
426 wp_loc_sp = matched_sp;
427 wp_loc_sp->SetEnabled(false);
428 } else {
429 // Nil the matched watchpoint location; we will be creating a new one.
430 m_process_sp->DisableWatchpoint(matched_sp.get());
431 m_watchpoint_location_list.Remove(matched_sp->GetID());
432 }
Johnny Chen3c532582011-09-13 23:29:31433 }
434
Johnny Chen45e541f2011-09-14 22:20:15435 if (!wp_loc_sp) {
436 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
437 if (!new_loc) {
438 printf("WatchpointLocation ctor failed, out of memory?\n");
439 return wp_loc_sp;
440 }
441 new_loc->SetWatchpointType(type);
442 wp_loc_sp.reset(new_loc);
443 m_watchpoint_location_list.Add(wp_loc_sp);
444 }
Johnny Chen0c406372011-09-14 20:23:45445
Johnny Chen0c406372011-09-14 20:23:45446 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen0c406372011-09-14 20:23:45447 if (log)
448 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
449 __FUNCTION__,
450 rc.Success() ? "succeeded" : "failed",
451 wp_loc_sp->GetID());
452
Johnny Chen45e541f2011-09-14 22:20:15453 if (rc.Fail()) wp_loc_sp.reset();
Johnny Chen7313a642011-09-13 01:15:36454 return wp_loc_sp;
Johnny Chen887062a2011-09-12 23:38:44455}
456
Chris Lattner30fdc8d2010-06-08 16:52:24457void
458Target::RemoveAllBreakpoints (bool internal_also)
459{
Greg Clayton2d4edfb2010-11-06 01:53:30460 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24461 if (log)
462 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
463
Greg Clayton9fed0d82010-07-23 23:33:17464 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24465 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17466 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03467
468 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24469}
470
471void
472Target::DisableAllBreakpoints (bool internal_also)
473{
Greg Clayton2d4edfb2010-11-06 01:53:30474 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24475 if (log)
476 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
477
478 m_breakpoint_list.SetEnabledAll (false);
479 if (internal_also)
480 m_internal_breakpoint_list.SetEnabledAll (false);
481}
482
483void
484Target::EnableAllBreakpoints (bool internal_also)
485{
Greg Clayton2d4edfb2010-11-06 01:53:30486 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24487 if (log)
488 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
489
490 m_breakpoint_list.SetEnabledAll (true);
491 if (internal_also)
492 m_internal_breakpoint_list.SetEnabledAll (true);
493}
494
495bool
496Target::RemoveBreakpointByID (break_id_t break_id)
497{
Greg Clayton2d4edfb2010-11-06 01:53:30498 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24499 if (log)
500 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
501
502 if (DisableBreakpointByID (break_id))
503 {
504 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17505 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24506 else
Jim Ingham36f3b362010-10-14 23:45:03507 {
Greg Claytonaa1c5872011-01-24 23:35:47508 if (m_last_created_breakpoint)
509 {
510 if (m_last_created_breakpoint->GetID() == break_id)
511 m_last_created_breakpoint.reset();
512 }
Greg Clayton9fed0d82010-07-23 23:33:17513 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03514 }
Chris Lattner30fdc8d2010-06-08 16:52:24515 return true;
516 }
517 return false;
518}
519
520bool
521Target::DisableBreakpointByID (break_id_t break_id)
522{
Greg Clayton2d4edfb2010-11-06 01:53:30523 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24524 if (log)
525 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
526
527 BreakpointSP bp_sp;
528
529 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
530 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
531 else
532 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
533 if (bp_sp)
534 {
535 bp_sp->SetEnabled (false);
536 return true;
537 }
538 return false;
539}
540
541bool
542Target::EnableBreakpointByID (break_id_t break_id)
543{
Greg Clayton2d4edfb2010-11-06 01:53:30544 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24545 if (log)
546 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
547 __FUNCTION__,
548 break_id,
549 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
550
551 BreakpointSP bp_sp;
552
553 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
554 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
555 else
556 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
557
558 if (bp_sp)
559 {
560 bp_sp->SetEnabled (true);
561 return true;
562 }
563 return false;
564}
565
Johnny Chenedf50372011-09-23 21:21:43566// The flag 'end_to_end', default to true, signifies that the operation is
567// performed end to end, for both the debugger and the debuggee.
568
569// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
570// for end to end operations.
Johnny Chen86364b42011-09-20 23:28:55571bool
Johnny Chenedf50372011-09-23 21:21:43572Target::RemoveAllWatchpointLocations (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55573{
574 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
575 if (log)
576 log->Printf ("Target::%s\n", __FUNCTION__);
577
Johnny Chenedf50372011-09-23 21:21:43578 if (!end_to_end) {
579 m_watchpoint_location_list.RemoveAll();
580 return true;
581 }
582
583 // Otherwise, it's an end to end operation.
584
Johnny Chen86364b42011-09-20 23:28:55585 if (!ProcessIsValid())
586 return false;
587
588 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
589 for (size_t i = 0; i < num_watchpoints; ++i)
590 {
591 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
592 if (!wp_loc_sp)
593 return false;
594
595 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
596 if (rc.Fail())
597 return false;
598 }
599 m_watchpoint_location_list.RemoveAll ();
600 return true; // Success!
601}
602
Johnny Chenedf50372011-09-23 21:21:43603// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
604// for end to end operations.
Johnny Chen86364b42011-09-20 23:28:55605bool
Johnny Chenedf50372011-09-23 21:21:43606Target::DisableAllWatchpointLocations (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55607{
608 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
609 if (log)
610 log->Printf ("Target::%s\n", __FUNCTION__);
611
Johnny Chenedf50372011-09-23 21:21:43612 if (!end_to_end) {
613 m_watchpoint_location_list.SetEnabledAll(false);
614 return true;
615 }
616
617 // Otherwise, it's an end to end operation.
618
Johnny Chen86364b42011-09-20 23:28:55619 if (!ProcessIsValid())
620 return false;
621
622 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
623 for (size_t i = 0; i < num_watchpoints; ++i)
624 {
625 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
626 if (!wp_loc_sp)
627 return false;
628
629 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
630 if (rc.Fail())
631 return false;
632 }
Johnny Chen86364b42011-09-20 23:28:55633 return true; // Success!
634}
635
Johnny Chenedf50372011-09-23 21:21:43636// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
637// for end to end operations.
Johnny Chen86364b42011-09-20 23:28:55638bool
Johnny Chenedf50372011-09-23 21:21:43639Target::EnableAllWatchpointLocations (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55640{
641 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
642 if (log)
643 log->Printf ("Target::%s\n", __FUNCTION__);
644
Johnny Chenedf50372011-09-23 21:21:43645 if (!end_to_end) {
646 m_watchpoint_location_list.SetEnabledAll(true);
647 return true;
648 }
649
650 // Otherwise, it's an end to end operation.
651
Johnny Chen86364b42011-09-20 23:28:55652 if (!ProcessIsValid())
653 return false;
654
655 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
656 for (size_t i = 0; i < num_watchpoints; ++i)
657 {
658 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
659 if (!wp_loc_sp)
660 return false;
661
662 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
663 if (rc.Fail())
664 return false;
665 }
Johnny Chen86364b42011-09-20 23:28:55666 return true; // Success!
667}
668
Johnny Chenedf50372011-09-23 21:21:43669// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chen86364b42011-09-20 23:28:55670bool
671Target::DisableWatchpointLocationByID (lldb::watch_id_t watch_id)
672{
673 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
674 if (log)
675 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
676
677 if (!ProcessIsValid())
678 return false;
679
680 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
681 if (wp_loc_sp)
682 {
683 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58684 if (rc.Success())
685 return true;
Johnny Chen86364b42011-09-20 23:28:55686
Johnny Chenf04ee932011-09-22 18:04:58687 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55688 }
689 return false;
690}
691
Johnny Chenedf50372011-09-23 21:21:43692// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chen86364b42011-09-20 23:28:55693bool
694Target::EnableWatchpointLocationByID (lldb::watch_id_t watch_id)
695{
696 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
697 if (log)
698 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
699
700 if (!ProcessIsValid())
701 return false;
702
703 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
704 if (wp_loc_sp)
705 {
706 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58707 if (rc.Success())
708 return true;
Johnny Chen86364b42011-09-20 23:28:55709
Johnny Chenf04ee932011-09-22 18:04:58710 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55711 }
712 return false;
713}
714
Johnny Chenedf50372011-09-23 21:21:43715// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chen86364b42011-09-20 23:28:55716bool
717Target::RemoveWatchpointLocationByID (lldb::watch_id_t watch_id)
718{
719 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
720 if (log)
721 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
722
723 if (DisableWatchpointLocationByID (watch_id))
724 {
725 m_watchpoint_location_list.Remove(watch_id);
726 return true;
727 }
728 return false;
729}
730
Chris Lattner30fdc8d2010-06-08 16:52:24731ModuleSP
732Target::GetExecutableModule ()
733{
Greg Claytonaa149cb2011-08-11 02:48:45734 return m_images.GetModuleAtIndex(0);
735}
736
737Module*
738Target::GetExecutableModulePointer ()
739{
740 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24741}
742
743void
744Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
745{
746 m_images.Clear();
747 m_scratch_ast_context_ap.reset();
748
749 if (executable_sp.get())
750 {
751 Timer scoped_timer (__PRETTY_FUNCTION__,
752 "Target::SetExecutableModule (executable = '%s/%s')",
753 executable_sp->GetFileSpec().GetDirectory().AsCString(),
754 executable_sp->GetFileSpec().GetFilename().AsCString());
755
756 m_images.Append(executable_sp); // The first image is our exectuable file
757
Jim Ingham5aee1622010-08-09 23:31:02758 // 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:51759 if (!m_arch.IsValid())
760 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02761
Chris Lattner30fdc8d2010-06-08 16:52:24762 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15763 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamb7f6b2f2011-09-08 22:13:49764 // Let's find the file & line for main and set the default source file from there.
765 if (!m_source_manager.DefaultFileAndLineSet())
766 {
767 SymbolContextList sc_list;
768 uint32_t num_matches;
769 ConstString main_name("main");
770 bool symbols_okay = false; // Force it to be a debug symbol.
771 bool append = false;
772 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
773 for (uint32_t idx = 0; idx < num_matches; idx++)
774 {
775 SymbolContext sc;
776 sc_list.GetContextAtIndex(idx, sc);
777 if (sc.line_entry.file)
778 {
779 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
780 break;
781 }
782 }
783 }
Chris Lattner30fdc8d2010-06-08 16:52:24784
Greg Claytoncac9c5f2011-09-24 00:52:29785 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24786 {
787 executable_objfile->GetDependentModules(dependent_files);
788 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
789 {
Greg Claytonded470d2011-03-19 01:12:21790 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
791 FileSpec platform_dependent_file_spec;
792 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55793 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21794 else
795 platform_dependent_file_spec = dependent_file_spec;
796
797 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton32e0a752011-03-30 18:16:51798 m_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24799 if (image_module_sp.get())
800 {
Chris Lattner30fdc8d2010-06-08 16:52:24801 ObjectFile *objfile = image_module_sp->GetObjectFile();
802 if (objfile)
803 objfile->GetDependentModules(dependent_files);
804 }
805 }
806 }
807
Chris Lattner30fdc8d2010-06-08 16:52:24808 }
Caroline Tice1559a462010-09-27 00:30:10809
810 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24811}
812
813
Jim Ingham5aee1622010-08-09 23:31:02814bool
815Target::SetArchitecture (const ArchSpec &arch_spec)
816{
Greg Clayton32e0a752011-03-30 18:16:51817 if (m_arch == arch_spec)
Jim Ingham5aee1622010-08-09 23:31:02818 {
819 // If we're setting the architecture to our current architecture, we
820 // don't need to do anything.
821 return true;
822 }
Greg Clayton32e0a752011-03-30 18:16:51823 else if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02824 {
825 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton32e0a752011-03-30 18:16:51826 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02827 return true;
828 }
829 else
830 {
831 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51832 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02833 ModuleSP executable_sp = GetExecutableModule ();
834 m_images.Clear();
835 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02836 // Need to do something about unsetting breakpoints.
837
838 if (executable_sp)
839 {
840 FileSpec exec_file_spec = executable_sp->GetFileSpec();
841 Error error = ModuleList::GetSharedModule(exec_file_spec,
842 arch_spec,
843 NULL,
844 NULL,
845 0,
846 executable_sp,
847 NULL,
848 NULL);
849
850 if (!error.Fail() && executable_sp)
851 {
852 SetExecutableModule (executable_sp, true);
853 return true;
854 }
855 else
856 {
857 return false;
858 }
859 }
860 else
861 {
862 return false;
863 }
864 }
865}
Chris Lattner30fdc8d2010-06-08 16:52:24866
Chris Lattner30fdc8d2010-06-08 16:52:24867void
868Target::ModuleAdded (ModuleSP &module_sp)
869{
870 // A module is being added to this target for the first time
871 ModuleList module_list;
872 module_list.Append(module_sp);
873 ModulesDidLoad (module_list);
874}
875
876void
877Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
878{
Jim Inghame716ae02011-08-03 01:00:06879 // A module is replacing an already added module
Chris Lattner30fdc8d2010-06-08 16:52:24880 ModuleList module_list;
881 module_list.Append (old_module_sp);
882 ModulesDidUnload (module_list);
883 module_list.Clear ();
884 module_list.Append (new_module_sp);
885 ModulesDidLoad (module_list);
886}
887
888void
889Target::ModulesDidLoad (ModuleList &module_list)
890{
891 m_breakpoint_list.UpdateBreakpoints (module_list, true);
892 // TODO: make event data that packages up the module_list
893 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
894}
895
896void
897Target::ModulesDidUnload (ModuleList &module_list)
898{
899 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26900
901 // Remove the images from the target image list
902 m_images.Remove(module_list);
903
Chris Lattner30fdc8d2010-06-08 16:52:24904 // TODO: make event data that packages up the module_list
905 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
906}
907
908size_t
Greg Claytondb598232011-01-07 01:57:07909Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
910{
911 const Section *section = addr.GetSection();
912 if (section && section->GetModule())
913 {
914 ObjectFile *objfile = section->GetModule()->GetObjectFile();
915 if (objfile)
916 {
917 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
918 addr.GetOffset(),
919 dst,
920 dst_len);
921 if (bytes_read > 0)
922 return bytes_read;
923 else
924 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
925 }
926 else
927 {
928 error.SetErrorString("address isn't from a object file");
929 }
930 }
931 else
932 {
933 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
934 }
935 return 0;
936}
937
938size_t
Enrico Granata9128ee2f2011-09-06 19:20:51939Target::ReadMemory (const Address& addr,
940 bool prefer_file_cache,
941 void *dst,
942 size_t dst_len,
943 Error &error,
944 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24945{
Chris Lattner30fdc8d2010-06-08 16:52:24946 error.Clear();
Greg Claytondb598232011-01-07 01:57:07947
Enrico Granata9128ee2f2011-09-06 19:20:51948 // if we end up reading this from process memory, we will fill this
949 // with the actual load address
950 if (load_addr_ptr)
951 *load_addr_ptr = LLDB_INVALID_ADDRESS;
952
Greg Claytondb598232011-01-07 01:57:07953 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02954
955 addr_t load_addr = LLDB_INVALID_ADDRESS;
956 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58957 Address resolved_addr;
958 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03959 {
Greg Claytond16e1e52011-07-12 17:06:17960 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02961 {
Greg Claytond16e1e52011-07-12 17:06:17962 // No sections are loaded, so we must assume we are not running
963 // yet and anything we are given is a file address.
964 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
965 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02966 }
Greg Claytondda4f7b2010-06-30 23:03:03967 else
Greg Claytonc749eb82011-07-11 05:12:02968 {
Greg Claytond16e1e52011-07-12 17:06:17969 // We have at least one section loaded. This can be becuase
970 // we have manually loaded some sections with "target modules load ..."
971 // or because we have have a live process that has sections loaded
972 // through the dynamic loader
973 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
974 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02975 }
Greg Claytondda4f7b2010-06-30 23:03:03976 }
Greg Clayton357132e2011-03-26 19:14:58977 if (!resolved_addr.IsValid())
978 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03979
Greg Claytonc749eb82011-07-11 05:12:02980
Greg Claytondb598232011-01-07 01:57:07981 if (prefer_file_cache)
982 {
983 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
984 if (bytes_read > 0)
985 return bytes_read;
986 }
Greg Claytondda4f7b2010-06-30 23:03:03987
Johnny Chen86364b42011-09-20 23:28:55988 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03989 {
Greg Claytonc749eb82011-07-11 05:12:02990 if (load_addr == LLDB_INVALID_ADDRESS)
991 load_addr = resolved_addr.GetLoadAddress (this);
992
Greg Claytondda4f7b2010-06-30 23:03:03993 if (load_addr == LLDB_INVALID_ADDRESS)
994 {
995 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
996 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
997 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08998 resolved_addr.GetFileAddress(),
999 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:031000 else
1001 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
1002 }
1003 else
1004 {
Greg Claytondb598232011-01-07 01:57:071005 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:241006 if (bytes_read != dst_len)
1007 {
1008 if (error.Success())
1009 {
1010 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:031011 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:241012 else
Greg Claytondda4f7b2010-06-30 23:03:031013 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:241014 }
1015 }
Greg Claytondda4f7b2010-06-30 23:03:031016 if (bytes_read)
Enrico Granata9128ee2f2011-09-06 19:20:511017 {
1018 if (load_addr_ptr)
1019 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:031020 return bytes_read;
Enrico Granata9128ee2f2011-09-06 19:20:511021 }
Greg Claytondda4f7b2010-06-30 23:03:031022 // If the address is not section offset we have an address that
1023 // doesn't resolve to any address in any currently loaded shared
1024 // libaries and we failed to read memory so there isn't anything
1025 // more we can do. If it is section offset, we might be able to
1026 // read cached memory from the object file.
1027 if (!resolved_addr.IsSectionOffset())
1028 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:241029 }
Chris Lattner30fdc8d2010-06-08 16:52:241030 }
Greg Claytondda4f7b2010-06-30 23:03:031031
Greg Claytonc749eb82011-07-11 05:12:021032 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:031033 {
Greg Claytondb598232011-01-07 01:57:071034 // If we didn't already try and read from the object file cache, then
1035 // try it after failing to read from the process.
1036 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:031037 }
1038 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:241039}
1040
Greg Claytond16e1e52011-07-12 17:06:171041size_t
1042Target::ReadScalarIntegerFromMemory (const Address& addr,
1043 bool prefer_file_cache,
1044 uint32_t byte_size,
1045 bool is_signed,
1046 Scalar &scalar,
1047 Error &error)
1048{
1049 uint64_t uval;
1050
1051 if (byte_size <= sizeof(uval))
1052 {
1053 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1054 if (bytes_read == byte_size)
1055 {
1056 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1057 uint32_t offset = 0;
1058 if (byte_size <= 4)
1059 scalar = data.GetMaxU32 (&offset, byte_size);
1060 else
1061 scalar = data.GetMaxU64 (&offset, byte_size);
1062
1063 if (is_signed)
1064 scalar.SignExtend(byte_size * 8);
1065 return bytes_read;
1066 }
1067 }
1068 else
1069 {
1070 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1071 }
1072 return 0;
1073}
1074
1075uint64_t
1076Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1077 bool prefer_file_cache,
1078 size_t integer_byte_size,
1079 uint64_t fail_value,
1080 Error &error)
1081{
1082 Scalar scalar;
1083 if (ReadScalarIntegerFromMemory (addr,
1084 prefer_file_cache,
1085 integer_byte_size,
1086 false,
1087 scalar,
1088 error))
1089 return scalar.ULongLong(fail_value);
1090 return fail_value;
1091}
1092
1093bool
1094Target::ReadPointerFromMemory (const Address& addr,
1095 bool prefer_file_cache,
1096 Error &error,
1097 Address &pointer_addr)
1098{
1099 Scalar scalar;
1100 if (ReadScalarIntegerFromMemory (addr,
1101 prefer_file_cache,
1102 m_arch.GetAddressByteSize(),
1103 false,
1104 scalar,
1105 error))
1106 {
1107 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1108 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1109 {
1110 if (m_section_load_list.IsEmpty())
1111 {
1112 // No sections are loaded, so we must assume we are not running
1113 // yet and anything we are given is a file address.
1114 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1115 }
1116 else
1117 {
1118 // We have at least one section loaded. This can be becuase
1119 // we have manually loaded some sections with "target modules load ..."
1120 // or because we have have a live process that has sections loaded
1121 // through the dynamic loader
1122 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1123 }
1124 // We weren't able to resolve the pointer value, so just return
1125 // an address with no section
1126 if (!pointer_addr.IsValid())
1127 pointer_addr.SetOffset (pointer_vm_addr);
1128 return true;
1129
1130 }
1131 }
1132 return false;
1133}
Chris Lattner30fdc8d2010-06-08 16:52:241134
1135ModuleSP
1136Target::GetSharedModule
1137(
1138 const FileSpec& file_spec,
1139 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:101140 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:241141 const ConstString *object_name,
1142 off_t object_offset,
1143 Error *error_ptr
1144)
1145{
1146 // Don't pass in the UUID so we can tell if we have a stale value in our list
1147 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1148 bool did_create_module = false;
1149 ModuleSP module_sp;
1150
Chris Lattner30fdc8d2010-06-08 16:52:241151 Error error;
1152
Greg Clayton32e0a752011-03-30 18:16:511153 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner30fdc8d2010-06-08 16:52:241154 if (m_image_search_paths.GetSize())
1155 {
1156 FileSpec transformed_spec;
1157 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1158 {
1159 transformed_spec.GetFilename() = file_spec.GetFilename();
1160 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1161 }
1162 }
1163
Greg Clayton32e0a752011-03-30 18:16:511164 // The platform is responsible for finding and caching an appropriate
1165 // module in the shared module cache.
1166 if (m_platform_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241167 {
Greg Clayton32e0a752011-03-30 18:16:511168 FileSpec platform_file_spec;
1169 error = m_platform_sp->GetSharedModule (file_spec,
1170 arch,
1171 uuid_ptr,
1172 object_name,
1173 object_offset,
1174 module_sp,
1175 &old_module_sp,
1176 &did_create_module);
1177 }
1178 else
1179 {
1180 error.SetErrorString("no platform is currently set");
Chris Lattner30fdc8d2010-06-08 16:52:241181 }
1182
Greg Clayton32e0a752011-03-30 18:16:511183 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner30fdc8d2010-06-08 16:52:241184 if (module_sp)
1185 {
1186 m_images.Append (module_sp);
1187 if (did_create_module)
1188 {
1189 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1190 ModuleUpdated(old_module_sp, module_sp);
1191 else
1192 ModuleAdded(module_sp);
1193 }
1194 }
1195 if (error_ptr)
1196 *error_ptr = error;
1197 return module_sp;
1198}
1199
1200
1201Target *
1202Target::CalculateTarget ()
1203{
1204 return this;
1205}
1206
1207Process *
1208Target::CalculateProcess ()
1209{
1210 return NULL;
1211}
1212
1213Thread *
1214Target::CalculateThread ()
1215{
1216 return NULL;
1217}
1218
1219StackFrame *
1220Target::CalculateStackFrame ()
1221{
1222 return NULL;
1223}
1224
1225void
Greg Clayton0603aa92010-10-04 01:05:561226Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:241227{
Greg Claytonc14ee322011-09-22 04:58:261228 exe_ctx.Clear();
1229 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:241230}
1231
1232PathMappingList &
1233Target::GetImageSearchPathList ()
1234{
1235 return m_image_search_paths;
1236}
1237
1238void
1239Target::ImageSearchPathsChanged
1240(
1241 const PathMappingList &path_list,
1242 void *baton
1243)
1244{
1245 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:451246 ModuleSP exe_module_sp (target->GetExecutableModule());
1247 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241248 {
Greg Claytonaa149cb2011-08-11 02:48:451249 target->m_images.Clear();
1250 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:241251 }
1252}
1253
1254ClangASTContext *
1255Target::GetScratchClangASTContext()
1256{
Greg Clayton73da2442011-08-03 01:23:551257 // Now see if we know the target triple, and if so, create our scratch AST context:
1258 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1259 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:241260 return m_scratch_ast_context_ap.get();
1261}
Caroline Ticedaccaa92010-09-20 20:44:431262
Greg Clayton99d0faf2010-11-18 23:32:351263void
Caroline Tice20bd37f2011-03-10 22:14:101264Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:431265{
Greg Clayton99d0faf2010-11-18 23:32:351266 UserSettingsControllerSP &usc = GetSettingsController();
1267 usc.reset (new SettingsController);
1268 UserSettingsController::InitializeSettingsController (usc,
1269 SettingsController::global_settings_table,
1270 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:101271
1272 // Now call SettingsInitialize() on each 'child' setting of Target
1273 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:351274}
Caroline Ticedaccaa92010-09-20 20:44:431275
Greg Clayton99d0faf2010-11-18 23:32:351276void
Caroline Tice20bd37f2011-03-10 22:14:101277Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:351278{
Caroline Tice20bd37f2011-03-10 22:14:101279
1280 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1281
1282 Process::SettingsTerminate ();
1283
1284 // Now terminate Target Settings.
1285
Greg Clayton99d0faf2010-11-18 23:32:351286 UserSettingsControllerSP &usc = GetSettingsController();
1287 UserSettingsController::FinalizeSettingsController (usc);
1288 usc.reset();
1289}
Caroline Ticedaccaa92010-09-20 20:44:431290
Greg Clayton99d0faf2010-11-18 23:32:351291UserSettingsControllerSP &
1292Target::GetSettingsController ()
1293{
1294 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:431295 return g_settings_controller;
1296}
1297
1298ArchSpec
1299Target::GetDefaultArchitecture ()
1300{
Greg Clayton64195a22011-02-23 00:35:021301 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1302
1303 if (settings_controller_sp)
1304 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1305 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:431306}
1307
1308void
Greg Clayton64195a22011-02-23 00:35:021309Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:431310{
Greg Clayton64195a22011-02-23 00:35:021311 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1312
1313 if (settings_controller_sp)
1314 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:431315}
1316
Greg Clayton0603aa92010-10-04 01:05:561317Target *
1318Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1319{
1320 // The target can either exist in the "process" of ExecutionContext, or in
1321 // the "target_sp" member of SymbolContext. This accessor helper function
1322 // will get the target from one of these locations.
1323
1324 Target *target = NULL;
1325 if (sc_ptr != NULL)
1326 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:261327 if (target == NULL && exe_ctx_ptr)
1328 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:561329 return target;
1330}
1331
1332
Caroline Tice1559a462010-09-27 00:30:101333void
1334Target::UpdateInstanceName ()
1335{
1336 StreamString sstr;
1337
Greg Claytonaa149cb2011-08-11 02:48:451338 Module *exe_module = GetExecutableModulePointer();
1339 if (exe_module)
Caroline Tice1559a462010-09-27 00:30:101340 {
Greg Clayton307de252010-10-27 02:06:371341 sstr.Printf ("%s_%s",
Greg Claytonaa149cb2011-08-11 02:48:451342 exe_module->GetFileSpec().GetFilename().AsCString(),
1343 exe_module->GetArchitecture().GetArchitectureName());
1344 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:101345 }
1346}
1347
Sean Callanan322f5292010-10-29 00:29:031348const char *
1349Target::GetExpressionPrefixContentsAsCString ()
1350{
Greg Clayton7e14f912011-04-23 02:04:551351 if (m_expr_prefix_contents_sp)
1352 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1353 return NULL;
Sean Callanan322f5292010-10-29 00:29:031354}
1355
Greg Clayton8b2fe6d2010-12-14 02:59:591356ExecutionResults
1357Target::EvaluateExpression
1358(
1359 const char *expr_cstr,
1360 StackFrame *frame,
Sean Callanan3bfdaa22011-09-15 02:13:071361 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton8b2fe6d2010-12-14 02:59:591362 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:351363 bool keep_in_memory,
Jim Ingham2837b762011-05-04 03:43:181364 lldb::DynamicValueType use_dynamic,
Greg Clayton8b2fe6d2010-12-14 02:59:591365 lldb::ValueObjectSP &result_valobj_sp
1366)
1367{
1368 ExecutionResults execution_results = eExecutionSetupError;
1369
1370 result_valobj_sp.reset();
Jim Ingham6026ca32011-05-12 02:06:141371
1372 // We shouldn't run stop hooks in expressions.
1373 // Be sure to reset this if you return anywhere within this function.
1374 bool old_suppress_value = m_suppress_stop_hooks;
1375 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:591376
1377 ExecutionContext exe_ctx;
1378 if (frame)
1379 {
1380 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:081381 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:181382 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granata27b625e2011-08-09 01:04:561383 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1384 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham2837b762011-05-04 03:43:181385 lldb::VariableSP var_sp;
1386 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1387 use_dynamic,
1388 expr_path_options,
1389 var_sp,
1390 error);
Greg Clayton8b2fe6d2010-12-14 02:59:591391 }
1392 else if (m_process_sp)
1393 {
1394 m_process_sp->CalculateExecutionContext(exe_ctx);
1395 }
1396 else
1397 {
1398 CalculateExecutionContext(exe_ctx);
1399 }
1400
1401 if (result_valobj_sp)
1402 {
1403 execution_results = eExecutionCompleted;
1404 // We got a result from the frame variable expression path above...
1405 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1406
1407 lldb::ValueObjectSP const_valobj_sp;
1408
1409 // Check in case our value is already a constant value
1410 if (result_valobj_sp->GetIsConstant())
1411 {
1412 const_valobj_sp = result_valobj_sp;
1413 const_valobj_sp->SetName (persistent_variable_name);
1414 }
1415 else
Jim Ingham78a685a2011-04-16 00:01:131416 {
Jim Ingham2837b762011-05-04 03:43:181417 if (use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:131418 {
Jim Ingham2837b762011-05-04 03:43:181419 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:131420 if (dynamic_sp)
1421 result_valobj_sp = dynamic_sp;
1422 }
1423
Jim Ingham6035b672011-03-31 00:19:251424 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:131425 }
Greg Clayton8b2fe6d2010-12-14 02:59:591426
Sean Callanan92adcac2011-01-13 08:53:351427 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1428
Greg Clayton8b2fe6d2010-12-14 02:59:591429 result_valobj_sp = const_valobj_sp;
1430
Sean Callanan92adcac2011-01-13 08:53:351431 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1432 assert (clang_expr_variable_sp.get());
1433
1434 // Set flags and live data as appropriate
1435
1436 const Value &result_value = live_valobj_sp->GetValue();
1437
1438 switch (result_value.GetValueType())
1439 {
1440 case Value::eValueTypeHostAddress:
1441 case Value::eValueTypeFileAddress:
1442 // we don't do anything with these for now
1443 break;
1444 case Value::eValueTypeScalar:
1445 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1446 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1447 break;
1448 case Value::eValueTypeLoadAddress:
1449 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1450 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1451 break;
1452 }
Greg Clayton8b2fe6d2010-12-14 02:59:591453 }
1454 else
1455 {
1456 // Make sure we aren't just trying to see the value of a persistent
1457 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:351458 lldb::ClangExpressionVariableSP persistent_var_sp;
1459 // Only check for persistent variables the expression starts with a '$'
1460 if (expr_cstr[0] == '$')
1461 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1462
Greg Clayton8b2fe6d2010-12-14 02:59:591463 if (persistent_var_sp)
1464 {
1465 result_valobj_sp = persistent_var_sp->GetValueObject ();
1466 execution_results = eExecutionCompleted;
1467 }
1468 else
1469 {
1470 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan3bfdaa22011-09-15 02:13:071471
Greg Clayton8b2fe6d2010-12-14 02:59:591472 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:071473 execution_policy,
Sean Callanan92adcac2011-01-13 08:53:351474 unwind_on_error,
Greg Clayton8b2fe6d2010-12-14 02:59:591475 expr_cstr,
1476 prefix,
1477 result_valobj_sp);
1478 }
1479 }
Jim Ingham6026ca32011-05-12 02:06:141480
1481 m_suppress_stop_hooks = old_suppress_value;
1482
Greg Clayton8b2fe6d2010-12-14 02:59:591483 return execution_results;
1484}
1485
Greg Claytonf3ef3d22011-05-22 22:46:531486lldb::addr_t
1487Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1488{
1489 addr_t code_addr = load_addr;
1490 switch (m_arch.GetMachine())
1491 {
1492 case llvm::Triple::arm:
1493 case llvm::Triple::thumb:
1494 switch (addr_class)
1495 {
1496 case eAddressClassData:
1497 case eAddressClassDebug:
1498 return LLDB_INVALID_ADDRESS;
1499
1500 case eAddressClassUnknown:
1501 case eAddressClassInvalid:
1502 case eAddressClassCode:
1503 case eAddressClassCodeAlternateISA:
1504 case eAddressClassRuntime:
1505 // Check if bit zero it no set?
1506 if ((code_addr & 1ull) == 0)
1507 {
1508 // Bit zero isn't set, check if the address is a multiple of 2?
1509 if (code_addr & 2ull)
1510 {
1511 // The address is a multiple of 2 so it must be thumb, set bit zero
1512 code_addr |= 1ull;
1513 }
1514 else if (addr_class == eAddressClassCodeAlternateISA)
1515 {
1516 // We checked the address and the address claims to be the alternate ISA
1517 // which means thumb, so set bit zero.
1518 code_addr |= 1ull;
1519 }
1520 }
1521 break;
1522 }
1523 break;
1524
1525 default:
1526 break;
1527 }
1528 return code_addr;
1529}
1530
1531lldb::addr_t
1532Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1533{
1534 addr_t opcode_addr = load_addr;
1535 switch (m_arch.GetMachine())
1536 {
1537 case llvm::Triple::arm:
1538 case llvm::Triple::thumb:
1539 switch (addr_class)
1540 {
1541 case eAddressClassData:
1542 case eAddressClassDebug:
1543 return LLDB_INVALID_ADDRESS;
1544
1545 case eAddressClassInvalid:
1546 case eAddressClassUnknown:
1547 case eAddressClassCode:
1548 case eAddressClassCodeAlternateISA:
1549 case eAddressClassRuntime:
1550 opcode_addr &= ~(1ull);
1551 break;
1552 }
1553 break;
1554
1555 default:
1556 break;
1557 }
1558 return opcode_addr;
1559}
1560
Jim Ingham9575d842011-03-11 03:53:591561lldb::user_id_t
1562Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1563{
1564 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1565 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1566 m_stop_hooks[new_uid] = new_hook_sp;
1567 return new_uid;
1568}
1569
1570bool
1571Target::RemoveStopHookByID (lldb::user_id_t user_id)
1572{
1573 size_t num_removed;
1574 num_removed = m_stop_hooks.erase (user_id);
1575 if (num_removed == 0)
1576 return false;
1577 else
1578 return true;
1579}
1580
1581void
1582Target::RemoveAllStopHooks ()
1583{
1584 m_stop_hooks.clear();
1585}
1586
1587Target::StopHookSP
1588Target::GetStopHookByID (lldb::user_id_t user_id)
1589{
1590 StopHookSP found_hook;
1591
1592 StopHookCollection::iterator specified_hook_iter;
1593 specified_hook_iter = m_stop_hooks.find (user_id);
1594 if (specified_hook_iter != m_stop_hooks.end())
1595 found_hook = (*specified_hook_iter).second;
1596 return found_hook;
1597}
1598
1599bool
1600Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1601{
1602 StopHookCollection::iterator specified_hook_iter;
1603 specified_hook_iter = m_stop_hooks.find (user_id);
1604 if (specified_hook_iter == m_stop_hooks.end())
1605 return false;
1606
1607 (*specified_hook_iter).second->SetIsActive (active_state);
1608 return true;
1609}
1610
1611void
1612Target::SetAllStopHooksActiveState (bool active_state)
1613{
1614 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1615 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1616 {
1617 (*pos).second->SetIsActive (active_state);
1618 }
1619}
1620
1621void
1622Target::RunStopHooks ()
1623{
Jim Ingham6026ca32011-05-12 02:06:141624 if (m_suppress_stop_hooks)
1625 return;
1626
Jim Ingham9575d842011-03-11 03:53:591627 if (!m_process_sp)
1628 return;
1629
1630 if (m_stop_hooks.empty())
1631 return;
1632
1633 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1634
1635 // If there aren't any active stop hooks, don't bother either:
1636 bool any_active_hooks = false;
1637 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1638 {
1639 if ((*pos).second->IsActive())
1640 {
1641 any_active_hooks = true;
1642 break;
1643 }
1644 }
1645 if (!any_active_hooks)
1646 return;
1647
1648 CommandReturnObject result;
1649
1650 std::vector<ExecutionContext> exc_ctx_with_reasons;
1651 std::vector<SymbolContext> sym_ctx_with_reasons;
1652
1653 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1654 size_t num_threads = cur_threadlist.GetSize();
1655 for (size_t i = 0; i < num_threads; i++)
1656 {
1657 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1658 if (cur_thread_sp->ThreadStoppedForAReason())
1659 {
1660 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1661 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1662 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1663 }
1664 }
1665
1666 // If no threads stopped for a reason, don't run the stop-hooks.
1667 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1668 if (num_exe_ctx == 0)
1669 return;
1670
Jim Ingham5b52f0c2011-06-02 23:58:261671 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1672 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:591673
1674 bool keep_going = true;
1675 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271676 bool print_hook_header;
1677 bool print_thread_header;
1678
1679 if (num_exe_ctx == 1)
1680 print_thread_header = false;
1681 else
1682 print_thread_header = true;
1683
1684 if (m_stop_hooks.size() == 1)
1685 print_hook_header = false;
1686 else
1687 print_hook_header = true;
1688
Jim Ingham9575d842011-03-11 03:53:591689 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1690 {
1691 // result.Clear();
1692 StopHookSP cur_hook_sp = (*pos).second;
1693 if (!cur_hook_sp->IsActive())
1694 continue;
1695
1696 bool any_thread_matched = false;
1697 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1698 {
1699 if ((cur_hook_sp->GetSpecifier () == NULL
1700 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1701 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Claytonc14ee322011-09-22 04:58:261702 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Ingham9575d842011-03-11 03:53:591703 {
1704 if (!hooks_ran)
1705 {
Jim Ingham9575d842011-03-11 03:53:591706 hooks_ran = true;
1707 }
Jim Ingham381e25b2011-03-22 01:47:271708 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591709 {
1710 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1711 any_thread_matched = true;
1712 }
1713
Jim Ingham381e25b2011-03-22 01:47:271714 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:261715 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591716
1717 bool stop_on_continue = true;
1718 bool stop_on_error = true;
1719 bool echo_commands = false;
1720 bool print_results = true;
1721 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:511722 &exc_ctx_with_reasons[i],
1723 stop_on_continue,
1724 stop_on_error,
1725 echo_commands,
1726 print_results,
1727 result);
Jim Ingham9575d842011-03-11 03:53:591728
1729 // If the command started the target going again, we should bag out of
1730 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:511731 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1732 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:591733 {
1734 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1735 keep_going = false;
1736 }
1737 }
1738 }
1739 }
Jason Molenda879cf772011-09-23 00:42:551740
Caroline Tice969ed3d12011-05-02 20:41:461741 result.GetImmediateOutputStream()->Flush();
1742 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:591743}
1744
Greg Clayton7b242382011-07-08 00:48:091745bool
1746Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1747{
1748 bool changed = false;
1749 if (module)
1750 {
1751 ObjectFile *object_file = module->GetObjectFile();
1752 if (object_file)
1753 {
1754 SectionList *section_list = object_file->GetSectionList ();
1755 if (section_list)
1756 {
1757 // All sections listed in the dyld image info structure will all
1758 // either be fixed up already, or they will all be off by a single
1759 // slide amount that is determined by finding the first segment
1760 // that is at file offset zero which also has bytes (a file size
1761 // that is greater than zero) in the object file.
1762
1763 // Determine the slide amount (if any)
1764 const size_t num_sections = section_list->GetSize();
1765 size_t sect_idx = 0;
1766 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1767 {
1768 // Iterate through the object file sections to find the
1769 // first section that starts of file offset zero and that
1770 // has bytes in the file...
1771 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1772 if (section)
1773 {
1774 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1775 changed = true;
1776 }
1777 }
1778 }
1779 }
1780 }
1781 return changed;
1782}
1783
1784
Jim Ingham9575d842011-03-11 03:53:591785//--------------------------------------------------------------
1786// class Target::StopHook
1787//--------------------------------------------------------------
1788
1789
1790Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1791 UserID (uid),
1792 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:591793 m_commands (),
1794 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:401795 m_thread_spec_ap(NULL),
1796 m_active (true)
Jim Ingham9575d842011-03-11 03:53:591797{
1798}
1799
1800Target::StopHook::StopHook (const StopHook &rhs) :
1801 UserID (rhs.GetID()),
1802 m_target_sp (rhs.m_target_sp),
1803 m_commands (rhs.m_commands),
1804 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:401805 m_thread_spec_ap (NULL),
1806 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:591807{
1808 if (rhs.m_thread_spec_ap.get() != NULL)
1809 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1810}
1811
1812
1813Target::StopHook::~StopHook ()
1814{
1815}
1816
1817void
1818Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1819{
1820 m_thread_spec_ap.reset (specifier);
1821}
1822
1823
1824void
1825Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1826{
1827 int indent_level = s->GetIndentLevel();
1828
1829 s->SetIndentLevel(indent_level + 2);
1830
1831 s->Printf ("Hook: %d\n", GetID());
1832 if (m_active)
1833 s->Indent ("State: enabled\n");
1834 else
1835 s->Indent ("State: disabled\n");
1836
1837 if (m_specifier_sp)
1838 {
1839 s->Indent();
1840 s->PutCString ("Specifier:\n");
1841 s->SetIndentLevel (indent_level + 4);
1842 m_specifier_sp->GetDescription (s, level);
1843 s->SetIndentLevel (indent_level + 2);
1844 }
1845
1846 if (m_thread_spec_ap.get() != NULL)
1847 {
1848 StreamString tmp;
1849 s->Indent("Thread:\n");
1850 m_thread_spec_ap->GetDescription (&tmp, level);
1851 s->SetIndentLevel (indent_level + 4);
1852 s->Indent (tmp.GetData());
1853 s->PutCString ("\n");
1854 s->SetIndentLevel (indent_level + 2);
1855 }
1856
1857 s->Indent ("Commands: \n");
1858 s->SetIndentLevel (indent_level + 4);
1859 uint32_t num_commands = m_commands.GetSize();
1860 for (uint32_t i = 0; i < num_commands; i++)
1861 {
1862 s->Indent(m_commands.GetStringAtIndex(i));
1863 s->PutCString ("\n");
1864 }
1865 s->SetIndentLevel (indent_level);
1866}
1867
1868
Caroline Ticedaccaa92010-09-20 20:44:431869//--------------------------------------------------------------
1870// class Target::SettingsController
1871//--------------------------------------------------------------
1872
1873Target::SettingsController::SettingsController () :
1874 UserSettingsController ("target", Debugger::GetSettingsController()),
1875 m_default_architecture ()
1876{
1877 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1878 InstanceSettings::GetDefaultName().AsCString()));
1879}
1880
1881Target::SettingsController::~SettingsController ()
1882{
1883}
1884
1885lldb::InstanceSettingsSP
1886Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1887{
Greg Claytondbe54502010-11-19 03:46:011888 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1889 false,
1890 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431891 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1892 return new_settings_sp;
1893}
1894
Caroline Ticedaccaa92010-09-20 20:44:431895
Jim Ingham78a685a2011-04-16 00:01:131896#define TSC_DEFAULT_ARCH "default-arch"
1897#define TSC_EXPR_PREFIX "expr-prefix"
Jim Ingham78a685a2011-04-16 00:01:131898#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton385aa282011-04-22 03:55:061899#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Clayton7e14f912011-04-23 02:04:551900#define TSC_SOURCE_MAP "source-map"
Enrico Granata22c55d12011-08-12 02:00:061901#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata9128ee2f2011-09-06 19:20:511902#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytonbfe5f3b2011-02-18 01:44:251903
1904
1905static const ConstString &
1906GetSettingNameForDefaultArch ()
1907{
1908 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytonbfe5f3b2011-02-18 01:44:251909 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431910}
1911
Greg Claytonbfe5f3b2011-02-18 01:44:251912static const ConstString &
1913GetSettingNameForExpressionPrefix ()
1914{
1915 static ConstString g_const_string (TSC_EXPR_PREFIX);
1916 return g_const_string;
1917}
1918
1919static const ConstString &
Jim Ingham78a685a2011-04-16 00:01:131920GetSettingNameForPreferDynamicValue ()
1921{
1922 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1923 return g_const_string;
1924}
1925
Greg Clayton7e14f912011-04-23 02:04:551926static const ConstString &
1927GetSettingNameForSourcePathMap ()
1928{
1929 static ConstString g_const_string (TSC_SOURCE_MAP);
1930 return g_const_string;
1931}
Greg Claytonbfe5f3b2011-02-18 01:44:251932
Greg Clayton385aa282011-04-22 03:55:061933static const ConstString &
1934GetSettingNameForSkipPrologue ()
1935{
1936 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1937 return g_const_string;
1938}
1939
Enrico Granata22c55d12011-08-12 02:00:061940static const ConstString &
1941GetSettingNameForMaxChildren ()
1942{
1943 static ConstString g_const_string (TSC_MAX_CHILDREN);
1944 return g_const_string;
1945}
Greg Clayton385aa282011-04-22 03:55:061946
Enrico Granata9128ee2f2011-09-06 19:20:511947static const ConstString &
1948GetSettingNameForMaxStringSummaryLength ()
1949{
1950 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1951 return g_const_string;
1952}
Greg Clayton385aa282011-04-22 03:55:061953
Caroline Ticedaccaa92010-09-20 20:44:431954bool
1955Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1956 const char *index_value,
1957 const char *value,
1958 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541959 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431960 Error&err)
1961{
Greg Claytonbfe5f3b2011-02-18 01:44:251962 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431963 {
Greg Claytoneb0103f2011-04-07 22:46:351964 m_default_architecture.SetTriple (value, NULL);
Greg Clayton64195a22011-02-23 00:35:021965 if (!m_default_architecture.IsValid())
1966 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431967 }
1968 return true;
1969}
1970
1971
1972bool
1973Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1974 StringList &value,
1975 Error &err)
1976{
Greg Claytonbfe5f3b2011-02-18 01:44:251977 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431978 {
Greg Clayton307de252010-10-27 02:06:371979 // If the arch is invalid (the default), don't show a string for it
1980 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021981 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431982 return true;
1983 }
1984 else
1985 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1986
1987 return false;
1988}
1989
1990//--------------------------------------------------------------
1991// class TargetInstanceSettings
1992//--------------------------------------------------------------
1993
Greg Clayton85851dd2010-12-04 00:10:171994TargetInstanceSettings::TargetInstanceSettings
1995(
1996 UserSettingsController &owner,
1997 bool live_instance,
1998 const char *name
1999) :
Greg Claytonbfe5f3b2011-02-18 01:44:252000 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Clayton7e14f912011-04-23 02:04:552001 m_expr_prefix_file (),
2002 m_expr_prefix_contents_sp (),
Jim Ingham2837b762011-05-04 03:43:182003 m_prefer_dynamic_value (2),
Greg Clayton7e14f912011-04-23 02:04:552004 m_skip_prologue (true, true),
Enrico Granata22c55d12011-08-12 02:00:062005 m_source_map (NULL, NULL),
Enrico Granata9128ee2f2011-09-06 19:20:512006 m_max_children_display(256),
2007 m_max_strlen_length(1024)
Caroline Ticedaccaa92010-09-20 20:44:432008{
2009 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2010 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2011 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2012 // This is true for CreateInstanceName() too.
2013
2014 if (GetInstanceName () == InstanceSettings::InvalidName())
2015 {
2016 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2017 m_owner.RegisterInstanceSettings (this);
2018 }
2019
2020 if (live_instance)
2021 {
2022 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2023 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:432024 }
2025}
2026
2027TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton7e14f912011-04-23 02:04:552028 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
2029 m_expr_prefix_file (rhs.m_expr_prefix_file),
2030 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
2031 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2032 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata22c55d12011-08-12 02:00:062033 m_source_map (rhs.m_source_map),
Enrico Granata9128ee2f2011-09-06 19:20:512034 m_max_children_display(rhs.m_max_children_display),
2035 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Ticedaccaa92010-09-20 20:44:432036{
2037 if (m_instance_name != InstanceSettings::GetDefaultName())
2038 {
2039 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2040 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:432041 }
2042}
2043
2044TargetInstanceSettings::~TargetInstanceSettings ()
2045{
2046}
2047
2048TargetInstanceSettings&
2049TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2050{
2051 if (this != &rhs)
2052 {
2053 }
2054
2055 return *this;
2056}
2057
Caroline Ticedaccaa92010-09-20 20:44:432058void
2059TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2060 const char *index_value,
2061 const char *value,
2062 const ConstString &instance_name,
2063 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:542064 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:432065 Error &err,
2066 bool pending)
2067{
Greg Claytonbfe5f3b2011-02-18 01:44:252068 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:032069 {
Greg Clayton7e14f912011-04-23 02:04:552070 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2071 if (err.Success())
Sean Callanan322f5292010-10-29 00:29:032072 {
Greg Clayton7e14f912011-04-23 02:04:552073 switch (op)
Sean Callanan322f5292010-10-29 00:29:032074 {
Greg Clayton7e14f912011-04-23 02:04:552075 default:
2076 break;
2077 case eVarSetOperationAssign:
2078 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:032079 {
Greg Clayton7e14f912011-04-23 02:04:552080 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2081 {
2082 err.SetErrorToGenericError ();
2083 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
2084 return;
2085 }
2086
2087 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
2088
2089 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
2090 {
2091 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
2092 m_expr_prefix_contents_sp.reset();
2093 }
Sean Callanan322f5292010-10-29 00:29:032094 }
Greg Clayton7e14f912011-04-23 02:04:552095 break;
2096 case eVarSetOperationClear:
2097 m_expr_prefix_contents_sp.reset();
Sean Callanan322f5292010-10-29 00:29:032098 }
Sean Callanan322f5292010-10-29 00:29:032099 }
2100 }
Jim Ingham78a685a2011-04-16 00:01:132101 else if (var_name == GetSettingNameForPreferDynamicValue())
2102 {
Jim Ingham2837b762011-05-04 03:43:182103 int new_value;
2104 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2105 if (err.Success())
2106 m_prefer_dynamic_value = new_value;
Greg Clayton385aa282011-04-22 03:55:062107 }
2108 else if (var_name == GetSettingNameForSkipPrologue())
2109 {
Greg Clayton7e14f912011-04-23 02:04:552110 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2111 }
Enrico Granata22c55d12011-08-12 02:00:062112 else if (var_name == GetSettingNameForMaxChildren())
2113 {
2114 bool ok;
2115 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2116 if (ok)
2117 m_max_children_display = new_value;
2118 }
Enrico Granata9128ee2f2011-09-06 19:20:512119 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2120 {
2121 bool ok;
2122 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2123 if (ok)
2124 m_max_strlen_length = new_value;
2125 }
Greg Clayton7e14f912011-04-23 02:04:552126 else if (var_name == GetSettingNameForSourcePathMap ())
2127 {
2128 switch (op)
2129 {
2130 case eVarSetOperationReplace:
2131 case eVarSetOperationInsertBefore:
2132 case eVarSetOperationInsertAfter:
2133 case eVarSetOperationRemove:
2134 default:
2135 break;
2136 case eVarSetOperationAssign:
2137 m_source_map.Clear(true);
2138 // Fall through to append....
2139 case eVarSetOperationAppend:
2140 {
2141 Args args(value);
2142 const uint32_t argc = args.GetArgumentCount();
2143 if (argc & 1 || argc == 0)
2144 {
2145 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2146 }
2147 else
2148 {
2149 char resolved_new_path[PATH_MAX];
2150 FileSpec file_spec;
2151 const char *old_path;
2152 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2153 {
2154 const char *new_path = args.GetArgumentAtIndex(idx+1);
2155 assert (new_path); // We have an even number of paths, this shouldn't happen!
2156
2157 file_spec.SetFile(new_path, true);
2158 if (file_spec.Exists())
2159 {
2160 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2161 {
2162 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2163 return;
2164 }
2165 }
2166 else
2167 {
2168 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2169 return;
2170 }
2171 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2172 }
2173 }
2174 }
2175 break;
2176
2177 case eVarSetOperationClear:
2178 m_source_map.Clear(true);
2179 break;
2180 }
Jim Ingham78a685a2011-04-16 00:01:132181 }
Caroline Ticedaccaa92010-09-20 20:44:432182}
2183
2184void
Greg Claytonbfe5f3b2011-02-18 01:44:252185TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:432186{
Sean Callanan322f5292010-10-29 00:29:032187 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2188
2189 if (!new_settings_ptr)
2190 return;
2191
Greg Clayton7e14f912011-04-23 02:04:552192 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
2193 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
2194 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
2195 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata22c55d12011-08-12 02:00:062196 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata9128ee2f2011-09-06 19:20:512197 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Ticedaccaa92010-09-20 20:44:432198}
2199
Caroline Tice12cecd72010-09-20 21:37:422200bool
Caroline Ticedaccaa92010-09-20 20:44:432201TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2202 const ConstString &var_name,
2203 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:422204 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:432205{
Greg Claytonbfe5f3b2011-02-18 01:44:252206 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:032207 {
Greg Clayton7e14f912011-04-23 02:04:552208 char path[PATH_MAX];
2209 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2210 if (path_len > 0)
2211 value.AppendString (path, path_len);
Sean Callanan322f5292010-10-29 00:29:032212 }
Jim Ingham78a685a2011-04-16 00:01:132213 else if (var_name == GetSettingNameForPreferDynamicValue())
2214 {
Jim Ingham2837b762011-05-04 03:43:182215 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Ingham78a685a2011-04-16 00:01:132216 }
Greg Clayton385aa282011-04-22 03:55:062217 else if (var_name == GetSettingNameForSkipPrologue())
2218 {
2219 if (m_skip_prologue)
2220 value.AppendString ("true");
2221 else
2222 value.AppendString ("false");
2223 }
Greg Clayton7e14f912011-04-23 02:04:552224 else if (var_name == GetSettingNameForSourcePathMap ())
2225 {
2226 }
Enrico Granata22c55d12011-08-12 02:00:062227 else if (var_name == GetSettingNameForMaxChildren())
2228 {
2229 StreamString count_str;
2230 count_str.Printf ("%d", m_max_children_display);
2231 value.AppendString (count_str.GetData());
2232 }
Enrico Granata9128ee2f2011-09-06 19:20:512233 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2234 {
2235 StreamString count_str;
2236 count_str.Printf ("%d", m_max_strlen_length);
2237 value.AppendString (count_str.GetData());
2238 }
Sean Callanan322f5292010-10-29 00:29:032239 else
2240 {
2241 if (err)
2242 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2243 return false;
2244 }
2245
2246 return true;
Caroline Ticedaccaa92010-09-20 20:44:432247}
2248
2249const ConstString
2250TargetInstanceSettings::CreateInstanceName ()
2251{
Caroline Ticedaccaa92010-09-20 20:44:432252 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:102253 static int instance_count = 1;
2254
Caroline Ticedaccaa92010-09-20 20:44:432255 sstr.Printf ("target_%d", instance_count);
2256 ++instance_count;
2257
2258 const ConstString ret_val (sstr.GetData());
2259 return ret_val;
2260}
2261
2262//--------------------------------------------------
2263// Target::SettingsController Variable Tables
2264//--------------------------------------------------
Jim Ingham2837b762011-05-04 03:43:182265OptionEnumValueElement
2266TargetInstanceSettings::g_dynamic_value_types[] =
2267{
Greg Clayton5d2fbfe2011-05-30 00:39:482268{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2269{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2270{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham2837b762011-05-04 03:43:182271{ 0, NULL, NULL }
2272};
Caroline Ticedaccaa92010-09-20 20:44:432273
2274SettingEntry
2275Target::SettingsController::global_settings_table[] =
2276{
Greg Claytonbfe5f3b2011-02-18 01:44:252277 // var-name var-type default enum init'd hidden help-text
2278 // ================= ================== =========== ==== ====== ====== =========================================================================
2279 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2280 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2281};
2282
Caroline Ticedaccaa92010-09-20 20:44:432283SettingEntry
2284Target::SettingsController::instance_settings_table[] =
2285{
Enrico Granata9128ee2f2011-09-06 19:20:512286 // var-name var-type default enum init'd hidden help-text
2287 // ================= ================== =============== ======================= ====== ====== =========================================================================
2288 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2289 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2290 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2291 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2292 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2293 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2294 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:432295};