blob: 7fc9257af92eca167acf7a93f2dfab0af80a5c8e [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"
Greg Clayton427f2902010-12-14 02:59:59 +000021#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Core/Event.h"
23#include "lldb/Core/Log.h"
Caroline Tice4a348082011-05-02 20:41:46 +000024#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000026#include "lldb/Core/Timer.h"
27#include "lldb/Core/ValueObject.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000028#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000030#include "lldb/Interpreter/CommandInterpreter.h"
31#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032#include "lldb/lldb-private-log.h"
33#include "lldb/Symbol/ObjectFile.h"
34#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000035#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000036#include "lldb/Target/Thread.h"
37#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
39using namespace lldb;
40using namespace lldb_private;
41
42//----------------------------------------------------------------------
43// Target constructor
44//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000045Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
46 Broadcaster ("lldb.target"),
47 ExecutionContextScope (),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000048 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000049 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000050 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000051 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000052 m_arch (target_arch),
53 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000054 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000055 m_breakpoint_list (false),
56 m_internal_breakpoint_list (true),
Johnny Chen9a3c2a52011-09-06 20:05:25 +000057 m_watchpoint_location_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000058 m_process_sp (),
59 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000060 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000061 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000062 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000063 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000064 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000065 m_stop_hook_next_id (0),
66 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000067{
Greg Clayton49ce6822010-10-31 03:01:06 +000068 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
69 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
70 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
71
Greg Claytone005f2c2010-11-06 01:53:30 +000072 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000073 if (log)
74 log->Printf ("%p Target::Target()", this);
75}
76
77//----------------------------------------------------------------------
78// Destructor
79//----------------------------------------------------------------------
80Target::~Target()
81{
Greg Claytone005f2c2010-11-06 01:53:30 +000082 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000083 if (log)
84 log->Printf ("%p Target::~Target()", this);
85 DeleteCurrentProcess ();
86}
87
88void
Caroline Tice7826c882010-10-26 03:11:13 +000089Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000090{
Greg Clayton3fed8b92010-10-08 00:21:05 +000091// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000092 if (description_level != lldb::eDescriptionLevelBrief)
93 {
94 s->Indent();
95 s->PutCString("Target\n");
96 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000097 m_images.Dump(s);
98 m_breakpoint_list.Dump(s);
99 m_internal_breakpoint_list.Dump(s);
100 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000101 }
102 else
103 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000104 Module *exe_module = GetExecutableModulePointer();
105 if (exe_module)
106 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000107 else
108 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000109 }
Chris Lattner24943d22010-06-08 16:52:24 +0000110}
111
112void
113Target::DeleteCurrentProcess ()
114{
115 if (m_process_sp.get())
116 {
Greg Clayton49480b12010-09-14 23:52:43 +0000117 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000118 if (m_process_sp->IsAlive())
119 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000120
121 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000122
123 // Do any cleanup of the target we need to do between process instances.
124 // NB It is better to do this before destroying the process in case the
125 // clean up needs some help from the process.
126 m_breakpoint_list.ClearAllBreakpointSites();
127 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenda5a8022011-09-20 23:28:55 +0000128 m_watchpoint_location_list.RemoveAll();
Chris Lattner24943d22010-06-08 16:52:24 +0000129 m_process_sp.reset();
130 }
131}
132
133const lldb::ProcessSP &
134Target::CreateProcess (Listener &listener, const char *plugin_name)
135{
136 DeleteCurrentProcess ();
137 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
138 return m_process_sp;
139}
140
141const lldb::ProcessSP &
142Target::GetProcessSP () const
143{
144 return m_process_sp;
145}
146
147lldb::TargetSP
148Target::GetSP()
149{
Greg Clayton987c7eb2011-09-17 08:33:22 +0000150 // This object contains an instrusive ref count base class so we can
151 // easily make a shared pointer to this object
152 return TargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000153}
154
Greg Clayton153ccd72011-08-10 02:10:13 +0000155void
156Target::Destroy()
157{
158 Mutex::Locker locker (m_mutex);
159 DeleteCurrentProcess ();
160 m_platform_sp.reset();
161 m_arch.Clear();
162 m_images.Clear();
163 m_section_load_list.Clear();
164 const bool notify = false;
165 m_breakpoint_list.RemoveAll(notify);
166 m_internal_breakpoint_list.RemoveAll(notify);
167 m_last_created_breakpoint.reset();
168 m_search_filter_sp.reset();
169 m_image_search_paths.Clear(notify);
170 m_scratch_ast_context_ap.reset();
171 m_persistent_variables.Clear();
172 m_stop_hooks.clear();
173 m_stop_hook_next_id = 0;
174 m_suppress_stop_hooks = false;
175}
176
177
Chris Lattner24943d22010-06-08 16:52:24 +0000178BreakpointList &
179Target::GetBreakpointList(bool internal)
180{
181 if (internal)
182 return m_internal_breakpoint_list;
183 else
184 return m_breakpoint_list;
185}
186
187const BreakpointList &
188Target::GetBreakpointList(bool internal) const
189{
190 if (internal)
191 return m_internal_breakpoint_list;
192 else
193 return m_breakpoint_list;
194}
195
196BreakpointSP
197Target::GetBreakpointByID (break_id_t break_id)
198{
199 BreakpointSP bp_sp;
200
201 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
202 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
203 else
204 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
205
206 return bp_sp;
207}
208
209BreakpointSP
Jim Ingham03c8ee52011-09-21 01:17:13 +0000210Target::CreateBreakpoint (const FileSpecList *containingModules,
211 const FileSpec &file,
212 RegularExpression &source_regex,
213 bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000214{
Jim Ingham03c8ee52011-09-21 01:17:13 +0000215 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
216 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, file, source_regex));
217 return CreateBreakpoint (filter_sp, resolver_sp, internal);
218}
219
220
221BreakpointSP
222Target::CreateBreakpoint (const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
223{
224 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Chris Lattner24943d22010-06-08 16:52:24 +0000225 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
226 return CreateBreakpoint (filter_sp, resolver_sp, internal);
227}
228
229
230BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000231Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000232{
Chris Lattner24943d22010-06-08 16:52:24 +0000233 Address so_addr;
234 // Attempt to resolve our load address if possible, though it is ok if
235 // it doesn't resolve to section/offset.
236
Greg Clayton33ed1702010-08-24 00:45:41 +0000237 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000238 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000239 if (!so_addr.IsValid())
240 {
241 // The address didn't resolve, so just set this as an absolute address
242 so_addr.SetOffset (addr);
243 }
244 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000245 return bp_sp;
246}
247
248BreakpointSP
249Target::CreateBreakpoint (Address &addr, bool internal)
250{
251 TargetSP target_sp = this->GetSP();
252 SearchFilterSP filter_sp(new SearchFilter (target_sp));
253 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
254 return CreateBreakpoint (filter_sp, resolver_sp, internal);
255}
256
257BreakpointSP
Jim Ingham03c8ee52011-09-21 01:17:13 +0000258Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000259 const char *func_name,
260 uint32_t func_name_type_mask,
261 bool internal,
262 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000263{
Greg Clayton12bec712010-06-28 21:30:43 +0000264 BreakpointSP bp_sp;
265 if (func_name)
266 {
Jim Ingham03c8ee52011-09-21 01:17:13 +0000267 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000268
269 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
270 func_name,
271 func_name_type_mask,
272 Breakpoint::Exact,
273 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton12bec712010-06-28 21:30:43 +0000274 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
275 }
276 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000277}
278
279
280SearchFilterSP
281Target::GetSearchFilterForModule (const FileSpec *containingModule)
282{
283 SearchFilterSP filter_sp;
284 lldb::TargetSP target_sp = this->GetSP();
285 if (containingModule != NULL)
286 {
287 // TODO: We should look into sharing module based search filters
288 // across many breakpoints like we do for the simple target based one
289 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
290 }
291 else
292 {
293 if (m_search_filter_sp.get() == NULL)
294 m_search_filter_sp.reset (new SearchFilter (target_sp));
295 filter_sp = m_search_filter_sp;
296 }
297 return filter_sp;
298}
299
Jim Ingham03c8ee52011-09-21 01:17:13 +0000300SearchFilterSP
301Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
302{
303 SearchFilterSP filter_sp;
304 lldb::TargetSP target_sp = this->GetSP();
305 if (containingModules && containingModules->GetSize() != 0)
306 {
307 // TODO: We should look into sharing module based search filters
308 // across many breakpoints like we do for the simple target based one
309 filter_sp.reset (new SearchFilterByModuleList (target_sp, *containingModules));
310 }
311 else
312 {
313 if (m_search_filter_sp.get() == NULL)
314 m_search_filter_sp.reset (new SearchFilter (target_sp));
315 filter_sp = m_search_filter_sp;
316 }
317 return filter_sp;
318}
319
Chris Lattner24943d22010-06-08 16:52:24 +0000320BreakpointSP
Jim Ingham03c8ee52011-09-21 01:17:13 +0000321Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton7dd98df2011-07-12 17:06:17 +0000322 RegularExpression &func_regex,
323 bool internal,
324 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000325{
Jim Ingham03c8ee52011-09-21 01:17:13 +0000326 SearchFilterSP filter_sp(GetSearchFilterForModuleList (containingModules));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000327 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
328 func_regex,
329 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000330
331 return CreateBreakpoint (filter_sp, resolver_sp, internal);
332}
333
334BreakpointSP
335Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
336{
337 BreakpointSP bp_sp;
338 if (filter_sp && resolver_sp)
339 {
340 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
341 resolver_sp->SetBreakpoint (bp_sp.get());
342
343 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000344 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000345 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000346 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000347
Greg Claytone005f2c2010-11-06 01:53:30 +0000348 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000349 if (log)
350 {
351 StreamString s;
352 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
353 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
354 }
355
Chris Lattner24943d22010-06-08 16:52:24 +0000356 bp_sp->ResolveBreakpoint();
357 }
Jim Inghamd1686902010-10-14 23:45:03 +0000358
359 if (!internal && bp_sp)
360 {
361 m_last_created_breakpoint = bp_sp;
362 }
363
Chris Lattner24943d22010-06-08 16:52:24 +0000364 return bp_sp;
365}
366
Johnny Chenda5a8022011-09-20 23:28:55 +0000367bool
368Target::ProcessIsValid()
369{
370 return (m_process_sp && m_process_sp->IsAlive());
371}
372
Johnny Chen87ff53b2011-09-14 00:26:03 +0000373// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
374// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen34bbf852011-09-12 23:38:44 +0000375WatchpointLocationSP
376Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
377{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000378 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
379 if (log)
380 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
381 __FUNCTION__, addr, size, type);
382
Johnny Chen34bbf852011-09-12 23:38:44 +0000383 WatchpointLocationSP wp_loc_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000384 if (!ProcessIsValid())
Johnny Chen61286a52011-09-13 18:30:59 +0000385 return wp_loc_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000386 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen34bbf852011-09-12 23:38:44 +0000387 return wp_loc_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000388
Johnny Chen87ff53b2011-09-14 00:26:03 +0000389 // Currently we only support one watchpoint location per address, with total
390 // number of watchpoint locations limited by the hardware which the inferior
391 // is running on.
Johnny Chen69b6ec82011-09-13 23:29:31 +0000392 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
393 if (matched_sp)
394 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000395 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000396 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000397 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
398 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000399 // Return the existing watchpoint location if both size and type match.
400 if (size == old_size && type == old_type) {
401 wp_loc_sp = matched_sp;
402 wp_loc_sp->SetEnabled(false);
403 } else {
404 // Nil the matched watchpoint location; we will be creating a new one.
405 m_process_sp->DisableWatchpoint(matched_sp.get());
406 m_watchpoint_location_list.Remove(matched_sp->GetID());
407 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000408 }
409
Johnny Chen22a56cc2011-09-14 22:20:15 +0000410 if (!wp_loc_sp) {
411 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
412 if (!new_loc) {
413 printf("WatchpointLocation ctor failed, out of memory?\n");
414 return wp_loc_sp;
415 }
416 new_loc->SetWatchpointType(type);
417 wp_loc_sp.reset(new_loc);
418 m_watchpoint_location_list.Add(wp_loc_sp);
419 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000420
Johnny Chen5b2fc572011-09-14 20:23:45 +0000421 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000422 if (log)
423 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
424 __FUNCTION__,
425 rc.Success() ? "succeeded" : "failed",
426 wp_loc_sp->GetID());
427
Johnny Chen22a56cc2011-09-14 22:20:15 +0000428 if (rc.Fail()) wp_loc_sp.reset();
Johnny Chen9bf11992011-09-13 01:15:36 +0000429 return wp_loc_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000430}
431
Chris Lattner24943d22010-06-08 16:52:24 +0000432void
433Target::RemoveAllBreakpoints (bool internal_also)
434{
Greg Claytone005f2c2010-11-06 01:53:30 +0000435 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000436 if (log)
437 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
438
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000439 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000440 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000441 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000442
443 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000444}
445
446void
447Target::DisableAllBreakpoints (bool internal_also)
448{
Greg Claytone005f2c2010-11-06 01:53:30 +0000449 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000450 if (log)
451 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
452
453 m_breakpoint_list.SetEnabledAll (false);
454 if (internal_also)
455 m_internal_breakpoint_list.SetEnabledAll (false);
456}
457
458void
459Target::EnableAllBreakpoints (bool internal_also)
460{
Greg Claytone005f2c2010-11-06 01:53:30 +0000461 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000462 if (log)
463 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
464
465 m_breakpoint_list.SetEnabledAll (true);
466 if (internal_also)
467 m_internal_breakpoint_list.SetEnabledAll (true);
468}
469
470bool
471Target::RemoveBreakpointByID (break_id_t break_id)
472{
Greg Claytone005f2c2010-11-06 01:53:30 +0000473 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000474 if (log)
475 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
476
477 if (DisableBreakpointByID (break_id))
478 {
479 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000480 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000481 else
Jim Inghamd1686902010-10-14 23:45:03 +0000482 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000483 if (m_last_created_breakpoint)
484 {
485 if (m_last_created_breakpoint->GetID() == break_id)
486 m_last_created_breakpoint.reset();
487 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000488 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000489 }
Chris Lattner24943d22010-06-08 16:52:24 +0000490 return true;
491 }
492 return false;
493}
494
495bool
496Target::DisableBreakpointByID (break_id_t break_id)
497{
Greg Claytone005f2c2010-11-06 01:53:30 +0000498 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000499 if (log)
500 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
501
502 BreakpointSP bp_sp;
503
504 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
505 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
506 else
507 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
508 if (bp_sp)
509 {
510 bp_sp->SetEnabled (false);
511 return true;
512 }
513 return false;
514}
515
516bool
517Target::EnableBreakpointByID (break_id_t break_id)
518{
Greg Claytone005f2c2010-11-06 01:53:30 +0000519 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000520 if (log)
521 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
522 __FUNCTION__,
523 break_id,
524 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
525
526 BreakpointSP bp_sp;
527
528 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
529 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
530 else
531 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
532
533 if (bp_sp)
534 {
535 bp_sp->SetEnabled (true);
536 return true;
537 }
538 return false;
539}
540
Johnny Chenda5a8022011-09-20 23:28:55 +0000541// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
542bool
543Target::RemoveAllWatchpointLocations ()
544{
545 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
546 if (log)
547 log->Printf ("Target::%s\n", __FUNCTION__);
548
549 if (!ProcessIsValid())
550 return false;
551
552 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
553 for (size_t i = 0; i < num_watchpoints; ++i)
554 {
555 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
556 if (!wp_loc_sp)
557 return false;
558
559 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
560 if (rc.Fail())
561 return false;
562 }
563 m_watchpoint_location_list.RemoveAll ();
564 return true; // Success!
565}
566
567// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
568bool
569Target::DisableAllWatchpointLocations ()
570{
571 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
572 if (log)
573 log->Printf ("Target::%s\n", __FUNCTION__);
574
575 if (!ProcessIsValid())
576 return false;
577
578 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
579 for (size_t i = 0; i < num_watchpoints; ++i)
580 {
581 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
582 if (!wp_loc_sp)
583 return false;
584
585 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
586 if (rc.Fail())
587 return false;
588 }
589 m_watchpoint_location_list.SetEnabledAll (false);
590 return true; // Success!
591}
592
593// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
594bool
595Target::EnableAllWatchpointLocations ()
596{
597 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
598 if (log)
599 log->Printf ("Target::%s\n", __FUNCTION__);
600
601 if (!ProcessIsValid())
602 return false;
603
604 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
605 for (size_t i = 0; i < num_watchpoints; ++i)
606 {
607 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
608 if (!wp_loc_sp)
609 return false;
610
611 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
612 if (rc.Fail())
613 return false;
614 }
615 m_watchpoint_location_list.SetEnabledAll (true);
616 return true; // Success!
617}
618
619// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
620bool
621Target::DisableWatchpointLocationByID (lldb::watch_id_t watch_id)
622{
623 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
624 if (log)
625 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
626
627 if (!ProcessIsValid())
628 return false;
629
630 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
631 if (wp_loc_sp)
632 {
633 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
634 if (rc.Fail())
635 return false;
636
637 wp_loc_sp->SetEnabled (false);
638 return true;
639 }
640 return false;
641}
642
643// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
644bool
645Target::EnableWatchpointLocationByID (lldb::watch_id_t watch_id)
646{
647 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
648 if (log)
649 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
650
651 if (!ProcessIsValid())
652 return false;
653
654 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
655 if (wp_loc_sp)
656 {
657 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
658 if (rc.Fail())
659 return false;
660
661 wp_loc_sp->SetEnabled (true);
662 return true;
663 }
664 return false;
665}
666
667// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
668bool
669Target::RemoveWatchpointLocationByID (lldb::watch_id_t watch_id)
670{
671 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
672 if (log)
673 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
674
675 if (DisableWatchpointLocationByID (watch_id))
676 {
677 m_watchpoint_location_list.Remove(watch_id);
678 return true;
679 }
680 return false;
681}
682
Chris Lattner24943d22010-06-08 16:52:24 +0000683ModuleSP
684Target::GetExecutableModule ()
685{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000686 return m_images.GetModuleAtIndex(0);
687}
688
689Module*
690Target::GetExecutableModulePointer ()
691{
692 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000693}
694
695void
696Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
697{
698 m_images.Clear();
699 m_scratch_ast_context_ap.reset();
700
701 if (executable_sp.get())
702 {
703 Timer scoped_timer (__PRETTY_FUNCTION__,
704 "Target::SetExecutableModule (executable = '%s/%s')",
705 executable_sp->GetFileSpec().GetDirectory().AsCString(),
706 executable_sp->GetFileSpec().GetFilename().AsCString());
707
708 m_images.Append(executable_sp); // The first image is our exectuable file
709
Jim Ingham7508e732010-08-09 23:31:02 +0000710 // 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 +0000711 if (!m_arch.IsValid())
712 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000713
Chris Lattner24943d22010-06-08 16:52:24 +0000714 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000715 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000716 // Let's find the file & line for main and set the default source file from there.
717 if (!m_source_manager.DefaultFileAndLineSet())
718 {
719 SymbolContextList sc_list;
720 uint32_t num_matches;
721 ConstString main_name("main");
722 bool symbols_okay = false; // Force it to be a debug symbol.
723 bool append = false;
724 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
725 for (uint32_t idx = 0; idx < num_matches; idx++)
726 {
727 SymbolContext sc;
728 sc_list.GetContextAtIndex(idx, sc);
729 if (sc.line_entry.file)
730 {
731 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
732 break;
733 }
734 }
735 }
Chris Lattner24943d22010-06-08 16:52:24 +0000736
737 if (executable_objfile)
738 {
739 executable_objfile->GetDependentModules(dependent_files);
740 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
741 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000742 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
743 FileSpec platform_dependent_file_spec;
744 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000745 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000746 else
747 platform_dependent_file_spec = dependent_file_spec;
748
749 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000750 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000751 if (image_module_sp.get())
752 {
Chris Lattner24943d22010-06-08 16:52:24 +0000753 ObjectFile *objfile = image_module_sp->GetObjectFile();
754 if (objfile)
755 objfile->GetDependentModules(dependent_files);
756 }
757 }
758 }
759
Chris Lattner24943d22010-06-08 16:52:24 +0000760 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000761
762 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000763}
764
765
Jim Ingham7508e732010-08-09 23:31:02 +0000766bool
767Target::SetArchitecture (const ArchSpec &arch_spec)
768{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000769 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000770 {
771 // If we're setting the architecture to our current architecture, we
772 // don't need to do anything.
773 return true;
774 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000775 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000776 {
777 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000778 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000779 return true;
780 }
781 else
782 {
783 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000784 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000785 ModuleSP executable_sp = GetExecutableModule ();
786 m_images.Clear();
787 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000788 // Need to do something about unsetting breakpoints.
789
790 if (executable_sp)
791 {
792 FileSpec exec_file_spec = executable_sp->GetFileSpec();
793 Error error = ModuleList::GetSharedModule(exec_file_spec,
794 arch_spec,
795 NULL,
796 NULL,
797 0,
798 executable_sp,
799 NULL,
800 NULL);
801
802 if (!error.Fail() && executable_sp)
803 {
804 SetExecutableModule (executable_sp, true);
805 return true;
806 }
807 else
808 {
809 return false;
810 }
811 }
812 else
813 {
814 return false;
815 }
816 }
817}
Chris Lattner24943d22010-06-08 16:52:24 +0000818
Chris Lattner24943d22010-06-08 16:52:24 +0000819void
820Target::ModuleAdded (ModuleSP &module_sp)
821{
822 // A module is being added to this target for the first time
823 ModuleList module_list;
824 module_list.Append(module_sp);
825 ModulesDidLoad (module_list);
826}
827
828void
829Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
830{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000831 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000832 ModuleList module_list;
833 module_list.Append (old_module_sp);
834 ModulesDidUnload (module_list);
835 module_list.Clear ();
836 module_list.Append (new_module_sp);
837 ModulesDidLoad (module_list);
838}
839
840void
841Target::ModulesDidLoad (ModuleList &module_list)
842{
843 m_breakpoint_list.UpdateBreakpoints (module_list, true);
844 // TODO: make event data that packages up the module_list
845 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
846}
847
848void
849Target::ModulesDidUnload (ModuleList &module_list)
850{
851 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000852
853 // Remove the images from the target image list
854 m_images.Remove(module_list);
855
Chris Lattner24943d22010-06-08 16:52:24 +0000856 // TODO: make event data that packages up the module_list
857 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
858}
859
860size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000861Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
862{
863 const Section *section = addr.GetSection();
864 if (section && section->GetModule())
865 {
866 ObjectFile *objfile = section->GetModule()->GetObjectFile();
867 if (objfile)
868 {
869 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
870 addr.GetOffset(),
871 dst,
872 dst_len);
873 if (bytes_read > 0)
874 return bytes_read;
875 else
876 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
877 }
878 else
879 {
880 error.SetErrorString("address isn't from a object file");
881 }
882 }
883 else
884 {
885 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
886 }
887 return 0;
888}
889
890size_t
Enrico Granata91544802011-09-06 19:20:51 +0000891Target::ReadMemory (const Address& addr,
892 bool prefer_file_cache,
893 void *dst,
894 size_t dst_len,
895 Error &error,
896 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000897{
Chris Lattner24943d22010-06-08 16:52:24 +0000898 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000899
Enrico Granata91544802011-09-06 19:20:51 +0000900 // if we end up reading this from process memory, we will fill this
901 // with the actual load address
902 if (load_addr_ptr)
903 *load_addr_ptr = LLDB_INVALID_ADDRESS;
904
Greg Clayton26100dc2011-01-07 01:57:07 +0000905 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000906
907 addr_t load_addr = LLDB_INVALID_ADDRESS;
908 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000909 Address resolved_addr;
910 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000911 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000912 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000913 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000914 // No sections are loaded, so we must assume we are not running
915 // yet and anything we are given is a file address.
916 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
917 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000918 }
Greg Clayton70436352010-06-30 23:03:03 +0000919 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000920 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000921 // We have at least one section loaded. This can be becuase
922 // we have manually loaded some sections with "target modules load ..."
923 // or because we have have a live process that has sections loaded
924 // through the dynamic loader
925 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
926 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000927 }
Greg Clayton70436352010-06-30 23:03:03 +0000928 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000929 if (!resolved_addr.IsValid())
930 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000931
Greg Clayton9b82f862011-07-11 05:12:02 +0000932
Greg Clayton26100dc2011-01-07 01:57:07 +0000933 if (prefer_file_cache)
934 {
935 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
936 if (bytes_read > 0)
937 return bytes_read;
938 }
Greg Clayton70436352010-06-30 23:03:03 +0000939
Johnny Chenda5a8022011-09-20 23:28:55 +0000940 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +0000941 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000942 if (load_addr == LLDB_INVALID_ADDRESS)
943 load_addr = resolved_addr.GetLoadAddress (this);
944
Greg Clayton70436352010-06-30 23:03:03 +0000945 if (load_addr == LLDB_INVALID_ADDRESS)
946 {
947 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
948 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
949 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +0000950 resolved_addr.GetFileAddress(),
951 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +0000952 else
953 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
954 }
955 else
956 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000957 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000958 if (bytes_read != dst_len)
959 {
960 if (error.Success())
961 {
962 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000963 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000964 else
Greg Clayton70436352010-06-30 23:03:03 +0000965 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 +0000966 }
967 }
Greg Clayton70436352010-06-30 23:03:03 +0000968 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +0000969 {
970 if (load_addr_ptr)
971 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +0000972 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +0000973 }
Greg Clayton70436352010-06-30 23:03:03 +0000974 // If the address is not section offset we have an address that
975 // doesn't resolve to any address in any currently loaded shared
976 // libaries and we failed to read memory so there isn't anything
977 // more we can do. If it is section offset, we might be able to
978 // read cached memory from the object file.
979 if (!resolved_addr.IsSectionOffset())
980 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000981 }
Chris Lattner24943d22010-06-08 16:52:24 +0000982 }
Greg Clayton70436352010-06-30 23:03:03 +0000983
Greg Clayton9b82f862011-07-11 05:12:02 +0000984 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000985 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000986 // If we didn't already try and read from the object file cache, then
987 // try it after failing to read from the process.
988 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000989 }
990 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000991}
992
Greg Clayton7dd98df2011-07-12 17:06:17 +0000993size_t
994Target::ReadScalarIntegerFromMemory (const Address& addr,
995 bool prefer_file_cache,
996 uint32_t byte_size,
997 bool is_signed,
998 Scalar &scalar,
999 Error &error)
1000{
1001 uint64_t uval;
1002
1003 if (byte_size <= sizeof(uval))
1004 {
1005 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1006 if (bytes_read == byte_size)
1007 {
1008 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
1009 uint32_t offset = 0;
1010 if (byte_size <= 4)
1011 scalar = data.GetMaxU32 (&offset, byte_size);
1012 else
1013 scalar = data.GetMaxU64 (&offset, byte_size);
1014
1015 if (is_signed)
1016 scalar.SignExtend(byte_size * 8);
1017 return bytes_read;
1018 }
1019 }
1020 else
1021 {
1022 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1023 }
1024 return 0;
1025}
1026
1027uint64_t
1028Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1029 bool prefer_file_cache,
1030 size_t integer_byte_size,
1031 uint64_t fail_value,
1032 Error &error)
1033{
1034 Scalar scalar;
1035 if (ReadScalarIntegerFromMemory (addr,
1036 prefer_file_cache,
1037 integer_byte_size,
1038 false,
1039 scalar,
1040 error))
1041 return scalar.ULongLong(fail_value);
1042 return fail_value;
1043}
1044
1045bool
1046Target::ReadPointerFromMemory (const Address& addr,
1047 bool prefer_file_cache,
1048 Error &error,
1049 Address &pointer_addr)
1050{
1051 Scalar scalar;
1052 if (ReadScalarIntegerFromMemory (addr,
1053 prefer_file_cache,
1054 m_arch.GetAddressByteSize(),
1055 false,
1056 scalar,
1057 error))
1058 {
1059 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1060 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1061 {
1062 if (m_section_load_list.IsEmpty())
1063 {
1064 // No sections are loaded, so we must assume we are not running
1065 // yet and anything we are given is a file address.
1066 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1067 }
1068 else
1069 {
1070 // We have at least one section loaded. This can be becuase
1071 // we have manually loaded some sections with "target modules load ..."
1072 // or because we have have a live process that has sections loaded
1073 // through the dynamic loader
1074 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1075 }
1076 // We weren't able to resolve the pointer value, so just return
1077 // an address with no section
1078 if (!pointer_addr.IsValid())
1079 pointer_addr.SetOffset (pointer_vm_addr);
1080 return true;
1081
1082 }
1083 }
1084 return false;
1085}
Chris Lattner24943d22010-06-08 16:52:24 +00001086
1087ModuleSP
1088Target::GetSharedModule
1089(
1090 const FileSpec& file_spec,
1091 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +00001092 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +00001093 const ConstString *object_name,
1094 off_t object_offset,
1095 Error *error_ptr
1096)
1097{
1098 // Don't pass in the UUID so we can tell if we have a stale value in our list
1099 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1100 bool did_create_module = false;
1101 ModuleSP module_sp;
1102
Chris Lattner24943d22010-06-08 16:52:24 +00001103 Error error;
1104
Greg Clayton24bc5d92011-03-30 18:16:51 +00001105 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +00001106 if (m_image_search_paths.GetSize())
1107 {
1108 FileSpec transformed_spec;
1109 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1110 {
1111 transformed_spec.GetFilename() = file_spec.GetFilename();
1112 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1113 }
1114 }
1115
Greg Clayton24bc5d92011-03-30 18:16:51 +00001116 // The platform is responsible for finding and caching an appropriate
1117 // module in the shared module cache.
1118 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001119 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001120 FileSpec platform_file_spec;
1121 error = m_platform_sp->GetSharedModule (file_spec,
1122 arch,
1123 uuid_ptr,
1124 object_name,
1125 object_offset,
1126 module_sp,
1127 &old_module_sp,
1128 &did_create_module);
1129 }
1130 else
1131 {
1132 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +00001133 }
1134
Greg Clayton24bc5d92011-03-30 18:16:51 +00001135 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +00001136 if (module_sp)
1137 {
1138 m_images.Append (module_sp);
1139 if (did_create_module)
1140 {
1141 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1142 ModuleUpdated(old_module_sp, module_sp);
1143 else
1144 ModuleAdded(module_sp);
1145 }
1146 }
1147 if (error_ptr)
1148 *error_ptr = error;
1149 return module_sp;
1150}
1151
1152
1153Target *
1154Target::CalculateTarget ()
1155{
1156 return this;
1157}
1158
1159Process *
1160Target::CalculateProcess ()
1161{
1162 return NULL;
1163}
1164
1165Thread *
1166Target::CalculateThread ()
1167{
1168 return NULL;
1169}
1170
1171StackFrame *
1172Target::CalculateStackFrame ()
1173{
1174 return NULL;
1175}
1176
1177void
Greg Claytona830adb2010-10-04 01:05:56 +00001178Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001179{
Greg Clayton567e7f32011-09-22 04:58:26 +00001180 exe_ctx.Clear();
1181 exe_ctx.SetTargetPtr(this);
Chris Lattner24943d22010-06-08 16:52:24 +00001182}
1183
1184PathMappingList &
1185Target::GetImageSearchPathList ()
1186{
1187 return m_image_search_paths;
1188}
1189
1190void
1191Target::ImageSearchPathsChanged
1192(
1193 const PathMappingList &path_list,
1194 void *baton
1195)
1196{
1197 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001198 ModuleSP exe_module_sp (target->GetExecutableModule());
1199 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001200 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001201 target->m_images.Clear();
1202 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001203 }
1204}
1205
1206ClangASTContext *
1207Target::GetScratchClangASTContext()
1208{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001209 // Now see if we know the target triple, and if so, create our scratch AST context:
1210 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1211 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +00001212 return m_scratch_ast_context_ap.get();
1213}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001214
Greg Clayton990de7b2010-11-18 23:32:35 +00001215void
Caroline Tice2a456812011-03-10 22:14:10 +00001216Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001217{
Greg Clayton990de7b2010-11-18 23:32:35 +00001218 UserSettingsControllerSP &usc = GetSettingsController();
1219 usc.reset (new SettingsController);
1220 UserSettingsController::InitializeSettingsController (usc,
1221 SettingsController::global_settings_table,
1222 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001223
1224 // Now call SettingsInitialize() on each 'child' setting of Target
1225 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001226}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001227
Greg Clayton990de7b2010-11-18 23:32:35 +00001228void
Caroline Tice2a456812011-03-10 22:14:10 +00001229Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001230{
Caroline Tice2a456812011-03-10 22:14:10 +00001231
1232 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1233
1234 Process::SettingsTerminate ();
1235
1236 // Now terminate Target Settings.
1237
Greg Clayton990de7b2010-11-18 23:32:35 +00001238 UserSettingsControllerSP &usc = GetSettingsController();
1239 UserSettingsController::FinalizeSettingsController (usc);
1240 usc.reset();
1241}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001242
Greg Clayton990de7b2010-11-18 23:32:35 +00001243UserSettingsControllerSP &
1244Target::GetSettingsController ()
1245{
1246 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001247 return g_settings_controller;
1248}
1249
1250ArchSpec
1251Target::GetDefaultArchitecture ()
1252{
Greg Clayton940b1032011-02-23 00:35:02 +00001253 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1254
1255 if (settings_controller_sp)
1256 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1257 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001258}
1259
1260void
Greg Clayton940b1032011-02-23 00:35:02 +00001261Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001262{
Greg Clayton940b1032011-02-23 00:35:02 +00001263 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1264
1265 if (settings_controller_sp)
1266 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001267}
1268
Greg Claytona830adb2010-10-04 01:05:56 +00001269Target *
1270Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1271{
1272 // The target can either exist in the "process" of ExecutionContext, or in
1273 // the "target_sp" member of SymbolContext. This accessor helper function
1274 // will get the target from one of these locations.
1275
1276 Target *target = NULL;
1277 if (sc_ptr != NULL)
1278 target = sc_ptr->target_sp.get();
Greg Clayton567e7f32011-09-22 04:58:26 +00001279 if (target == NULL && exe_ctx_ptr)
1280 target = exe_ctx_ptr->GetTargetPtr();
Greg Claytona830adb2010-10-04 01:05:56 +00001281 return target;
1282}
1283
1284
Caroline Tice1ebef442010-09-27 00:30:10 +00001285void
1286Target::UpdateInstanceName ()
1287{
1288 StreamString sstr;
1289
Greg Clayton5beb99d2011-08-11 02:48:45 +00001290 Module *exe_module = GetExecutableModulePointer();
1291 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001292 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001293 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001294 exe_module->GetFileSpec().GetFilename().AsCString(),
1295 exe_module->GetArchitecture().GetArchitectureName());
1296 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001297 }
1298}
1299
Sean Callanan77e93942010-10-29 00:29:03 +00001300const char *
1301Target::GetExpressionPrefixContentsAsCString ()
1302{
Greg Claytonff44ab42011-04-23 02:04:55 +00001303 if (m_expr_prefix_contents_sp)
1304 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1305 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001306}
1307
Greg Clayton427f2902010-12-14 02:59:59 +00001308ExecutionResults
1309Target::EvaluateExpression
1310(
1311 const char *expr_cstr,
1312 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001313 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton427f2902010-12-14 02:59:59 +00001314 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001315 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001316 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001317 lldb::ValueObjectSP &result_valobj_sp
1318)
1319{
1320 ExecutionResults execution_results = eExecutionSetupError;
1321
1322 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001323
1324 // We shouldn't run stop hooks in expressions.
1325 // Be sure to reset this if you return anywhere within this function.
1326 bool old_suppress_value = m_suppress_stop_hooks;
1327 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001328
1329 ExecutionContext exe_ctx;
1330 if (frame)
1331 {
1332 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001333 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001334 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001335 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1336 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001337 lldb::VariableSP var_sp;
1338 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1339 use_dynamic,
1340 expr_path_options,
1341 var_sp,
1342 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001343 }
1344 else if (m_process_sp)
1345 {
1346 m_process_sp->CalculateExecutionContext(exe_ctx);
1347 }
1348 else
1349 {
1350 CalculateExecutionContext(exe_ctx);
1351 }
1352
1353 if (result_valobj_sp)
1354 {
1355 execution_results = eExecutionCompleted;
1356 // We got a result from the frame variable expression path above...
1357 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1358
1359 lldb::ValueObjectSP const_valobj_sp;
1360
1361 // Check in case our value is already a constant value
1362 if (result_valobj_sp->GetIsConstant())
1363 {
1364 const_valobj_sp = result_valobj_sp;
1365 const_valobj_sp->SetName (persistent_variable_name);
1366 }
1367 else
Jim Inghame41494a2011-04-16 00:01:13 +00001368 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001369 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001370 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001371 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001372 if (dynamic_sp)
1373 result_valobj_sp = dynamic_sp;
1374 }
1375
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001376 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001377 }
Greg Clayton427f2902010-12-14 02:59:59 +00001378
Sean Callanan6a925532011-01-13 08:53:35 +00001379 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1380
Greg Clayton427f2902010-12-14 02:59:59 +00001381 result_valobj_sp = const_valobj_sp;
1382
Sean Callanan6a925532011-01-13 08:53:35 +00001383 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1384 assert (clang_expr_variable_sp.get());
1385
1386 // Set flags and live data as appropriate
1387
1388 const Value &result_value = live_valobj_sp->GetValue();
1389
1390 switch (result_value.GetValueType())
1391 {
1392 case Value::eValueTypeHostAddress:
1393 case Value::eValueTypeFileAddress:
1394 // we don't do anything with these for now
1395 break;
1396 case Value::eValueTypeScalar:
1397 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1398 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1399 break;
1400 case Value::eValueTypeLoadAddress:
1401 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1402 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1403 break;
1404 }
Greg Clayton427f2902010-12-14 02:59:59 +00001405 }
1406 else
1407 {
1408 // Make sure we aren't just trying to see the value of a persistent
1409 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001410 lldb::ClangExpressionVariableSP persistent_var_sp;
1411 // Only check for persistent variables the expression starts with a '$'
1412 if (expr_cstr[0] == '$')
1413 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1414
Greg Clayton427f2902010-12-14 02:59:59 +00001415 if (persistent_var_sp)
1416 {
1417 result_valobj_sp = persistent_var_sp->GetValueObject ();
1418 execution_results = eExecutionCompleted;
1419 }
1420 else
1421 {
1422 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001423
Greg Clayton427f2902010-12-14 02:59:59 +00001424 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001425 execution_policy,
Sean Callanan6a925532011-01-13 08:53:35 +00001426 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001427 expr_cstr,
1428 prefix,
1429 result_valobj_sp);
1430 }
1431 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001432
1433 m_suppress_stop_hooks = old_suppress_value;
1434
Greg Clayton427f2902010-12-14 02:59:59 +00001435 return execution_results;
1436}
1437
Greg Claytonc0fa5332011-05-22 22:46:53 +00001438lldb::addr_t
1439Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1440{
1441 addr_t code_addr = load_addr;
1442 switch (m_arch.GetMachine())
1443 {
1444 case llvm::Triple::arm:
1445 case llvm::Triple::thumb:
1446 switch (addr_class)
1447 {
1448 case eAddressClassData:
1449 case eAddressClassDebug:
1450 return LLDB_INVALID_ADDRESS;
1451
1452 case eAddressClassUnknown:
1453 case eAddressClassInvalid:
1454 case eAddressClassCode:
1455 case eAddressClassCodeAlternateISA:
1456 case eAddressClassRuntime:
1457 // Check if bit zero it no set?
1458 if ((code_addr & 1ull) == 0)
1459 {
1460 // Bit zero isn't set, check if the address is a multiple of 2?
1461 if (code_addr & 2ull)
1462 {
1463 // The address is a multiple of 2 so it must be thumb, set bit zero
1464 code_addr |= 1ull;
1465 }
1466 else if (addr_class == eAddressClassCodeAlternateISA)
1467 {
1468 // We checked the address and the address claims to be the alternate ISA
1469 // which means thumb, so set bit zero.
1470 code_addr |= 1ull;
1471 }
1472 }
1473 break;
1474 }
1475 break;
1476
1477 default:
1478 break;
1479 }
1480 return code_addr;
1481}
1482
1483lldb::addr_t
1484Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1485{
1486 addr_t opcode_addr = load_addr;
1487 switch (m_arch.GetMachine())
1488 {
1489 case llvm::Triple::arm:
1490 case llvm::Triple::thumb:
1491 switch (addr_class)
1492 {
1493 case eAddressClassData:
1494 case eAddressClassDebug:
1495 return LLDB_INVALID_ADDRESS;
1496
1497 case eAddressClassInvalid:
1498 case eAddressClassUnknown:
1499 case eAddressClassCode:
1500 case eAddressClassCodeAlternateISA:
1501 case eAddressClassRuntime:
1502 opcode_addr &= ~(1ull);
1503 break;
1504 }
1505 break;
1506
1507 default:
1508 break;
1509 }
1510 return opcode_addr;
1511}
1512
Jim Inghamd60d94a2011-03-11 03:53:59 +00001513lldb::user_id_t
1514Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1515{
1516 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1517 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1518 m_stop_hooks[new_uid] = new_hook_sp;
1519 return new_uid;
1520}
1521
1522bool
1523Target::RemoveStopHookByID (lldb::user_id_t user_id)
1524{
1525 size_t num_removed;
1526 num_removed = m_stop_hooks.erase (user_id);
1527 if (num_removed == 0)
1528 return false;
1529 else
1530 return true;
1531}
1532
1533void
1534Target::RemoveAllStopHooks ()
1535{
1536 m_stop_hooks.clear();
1537}
1538
1539Target::StopHookSP
1540Target::GetStopHookByID (lldb::user_id_t user_id)
1541{
1542 StopHookSP found_hook;
1543
1544 StopHookCollection::iterator specified_hook_iter;
1545 specified_hook_iter = m_stop_hooks.find (user_id);
1546 if (specified_hook_iter != m_stop_hooks.end())
1547 found_hook = (*specified_hook_iter).second;
1548 return found_hook;
1549}
1550
1551bool
1552Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1553{
1554 StopHookCollection::iterator specified_hook_iter;
1555 specified_hook_iter = m_stop_hooks.find (user_id);
1556 if (specified_hook_iter == m_stop_hooks.end())
1557 return false;
1558
1559 (*specified_hook_iter).second->SetIsActive (active_state);
1560 return true;
1561}
1562
1563void
1564Target::SetAllStopHooksActiveState (bool active_state)
1565{
1566 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1567 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1568 {
1569 (*pos).second->SetIsActive (active_state);
1570 }
1571}
1572
1573void
1574Target::RunStopHooks ()
1575{
Jim Ingham3613ae12011-05-12 02:06:14 +00001576 if (m_suppress_stop_hooks)
1577 return;
1578
Jim Inghamd60d94a2011-03-11 03:53:59 +00001579 if (!m_process_sp)
1580 return;
1581
1582 if (m_stop_hooks.empty())
1583 return;
1584
1585 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1586
1587 // If there aren't any active stop hooks, don't bother either:
1588 bool any_active_hooks = false;
1589 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1590 {
1591 if ((*pos).second->IsActive())
1592 {
1593 any_active_hooks = true;
1594 break;
1595 }
1596 }
1597 if (!any_active_hooks)
1598 return;
1599
1600 CommandReturnObject result;
1601
1602 std::vector<ExecutionContext> exc_ctx_with_reasons;
1603 std::vector<SymbolContext> sym_ctx_with_reasons;
1604
1605 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1606 size_t num_threads = cur_threadlist.GetSize();
1607 for (size_t i = 0; i < num_threads; i++)
1608 {
1609 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1610 if (cur_thread_sp->ThreadStoppedForAReason())
1611 {
1612 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1613 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1614 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1615 }
1616 }
1617
1618 // If no threads stopped for a reason, don't run the stop-hooks.
1619 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1620 if (num_exe_ctx == 0)
1621 return;
1622
Jim Inghame5ed8e92011-06-02 23:58:26 +00001623 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1624 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001625
1626 bool keep_going = true;
1627 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001628 bool print_hook_header;
1629 bool print_thread_header;
1630
1631 if (num_exe_ctx == 1)
1632 print_thread_header = false;
1633 else
1634 print_thread_header = true;
1635
1636 if (m_stop_hooks.size() == 1)
1637 print_hook_header = false;
1638 else
1639 print_hook_header = true;
1640
Jim Inghamd60d94a2011-03-11 03:53:59 +00001641 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1642 {
1643 // result.Clear();
1644 StopHookSP cur_hook_sp = (*pos).second;
1645 if (!cur_hook_sp->IsActive())
1646 continue;
1647
1648 bool any_thread_matched = false;
1649 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1650 {
1651 if ((cur_hook_sp->GetSpecifier () == NULL
1652 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1653 && (cur_hook_sp->GetThreadSpecifier() == NULL
Greg Clayton567e7f32011-09-22 04:58:26 +00001654 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadPtr())))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001655 {
1656 if (!hooks_ran)
1657 {
Jim Inghamc54840c2011-03-22 01:47:27 +00001658 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001659 hooks_ran = true;
1660 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001661 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001662 {
1663 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1664 any_thread_matched = true;
1665 }
1666
Jim Inghamc54840c2011-03-22 01:47:27 +00001667 if (print_thread_header)
Greg Clayton567e7f32011-09-22 04:58:26 +00001668 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001669
1670 bool stop_on_continue = true;
1671 bool stop_on_error = true;
1672 bool echo_commands = false;
1673 bool print_results = true;
1674 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001675 &exc_ctx_with_reasons[i],
1676 stop_on_continue,
1677 stop_on_error,
1678 echo_commands,
1679 print_results,
1680 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001681
1682 // If the command started the target going again, we should bag out of
1683 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001684 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1685 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001686 {
1687 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1688 keep_going = false;
1689 }
1690 }
1691 }
1692 }
1693 if (hooks_ran)
1694 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice4a348082011-05-02 20:41:46 +00001695
1696 result.GetImmediateOutputStream()->Flush();
1697 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001698}
1699
Greg Claytonbbea1332011-07-08 00:48:09 +00001700bool
1701Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1702{
1703 bool changed = false;
1704 if (module)
1705 {
1706 ObjectFile *object_file = module->GetObjectFile();
1707 if (object_file)
1708 {
1709 SectionList *section_list = object_file->GetSectionList ();
1710 if (section_list)
1711 {
1712 // All sections listed in the dyld image info structure will all
1713 // either be fixed up already, or they will all be off by a single
1714 // slide amount that is determined by finding the first segment
1715 // that is at file offset zero which also has bytes (a file size
1716 // that is greater than zero) in the object file.
1717
1718 // Determine the slide amount (if any)
1719 const size_t num_sections = section_list->GetSize();
1720 size_t sect_idx = 0;
1721 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1722 {
1723 // Iterate through the object file sections to find the
1724 // first section that starts of file offset zero and that
1725 // has bytes in the file...
1726 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1727 if (section)
1728 {
1729 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1730 changed = true;
1731 }
1732 }
1733 }
1734 }
1735 }
1736 return changed;
1737}
1738
1739
Jim Inghamd60d94a2011-03-11 03:53:59 +00001740//--------------------------------------------------------------
1741// class Target::StopHook
1742//--------------------------------------------------------------
1743
1744
1745Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1746 UserID (uid),
1747 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001748 m_commands (),
1749 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001750 m_thread_spec_ap(NULL),
1751 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001752{
1753}
1754
1755Target::StopHook::StopHook (const StopHook &rhs) :
1756 UserID (rhs.GetID()),
1757 m_target_sp (rhs.m_target_sp),
1758 m_commands (rhs.m_commands),
1759 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001760 m_thread_spec_ap (NULL),
1761 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001762{
1763 if (rhs.m_thread_spec_ap.get() != NULL)
1764 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1765}
1766
1767
1768Target::StopHook::~StopHook ()
1769{
1770}
1771
1772void
1773Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1774{
1775 m_thread_spec_ap.reset (specifier);
1776}
1777
1778
1779void
1780Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1781{
1782 int indent_level = s->GetIndentLevel();
1783
1784 s->SetIndentLevel(indent_level + 2);
1785
1786 s->Printf ("Hook: %d\n", GetID());
1787 if (m_active)
1788 s->Indent ("State: enabled\n");
1789 else
1790 s->Indent ("State: disabled\n");
1791
1792 if (m_specifier_sp)
1793 {
1794 s->Indent();
1795 s->PutCString ("Specifier:\n");
1796 s->SetIndentLevel (indent_level + 4);
1797 m_specifier_sp->GetDescription (s, level);
1798 s->SetIndentLevel (indent_level + 2);
1799 }
1800
1801 if (m_thread_spec_ap.get() != NULL)
1802 {
1803 StreamString tmp;
1804 s->Indent("Thread:\n");
1805 m_thread_spec_ap->GetDescription (&tmp, level);
1806 s->SetIndentLevel (indent_level + 4);
1807 s->Indent (tmp.GetData());
1808 s->PutCString ("\n");
1809 s->SetIndentLevel (indent_level + 2);
1810 }
1811
1812 s->Indent ("Commands: \n");
1813 s->SetIndentLevel (indent_level + 4);
1814 uint32_t num_commands = m_commands.GetSize();
1815 for (uint32_t i = 0; i < num_commands; i++)
1816 {
1817 s->Indent(m_commands.GetStringAtIndex(i));
1818 s->PutCString ("\n");
1819 }
1820 s->SetIndentLevel (indent_level);
1821}
1822
1823
Caroline Tice5bc8c972010-09-20 20:44:43 +00001824//--------------------------------------------------------------
1825// class Target::SettingsController
1826//--------------------------------------------------------------
1827
1828Target::SettingsController::SettingsController () :
1829 UserSettingsController ("target", Debugger::GetSettingsController()),
1830 m_default_architecture ()
1831{
1832 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1833 InstanceSettings::GetDefaultName().AsCString()));
1834}
1835
1836Target::SettingsController::~SettingsController ()
1837{
1838}
1839
1840lldb::InstanceSettingsSP
1841Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1842{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001843 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1844 false,
1845 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001846 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1847 return new_settings_sp;
1848}
1849
Caroline Tice5bc8c972010-09-20 20:44:43 +00001850
Jim Inghame41494a2011-04-16 00:01:13 +00001851#define TSC_DEFAULT_ARCH "default-arch"
1852#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001853#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001854#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001855#define TSC_SOURCE_MAP "source-map"
Enrico Granata018921d2011-08-12 02:00:06 +00001856#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata91544802011-09-06 19:20:51 +00001857#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytond284b662011-02-18 01:44:25 +00001858
1859
1860static const ConstString &
1861GetSettingNameForDefaultArch ()
1862{
1863 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001864 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001865}
1866
Greg Claytond284b662011-02-18 01:44:25 +00001867static const ConstString &
1868GetSettingNameForExpressionPrefix ()
1869{
1870 static ConstString g_const_string (TSC_EXPR_PREFIX);
1871 return g_const_string;
1872}
1873
1874static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001875GetSettingNameForPreferDynamicValue ()
1876{
1877 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1878 return g_const_string;
1879}
1880
Greg Claytonff44ab42011-04-23 02:04:55 +00001881static const ConstString &
1882GetSettingNameForSourcePathMap ()
1883{
1884 static ConstString g_const_string (TSC_SOURCE_MAP);
1885 return g_const_string;
1886}
Greg Claytond284b662011-02-18 01:44:25 +00001887
Greg Clayton17cd9952011-04-22 03:55:06 +00001888static const ConstString &
1889GetSettingNameForSkipPrologue ()
1890{
1891 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1892 return g_const_string;
1893}
1894
Enrico Granata018921d2011-08-12 02:00:06 +00001895static const ConstString &
1896GetSettingNameForMaxChildren ()
1897{
1898 static ConstString g_const_string (TSC_MAX_CHILDREN);
1899 return g_const_string;
1900}
Greg Clayton17cd9952011-04-22 03:55:06 +00001901
Enrico Granata91544802011-09-06 19:20:51 +00001902static const ConstString &
1903GetSettingNameForMaxStringSummaryLength ()
1904{
1905 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1906 return g_const_string;
1907}
Greg Clayton17cd9952011-04-22 03:55:06 +00001908
Caroline Tice5bc8c972010-09-20 20:44:43 +00001909bool
1910Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1911 const char *index_value,
1912 const char *value,
1913 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001914 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001915 Error&err)
1916{
Greg Claytond284b662011-02-18 01:44:25 +00001917 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001918 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001919 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001920 if (!m_default_architecture.IsValid())
1921 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001922 }
1923 return true;
1924}
1925
1926
1927bool
1928Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1929 StringList &value,
1930 Error &err)
1931{
Greg Claytond284b662011-02-18 01:44:25 +00001932 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001933 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001934 // If the arch is invalid (the default), don't show a string for it
1935 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001936 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001937 return true;
1938 }
1939 else
1940 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1941
1942 return false;
1943}
1944
1945//--------------------------------------------------------------
1946// class TargetInstanceSettings
1947//--------------------------------------------------------------
1948
Greg Clayton638351a2010-12-04 00:10:17 +00001949TargetInstanceSettings::TargetInstanceSettings
1950(
1951 UserSettingsController &owner,
1952 bool live_instance,
1953 const char *name
1954) :
Greg Claytond284b662011-02-18 01:44:25 +00001955 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001956 m_expr_prefix_file (),
1957 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001958 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001959 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00001960 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00001961 m_max_children_display(256),
1962 m_max_strlen_length(1024)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001963{
1964 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1965 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1966 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1967 // This is true for CreateInstanceName() too.
1968
1969 if (GetInstanceName () == InstanceSettings::InvalidName())
1970 {
1971 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1972 m_owner.RegisterInstanceSettings (this);
1973 }
1974
1975 if (live_instance)
1976 {
1977 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1978 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001979 }
1980}
1981
1982TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001983 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1984 m_expr_prefix_file (rhs.m_expr_prefix_file),
1985 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1986 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1987 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00001988 m_source_map (rhs.m_source_map),
Enrico Granata91544802011-09-06 19:20:51 +00001989 m_max_children_display(rhs.m_max_children_display),
1990 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001991{
1992 if (m_instance_name != InstanceSettings::GetDefaultName())
1993 {
1994 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1995 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001996 }
1997}
1998
1999TargetInstanceSettings::~TargetInstanceSettings ()
2000{
2001}
2002
2003TargetInstanceSettings&
2004TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
2005{
2006 if (this != &rhs)
2007 {
2008 }
2009
2010 return *this;
2011}
2012
Caroline Tice5bc8c972010-09-20 20:44:43 +00002013void
2014TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
2015 const char *index_value,
2016 const char *value,
2017 const ConstString &instance_name,
2018 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00002019 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00002020 Error &err,
2021 bool pending)
2022{
Greg Claytond284b662011-02-18 01:44:25 +00002023 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002024 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002025 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
2026 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00002027 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002028 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002029 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002030 default:
2031 break;
2032 case eVarSetOperationAssign:
2033 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002034 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002035 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2036 {
2037 err.SetErrorToGenericError ();
2038 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
2039 return;
2040 }
2041
2042 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
2043
2044 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
2045 {
2046 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
2047 m_expr_prefix_contents_sp.reset();
2048 }
Sean Callanan77e93942010-10-29 00:29:03 +00002049 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002050 break;
2051 case eVarSetOperationClear:
2052 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00002053 }
Sean Callanan77e93942010-10-29 00:29:03 +00002054 }
2055 }
Jim Inghame41494a2011-04-16 00:01:13 +00002056 else if (var_name == GetSettingNameForPreferDynamicValue())
2057 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002058 int new_value;
2059 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2060 if (err.Success())
2061 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002062 }
2063 else if (var_name == GetSettingNameForSkipPrologue())
2064 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002065 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2066 }
Enrico Granata018921d2011-08-12 02:00:06 +00002067 else if (var_name == GetSettingNameForMaxChildren())
2068 {
2069 bool ok;
2070 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2071 if (ok)
2072 m_max_children_display = new_value;
2073 }
Enrico Granata91544802011-09-06 19:20:51 +00002074 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2075 {
2076 bool ok;
2077 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2078 if (ok)
2079 m_max_strlen_length = new_value;
2080 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002081 else if (var_name == GetSettingNameForSourcePathMap ())
2082 {
2083 switch (op)
2084 {
2085 case eVarSetOperationReplace:
2086 case eVarSetOperationInsertBefore:
2087 case eVarSetOperationInsertAfter:
2088 case eVarSetOperationRemove:
2089 default:
2090 break;
2091 case eVarSetOperationAssign:
2092 m_source_map.Clear(true);
2093 // Fall through to append....
2094 case eVarSetOperationAppend:
2095 {
2096 Args args(value);
2097 const uint32_t argc = args.GetArgumentCount();
2098 if (argc & 1 || argc == 0)
2099 {
2100 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2101 }
2102 else
2103 {
2104 char resolved_new_path[PATH_MAX];
2105 FileSpec file_spec;
2106 const char *old_path;
2107 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2108 {
2109 const char *new_path = args.GetArgumentAtIndex(idx+1);
2110 assert (new_path); // We have an even number of paths, this shouldn't happen!
2111
2112 file_spec.SetFile(new_path, true);
2113 if (file_spec.Exists())
2114 {
2115 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2116 {
2117 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2118 return;
2119 }
2120 }
2121 else
2122 {
2123 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2124 return;
2125 }
2126 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2127 }
2128 }
2129 }
2130 break;
2131
2132 case eVarSetOperationClear:
2133 m_source_map.Clear(true);
2134 break;
2135 }
Jim Inghame41494a2011-04-16 00:01:13 +00002136 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002137}
2138
2139void
Greg Claytond284b662011-02-18 01:44:25 +00002140TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002141{
Sean Callanan77e93942010-10-29 00:29:03 +00002142 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2143
2144 if (!new_settings_ptr)
2145 return;
2146
Greg Claytonff44ab42011-04-23 02:04:55 +00002147 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
2148 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
2149 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
2150 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata018921d2011-08-12 02:00:06 +00002151 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata91544802011-09-06 19:20:51 +00002152 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002153}
2154
Caroline Ticebcb5b452010-09-20 21:37:42 +00002155bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002156TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2157 const ConstString &var_name,
2158 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002159 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002160{
Greg Claytond284b662011-02-18 01:44:25 +00002161 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002162 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002163 char path[PATH_MAX];
2164 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2165 if (path_len > 0)
2166 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002167 }
Jim Inghame41494a2011-04-16 00:01:13 +00002168 else if (var_name == GetSettingNameForPreferDynamicValue())
2169 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002170 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002171 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002172 else if (var_name == GetSettingNameForSkipPrologue())
2173 {
2174 if (m_skip_prologue)
2175 value.AppendString ("true");
2176 else
2177 value.AppendString ("false");
2178 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002179 else if (var_name == GetSettingNameForSourcePathMap ())
2180 {
2181 }
Enrico Granata018921d2011-08-12 02:00:06 +00002182 else if (var_name == GetSettingNameForMaxChildren())
2183 {
2184 StreamString count_str;
2185 count_str.Printf ("%d", m_max_children_display);
2186 value.AppendString (count_str.GetData());
2187 }
Enrico Granata91544802011-09-06 19:20:51 +00002188 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2189 {
2190 StreamString count_str;
2191 count_str.Printf ("%d", m_max_strlen_length);
2192 value.AppendString (count_str.GetData());
2193 }
Sean Callanan77e93942010-10-29 00:29:03 +00002194 else
2195 {
2196 if (err)
2197 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2198 return false;
2199 }
2200
2201 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002202}
2203
2204const ConstString
2205TargetInstanceSettings::CreateInstanceName ()
2206{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002207 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002208 static int instance_count = 1;
2209
Caroline Tice5bc8c972010-09-20 20:44:43 +00002210 sstr.Printf ("target_%d", instance_count);
2211 ++instance_count;
2212
2213 const ConstString ret_val (sstr.GetData());
2214 return ret_val;
2215}
2216
2217//--------------------------------------------------
2218// Target::SettingsController Variable Tables
2219//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002220OptionEnumValueElement
2221TargetInstanceSettings::g_dynamic_value_types[] =
2222{
Greg Clayton577fbc32011-05-30 00:39:48 +00002223{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2224{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2225{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002226{ 0, NULL, NULL }
2227};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002228
2229SettingEntry
2230Target::SettingsController::global_settings_table[] =
2231{
Greg Claytond284b662011-02-18 01:44:25 +00002232 // var-name var-type default enum init'd hidden help-text
2233 // ================= ================== =========== ==== ====== ====== =========================================================================
2234 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2235 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2236};
2237
Caroline Tice5bc8c972010-09-20 20:44:43 +00002238SettingEntry
2239Target::SettingsController::instance_settings_table[] =
2240{
Enrico Granata91544802011-09-06 19:20:51 +00002241 // var-name var-type default enum init'd hidden help-text
2242 // ================= ================== =============== ======================= ====== ====== =========================================================================
2243 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2244 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2245 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2246 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2247 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2248 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2249 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002250};