blob: 036a0180ba592a1c4faaaeba926f274bd71f40f3 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham03c8ee52011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen096c2932011-09-26 22:40:50 +000021#include "lldb/Breakpoint/WatchpointLocation.h"
Greg Clayton427f2902010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Caroline Tice4a348082011-05-02 20:41:46 +000025#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000027#include "lldb/Core/Timer.h"
28#include "lldb/Core/ValueObject.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000029#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000031#include "lldb/Interpreter/CommandInterpreter.h"
32#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/lldb-private-log.h"
34#include "lldb/Symbol/ObjectFile.h"
35#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000036#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000037#include "lldb/Target/Thread.h"
38#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
43//----------------------------------------------------------------------
44// Target constructor
45//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000046Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
47 Broadcaster ("lldb.target"),
48 ExecutionContextScope (),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000049 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000050 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000051 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000052 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000053 m_arch (target_arch),
54 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000055 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000056 m_breakpoint_list (false),
57 m_internal_breakpoint_list (true),
Johnny Chen9a3c2a52011-09-06 20:05:25 +000058 m_watchpoint_location_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000059 m_process_sp (),
60 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000061 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000062 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000063 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000064 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000065 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000066 m_stop_hook_next_id (0),
67 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000068{
Greg Clayton49ce6822010-10-31 03:01:06 +000069 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
70 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
71 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
72
Greg Claytone005f2c2010-11-06 01:53:30 +000073 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000074 if (log)
75 log->Printf ("%p Target::Target()", this);
76}
77
78//----------------------------------------------------------------------
79// Destructor
80//----------------------------------------------------------------------
81Target::~Target()
82{
Greg Claytone005f2c2010-11-06 01:53:30 +000083 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000084 if (log)
85 log->Printf ("%p Target::~Target()", this);
86 DeleteCurrentProcess ();
87}
88
89void
Caroline Tice7826c882010-10-26 03:11:13 +000090Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000091{
Greg Clayton3fed8b92010-10-08 00:21:05 +000092// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000093 if (description_level != lldb::eDescriptionLevelBrief)
94 {
95 s->Indent();
96 s->PutCString("Target\n");
97 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000098 m_images.Dump(s);
99 m_breakpoint_list.Dump(s);
100 m_internal_breakpoint_list.Dump(s);
101 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000102 }
103 else
104 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000105 Module *exe_module = GetExecutableModulePointer();
106 if (exe_module)
107 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000108 else
109 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000110 }
Chris Lattner24943d22010-06-08 16:52:24 +0000111}
112
113void
114Target::DeleteCurrentProcess ()
115{
116 if (m_process_sp.get())
117 {
Greg Clayton49480b12010-09-14 23:52:43 +0000118 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000119 if (m_process_sp->IsAlive())
120 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000121
122 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000123
124 // Do any cleanup of the target we need to do between process instances.
125 // NB It is better to do this before destroying the process in case the
126 // clean up needs some help from the process.
127 m_breakpoint_list.ClearAllBreakpointSites();
128 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenc86582f2011-09-23 21:21:43 +0000129 // Disable watchpoint locations just on the debugger side.
130 DisableAllWatchpointLocations(false);
Chris Lattner24943d22010-06-08 16:52:24 +0000131 m_process_sp.reset();
132 }
133}
134
135const lldb::ProcessSP &
136Target::CreateProcess (Listener &listener, const char *plugin_name)
137{
138 DeleteCurrentProcess ();
139 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
140 return m_process_sp;
141}
142
143const lldb::ProcessSP &
144Target::GetProcessSP () const
145{
146 return m_process_sp;
147}
148
149lldb::TargetSP
150Target::GetSP()
151{
Greg Clayton987c7eb2011-09-17 08:33:22 +0000152 // This object contains an instrusive ref count base class so we can
153 // easily make a shared pointer to this object
154 return TargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000155}
156
Greg Clayton153ccd72011-08-10 02:10:13 +0000157void
158Target::Destroy()
159{
160 Mutex::Locker locker (m_mutex);
161 DeleteCurrentProcess ();
162 m_platform_sp.reset();
163 m_arch.Clear();
164 m_images.Clear();
165 m_section_load_list.Clear();
166 const bool notify = false;
167 m_breakpoint_list.RemoveAll(notify);
168 m_internal_breakpoint_list.RemoveAll(notify);
169 m_last_created_breakpoint.reset();
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000170 m_last_created_watchpoint_location.reset();
Greg Clayton153ccd72011-08-10 02:10:13 +0000171 m_search_filter_sp.reset();
172 m_image_search_paths.Clear(notify);
173 m_scratch_ast_context_ap.reset();
174 m_persistent_variables.Clear();
175 m_stop_hooks.clear();
176 m_stop_hook_next_id = 0;
177 m_suppress_stop_hooks = false;
178}
179
180
Chris Lattner24943d22010-06-08 16:52:24 +0000181BreakpointList &
182Target::GetBreakpointList(bool internal)
183{
184 if (internal)
185 return m_internal_breakpoint_list;
186 else
187 return m_breakpoint_list;
188}
189
190const BreakpointList &
191Target::GetBreakpointList(bool internal) const
192{
193 if (internal)
194 return m_internal_breakpoint_list;
195 else
196 return m_breakpoint_list;
197}
198
199BreakpointSP
200Target::GetBreakpointByID (break_id_t break_id)
201{
202 BreakpointSP bp_sp;
203
204 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
205 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
206 else
207 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
208
209 return bp_sp;
210}
211
212BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000213Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
214 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000215 RegularExpression &source_regex,
216 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000217{
Jim Inghamd6d47972011-09-23 00:54:11 +0000218 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
219 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000220 return CreateBreakpoint (filter_sp, resolver_sp, internal);
221}
222
223
224BreakpointSP
225Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
226{
227 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner24943d22010-06-08 16:52:24 +0000228 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
229 return CreateBreakpoint (filter_sp, resolver_sp, internal);
230}
231
232
233BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000234Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000235{
Chris Lattner24943d22010-06-08 16:52:24 +0000236 Address so_addr;
237 // Attempt to resolve our load address if possible, though it is ok if
238 // it doesn't resolve to section/offset.
239
Greg Clayton33ed1702010-08-24 00:45:41 +0000240 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000241 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000242 if (!so_addr.IsValid())
243 {
244 // The address didn't resolve, so just set this as an absolute address
245 so_addr.SetOffset (addr);
246 }
247 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000248 return bp_sp;
249}
250
251BreakpointSP
252Target::CreateBreakpoint (Address &addr, bool internal)
253{
254 TargetSP target_sp = this->GetSP();
255 SearchFilterSP filter_sp(new SearchFilter (target_sp));
256 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
257 return CreateBreakpoint (filter_sp, resolver_sp, internal);
258}
259
260BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000261Target::CreateBreakpoint (const FileSpecList *containingModules,
262 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000263 const char *func_name,
264 uint32_t func_name_type_mask,
265 bool internal,
266 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000267{
Greg Clayton12bec712010-06-28 21:30:43 +0000268 BreakpointSP bp_sp;
269 if (func_name)
270 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000271 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000272
273 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
274 func_name,
275 func_name_type_mask,
276 Breakpoint::Exact,
277 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000278 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
279 }
280 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000281}
282
283
284SearchFilterSP
285Target::GetSearchFilterForModule (const FileSpec *containingModule)
286{
287 SearchFilterSP filter_sp;
288 lldb::TargetSP target_sp = this->GetSP();
289 if (containingModule != NULL)
290 {
291 // TODO: We should look into sharing module based search filters
292 // across many breakpoints like we do for the simple target based one
293 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
294 }
295 else
296 {
297 if (m_search_filter_sp.get() == NULL)
298 m_search_filter_sp.reset (new SearchFilter (target_sp));
299 filter_sp = m_search_filter_sp;
300 }
301 return filter_sp;
302}
303
Jim Ingham03c8ee52011-09-21 01:17:13 +0000304SearchFilterSP
305Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
306{
307 SearchFilterSP filter_sp;
308 lldb::TargetSP target_sp = this->GetSP();
309 if (containingModules && containingModules->GetSize() != 0)
310 {
311 // TODO: We should look into sharing module based search filters
312 // across many breakpoints like we do for the simple target based one
313 filter_sp.reset (new SearchFilterByModuleList (target_sp, *containingModules));
314 }
315 else
316 {
317 if (m_search_filter_sp.get() == NULL)
318 m_search_filter_sp.reset (new SearchFilter (target_sp));
319 filter_sp = m_search_filter_sp;
320 }
321 return filter_sp;
322}
323
Jim Inghamd6d47972011-09-23 00:54:11 +0000324SearchFilterSP
325Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles)
326{
327 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
328 return GetSearchFilterForModuleList(containingModules);
329
330 SearchFilterSP filter_sp;
331 lldb::TargetSP target_sp = this->GetSP();
332 if (containingModules == NULL)
333 {
334 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
335 // but that will take a little reworking.
336
337 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, FileSpecList(), *containingSourceFiles));
338 }
339 else
340 {
341 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, *containingModules, *containingSourceFiles));
342 }
343 return filter_sp;
344}
345
Chris Lattner24943d22010-06-08 16:52:24 +0000346BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000347Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
348 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000349 RegularExpression &func_regex,
350 bool internal,
351 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000352{
Jim Inghamd6d47972011-09-23 00:54:11 +0000353 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000354 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
355 func_regex,
356 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000357
358 return CreateBreakpoint (filter_sp, resolver_sp, internal);
359}
360
361BreakpointSP
362Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
363{
364 BreakpointSP bp_sp;
365 if (filter_sp && resolver_sp)
366 {
367 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
368 resolver_sp->SetBreakpoint (bp_sp.get());
369
370 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000371 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000372 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000373 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000374
Greg Claytone005f2c2010-11-06 01:53:30 +0000375 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000376 if (log)
377 {
378 StreamString s;
379 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
380 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
381 }
382
Chris Lattner24943d22010-06-08 16:52:24 +0000383 bp_sp->ResolveBreakpoint();
384 }
Jim Inghamd1686902010-10-14 23:45:03 +0000385
386 if (!internal && bp_sp)
387 {
388 m_last_created_breakpoint = bp_sp;
389 }
390
Chris Lattner24943d22010-06-08 16:52:24 +0000391 return bp_sp;
392}
393
Johnny Chenda5a8022011-09-20 23:28:55 +0000394bool
395Target::ProcessIsValid()
396{
397 return (m_process_sp && m_process_sp->IsAlive());
398}
399
Johnny Chen87ff53b2011-09-14 00:26:03 +0000400// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
401// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen34bbf852011-09-12 23:38:44 +0000402WatchpointLocationSP
403Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
404{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000405 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
406 if (log)
407 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
408 __FUNCTION__, addr, size, type);
409
Johnny Chen34bbf852011-09-12 23:38:44 +0000410 WatchpointLocationSP wp_loc_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000411 if (!ProcessIsValid())
Johnny Chen61286a52011-09-13 18:30:59 +0000412 return wp_loc_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000413 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen34bbf852011-09-12 23:38:44 +0000414 return wp_loc_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000415
Johnny Chen87ff53b2011-09-14 00:26:03 +0000416 // Currently we only support one watchpoint location per address, with total
417 // number of watchpoint locations limited by the hardware which the inferior
418 // is running on.
Johnny Chen69b6ec82011-09-13 23:29:31 +0000419 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
420 if (matched_sp)
421 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000422 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000423 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000424 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
425 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000426 // Return the existing watchpoint location if both size and type match.
427 if (size == old_size && type == old_type) {
428 wp_loc_sp = matched_sp;
429 wp_loc_sp->SetEnabled(false);
430 } else {
431 // Nil the matched watchpoint location; we will be creating a new one.
432 m_process_sp->DisableWatchpoint(matched_sp.get());
433 m_watchpoint_location_list.Remove(matched_sp->GetID());
434 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000435 }
436
Johnny Chen22a56cc2011-09-14 22:20:15 +0000437 if (!wp_loc_sp) {
438 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
439 if (!new_loc) {
440 printf("WatchpointLocation ctor failed, out of memory?\n");
441 return wp_loc_sp;
442 }
443 new_loc->SetWatchpointType(type);
Johnny Chen096c2932011-09-26 22:40:50 +0000444 new_loc->SetTarget(this);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000445 wp_loc_sp.reset(new_loc);
446 m_watchpoint_location_list.Add(wp_loc_sp);
447 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000448
Johnny Chen5b2fc572011-09-14 20:23:45 +0000449 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000450 if (log)
451 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
452 __FUNCTION__,
453 rc.Success() ? "succeeded" : "failed",
454 wp_loc_sp->GetID());
455
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000456 if (rc.Fail())
457 wp_loc_sp.reset();
458 else
459 m_last_created_watchpoint_location = wp_loc_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000460 return wp_loc_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000461}
462
Chris Lattner24943d22010-06-08 16:52:24 +0000463void
464Target::RemoveAllBreakpoints (bool internal_also)
465{
Greg Claytone005f2c2010-11-06 01:53:30 +0000466 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000467 if (log)
468 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
469
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000470 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000471 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000472 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000473
474 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000475}
476
477void
478Target::DisableAllBreakpoints (bool internal_also)
479{
Greg Claytone005f2c2010-11-06 01:53:30 +0000480 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000481 if (log)
482 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
483
484 m_breakpoint_list.SetEnabledAll (false);
485 if (internal_also)
486 m_internal_breakpoint_list.SetEnabledAll (false);
487}
488
489void
490Target::EnableAllBreakpoints (bool internal_also)
491{
Greg Claytone005f2c2010-11-06 01:53:30 +0000492 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000493 if (log)
494 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
495
496 m_breakpoint_list.SetEnabledAll (true);
497 if (internal_also)
498 m_internal_breakpoint_list.SetEnabledAll (true);
499}
500
501bool
502Target::RemoveBreakpointByID (break_id_t break_id)
503{
Greg Claytone005f2c2010-11-06 01:53:30 +0000504 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000505 if (log)
506 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
507
508 if (DisableBreakpointByID (break_id))
509 {
510 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000511 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000512 else
Jim Inghamd1686902010-10-14 23:45:03 +0000513 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000514 if (m_last_created_breakpoint)
515 {
516 if (m_last_created_breakpoint->GetID() == break_id)
517 m_last_created_breakpoint.reset();
518 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000519 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000520 }
Chris Lattner24943d22010-06-08 16:52:24 +0000521 return true;
522 }
523 return false;
524}
525
526bool
527Target::DisableBreakpointByID (break_id_t break_id)
528{
Greg Claytone005f2c2010-11-06 01:53:30 +0000529 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000530 if (log)
531 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
532
533 BreakpointSP bp_sp;
534
535 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
536 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
537 else
538 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
539 if (bp_sp)
540 {
541 bp_sp->SetEnabled (false);
542 return true;
543 }
544 return false;
545}
546
547bool
548Target::EnableBreakpointByID (break_id_t break_id)
549{
Greg Claytone005f2c2010-11-06 01:53:30 +0000550 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000551 if (log)
552 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
553 __FUNCTION__,
554 break_id,
555 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
556
557 BreakpointSP bp_sp;
558
559 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
560 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
561 else
562 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
563
564 if (bp_sp)
565 {
566 bp_sp->SetEnabled (true);
567 return true;
568 }
569 return false;
570}
571
Johnny Chenc86582f2011-09-23 21:21:43 +0000572// The flag 'end_to_end', default to true, signifies that the operation is
573// performed end to end, for both the debugger and the debuggee.
574
575// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
576// for end to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000577bool
Johnny Chenc86582f2011-09-23 21:21:43 +0000578Target::RemoveAllWatchpointLocations (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000579{
580 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
581 if (log)
582 log->Printf ("Target::%s\n", __FUNCTION__);
583
Johnny Chenc86582f2011-09-23 21:21:43 +0000584 if (!end_to_end) {
585 m_watchpoint_location_list.RemoveAll();
586 return true;
587 }
588
589 // Otherwise, it's an end to end operation.
590
Johnny Chenda5a8022011-09-20 23:28:55 +0000591 if (!ProcessIsValid())
592 return false;
593
594 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
595 for (size_t i = 0; i < num_watchpoints; ++i)
596 {
597 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
598 if (!wp_loc_sp)
599 return false;
600
601 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
602 if (rc.Fail())
603 return false;
604 }
605 m_watchpoint_location_list.RemoveAll ();
606 return true; // Success!
607}
608
Johnny Chenc86582f2011-09-23 21:21:43 +0000609// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
610// for end to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000611bool
Johnny Chenc86582f2011-09-23 21:21:43 +0000612Target::DisableAllWatchpointLocations (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000613{
614 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
615 if (log)
616 log->Printf ("Target::%s\n", __FUNCTION__);
617
Johnny Chenc86582f2011-09-23 21:21:43 +0000618 if (!end_to_end) {
619 m_watchpoint_location_list.SetEnabledAll(false);
620 return true;
621 }
622
623 // Otherwise, it's an end to end operation.
624
Johnny Chenda5a8022011-09-20 23:28:55 +0000625 if (!ProcessIsValid())
626 return false;
627
628 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
629 for (size_t i = 0; i < num_watchpoints; ++i)
630 {
631 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
632 if (!wp_loc_sp)
633 return false;
634
635 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
636 if (rc.Fail())
637 return false;
638 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000639 return true; // Success!
640}
641
Johnny Chenc86582f2011-09-23 21:21:43 +0000642// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
643// for end to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000644bool
Johnny Chenc86582f2011-09-23 21:21:43 +0000645Target::EnableAllWatchpointLocations (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000646{
647 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
648 if (log)
649 log->Printf ("Target::%s\n", __FUNCTION__);
650
Johnny Chenc86582f2011-09-23 21:21:43 +0000651 if (!end_to_end) {
652 m_watchpoint_location_list.SetEnabledAll(true);
653 return true;
654 }
655
656 // Otherwise, it's an end to end operation.
657
Johnny Chenda5a8022011-09-20 23:28:55 +0000658 if (!ProcessIsValid())
659 return false;
660
661 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
662 for (size_t i = 0; i < num_watchpoints; ++i)
663 {
664 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
665 if (!wp_loc_sp)
666 return false;
667
668 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
669 if (rc.Fail())
670 return false;
671 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000672 return true; // Success!
673}
674
Johnny Chene14cf4e2011-10-05 21:35:46 +0000675// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
676// during these operations.
677bool
678Target::IgnoreAllWatchpointLocations (uint32_t ignore_count)
679{
680 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
681 if (log)
682 log->Printf ("Target::%s\n", __FUNCTION__);
683
684 if (!ProcessIsValid())
685 return false;
686
687 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
688 for (size_t i = 0; i < num_watchpoints; ++i)
689 {
690 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
691 if (!wp_loc_sp)
692 return false;
693
694 wp_loc_sp->SetIgnoreCount(ignore_count);
695 }
696 return true; // Success!
697}
698
Johnny Chenc86582f2011-09-23 21:21:43 +0000699// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000700bool
701Target::DisableWatchpointLocationByID (lldb::watch_id_t watch_id)
702{
703 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
704 if (log)
705 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
706
707 if (!ProcessIsValid())
708 return false;
709
710 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
711 if (wp_loc_sp)
712 {
713 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000714 if (rc.Success())
715 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000716
Johnny Chen01acfa72011-09-22 18:04:58 +0000717 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000718 }
719 return false;
720}
721
Johnny Chenc86582f2011-09-23 21:21:43 +0000722// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000723bool
724Target::EnableWatchpointLocationByID (lldb::watch_id_t watch_id)
725{
726 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
727 if (log)
728 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
729
730 if (!ProcessIsValid())
731 return false;
732
733 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
734 if (wp_loc_sp)
735 {
736 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000737 if (rc.Success())
738 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000739
Johnny Chen01acfa72011-09-22 18:04:58 +0000740 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000741 }
742 return false;
743}
744
Johnny Chenc86582f2011-09-23 21:21:43 +0000745// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000746bool
747Target::RemoveWatchpointLocationByID (lldb::watch_id_t watch_id)
748{
749 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
750 if (log)
751 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
752
753 if (DisableWatchpointLocationByID (watch_id))
754 {
755 m_watchpoint_location_list.Remove(watch_id);
756 return true;
757 }
758 return false;
759}
760
Johnny Chene14cf4e2011-10-05 21:35:46 +0000761// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
762bool
763Target::IgnoreWatchpointLocationByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
764{
765 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
766 if (log)
767 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
768
769 if (!ProcessIsValid())
770 return false;
771
772 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
773 if (wp_loc_sp)
774 {
775 wp_loc_sp->SetIgnoreCount(ignore_count);
776 return true;
777 }
778 return false;
779}
780
Chris Lattner24943d22010-06-08 16:52:24 +0000781ModuleSP
782Target::GetExecutableModule ()
783{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000784 return m_images.GetModuleAtIndex(0);
785}
786
787Module*
788Target::GetExecutableModulePointer ()
789{
790 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000791}
792
793void
794Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
795{
796 m_images.Clear();
797 m_scratch_ast_context_ap.reset();
798
799 if (executable_sp.get())
800 {
801 Timer scoped_timer (__PRETTY_FUNCTION__,
802 "Target::SetExecutableModule (executable = '%s/%s')",
803 executable_sp->GetFileSpec().GetDirectory().AsCString(),
804 executable_sp->GetFileSpec().GetFilename().AsCString());
805
806 m_images.Append(executable_sp); // The first image is our exectuable file
807
Jim Ingham7508e732010-08-09 23:31:02 +0000808 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000809 if (!m_arch.IsValid())
810 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000811
Chris Lattner24943d22010-06-08 16:52:24 +0000812 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000813 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000814 // Let's find the file & line for main and set the default source file from there.
815 if (!m_source_manager.DefaultFileAndLineSet())
816 {
817 SymbolContextList sc_list;
818 uint32_t num_matches;
819 ConstString main_name("main");
820 bool symbols_okay = false; // Force it to be a debug symbol.
821 bool append = false;
822 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
823 for (uint32_t idx = 0; idx < num_matches; idx++)
824 {
825 SymbolContext sc;
826 sc_list.GetContextAtIndex(idx, sc);
827 if (sc.line_entry.file)
828 {
829 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
830 break;
831 }
832 }
833 }
Chris Lattner24943d22010-06-08 16:52:24 +0000834
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000835 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000836 {
837 executable_objfile->GetDependentModules(dependent_files);
838 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
839 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000840 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
841 FileSpec platform_dependent_file_spec;
842 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000843 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000844 else
845 platform_dependent_file_spec = dependent_file_spec;
846
847 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000848 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000849 if (image_module_sp.get())
850 {
Chris Lattner24943d22010-06-08 16:52:24 +0000851 ObjectFile *objfile = image_module_sp->GetObjectFile();
852 if (objfile)
853 objfile->GetDependentModules(dependent_files);
854 }
855 }
856 }
857
Chris Lattner24943d22010-06-08 16:52:24 +0000858 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000859
860 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000861}
862
863
Jim Ingham7508e732010-08-09 23:31:02 +0000864bool
865Target::SetArchitecture (const ArchSpec &arch_spec)
866{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000867 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000868 {
869 // If we're setting the architecture to our current architecture, we
870 // don't need to do anything.
871 return true;
872 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000873 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000874 {
875 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000876 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000877 return true;
878 }
879 else
880 {
881 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000882 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000883 ModuleSP executable_sp = GetExecutableModule ();
884 m_images.Clear();
885 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000886 // Need to do something about unsetting breakpoints.
887
888 if (executable_sp)
889 {
890 FileSpec exec_file_spec = executable_sp->GetFileSpec();
891 Error error = ModuleList::GetSharedModule(exec_file_spec,
892 arch_spec,
893 NULL,
894 NULL,
895 0,
896 executable_sp,
897 NULL,
898 NULL);
899
900 if (!error.Fail() && executable_sp)
901 {
902 SetExecutableModule (executable_sp, true);
903 return true;
904 }
905 else
906 {
907 return false;
908 }
909 }
910 else
911 {
912 return false;
913 }
914 }
915}
Chris Lattner24943d22010-06-08 16:52:24 +0000916
Chris Lattner24943d22010-06-08 16:52:24 +0000917void
918Target::ModuleAdded (ModuleSP &module_sp)
919{
920 // A module is being added to this target for the first time
921 ModuleList module_list;
922 module_list.Append(module_sp);
923 ModulesDidLoad (module_list);
924}
925
926void
927Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
928{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000929 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000930 ModuleList module_list;
931 module_list.Append (old_module_sp);
932 ModulesDidUnload (module_list);
933 module_list.Clear ();
934 module_list.Append (new_module_sp);
935 ModulesDidLoad (module_list);
936}
937
938void
939Target::ModulesDidLoad (ModuleList &module_list)
940{
941 m_breakpoint_list.UpdateBreakpoints (module_list, true);
942 // TODO: make event data that packages up the module_list
943 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
944}
945
946void
947Target::ModulesDidUnload (ModuleList &module_list)
948{
949 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000950
951 // Remove the images from the target image list
952 m_images.Remove(module_list);
953
Chris Lattner24943d22010-06-08 16:52:24 +0000954 // TODO: make event data that packages up the module_list
955 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
956}
957
958size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000959Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
960{
961 const Section *section = addr.GetSection();
962 if (section && section->GetModule())
963 {
964 ObjectFile *objfile = section->GetModule()->GetObjectFile();
965 if (objfile)
966 {
967 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
968 addr.GetOffset(),
969 dst,
970 dst_len);
971 if (bytes_read > 0)
972 return bytes_read;
973 else
974 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
975 }
976 else
977 {
978 error.SetErrorString("address isn't from a object file");
979 }
980 }
981 else
982 {
983 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
984 }
985 return 0;
986}
987
988size_t
Enrico Granata91544802011-09-06 19:20:51 +0000989Target::ReadMemory (const Address& addr,
990 bool prefer_file_cache,
991 void *dst,
992 size_t dst_len,
993 Error &error,
994 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000995{
Chris Lattner24943d22010-06-08 16:52:24 +0000996 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000997
Enrico Granata91544802011-09-06 19:20:51 +0000998 // if we end up reading this from process memory, we will fill this
999 // with the actual load address
1000 if (load_addr_ptr)
1001 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1002
Greg Clayton26100dc2011-01-07 01:57:07 +00001003 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +00001004
1005 addr_t load_addr = LLDB_INVALID_ADDRESS;
1006 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +00001007 Address resolved_addr;
1008 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001009 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001010 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +00001011 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001012 // No sections are loaded, so we must assume we are not running
1013 // yet and anything we are given is a file address.
1014 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1015 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001016 }
Greg Clayton70436352010-06-30 23:03:03 +00001017 else
Greg Clayton9b82f862011-07-11 05:12:02 +00001018 {
Greg Clayton7dd98df2011-07-12 17:06:17 +00001019 // We have at least one section loaded. This can be becuase
1020 // we have manually loaded some sections with "target modules load ..."
1021 // or because we have have a live process that has sections loaded
1022 // through the dynamic loader
1023 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1024 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001025 }
Greg Clayton70436352010-06-30 23:03:03 +00001026 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001027 if (!resolved_addr.IsValid())
1028 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001029
Greg Clayton9b82f862011-07-11 05:12:02 +00001030
Greg Clayton26100dc2011-01-07 01:57:07 +00001031 if (prefer_file_cache)
1032 {
1033 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1034 if (bytes_read > 0)
1035 return bytes_read;
1036 }
Greg Clayton70436352010-06-30 23:03:03 +00001037
Johnny Chenda5a8022011-09-20 23:28:55 +00001038 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001039 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001040 if (load_addr == LLDB_INVALID_ADDRESS)
1041 load_addr = resolved_addr.GetLoadAddress (this);
1042
Greg Clayton70436352010-06-30 23:03:03 +00001043 if (load_addr == LLDB_INVALID_ADDRESS)
1044 {
1045 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
1046 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
1047 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001048 resolved_addr.GetFileAddress(),
1049 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001050 else
1051 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
1052 }
1053 else
1054 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001055 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001056 if (bytes_read != dst_len)
1057 {
1058 if (error.Success())
1059 {
1060 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +00001061 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001062 else
Greg Clayton70436352010-06-30 23:03:03 +00001063 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001064 }
1065 }
Greg Clayton70436352010-06-30 23:03:03 +00001066 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001067 {
1068 if (load_addr_ptr)
1069 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001070 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001071 }
Greg Clayton70436352010-06-30 23:03:03 +00001072 // If the address is not section offset we have an address that
1073 // doesn't resolve to any address in any currently loaded shared
1074 // libaries and we failed to read memory so there isn't anything
1075 // more we can do. If it is section offset, we might be able to
1076 // read cached memory from the object file.
1077 if (!resolved_addr.IsSectionOffset())
1078 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001079 }
Chris Lattner24943d22010-06-08 16:52:24 +00001080 }
Greg Clayton70436352010-06-30 23:03:03 +00001081
Greg Clayton9b82f862011-07-11 05:12:02 +00001082 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001083 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001084 // If we didn't already try and read from the object file cache, then
1085 // try it after failing to read from the process.
1086 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001087 }
1088 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001089}
1090
Greg Clayton7dd98df2011-07-12 17:06:17 +00001091size_t
1092Target::ReadScalarIntegerFromMemory (const Address& addr,
1093 bool prefer_file_cache,
1094 uint32_t byte_size,
1095 bool is_signed,
1096 Scalar &scalar,
1097 Error &error)
1098{
1099 uint64_t uval;
1100
1101 if (byte_size <= sizeof(uval))
1102 {
1103 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1104 if (bytes_read == byte_size)
1105 {
1106 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1107 uint32_t offset = 0;
1108 if (byte_size <= 4)
1109 scalar = data.GetMaxU32 (&offset, byte_size);
1110 else
1111 scalar = data.GetMaxU64 (&offset, byte_size);
1112
1113 if (is_signed)
1114 scalar.SignExtend(byte_size * 8);
1115 return bytes_read;
1116 }
1117 }
1118 else
1119 {
1120 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1121 }
1122 return 0;
1123}
1124
1125uint64_t
1126Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1127 bool prefer_file_cache,
1128 size_t integer_byte_size,
1129 uint64_t fail_value,
1130 Error &error)
1131{
1132 Scalar scalar;
1133 if (ReadScalarIntegerFromMemory (addr,
1134 prefer_file_cache,
1135 integer_byte_size,
1136 false,
1137 scalar,
1138 error))
1139 return scalar.ULongLong(fail_value);
1140 return fail_value;
1141}
1142
1143bool
1144Target::ReadPointerFromMemory (const Address& addr,
1145 bool prefer_file_cache,
1146 Error &error,
1147 Address &pointer_addr)
1148{
1149 Scalar scalar;
1150 if (ReadScalarIntegerFromMemory (addr,
1151 prefer_file_cache,
1152 m_arch.GetAddressByteSize(),
1153 false,
1154 scalar,
1155 error))
1156 {
1157 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1158 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1159 {
1160 if (m_section_load_list.IsEmpty())
1161 {
1162 // No sections are loaded, so we must assume we are not running
1163 // yet and anything we are given is a file address.
1164 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1165 }
1166 else
1167 {
1168 // We have at least one section loaded. This can be becuase
1169 // we have manually loaded some sections with "target modules load ..."
1170 // or because we have have a live process that has sections loaded
1171 // through the dynamic loader
1172 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1173 }
1174 // We weren't able to resolve the pointer value, so just return
1175 // an address with no section
1176 if (!pointer_addr.IsValid())
1177 pointer_addr.SetOffset (pointer_vm_addr);
1178 return true;
1179
1180 }
1181 }
1182 return false;
1183}
Chris Lattner24943d22010-06-08 16:52:24 +00001184
1185ModuleSP
1186Target::GetSharedModule
1187(
1188 const FileSpec& file_spec,
1189 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +00001190 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +00001191 const ConstString *object_name,
1192 off_t object_offset,
1193 Error *error_ptr
1194)
1195{
1196 // Don't pass in the UUID so we can tell if we have a stale value in our list
1197 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1198 bool did_create_module = false;
1199 ModuleSP module_sp;
1200
Chris Lattner24943d22010-06-08 16:52:24 +00001201 Error error;
1202
Greg Clayton24bc5d92011-03-30 18:16:51 +00001203 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +00001204 if (m_image_search_paths.GetSize())
1205 {
1206 FileSpec transformed_spec;
1207 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1208 {
1209 transformed_spec.GetFilename() = file_spec.GetFilename();
1210 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1211 }
1212 }
1213
Greg Clayton24bc5d92011-03-30 18:16:51 +00001214 // The platform is responsible for finding and caching an appropriate
1215 // module in the shared module cache.
1216 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001217 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001218 FileSpec platform_file_spec;
1219 error = m_platform_sp->GetSharedModule (file_spec,
1220 arch,
1221 uuid_ptr,
1222 object_name,
1223 object_offset,
1224 module_sp,
1225 &old_module_sp,
1226 &did_create_module);
1227 }
1228 else
1229 {
1230 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +00001231 }
1232
Greg Clayton24bc5d92011-03-30 18:16:51 +00001233 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +00001234 if (module_sp)
1235 {
1236 m_images.Append (module_sp);
1237 if (did_create_module)
1238 {
1239 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1240 ModuleUpdated(old_module_sp, module_sp);
1241 else
1242 ModuleAdded(module_sp);
1243 }
1244 }
1245 if (error_ptr)
1246 *error_ptr = error;
1247 return module_sp;
1248}
1249
1250
1251Target *
1252Target::CalculateTarget ()
1253{
1254 return this;
1255}
1256
1257Process *
1258Target::CalculateProcess ()
1259{
1260 return NULL;
1261}
1262
1263Thread *
1264Target::CalculateThread ()
1265{
1266 return NULL;
1267}
1268
1269StackFrame *
1270Target::CalculateStackFrame ()
1271{
1272 return NULL;
1273}
1274
1275void
Greg Claytona830adb2010-10-04 01:05:56 +00001276Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001277{
Greg Clayton567e7f32011-09-22 04:58:26 +00001278 exe_ctx.Clear();
1279 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001280}
1281
1282PathMappingList &
1283Target::GetImageSearchPathList ()
1284{
1285 return m_image_search_paths;
1286}
1287
1288void
1289Target::ImageSearchPathsChanged
1290(
1291 const PathMappingList &path_list,
1292 void *baton
1293)
1294{
1295 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001296 ModuleSP exe_module_sp (target->GetExecutableModule());
1297 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001298 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001299 target->m_images.Clear();
1300 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001301 }
1302}
1303
1304ClangASTContext *
1305Target::GetScratchClangASTContext()
1306{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001307 // Now see if we know the target triple, and if so, create our scratch AST context:
1308 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1309 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +00001310 return m_scratch_ast_context_ap.get();
1311}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001312
Greg Clayton990de7b2010-11-18 23:32:35 +00001313void
Caroline Tice2a456812011-03-10 22:14:10 +00001314Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001315{
Greg Clayton990de7b2010-11-18 23:32:35 +00001316 UserSettingsControllerSP &usc = GetSettingsController();
1317 usc.reset (new SettingsController);
1318 UserSettingsController::InitializeSettingsController (usc,
1319 SettingsController::global_settings_table,
1320 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001321
1322 // Now call SettingsInitialize() on each 'child' setting of Target
1323 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001324}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001325
Greg Clayton990de7b2010-11-18 23:32:35 +00001326void
Caroline Tice2a456812011-03-10 22:14:10 +00001327Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001328{
Caroline Tice2a456812011-03-10 22:14:10 +00001329
1330 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1331
1332 Process::SettingsTerminate ();
1333
1334 // Now terminate Target Settings.
1335
Greg Clayton990de7b2010-11-18 23:32:35 +00001336 UserSettingsControllerSP &usc = GetSettingsController();
1337 UserSettingsController::FinalizeSettingsController (usc);
1338 usc.reset();
1339}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001340
Greg Clayton990de7b2010-11-18 23:32:35 +00001341UserSettingsControllerSP &
1342Target::GetSettingsController ()
1343{
1344 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001345 return g_settings_controller;
1346}
1347
1348ArchSpec
1349Target::GetDefaultArchitecture ()
1350{
Greg Clayton940b1032011-02-23 00:35:02 +00001351 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1352
1353 if (settings_controller_sp)
1354 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1355 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001356}
1357
1358void
Greg Clayton940b1032011-02-23 00:35:02 +00001359Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001360{
Greg Clayton940b1032011-02-23 00:35:02 +00001361 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1362
1363 if (settings_controller_sp)
1364 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001365}
1366
Greg Claytona830adb2010-10-04 01:05:56 +00001367Target *
1368Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1369{
1370 // The target can either exist in the "process" of ExecutionContext, or in
1371 // the "target_sp" member of SymbolContext. This accessor helper function
1372 // will get the target from one of these locations.
1373
1374 Target *target = NULL;
1375 if (sc_ptr != NULL)
1376 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001377 if (target == NULL && exe_ctx_ptr)
1378 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001379 return target;
1380}
1381
1382
Caroline Tice1ebef442010-09-27 00:30:10 +00001383void
1384Target::UpdateInstanceName ()
1385{
1386 StreamString sstr;
1387
Greg Clayton5beb99d2011-08-11 02:48:45 +00001388 Module *exe_module = GetExecutableModulePointer();
1389 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001390 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001391 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001392 exe_module->GetFileSpec().GetFilename().AsCString(),
1393 exe_module->GetArchitecture().GetArchitectureName());
1394 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001395 }
1396}
1397
Sean Callanan77e93942010-10-29 00:29:03 +00001398const char *
1399Target::GetExpressionPrefixContentsAsCString ()
1400{
Greg Claytonff44ab42011-04-23 02:04:55 +00001401 if (m_expr_prefix_contents_sp)
1402 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1403 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001404}
1405
Greg Clayton427f2902010-12-14 02:59:59 +00001406ExecutionResults
1407Target::EvaluateExpression
1408(
1409 const char *expr_cstr,
1410 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001411 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton427f2902010-12-14 02:59:59 +00001412 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001413 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001414 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001415 lldb::ValueObjectSP &result_valobj_sp
1416)
1417{
1418 ExecutionResults execution_results = eExecutionSetupError;
1419
1420 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001421
1422 // We shouldn't run stop hooks in expressions.
1423 // Be sure to reset this if you return anywhere within this function.
1424 bool old_suppress_value = m_suppress_stop_hooks;
1425 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001426
1427 ExecutionContext exe_ctx;
1428 if (frame)
1429 {
1430 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001431 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001432 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001433 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1434 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001435 lldb::VariableSP var_sp;
1436 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1437 use_dynamic,
1438 expr_path_options,
1439 var_sp,
1440 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001441 }
1442 else if (m_process_sp)
1443 {
1444 m_process_sp->CalculateExecutionContext(exe_ctx);
1445 }
1446 else
1447 {
1448 CalculateExecutionContext(exe_ctx);
1449 }
1450
1451 if (result_valobj_sp)
1452 {
1453 execution_results = eExecutionCompleted;
1454 // We got a result from the frame variable expression path above...
1455 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1456
1457 lldb::ValueObjectSP const_valobj_sp;
1458
1459 // Check in case our value is already a constant value
1460 if (result_valobj_sp->GetIsConstant())
1461 {
1462 const_valobj_sp = result_valobj_sp;
1463 const_valobj_sp->SetName (persistent_variable_name);
1464 }
1465 else
Jim Inghame41494a2011-04-16 00:01:13 +00001466 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001467 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001468 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001469 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001470 if (dynamic_sp)
1471 result_valobj_sp = dynamic_sp;
1472 }
1473
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001474 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001475 }
Greg Clayton427f2902010-12-14 02:59:59 +00001476
Sean Callanan6a925532011-01-13 08:53:35 +00001477 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1478
Greg Clayton427f2902010-12-14 02:59:59 +00001479 result_valobj_sp = const_valobj_sp;
1480
Sean Callanan6a925532011-01-13 08:53:35 +00001481 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1482 assert (clang_expr_variable_sp.get());
1483
1484 // Set flags and live data as appropriate
1485
1486 const Value &result_value = live_valobj_sp->GetValue();
1487
1488 switch (result_value.GetValueType())
1489 {
1490 case Value::eValueTypeHostAddress:
1491 case Value::eValueTypeFileAddress:
1492 // we don't do anything with these for now
1493 break;
1494 case Value::eValueTypeScalar:
1495 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1496 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1497 break;
1498 case Value::eValueTypeLoadAddress:
1499 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1500 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1501 break;
1502 }
Greg Clayton427f2902010-12-14 02:59:59 +00001503 }
1504 else
1505 {
1506 // Make sure we aren't just trying to see the value of a persistent
1507 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001508 lldb::ClangExpressionVariableSP persistent_var_sp;
1509 // Only check for persistent variables the expression starts with a '$'
1510 if (expr_cstr[0] == '$')
1511 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1512
Greg Clayton427f2902010-12-14 02:59:59 +00001513 if (persistent_var_sp)
1514 {
1515 result_valobj_sp = persistent_var_sp->GetValueObject ();
1516 execution_results = eExecutionCompleted;
1517 }
1518 else
1519 {
1520 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001521
Greg Clayton427f2902010-12-14 02:59:59 +00001522 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001523 execution_policy,
Sean Callanan6a925532011-01-13 08:53:35 +00001524 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001525 expr_cstr,
1526 prefix,
1527 result_valobj_sp);
1528 }
1529 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001530
1531 m_suppress_stop_hooks = old_suppress_value;
1532
Greg Clayton427f2902010-12-14 02:59:59 +00001533 return execution_results;
1534}
1535
Greg Claytonc0fa5332011-05-22 22:46:53 +00001536lldb::addr_t
1537Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1538{
1539 addr_t code_addr = load_addr;
1540 switch (m_arch.GetMachine())
1541 {
1542 case llvm::Triple::arm:
1543 case llvm::Triple::thumb:
1544 switch (addr_class)
1545 {
1546 case eAddressClassData:
1547 case eAddressClassDebug:
1548 return LLDB_INVALID_ADDRESS;
1549
1550 case eAddressClassUnknown:
1551 case eAddressClassInvalid:
1552 case eAddressClassCode:
1553 case eAddressClassCodeAlternateISA:
1554 case eAddressClassRuntime:
1555 // Check if bit zero it no set?
1556 if ((code_addr & 1ull) == 0)
1557 {
1558 // Bit zero isn't set, check if the address is a multiple of 2?
1559 if (code_addr & 2ull)
1560 {
1561 // The address is a multiple of 2 so it must be thumb, set bit zero
1562 code_addr |= 1ull;
1563 }
1564 else if (addr_class == eAddressClassCodeAlternateISA)
1565 {
1566 // We checked the address and the address claims to be the alternate ISA
1567 // which means thumb, so set bit zero.
1568 code_addr |= 1ull;
1569 }
1570 }
1571 break;
1572 }
1573 break;
1574
1575 default:
1576 break;
1577 }
1578 return code_addr;
1579}
1580
1581lldb::addr_t
1582Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1583{
1584 addr_t opcode_addr = load_addr;
1585 switch (m_arch.GetMachine())
1586 {
1587 case llvm::Triple::arm:
1588 case llvm::Triple::thumb:
1589 switch (addr_class)
1590 {
1591 case eAddressClassData:
1592 case eAddressClassDebug:
1593 return LLDB_INVALID_ADDRESS;
1594
1595 case eAddressClassInvalid:
1596 case eAddressClassUnknown:
1597 case eAddressClassCode:
1598 case eAddressClassCodeAlternateISA:
1599 case eAddressClassRuntime:
1600 opcode_addr &= ~(1ull);
1601 break;
1602 }
1603 break;
1604
1605 default:
1606 break;
1607 }
1608 return opcode_addr;
1609}
1610
Jim Inghamd60d94a2011-03-11 03:53:59 +00001611lldb::user_id_t
1612Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1613{
1614 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1615 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1616 m_stop_hooks[new_uid] = new_hook_sp;
1617 return new_uid;
1618}
1619
1620bool
1621Target::RemoveStopHookByID (lldb::user_id_t user_id)
1622{
1623 size_t num_removed;
1624 num_removed = m_stop_hooks.erase (user_id);
1625 if (num_removed == 0)
1626 return false;
1627 else
1628 return true;
1629}
1630
1631void
1632Target::RemoveAllStopHooks ()
1633{
1634 m_stop_hooks.clear();
1635}
1636
1637Target::StopHookSP
1638Target::GetStopHookByID (lldb::user_id_t user_id)
1639{
1640 StopHookSP found_hook;
1641
1642 StopHookCollection::iterator specified_hook_iter;
1643 specified_hook_iter = m_stop_hooks.find (user_id);
1644 if (specified_hook_iter != m_stop_hooks.end())
1645 found_hook = (*specified_hook_iter).second;
1646 return found_hook;
1647}
1648
1649bool
1650Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1651{
1652 StopHookCollection::iterator specified_hook_iter;
1653 specified_hook_iter = m_stop_hooks.find (user_id);
1654 if (specified_hook_iter == m_stop_hooks.end())
1655 return false;
1656
1657 (*specified_hook_iter).second->SetIsActive (active_state);
1658 return true;
1659}
1660
1661void
1662Target::SetAllStopHooksActiveState (bool active_state)
1663{
1664 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1665 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1666 {
1667 (*pos).second->SetIsActive (active_state);
1668 }
1669}
1670
1671void
1672Target::RunStopHooks ()
1673{
Jim Ingham3613ae12011-05-12 02:06:14 +00001674 if (m_suppress_stop_hooks)
1675 return;
1676
Jim Inghamd60d94a2011-03-11 03:53:59 +00001677 if (!m_process_sp)
1678 return;
1679
1680 if (m_stop_hooks.empty())
1681 return;
1682
1683 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1684
1685 // If there aren't any active stop hooks, don't bother either:
1686 bool any_active_hooks = false;
1687 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1688 {
1689 if ((*pos).second->IsActive())
1690 {
1691 any_active_hooks = true;
1692 break;
1693 }
1694 }
1695 if (!any_active_hooks)
1696 return;
1697
1698 CommandReturnObject result;
1699
1700 std::vector<ExecutionContext> exc_ctx_with_reasons;
1701 std::vector<SymbolContext> sym_ctx_with_reasons;
1702
1703 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1704 size_t num_threads = cur_threadlist.GetSize();
1705 for (size_t i = 0; i < num_threads; i++)
1706 {
1707 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1708 if (cur_thread_sp->ThreadStoppedForAReason())
1709 {
1710 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1711 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1712 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1713 }
1714 }
1715
1716 // If no threads stopped for a reason, don't run the stop-hooks.
1717 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1718 if (num_exe_ctx == 0)
1719 return;
1720
Jim Inghame5ed8e92011-06-02 23:58:26 +00001721 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1722 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001723
1724 bool keep_going = true;
1725 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001726 bool print_hook_header;
1727 bool print_thread_header;
1728
1729 if (num_exe_ctx == 1)
1730 print_thread_header = false;
1731 else
1732 print_thread_header = true;
1733
1734 if (m_stop_hooks.size() == 1)
1735 print_hook_header = false;
1736 else
1737 print_hook_header = true;
1738
Jim Inghamd60d94a2011-03-11 03:53:59 +00001739 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1740 {
1741 // result.Clear();
1742 StopHookSP cur_hook_sp = (*pos).second;
1743 if (!cur_hook_sp->IsActive())
1744 continue;
1745
1746 bool any_thread_matched = false;
1747 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1748 {
1749 if ((cur_hook_sp->GetSpecifier () == NULL
1750 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1751 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Clayton567e7f32011-09-22 04:58:26 +00001752 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001753 {
1754 if (!hooks_ran)
1755 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001756 hooks_ran = true;
1757 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001758 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001759 {
1760 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1761 any_thread_matched = true;
1762 }
1763
Jim Inghamc54840c2011-03-22 01:47:27 +00001764 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001765 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001766
1767 bool stop_on_continue = true;
1768 bool stop_on_error = true;
1769 bool echo_commands = false;
1770 bool print_results = true;
1771 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001772 &exc_ctx_with_reasons[i],
1773 stop_on_continue,
1774 stop_on_error,
1775 echo_commands,
1776 print_results,
1777 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001778
1779 // If the command started the target going again, we should bag out of
1780 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001781 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1782 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001783 {
1784 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1785 keep_going = false;
1786 }
1787 }
1788 }
1789 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00001790
Caroline Tice4a348082011-05-02 20:41:46 +00001791 result.GetImmediateOutputStream()->Flush();
1792 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001793}
1794
Greg Claytonbbea1332011-07-08 00:48:09 +00001795bool
1796Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1797{
1798 bool changed = false;
1799 if (module)
1800 {
1801 ObjectFile *object_file = module->GetObjectFile();
1802 if (object_file)
1803 {
1804 SectionList *section_list = object_file->GetSectionList ();
1805 if (section_list)
1806 {
1807 // All sections listed in the dyld image info structure will all
1808 // either be fixed up already, or they will all be off by a single
1809 // slide amount that is determined by finding the first segment
1810 // that is at file offset zero which also has bytes (a file size
1811 // that is greater than zero) in the object file.
1812
1813 // Determine the slide amount (if any)
1814 const size_t num_sections = section_list->GetSize();
1815 size_t sect_idx = 0;
1816 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1817 {
1818 // Iterate through the object file sections to find the
1819 // first section that starts of file offset zero and that
1820 // has bytes in the file...
1821 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1822 if (section)
1823 {
1824 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1825 changed = true;
1826 }
1827 }
1828 }
1829 }
1830 }
1831 return changed;
1832}
1833
1834
Jim Inghamd60d94a2011-03-11 03:53:59 +00001835//--------------------------------------------------------------
1836// class Target::StopHook
1837//--------------------------------------------------------------
1838
1839
1840Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1841 UserID (uid),
1842 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001843 m_commands (),
1844 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001845 m_thread_spec_ap(NULL),
1846 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001847{
1848}
1849
1850Target::StopHook::StopHook (const StopHook &rhs) :
1851 UserID (rhs.GetID()),
1852 m_target_sp (rhs.m_target_sp),
1853 m_commands (rhs.m_commands),
1854 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001855 m_thread_spec_ap (NULL),
1856 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001857{
1858 if (rhs.m_thread_spec_ap.get() != NULL)
1859 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1860}
1861
1862
1863Target::StopHook::~StopHook ()
1864{
1865}
1866
1867void
1868Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1869{
1870 m_thread_spec_ap.reset (specifier);
1871}
1872
1873
1874void
1875Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1876{
1877 int indent_level = s->GetIndentLevel();
1878
1879 s->SetIndentLevel(indent_level + 2);
1880
1881 s->Printf ("Hook: %d\n", GetID());
1882 if (m_active)
1883 s->Indent ("State: enabled\n");
1884 else
1885 s->Indent ("State: disabled\n");
1886
1887 if (m_specifier_sp)
1888 {
1889 s->Indent();
1890 s->PutCString ("Specifier:\n");
1891 s->SetIndentLevel (indent_level + 4);
1892 m_specifier_sp->GetDescription (s, level);
1893 s->SetIndentLevel (indent_level + 2);
1894 }
1895
1896 if (m_thread_spec_ap.get() != NULL)
1897 {
1898 StreamString tmp;
1899 s->Indent("Thread:\n");
1900 m_thread_spec_ap->GetDescription (&tmp, level);
1901 s->SetIndentLevel (indent_level + 4);
1902 s->Indent (tmp.GetData());
1903 s->PutCString ("\n");
1904 s->SetIndentLevel (indent_level + 2);
1905 }
1906
1907 s->Indent ("Commands: \n");
1908 s->SetIndentLevel (indent_level + 4);
1909 uint32_t num_commands = m_commands.GetSize();
1910 for (uint32_t i = 0; i < num_commands; i++)
1911 {
1912 s->Indent(m_commands.GetStringAtIndex(i));
1913 s->PutCString ("\n");
1914 }
1915 s->SetIndentLevel (indent_level);
1916}
1917
1918
Caroline Tice5bc8c972010-09-20 20:44:43 +00001919//--------------------------------------------------------------
1920// class Target::SettingsController
1921//--------------------------------------------------------------
1922
1923Target::SettingsController::SettingsController () :
1924 UserSettingsController ("target", Debugger::GetSettingsController()),
1925 m_default_architecture ()
1926{
1927 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1928 InstanceSettings::GetDefaultName().AsCString()));
1929}
1930
1931Target::SettingsController::~SettingsController ()
1932{
1933}
1934
1935lldb::InstanceSettingsSP
1936Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1937{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001938 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1939 false,
1940 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001941 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1942 return new_settings_sp;
1943}
1944
Caroline Tice5bc8c972010-09-20 20:44:43 +00001945
Jim Inghame41494a2011-04-16 00:01:13 +00001946#define TSC_DEFAULT_ARCH "default-arch"
1947#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001948#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001949#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001950#define TSC_SOURCE_MAP "source-map"
Enrico Granata018921d2011-08-12 02:00:06 +00001951#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata91544802011-09-06 19:20:51 +00001952#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytond284b662011-02-18 01:44:25 +00001953
1954
1955static const ConstString &
1956GetSettingNameForDefaultArch ()
1957{
1958 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001959 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001960}
1961
Greg Claytond284b662011-02-18 01:44:25 +00001962static const ConstString &
1963GetSettingNameForExpressionPrefix ()
1964{
1965 static ConstString g_const_string (TSC_EXPR_PREFIX);
1966 return g_const_string;
1967}
1968
1969static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001970GetSettingNameForPreferDynamicValue ()
1971{
1972 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1973 return g_const_string;
1974}
1975
Greg Claytonff44ab42011-04-23 02:04:55 +00001976static const ConstString &
1977GetSettingNameForSourcePathMap ()
1978{
1979 static ConstString g_const_string (TSC_SOURCE_MAP);
1980 return g_const_string;
1981}
Greg Claytond284b662011-02-18 01:44:25 +00001982
Greg Clayton17cd9952011-04-22 03:55:06 +00001983static const ConstString &
1984GetSettingNameForSkipPrologue ()
1985{
1986 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1987 return g_const_string;
1988}
1989
Enrico Granata018921d2011-08-12 02:00:06 +00001990static const ConstString &
1991GetSettingNameForMaxChildren ()
1992{
1993 static ConstString g_const_string (TSC_MAX_CHILDREN);
1994 return g_const_string;
1995}
Greg Clayton17cd9952011-04-22 03:55:06 +00001996
Enrico Granata91544802011-09-06 19:20:51 +00001997static const ConstString &
1998GetSettingNameForMaxStringSummaryLength ()
1999{
2000 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
2001 return g_const_string;
2002}
Greg Clayton17cd9952011-04-22 03:55:06 +00002003
Caroline Tice5bc8c972010-09-20 20:44:43 +00002004bool
2005Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
2006 const char *index_value,
2007 const char *value,
2008 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002009 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002010 Error&err)
2011{
Greg Claytond284b662011-02-18 01:44:25 +00002012 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002013 {
Greg Claytonf15996e2011-04-07 22:46:35 +00002014 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00002015 if (!m_default_architecture.IsValid())
2016 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002017 }
2018 return true;
2019}
2020
2021
2022bool
2023Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2024 StringList &value,
2025 Error &err)
2026{
Greg Claytond284b662011-02-18 01:44:25 +00002027 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002028 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002029 // If the arch is invalid (the default), don't show a string for it
2030 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002031 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002032 return true;
2033 }
2034 else
2035 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2036
2037 return false;
2038}
2039
2040//--------------------------------------------------------------
2041// class TargetInstanceSettings
2042//--------------------------------------------------------------
2043
Greg Clayton638351a2010-12-04 00:10:17 +00002044TargetInstanceSettings::TargetInstanceSettings
2045(
2046 UserSettingsController &owner,
2047 bool live_instance,
2048 const char *name
2049) :
Greg Claytond284b662011-02-18 01:44:25 +00002050 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002051 m_expr_prefix_file (),
2052 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002053 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00002054 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002055 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00002056 m_max_children_display(256),
2057 m_max_strlen_length(1024)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002058{
2059 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2060 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2061 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2062 // This is true for CreateInstanceName() too.
2063
2064 if (GetInstanceName () == InstanceSettings::InvalidName())
2065 {
2066 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2067 m_owner.RegisterInstanceSettings (this);
2068 }
2069
2070 if (live_instance)
2071 {
2072 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2073 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002074 }
2075}
2076
2077TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00002078 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
2079 m_expr_prefix_file (rhs.m_expr_prefix_file),
2080 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
2081 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2082 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002083 m_source_map (rhs.m_source_map),
Enrico Granata91544802011-09-06 19:20:51 +00002084 m_max_children_display(rhs.m_max_children_display),
2085 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002086{
2087 if (m_instance_name != InstanceSettings::GetDefaultName())
2088 {
2089 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2090 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002091 }
2092}
2093
2094TargetInstanceSettings::~TargetInstanceSettings ()
2095{
2096}
2097
2098TargetInstanceSettings&
2099TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2100{
2101 if (this != &rhs)
2102 {
2103 }
2104
2105 return *this;
2106}
2107
Caroline Tice5bc8c972010-09-20 20:44:43 +00002108void
2109TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2110 const char *index_value,
2111 const char *value,
2112 const ConstString &instance_name,
2113 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002114 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002115 Error &err,
2116 bool pending)
2117{
Greg Claytond284b662011-02-18 01:44:25 +00002118 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002119 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002120 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2121 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002122 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002123 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002124 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002125 default:
2126 break;
2127 case eVarSetOperationAssign:
2128 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002129 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002130 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2131 {
2132 err.SetErrorToGenericError ();
2133 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
2134 return;
2135 }
2136
2137 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
2138
2139 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
2140 {
2141 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
2142 m_expr_prefix_contents_sp.reset();
2143 }
Sean Callanan77e93942010-10-29 00:29:03 +00002144 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002145 break;
2146 case eVarSetOperationClear:
2147 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00002148 }
Sean Callanan77e93942010-10-29 00:29:03 +00002149 }
2150 }
Jim Inghame41494a2011-04-16 00:01:13 +00002151 else if (var_name == GetSettingNameForPreferDynamicValue())
2152 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002153 int new_value;
2154 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2155 if (err.Success())
2156 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002157 }
2158 else if (var_name == GetSettingNameForSkipPrologue())
2159 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002160 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2161 }
Enrico Granata018921d2011-08-12 02:00:06 +00002162 else if (var_name == GetSettingNameForMaxChildren())
2163 {
2164 bool ok;
2165 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2166 if (ok)
2167 m_max_children_display = new_value;
2168 }
Enrico Granata91544802011-09-06 19:20:51 +00002169 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2170 {
2171 bool ok;
2172 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2173 if (ok)
2174 m_max_strlen_length = new_value;
2175 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002176 else if (var_name == GetSettingNameForSourcePathMap ())
2177 {
2178 switch (op)
2179 {
2180 case eVarSetOperationReplace:
2181 case eVarSetOperationInsertBefore:
2182 case eVarSetOperationInsertAfter:
2183 case eVarSetOperationRemove:
2184 default:
2185 break;
2186 case eVarSetOperationAssign:
2187 m_source_map.Clear(true);
2188 // Fall through to append....
2189 case eVarSetOperationAppend:
2190 {
2191 Args args(value);
2192 const uint32_t argc = args.GetArgumentCount();
2193 if (argc & 1 || argc == 0)
2194 {
2195 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2196 }
2197 else
2198 {
2199 char resolved_new_path[PATH_MAX];
2200 FileSpec file_spec;
2201 const char *old_path;
2202 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2203 {
2204 const char *new_path = args.GetArgumentAtIndex(idx+1);
2205 assert (new_path); // We have an even number of paths, this shouldn't happen!
2206
2207 file_spec.SetFile(new_path, true);
2208 if (file_spec.Exists())
2209 {
2210 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2211 {
2212 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2213 return;
2214 }
2215 }
2216 else
2217 {
2218 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2219 return;
2220 }
2221 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2222 }
2223 }
2224 }
2225 break;
2226
2227 case eVarSetOperationClear:
2228 m_source_map.Clear(true);
2229 break;
2230 }
Jim Inghame41494a2011-04-16 00:01:13 +00002231 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002232}
2233
2234void
Greg Claytond284b662011-02-18 01:44:25 +00002235TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002236{
Sean Callanan77e93942010-10-29 00:29:03 +00002237 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2238
2239 if (!new_settings_ptr)
2240 return;
2241
Greg Claytonff44ab42011-04-23 02:04:55 +00002242 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
2243 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
2244 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
2245 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata018921d2011-08-12 02:00:06 +00002246 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata91544802011-09-06 19:20:51 +00002247 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002248}
2249
Caroline Ticebcb5b452010-09-20 21:37:42 +00002250bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002251TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2252 const ConstString &var_name,
2253 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002254 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002255{
Greg Claytond284b662011-02-18 01:44:25 +00002256 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002257 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002258 char path[PATH_MAX];
2259 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2260 if (path_len > 0)
2261 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002262 }
Jim Inghame41494a2011-04-16 00:01:13 +00002263 else if (var_name == GetSettingNameForPreferDynamicValue())
2264 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002265 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002266 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002267 else if (var_name == GetSettingNameForSkipPrologue())
2268 {
2269 if (m_skip_prologue)
2270 value.AppendString ("true");
2271 else
2272 value.AppendString ("false");
2273 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002274 else if (var_name == GetSettingNameForSourcePathMap ())
2275 {
2276 }
Enrico Granata018921d2011-08-12 02:00:06 +00002277 else if (var_name == GetSettingNameForMaxChildren())
2278 {
2279 StreamString count_str;
2280 count_str.Printf ("%d", m_max_children_display);
2281 value.AppendString (count_str.GetData());
2282 }
Enrico Granata91544802011-09-06 19:20:51 +00002283 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2284 {
2285 StreamString count_str;
2286 count_str.Printf ("%d", m_max_strlen_length);
2287 value.AppendString (count_str.GetData());
2288 }
Sean Callanan77e93942010-10-29 00:29:03 +00002289 else
2290 {
2291 if (err)
2292 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2293 return false;
2294 }
2295
2296 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002297}
2298
2299const ConstString
2300TargetInstanceSettings::CreateInstanceName ()
2301{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002302 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002303 static int instance_count = 1;
2304
Caroline Tice5bc8c972010-09-20 20:44:43 +00002305 sstr.Printf ("target_%d", instance_count);
2306 ++instance_count;
2307
2308 const ConstString ret_val (sstr.GetData());
2309 return ret_val;
2310}
2311
2312//--------------------------------------------------
2313// Target::SettingsController Variable Tables
2314//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002315OptionEnumValueElement
2316TargetInstanceSettings::g_dynamic_value_types[] =
2317{
Greg Clayton577fbc32011-05-30 00:39:48 +00002318{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2319{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2320{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002321{ 0, NULL, NULL }
2322};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002323
2324SettingEntry
2325Target::SettingsController::global_settings_table[] =
2326{
Greg Claytond284b662011-02-18 01:44:25 +00002327 // var-name var-type default enum init'd hidden help-text
2328 // ================= ================== =========== ==== ====== ====== =========================================================================
2329 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2330 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2331};
2332
Caroline Tice5bc8c972010-09-20 20:44:43 +00002333SettingEntry
2334Target::SettingsController::instance_settings_table[] =
2335{
Enrico Granata91544802011-09-06 19:20:51 +00002336 // var-name var-type default enum init'd hidden help-text
2337 // ================= ================== =============== ======================= ====== ====== =========================================================================
2338 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2339 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2340 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2341 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2342 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2343 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2344 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002345};