blob: 6a742c28e598d2acc41b5f12ef5a4b59f48ed90d [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham03c8ee52011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen096c2932011-09-26 22:40:50 +000021#include "lldb/Breakpoint/WatchpointLocation.h"
Greg Clayton427f2902010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Caroline Tice4a348082011-05-02 20:41:46 +000025#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000027#include "lldb/Core/Timer.h"
28#include "lldb/Core/ValueObject.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000029#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000031#include "lldb/Interpreter/CommandInterpreter.h"
32#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/lldb-private-log.h"
34#include "lldb/Symbol/ObjectFile.h"
35#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000036#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000037#include "lldb/Target/Thread.h"
38#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
43//----------------------------------------------------------------------
44// Target constructor
45//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000046Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
47 Broadcaster ("lldb.target"),
48 ExecutionContextScope (),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000049 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000050 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000051 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000052 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000053 m_arch (target_arch),
54 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000055 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000056 m_breakpoint_list (false),
57 m_internal_breakpoint_list (true),
Johnny Chen9a3c2a52011-09-06 20:05:25 +000058 m_watchpoint_location_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000059 m_process_sp (),
60 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000061 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000062 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000063 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000064 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000065 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000066 m_stop_hook_next_id (0),
67 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000068{
Greg Clayton49ce6822010-10-31 03:01:06 +000069 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
70 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
71 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
72
Greg Claytone005f2c2010-11-06 01:53:30 +000073 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000074 if (log)
75 log->Printf ("%p Target::Target()", this);
76}
77
78//----------------------------------------------------------------------
79// Destructor
80//----------------------------------------------------------------------
81Target::~Target()
82{
Greg Claytone005f2c2010-11-06 01:53:30 +000083 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000084 if (log)
85 log->Printf ("%p Target::~Target()", this);
86 DeleteCurrentProcess ();
87}
88
89void
Caroline Tice7826c882010-10-26 03:11:13 +000090Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000091{
Greg Clayton3fed8b92010-10-08 00:21:05 +000092// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000093 if (description_level != lldb::eDescriptionLevelBrief)
94 {
95 s->Indent();
96 s->PutCString("Target\n");
97 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000098 m_images.Dump(s);
99 m_breakpoint_list.Dump(s);
100 m_internal_breakpoint_list.Dump(s);
101 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000102 }
103 else
104 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000105 Module *exe_module = GetExecutableModulePointer();
106 if (exe_module)
107 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000108 else
109 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000110 }
Chris Lattner24943d22010-06-08 16:52:24 +0000111}
112
113void
114Target::DeleteCurrentProcess ()
115{
116 if (m_process_sp.get())
117 {
Greg Clayton49480b12010-09-14 23:52:43 +0000118 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000119 if (m_process_sp->IsAlive())
120 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000121
122 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000123
124 // Do any cleanup of the target we need to do between process instances.
125 // NB It is better to do this before destroying the process in case the
126 // clean up needs some help from the process.
127 m_breakpoint_list.ClearAllBreakpointSites();
128 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenc86582f2011-09-23 21:21:43 +0000129 // Disable watchpoint locations just on the debugger side.
130 DisableAllWatchpointLocations(false);
Chris Lattner24943d22010-06-08 16:52:24 +0000131 m_process_sp.reset();
132 }
133}
134
135const lldb::ProcessSP &
136Target::CreateProcess (Listener &listener, const char *plugin_name)
137{
138 DeleteCurrentProcess ();
139 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
140 return m_process_sp;
141}
142
143const lldb::ProcessSP &
144Target::GetProcessSP () const
145{
146 return m_process_sp;
147}
148
149lldb::TargetSP
150Target::GetSP()
151{
Greg Clayton987c7eb2011-09-17 08:33:22 +0000152 // This object contains an instrusive ref count base class so we can
153 // easily make a shared pointer to this object
154 return TargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000155}
156
Greg Clayton153ccd72011-08-10 02:10:13 +0000157void
158Target::Destroy()
159{
160 Mutex::Locker locker (m_mutex);
161 DeleteCurrentProcess ();
162 m_platform_sp.reset();
163 m_arch.Clear();
164 m_images.Clear();
165 m_section_load_list.Clear();
166 const bool notify = false;
167 m_breakpoint_list.RemoveAll(notify);
168 m_internal_breakpoint_list.RemoveAll(notify);
169 m_last_created_breakpoint.reset();
170 m_search_filter_sp.reset();
171 m_image_search_paths.Clear(notify);
172 m_scratch_ast_context_ap.reset();
173 m_persistent_variables.Clear();
174 m_stop_hooks.clear();
175 m_stop_hook_next_id = 0;
176 m_suppress_stop_hooks = false;
177}
178
179
Chris Lattner24943d22010-06-08 16:52:24 +0000180BreakpointList &
181Target::GetBreakpointList(bool internal)
182{
183 if (internal)
184 return m_internal_breakpoint_list;
185 else
186 return m_breakpoint_list;
187}
188
189const BreakpointList &
190Target::GetBreakpointList(bool internal) const
191{
192 if (internal)
193 return m_internal_breakpoint_list;
194 else
195 return m_breakpoint_list;
196}
197
198BreakpointSP
199Target::GetBreakpointByID (break_id_t break_id)
200{
201 BreakpointSP bp_sp;
202
203 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
204 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
205 else
206 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
207
208 return bp_sp;
209}
210
211BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000212Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
213 const FileSpecList *source_file_spec_list,
Jim Ingham03c8ee52011-09-21 01:17:13 +0000214 RegularExpression &source_regex,
215 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000216{
Jim Inghamd6d47972011-09-23 00:54:11 +0000217 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
218 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham03c8ee52011-09-21 01:17:13 +0000219 return CreateBreakpoint (filter_sp, resolver_sp, internal);
220}
221
222
223BreakpointSP
224Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
225{
226 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner24943d22010-06-08 16:52:24 +0000227 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
228 return CreateBreakpoint (filter_sp, resolver_sp, internal);
229}
230
231
232BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000233Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000234{
Chris Lattner24943d22010-06-08 16:52:24 +0000235 Address so_addr;
236 // Attempt to resolve our load address if possible, though it is ok if
237 // it doesn't resolve to section/offset.
238
Greg Clayton33ed1702010-08-24 00:45:41 +0000239 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000240 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000241 if (!so_addr.IsValid())
242 {
243 // The address didn't resolve, so just set this as an absolute address
244 so_addr.SetOffset (addr);
245 }
246 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000247 return bp_sp;
248}
249
250BreakpointSP
251Target::CreateBreakpoint (Address &addr, bool internal)
252{
253 TargetSP target_sp = this->GetSP();
254 SearchFilterSP filter_sp(new SearchFilter (target_sp));
255 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
256 return CreateBreakpoint (filter_sp, resolver_sp, internal);
257}
258
259BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000260Target::CreateBreakpoint (const FileSpecList *containingModules,
261 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000262 const char *func_name,
263 uint32_t func_name_type_mask,
264 bool internal,
265 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000266{
Greg Clayton12bec712010-06-28 21:30:43 +0000267 BreakpointSP bp_sp;
268 if (func_name)
269 {
Jim Inghamd6d47972011-09-23 00:54:11 +0000270 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000271
272 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
273 func_name,
274 func_name_type_mask,
275 Breakpoint::Exact,
276 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000277 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
278 }
279 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000280}
281
282
283SearchFilterSP
284Target::GetSearchFilterForModule (const FileSpec *containingModule)
285{
286 SearchFilterSP filter_sp;
287 lldb::TargetSP target_sp = this->GetSP();
288 if (containingModule != NULL)
289 {
290 // TODO: We should look into sharing module based search filters
291 // across many breakpoints like we do for the simple target based one
292 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
293 }
294 else
295 {
296 if (m_search_filter_sp.get() == NULL)
297 m_search_filter_sp.reset (new SearchFilter (target_sp));
298 filter_sp = m_search_filter_sp;
299 }
300 return filter_sp;
301}
302
Jim Ingham03c8ee52011-09-21 01:17:13 +0000303SearchFilterSP
304Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
305{
306 SearchFilterSP filter_sp;
307 lldb::TargetSP target_sp = this->GetSP();
308 if (containingModules && containingModules->GetSize() != 0)
309 {
310 // TODO: We should look into sharing module based search filters
311 // across many breakpoints like we do for the simple target based one
312 filter_sp.reset (new SearchFilterByModuleList (target_sp, *containingModules));
313 }
314 else
315 {
316 if (m_search_filter_sp.get() == NULL)
317 m_search_filter_sp.reset (new SearchFilter (target_sp));
318 filter_sp = m_search_filter_sp;
319 }
320 return filter_sp;
321}
322
Jim Inghamd6d47972011-09-23 00:54:11 +0000323SearchFilterSP
324Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles)
325{
326 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
327 return GetSearchFilterForModuleList(containingModules);
328
329 SearchFilterSP filter_sp;
330 lldb::TargetSP target_sp = this->GetSP();
331 if (containingModules == NULL)
332 {
333 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
334 // but that will take a little reworking.
335
336 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, FileSpecList(), *containingSourceFiles));
337 }
338 else
339 {
340 filter_sp.reset (new SearchFilterByModuleListAndCU (target_sp, *containingModules, *containingSourceFiles));
341 }
342 return filter_sp;
343}
344
Chris Lattner24943d22010-06-08 16:52:24 +0000345BreakpointSP
Jim Inghamd6d47972011-09-23 00:54:11 +0000346Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
347 const FileSpecList *containingSourceFiles,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000348 RegularExpression &func_regex,
349 bool internal,
350 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000351{
Jim Inghamd6d47972011-09-23 00:54:11 +0000352 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000353 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
354 func_regex,
355 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000356
357 return CreateBreakpoint (filter_sp, resolver_sp, internal);
358}
359
360BreakpointSP
361Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
362{
363 BreakpointSP bp_sp;
364 if (filter_sp && resolver_sp)
365 {
366 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
367 resolver_sp->SetBreakpoint (bp_sp.get());
368
369 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000370 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000371 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000372 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000373
Greg Claytone005f2c2010-11-06 01:53:30 +0000374 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000375 if (log)
376 {
377 StreamString s;
378 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
379 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
380 }
381
Chris Lattner24943d22010-06-08 16:52:24 +0000382 bp_sp->ResolveBreakpoint();
383 }
Jim Inghamd1686902010-10-14 23:45:03 +0000384
385 if (!internal && bp_sp)
386 {
387 m_last_created_breakpoint = bp_sp;
388 }
389
Chris Lattner24943d22010-06-08 16:52:24 +0000390 return bp_sp;
391}
392
Johnny Chenda5a8022011-09-20 23:28:55 +0000393bool
394Target::ProcessIsValid()
395{
396 return (m_process_sp && m_process_sp->IsAlive());
397}
398
Johnny Chen87ff53b2011-09-14 00:26:03 +0000399// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
400// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen34bbf852011-09-12 23:38:44 +0000401WatchpointLocationSP
402Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
403{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000404 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
405 if (log)
406 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
407 __FUNCTION__, addr, size, type);
408
Johnny Chen34bbf852011-09-12 23:38:44 +0000409 WatchpointLocationSP wp_loc_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000410 if (!ProcessIsValid())
Johnny Chen61286a52011-09-13 18:30:59 +0000411 return wp_loc_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000412 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen34bbf852011-09-12 23:38:44 +0000413 return wp_loc_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000414
Johnny Chen87ff53b2011-09-14 00:26:03 +0000415 // Currently we only support one watchpoint location per address, with total
416 // number of watchpoint locations limited by the hardware which the inferior
417 // is running on.
Johnny Chen69b6ec82011-09-13 23:29:31 +0000418 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
419 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 Chen22a56cc2011-09-14 22:20:15 +0000425 // Return the existing watchpoint location if both size and type match.
426 if (size == old_size && type == old_type) {
427 wp_loc_sp = matched_sp;
428 wp_loc_sp->SetEnabled(false);
429 } else {
430 // Nil the matched watchpoint location; we will be creating a new one.
431 m_process_sp->DisableWatchpoint(matched_sp.get());
432 m_watchpoint_location_list.Remove(matched_sp->GetID());
433 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000434 }
435
Johnny Chen22a56cc2011-09-14 22:20:15 +0000436 if (!wp_loc_sp) {
437 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
438 if (!new_loc) {
439 printf("WatchpointLocation ctor failed, out of memory?\n");
440 return wp_loc_sp;
441 }
442 new_loc->SetWatchpointType(type);
Johnny Chen096c2932011-09-26 22:40:50 +0000443 new_loc->SetTarget(this);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000444 wp_loc_sp.reset(new_loc);
445 m_watchpoint_location_list.Add(wp_loc_sp);
446 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000447
Johnny Chen5b2fc572011-09-14 20:23:45 +0000448 Error rc = m_process_sp->EnableWatchpoint(wp_loc_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",
453 wp_loc_sp->GetID());
454
Johnny Chen22a56cc2011-09-14 22:20:15 +0000455 if (rc.Fail()) wp_loc_sp.reset();
Johnny Chen9bf11992011-09-13 01:15:36 +0000456 return wp_loc_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000457}
458
Chris Lattner24943d22010-06-08 16:52:24 +0000459void
460Target::RemoveAllBreakpoints (bool internal_also)
461{
Greg Claytone005f2c2010-11-06 01:53:30 +0000462 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000463 if (log)
464 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
465
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000466 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000467 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000468 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000469
470 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000471}
472
473void
474Target::DisableAllBreakpoints (bool internal_also)
475{
Greg Claytone005f2c2010-11-06 01:53:30 +0000476 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000477 if (log)
478 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
479
480 m_breakpoint_list.SetEnabledAll (false);
481 if (internal_also)
482 m_internal_breakpoint_list.SetEnabledAll (false);
483}
484
485void
486Target::EnableAllBreakpoints (bool internal_also)
487{
Greg Claytone005f2c2010-11-06 01:53:30 +0000488 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000489 if (log)
490 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
491
492 m_breakpoint_list.SetEnabledAll (true);
493 if (internal_also)
494 m_internal_breakpoint_list.SetEnabledAll (true);
495}
496
497bool
498Target::RemoveBreakpointByID (break_id_t break_id)
499{
Greg Claytone005f2c2010-11-06 01:53:30 +0000500 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000501 if (log)
502 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
503
504 if (DisableBreakpointByID (break_id))
505 {
506 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000507 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000508 else
Jim Inghamd1686902010-10-14 23:45:03 +0000509 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000510 if (m_last_created_breakpoint)
511 {
512 if (m_last_created_breakpoint->GetID() == break_id)
513 m_last_created_breakpoint.reset();
514 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000515 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000516 }
Chris Lattner24943d22010-06-08 16:52:24 +0000517 return true;
518 }
519 return false;
520}
521
522bool
523Target::DisableBreakpointByID (break_id_t break_id)
524{
Greg Claytone005f2c2010-11-06 01:53:30 +0000525 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000526 if (log)
527 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
528
529 BreakpointSP bp_sp;
530
531 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
532 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
533 else
534 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
535 if (bp_sp)
536 {
537 bp_sp->SetEnabled (false);
538 return true;
539 }
540 return false;
541}
542
543bool
544Target::EnableBreakpointByID (break_id_t break_id)
545{
Greg Claytone005f2c2010-11-06 01:53:30 +0000546 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000547 if (log)
548 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
549 __FUNCTION__,
550 break_id,
551 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
552
553 BreakpointSP bp_sp;
554
555 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
556 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
557 else
558 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
559
560 if (bp_sp)
561 {
562 bp_sp->SetEnabled (true);
563 return true;
564 }
565 return false;
566}
567
Johnny Chenc86582f2011-09-23 21:21:43 +0000568// The flag 'end_to_end', default to true, signifies that the operation is
569// performed end to end, for both the debugger and the debuggee.
570
571// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
572// for end to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000573bool
Johnny Chenc86582f2011-09-23 21:21:43 +0000574Target::RemoveAllWatchpointLocations (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000575{
576 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
577 if (log)
578 log->Printf ("Target::%s\n", __FUNCTION__);
579
Johnny Chenc86582f2011-09-23 21:21:43 +0000580 if (!end_to_end) {
581 m_watchpoint_location_list.RemoveAll();
582 return true;
583 }
584
585 // Otherwise, it's an end to end operation.
586
Johnny Chenda5a8022011-09-20 23:28:55 +0000587 if (!ProcessIsValid())
588 return false;
589
590 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
591 for (size_t i = 0; i < num_watchpoints; ++i)
592 {
593 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
594 if (!wp_loc_sp)
595 return false;
596
597 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
598 if (rc.Fail())
599 return false;
600 }
601 m_watchpoint_location_list.RemoveAll ();
602 return true; // Success!
603}
604
Johnny Chenc86582f2011-09-23 21:21:43 +0000605// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
606// for end to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000607bool
Johnny Chenc86582f2011-09-23 21:21:43 +0000608Target::DisableAllWatchpointLocations (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000609{
610 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
611 if (log)
612 log->Printf ("Target::%s\n", __FUNCTION__);
613
Johnny Chenc86582f2011-09-23 21:21:43 +0000614 if (!end_to_end) {
615 m_watchpoint_location_list.SetEnabledAll(false);
616 return true;
617 }
618
619 // Otherwise, it's an end to end operation.
620
Johnny Chenda5a8022011-09-20 23:28:55 +0000621 if (!ProcessIsValid())
622 return false;
623
624 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
625 for (size_t i = 0; i < num_watchpoints; ++i)
626 {
627 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
628 if (!wp_loc_sp)
629 return false;
630
631 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
632 if (rc.Fail())
633 return false;
634 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000635 return true; // Success!
636}
637
Johnny Chenc86582f2011-09-23 21:21:43 +0000638// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list
639// for end to end operations.
Johnny Chenda5a8022011-09-20 23:28:55 +0000640bool
Johnny Chenc86582f2011-09-23 21:21:43 +0000641Target::EnableAllWatchpointLocations (bool end_to_end)
Johnny Chenda5a8022011-09-20 23:28:55 +0000642{
643 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
644 if (log)
645 log->Printf ("Target::%s\n", __FUNCTION__);
646
Johnny Chenc86582f2011-09-23 21:21:43 +0000647 if (!end_to_end) {
648 m_watchpoint_location_list.SetEnabledAll(true);
649 return true;
650 }
651
652 // Otherwise, it's an end to end operation.
653
Johnny Chenda5a8022011-09-20 23:28:55 +0000654 if (!ProcessIsValid())
655 return false;
656
657 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
658 for (size_t i = 0; i < num_watchpoints; ++i)
659 {
660 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
661 if (!wp_loc_sp)
662 return false;
663
664 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
665 if (rc.Fail())
666 return false;
667 }
Johnny Chenda5a8022011-09-20 23:28:55 +0000668 return true; // Success!
669}
670
Johnny Chenc86582f2011-09-23 21:21:43 +0000671// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000672bool
673Target::DisableWatchpointLocationByID (lldb::watch_id_t watch_id)
674{
675 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
676 if (log)
677 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
678
679 if (!ProcessIsValid())
680 return false;
681
682 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
683 if (wp_loc_sp)
684 {
685 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000686 if (rc.Success())
687 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000688
Johnny Chen01acfa72011-09-22 18:04:58 +0000689 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000690 }
691 return false;
692}
693
Johnny Chenc86582f2011-09-23 21:21:43 +0000694// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000695bool
696Target::EnableWatchpointLocationByID (lldb::watch_id_t watch_id)
697{
698 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
699 if (log)
700 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
701
702 if (!ProcessIsValid())
703 return false;
704
705 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
706 if (wp_loc_sp)
707 {
708 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen01acfa72011-09-22 18:04:58 +0000709 if (rc.Success())
710 return true;
Johnny Chenda5a8022011-09-20 23:28:55 +0000711
Johnny Chen01acfa72011-09-22 18:04:58 +0000712 // Else, fallthrough.
Johnny Chenda5a8022011-09-20 23:28:55 +0000713 }
714 return false;
715}
716
Johnny Chenc86582f2011-09-23 21:21:43 +0000717// Assumption: Caller holds the list mutex lock for m_watchpoint_location_list.
Johnny Chenda5a8022011-09-20 23:28:55 +0000718bool
719Target::RemoveWatchpointLocationByID (lldb::watch_id_t watch_id)
720{
721 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
722 if (log)
723 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
724
725 if (DisableWatchpointLocationByID (watch_id))
726 {
727 m_watchpoint_location_list.Remove(watch_id);
728 return true;
729 }
730 return false;
731}
732
Chris Lattner24943d22010-06-08 16:52:24 +0000733ModuleSP
734Target::GetExecutableModule ()
735{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000736 return m_images.GetModuleAtIndex(0);
737}
738
739Module*
740Target::GetExecutableModulePointer ()
741{
742 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000743}
744
745void
746Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
747{
748 m_images.Clear();
749 m_scratch_ast_context_ap.reset();
750
751 if (executable_sp.get())
752 {
753 Timer scoped_timer (__PRETTY_FUNCTION__,
754 "Target::SetExecutableModule (executable = '%s/%s')",
755 executable_sp->GetFileSpec().GetDirectory().AsCString(),
756 executable_sp->GetFileSpec().GetFilename().AsCString());
757
758 m_images.Append(executable_sp); // The first image is our exectuable file
759
Jim Ingham7508e732010-08-09 23:31:02 +0000760 // 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 +0000761 if (!m_arch.IsValid())
762 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000763
Chris Lattner24943d22010-06-08 16:52:24 +0000764 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000765 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000766 // Let's find the file & line for main and set the default source file from there.
767 if (!m_source_manager.DefaultFileAndLineSet())
768 {
769 SymbolContextList sc_list;
770 uint32_t num_matches;
771 ConstString main_name("main");
772 bool symbols_okay = false; // Force it to be a debug symbol.
773 bool append = false;
774 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
775 for (uint32_t idx = 0; idx < num_matches; idx++)
776 {
777 SymbolContext sc;
778 sc_list.GetContextAtIndex(idx, sc);
779 if (sc.line_entry.file)
780 {
781 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
782 break;
783 }
784 }
785 }
Chris Lattner24943d22010-06-08 16:52:24 +0000786
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000787 if (executable_objfile && get_dependent_files)
Chris Lattner24943d22010-06-08 16:52:24 +0000788 {
789 executable_objfile->GetDependentModules(dependent_files);
790 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
791 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000792 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
793 FileSpec platform_dependent_file_spec;
794 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000795 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000796 else
797 platform_dependent_file_spec = dependent_file_spec;
798
799 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000800 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000801 if (image_module_sp.get())
802 {
Chris Lattner24943d22010-06-08 16:52:24 +0000803 ObjectFile *objfile = image_module_sp->GetObjectFile();
804 if (objfile)
805 objfile->GetDependentModules(dependent_files);
806 }
807 }
808 }
809
Chris Lattner24943d22010-06-08 16:52:24 +0000810 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000811
812 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000813}
814
815
Jim Ingham7508e732010-08-09 23:31:02 +0000816bool
817Target::SetArchitecture (const ArchSpec &arch_spec)
818{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000819 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000820 {
821 // If we're setting the architecture to our current architecture, we
822 // don't need to do anything.
823 return true;
824 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000825 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000826 {
827 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000828 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000829 return true;
830 }
831 else
832 {
833 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000834 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000835 ModuleSP executable_sp = GetExecutableModule ();
836 m_images.Clear();
837 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000838 // Need to do something about unsetting breakpoints.
839
840 if (executable_sp)
841 {
842 FileSpec exec_file_spec = executable_sp->GetFileSpec();
843 Error error = ModuleList::GetSharedModule(exec_file_spec,
844 arch_spec,
845 NULL,
846 NULL,
847 0,
848 executable_sp,
849 NULL,
850 NULL);
851
852 if (!error.Fail() && executable_sp)
853 {
854 SetExecutableModule (executable_sp, true);
855 return true;
856 }
857 else
858 {
859 return false;
860 }
861 }
862 else
863 {
864 return false;
865 }
866 }
867}
Chris Lattner24943d22010-06-08 16:52:24 +0000868
Chris Lattner24943d22010-06-08 16:52:24 +0000869void
870Target::ModuleAdded (ModuleSP &module_sp)
871{
872 // A module is being added to this target for the first time
873 ModuleList module_list;
874 module_list.Append(module_sp);
875 ModulesDidLoad (module_list);
876}
877
878void
879Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
880{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000881 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000882 ModuleList module_list;
883 module_list.Append (old_module_sp);
884 ModulesDidUnload (module_list);
885 module_list.Clear ();
886 module_list.Append (new_module_sp);
887 ModulesDidLoad (module_list);
888}
889
890void
891Target::ModulesDidLoad (ModuleList &module_list)
892{
893 m_breakpoint_list.UpdateBreakpoints (module_list, true);
894 // TODO: make event data that packages up the module_list
895 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
896}
897
898void
899Target::ModulesDidUnload (ModuleList &module_list)
900{
901 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000902
903 // Remove the images from the target image list
904 m_images.Remove(module_list);
905
Chris Lattner24943d22010-06-08 16:52:24 +0000906 // TODO: make event data that packages up the module_list
907 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
908}
909
910size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000911Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
912{
913 const Section *section = addr.GetSection();
914 if (section && section->GetModule())
915 {
916 ObjectFile *objfile = section->GetModule()->GetObjectFile();
917 if (objfile)
918 {
919 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
920 addr.GetOffset(),
921 dst,
922 dst_len);
923 if (bytes_read > 0)
924 return bytes_read;
925 else
926 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
927 }
928 else
929 {
930 error.SetErrorString("address isn't from a object file");
931 }
932 }
933 else
934 {
935 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
936 }
937 return 0;
938}
939
940size_t
Enrico Granata91544802011-09-06 19:20:51 +0000941Target::ReadMemory (const Address& addr,
942 bool prefer_file_cache,
943 void *dst,
944 size_t dst_len,
945 Error &error,
946 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000947{
Chris Lattner24943d22010-06-08 16:52:24 +0000948 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000949
Enrico Granata91544802011-09-06 19:20:51 +0000950 // if we end up reading this from process memory, we will fill this
951 // with the actual load address
952 if (load_addr_ptr)
953 *load_addr_ptr = LLDB_INVALID_ADDRESS;
954
Greg Clayton26100dc2011-01-07 01:57:07 +0000955 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000956
957 addr_t load_addr = LLDB_INVALID_ADDRESS;
958 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000959 Address resolved_addr;
960 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000961 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000962 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000963 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000964 // No sections are loaded, so we must assume we are not running
965 // yet and anything we are given is a file address.
966 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
967 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000968 }
Greg Clayton70436352010-06-30 23:03:03 +0000969 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000970 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000971 // We have at least one section loaded. This can be becuase
972 // we have manually loaded some sections with "target modules load ..."
973 // or because we have have a live process that has sections loaded
974 // through the dynamic loader
975 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
976 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000977 }
Greg Clayton70436352010-06-30 23:03:03 +0000978 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000979 if (!resolved_addr.IsValid())
980 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000981
Greg Clayton9b82f862011-07-11 05:12:02 +0000982
Greg Clayton26100dc2011-01-07 01:57:07 +0000983 if (prefer_file_cache)
984 {
985 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
986 if (bytes_read > 0)
987 return bytes_read;
988 }
Greg Clayton70436352010-06-30 23:03:03 +0000989
Johnny Chenda5a8022011-09-20 23:28:55 +0000990 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +0000991 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000992 if (load_addr == LLDB_INVALID_ADDRESS)
993 load_addr = resolved_addr.GetLoadAddress (this);
994
Greg Clayton70436352010-06-30 23:03:03 +0000995 if (load_addr == LLDB_INVALID_ADDRESS)
996 {
997 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
998 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
999 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +00001000 resolved_addr.GetFileAddress(),
1001 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +00001002 else
1003 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
1004 }
1005 else
1006 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001007 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +00001008 if (bytes_read != dst_len)
1009 {
1010 if (error.Success())
1011 {
1012 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +00001013 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001014 else
Greg Clayton70436352010-06-30 23:03:03 +00001015 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +00001016 }
1017 }
Greg Clayton70436352010-06-30 23:03:03 +00001018 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +00001019 {
1020 if (load_addr_ptr)
1021 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +00001022 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +00001023 }
Greg Clayton70436352010-06-30 23:03:03 +00001024 // If the address is not section offset we have an address that
1025 // doesn't resolve to any address in any currently loaded shared
1026 // libaries and we failed to read memory so there isn't anything
1027 // more we can do. If it is section offset, we might be able to
1028 // read cached memory from the object file.
1029 if (!resolved_addr.IsSectionOffset())
1030 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001031 }
Chris Lattner24943d22010-06-08 16:52:24 +00001032 }
Greg Clayton70436352010-06-30 23:03:03 +00001033
Greg Clayton9b82f862011-07-11 05:12:02 +00001034 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +00001035 {
Greg Clayton26100dc2011-01-07 01:57:07 +00001036 // If we didn't already try and read from the object file cache, then
1037 // try it after failing to read from the process.
1038 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +00001039 }
1040 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001041}
1042
Greg Clayton7dd98df2011-07-12 17:06:17 +00001043size_t
1044Target::ReadScalarIntegerFromMemory (const Address& addr,
1045 bool prefer_file_cache,
1046 uint32_t byte_size,
1047 bool is_signed,
1048 Scalar &scalar,
1049 Error &error)
1050{
1051 uint64_t uval;
1052
1053 if (byte_size <= sizeof(uval))
1054 {
1055 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1056 if (bytes_read == byte_size)
1057 {
1058 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1059 uint32_t offset = 0;
1060 if (byte_size <= 4)
1061 scalar = data.GetMaxU32 (&offset, byte_size);
1062 else
1063 scalar = data.GetMaxU64 (&offset, byte_size);
1064
1065 if (is_signed)
1066 scalar.SignExtend(byte_size * 8);
1067 return bytes_read;
1068 }
1069 }
1070 else
1071 {
1072 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1073 }
1074 return 0;
1075}
1076
1077uint64_t
1078Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1079 bool prefer_file_cache,
1080 size_t integer_byte_size,
1081 uint64_t fail_value,
1082 Error &error)
1083{
1084 Scalar scalar;
1085 if (ReadScalarIntegerFromMemory (addr,
1086 prefer_file_cache,
1087 integer_byte_size,
1088 false,
1089 scalar,
1090 error))
1091 return scalar.ULongLong(fail_value);
1092 return fail_value;
1093}
1094
1095bool
1096Target::ReadPointerFromMemory (const Address& addr,
1097 bool prefer_file_cache,
1098 Error &error,
1099 Address &pointer_addr)
1100{
1101 Scalar scalar;
1102 if (ReadScalarIntegerFromMemory (addr,
1103 prefer_file_cache,
1104 m_arch.GetAddressByteSize(),
1105 false,
1106 scalar,
1107 error))
1108 {
1109 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1110 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1111 {
1112 if (m_section_load_list.IsEmpty())
1113 {
1114 // No sections are loaded, so we must assume we are not running
1115 // yet and anything we are given is a file address.
1116 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1117 }
1118 else
1119 {
1120 // We have at least one section loaded. This can be becuase
1121 // we have manually loaded some sections with "target modules load ..."
1122 // or because we have have a live process that has sections loaded
1123 // through the dynamic loader
1124 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1125 }
1126 // We weren't able to resolve the pointer value, so just return
1127 // an address with no section
1128 if (!pointer_addr.IsValid())
1129 pointer_addr.SetOffset (pointer_vm_addr);
1130 return true;
1131
1132 }
1133 }
1134 return false;
1135}
Chris Lattner24943d22010-06-08 16:52:24 +00001136
1137ModuleSP
1138Target::GetSharedModule
1139(
1140 const FileSpec& file_spec,
1141 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +00001142 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +00001143 const ConstString *object_name,
1144 off_t object_offset,
1145 Error *error_ptr
1146)
1147{
1148 // Don't pass in the UUID so we can tell if we have a stale value in our list
1149 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1150 bool did_create_module = false;
1151 ModuleSP module_sp;
1152
Chris Lattner24943d22010-06-08 16:52:24 +00001153 Error error;
1154
Greg Clayton24bc5d92011-03-30 18:16:51 +00001155 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +00001156 if (m_image_search_paths.GetSize())
1157 {
1158 FileSpec transformed_spec;
1159 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1160 {
1161 transformed_spec.GetFilename() = file_spec.GetFilename();
1162 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1163 }
1164 }
1165
Greg Clayton24bc5d92011-03-30 18:16:51 +00001166 // The platform is responsible for finding and caching an appropriate
1167 // module in the shared module cache.
1168 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001169 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001170 FileSpec platform_file_spec;
1171 error = m_platform_sp->GetSharedModule (file_spec,
1172 arch,
1173 uuid_ptr,
1174 object_name,
1175 object_offset,
1176 module_sp,
1177 &old_module_sp,
1178 &did_create_module);
1179 }
1180 else
1181 {
1182 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +00001183 }
1184
Greg Clayton24bc5d92011-03-30 18:16:51 +00001185 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +00001186 if (module_sp)
1187 {
1188 m_images.Append (module_sp);
1189 if (did_create_module)
1190 {
1191 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1192 ModuleUpdated(old_module_sp, module_sp);
1193 else
1194 ModuleAdded(module_sp);
1195 }
1196 }
1197 if (error_ptr)
1198 *error_ptr = error;
1199 return module_sp;
1200}
1201
1202
1203Target *
1204Target::CalculateTarget ()
1205{
1206 return this;
1207}
1208
1209Process *
1210Target::CalculateProcess ()
1211{
1212 return NULL;
1213}
1214
1215Thread *
1216Target::CalculateThread ()
1217{
1218 return NULL;
1219}
1220
1221StackFrame *
1222Target::CalculateStackFrame ()
1223{
1224 return NULL;
1225}
1226
1227void
Greg Claytona830adb2010-10-04 01:05:56 +00001228Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001229{
Greg Clayton567e7f32011-09-22 04:58:26 +00001230 exe_ctx.Clear();
1231 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001232}
1233
1234PathMappingList &
1235Target::GetImageSearchPathList ()
1236{
1237 return m_image_search_paths;
1238}
1239
1240void
1241Target::ImageSearchPathsChanged
1242(
1243 const PathMappingList &path_list,
1244 void *baton
1245)
1246{
1247 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001248 ModuleSP exe_module_sp (target->GetExecutableModule());
1249 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001250 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001251 target->m_images.Clear();
1252 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001253 }
1254}
1255
1256ClangASTContext *
1257Target::GetScratchClangASTContext()
1258{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001259 // Now see if we know the target triple, and if so, create our scratch AST context:
1260 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1261 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +00001262 return m_scratch_ast_context_ap.get();
1263}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001264
Greg Clayton990de7b2010-11-18 23:32:35 +00001265void
Caroline Tice2a456812011-03-10 22:14:10 +00001266Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001267{
Greg Clayton990de7b2010-11-18 23:32:35 +00001268 UserSettingsControllerSP &usc = GetSettingsController();
1269 usc.reset (new SettingsController);
1270 UserSettingsController::InitializeSettingsController (usc,
1271 SettingsController::global_settings_table,
1272 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001273
1274 // Now call SettingsInitialize() on each 'child' setting of Target
1275 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001276}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001277
Greg Clayton990de7b2010-11-18 23:32:35 +00001278void
Caroline Tice2a456812011-03-10 22:14:10 +00001279Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001280{
Caroline Tice2a456812011-03-10 22:14:10 +00001281
1282 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1283
1284 Process::SettingsTerminate ();
1285
1286 // Now terminate Target Settings.
1287
Greg Clayton990de7b2010-11-18 23:32:35 +00001288 UserSettingsControllerSP &usc = GetSettingsController();
1289 UserSettingsController::FinalizeSettingsController (usc);
1290 usc.reset();
1291}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001292
Greg Clayton990de7b2010-11-18 23:32:35 +00001293UserSettingsControllerSP &
1294Target::GetSettingsController ()
1295{
1296 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001297 return g_settings_controller;
1298}
1299
1300ArchSpec
1301Target::GetDefaultArchitecture ()
1302{
Greg Clayton940b1032011-02-23 00:35:02 +00001303 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1304
1305 if (settings_controller_sp)
1306 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1307 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001308}
1309
1310void
Greg Clayton940b1032011-02-23 00:35:02 +00001311Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001312{
Greg Clayton940b1032011-02-23 00:35:02 +00001313 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1314
1315 if (settings_controller_sp)
1316 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001317}
1318
Greg Claytona830adb2010-10-04 01:05:56 +00001319Target *
1320Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1321{
1322 // The target can either exist in the "process" of ExecutionContext, or in
1323 // the "target_sp" member of SymbolContext. This accessor helper function
1324 // will get the target from one of these locations.
1325
1326 Target *target = NULL;
1327 if (sc_ptr != NULL)
1328 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001329 if (target == NULL && exe_ctx_ptr)
1330 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001331 return target;
1332}
1333
1334
Caroline Tice1ebef442010-09-27 00:30:10 +00001335void
1336Target::UpdateInstanceName ()
1337{
1338 StreamString sstr;
1339
Greg Clayton5beb99d2011-08-11 02:48:45 +00001340 Module *exe_module = GetExecutableModulePointer();
1341 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001342 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001343 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001344 exe_module->GetFileSpec().GetFilename().AsCString(),
1345 exe_module->GetArchitecture().GetArchitectureName());
1346 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001347 }
1348}
1349
Sean Callanan77e93942010-10-29 00:29:03 +00001350const char *
1351Target::GetExpressionPrefixContentsAsCString ()
1352{
Greg Claytonff44ab42011-04-23 02:04:55 +00001353 if (m_expr_prefix_contents_sp)
1354 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1355 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001356}
1357
Greg Clayton427f2902010-12-14 02:59:59 +00001358ExecutionResults
1359Target::EvaluateExpression
1360(
1361 const char *expr_cstr,
1362 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001363 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton427f2902010-12-14 02:59:59 +00001364 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001365 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001366 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001367 lldb::ValueObjectSP &result_valobj_sp
1368)
1369{
1370 ExecutionResults execution_results = eExecutionSetupError;
1371
1372 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001373
1374 // We shouldn't run stop hooks in expressions.
1375 // Be sure to reset this if you return anywhere within this function.
1376 bool old_suppress_value = m_suppress_stop_hooks;
1377 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001378
1379 ExecutionContext exe_ctx;
1380 if (frame)
1381 {
1382 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001383 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001384 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001385 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1386 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001387 lldb::VariableSP var_sp;
1388 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1389 use_dynamic,
1390 expr_path_options,
1391 var_sp,
1392 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001393 }
1394 else if (m_process_sp)
1395 {
1396 m_process_sp->CalculateExecutionContext(exe_ctx);
1397 }
1398 else
1399 {
1400 CalculateExecutionContext(exe_ctx);
1401 }
1402
1403 if (result_valobj_sp)
1404 {
1405 execution_results = eExecutionCompleted;
1406 // We got a result from the frame variable expression path above...
1407 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1408
1409 lldb::ValueObjectSP const_valobj_sp;
1410
1411 // Check in case our value is already a constant value
1412 if (result_valobj_sp->GetIsConstant())
1413 {
1414 const_valobj_sp = result_valobj_sp;
1415 const_valobj_sp->SetName (persistent_variable_name);
1416 }
1417 else
Jim Inghame41494a2011-04-16 00:01:13 +00001418 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001419 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001420 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001421 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001422 if (dynamic_sp)
1423 result_valobj_sp = dynamic_sp;
1424 }
1425
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001426 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001427 }
Greg Clayton427f2902010-12-14 02:59:59 +00001428
Sean Callanan6a925532011-01-13 08:53:35 +00001429 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1430
Greg Clayton427f2902010-12-14 02:59:59 +00001431 result_valobj_sp = const_valobj_sp;
1432
Sean Callanan6a925532011-01-13 08:53:35 +00001433 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1434 assert (clang_expr_variable_sp.get());
1435
1436 // Set flags and live data as appropriate
1437
1438 const Value &result_value = live_valobj_sp->GetValue();
1439
1440 switch (result_value.GetValueType())
1441 {
1442 case Value::eValueTypeHostAddress:
1443 case Value::eValueTypeFileAddress:
1444 // we don't do anything with these for now
1445 break;
1446 case Value::eValueTypeScalar:
1447 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1448 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1449 break;
1450 case Value::eValueTypeLoadAddress:
1451 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1452 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1453 break;
1454 }
Greg Clayton427f2902010-12-14 02:59:59 +00001455 }
1456 else
1457 {
1458 // Make sure we aren't just trying to see the value of a persistent
1459 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001460 lldb::ClangExpressionVariableSP persistent_var_sp;
1461 // Only check for persistent variables the expression starts with a '$'
1462 if (expr_cstr[0] == '$')
1463 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1464
Greg Clayton427f2902010-12-14 02:59:59 +00001465 if (persistent_var_sp)
1466 {
1467 result_valobj_sp = persistent_var_sp->GetValueObject ();
1468 execution_results = eExecutionCompleted;
1469 }
1470 else
1471 {
1472 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001473
Greg Clayton427f2902010-12-14 02:59:59 +00001474 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001475 execution_policy,
Sean Callanan6a925532011-01-13 08:53:35 +00001476 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001477 expr_cstr,
1478 prefix,
1479 result_valobj_sp);
1480 }
1481 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001482
1483 m_suppress_stop_hooks = old_suppress_value;
1484
Greg Clayton427f2902010-12-14 02:59:59 +00001485 return execution_results;
1486}
1487
Greg Claytonc0fa5332011-05-22 22:46:53 +00001488lldb::addr_t
1489Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1490{
1491 addr_t code_addr = load_addr;
1492 switch (m_arch.GetMachine())
1493 {
1494 case llvm::Triple::arm:
1495 case llvm::Triple::thumb:
1496 switch (addr_class)
1497 {
1498 case eAddressClassData:
1499 case eAddressClassDebug:
1500 return LLDB_INVALID_ADDRESS;
1501
1502 case eAddressClassUnknown:
1503 case eAddressClassInvalid:
1504 case eAddressClassCode:
1505 case eAddressClassCodeAlternateISA:
1506 case eAddressClassRuntime:
1507 // Check if bit zero it no set?
1508 if ((code_addr & 1ull) == 0)
1509 {
1510 // Bit zero isn't set, check if the address is a multiple of 2?
1511 if (code_addr & 2ull)
1512 {
1513 // The address is a multiple of 2 so it must be thumb, set bit zero
1514 code_addr |= 1ull;
1515 }
1516 else if (addr_class == eAddressClassCodeAlternateISA)
1517 {
1518 // We checked the address and the address claims to be the alternate ISA
1519 // which means thumb, so set bit zero.
1520 code_addr |= 1ull;
1521 }
1522 }
1523 break;
1524 }
1525 break;
1526
1527 default:
1528 break;
1529 }
1530 return code_addr;
1531}
1532
1533lldb::addr_t
1534Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1535{
1536 addr_t opcode_addr = load_addr;
1537 switch (m_arch.GetMachine())
1538 {
1539 case llvm::Triple::arm:
1540 case llvm::Triple::thumb:
1541 switch (addr_class)
1542 {
1543 case eAddressClassData:
1544 case eAddressClassDebug:
1545 return LLDB_INVALID_ADDRESS;
1546
1547 case eAddressClassInvalid:
1548 case eAddressClassUnknown:
1549 case eAddressClassCode:
1550 case eAddressClassCodeAlternateISA:
1551 case eAddressClassRuntime:
1552 opcode_addr &= ~(1ull);
1553 break;
1554 }
1555 break;
1556
1557 default:
1558 break;
1559 }
1560 return opcode_addr;
1561}
1562
Jim Inghamd60d94a2011-03-11 03:53:59 +00001563lldb::user_id_t
1564Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1565{
1566 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1567 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1568 m_stop_hooks[new_uid] = new_hook_sp;
1569 return new_uid;
1570}
1571
1572bool
1573Target::RemoveStopHookByID (lldb::user_id_t user_id)
1574{
1575 size_t num_removed;
1576 num_removed = m_stop_hooks.erase (user_id);
1577 if (num_removed == 0)
1578 return false;
1579 else
1580 return true;
1581}
1582
1583void
1584Target::RemoveAllStopHooks ()
1585{
1586 m_stop_hooks.clear();
1587}
1588
1589Target::StopHookSP
1590Target::GetStopHookByID (lldb::user_id_t user_id)
1591{
1592 StopHookSP found_hook;
1593
1594 StopHookCollection::iterator specified_hook_iter;
1595 specified_hook_iter = m_stop_hooks.find (user_id);
1596 if (specified_hook_iter != m_stop_hooks.end())
1597 found_hook = (*specified_hook_iter).second;
1598 return found_hook;
1599}
1600
1601bool
1602Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1603{
1604 StopHookCollection::iterator specified_hook_iter;
1605 specified_hook_iter = m_stop_hooks.find (user_id);
1606 if (specified_hook_iter == m_stop_hooks.end())
1607 return false;
1608
1609 (*specified_hook_iter).second->SetIsActive (active_state);
1610 return true;
1611}
1612
1613void
1614Target::SetAllStopHooksActiveState (bool active_state)
1615{
1616 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1617 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1618 {
1619 (*pos).second->SetIsActive (active_state);
1620 }
1621}
1622
1623void
1624Target::RunStopHooks ()
1625{
Jim Ingham3613ae12011-05-12 02:06:14 +00001626 if (m_suppress_stop_hooks)
1627 return;
1628
Jim Inghamd60d94a2011-03-11 03:53:59 +00001629 if (!m_process_sp)
1630 return;
1631
1632 if (m_stop_hooks.empty())
1633 return;
1634
1635 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1636
1637 // If there aren't any active stop hooks, don't bother either:
1638 bool any_active_hooks = false;
1639 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1640 {
1641 if ((*pos).second->IsActive())
1642 {
1643 any_active_hooks = true;
1644 break;
1645 }
1646 }
1647 if (!any_active_hooks)
1648 return;
1649
1650 CommandReturnObject result;
1651
1652 std::vector<ExecutionContext> exc_ctx_with_reasons;
1653 std::vector<SymbolContext> sym_ctx_with_reasons;
1654
1655 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1656 size_t num_threads = cur_threadlist.GetSize();
1657 for (size_t i = 0; i < num_threads; i++)
1658 {
1659 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1660 if (cur_thread_sp->ThreadStoppedForAReason())
1661 {
1662 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1663 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1664 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1665 }
1666 }
1667
1668 // If no threads stopped for a reason, don't run the stop-hooks.
1669 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1670 if (num_exe_ctx == 0)
1671 return;
1672
Jim Inghame5ed8e92011-06-02 23:58:26 +00001673 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1674 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001675
1676 bool keep_going = true;
1677 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001678 bool print_hook_header;
1679 bool print_thread_header;
1680
1681 if (num_exe_ctx == 1)
1682 print_thread_header = false;
1683 else
1684 print_thread_header = true;
1685
1686 if (m_stop_hooks.size() == 1)
1687 print_hook_header = false;
1688 else
1689 print_hook_header = true;
1690
Jim Inghamd60d94a2011-03-11 03:53:59 +00001691 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1692 {
1693 // result.Clear();
1694 StopHookSP cur_hook_sp = (*pos).second;
1695 if (!cur_hook_sp->IsActive())
1696 continue;
1697
1698 bool any_thread_matched = false;
1699 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1700 {
1701 if ((cur_hook_sp->GetSpecifier () == NULL
1702 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1703 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Clayton567e7f32011-09-22 04:58:26 +00001704 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001705 {
1706 if (!hooks_ran)
1707 {
Jim Inghamd60d94a2011-03-11 03:53:59 +00001708 hooks_ran = true;
1709 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001710 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001711 {
1712 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1713 any_thread_matched = true;
1714 }
1715
Jim Inghamc54840c2011-03-22 01:47:27 +00001716 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001717 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001718
1719 bool stop_on_continue = true;
1720 bool stop_on_error = true;
1721 bool echo_commands = false;
1722 bool print_results = true;
1723 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001724 &exc_ctx_with_reasons[i],
1725 stop_on_continue,
1726 stop_on_error,
1727 echo_commands,
1728 print_results,
1729 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001730
1731 // If the command started the target going again, we should bag out of
1732 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001733 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1734 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001735 {
1736 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1737 keep_going = false;
1738 }
1739 }
1740 }
1741 }
Jason Molenda850ac6e2011-09-23 00:42:55 +00001742
Caroline Tice4a348082011-05-02 20:41:46 +00001743 result.GetImmediateOutputStream()->Flush();
1744 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001745}
1746
Greg Claytonbbea1332011-07-08 00:48:09 +00001747bool
1748Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1749{
1750 bool changed = false;
1751 if (module)
1752 {
1753 ObjectFile *object_file = module->GetObjectFile();
1754 if (object_file)
1755 {
1756 SectionList *section_list = object_file->GetSectionList ();
1757 if (section_list)
1758 {
1759 // All sections listed in the dyld image info structure will all
1760 // either be fixed up already, or they will all be off by a single
1761 // slide amount that is determined by finding the first segment
1762 // that is at file offset zero which also has bytes (a file size
1763 // that is greater than zero) in the object file.
1764
1765 // Determine the slide amount (if any)
1766 const size_t num_sections = section_list->GetSize();
1767 size_t sect_idx = 0;
1768 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1769 {
1770 // Iterate through the object file sections to find the
1771 // first section that starts of file offset zero and that
1772 // has bytes in the file...
1773 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1774 if (section)
1775 {
1776 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1777 changed = true;
1778 }
1779 }
1780 }
1781 }
1782 }
1783 return changed;
1784}
1785
1786
Jim Inghamd60d94a2011-03-11 03:53:59 +00001787//--------------------------------------------------------------
1788// class Target::StopHook
1789//--------------------------------------------------------------
1790
1791
1792Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1793 UserID (uid),
1794 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001795 m_commands (),
1796 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001797 m_thread_spec_ap(NULL),
1798 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001799{
1800}
1801
1802Target::StopHook::StopHook (const StopHook &rhs) :
1803 UserID (rhs.GetID()),
1804 m_target_sp (rhs.m_target_sp),
1805 m_commands (rhs.m_commands),
1806 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001807 m_thread_spec_ap (NULL),
1808 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001809{
1810 if (rhs.m_thread_spec_ap.get() != NULL)
1811 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1812}
1813
1814
1815Target::StopHook::~StopHook ()
1816{
1817}
1818
1819void
1820Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1821{
1822 m_thread_spec_ap.reset (specifier);
1823}
1824
1825
1826void
1827Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1828{
1829 int indent_level = s->GetIndentLevel();
1830
1831 s->SetIndentLevel(indent_level + 2);
1832
1833 s->Printf ("Hook: %d\n", GetID());
1834 if (m_active)
1835 s->Indent ("State: enabled\n");
1836 else
1837 s->Indent ("State: disabled\n");
1838
1839 if (m_specifier_sp)
1840 {
1841 s->Indent();
1842 s->PutCString ("Specifier:\n");
1843 s->SetIndentLevel (indent_level + 4);
1844 m_specifier_sp->GetDescription (s, level);
1845 s->SetIndentLevel (indent_level + 2);
1846 }
1847
1848 if (m_thread_spec_ap.get() != NULL)
1849 {
1850 StreamString tmp;
1851 s->Indent("Thread:\n");
1852 m_thread_spec_ap->GetDescription (&tmp, level);
1853 s->SetIndentLevel (indent_level + 4);
1854 s->Indent (tmp.GetData());
1855 s->PutCString ("\n");
1856 s->SetIndentLevel (indent_level + 2);
1857 }
1858
1859 s->Indent ("Commands: \n");
1860 s->SetIndentLevel (indent_level + 4);
1861 uint32_t num_commands = m_commands.GetSize();
1862 for (uint32_t i = 0; i < num_commands; i++)
1863 {
1864 s->Indent(m_commands.GetStringAtIndex(i));
1865 s->PutCString ("\n");
1866 }
1867 s->SetIndentLevel (indent_level);
1868}
1869
1870
Caroline Tice5bc8c972010-09-20 20:44:43 +00001871//--------------------------------------------------------------
1872// class Target::SettingsController
1873//--------------------------------------------------------------
1874
1875Target::SettingsController::SettingsController () :
1876 UserSettingsController ("target", Debugger::GetSettingsController()),
1877 m_default_architecture ()
1878{
1879 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1880 InstanceSettings::GetDefaultName().AsCString()));
1881}
1882
1883Target::SettingsController::~SettingsController ()
1884{
1885}
1886
1887lldb::InstanceSettingsSP
1888Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1889{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001890 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1891 false,
1892 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001893 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1894 return new_settings_sp;
1895}
1896
Caroline Tice5bc8c972010-09-20 20:44:43 +00001897
Jim Inghame41494a2011-04-16 00:01:13 +00001898#define TSC_DEFAULT_ARCH "default-arch"
1899#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001900#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001901#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001902#define TSC_SOURCE_MAP "source-map"
Enrico Granata018921d2011-08-12 02:00:06 +00001903#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata91544802011-09-06 19:20:51 +00001904#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytond284b662011-02-18 01:44:25 +00001905
1906
1907static const ConstString &
1908GetSettingNameForDefaultArch ()
1909{
1910 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001911 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001912}
1913
Greg Claytond284b662011-02-18 01:44:25 +00001914static const ConstString &
1915GetSettingNameForExpressionPrefix ()
1916{
1917 static ConstString g_const_string (TSC_EXPR_PREFIX);
1918 return g_const_string;
1919}
1920
1921static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001922GetSettingNameForPreferDynamicValue ()
1923{
1924 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1925 return g_const_string;
1926}
1927
Greg Claytonff44ab42011-04-23 02:04:55 +00001928static const ConstString &
1929GetSettingNameForSourcePathMap ()
1930{
1931 static ConstString g_const_string (TSC_SOURCE_MAP);
1932 return g_const_string;
1933}
Greg Claytond284b662011-02-18 01:44:25 +00001934
Greg Clayton17cd9952011-04-22 03:55:06 +00001935static const ConstString &
1936GetSettingNameForSkipPrologue ()
1937{
1938 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1939 return g_const_string;
1940}
1941
Enrico Granata018921d2011-08-12 02:00:06 +00001942static const ConstString &
1943GetSettingNameForMaxChildren ()
1944{
1945 static ConstString g_const_string (TSC_MAX_CHILDREN);
1946 return g_const_string;
1947}
Greg Clayton17cd9952011-04-22 03:55:06 +00001948
Enrico Granata91544802011-09-06 19:20:51 +00001949static const ConstString &
1950GetSettingNameForMaxStringSummaryLength ()
1951{
1952 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1953 return g_const_string;
1954}
Greg Clayton17cd9952011-04-22 03:55:06 +00001955
Caroline Tice5bc8c972010-09-20 20:44:43 +00001956bool
1957Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1958 const char *index_value,
1959 const char *value,
1960 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001961 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001962 Error&err)
1963{
Greg Claytond284b662011-02-18 01:44:25 +00001964 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001965 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001966 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001967 if (!m_default_architecture.IsValid())
1968 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001969 }
1970 return true;
1971}
1972
1973
1974bool
1975Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1976 StringList &value,
1977 Error &err)
1978{
Greg Claytond284b662011-02-18 01:44:25 +00001979 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001980 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001981 // If the arch is invalid (the default), don't show a string for it
1982 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001983 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001984 return true;
1985 }
1986 else
1987 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1988
1989 return false;
1990}
1991
1992//--------------------------------------------------------------
1993// class TargetInstanceSettings
1994//--------------------------------------------------------------
1995
Greg Clayton638351a2010-12-04 00:10:17 +00001996TargetInstanceSettings::TargetInstanceSettings
1997(
1998 UserSettingsController &owner,
1999 bool live_instance,
2000 const char *name
2001) :
Greg Claytond284b662011-02-18 01:44:25 +00002002 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00002003 m_expr_prefix_file (),
2004 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00002005 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00002006 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00002007 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00002008 m_max_children_display(256),
2009 m_max_strlen_length(1024)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002010{
2011 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
2012 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
2013 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
2014 // This is true for CreateInstanceName() too.
2015
2016 if (GetInstanceName () == InstanceSettings::InvalidName())
2017 {
2018 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
2019 m_owner.RegisterInstanceSettings (this);
2020 }
2021
2022 if (live_instance)
2023 {
2024 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2025 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002026 }
2027}
2028
2029TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00002030 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
2031 m_expr_prefix_file (rhs.m_expr_prefix_file),
2032 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
2033 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
2034 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00002035 m_source_map (rhs.m_source_map),
Enrico Granata91544802011-09-06 19:20:51 +00002036 m_max_children_display(rhs.m_max_children_display),
2037 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002038{
2039 if (m_instance_name != InstanceSettings::GetDefaultName())
2040 {
2041 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
2042 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00002043 }
2044}
2045
2046TargetInstanceSettings::~TargetInstanceSettings ()
2047{
2048}
2049
2050TargetInstanceSettings&
2051TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2052{
2053 if (this != &rhs)
2054 {
2055 }
2056
2057 return *this;
2058}
2059
Caroline Tice5bc8c972010-09-20 20:44:43 +00002060void
2061TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2062 const char *index_value,
2063 const char *value,
2064 const ConstString &instance_name,
2065 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002066 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002067 Error &err,
2068 bool pending)
2069{
Greg Claytond284b662011-02-18 01:44:25 +00002070 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002071 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002072 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2073 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002074 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002075 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002076 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002077 default:
2078 break;
2079 case eVarSetOperationAssign:
2080 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002081 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002082 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2083 {
2084 err.SetErrorToGenericError ();
2085 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
2086 return;
2087 }
2088
2089 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
2090
2091 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
2092 {
2093 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
2094 m_expr_prefix_contents_sp.reset();
2095 }
Sean Callanan77e93942010-10-29 00:29:03 +00002096 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002097 break;
2098 case eVarSetOperationClear:
2099 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00002100 }
Sean Callanan77e93942010-10-29 00:29:03 +00002101 }
2102 }
Jim Inghame41494a2011-04-16 00:01:13 +00002103 else if (var_name == GetSettingNameForPreferDynamicValue())
2104 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002105 int new_value;
2106 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2107 if (err.Success())
2108 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002109 }
2110 else if (var_name == GetSettingNameForSkipPrologue())
2111 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002112 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2113 }
Enrico Granata018921d2011-08-12 02:00:06 +00002114 else if (var_name == GetSettingNameForMaxChildren())
2115 {
2116 bool ok;
2117 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2118 if (ok)
2119 m_max_children_display = new_value;
2120 }
Enrico Granata91544802011-09-06 19:20:51 +00002121 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2122 {
2123 bool ok;
2124 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2125 if (ok)
2126 m_max_strlen_length = new_value;
2127 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002128 else if (var_name == GetSettingNameForSourcePathMap ())
2129 {
2130 switch (op)
2131 {
2132 case eVarSetOperationReplace:
2133 case eVarSetOperationInsertBefore:
2134 case eVarSetOperationInsertAfter:
2135 case eVarSetOperationRemove:
2136 default:
2137 break;
2138 case eVarSetOperationAssign:
2139 m_source_map.Clear(true);
2140 // Fall through to append....
2141 case eVarSetOperationAppend:
2142 {
2143 Args args(value);
2144 const uint32_t argc = args.GetArgumentCount();
2145 if (argc & 1 || argc == 0)
2146 {
2147 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2148 }
2149 else
2150 {
2151 char resolved_new_path[PATH_MAX];
2152 FileSpec file_spec;
2153 const char *old_path;
2154 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2155 {
2156 const char *new_path = args.GetArgumentAtIndex(idx+1);
2157 assert (new_path); // We have an even number of paths, this shouldn't happen!
2158
2159 file_spec.SetFile(new_path, true);
2160 if (file_spec.Exists())
2161 {
2162 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2163 {
2164 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2165 return;
2166 }
2167 }
2168 else
2169 {
2170 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2171 return;
2172 }
2173 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2174 }
2175 }
2176 }
2177 break;
2178
2179 case eVarSetOperationClear:
2180 m_source_map.Clear(true);
2181 break;
2182 }
Jim Inghame41494a2011-04-16 00:01:13 +00002183 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002184}
2185
2186void
Greg Claytond284b662011-02-18 01:44:25 +00002187TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002188{
Sean Callanan77e93942010-10-29 00:29:03 +00002189 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2190
2191 if (!new_settings_ptr)
2192 return;
2193
Greg Claytonff44ab42011-04-23 02:04:55 +00002194 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
2195 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
2196 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
2197 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata018921d2011-08-12 02:00:06 +00002198 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata91544802011-09-06 19:20:51 +00002199 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002200}
2201
Caroline Ticebcb5b452010-09-20 21:37:42 +00002202bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002203TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2204 const ConstString &var_name,
2205 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002206 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002207{
Greg Claytond284b662011-02-18 01:44:25 +00002208 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002209 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002210 char path[PATH_MAX];
2211 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2212 if (path_len > 0)
2213 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002214 }
Jim Inghame41494a2011-04-16 00:01:13 +00002215 else if (var_name == GetSettingNameForPreferDynamicValue())
2216 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002217 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002218 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002219 else if (var_name == GetSettingNameForSkipPrologue())
2220 {
2221 if (m_skip_prologue)
2222 value.AppendString ("true");
2223 else
2224 value.AppendString ("false");
2225 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002226 else if (var_name == GetSettingNameForSourcePathMap ())
2227 {
2228 }
Enrico Granata018921d2011-08-12 02:00:06 +00002229 else if (var_name == GetSettingNameForMaxChildren())
2230 {
2231 StreamString count_str;
2232 count_str.Printf ("%d", m_max_children_display);
2233 value.AppendString (count_str.GetData());
2234 }
Enrico Granata91544802011-09-06 19:20:51 +00002235 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2236 {
2237 StreamString count_str;
2238 count_str.Printf ("%d", m_max_strlen_length);
2239 value.AppendString (count_str.GetData());
2240 }
Sean Callanan77e93942010-10-29 00:29:03 +00002241 else
2242 {
2243 if (err)
2244 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2245 return false;
2246 }
2247
2248 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002249}
2250
2251const ConstString
2252TargetInstanceSettings::CreateInstanceName ()
2253{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002254 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002255 static int instance_count = 1;
2256
Caroline Tice5bc8c972010-09-20 20:44:43 +00002257 sstr.Printf ("target_%d", instance_count);
2258 ++instance_count;
2259
2260 const ConstString ret_val (sstr.GetData());
2261 return ret_val;
2262}
2263
2264//--------------------------------------------------
2265// Target::SettingsController Variable Tables
2266//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002267OptionEnumValueElement
2268TargetInstanceSettings::g_dynamic_value_types[] =
2269{
Greg Clayton577fbc32011-05-30 00:39:48 +00002270{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2271{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2272{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002273{ 0, NULL, NULL }
2274};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002275
2276SettingEntry
2277Target::SettingsController::global_settings_table[] =
2278{
Greg Claytond284b662011-02-18 01:44:25 +00002279 // var-name var-type default enum init'd hidden help-text
2280 // ================= ================== =========== ==== ====== ====== =========================================================================
2281 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2282 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2283};
2284
Caroline Tice5bc8c972010-09-20 20:44:43 +00002285SettingEntry
2286Target::SettingsController::instance_settings_table[] =
2287{
Enrico Granata91544802011-09-06 19:20:51 +00002288 // var-name var-type default enum init'd hidden help-text
2289 // ================= ================== =============== ======================= ====== ====== =========================================================================
2290 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2291 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2292 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2293 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2294 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2295 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2296 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002297};