blob: dbfee315cbdb29b32376e67d28fa0097187e80ed [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 Chenecd4feb2011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.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 Chenecd4feb2011-10-14 00:42:25 +000058 m_watchpoint_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 Chenecd4feb2011-10-14 00:42:25 +0000129 // Disable watchpoints just on the debugger side.
130 DisableAllWatchpoints(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 Chenecd4feb2011-10-14 00:42:25 +0000170 m_last_created_watchpoint.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 Chenecd4feb2011-10-14 00:42:25 +0000400// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chen87ff53b2011-09-14 00:26:03 +0000401// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000402WatchpointSP
403Target::CreateWatchpoint(lldb::addr_t addr, size_t size, uint32_t type)
Johnny Chen34bbf852011-09-12 23:38:44 +0000404{
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 Chenecd4feb2011-10-14 00:42:25 +0000410 WatchpointSP wp_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000411 if (!ProcessIsValid())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000412 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000413 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000414 return wp_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000415
Johnny Chenecd4feb2011-10-14 00:42:25 +0000416 // Currently we only support one watchpoint per address, with total number
417 // of watchpoints limited by the hardware which the inferior is running on.
418 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen69b6ec82011-09-13 23:29:31 +0000419 if (matched_sp)
420 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000421 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000422 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000423 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
424 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chenecd4feb2011-10-14 00:42:25 +0000425 // Return the existing watchpoint if both size and type match.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000426 if (size == old_size && type == old_type) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000427 wp_sp = matched_sp;
428 wp_sp->SetEnabled(false);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000429 } else {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000430 // Nil the matched watchpoint; we will be creating a new one.
Johnny Chen22a56cc2011-09-14 22:20:15 +0000431 m_process_sp->DisableWatchpoint(matched_sp.get());
Johnny Chenecd4feb2011-10-14 00:42:25 +0000432 m_watchpoint_list.Remove(matched_sp->GetID());
Johnny Chen22a56cc2011-09-14 22:20:15 +0000433 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000434 }
435
Johnny Chenecd4feb2011-10-14 00:42:25 +0000436 if (!wp_sp) {
437 Watchpoint *new_wp = new Watchpoint(addr, size);
438 if (!new_wp) {
439 printf("Watchpoint ctor failed, out of memory?\n");
440 return wp_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000441 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000442 new_wp->SetWatchpointType(type);
443 new_wp->SetTarget(this);
444 wp_sp.reset(new_wp);
445 m_watchpoint_list.Add(wp_sp);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000446 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000447
Johnny Chenecd4feb2011-10-14 00:42:25 +0000448 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000449 if (log)
450 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
451 __FUNCTION__,
452 rc.Success() ? "succeeded" : "failed",
Johnny Chenecd4feb2011-10-14 00:42:25 +0000453 wp_sp->GetID());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000454
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000455 if (rc.Fail())
Johnny Chenecd4feb2011-10-14 00:42:25 +0000456 wp_sp.reset();
Johnny Chen5eb54bb2011-09-27 20:29:45 +0000457 else
Johnny Chenecd4feb2011-10-14 00:42:25 +0000458 m_last_created_watchpoint = wp_sp;
459 return wp_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000460}
461
Chris Lattner24943d22010-06-08 16:52:24 +0000462void
463Target::RemoveAllBreakpoints (bool internal_also)
464{
Greg Claytone005f2c2010-11-06 01:53:30 +0000465 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000466 if (log)
467 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
468
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000469 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000470 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000471 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000472
473 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000474}
475
476void
477Target::DisableAllBreakpoints (bool internal_also)
478{
Greg Claytone005f2c2010-11-06 01:53:30 +0000479 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000480 if (log)
481 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
482
483 m_breakpoint_list.SetEnabledAll (false);
484 if (internal_also)
485 m_internal_breakpoint_list.SetEnabledAll (false);
486}
487
488void
489Target::EnableAllBreakpoints (bool internal_also)
490{
Greg Claytone005f2c2010-11-06 01:53:30 +0000491 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000492 if (log)
493 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
494
495 m_breakpoint_list.SetEnabledAll (true);
496 if (internal_also)
497 m_internal_breakpoint_list.SetEnabledAll (true);
498}
499
500bool
501Target::RemoveBreakpointByID (break_id_t break_id)
502{
Greg Claytone005f2c2010-11-06 01:53:30 +0000503 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000504 if (log)
505 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
506
507 if (DisableBreakpointByID (break_id))
508 {
509 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000510 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000511 else
Jim Inghamd1686902010-10-14 23:45:03 +0000512 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000513 if (m_last_created_breakpoint)
514 {
515 if (m_last_created_breakpoint->GetID() == break_id)
516 m_last_created_breakpoint.reset();
517 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000518 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000519 }
Chris Lattner24943d22010-06-08 16:52:24 +0000520 return true;
521 }
522 return false;
523}
524
525bool
526Target::DisableBreakpointByID (break_id_t break_id)
527{
Greg Claytone005f2c2010-11-06 01:53:30 +0000528 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000529 if (log)
530 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
531
532 BreakpointSP bp_sp;
533
534 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
535 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
536 else
537 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
538 if (bp_sp)
539 {
540 bp_sp->SetEnabled (false);
541 return true;
542 }
543 return false;
544}
545
546bool
547Target::EnableBreakpointByID (break_id_t break_id)
548{
Greg Claytone005f2c2010-11-06 01:53:30 +0000549 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000550 if (log)
551 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
552 __FUNCTION__,
553 break_id,
554 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
555
556 BreakpointSP bp_sp;
557
558 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
559 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
560 else
561 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
562
563 if (bp_sp)
564 {
565 bp_sp->SetEnabled (true);
566 return true;
567 }
568 return false;
569}
570
Johnny Chenc86582f2011-09-23 21:21:43 +0000571// The flag 'end_to_end', default to true, signifies that the operation is
572// performed end to end, for both the debugger and the debuggee.
573
Johnny Chenecd4feb2011-10-14 00:42:25 +0000574// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
575// to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000576bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000577Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000578{
579 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
580 if (log)
581 log->Printf ("Target::%s\n", __FUNCTION__);
582
Johnny Chenc86582f2011-09-23 21:21:43 +0000583 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000584 m_watchpoint_list.RemoveAll();
Johnny Chenc86582f2011-09-23 21:21:43 +0000585 return true;
586 }
587
588 // Otherwise, it's an end to end operation.
589
Johnny Chenda5a8022011-09-20 23:28:55 +0000590 if (!ProcessIsValid())
591 return false;
592
Johnny Chenecd4feb2011-10-14 00:42:25 +0000593 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000594 for (size_t i = 0; i < num_watchpoints; ++i)
595 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000596 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
597 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000598 return false;
599
Johnny Chenecd4feb2011-10-14 00:42:25 +0000600 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000601 if (rc.Fail())
602 return false;
603 }
Johnny Chenecd4feb2011-10-14 00:42:25 +0000604 m_watchpoint_list.RemoveAll ();
Johnny Chenda5a8022011-09-20 23:28:55 +0000605 return true; // Success!
606}
607
Johnny Chenecd4feb2011-10-14 00:42:25 +0000608// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
609// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000610bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000611Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000612{
613 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
614 if (log)
615 log->Printf ("Target::%s\n", __FUNCTION__);
616
Johnny Chenc86582f2011-09-23 21:21:43 +0000617 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000618 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenc86582f2011-09-23 21:21:43 +0000619 return true;
620 }
621
622 // Otherwise, it's an end to end operation.
623
Johnny Chenda5a8022011-09-20 23:28:55 +0000624 if (!ProcessIsValid())
625 return false;
626
Johnny Chenecd4feb2011-10-14 00:42:25 +0000627 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000628 for (size_t i = 0; i < num_watchpoints; ++i)
629 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000630 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
631 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000632 return false;
633
Johnny Chenecd4feb2011-10-14 00:42:25 +0000634 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000635 if (rc.Fail())
636 return false;
637 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000638 return true; // Success!
639}
640
Johnny Chenecd4feb2011-10-14 00:42:25 +0000641// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
642// end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000643bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000644Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000645{
646 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
647 if (log)
648 log->Printf ("Target::%s\n", __FUNCTION__);
649
Johnny Chenc86582f2011-09-23 21:21:43 +0000650 if (!end_to_end) {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000651 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenc86582f2011-09-23 21:21:43 +0000652 return true;
653 }
654
655 // Otherwise, it's an end to end operation.
656
Johnny Chenda5a8022011-09-20 23:28:55 +0000657 if (!ProcessIsValid())
658 return false;
659
Johnny Chenecd4feb2011-10-14 00:42:25 +0000660 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chenda5a8022011-09-20 23:28:55 +0000661 for (size_t i = 0; i < num_watchpoints; ++i)
662 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000663 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
664 if (!wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000665 return false;
666
Johnny Chenecd4feb2011-10-14 00:42:25 +0000667 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenda5a8022011-09-20 23:28:55 +0000668 if (rc.Fail())
669 return false;
670 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000671 return true; // Success!
672}
673
Johnny Chenecd4feb2011-10-14 00:42:25 +0000674// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chene14cf4e2011-10-05 21:35:46 +0000675// during these operations.
676bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000677Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000678{
679 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
680 if (log)
681 log->Printf ("Target::%s\n", __FUNCTION__);
682
683 if (!ProcessIsValid())
684 return false;
685
Johnny Chenecd4feb2011-10-14 00:42:25 +0000686 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000687 for (size_t i = 0; i < num_watchpoints; ++i)
688 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000689 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
690 if (!wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000691 return false;
692
Johnny Chenecd4feb2011-10-14 00:42:25 +0000693 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000694 }
695 return true; // Success!
696}
697
Johnny Chenecd4feb2011-10-14 00:42:25 +0000698// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000699bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000700Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000701{
702 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
703 if (log)
704 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
705
706 if (!ProcessIsValid())
707 return false;
708
Johnny Chenecd4feb2011-10-14 00:42:25 +0000709 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
710 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000711 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000712 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000713 if (rc.Success())
714 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000715
Johnny Chen01acfa72011-09-22 18:04:58 +0000716 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000717 }
718 return false;
719}
720
Johnny Chenecd4feb2011-10-14 00:42:25 +0000721// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000722bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000723Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000724{
725 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
726 if (log)
727 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
728
729 if (!ProcessIsValid())
730 return false;
731
Johnny Chenecd4feb2011-10-14 00:42:25 +0000732 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
733 if (wp_sp)
Johnny Chenda5a8022011-09-20 23:28:55 +0000734 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000735 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000736 if (rc.Success())
737 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000738
Johnny Chen01acfa72011-09-22 18:04:58 +0000739 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000740 }
741 return false;
742}
743
Johnny Chenecd4feb2011-10-14 00:42:25 +0000744// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000745bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000746Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chenda5a8022011-09-20 23:28:55 +0000747{
748 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
749 if (log)
750 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
751
Johnny Chenecd4feb2011-10-14 00:42:25 +0000752 if (DisableWatchpointByID (watch_id))
Johnny Chenda5a8022011-09-20 23:28:55 +0000753 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000754 m_watchpoint_list.Remove(watch_id);
Johnny Chenda5a8022011-09-20 23:28:55 +0000755 return true;
756 }
757 return false;
758}
759
Johnny Chenecd4feb2011-10-14 00:42:25 +0000760// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chene14cf4e2011-10-05 21:35:46 +0000761bool
Johnny Chenecd4feb2011-10-14 00:42:25 +0000762Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000763{
764 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
765 if (log)
766 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
767
768 if (!ProcessIsValid())
769 return false;
770
Johnny Chenecd4feb2011-10-14 00:42:25 +0000771 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
772 if (wp_sp)
Johnny Chene14cf4e2011-10-05 21:35:46 +0000773 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000774 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000775 return true;
776 }
777 return false;
778}
779
Chris Lattner24943d22010-06-08 16:52:24 +0000780ModuleSP
781Target::GetExecutableModule ()
782{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000783 return m_images.GetModuleAtIndex(0);
784}
785
786Module*
787Target::GetExecutableModulePointer ()
788{
789 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000790}
791
792void
793Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
794{
795 m_images.Clear();
796 m_scratch_ast_context_ap.reset();
797
798 if (executable_sp.get())
799 {
800 Timer scoped_timer (__PRETTY_FUNCTION__,
801 "Target::SetExecutableModule (executable = '%s/%s')",
802 executable_sp->GetFileSpec().GetDirectory().AsCString(),
803 executable_sp->GetFileSpec().GetFilename().AsCString());
804
805 m_images.Append(executable_sp); // The first image is our exectuable file
806
Jim Ingham7508e732010-08-09 23:31:02 +0000807 // 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 +0000808 if (!m_arch.IsValid())
809 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000810
Chris Lattner24943d22010-06-08 16:52:24 +0000811 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000812 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000813
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000814 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000815 {
816 executable_objfile->GetDependentModules(dependent_files);
817 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
818 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000819 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
820 FileSpec platform_dependent_file_spec;
821 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000822 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000823 else
824 platform_dependent_file_spec = dependent_file_spec;
825
826 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000827 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000828 if (image_module_sp.get())
829 {
Chris Lattner24943d22010-06-08 16:52:24 +0000830 ObjectFile *objfile = image_module_sp->GetObjectFile();
831 if (objfile)
832 objfile->GetDependentModules(dependent_files);
833 }
834 }
835 }
836
Chris Lattner24943d22010-06-08 16:52:24 +0000837 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000838
839 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000840}
841
842
Jim Ingham7508e732010-08-09 23:31:02 +0000843bool
844Target::SetArchitecture (const ArchSpec &arch_spec)
845{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000846 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000847 {
848 // If we're setting the architecture to our current architecture, we
849 // don't need to do anything.
850 return true;
851 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000852 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000853 {
854 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000855 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000856 return true;
857 }
858 else
859 {
860 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000861 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000862 ModuleSP executable_sp = GetExecutableModule ();
863 m_images.Clear();
864 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000865 // Need to do something about unsetting breakpoints.
866
867 if (executable_sp)
868 {
869 FileSpec exec_file_spec = executable_sp->GetFileSpec();
870 Error error = ModuleList::GetSharedModule(exec_file_spec,
871 arch_spec,
872 NULL,
873 NULL,
874 0,
875 executable_sp,
876 NULL,
877 NULL);
878
879 if (!error.Fail() && executable_sp)
880 {
881 SetExecutableModule (executable_sp, true);
882 return true;
883 }
884 else
885 {
886 return false;
887 }
888 }
889 else
890 {
891 return false;
892 }
893 }
894}
Chris Lattner24943d22010-06-08 16:52:24 +0000895
Chris Lattner24943d22010-06-08 16:52:24 +0000896void
897Target::ModuleAdded (ModuleSP &module_sp)
898{
899 // A module is being added to this target for the first time
900 ModuleList module_list;
901 module_list.Append(module_sp);
902 ModulesDidLoad (module_list);
903}
904
905void
906Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
907{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000908 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000909 ModuleList module_list;
910 module_list.Append (old_module_sp);
911 ModulesDidUnload (module_list);
912 module_list.Clear ();
913 module_list.Append (new_module_sp);
914 ModulesDidLoad (module_list);
915}
916
917void
918Target::ModulesDidLoad (ModuleList &module_list)
919{
920 m_breakpoint_list.UpdateBreakpoints (module_list, true);
921 // TODO: make event data that packages up the module_list
922 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
923}
924
925void
926Target::ModulesDidUnload (ModuleList &module_list)
927{
928 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000929
930 // Remove the images from the target image list
931 m_images.Remove(module_list);
932
Chris Lattner24943d22010-06-08 16:52:24 +0000933 // TODO: make event data that packages up the module_list
934 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
935}
936
937size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000938Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
939{
940 const Section *section = addr.GetSection();
941 if (section && section->GetModule())
942 {
943 ObjectFile *objfile = section->GetModule()->GetObjectFile();
944 if (objfile)
945 {
946 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
947 addr.GetOffset(),
948 dst,
949 dst_len);
950 if (bytes_read > 0)
951 return bytes_read;
952 else
953 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
954 }
955 else
956 {
957 error.SetErrorString("address isn't from a object file");
958 }
959 }
960 else
961 {
962 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
963 }
964 return 0;
965}
966
967size_t
Enrico Granata91544802011-09-06 19:20:51 +0000968Target::ReadMemory (const Address& addr,
969 bool prefer_file_cache,
970 void *dst,
971 size_t dst_len,
972 Error &error,
973 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000974{
Chris Lattner24943d22010-06-08 16:52:24 +0000975 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000976
Enrico Granata91544802011-09-06 19:20:51 +0000977 // if we end up reading this from process memory, we will fill this
978 // with the actual load address
979 if (load_addr_ptr)
980 *load_addr_ptr = LLDB_INVALID_ADDRESS;
981
Greg Clayton26100dc2011-01-07 01:57:07 +0000982 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000983
984 addr_t load_addr = LLDB_INVALID_ADDRESS;
985 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000986 Address resolved_addr;
987 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000988 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000989 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000990 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000991 // No sections are loaded, so we must assume we are not running
992 // yet and anything we are given is a file address.
993 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
994 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000995 }
Greg Clayton70436352010-06-30 23:03:03 +0000996 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000997 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000998 // We have at least one section loaded. This can be becuase
999 // we have manually loaded some sections with "target modules load ..."
1000 // or because we have have a live process that has sections loaded
1001 // through the dynamic loader
1002 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
1003 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +00001004 }
Greg Clayton70436352010-06-30 23:03:03 +00001005 }
Greg Clayton889fbd02011-03-26 19:14:58 +00001006 if (!resolved_addr.IsValid())
1007 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +00001008
Greg Clayton9b82f862011-07-11 05:12:02 +00001009
Greg Clayton26100dc2011-01-07 01:57:07 +00001010 if (prefer_file_cache)
1011 {
1012 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1013 if (bytes_read > 0)
1014 return bytes_read;
1015 }
Greg Clayton70436352010-06-30 23:03:03 +00001016
Johnny Chenda5a8022011-09-20 23:28:55 +00001017 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +00001018 {
Greg Clayton9b82f862011-07-11 05:12:02 +00001019 if (load_addr == LLDB_INVALID_ADDRESS)
1020 load_addr = resolved_addr.GetLoadAddress (this);
1021
Greg Clayton70436352010-06-30 23:03:03 +00001022 if (load_addr == LLDB_INVALID_ADDRESS)
1023 {
1024 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
Greg Clayton9c236732011-10-26 00:56:27 +00001025 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded",
Greg Clayton70436352010-06-30 23:03:03 +00001026 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001027 resolved_addr.GetFileAddress(),
1028 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001029 else
Greg Clayton9c236732011-10-26 00:56:27 +00001030 error.SetErrorStringWithFormat("0x%llx can't be resolved", resolved_addr.GetFileAddress());
Greg Clayton70436352010-06-30 23:03:03 +00001031 }
1032 else
1033 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001034 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001035 if (bytes_read != dst_len)
1036 {
1037 if (error.Success())
1038 {
1039 if (bytes_read == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00001040 error.SetErrorStringWithFormat("read memory from 0x%llx failed", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001041 else
Greg Clayton9c236732011-10-26 00:56:27 +00001042 error.SetErrorStringWithFormat("only %zu of %zu bytes were read from memory at 0x%llx", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001043 }
1044 }
Greg Clayton70436352010-06-30 23:03:03 +00001045 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001046 {
1047 if (load_addr_ptr)
1048 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001049 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001050 }
Greg Clayton70436352010-06-30 23:03:03 +00001051 // If the address is not section offset we have an address that
1052 // doesn't resolve to any address in any currently loaded shared
1053 // libaries and we failed to read memory so there isn't anything
1054 // more we can do. If it is section offset, we might be able to
1055 // read cached memory from the object file.
1056 if (!resolved_addr.IsSectionOffset())
1057 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001058 }
Chris Lattner24943d22010-06-08 16:52:24 +00001059 }
Greg Clayton70436352010-06-30 23:03:03 +00001060
Greg Clayton9b82f862011-07-11 05:12:02 +00001061 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001062 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001063 // If we didn't already try and read from the object file cache, then
1064 // try it after failing to read from the process.
1065 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001066 }
1067 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001068}
1069
Greg Clayton7dd98df2011-07-12 17:06:17 +00001070size_t
1071Target::ReadScalarIntegerFromMemory (const Address& addr,
1072 bool prefer_file_cache,
1073 uint32_t byte_size,
1074 bool is_signed,
1075 Scalar &scalar,
1076 Error &error)
1077{
1078 uint64_t uval;
1079
1080 if (byte_size <= sizeof(uval))
1081 {
1082 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1083 if (bytes_read == byte_size)
1084 {
1085 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1086 uint32_t offset = 0;
1087 if (byte_size <= 4)
1088 scalar = data.GetMaxU32 (&offset, byte_size);
1089 else
1090 scalar = data.GetMaxU64 (&offset, byte_size);
1091
1092 if (is_signed)
1093 scalar.SignExtend(byte_size * 8);
1094 return bytes_read;
1095 }
1096 }
1097 else
1098 {
1099 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1100 }
1101 return 0;
1102}
1103
1104uint64_t
1105Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1106 bool prefer_file_cache,
1107 size_t integer_byte_size,
1108 uint64_t fail_value,
1109 Error &error)
1110{
1111 Scalar scalar;
1112 if (ReadScalarIntegerFromMemory (addr,
1113 prefer_file_cache,
1114 integer_byte_size,
1115 false,
1116 scalar,
1117 error))
1118 return scalar.ULongLong(fail_value);
1119 return fail_value;
1120}
1121
1122bool
1123Target::ReadPointerFromMemory (const Address& addr,
1124 bool prefer_file_cache,
1125 Error &error,
1126 Address &pointer_addr)
1127{
1128 Scalar scalar;
1129 if (ReadScalarIntegerFromMemory (addr,
1130 prefer_file_cache,
1131 m_arch.GetAddressByteSize(),
1132 false,
1133 scalar,
1134 error))
1135 {
1136 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1137 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1138 {
1139 if (m_section_load_list.IsEmpty())
1140 {
1141 // No sections are loaded, so we must assume we are not running
1142 // yet and anything we are given is a file address.
1143 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1144 }
1145 else
1146 {
1147 // We have at least one section loaded. This can be becuase
1148 // we have manually loaded some sections with "target modules load ..."
1149 // or because we have have a live process that has sections loaded
1150 // through the dynamic loader
1151 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1152 }
1153 // We weren't able to resolve the pointer value, so just return
1154 // an address with no section
1155 if (!pointer_addr.IsValid())
1156 pointer_addr.SetOffset (pointer_vm_addr);
1157 return true;
1158
1159 }
1160 }
1161 return false;
1162}
Chris Lattner24943d22010-06-08 16:52:24 +00001163
1164ModuleSP
1165Target::GetSharedModule
1166(
1167 const FileSpec& file_spec,
1168 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +00001169 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +00001170 const ConstString *object_name,
1171 off_t object_offset,
1172 Error *error_ptr
1173)
1174{
1175 // Don't pass in the UUID so we can tell if we have a stale value in our list
1176 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1177 bool did_create_module = false;
1178 ModuleSP module_sp;
1179
Chris Lattner24943d22010-06-08 16:52:24 +00001180 Error error;
1181
Greg Clayton24bc5d92011-03-30 18:16:51 +00001182 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +00001183 if (m_image_search_paths.GetSize())
1184 {
1185 FileSpec transformed_spec;
1186 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1187 {
1188 transformed_spec.GetFilename() = file_spec.GetFilename();
1189 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1190 }
1191 }
1192
Greg Clayton24bc5d92011-03-30 18:16:51 +00001193 // The platform is responsible for finding and caching an appropriate
1194 // module in the shared module cache.
1195 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001196 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001197 FileSpec platform_file_spec;
1198 error = m_platform_sp->GetSharedModule (file_spec,
1199 arch,
1200 uuid_ptr,
1201 object_name,
1202 object_offset,
1203 module_sp,
1204 &old_module_sp,
1205 &did_create_module);
1206 }
1207 else
1208 {
1209 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +00001210 }
1211
Greg Clayton24bc5d92011-03-30 18:16:51 +00001212 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +00001213 if (module_sp)
1214 {
1215 m_images.Append (module_sp);
1216 if (did_create_module)
1217 {
1218 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1219 ModuleUpdated(old_module_sp, module_sp);
1220 else
1221 ModuleAdded(module_sp);
1222 }
1223 }
1224 if (error_ptr)
1225 *error_ptr = error;
1226 return module_sp;
1227}
1228
1229
1230Target *
1231Target::CalculateTarget ()
1232{
1233 return this;
1234}
1235
1236Process *
1237Target::CalculateProcess ()
1238{
1239 return NULL;
1240}
1241
1242Thread *
1243Target::CalculateThread ()
1244{
1245 return NULL;
1246}
1247
1248StackFrame *
1249Target::CalculateStackFrame ()
1250{
1251 return NULL;
1252}
1253
1254void
Greg Claytona830adb2010-10-04 01:05:56 +00001255Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001256{
Greg Clayton567e7f32011-09-22 04:58:26 +00001257 exe_ctx.Clear();
1258 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001259}
1260
1261PathMappingList &
1262Target::GetImageSearchPathList ()
1263{
1264 return m_image_search_paths;
1265}
1266
1267void
1268Target::ImageSearchPathsChanged
1269(
1270 const PathMappingList &path_list,
1271 void *baton
1272)
1273{
1274 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001275 ModuleSP exe_module_sp (target->GetExecutableModule());
1276 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001277 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001278 target->m_images.Clear();
1279 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001280 }
1281}
1282
1283ClangASTContext *
1284Target::GetScratchClangASTContext()
1285{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001286 // Now see if we know the target triple, and if so, create our scratch AST context:
1287 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1288 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +00001289 return m_scratch_ast_context_ap.get();
1290}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001291
Greg Clayton990de7b2010-11-18 23:32:35 +00001292void
Caroline Tice2a456812011-03-10 22:14:10 +00001293Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001294{
Greg Clayton990de7b2010-11-18 23:32:35 +00001295 UserSettingsControllerSP &usc = GetSettingsController();
1296 usc.reset (new SettingsController);
1297 UserSettingsController::InitializeSettingsController (usc,
1298 SettingsController::global_settings_table,
1299 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001300
1301 // Now call SettingsInitialize() on each 'child' setting of Target
1302 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001303}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001304
Greg Clayton990de7b2010-11-18 23:32:35 +00001305void
Caroline Tice2a456812011-03-10 22:14:10 +00001306Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001307{
Caroline Tice2a456812011-03-10 22:14:10 +00001308
1309 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1310
1311 Process::SettingsTerminate ();
1312
1313 // Now terminate Target Settings.
1314
Greg Clayton990de7b2010-11-18 23:32:35 +00001315 UserSettingsControllerSP &usc = GetSettingsController();
1316 UserSettingsController::FinalizeSettingsController (usc);
1317 usc.reset();
1318}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001319
Greg Clayton990de7b2010-11-18 23:32:35 +00001320UserSettingsControllerSP &
1321Target::GetSettingsController ()
1322{
1323 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001324 return g_settings_controller;
1325}
1326
1327ArchSpec
1328Target::GetDefaultArchitecture ()
1329{
Greg Clayton940b1032011-02-23 00:35:02 +00001330 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1331
1332 if (settings_controller_sp)
1333 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1334 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001335}
1336
1337void
Greg Clayton940b1032011-02-23 00:35:02 +00001338Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001339{
Greg Clayton940b1032011-02-23 00:35:02 +00001340 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1341
1342 if (settings_controller_sp)
1343 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001344}
1345
Greg Claytona830adb2010-10-04 01:05:56 +00001346Target *
1347Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1348{
1349 // The target can either exist in the "process" of ExecutionContext, or in
1350 // the "target_sp" member of SymbolContext. This accessor helper function
1351 // will get the target from one of these locations.
1352
1353 Target *target = NULL;
1354 if (sc_ptr != NULL)
1355 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001356 if (target == NULL && exe_ctx_ptr)
1357 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001358 return target;
1359}
1360
1361
Caroline Tice1ebef442010-09-27 00:30:10 +00001362void
1363Target::UpdateInstanceName ()
1364{
1365 StreamString sstr;
1366
Greg Clayton5beb99d2011-08-11 02:48:45 +00001367 Module *exe_module = GetExecutableModulePointer();
1368 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001369 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001370 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001371 exe_module->GetFileSpec().GetFilename().AsCString(),
1372 exe_module->GetArchitecture().GetArchitectureName());
1373 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001374 }
1375}
1376
Sean Callanan77e93942010-10-29 00:29:03 +00001377const char *
1378Target::GetExpressionPrefixContentsAsCString ()
1379{
Greg Claytonff44ab42011-04-23 02:04:55 +00001380 if (m_expr_prefix_contents_sp)
1381 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1382 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001383}
1384
Greg Clayton427f2902010-12-14 02:59:59 +00001385ExecutionResults
1386Target::EvaluateExpression
1387(
1388 const char *expr_cstr,
1389 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001390 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton427f2902010-12-14 02:59:59 +00001391 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001392 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001393 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001394 lldb::ValueObjectSP &result_valobj_sp
1395)
1396{
1397 ExecutionResults execution_results = eExecutionSetupError;
1398
1399 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001400
1401 // We shouldn't run stop hooks in expressions.
1402 // Be sure to reset this if you return anywhere within this function.
1403 bool old_suppress_value = m_suppress_stop_hooks;
1404 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001405
1406 ExecutionContext exe_ctx;
1407 if (frame)
1408 {
1409 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001410 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001411 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001412 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1413 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001414 lldb::VariableSP var_sp;
1415 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1416 use_dynamic,
1417 expr_path_options,
1418 var_sp,
1419 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001420 }
1421 else if (m_process_sp)
1422 {
1423 m_process_sp->CalculateExecutionContext(exe_ctx);
1424 }
1425 else
1426 {
1427 CalculateExecutionContext(exe_ctx);
1428 }
1429
1430 if (result_valobj_sp)
1431 {
1432 execution_results = eExecutionCompleted;
1433 // We got a result from the frame variable expression path above...
1434 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1435
1436 lldb::ValueObjectSP const_valobj_sp;
1437
1438 // Check in case our value is already a constant value
1439 if (result_valobj_sp->GetIsConstant())
1440 {
1441 const_valobj_sp = result_valobj_sp;
1442 const_valobj_sp->SetName (persistent_variable_name);
1443 }
1444 else
Jim Inghame41494a2011-04-16 00:01:13 +00001445 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001446 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001447 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001448 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001449 if (dynamic_sp)
1450 result_valobj_sp = dynamic_sp;
1451 }
1452
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001453 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001454 }
Greg Clayton427f2902010-12-14 02:59:59 +00001455
Sean Callanan6a925532011-01-13 08:53:35 +00001456 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1457
Greg Clayton427f2902010-12-14 02:59:59 +00001458 result_valobj_sp = const_valobj_sp;
1459
Sean Callanan6a925532011-01-13 08:53:35 +00001460 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1461 assert (clang_expr_variable_sp.get());
1462
1463 // Set flags and live data as appropriate
1464
1465 const Value &result_value = live_valobj_sp->GetValue();
1466
1467 switch (result_value.GetValueType())
1468 {
1469 case Value::eValueTypeHostAddress:
1470 case Value::eValueTypeFileAddress:
1471 // we don't do anything with these for now
1472 break;
1473 case Value::eValueTypeScalar:
1474 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1475 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1476 break;
1477 case Value::eValueTypeLoadAddress:
1478 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1479 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1480 break;
1481 }
Greg Clayton427f2902010-12-14 02:59:59 +00001482 }
1483 else
1484 {
1485 // Make sure we aren't just trying to see the value of a persistent
1486 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001487 lldb::ClangExpressionVariableSP persistent_var_sp;
1488 // Only check for persistent variables the expression starts with a '$'
1489 if (expr_cstr[0] == '$')
1490 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1491
Greg Clayton427f2902010-12-14 02:59:59 +00001492 if (persistent_var_sp)
1493 {
1494 result_valobj_sp = persistent_var_sp->GetValueObject ();
1495 execution_results = eExecutionCompleted;
1496 }
1497 else
1498 {
1499 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001500
Greg Clayton427f2902010-12-14 02:59:59 +00001501 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001502 execution_policy,
Sean Callanan6a925532011-01-13 08:53:35 +00001503 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001504 expr_cstr,
1505 prefix,
1506 result_valobj_sp);
1507 }
1508 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001509
1510 m_suppress_stop_hooks = old_suppress_value;
1511
Greg Clayton427f2902010-12-14 02:59:59 +00001512 return execution_results;
1513}
1514
Greg Claytonc0fa5332011-05-22 22:46:53 +00001515lldb::addr_t
1516Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1517{
1518 addr_t code_addr = load_addr;
1519 switch (m_arch.GetMachine())
1520 {
1521 case llvm::Triple::arm:
1522 case llvm::Triple::thumb:
1523 switch (addr_class)
1524 {
1525 case eAddressClassData:
1526 case eAddressClassDebug:
1527 return LLDB_INVALID_ADDRESS;
1528
1529 case eAddressClassUnknown:
1530 case eAddressClassInvalid:
1531 case eAddressClassCode:
1532 case eAddressClassCodeAlternateISA:
1533 case eAddressClassRuntime:
1534 // Check if bit zero it no set?
1535 if ((code_addr & 1ull) == 0)
1536 {
1537 // Bit zero isn't set, check if the address is a multiple of 2?
1538 if (code_addr & 2ull)
1539 {
1540 // The address is a multiple of 2 so it must be thumb, set bit zero
1541 code_addr |= 1ull;
1542 }
1543 else if (addr_class == eAddressClassCodeAlternateISA)
1544 {
1545 // We checked the address and the address claims to be the alternate ISA
1546 // which means thumb, so set bit zero.
1547 code_addr |= 1ull;
1548 }
1549 }
1550 break;
1551 }
1552 break;
1553
1554 default:
1555 break;
1556 }
1557 return code_addr;
1558}
1559
1560lldb::addr_t
1561Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1562{
1563 addr_t opcode_addr = load_addr;
1564 switch (m_arch.GetMachine())
1565 {
1566 case llvm::Triple::arm:
1567 case llvm::Triple::thumb:
1568 switch (addr_class)
1569 {
1570 case eAddressClassData:
1571 case eAddressClassDebug:
1572 return LLDB_INVALID_ADDRESS;
1573
1574 case eAddressClassInvalid:
1575 case eAddressClassUnknown:
1576 case eAddressClassCode:
1577 case eAddressClassCodeAlternateISA:
1578 case eAddressClassRuntime:
1579 opcode_addr &= ~(1ull);
1580 break;
1581 }
1582 break;
1583
1584 default:
1585 break;
1586 }
1587 return opcode_addr;
1588}
1589
Jim Inghamd60d94a2011-03-11 03:53:59 +00001590lldb::user_id_t
1591Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1592{
1593 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1594 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1595 m_stop_hooks[new_uid] = new_hook_sp;
1596 return new_uid;
1597}
1598
1599bool
1600Target::RemoveStopHookByID (lldb::user_id_t user_id)
1601{
1602 size_t num_removed;
1603 num_removed = m_stop_hooks.erase (user_id);
1604 if (num_removed == 0)
1605 return false;
1606 else
1607 return true;
1608}
1609
1610void
1611Target::RemoveAllStopHooks ()
1612{
1613 m_stop_hooks.clear();
1614}
1615
1616Target::StopHookSP
1617Target::GetStopHookByID (lldb::user_id_t user_id)
1618{
1619 StopHookSP found_hook;
1620
1621 StopHookCollection::iterator specified_hook_iter;
1622 specified_hook_iter = m_stop_hooks.find (user_id);
1623 if (specified_hook_iter != m_stop_hooks.end())
1624 found_hook = (*specified_hook_iter).second;
1625 return found_hook;
1626}
1627
1628bool
1629Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1630{
1631 StopHookCollection::iterator specified_hook_iter;
1632 specified_hook_iter = m_stop_hooks.find (user_id);
1633 if (specified_hook_iter == m_stop_hooks.end())
1634 return false;
1635
1636 (*specified_hook_iter).second->SetIsActive (active_state);
1637 return true;
1638}
1639
1640void
1641Target::SetAllStopHooksActiveState (bool active_state)
1642{
1643 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1644 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1645 {
1646 (*pos).second->SetIsActive (active_state);
1647 }
1648}
1649
1650void
1651Target::RunStopHooks ()
1652{
Jim Ingham3613ae12011-05-12 02:06:14 +00001653 if (m_suppress_stop_hooks)
1654 return;
1655
Jim Inghamd60d94a2011-03-11 03:53:59 +00001656 if (!m_process_sp)
1657 return;
1658
1659 if (m_stop_hooks.empty())
1660 return;
1661
1662 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1663
1664 // If there aren't any active stop hooks, don't bother either:
1665 bool any_active_hooks = false;
1666 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1667 {
1668 if ((*pos).second->IsActive())
1669 {
1670 any_active_hooks = true;
1671 break;
1672 }
1673 }
1674 if (!any_active_hooks)
1675 return;
1676
1677 CommandReturnObject result;
1678
1679 std::vector<ExecutionContext> exc_ctx_with_reasons;
1680 std::vector<SymbolContext> sym_ctx_with_reasons;
1681
1682 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1683 size_t num_threads = cur_threadlist.GetSize();
1684 for (size_t i = 0; i < num_threads; i++)
1685 {
1686 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1687 if (cur_thread_sp->ThreadStoppedForAReason())
1688 {
1689 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1690 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1691 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1692 }
1693 }
1694
1695 // If no threads stopped for a reason, don't run the stop-hooks.
1696 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1697 if (num_exe_ctx == 0)
1698 return;
1699
Jim Inghame5ed8e92011-06-02 23:58:26 +00001700 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1701 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001702
1703 bool keep_going = true;
1704 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001705 bool print_hook_header;
1706 bool print_thread_header;
1707
1708 if (num_exe_ctx == 1)
1709 print_thread_header = false;
1710 else
1711 print_thread_header = true;
1712
1713 if (m_stop_hooks.size() == 1)
1714 print_hook_header = false;
1715 else
1716 print_hook_header = true;
1717
Jim Inghamd60d94a2011-03-11 03:53:59 +00001718 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1719 {
1720 // result.Clear();
1721 StopHookSP cur_hook_sp = (*pos).second;
1722 if (!cur_hook_sp->IsActive())
1723 continue;
1724
1725 bool any_thread_matched = false;
1726 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1727 {
1728 if ((cur_hook_sp->GetSpecifier () == NULL
1729 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1730 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Clayton567e7f32011-09-22 04:58:26 +00001731 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001732 {
1733 if (!hooks_ran)
1734 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001735 hooks_ran = true;
1736 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001737 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001738 {
Johnny Chen4d96a742011-10-24 23:01:06 +00001739 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
1740 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
1741 NULL);
1742 if (cmd)
1743 result.AppendMessageWithFormat("\n- Hook %llu (%s)\n", cur_hook_sp->GetID(), cmd);
1744 else
1745 result.AppendMessageWithFormat("\n- Hook %llu\n", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001746 any_thread_matched = true;
1747 }
1748
Jim Inghamc54840c2011-03-22 01:47:27 +00001749 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001750 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001751
1752 bool stop_on_continue = true;
1753 bool stop_on_error = true;
1754 bool echo_commands = false;
1755 bool print_results = true;
1756 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001757 &exc_ctx_with_reasons[i],
1758 stop_on_continue,
1759 stop_on_error,
1760 echo_commands,
1761 print_results,
1762 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001763
1764 // If the command started the target going again, we should bag out of
1765 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001766 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1767 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001768 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001769 result.AppendMessageWithFormat ("Aborting stop hooks, hook %llu set the program running.", cur_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001770 keep_going = false;
1771 }
1772 }
1773 }
1774 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00001775
Caroline Tice4a348082011-05-02 20:41:46 +00001776 result.GetImmediateOutputStream()->Flush();
1777 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001778}
1779
Greg Claytonbbea1332011-07-08 00:48:09 +00001780bool
1781Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1782{
1783 bool changed = false;
1784 if (module)
1785 {
1786 ObjectFile *object_file = module->GetObjectFile();
1787 if (object_file)
1788 {
1789 SectionList *section_list = object_file->GetSectionList ();
1790 if (section_list)
1791 {
1792 // All sections listed in the dyld image info structure will all
1793 // either be fixed up already, or they will all be off by a single
1794 // slide amount that is determined by finding the first segment
1795 // that is at file offset zero which also has bytes (a file size
1796 // that is greater than zero) in the object file.
1797
1798 // Determine the slide amount (if any)
1799 const size_t num_sections = section_list->GetSize();
1800 size_t sect_idx = 0;
1801 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1802 {
1803 // Iterate through the object file sections to find the
1804 // first section that starts of file offset zero and that
1805 // has bytes in the file...
1806 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1807 if (section)
1808 {
1809 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1810 changed = true;
1811 }
1812 }
1813 }
1814 }
1815 }
1816 return changed;
1817}
1818
1819
Jim Inghamd60d94a2011-03-11 03:53:59 +00001820//--------------------------------------------------------------
1821// class Target::StopHook
1822//--------------------------------------------------------------
1823
1824
1825Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1826 UserID (uid),
1827 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001828 m_commands (),
1829 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001830 m_thread_spec_ap(NULL),
1831 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001832{
1833}
1834
1835Target::StopHook::StopHook (const StopHook &rhs) :
1836 UserID (rhs.GetID()),
1837 m_target_sp (rhs.m_target_sp),
1838 m_commands (rhs.m_commands),
1839 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001840 m_thread_spec_ap (NULL),
1841 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001842{
1843 if (rhs.m_thread_spec_ap.get() != NULL)
1844 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1845}
1846
1847
1848Target::StopHook::~StopHook ()
1849{
1850}
1851
1852void
1853Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1854{
1855 m_thread_spec_ap.reset (specifier);
1856}
1857
1858
1859void
1860Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1861{
1862 int indent_level = s->GetIndentLevel();
1863
1864 s->SetIndentLevel(indent_level + 2);
1865
Greg Clayton444e35b2011-10-19 18:09:39 +00001866 s->Printf ("Hook: %llu\n", GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001867 if (m_active)
1868 s->Indent ("State: enabled\n");
1869 else
1870 s->Indent ("State: disabled\n");
1871
1872 if (m_specifier_sp)
1873 {
1874 s->Indent();
1875 s->PutCString ("Specifier:\n");
1876 s->SetIndentLevel (indent_level + 4);
1877 m_specifier_sp->GetDescription (s, level);
1878 s->SetIndentLevel (indent_level + 2);
1879 }
1880
1881 if (m_thread_spec_ap.get() != NULL)
1882 {
1883 StreamString tmp;
1884 s->Indent("Thread:\n");
1885 m_thread_spec_ap->GetDescription (&tmp, level);
1886 s->SetIndentLevel (indent_level + 4);
1887 s->Indent (tmp.GetData());
1888 s->PutCString ("\n");
1889 s->SetIndentLevel (indent_level + 2);
1890 }
1891
1892 s->Indent ("Commands: \n");
1893 s->SetIndentLevel (indent_level + 4);
1894 uint32_t num_commands = m_commands.GetSize();
1895 for (uint32_t i = 0; i < num_commands; i++)
1896 {
1897 s->Indent(m_commands.GetStringAtIndex(i));
1898 s->PutCString ("\n");
1899 }
1900 s->SetIndentLevel (indent_level);
1901}
1902
1903
Caroline Tice5bc8c972010-09-20 20:44:43 +00001904//--------------------------------------------------------------
1905// class Target::SettingsController
1906//--------------------------------------------------------------
1907
1908Target::SettingsController::SettingsController () :
1909 UserSettingsController ("target", Debugger::GetSettingsController()),
1910 m_default_architecture ()
1911{
1912 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1913 InstanceSettings::GetDefaultName().AsCString()));
1914}
1915
1916Target::SettingsController::~SettingsController ()
1917{
1918}
1919
1920lldb::InstanceSettingsSP
1921Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1922{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001923 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1924 false,
1925 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001926 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1927 return new_settings_sp;
1928}
1929
Caroline Tice5bc8c972010-09-20 20:44:43 +00001930
Jim Inghame41494a2011-04-16 00:01:13 +00001931#define TSC_DEFAULT_ARCH "default-arch"
1932#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001933#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001934#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001935#define TSC_SOURCE_MAP "source-map"
Enrico Granata018921d2011-08-12 02:00:06 +00001936#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata91544802011-09-06 19:20:51 +00001937#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytond284b662011-02-18 01:44:25 +00001938
1939
1940static const ConstString &
1941GetSettingNameForDefaultArch ()
1942{
1943 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001944 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001945}
1946
Greg Claytond284b662011-02-18 01:44:25 +00001947static const ConstString &
1948GetSettingNameForExpressionPrefix ()
1949{
1950 static ConstString g_const_string (TSC_EXPR_PREFIX);
1951 return g_const_string;
1952}
1953
1954static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001955GetSettingNameForPreferDynamicValue ()
1956{
1957 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1958 return g_const_string;
1959}
1960
Greg Claytonff44ab42011-04-23 02:04:55 +00001961static const ConstString &
1962GetSettingNameForSourcePathMap ()
1963{
1964 static ConstString g_const_string (TSC_SOURCE_MAP);
1965 return g_const_string;
1966}
Greg Claytond284b662011-02-18 01:44:25 +00001967
Greg Clayton17cd9952011-04-22 03:55:06 +00001968static const ConstString &
1969GetSettingNameForSkipPrologue ()
1970{
1971 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1972 return g_const_string;
1973}
1974
Enrico Granata018921d2011-08-12 02:00:06 +00001975static const ConstString &
1976GetSettingNameForMaxChildren ()
1977{
1978 static ConstString g_const_string (TSC_MAX_CHILDREN);
1979 return g_const_string;
1980}
Greg Clayton17cd9952011-04-22 03:55:06 +00001981
Enrico Granata91544802011-09-06 19:20:51 +00001982static const ConstString &
1983GetSettingNameForMaxStringSummaryLength ()
1984{
1985 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1986 return g_const_string;
1987}
Greg Clayton17cd9952011-04-22 03:55:06 +00001988
Caroline Tice5bc8c972010-09-20 20:44:43 +00001989bool
1990Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1991 const char *index_value,
1992 const char *value,
1993 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001994 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001995 Error&err)
1996{
Greg Claytond284b662011-02-18 01:44:25 +00001997 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001998 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001999 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00002000 if (!m_default_architecture.IsValid())
2001 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002002 }
2003 return true;
2004}
2005
2006
2007bool
2008Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
2009 StringList &value,
2010 Error &err)
2011{
Greg Claytond284b662011-02-18 01:44:25 +00002012 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00002013 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00002014 // If the arch is invalid (the default), don't show a string for it
2015 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00002016 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00002017 return true;
2018 }
2019 else
2020 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2021
2022 return false;
2023}
2024
2025//--------------------------------------------------------------
2026// class TargetInstanceSettings
2027//--------------------------------------------------------------
2028
Greg Clayton638351a2010-12-04 00:10:17 +00002029TargetInstanceSettings::TargetInstanceSettings
2030(
2031 UserSettingsController &owner,
2032 bool live_instance,
2033 const char *name
2034) :
Greg Claytond284b662011-02-18 01:44:25 +00002035 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002036 m_expr_prefix_file (),
2037 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002038 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00002039 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002040 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00002041 m_max_children_display(256),
2042 m_max_strlen_length(1024)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002043{
2044 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2045 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2046 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2047 // This is true for CreateInstanceName() too.
2048
2049 if (GetInstanceName () == InstanceSettings::InvalidName())
2050 {
2051 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2052 m_owner.RegisterInstanceSettings (this);
2053 }
2054
2055 if (live_instance)
2056 {
2057 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2058 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002059 }
2060}
2061
2062TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00002063 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
2064 m_expr_prefix_file (rhs.m_expr_prefix_file),
2065 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
2066 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2067 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002068 m_source_map (rhs.m_source_map),
Enrico Granata91544802011-09-06 19:20:51 +00002069 m_max_children_display(rhs.m_max_children_display),
2070 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002071{
2072 if (m_instance_name != InstanceSettings::GetDefaultName())
2073 {
2074 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2075 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002076 }
2077}
2078
2079TargetInstanceSettings::~TargetInstanceSettings ()
2080{
2081}
2082
2083TargetInstanceSettings&
2084TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2085{
2086 if (this != &rhs)
2087 {
2088 }
2089
2090 return *this;
2091}
2092
Caroline Tice5bc8c972010-09-20 20:44:43 +00002093void
2094TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2095 const char *index_value,
2096 const char *value,
2097 const ConstString &instance_name,
2098 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002099 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002100 Error &err,
2101 bool pending)
2102{
Greg Claytond284b662011-02-18 01:44:25 +00002103 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002104 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002105 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2106 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002107 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002108 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002109 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002110 default:
2111 break;
2112 case eVarSetOperationAssign:
2113 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002114 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002115 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2116 {
2117 err.SetErrorToGenericError ();
Greg Clayton9c236732011-10-26 00:56:27 +00002118 err.SetErrorStringWithFormat ("%s does not exist", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002119 return;
2120 }
2121
2122 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
2123
2124 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
2125 {
Greg Clayton9c236732011-10-26 00:56:27 +00002126 err.SetErrorStringWithFormat ("couldn't read data from '%s'", value);
Greg Claytonff44ab42011-04-23 02:04:55 +00002127 m_expr_prefix_contents_sp.reset();
2128 }
Sean Callanan77e93942010-10-29 00:29:03 +00002129 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002130 break;
2131 case eVarSetOperationClear:
2132 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00002133 }
Sean Callanan77e93942010-10-29 00:29:03 +00002134 }
2135 }
Jim Inghame41494a2011-04-16 00:01:13 +00002136 else if (var_name == GetSettingNameForPreferDynamicValue())
2137 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002138 int new_value;
2139 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2140 if (err.Success())
2141 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002142 }
2143 else if (var_name == GetSettingNameForSkipPrologue())
2144 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002145 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2146 }
Enrico Granata018921d2011-08-12 02:00:06 +00002147 else if (var_name == GetSettingNameForMaxChildren())
2148 {
2149 bool ok;
2150 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2151 if (ok)
2152 m_max_children_display = new_value;
2153 }
Enrico Granata91544802011-09-06 19:20:51 +00002154 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2155 {
2156 bool ok;
2157 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2158 if (ok)
2159 m_max_strlen_length = new_value;
2160 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002161 else if (var_name == GetSettingNameForSourcePathMap ())
2162 {
2163 switch (op)
2164 {
2165 case eVarSetOperationReplace:
2166 case eVarSetOperationInsertBefore:
2167 case eVarSetOperationInsertAfter:
2168 case eVarSetOperationRemove:
2169 default:
2170 break;
2171 case eVarSetOperationAssign:
2172 m_source_map.Clear(true);
2173 // Fall through to append....
2174 case eVarSetOperationAppend:
2175 {
2176 Args args(value);
2177 const uint32_t argc = args.GetArgumentCount();
2178 if (argc & 1 || argc == 0)
2179 {
2180 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2181 }
2182 else
2183 {
2184 char resolved_new_path[PATH_MAX];
2185 FileSpec file_spec;
2186 const char *old_path;
2187 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2188 {
2189 const char *new_path = args.GetArgumentAtIndex(idx+1);
2190 assert (new_path); // We have an even number of paths, this shouldn't happen!
2191
2192 file_spec.SetFile(new_path, true);
2193 if (file_spec.Exists())
2194 {
2195 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2196 {
2197 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2198 return;
2199 }
2200 }
2201 else
2202 {
2203 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2204 return;
2205 }
2206 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2207 }
2208 }
2209 }
2210 break;
2211
2212 case eVarSetOperationClear:
2213 m_source_map.Clear(true);
2214 break;
2215 }
Jim Inghame41494a2011-04-16 00:01:13 +00002216 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002217}
2218
2219void
Greg Claytond284b662011-02-18 01:44:25 +00002220TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002221{
Sean Callanan77e93942010-10-29 00:29:03 +00002222 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2223
2224 if (!new_settings_ptr)
2225 return;
2226
Greg Claytonff44ab42011-04-23 02:04:55 +00002227 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
2228 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
2229 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
2230 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata018921d2011-08-12 02:00:06 +00002231 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata91544802011-09-06 19:20:51 +00002232 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002233}
2234
Caroline Ticebcb5b452010-09-20 21:37:42 +00002235bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002236TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2237 const ConstString &var_name,
2238 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002239 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002240{
Greg Claytond284b662011-02-18 01:44:25 +00002241 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002242 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002243 char path[PATH_MAX];
2244 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2245 if (path_len > 0)
2246 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002247 }
Jim Inghame41494a2011-04-16 00:01:13 +00002248 else if (var_name == GetSettingNameForPreferDynamicValue())
2249 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002250 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002251 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002252 else if (var_name == GetSettingNameForSkipPrologue())
2253 {
2254 if (m_skip_prologue)
2255 value.AppendString ("true");
2256 else
2257 value.AppendString ("false");
2258 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002259 else if (var_name == GetSettingNameForSourcePathMap ())
2260 {
2261 }
Enrico Granata018921d2011-08-12 02:00:06 +00002262 else if (var_name == GetSettingNameForMaxChildren())
2263 {
2264 StreamString count_str;
2265 count_str.Printf ("%d", m_max_children_display);
2266 value.AppendString (count_str.GetData());
2267 }
Enrico Granata91544802011-09-06 19:20:51 +00002268 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2269 {
2270 StreamString count_str;
2271 count_str.Printf ("%d", m_max_strlen_length);
2272 value.AppendString (count_str.GetData());
2273 }
Sean Callanan77e93942010-10-29 00:29:03 +00002274 else
2275 {
2276 if (err)
2277 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2278 return false;
2279 }
2280
2281 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002282}
2283
2284const ConstString
2285TargetInstanceSettings::CreateInstanceName ()
2286{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002287 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002288 static int instance_count = 1;
2289
Caroline Tice5bc8c972010-09-20 20:44:43 +00002290 sstr.Printf ("target_%d", instance_count);
2291 ++instance_count;
2292
2293 const ConstString ret_val (sstr.GetData());
2294 return ret_val;
2295}
2296
2297//--------------------------------------------------
2298// Target::SettingsController Variable Tables
2299//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002300OptionEnumValueElement
2301TargetInstanceSettings::g_dynamic_value_types[] =
2302{
Greg Clayton577fbc32011-05-30 00:39:48 +00002303{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2304{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2305{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002306{ 0, NULL, NULL }
2307};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002308
2309SettingEntry
2310Target::SettingsController::global_settings_table[] =
2311{
Greg Claytond284b662011-02-18 01:44:25 +00002312 // var-name var-type default enum init'd hidden help-text
2313 // ================= ================== =========== ==== ====== ====== =========================================================================
2314 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2315 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2316};
2317
Caroline Tice5bc8c972010-09-20 20:44:43 +00002318SettingEntry
2319Target::SettingsController::instance_settings_table[] =
2320{
Enrico Granata91544802011-09-06 19:20:51 +00002321 // var-name var-type default enum init'd hidden help-text
2322 // ================= ================== =============== ======================= ====== ====== =========================================================================
2323 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2324 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2325 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2326 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2327 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2328 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2329 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002330};