blob: ebaab10b0ed6cce7cabe8fbcfbf4d8ead0851908 [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 Chen86364b42011-09-20 23:28:55128 m_watchpoint_location_list.RemoveAll();
Chris Lattner30fdc8d2010-06-08 16:52:24129 m_process_sp.reset();
130 }
131}
132
133const lldb::ProcessSP &
134Target::CreateProcess (Listener &listener, const char *plugin_name)
135{
136 DeleteCurrentProcess ();
137 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
138 return m_process_sp;
139}
140
141const lldb::ProcessSP &
142Target::GetProcessSP () const
143{
144 return m_process_sp;
145}
146
147lldb::TargetSP
148Target::GetSP()
149{
Greg Clayton4d122c42011-09-17 08:33:22150 // This object contains an instrusive ref count base class so we can
151 // easily make a shared pointer to this object
152 return TargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24153}
154
Greg Clayton3418c852011-08-10 02:10:13155void
156Target::Destroy()
157{
158 Mutex::Locker locker (m_mutex);
159 DeleteCurrentProcess ();
160 m_platform_sp.reset();
161 m_arch.Clear();
162 m_images.Clear();
163 m_section_load_list.Clear();
164 const bool notify = false;
165 m_breakpoint_list.RemoveAll(notify);
166 m_internal_breakpoint_list.RemoveAll(notify);
167 m_last_created_breakpoint.reset();
168 m_search_filter_sp.reset();
169 m_image_search_paths.Clear(notify);
170 m_scratch_ast_context_ap.reset();
171 m_persistent_variables.Clear();
172 m_stop_hooks.clear();
173 m_stop_hook_next_id = 0;
174 m_suppress_stop_hooks = false;
175}
176
177
Chris Lattner30fdc8d2010-06-08 16:52:24178BreakpointList &
179Target::GetBreakpointList(bool internal)
180{
181 if (internal)
182 return m_internal_breakpoint_list;
183 else
184 return m_breakpoint_list;
185}
186
187const BreakpointList &
188Target::GetBreakpointList(bool internal) const
189{
190 if (internal)
191 return m_internal_breakpoint_list;
192 else
193 return m_breakpoint_list;
194}
195
196BreakpointSP
197Target::GetBreakpointByID (break_id_t break_id)
198{
199 BreakpointSP bp_sp;
200
201 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
202 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
203 else
204 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
205
206 return bp_sp;
207}
208
209BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11210Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
211 const FileSpecList *source_file_spec_list,
Jim Ingham969795f2011-09-21 01:17:13212 RegularExpression &source_regex,
213 bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24214{
Jim Ingham87df91b2011-09-23 00:54:11215 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
216 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham969795f2011-09-21 01:17:13217 return CreateBreakpoint (filter_sp, resolver_sp, internal);
218}
219
220
221BreakpointSP
222Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
223{
224 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner30fdc8d2010-06-08 16:52:24225 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
226 return CreateBreakpoint (filter_sp, resolver_sp, internal);
227}
228
229
230BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41231Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24232{
Chris Lattner30fdc8d2010-06-08 16:52:24233 Address so_addr;
234 // Attempt to resolve our load address if possible, though it is ok if
235 // it doesn't resolve to section/offset.
236
Greg Clayton1b72fcb2010-08-24 00:45:41237 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40238 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41239 if (!so_addr.IsValid())
240 {
241 // The address didn't resolve, so just set this as an absolute address
242 so_addr.SetOffset (addr);
243 }
244 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24245 return bp_sp;
246}
247
248BreakpointSP
249Target::CreateBreakpoint (Address &addr, bool internal)
250{
251 TargetSP target_sp = this->GetSP();
252 SearchFilterSP filter_sp(new SearchFilter (target_sp));
253 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
254 return CreateBreakpoint (filter_sp, resolver_sp, internal);
255}
256
257BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11258Target::CreateBreakpoint (const FileSpecList *containingModules,
259 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17260 const char *func_name,
261 uint32_t func_name_type_mask,
262 bool internal,
263 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24264{
Greg Clayton0c5cd902010-06-28 21:30:43265 BreakpointSP bp_sp;
266 if (func_name)
267 {
Jim Ingham87df91b2011-09-23 00:54:11268 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17269
270 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
271 func_name,
272 func_name_type_mask,
273 Breakpoint::Exact,
274 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton0c5cd902010-06-28 21:30:43275 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
276 }
277 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24278}
279
280
281SearchFilterSP
282Target::GetSearchFilterForModule (const FileSpec *containingModule)
283{
284 SearchFilterSP filter_sp;
285 lldb::TargetSP target_sp = this->GetSP();
286 if (containingModule != NULL)
287 {
288 // TODO: We should look into sharing module based search filters
289 // across many breakpoints like we do for the simple target based one
290 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
291 }
292 else
293 {
294 if (m_search_filter_sp.get() == NULL)
295 m_search_filter_sp.reset (new SearchFilter (target_sp));
296 filter_sp = m_search_filter_sp;
297 }
298 return filter_sp;
299}
300
Jim Ingham969795f2011-09-21 01:17:13301SearchFilterSP
302Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
303{
304 SearchFilterSP filter_sp;
305 lldb::TargetSP target_sp = this->GetSP();
306 if (containingModules && containingModules->GetSize() != 0)
307 {
308 // TODO: We should look into sharing module based search filters
309 // across many breakpoints like we do for the simple target based one
310 filter_sp.reset (new SearchFilterByModuleList (target_sp, *containingModules));
311 }
312 else
313 {
314 if (m_search_filter_sp.get() == NULL)
315 m_search_filter_sp.reset (new SearchFilter (target_sp));
316 filter_sp = m_search_filter_sp;
317 }
318 return filter_sp;
319}
320
Jim Ingham87df91b2011-09-23 00:54:11321SearchFilterSP
322Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles)
323{
324 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
325 return GetSearchFilterForModuleList(containingModules);
326
327 SearchFilterSP filter_sp;
328 lldb::TargetSP target_sp = this->GetSP();
329 if (containingModules == NULL)
330 {
331 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
332 // but that will take a little reworking.
333
334 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, FileSpecList(), *containingSourceFiles));
335 }
336 else
337 {
338 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, *containingModules, *containingSourceFiles));
339 }
340 return filter_sp;
341}
342
Chris Lattner30fdc8d2010-06-08 16:52:24343BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11344Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
345 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17346 RegularExpression &func_regex,
347 bool internal,
348 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24349{
Jim Ingham87df91b2011-09-23 00:54:11350 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Claytond16e1e52011-07-12 17:06:17351 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
352 func_regex,
353 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24354
355 return CreateBreakpoint (filter_sp, resolver_sp, internal);
356}
357
358BreakpointSP
359Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
360{
361 BreakpointSP bp_sp;
362 if (filter_sp && resolver_sp)
363 {
364 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
365 resolver_sp->SetBreakpoint (bp_sp.get());
366
367 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17368 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24369 else
Greg Clayton9fed0d82010-07-23 23:33:17370 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24371
Greg Clayton2d4edfb2010-11-06 01:53:30372 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24373 if (log)
374 {
375 StreamString s;
376 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
377 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
378 }
379
Chris Lattner30fdc8d2010-06-08 16:52:24380 bp_sp->ResolveBreakpoint();
381 }
Jim Ingham36f3b362010-10-14 23:45:03382
383 if (!internal && bp_sp)
384 {
385 m_last_created_breakpoint = bp_sp;
386 }
387
Chris Lattner30fdc8d2010-06-08 16:52:24388 return bp_sp;
389}
390
Johnny Chen86364b42011-09-20 23:28:55391bool
392Target::ProcessIsValid()
393{
394 return (m_process_sp && m_process_sp->IsAlive());
395}
396
Johnny Chenab9ee762011-09-14 00:26:03397// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
398// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen887062a2011-09-12 23:38:44399WatchpointLocationSP
400Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
401{
Johnny Chen0c406372011-09-14 20:23:45402 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
403 if (log)
404 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
405 __FUNCTION__, addr, size, type);
406
Johnny Chen887062a2011-09-12 23:38:44407 WatchpointLocationSP wp_loc_sp;
Johnny Chen86364b42011-09-20 23:28:55408 if (!ProcessIsValid())
Johnny Chen2fd89a02011-09-13 18:30:59409 return wp_loc_sp;
Johnny Chen45e541f2011-09-14 22:20:15410 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen887062a2011-09-12 23:38:44411 return wp_loc_sp;
Johnny Chen7313a642011-09-13 01:15:36412
Johnny Chenab9ee762011-09-14 00:26:03413 // Currently we only support one watchpoint location per address, with total
414 // number of watchpoint locations limited by the hardware which the inferior
415 // is running on.
Johnny Chen3c532582011-09-13 23:29:31416 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
417 if (matched_sp)
418 {
Johnny Chen0c406372011-09-14 20:23:45419 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31420 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45421 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
422 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen45e541f2011-09-14 22:20:15423 // Return the existing watchpoint location if both size and type match.
424 if (size == old_size && type == old_type) {
425 wp_loc_sp = matched_sp;
426 wp_loc_sp->SetEnabled(false);
427 } else {
428 // Nil the matched watchpoint location; we will be creating a new one.
429 m_process_sp->DisableWatchpoint(matched_sp.get());
430 m_watchpoint_location_list.Remove(matched_sp->GetID());
431 }
Johnny Chen3c532582011-09-13 23:29:31432 }
433
Johnny Chen45e541f2011-09-14 22:20:15434 if (!wp_loc_sp) {
435 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
436 if (!new_loc) {
437 printf("WatchpointLocation ctor failed, out of memory?\n");
438 return wp_loc_sp;
439 }
440 new_loc->SetWatchpointType(type);
441 wp_loc_sp.reset(new_loc);
442 m_watchpoint_location_list.Add(wp_loc_sp);
443 }
Johnny Chen0c406372011-09-14 20:23:45444
Johnny Chen0c406372011-09-14 20:23:45445 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen0c406372011-09-14 20:23:45446 if (log)
447 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
448 __FUNCTION__,
449 rc.Success() ? "succeeded" : "failed",
450 wp_loc_sp->GetID());
451
Johnny Chen45e541f2011-09-14 22:20:15452 if (rc.Fail()) wp_loc_sp.reset();
Johnny Chen7313a642011-09-13 01:15:36453 return wp_loc_sp;
Johnny Chen887062a2011-09-12 23:38:44454}
455
Chris Lattner30fdc8d2010-06-08 16:52:24456void
457Target::RemoveAllBreakpoints (bool internal_also)
458{
Greg Clayton2d4edfb2010-11-06 01:53:30459 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24460 if (log)
461 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
462
Greg Clayton9fed0d82010-07-23 23:33:17463 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24464 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17465 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03466
467 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24468}
469
470void
471Target::DisableAllBreakpoints (bool internal_also)
472{
Greg Clayton2d4edfb2010-11-06 01:53:30473 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24474 if (log)
475 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
476
477 m_breakpoint_list.SetEnabledAll (false);
478 if (internal_also)
479 m_internal_breakpoint_list.SetEnabledAll (false);
480}
481
482void
483Target::EnableAllBreakpoints (bool internal_also)
484{
Greg Clayton2d4edfb2010-11-06 01:53:30485 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24486 if (log)
487 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
488
489 m_breakpoint_list.SetEnabledAll (true);
490 if (internal_also)
491 m_internal_breakpoint_list.SetEnabledAll (true);
492}
493
494bool
495Target::RemoveBreakpointByID (break_id_t break_id)
496{
Greg Clayton2d4edfb2010-11-06 01:53:30497 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24498 if (log)
499 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
500
501 if (DisableBreakpointByID (break_id))
502 {
503 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17504 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24505 else
Jim Ingham36f3b362010-10-14 23:45:03506 {
Greg Claytonaa1c5872011-01-24 23:35:47507 if (m_last_created_breakpoint)
508 {
509 if (m_last_created_breakpoint->GetID() == break_id)
510 m_last_created_breakpoint.reset();
511 }
Greg Clayton9fed0d82010-07-23 23:33:17512 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03513 }
Chris Lattner30fdc8d2010-06-08 16:52:24514 return true;
515 }
516 return false;
517}
518
519bool
520Target::DisableBreakpointByID (break_id_t break_id)
521{
Greg Clayton2d4edfb2010-11-06 01:53:30522 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24523 if (log)
524 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
525
526 BreakpointSP bp_sp;
527
528 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
529 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
530 else
531 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
532 if (bp_sp)
533 {
534 bp_sp->SetEnabled (false);
535 return true;
536 }
537 return false;
538}
539
540bool
541Target::EnableBreakpointByID (break_id_t break_id)
542{
Greg Clayton2d4edfb2010-11-06 01:53:30543 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24544 if (log)
545 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
546 __FUNCTION__,
547 break_id,
548 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
549
550 BreakpointSP bp_sp;
551
552 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
553 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
554 else
555 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
556
557 if (bp_sp)
558 {
559 bp_sp->SetEnabled (true);
560 return true;
561 }
562 return false;
563}
564
Johnny Chen86364b42011-09-20 23:28:55565// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
566bool
567Target::RemoveAllWatchpointLocations ()
568{
569 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
570 if (log)
571 log->Printf ("Target::%s\n", __FUNCTION__);
572
573 if (!ProcessIsValid())
574 return false;
575
576 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
577 for (size_t i = 0; i < num_watchpoints; ++i)
578 {
579 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
580 if (!wp_loc_sp)
581 return false;
582
583 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
584 if (rc.Fail())
585 return false;
586 }
587 m_watchpoint_location_list.RemoveAll ();
588 return true; // Success!
589}
590
591// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
592bool
593Target::DisableAllWatchpointLocations ()
594{
595 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
596 if (log)
597 log->Printf ("Target::%s\n", __FUNCTION__);
598
599 if (!ProcessIsValid())
600 return false;
601
602 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
603 for (size_t i = 0; i < num_watchpoints; ++i)
604 {
605 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
606 if (!wp_loc_sp)
607 return false;
608
609 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
610 if (rc.Fail())
611 return false;
612 }
Johnny Chen86364b42011-09-20 23:28:55613 return true; // Success!
614}
615
616// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
617bool
618Target::EnableAllWatchpointLocations ()
619{
620 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
621 if (log)
622 log->Printf ("Target::%s\n", __FUNCTION__);
623
624 if (!ProcessIsValid())
625 return false;
626
627 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
628 for (size_t i = 0; i < num_watchpoints; ++i)
629 {
630 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
631 if (!wp_loc_sp)
632 return false;
633
634 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
635 if (rc.Fail())
636 return false;
637 }
Johnny Chen86364b42011-09-20 23:28:55638 return true; // Success!
639}
640
641// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
642bool
643Target::DisableWatchpointLocationByID (lldb::watch_id_t watch_id)
644{
645 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
646 if (log)
647 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
648
649 if (!ProcessIsValid())
650 return false;
651
652 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
653 if (wp_loc_sp)
654 {
655 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58656 if (rc.Success())
657 return true;
Johnny Chen86364b42011-09-20 23:28:55658
Johnny Chenf04ee932011-09-22 18:04:58659 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55660 }
661 return false;
662}
663
664// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
665bool
666Target::EnableWatchpointLocationByID (lldb::watch_id_t watch_id)
667{
668 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
669 if (log)
670 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
671
672 if (!ProcessIsValid())
673 return false;
674
675 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
676 if (wp_loc_sp)
677 {
678 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58679 if (rc.Success())
680 return true;
Johnny Chen86364b42011-09-20 23:28:55681
Johnny Chenf04ee932011-09-22 18:04:58682 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55683 }
684 return false;
685}
686
687// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
688bool
689Target::RemoveWatchpointLocationByID (lldb::watch_id_t watch_id)
690{
691 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
692 if (log)
693 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
694
695 if (DisableWatchpointLocationByID (watch_id))
696 {
697 m_watchpoint_location_list.Remove(watch_id);
698 return true;
699 }
700 return false;
701}
702
Chris Lattner30fdc8d2010-06-08 16:52:24703ModuleSP
704Target::GetExecutableModule ()
705{
Greg Claytonaa149cb2011-08-11 02:48:45706 return m_images.GetModuleAtIndex(0);
707}
708
709Module*
710Target::GetExecutableModulePointer ()
711{
712 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24713}
714
715void
716Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
717{
718 m_images.Clear();
719 m_scratch_ast_context_ap.reset();
720
721 if (executable_sp.get())
722 {
723 Timer scoped_timer (__PRETTY_FUNCTION__,
724 "Target::SetExecutableModule (executable = '%s/%s')",
725 executable_sp->GetFileSpec().GetDirectory().AsCString(),
726 executable_sp->GetFileSpec().GetFilename().AsCString());
727
728 m_images.Append(executable_sp); // The first image is our exectuable file
729
Jim Ingham5aee1622010-08-09 23:31:02730 // 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:51731 if (!m_arch.IsValid())
732 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02733
Chris Lattner30fdc8d2010-06-08 16:52:24734 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15735 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamb7f6b2f2011-09-08 22:13:49736 // Let's find the file & line for main and set the default source file from there.
737 if (!m_source_manager.DefaultFileAndLineSet())
738 {
739 SymbolContextList sc_list;
740 uint32_t num_matches;
741 ConstString main_name("main");
742 bool symbols_okay = false; // Force it to be a debug symbol.
743 bool append = false;
744 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
745 for (uint32_t idx = 0; idx < num_matches; idx++)
746 {
747 SymbolContext sc;
748 sc_list.GetContextAtIndex(idx, sc);
749 if (sc.line_entry.file)
750 {
751 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
752 break;
753 }
754 }
755 }
Chris Lattner30fdc8d2010-06-08 16:52:24756
757 if (executable_objfile)
758 {
759 executable_objfile->GetDependentModules(dependent_files);
760 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
761 {
Greg Claytonded470d2011-03-19 01:12:21762 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
763 FileSpec platform_dependent_file_spec;
764 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55765 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21766 else
767 platform_dependent_file_spec = dependent_file_spec;
768
769 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton32e0a752011-03-30 18:16:51770 m_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24771 if (image_module_sp.get())
772 {
Chris Lattner30fdc8d2010-06-08 16:52:24773 ObjectFile *objfile = image_module_sp->GetObjectFile();
774 if (objfile)
775 objfile->GetDependentModules(dependent_files);
776 }
777 }
778 }
779
Chris Lattner30fdc8d2010-06-08 16:52:24780 }
Caroline Tice1559a462010-09-27 00:30:10781
782 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24783}
784
785
Jim Ingham5aee1622010-08-09 23:31:02786bool
787Target::SetArchitecture (const ArchSpec &arch_spec)
788{
Greg Clayton32e0a752011-03-30 18:16:51789 if (m_arch == arch_spec)
Jim Ingham5aee1622010-08-09 23:31:02790 {
791 // If we're setting the architecture to our current architecture, we
792 // don't need to do anything.
793 return true;
794 }
Greg Clayton32e0a752011-03-30 18:16:51795 else if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02796 {
797 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton32e0a752011-03-30 18:16:51798 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02799 return true;
800 }
801 else
802 {
803 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51804 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02805 ModuleSP executable_sp = GetExecutableModule ();
806 m_images.Clear();
807 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02808 // Need to do something about unsetting breakpoints.
809
810 if (executable_sp)
811 {
812 FileSpec exec_file_spec = executable_sp->GetFileSpec();
813 Error error = ModuleList::GetSharedModule(exec_file_spec,
814 arch_spec,
815 NULL,
816 NULL,
817 0,
818 executable_sp,
819 NULL,
820 NULL);
821
822 if (!error.Fail() && executable_sp)
823 {
824 SetExecutableModule (executable_sp, true);
825 return true;
826 }
827 else
828 {
829 return false;
830 }
831 }
832 else
833 {
834 return false;
835 }
836 }
837}
Chris Lattner30fdc8d2010-06-08 16:52:24838
Chris Lattner30fdc8d2010-06-08 16:52:24839void
840Target::ModuleAdded (ModuleSP &module_sp)
841{
842 // A module is being added to this target for the first time
843 ModuleList module_list;
844 module_list.Append(module_sp);
845 ModulesDidLoad (module_list);
846}
847
848void
849Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
850{
Jim Inghame716ae02011-08-03 01:00:06851 // A module is replacing an already added module
Chris Lattner30fdc8d2010-06-08 16:52:24852 ModuleList module_list;
853 module_list.Append (old_module_sp);
854 ModulesDidUnload (module_list);
855 module_list.Clear ();
856 module_list.Append (new_module_sp);
857 ModulesDidLoad (module_list);
858}
859
860void
861Target::ModulesDidLoad (ModuleList &module_list)
862{
863 m_breakpoint_list.UpdateBreakpoints (module_list, true);
864 // TODO: make event data that packages up the module_list
865 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
866}
867
868void
869Target::ModulesDidUnload (ModuleList &module_list)
870{
871 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26872
873 // Remove the images from the target image list
874 m_images.Remove(module_list);
875
Chris Lattner30fdc8d2010-06-08 16:52:24876 // TODO: make event data that packages up the module_list
877 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
878}
879
880size_t
Greg Claytondb598232011-01-07 01:57:07881Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
882{
883 const Section *section = addr.GetSection();
884 if (section && section->GetModule())
885 {
886 ObjectFile *objfile = section->GetModule()->GetObjectFile();
887 if (objfile)
888 {
889 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
890 addr.GetOffset(),
891 dst,
892 dst_len);
893 if (bytes_read > 0)
894 return bytes_read;
895 else
896 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
897 }
898 else
899 {
900 error.SetErrorString("address isn't from a object file");
901 }
902 }
903 else
904 {
905 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
906 }
907 return 0;
908}
909
910size_t
Enrico Granata9128ee2f2011-09-06 19:20:51911Target::ReadMemory (const Address& addr,
912 bool prefer_file_cache,
913 void *dst,
914 size_t dst_len,
915 Error &error,
916 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24917{
Chris Lattner30fdc8d2010-06-08 16:52:24918 error.Clear();
Greg Claytondb598232011-01-07 01:57:07919
Enrico Granata9128ee2f2011-09-06 19:20:51920 // if we end up reading this from process memory, we will fill this
921 // with the actual load address
922 if (load_addr_ptr)
923 *load_addr_ptr = LLDB_INVALID_ADDRESS;
924
Greg Claytondb598232011-01-07 01:57:07925 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02926
927 addr_t load_addr = LLDB_INVALID_ADDRESS;
928 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58929 Address resolved_addr;
930 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03931 {
Greg Claytond16e1e52011-07-12 17:06:17932 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02933 {
Greg Claytond16e1e52011-07-12 17:06:17934 // No sections are loaded, so we must assume we are not running
935 // yet and anything we are given is a file address.
936 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
937 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02938 }
Greg Claytondda4f7b2010-06-30 23:03:03939 else
Greg Claytonc749eb82011-07-11 05:12:02940 {
Greg Claytond16e1e52011-07-12 17:06:17941 // We have at least one section loaded. This can be becuase
942 // we have manually loaded some sections with "target modules load ..."
943 // or because we have have a live process that has sections loaded
944 // through the dynamic loader
945 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
946 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02947 }
Greg Claytondda4f7b2010-06-30 23:03:03948 }
Greg Clayton357132e2011-03-26 19:14:58949 if (!resolved_addr.IsValid())
950 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03951
Greg Claytonc749eb82011-07-11 05:12:02952
Greg Claytondb598232011-01-07 01:57:07953 if (prefer_file_cache)
954 {
955 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
956 if (bytes_read > 0)
957 return bytes_read;
958 }
Greg Claytondda4f7b2010-06-30 23:03:03959
Johnny Chen86364b42011-09-20 23:28:55960 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03961 {
Greg Claytonc749eb82011-07-11 05:12:02962 if (load_addr == LLDB_INVALID_ADDRESS)
963 load_addr = resolved_addr.GetLoadAddress (this);
964
Greg Claytondda4f7b2010-06-30 23:03:03965 if (load_addr == LLDB_INVALID_ADDRESS)
966 {
967 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
968 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
969 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08970 resolved_addr.GetFileAddress(),
971 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03972 else
973 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
974 }
975 else
976 {
Greg Claytondb598232011-01-07 01:57:07977 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24978 if (bytes_read != dst_len)
979 {
980 if (error.Success())
981 {
982 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03983 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24984 else
Greg Claytondda4f7b2010-06-30 23:03:03985 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:24986 }
987 }
Greg Claytondda4f7b2010-06-30 23:03:03988 if (bytes_read)
Enrico Granata9128ee2f2011-09-06 19:20:51989 {
990 if (load_addr_ptr)
991 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03992 return bytes_read;
Enrico Granata9128ee2f2011-09-06 19:20:51993 }
Greg Claytondda4f7b2010-06-30 23:03:03994 // If the address is not section offset we have an address that
995 // doesn't resolve to any address in any currently loaded shared
996 // libaries and we failed to read memory so there isn't anything
997 // more we can do. If it is section offset, we might be able to
998 // read cached memory from the object file.
999 if (!resolved_addr.IsSectionOffset())
1000 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:241001 }
Chris Lattner30fdc8d2010-06-08 16:52:241002 }
Greg Claytondda4f7b2010-06-30 23:03:031003
Greg Claytonc749eb82011-07-11 05:12:021004 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:031005 {
Greg Claytondb598232011-01-07 01:57:071006 // If we didn't already try and read from the object file cache, then
1007 // try it after failing to read from the process.
1008 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:031009 }
1010 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:241011}
1012
Greg Claytond16e1e52011-07-12 17:06:171013size_t
1014Target::ReadScalarIntegerFromMemory (const Address& addr,
1015 bool prefer_file_cache,
1016 uint32_t byte_size,
1017 bool is_signed,
1018 Scalar &scalar,
1019 Error &error)
1020{
1021 uint64_t uval;
1022
1023 if (byte_size <= sizeof(uval))
1024 {
1025 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1026 if (bytes_read == byte_size)
1027 {
1028 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1029 uint32_t offset = 0;
1030 if (byte_size <= 4)
1031 scalar = data.GetMaxU32 (&offset, byte_size);
1032 else
1033 scalar = data.GetMaxU64 (&offset, byte_size);
1034
1035 if (is_signed)
1036 scalar.SignExtend(byte_size * 8);
1037 return bytes_read;
1038 }
1039 }
1040 else
1041 {
1042 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1043 }
1044 return 0;
1045}
1046
1047uint64_t
1048Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1049 bool prefer_file_cache,
1050 size_t integer_byte_size,
1051 uint64_t fail_value,
1052 Error &error)
1053{
1054 Scalar scalar;
1055 if (ReadScalarIntegerFromMemory (addr,
1056 prefer_file_cache,
1057 integer_byte_size,
1058 false,
1059 scalar,
1060 error))
1061 return scalar.ULongLong(fail_value);
1062 return fail_value;
1063}
1064
1065bool
1066Target::ReadPointerFromMemory (const Address& addr,
1067 bool prefer_file_cache,
1068 Error &error,
1069 Address &pointer_addr)
1070{
1071 Scalar scalar;
1072 if (ReadScalarIntegerFromMemory (addr,
1073 prefer_file_cache,
1074 m_arch.GetAddressByteSize(),
1075 false,
1076 scalar,
1077 error))
1078 {
1079 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1080 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1081 {
1082 if (m_section_load_list.IsEmpty())
1083 {
1084 // No sections are loaded, so we must assume we are not running
1085 // yet and anything we are given is a file address.
1086 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1087 }
1088 else
1089 {
1090 // We have at least one section loaded. This can be becuase
1091 // we have manually loaded some sections with "target modules load ..."
1092 // or because we have have a live process that has sections loaded
1093 // through the dynamic loader
1094 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1095 }
1096 // We weren't able to resolve the pointer value, so just return
1097 // an address with no section
1098 if (!pointer_addr.IsValid())
1099 pointer_addr.SetOffset (pointer_vm_addr);
1100 return true;
1101
1102 }
1103 }
1104 return false;
1105}
Chris Lattner30fdc8d2010-06-08 16:52:241106
1107ModuleSP
1108Target::GetSharedModule
1109(
1110 const FileSpec& file_spec,
1111 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:101112 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:241113 const ConstString *object_name,
1114 off_t object_offset,
1115 Error *error_ptr
1116)
1117{
1118 // Don't pass in the UUID so we can tell if we have a stale value in our list
1119 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1120 bool did_create_module = false;
1121 ModuleSP module_sp;
1122
Chris Lattner30fdc8d2010-06-08 16:52:241123 Error error;
1124
Greg Clayton32e0a752011-03-30 18:16:511125 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner30fdc8d2010-06-08 16:52:241126 if (m_image_search_paths.GetSize())
1127 {
1128 FileSpec transformed_spec;
1129 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1130 {
1131 transformed_spec.GetFilename() = file_spec.GetFilename();
1132 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1133 }
1134 }
1135
Greg Clayton32e0a752011-03-30 18:16:511136 // The platform is responsible for finding and caching an appropriate
1137 // module in the shared module cache.
1138 if (m_platform_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241139 {
Greg Clayton32e0a752011-03-30 18:16:511140 FileSpec platform_file_spec;
1141 error = m_platform_sp->GetSharedModule (file_spec,
1142 arch,
1143 uuid_ptr,
1144 object_name,
1145 object_offset,
1146 module_sp,
1147 &old_module_sp,
1148 &did_create_module);
1149 }
1150 else
1151 {
1152 error.SetErrorString("no platform is currently set");
Chris Lattner30fdc8d2010-06-08 16:52:241153 }
1154
Greg Clayton32e0a752011-03-30 18:16:511155 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner30fdc8d2010-06-08 16:52:241156 if (module_sp)
1157 {
1158 m_images.Append (module_sp);
1159 if (did_create_module)
1160 {
1161 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1162 ModuleUpdated(old_module_sp, module_sp);
1163 else
1164 ModuleAdded(module_sp);
1165 }
1166 }
1167 if (error_ptr)
1168 *error_ptr = error;
1169 return module_sp;
1170}
1171
1172
1173Target *
1174Target::CalculateTarget ()
1175{
1176 return this;
1177}
1178
1179Process *
1180Target::CalculateProcess ()
1181{
1182 return NULL;
1183}
1184
1185Thread *
1186Target::CalculateThread ()
1187{
1188 return NULL;
1189}
1190
1191StackFrame *
1192Target::CalculateStackFrame ()
1193{
1194 return NULL;
1195}
1196
1197void
Greg Clayton0603aa92010-10-04 01:05:561198Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:241199{
Greg Claytonc14ee322011-09-22 04:58:261200 exe_ctx.Clear();
1201 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:241202}
1203
1204PathMappingList &
1205Target::GetImageSearchPathList ()
1206{
1207 return m_image_search_paths;
1208}
1209
1210void
1211Target::ImageSearchPathsChanged
1212(
1213 const PathMappingList &path_list,
1214 void *baton
1215)
1216{
1217 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:451218 ModuleSP exe_module_sp (target->GetExecutableModule());
1219 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241220 {
Greg Claytonaa149cb2011-08-11 02:48:451221 target->m_images.Clear();
1222 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:241223 }
1224}
1225
1226ClangASTContext *
1227Target::GetScratchClangASTContext()
1228{
Greg Clayton73da2442011-08-03 01:23:551229 // Now see if we know the target triple, and if so, create our scratch AST context:
1230 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1231 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:241232 return m_scratch_ast_context_ap.get();
1233}
Caroline Ticedaccaa92010-09-20 20:44:431234
Greg Clayton99d0faf2010-11-18 23:32:351235void
Caroline Tice20bd37f2011-03-10 22:14:101236Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:431237{
Greg Clayton99d0faf2010-11-18 23:32:351238 UserSettingsControllerSP &usc = GetSettingsController();
1239 usc.reset (new SettingsController);
1240 UserSettingsController::InitializeSettingsController (usc,
1241 SettingsController::global_settings_table,
1242 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:101243
1244 // Now call SettingsInitialize() on each 'child' setting of Target
1245 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:351246}
Caroline Ticedaccaa92010-09-20 20:44:431247
Greg Clayton99d0faf2010-11-18 23:32:351248void
Caroline Tice20bd37f2011-03-10 22:14:101249Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:351250{
Caroline Tice20bd37f2011-03-10 22:14:101251
1252 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1253
1254 Process::SettingsTerminate ();
1255
1256 // Now terminate Target Settings.
1257
Greg Clayton99d0faf2010-11-18 23:32:351258 UserSettingsControllerSP &usc = GetSettingsController();
1259 UserSettingsController::FinalizeSettingsController (usc);
1260 usc.reset();
1261}
Caroline Ticedaccaa92010-09-20 20:44:431262
Greg Clayton99d0faf2010-11-18 23:32:351263UserSettingsControllerSP &
1264Target::GetSettingsController ()
1265{
1266 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:431267 return g_settings_controller;
1268}
1269
1270ArchSpec
1271Target::GetDefaultArchitecture ()
1272{
Greg Clayton64195a22011-02-23 00:35:021273 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1274
1275 if (settings_controller_sp)
1276 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1277 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:431278}
1279
1280void
Greg Clayton64195a22011-02-23 00:35:021281Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:431282{
Greg Clayton64195a22011-02-23 00:35:021283 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1284
1285 if (settings_controller_sp)
1286 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:431287}
1288
Greg Clayton0603aa92010-10-04 01:05:561289Target *
1290Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1291{
1292 // The target can either exist in the "process" of ExecutionContext, or in
1293 // the "target_sp" member of SymbolContext. This accessor helper function
1294 // will get the target from one of these locations.
1295
1296 Target *target = NULL;
1297 if (sc_ptr != NULL)
1298 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:261299 if (target == NULL && exe_ctx_ptr)
1300 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:561301 return target;
1302}
1303
1304
Caroline Tice1559a462010-09-27 00:30:101305void
1306Target::UpdateInstanceName ()
1307{
1308 StreamString sstr;
1309
Greg Claytonaa149cb2011-08-11 02:48:451310 Module *exe_module = GetExecutableModulePointer();
1311 if (exe_module)
Caroline Tice1559a462010-09-27 00:30:101312 {
Greg Clayton307de252010-10-27 02:06:371313 sstr.Printf ("%s_%s",
Greg Claytonaa149cb2011-08-11 02:48:451314 exe_module->GetFileSpec().GetFilename().AsCString(),
1315 exe_module->GetArchitecture().GetArchitectureName());
1316 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:101317 }
1318}
1319
Sean Callanan322f5292010-10-29 00:29:031320const char *
1321Target::GetExpressionPrefixContentsAsCString ()
1322{
Greg Clayton7e14f912011-04-23 02:04:551323 if (m_expr_prefix_contents_sp)
1324 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1325 return NULL;
Sean Callanan322f5292010-10-29 00:29:031326}
1327
Greg Clayton8b2fe6d2010-12-14 02:59:591328ExecutionResults
1329Target::EvaluateExpression
1330(
1331 const char *expr_cstr,
1332 StackFrame *frame,
Sean Callanan3bfdaa22011-09-15 02:13:071333 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton8b2fe6d2010-12-14 02:59:591334 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:351335 bool keep_in_memory,
Jim Ingham2837b762011-05-04 03:43:181336 lldb::DynamicValueType use_dynamic,
Greg Clayton8b2fe6d2010-12-14 02:59:591337 lldb::ValueObjectSP &result_valobj_sp
1338)
1339{
1340 ExecutionResults execution_results = eExecutionSetupError;
1341
1342 result_valobj_sp.reset();
Jim Ingham6026ca32011-05-12 02:06:141343
1344 // We shouldn't run stop hooks in expressions.
1345 // Be sure to reset this if you return anywhere within this function.
1346 bool old_suppress_value = m_suppress_stop_hooks;
1347 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:591348
1349 ExecutionContext exe_ctx;
1350 if (frame)
1351 {
1352 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:081353 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:181354 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granata27b625e2011-08-09 01:04:561355 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1356 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham2837b762011-05-04 03:43:181357 lldb::VariableSP var_sp;
1358 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1359 use_dynamic,
1360 expr_path_options,
1361 var_sp,
1362 error);
Greg Clayton8b2fe6d2010-12-14 02:59:591363 }
1364 else if (m_process_sp)
1365 {
1366 m_process_sp->CalculateExecutionContext(exe_ctx);
1367 }
1368 else
1369 {
1370 CalculateExecutionContext(exe_ctx);
1371 }
1372
1373 if (result_valobj_sp)
1374 {
1375 execution_results = eExecutionCompleted;
1376 // We got a result from the frame variable expression path above...
1377 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1378
1379 lldb::ValueObjectSP const_valobj_sp;
1380
1381 // Check in case our value is already a constant value
1382 if (result_valobj_sp->GetIsConstant())
1383 {
1384 const_valobj_sp = result_valobj_sp;
1385 const_valobj_sp->SetName (persistent_variable_name);
1386 }
1387 else
Jim Ingham78a685a2011-04-16 00:01:131388 {
Jim Ingham2837b762011-05-04 03:43:181389 if (use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:131390 {
Jim Ingham2837b762011-05-04 03:43:181391 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:131392 if (dynamic_sp)
1393 result_valobj_sp = dynamic_sp;
1394 }
1395
Jim Ingham6035b672011-03-31 00:19:251396 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:131397 }
Greg Clayton8b2fe6d2010-12-14 02:59:591398
Sean Callanan92adcac2011-01-13 08:53:351399 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1400
Greg Clayton8b2fe6d2010-12-14 02:59:591401 result_valobj_sp = const_valobj_sp;
1402
Sean Callanan92adcac2011-01-13 08:53:351403 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1404 assert (clang_expr_variable_sp.get());
1405
1406 // Set flags and live data as appropriate
1407
1408 const Value &result_value = live_valobj_sp->GetValue();
1409
1410 switch (result_value.GetValueType())
1411 {
1412 case Value::eValueTypeHostAddress:
1413 case Value::eValueTypeFileAddress:
1414 // we don't do anything with these for now
1415 break;
1416 case Value::eValueTypeScalar:
1417 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1418 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1419 break;
1420 case Value::eValueTypeLoadAddress:
1421 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1422 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1423 break;
1424 }
Greg Clayton8b2fe6d2010-12-14 02:59:591425 }
1426 else
1427 {
1428 // Make sure we aren't just trying to see the value of a persistent
1429 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:351430 lldb::ClangExpressionVariableSP persistent_var_sp;
1431 // Only check for persistent variables the expression starts with a '$'
1432 if (expr_cstr[0] == '$')
1433 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1434
Greg Clayton8b2fe6d2010-12-14 02:59:591435 if (persistent_var_sp)
1436 {
1437 result_valobj_sp = persistent_var_sp->GetValueObject ();
1438 execution_results = eExecutionCompleted;
1439 }
1440 else
1441 {
1442 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan3bfdaa22011-09-15 02:13:071443
Greg Clayton8b2fe6d2010-12-14 02:59:591444 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:071445 execution_policy,
Sean Callanan92adcac2011-01-13 08:53:351446 unwind_on_error,
Greg Clayton8b2fe6d2010-12-14 02:59:591447 expr_cstr,
1448 prefix,
1449 result_valobj_sp);
1450 }
1451 }
Jim Ingham6026ca32011-05-12 02:06:141452
1453 m_suppress_stop_hooks = old_suppress_value;
1454
Greg Clayton8b2fe6d2010-12-14 02:59:591455 return execution_results;
1456}
1457
Greg Claytonf3ef3d22011-05-22 22:46:531458lldb::addr_t
1459Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1460{
1461 addr_t code_addr = load_addr;
1462 switch (m_arch.GetMachine())
1463 {
1464 case llvm::Triple::arm:
1465 case llvm::Triple::thumb:
1466 switch (addr_class)
1467 {
1468 case eAddressClassData:
1469 case eAddressClassDebug:
1470 return LLDB_INVALID_ADDRESS;
1471
1472 case eAddressClassUnknown:
1473 case eAddressClassInvalid:
1474 case eAddressClassCode:
1475 case eAddressClassCodeAlternateISA:
1476 case eAddressClassRuntime:
1477 // Check if bit zero it no set?
1478 if ((code_addr & 1ull) == 0)
1479 {
1480 // Bit zero isn't set, check if the address is a multiple of 2?
1481 if (code_addr & 2ull)
1482 {
1483 // The address is a multiple of 2 so it must be thumb, set bit zero
1484 code_addr |= 1ull;
1485 }
1486 else if (addr_class == eAddressClassCodeAlternateISA)
1487 {
1488 // We checked the address and the address claims to be the alternate ISA
1489 // which means thumb, so set bit zero.
1490 code_addr |= 1ull;
1491 }
1492 }
1493 break;
1494 }
1495 break;
1496
1497 default:
1498 break;
1499 }
1500 return code_addr;
1501}
1502
1503lldb::addr_t
1504Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1505{
1506 addr_t opcode_addr = load_addr;
1507 switch (m_arch.GetMachine())
1508 {
1509 case llvm::Triple::arm:
1510 case llvm::Triple::thumb:
1511 switch (addr_class)
1512 {
1513 case eAddressClassData:
1514 case eAddressClassDebug:
1515 return LLDB_INVALID_ADDRESS;
1516
1517 case eAddressClassInvalid:
1518 case eAddressClassUnknown:
1519 case eAddressClassCode:
1520 case eAddressClassCodeAlternateISA:
1521 case eAddressClassRuntime:
1522 opcode_addr &= ~(1ull);
1523 break;
1524 }
1525 break;
1526
1527 default:
1528 break;
1529 }
1530 return opcode_addr;
1531}
1532
Jim Ingham9575d842011-03-11 03:53:591533lldb::user_id_t
1534Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1535{
1536 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1537 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1538 m_stop_hooks[new_uid] = new_hook_sp;
1539 return new_uid;
1540}
1541
1542bool
1543Target::RemoveStopHookByID (lldb::user_id_t user_id)
1544{
1545 size_t num_removed;
1546 num_removed = m_stop_hooks.erase (user_id);
1547 if (num_removed == 0)
1548 return false;
1549 else
1550 return true;
1551}
1552
1553void
1554Target::RemoveAllStopHooks ()
1555{
1556 m_stop_hooks.clear();
1557}
1558
1559Target::StopHookSP
1560Target::GetStopHookByID (lldb::user_id_t user_id)
1561{
1562 StopHookSP found_hook;
1563
1564 StopHookCollection::iterator specified_hook_iter;
1565 specified_hook_iter = m_stop_hooks.find (user_id);
1566 if (specified_hook_iter != m_stop_hooks.end())
1567 found_hook = (*specified_hook_iter).second;
1568 return found_hook;
1569}
1570
1571bool
1572Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1573{
1574 StopHookCollection::iterator specified_hook_iter;
1575 specified_hook_iter = m_stop_hooks.find (user_id);
1576 if (specified_hook_iter == m_stop_hooks.end())
1577 return false;
1578
1579 (*specified_hook_iter).second->SetIsActive (active_state);
1580 return true;
1581}
1582
1583void
1584Target::SetAllStopHooksActiveState (bool active_state)
1585{
1586 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1587 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1588 {
1589 (*pos).second->SetIsActive (active_state);
1590 }
1591}
1592
1593void
1594Target::RunStopHooks ()
1595{
Jim Ingham6026ca32011-05-12 02:06:141596 if (m_suppress_stop_hooks)
1597 return;
1598
Jim Ingham9575d842011-03-11 03:53:591599 if (!m_process_sp)
1600 return;
1601
1602 if (m_stop_hooks.empty())
1603 return;
1604
1605 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1606
1607 // If there aren't any active stop hooks, don't bother either:
1608 bool any_active_hooks = false;
1609 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1610 {
1611 if ((*pos).second->IsActive())
1612 {
1613 any_active_hooks = true;
1614 break;
1615 }
1616 }
1617 if (!any_active_hooks)
1618 return;
1619
1620 CommandReturnObject result;
1621
1622 std::vector<ExecutionContext> exc_ctx_with_reasons;
1623 std::vector<SymbolContext> sym_ctx_with_reasons;
1624
1625 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1626 size_t num_threads = cur_threadlist.GetSize();
1627 for (size_t i = 0; i < num_threads; i++)
1628 {
1629 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1630 if (cur_thread_sp->ThreadStoppedForAReason())
1631 {
1632 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1633 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1634 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1635 }
1636 }
1637
1638 // If no threads stopped for a reason, don't run the stop-hooks.
1639 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1640 if (num_exe_ctx == 0)
1641 return;
1642
Jim Ingham5b52f0c2011-06-02 23:58:261643 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1644 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:591645
1646 bool keep_going = true;
1647 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271648 bool print_hook_header;
1649 bool print_thread_header;
1650
1651 if (num_exe_ctx == 1)
1652 print_thread_header = false;
1653 else
1654 print_thread_header = true;
1655
1656 if (m_stop_hooks.size() == 1)
1657 print_hook_header = false;
1658 else
1659 print_hook_header = true;
1660
Jim Ingham9575d842011-03-11 03:53:591661 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1662 {
1663 // result.Clear();
1664 StopHookSP cur_hook_sp = (*pos).second;
1665 if (!cur_hook_sp->IsActive())
1666 continue;
1667
1668 bool any_thread_matched = false;
1669 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1670 {
1671 if ((cur_hook_sp->GetSpecifier () == NULL
1672 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1673 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Claytonc14ee322011-09-22 04:58:261674 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Ingham9575d842011-03-11 03:53:591675 {
1676 if (!hooks_ran)
1677 {
Jim Ingham9575d842011-03-11 03:53:591678 hooks_ran = true;
1679 }
Jim Ingham381e25b2011-03-22 01:47:271680 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591681 {
1682 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1683 any_thread_matched = true;
1684 }
1685
Jim Ingham381e25b2011-03-22 01:47:271686 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:261687 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591688
1689 bool stop_on_continue = true;
1690 bool stop_on_error = true;
1691 bool echo_commands = false;
1692 bool print_results = true;
1693 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:511694 &exc_ctx_with_reasons[i],
1695 stop_on_continue,
1696 stop_on_error,
1697 echo_commands,
1698 print_results,
1699 result);
Jim Ingham9575d842011-03-11 03:53:591700
1701 // If the command started the target going again, we should bag out of
1702 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:511703 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1704 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:591705 {
1706 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1707 keep_going = false;
1708 }
1709 }
1710 }
1711 }
Jason Molenda879cf772011-09-23 00:42:551712
Caroline Tice969ed3d12011-05-02 20:41:461713 result.GetImmediateOutputStream()->Flush();
1714 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:591715}
1716
Greg Clayton7b242382011-07-08 00:48:091717bool
1718Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1719{
1720 bool changed = false;
1721 if (module)
1722 {
1723 ObjectFile *object_file = module->GetObjectFile();
1724 if (object_file)
1725 {
1726 SectionList *section_list = object_file->GetSectionList ();
1727 if (section_list)
1728 {
1729 // All sections listed in the dyld image info structure will all
1730 // either be fixed up already, or they will all be off by a single
1731 // slide amount that is determined by finding the first segment
1732 // that is at file offset zero which also has bytes (a file size
1733 // that is greater than zero) in the object file.
1734
1735 // Determine the slide amount (if any)
1736 const size_t num_sections = section_list->GetSize();
1737 size_t sect_idx = 0;
1738 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1739 {
1740 // Iterate through the object file sections to find the
1741 // first section that starts of file offset zero and that
1742 // has bytes in the file...
1743 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1744 if (section)
1745 {
1746 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1747 changed = true;
1748 }
1749 }
1750 }
1751 }
1752 }
1753 return changed;
1754}
1755
1756
Jim Ingham9575d842011-03-11 03:53:591757//--------------------------------------------------------------
1758// class Target::StopHook
1759//--------------------------------------------------------------
1760
1761
1762Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1763 UserID (uid),
1764 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:591765 m_commands (),
1766 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:401767 m_thread_spec_ap(NULL),
1768 m_active (true)
Jim Ingham9575d842011-03-11 03:53:591769{
1770}
1771
1772Target::StopHook::StopHook (const StopHook &rhs) :
1773 UserID (rhs.GetID()),
1774 m_target_sp (rhs.m_target_sp),
1775 m_commands (rhs.m_commands),
1776 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:401777 m_thread_spec_ap (NULL),
1778 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:591779{
1780 if (rhs.m_thread_spec_ap.get() != NULL)
1781 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1782}
1783
1784
1785Target::StopHook::~StopHook ()
1786{
1787}
1788
1789void
1790Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1791{
1792 m_thread_spec_ap.reset (specifier);
1793}
1794
1795
1796void
1797Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1798{
1799 int indent_level = s->GetIndentLevel();
1800
1801 s->SetIndentLevel(indent_level + 2);
1802
1803 s->Printf ("Hook: %d\n", GetID());
1804 if (m_active)
1805 s->Indent ("State: enabled\n");
1806 else
1807 s->Indent ("State: disabled\n");
1808
1809 if (m_specifier_sp)
1810 {
1811 s->Indent();
1812 s->PutCString ("Specifier:\n");
1813 s->SetIndentLevel (indent_level + 4);
1814 m_specifier_sp->GetDescription (s, level);
1815 s->SetIndentLevel (indent_level + 2);
1816 }
1817
1818 if (m_thread_spec_ap.get() != NULL)
1819 {
1820 StreamString tmp;
1821 s->Indent("Thread:\n");
1822 m_thread_spec_ap->GetDescription (&tmp, level);
1823 s->SetIndentLevel (indent_level + 4);
1824 s->Indent (tmp.GetData());
1825 s->PutCString ("\n");
1826 s->SetIndentLevel (indent_level + 2);
1827 }
1828
1829 s->Indent ("Commands: \n");
1830 s->SetIndentLevel (indent_level + 4);
1831 uint32_t num_commands = m_commands.GetSize();
1832 for (uint32_t i = 0; i < num_commands; i++)
1833 {
1834 s->Indent(m_commands.GetStringAtIndex(i));
1835 s->PutCString ("\n");
1836 }
1837 s->SetIndentLevel (indent_level);
1838}
1839
1840
Caroline Ticedaccaa92010-09-20 20:44:431841//--------------------------------------------------------------
1842// class Target::SettingsController
1843//--------------------------------------------------------------
1844
1845Target::SettingsController::SettingsController () :
1846 UserSettingsController ("target", Debugger::GetSettingsController()),
1847 m_default_architecture ()
1848{
1849 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1850 InstanceSettings::GetDefaultName().AsCString()));
1851}
1852
1853Target::SettingsController::~SettingsController ()
1854{
1855}
1856
1857lldb::InstanceSettingsSP
1858Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1859{
Greg Claytondbe54502010-11-19 03:46:011860 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1861 false,
1862 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431863 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1864 return new_settings_sp;
1865}
1866
Caroline Ticedaccaa92010-09-20 20:44:431867
Jim Ingham78a685a2011-04-16 00:01:131868#define TSC_DEFAULT_ARCH "default-arch"
1869#define TSC_EXPR_PREFIX "expr-prefix"
Jim Ingham78a685a2011-04-16 00:01:131870#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton385aa282011-04-22 03:55:061871#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Clayton7e14f912011-04-23 02:04:551872#define TSC_SOURCE_MAP "source-map"
Enrico Granata22c55d12011-08-12 02:00:061873#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata9128ee2f2011-09-06 19:20:511874#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytonbfe5f3b2011-02-18 01:44:251875
1876
1877static const ConstString &
1878GetSettingNameForDefaultArch ()
1879{
1880 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytonbfe5f3b2011-02-18 01:44:251881 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431882}
1883
Greg Claytonbfe5f3b2011-02-18 01:44:251884static const ConstString &
1885GetSettingNameForExpressionPrefix ()
1886{
1887 static ConstString g_const_string (TSC_EXPR_PREFIX);
1888 return g_const_string;
1889}
1890
1891static const ConstString &
Jim Ingham78a685a2011-04-16 00:01:131892GetSettingNameForPreferDynamicValue ()
1893{
1894 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1895 return g_const_string;
1896}
1897
Greg Clayton7e14f912011-04-23 02:04:551898static const ConstString &
1899GetSettingNameForSourcePathMap ()
1900{
1901 static ConstString g_const_string (TSC_SOURCE_MAP);
1902 return g_const_string;
1903}
Greg Claytonbfe5f3b2011-02-18 01:44:251904
Greg Clayton385aa282011-04-22 03:55:061905static const ConstString &
1906GetSettingNameForSkipPrologue ()
1907{
1908 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1909 return g_const_string;
1910}
1911
Enrico Granata22c55d12011-08-12 02:00:061912static const ConstString &
1913GetSettingNameForMaxChildren ()
1914{
1915 static ConstString g_const_string (TSC_MAX_CHILDREN);
1916 return g_const_string;
1917}
Greg Clayton385aa282011-04-22 03:55:061918
Enrico Granata9128ee2f2011-09-06 19:20:511919static const ConstString &
1920GetSettingNameForMaxStringSummaryLength ()
1921{
1922 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1923 return g_const_string;
1924}
Greg Clayton385aa282011-04-22 03:55:061925
Caroline Ticedaccaa92010-09-20 20:44:431926bool
1927Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1928 const char *index_value,
1929 const char *value,
1930 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541931 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431932 Error&err)
1933{
Greg Claytonbfe5f3b2011-02-18 01:44:251934 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431935 {
Greg Claytoneb0103f2011-04-07 22:46:351936 m_default_architecture.SetTriple (value, NULL);
Greg Clayton64195a22011-02-23 00:35:021937 if (!m_default_architecture.IsValid())
1938 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431939 }
1940 return true;
1941}
1942
1943
1944bool
1945Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1946 StringList &value,
1947 Error &err)
1948{
Greg Claytonbfe5f3b2011-02-18 01:44:251949 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431950 {
Greg Clayton307de252010-10-27 02:06:371951 // If the arch is invalid (the default), don't show a string for it
1952 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021953 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431954 return true;
1955 }
1956 else
1957 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1958
1959 return false;
1960}
1961
1962//--------------------------------------------------------------
1963// class TargetInstanceSettings
1964//--------------------------------------------------------------
1965
Greg Clayton85851dd2010-12-04 00:10:171966TargetInstanceSettings::TargetInstanceSettings
1967(
1968 UserSettingsController &owner,
1969 bool live_instance,
1970 const char *name
1971) :
Greg Claytonbfe5f3b2011-02-18 01:44:251972 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Clayton7e14f912011-04-23 02:04:551973 m_expr_prefix_file (),
1974 m_expr_prefix_contents_sp (),
Jim Ingham2837b762011-05-04 03:43:181975 m_prefer_dynamic_value (2),
Greg Clayton7e14f912011-04-23 02:04:551976 m_skip_prologue (true, true),
Enrico Granata22c55d12011-08-12 02:00:061977 m_source_map (NULL, NULL),
Enrico Granata9128ee2f2011-09-06 19:20:511978 m_max_children_display(256),
1979 m_max_strlen_length(1024)
Caroline Ticedaccaa92010-09-20 20:44:431980{
1981 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1982 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1983 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1984 // This is true for CreateInstanceName() too.
1985
1986 if (GetInstanceName () == InstanceSettings::InvalidName())
1987 {
1988 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1989 m_owner.RegisterInstanceSettings (this);
1990 }
1991
1992 if (live_instance)
1993 {
1994 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1995 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:431996 }
1997}
1998
1999TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton7e14f912011-04-23 02:04:552000 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
2001 m_expr_prefix_file (rhs.m_expr_prefix_file),
2002 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
2003 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2004 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata22c55d12011-08-12 02:00:062005 m_source_map (rhs.m_source_map),
Enrico Granata9128ee2f2011-09-06 19:20:512006 m_max_children_display(rhs.m_max_children_display),
2007 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Ticedaccaa92010-09-20 20:44:432008{
2009 if (m_instance_name != InstanceSettings::GetDefaultName())
2010 {
2011 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2012 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:432013 }
2014}
2015
2016TargetInstanceSettings::~TargetInstanceSettings ()
2017{
2018}
2019
2020TargetInstanceSettings&
2021TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2022{
2023 if (this != &rhs)
2024 {
2025 }
2026
2027 return *this;
2028}
2029
Caroline Ticedaccaa92010-09-20 20:44:432030void
2031TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2032 const char *index_value,
2033 const char *value,
2034 const ConstString &instance_name,
2035 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:542036 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:432037 Error &err,
2038 bool pending)
2039{
Greg Claytonbfe5f3b2011-02-18 01:44:252040 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:032041 {
Greg Clayton7e14f912011-04-23 02:04:552042 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2043 if (err.Success())
Sean Callanan322f5292010-10-29 00:29:032044 {
Greg Clayton7e14f912011-04-23 02:04:552045 switch (op)
Sean Callanan322f5292010-10-29 00:29:032046 {
Greg Clayton7e14f912011-04-23 02:04:552047 default:
2048 break;
2049 case eVarSetOperationAssign:
2050 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:032051 {
Greg Clayton7e14f912011-04-23 02:04:552052 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2053 {
2054 err.SetErrorToGenericError ();
2055 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
2056 return;
2057 }
2058
2059 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
2060
2061 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
2062 {
2063 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
2064 m_expr_prefix_contents_sp.reset();
2065 }
Sean Callanan322f5292010-10-29 00:29:032066 }
Greg Clayton7e14f912011-04-23 02:04:552067 break;
2068 case eVarSetOperationClear:
2069 m_expr_prefix_contents_sp.reset();
Sean Callanan322f5292010-10-29 00:29:032070 }
Sean Callanan322f5292010-10-29 00:29:032071 }
2072 }
Jim Ingham78a685a2011-04-16 00:01:132073 else if (var_name == GetSettingNameForPreferDynamicValue())
2074 {
Jim Ingham2837b762011-05-04 03:43:182075 int new_value;
2076 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2077 if (err.Success())
2078 m_prefer_dynamic_value = new_value;
Greg Clayton385aa282011-04-22 03:55:062079 }
2080 else if (var_name == GetSettingNameForSkipPrologue())
2081 {
Greg Clayton7e14f912011-04-23 02:04:552082 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2083 }
Enrico Granata22c55d12011-08-12 02:00:062084 else if (var_name == GetSettingNameForMaxChildren())
2085 {
2086 bool ok;
2087 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2088 if (ok)
2089 m_max_children_display = new_value;
2090 }
Enrico Granata9128ee2f2011-09-06 19:20:512091 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2092 {
2093 bool ok;
2094 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2095 if (ok)
2096 m_max_strlen_length = new_value;
2097 }
Greg Clayton7e14f912011-04-23 02:04:552098 else if (var_name == GetSettingNameForSourcePathMap ())
2099 {
2100 switch (op)
2101 {
2102 case eVarSetOperationReplace:
2103 case eVarSetOperationInsertBefore:
2104 case eVarSetOperationInsertAfter:
2105 case eVarSetOperationRemove:
2106 default:
2107 break;
2108 case eVarSetOperationAssign:
2109 m_source_map.Clear(true);
2110 // Fall through to append....
2111 case eVarSetOperationAppend:
2112 {
2113 Args args(value);
2114 const uint32_t argc = args.GetArgumentCount();
2115 if (argc & 1 || argc == 0)
2116 {
2117 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2118 }
2119 else
2120 {
2121 char resolved_new_path[PATH_MAX];
2122 FileSpec file_spec;
2123 const char *old_path;
2124 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2125 {
2126 const char *new_path = args.GetArgumentAtIndex(idx+1);
2127 assert (new_path); // We have an even number of paths, this shouldn't happen!
2128
2129 file_spec.SetFile(new_path, true);
2130 if (file_spec.Exists())
2131 {
2132 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2133 {
2134 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2135 return;
2136 }
2137 }
2138 else
2139 {
2140 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2141 return;
2142 }
2143 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2144 }
2145 }
2146 }
2147 break;
2148
2149 case eVarSetOperationClear:
2150 m_source_map.Clear(true);
2151 break;
2152 }
Jim Ingham78a685a2011-04-16 00:01:132153 }
Caroline Ticedaccaa92010-09-20 20:44:432154}
2155
2156void
Greg Claytonbfe5f3b2011-02-18 01:44:252157TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:432158{
Sean Callanan322f5292010-10-29 00:29:032159 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2160
2161 if (!new_settings_ptr)
2162 return;
2163
Greg Clayton7e14f912011-04-23 02:04:552164 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
2165 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
2166 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
2167 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata22c55d12011-08-12 02:00:062168 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata9128ee2f2011-09-06 19:20:512169 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Ticedaccaa92010-09-20 20:44:432170}
2171
Caroline Tice12cecd72010-09-20 21:37:422172bool
Caroline Ticedaccaa92010-09-20 20:44:432173TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2174 const ConstString &var_name,
2175 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:422176 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:432177{
Greg Claytonbfe5f3b2011-02-18 01:44:252178 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:032179 {
Greg Clayton7e14f912011-04-23 02:04:552180 char path[PATH_MAX];
2181 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2182 if (path_len > 0)
2183 value.AppendString (path, path_len);
Sean Callanan322f5292010-10-29 00:29:032184 }
Jim Ingham78a685a2011-04-16 00:01:132185 else if (var_name == GetSettingNameForPreferDynamicValue())
2186 {
Jim Ingham2837b762011-05-04 03:43:182187 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Ingham78a685a2011-04-16 00:01:132188 }
Greg Clayton385aa282011-04-22 03:55:062189 else if (var_name == GetSettingNameForSkipPrologue())
2190 {
2191 if (m_skip_prologue)
2192 value.AppendString ("true");
2193 else
2194 value.AppendString ("false");
2195 }
Greg Clayton7e14f912011-04-23 02:04:552196 else if (var_name == GetSettingNameForSourcePathMap ())
2197 {
2198 }
Enrico Granata22c55d12011-08-12 02:00:062199 else if (var_name == GetSettingNameForMaxChildren())
2200 {
2201 StreamString count_str;
2202 count_str.Printf ("%d", m_max_children_display);
2203 value.AppendString (count_str.GetData());
2204 }
Enrico Granata9128ee2f2011-09-06 19:20:512205 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2206 {
2207 StreamString count_str;
2208 count_str.Printf ("%d", m_max_strlen_length);
2209 value.AppendString (count_str.GetData());
2210 }
Sean Callanan322f5292010-10-29 00:29:032211 else
2212 {
2213 if (err)
2214 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2215 return false;
2216 }
2217
2218 return true;
Caroline Ticedaccaa92010-09-20 20:44:432219}
2220
2221const ConstString
2222TargetInstanceSettings::CreateInstanceName ()
2223{
Caroline Ticedaccaa92010-09-20 20:44:432224 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:102225 static int instance_count = 1;
2226
Caroline Ticedaccaa92010-09-20 20:44:432227 sstr.Printf ("target_%d", instance_count);
2228 ++instance_count;
2229
2230 const ConstString ret_val (sstr.GetData());
2231 return ret_val;
2232}
2233
2234//--------------------------------------------------
2235// Target::SettingsController Variable Tables
2236//--------------------------------------------------
Jim Ingham2837b762011-05-04 03:43:182237OptionEnumValueElement
2238TargetInstanceSettings::g_dynamic_value_types[] =
2239{
Greg Clayton5d2fbfe2011-05-30 00:39:482240{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2241{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2242{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham2837b762011-05-04 03:43:182243{ 0, NULL, NULL }
2244};
Caroline Ticedaccaa92010-09-20 20:44:432245
2246SettingEntry
2247Target::SettingsController::global_settings_table[] =
2248{
Greg Claytonbfe5f3b2011-02-18 01:44:252249 // var-name var-type default enum init'd hidden help-text
2250 // ================= ================== =========== ==== ====== ====== =========================================================================
2251 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2252 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2253};
2254
Caroline Ticedaccaa92010-09-20 20:44:432255SettingEntry
2256Target::SettingsController::instance_settings_table[] =
2257{
Enrico Granata9128ee2f2011-09-06 19:20:512258 // var-name var-type default enum init'd hidden help-text
2259 // ================= ================== =============== ======================= ====== ====== =========================================================================
2260 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2261 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2262 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2263 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2264 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2265 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2266 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:432267};