blob: fd9eb2438fa230cd30ab0a2e7603d17734ccce31 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5920#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2421#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Caroline Tice969ed3d12011-05-02 20:41:4623#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner30fdc8d2010-06-08 16:52:2424#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5925#include "lldb/Core/Timer.h"
26#include "lldb/Core/ValueObject.h"
Greg Claytoneb0103f2011-04-07 22:46:3527#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:2428#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:5929#include "lldb/Interpreter/CommandInterpreter.h"
30#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2431#include "lldb/lldb-private-log.h"
32#include "lldb/Symbol/ObjectFile.h"
33#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5934#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:5935#include "lldb/Target/Thread.h"
36#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:2437
38using namespace lldb;
39using namespace lldb_private;
40
41//----------------------------------------------------------------------
42// Target constructor
43//----------------------------------------------------------------------
Greg Clayton32e0a752011-03-30 18:16:5144Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
45 Broadcaster ("lldb.target"),
46 ExecutionContextScope (),
Greg Claytondbe54502010-11-19 03:46:0147 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2948 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:5149 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:2350 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:5151 m_arch (target_arch),
52 m_images (),
Greg Claytonf5e56de2010-09-14 23:36:4053 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2454 m_breakpoint_list (false),
55 m_internal_breakpoint_list (true),
Johnny Chen1d6bad02011-09-06 20:05:2556 m_watchpoint_location_list (),
Greg Clayton32e0a752011-03-30 18:16:5157 m_process_sp (),
58 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:2459 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5960 m_scratch_ast_context_ap (NULL),
Jim Ingham9575d842011-03-11 03:53:5961 m_persistent_variables (),
Jim Inghame37d6052011-09-13 00:29:5662 m_source_manager(*this),
Greg Clayton32e0a752011-03-30 18:16:5163 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:1464 m_stop_hook_next_id (0),
65 m_suppress_stop_hooks (false)
Chris Lattner30fdc8d2010-06-08 16:52:2466{
Greg Claytoncfd1ace2010-10-31 03:01:0667 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
68 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
69 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
70
Greg Clayton2d4edfb2010-11-06 01:53:3071 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2472 if (log)
73 log->Printf ("%p Target::Target()", this);
74}
75
76//----------------------------------------------------------------------
77// Destructor
78//----------------------------------------------------------------------
79Target::~Target()
80{
Greg Clayton2d4edfb2010-11-06 01:53:3081 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2482 if (log)
83 log->Printf ("%p Target::~Target()", this);
84 DeleteCurrentProcess ();
85}
86
87void
Caroline Ticeceb6b132010-10-26 03:11:1388Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2489{
Greg Clayton89411422010-10-08 00:21:0590// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1391 if (description_level != lldb::eDescriptionLevelBrief)
92 {
93 s->Indent();
94 s->PutCString("Target\n");
95 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:3596 m_images.Dump(s);
97 m_breakpoint_list.Dump(s);
98 m_internal_breakpoint_list.Dump(s);
99 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13100 }
101 else
102 {
Greg Claytonaa149cb2011-08-11 02:48:45103 Module *exe_module = GetExecutableModulePointer();
104 if (exe_module)
105 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28106 else
107 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13108 }
Chris Lattner30fdc8d2010-06-08 16:52:24109}
110
111void
112Target::DeleteCurrentProcess ()
113{
114 if (m_process_sp.get())
115 {
Greg Clayton17f69202010-09-14 23:52:43116 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24117 if (m_process_sp->IsAlive())
118 m_process_sp->Destroy();
Jim Inghamd0a3e12b2011-02-16 17:54:55119
120 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24121
122 // Do any cleanup of the target we need to do between process instances.
123 // NB It is better to do this before destroying the process in case the
124 // clean up needs some help from the process.
125 m_breakpoint_list.ClearAllBreakpointSites();
126 m_internal_breakpoint_list.ClearAllBreakpointSites();
Johnny Chenecb623a2011-09-15 20:54:25127 m_watchpoint_location_list.ClearAllWatchpointLocations();
Chris Lattner30fdc8d2010-06-08 16:52:24128 m_process_sp.reset();
129 }
130}
131
132const lldb::ProcessSP &
133Target::CreateProcess (Listener &listener, const char *plugin_name)
134{
135 DeleteCurrentProcess ();
136 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
137 return m_process_sp;
138}
139
140const lldb::ProcessSP &
141Target::GetProcessSP () const
142{
143 return m_process_sp;
144}
145
146lldb::TargetSP
147Target::GetSP()
148{
Greg Clayton4d122c42011-09-17 08:33:22149 // This object contains an instrusive ref count base class so we can
150 // easily make a shared pointer to this object
151 return TargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24152}
153
Greg Clayton3418c852011-08-10 02:10:13154void
155Target::Destroy()
156{
157 Mutex::Locker locker (m_mutex);
158 DeleteCurrentProcess ();
159 m_platform_sp.reset();
160 m_arch.Clear();
161 m_images.Clear();
162 m_section_load_list.Clear();
163 const bool notify = false;
164 m_breakpoint_list.RemoveAll(notify);
165 m_internal_breakpoint_list.RemoveAll(notify);
166 m_last_created_breakpoint.reset();
167 m_search_filter_sp.reset();
168 m_image_search_paths.Clear(notify);
169 m_scratch_ast_context_ap.reset();
170 m_persistent_variables.Clear();
171 m_stop_hooks.clear();
172 m_stop_hook_next_id = 0;
173 m_suppress_stop_hooks = false;
174}
175
176
Chris Lattner30fdc8d2010-06-08 16:52:24177BreakpointList &
178Target::GetBreakpointList(bool internal)
179{
180 if (internal)
181 return m_internal_breakpoint_list;
182 else
183 return m_breakpoint_list;
184}
185
186const BreakpointList &
187Target::GetBreakpointList(bool internal) const
188{
189 if (internal)
190 return m_internal_breakpoint_list;
191 else
192 return m_breakpoint_list;
193}
194
195BreakpointSP
196Target::GetBreakpointByID (break_id_t break_id)
197{
198 BreakpointSP bp_sp;
199
200 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
201 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
202 else
203 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
204
205 return bp_sp;
206}
207
208BreakpointSP
209Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
210{
211 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
212 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
213 return CreateBreakpoint (filter_sp, resolver_sp, internal);
214}
215
216
217BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41218Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24219{
Chris Lattner30fdc8d2010-06-08 16:52:24220 Address so_addr;
221 // Attempt to resolve our load address if possible, though it is ok if
222 // it doesn't resolve to section/offset.
223
Greg Clayton1b72fcb2010-08-24 00:45:41224 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40225 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41226 if (!so_addr.IsValid())
227 {
228 // The address didn't resolve, so just set this as an absolute address
229 so_addr.SetOffset (addr);
230 }
231 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24232 return bp_sp;
233}
234
235BreakpointSP
236Target::CreateBreakpoint (Address &addr, bool internal)
237{
238 TargetSP target_sp = this->GetSP();
239 SearchFilterSP filter_sp(new SearchFilter (target_sp));
240 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
241 return CreateBreakpoint (filter_sp, resolver_sp, internal);
242}
243
244BreakpointSP
Greg Claytond16e1e52011-07-12 17:06:17245Target::CreateBreakpoint (const FileSpec *containingModule,
246 const char *func_name,
247 uint32_t func_name_type_mask,
248 bool internal,
249 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24250{
Greg Clayton0c5cd902010-06-28 21:30:43251 BreakpointSP bp_sp;
252 if (func_name)
253 {
254 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Claytond16e1e52011-07-12 17:06:17255
256 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
257 func_name,
258 func_name_type_mask,
259 Breakpoint::Exact,
260 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton0c5cd902010-06-28 21:30:43261 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
262 }
263 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24264}
265
266
267SearchFilterSP
268Target::GetSearchFilterForModule (const FileSpec *containingModule)
269{
270 SearchFilterSP filter_sp;
271 lldb::TargetSP target_sp = this->GetSP();
272 if (containingModule != NULL)
273 {
274 // TODO: We should look into sharing module based search filters
275 // across many breakpoints like we do for the simple target based one
276 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
277 }
278 else
279 {
280 if (m_search_filter_sp.get() == NULL)
281 m_search_filter_sp.reset (new SearchFilter (target_sp));
282 filter_sp = m_search_filter_sp;
283 }
284 return filter_sp;
285}
286
287BreakpointSP
Greg Claytond16e1e52011-07-12 17:06:17288Target::CreateBreakpoint (const FileSpec *containingModule,
289 RegularExpression &func_regex,
290 bool internal,
291 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24292{
293 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Claytond16e1e52011-07-12 17:06:17294 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
295 func_regex,
296 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24297
298 return CreateBreakpoint (filter_sp, resolver_sp, internal);
299}
300
301BreakpointSP
302Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
303{
304 BreakpointSP bp_sp;
305 if (filter_sp && resolver_sp)
306 {
307 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
308 resolver_sp->SetBreakpoint (bp_sp.get());
309
310 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17311 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24312 else
Greg Clayton9fed0d82010-07-23 23:33:17313 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24314
Greg Clayton2d4edfb2010-11-06 01:53:30315 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24316 if (log)
317 {
318 StreamString s;
319 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
320 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
321 }
322
Chris Lattner30fdc8d2010-06-08 16:52:24323 bp_sp->ResolveBreakpoint();
324 }
Jim Ingham36f3b362010-10-14 23:45:03325
326 if (!internal && bp_sp)
327 {
328 m_last_created_breakpoint = bp_sp;
329 }
330
Chris Lattner30fdc8d2010-06-08 16:52:24331 return bp_sp;
332}
333
Johnny Chenab9ee762011-09-14 00:26:03334// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
335// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen887062a2011-09-12 23:38:44336WatchpointLocationSP
337Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
338{
Johnny Chen0c406372011-09-14 20:23:45339 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
340 if (log)
341 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
342 __FUNCTION__, addr, size, type);
343
Johnny Chen887062a2011-09-12 23:38:44344 WatchpointLocationSP wp_loc_sp;
Johnny Chen2fd89a02011-09-13 18:30:59345 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
346 if (!process_is_valid)
347 return wp_loc_sp;
Johnny Chen45e541f2011-09-14 22:20:15348 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen887062a2011-09-12 23:38:44349 return wp_loc_sp;
Johnny Chen7313a642011-09-13 01:15:36350
Johnny Chenab9ee762011-09-14 00:26:03351 // Currently we only support one watchpoint location per address, with total
352 // number of watchpoint locations limited by the hardware which the inferior
353 // is running on.
Johnny Chen3c532582011-09-13 23:29:31354 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
355 if (matched_sp)
356 {
Johnny Chen0c406372011-09-14 20:23:45357 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31358 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45359 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
360 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen45e541f2011-09-14 22:20:15361 // Return the existing watchpoint location if both size and type match.
362 if (size == old_size && type == old_type) {
363 wp_loc_sp = matched_sp;
364 wp_loc_sp->SetEnabled(false);
365 } else {
366 // Nil the matched watchpoint location; we will be creating a new one.
367 m_process_sp->DisableWatchpoint(matched_sp.get());
368 m_watchpoint_location_list.Remove(matched_sp->GetID());
369 }
Johnny Chen3c532582011-09-13 23:29:31370 }
371
Johnny Chen45e541f2011-09-14 22:20:15372 if (!wp_loc_sp) {
373 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
374 if (!new_loc) {
375 printf("WatchpointLocation ctor failed, out of memory?\n");
376 return wp_loc_sp;
377 }
378 new_loc->SetWatchpointType(type);
379 wp_loc_sp.reset(new_loc);
380 m_watchpoint_location_list.Add(wp_loc_sp);
381 }
Johnny Chen0c406372011-09-14 20:23:45382
Johnny Chen0c406372011-09-14 20:23:45383 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen0c406372011-09-14 20:23:45384 if (log)
385 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
386 __FUNCTION__,
387 rc.Success() ? "succeeded" : "failed",
388 wp_loc_sp->GetID());
389
Johnny Chen45e541f2011-09-14 22:20:15390 if (rc.Fail()) wp_loc_sp.reset();
Johnny Chen7313a642011-09-13 01:15:36391 return wp_loc_sp;
Johnny Chen887062a2011-09-12 23:38:44392}
393
Chris Lattner30fdc8d2010-06-08 16:52:24394void
395Target::RemoveAllBreakpoints (bool internal_also)
396{
Greg Clayton2d4edfb2010-11-06 01:53:30397 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24398 if (log)
399 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
400
Greg Clayton9fed0d82010-07-23 23:33:17401 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24402 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17403 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03404
405 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24406}
407
408void
409Target::DisableAllBreakpoints (bool internal_also)
410{
Greg Clayton2d4edfb2010-11-06 01:53:30411 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24412 if (log)
413 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
414
415 m_breakpoint_list.SetEnabledAll (false);
416 if (internal_also)
417 m_internal_breakpoint_list.SetEnabledAll (false);
418}
419
420void
421Target::EnableAllBreakpoints (bool internal_also)
422{
Greg Clayton2d4edfb2010-11-06 01:53:30423 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24424 if (log)
425 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
426
427 m_breakpoint_list.SetEnabledAll (true);
428 if (internal_also)
429 m_internal_breakpoint_list.SetEnabledAll (true);
430}
431
432bool
433Target::RemoveBreakpointByID (break_id_t break_id)
434{
Greg Clayton2d4edfb2010-11-06 01:53:30435 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24436 if (log)
437 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
438
439 if (DisableBreakpointByID (break_id))
440 {
441 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17442 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24443 else
Jim Ingham36f3b362010-10-14 23:45:03444 {
Greg Claytonaa1c5872011-01-24 23:35:47445 if (m_last_created_breakpoint)
446 {
447 if (m_last_created_breakpoint->GetID() == break_id)
448 m_last_created_breakpoint.reset();
449 }
Greg Clayton9fed0d82010-07-23 23:33:17450 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03451 }
Chris Lattner30fdc8d2010-06-08 16:52:24452 return true;
453 }
454 return false;
455}
456
457bool
458Target::DisableBreakpointByID (break_id_t break_id)
459{
Greg Clayton2d4edfb2010-11-06 01:53:30460 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24461 if (log)
462 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
463
464 BreakpointSP bp_sp;
465
466 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
467 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
468 else
469 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
470 if (bp_sp)
471 {
472 bp_sp->SetEnabled (false);
473 return true;
474 }
475 return false;
476}
477
478bool
479Target::EnableBreakpointByID (break_id_t break_id)
480{
Greg Clayton2d4edfb2010-11-06 01:53:30481 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24482 if (log)
483 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
484 __FUNCTION__,
485 break_id,
486 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
487
488 BreakpointSP bp_sp;
489
490 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
491 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
492 else
493 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
494
495 if (bp_sp)
496 {
497 bp_sp->SetEnabled (true);
498 return true;
499 }
500 return false;
501}
502
503ModuleSP
504Target::GetExecutableModule ()
505{
Greg Claytonaa149cb2011-08-11 02:48:45506 return m_images.GetModuleAtIndex(0);
507}
508
509Module*
510Target::GetExecutableModulePointer ()
511{
512 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24513}
514
515void
516Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
517{
518 m_images.Clear();
519 m_scratch_ast_context_ap.reset();
520
521 if (executable_sp.get())
522 {
523 Timer scoped_timer (__PRETTY_FUNCTION__,
524 "Target::SetExecutableModule (executable = '%s/%s')",
525 executable_sp->GetFileSpec().GetDirectory().AsCString(),
526 executable_sp->GetFileSpec().GetFilename().AsCString());
527
528 m_images.Append(executable_sp); // The first image is our exectuable file
529
Jim Ingham5aee1622010-08-09 23:31:02530 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton32e0a752011-03-30 18:16:51531 if (!m_arch.IsValid())
532 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02533
Chris Lattner30fdc8d2010-06-08 16:52:24534 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15535 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamb7f6b2f2011-09-08 22:13:49536 // Let's find the file & line for main and set the default source file from there.
537 if (!m_source_manager.DefaultFileAndLineSet())
538 {
539 SymbolContextList sc_list;
540 uint32_t num_matches;
541 ConstString main_name("main");
542 bool symbols_okay = false; // Force it to be a debug symbol.
543 bool append = false;
544 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
545 for (uint32_t idx = 0; idx < num_matches; idx++)
546 {
547 SymbolContext sc;
548 sc_list.GetContextAtIndex(idx, sc);
549 if (sc.line_entry.file)
550 {
551 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
552 break;
553 }
554 }
555 }
Chris Lattner30fdc8d2010-06-08 16:52:24556
557 if (executable_objfile)
558 {
559 executable_objfile->GetDependentModules(dependent_files);
560 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
561 {
Greg Claytonded470d2011-03-19 01:12:21562 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
563 FileSpec platform_dependent_file_spec;
564 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55565 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21566 else
567 platform_dependent_file_spec = dependent_file_spec;
568
569 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton32e0a752011-03-30 18:16:51570 m_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24571 if (image_module_sp.get())
572 {
Chris Lattner30fdc8d2010-06-08 16:52:24573 ObjectFile *objfile = image_module_sp->GetObjectFile();
574 if (objfile)
575 objfile->GetDependentModules(dependent_files);
576 }
577 }
578 }
579
Chris Lattner30fdc8d2010-06-08 16:52:24580 }
Caroline Tice1559a462010-09-27 00:30:10581
582 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24583}
584
585
Jim Ingham5aee1622010-08-09 23:31:02586bool
587Target::SetArchitecture (const ArchSpec &arch_spec)
588{
Greg Clayton32e0a752011-03-30 18:16:51589 if (m_arch == arch_spec)
Jim Ingham5aee1622010-08-09 23:31:02590 {
591 // If we're setting the architecture to our current architecture, we
592 // don't need to do anything.
593 return true;
594 }
Greg Clayton32e0a752011-03-30 18:16:51595 else if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02596 {
597 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton32e0a752011-03-30 18:16:51598 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02599 return true;
600 }
601 else
602 {
603 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51604 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02605 ModuleSP executable_sp = GetExecutableModule ();
606 m_images.Clear();
607 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02608 // Need to do something about unsetting breakpoints.
609
610 if (executable_sp)
611 {
612 FileSpec exec_file_spec = executable_sp->GetFileSpec();
613 Error error = ModuleList::GetSharedModule(exec_file_spec,
614 arch_spec,
615 NULL,
616 NULL,
617 0,
618 executable_sp,
619 NULL,
620 NULL);
621
622 if (!error.Fail() && executable_sp)
623 {
624 SetExecutableModule (executable_sp, true);
625 return true;
626 }
627 else
628 {
629 return false;
630 }
631 }
632 else
633 {
634 return false;
635 }
636 }
637}
Chris Lattner30fdc8d2010-06-08 16:52:24638
Chris Lattner30fdc8d2010-06-08 16:52:24639void
640Target::ModuleAdded (ModuleSP &module_sp)
641{
642 // A module is being added to this target for the first time
643 ModuleList module_list;
644 module_list.Append(module_sp);
645 ModulesDidLoad (module_list);
646}
647
648void
649Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
650{
Jim Inghame716ae02011-08-03 01:00:06651 // A module is replacing an already added module
Chris Lattner30fdc8d2010-06-08 16:52:24652 ModuleList module_list;
653 module_list.Append (old_module_sp);
654 ModulesDidUnload (module_list);
655 module_list.Clear ();
656 module_list.Append (new_module_sp);
657 ModulesDidLoad (module_list);
658}
659
660void
661Target::ModulesDidLoad (ModuleList &module_list)
662{
663 m_breakpoint_list.UpdateBreakpoints (module_list, true);
664 // TODO: make event data that packages up the module_list
665 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
666}
667
668void
669Target::ModulesDidUnload (ModuleList &module_list)
670{
671 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26672
673 // Remove the images from the target image list
674 m_images.Remove(module_list);
675
Chris Lattner30fdc8d2010-06-08 16:52:24676 // TODO: make event data that packages up the module_list
677 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
678}
679
680size_t
Greg Claytondb598232011-01-07 01:57:07681Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
682{
683 const Section *section = addr.GetSection();
684 if (section && section->GetModule())
685 {
686 ObjectFile *objfile = section->GetModule()->GetObjectFile();
687 if (objfile)
688 {
689 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
690 addr.GetOffset(),
691 dst,
692 dst_len);
693 if (bytes_read > 0)
694 return bytes_read;
695 else
696 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
697 }
698 else
699 {
700 error.SetErrorString("address isn't from a object file");
701 }
702 }
703 else
704 {
705 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
706 }
707 return 0;
708}
709
710size_t
Enrico Granata9128ee2f2011-09-06 19:20:51711Target::ReadMemory (const Address& addr,
712 bool prefer_file_cache,
713 void *dst,
714 size_t dst_len,
715 Error &error,
716 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24717{
Chris Lattner30fdc8d2010-06-08 16:52:24718 error.Clear();
Greg Claytondb598232011-01-07 01:57:07719
Enrico Granata9128ee2f2011-09-06 19:20:51720 // if we end up reading this from process memory, we will fill this
721 // with the actual load address
722 if (load_addr_ptr)
723 *load_addr_ptr = LLDB_INVALID_ADDRESS;
724
Greg Claytondda4f7b2010-06-30 23:03:03725 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
726
Greg Claytondb598232011-01-07 01:57:07727 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02728
729 addr_t load_addr = LLDB_INVALID_ADDRESS;
730 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58731 Address resolved_addr;
732 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03733 {
Greg Claytond16e1e52011-07-12 17:06:17734 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02735 {
Greg Claytond16e1e52011-07-12 17:06:17736 // No sections are loaded, so we must assume we are not running
737 // yet and anything we are given is a file address.
738 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
739 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02740 }
Greg Claytondda4f7b2010-06-30 23:03:03741 else
Greg Claytonc749eb82011-07-11 05:12:02742 {
Greg Claytond16e1e52011-07-12 17:06:17743 // We have at least one section loaded. This can be becuase
744 // we have manually loaded some sections with "target modules load ..."
745 // or because we have have a live process that has sections loaded
746 // through the dynamic loader
747 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
748 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02749 }
Greg Claytondda4f7b2010-06-30 23:03:03750 }
Greg Clayton357132e2011-03-26 19:14:58751 if (!resolved_addr.IsValid())
752 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03753
Greg Claytonc749eb82011-07-11 05:12:02754
Greg Claytondb598232011-01-07 01:57:07755 if (prefer_file_cache)
756 {
757 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
758 if (bytes_read > 0)
759 return bytes_read;
760 }
Greg Claytondda4f7b2010-06-30 23:03:03761
762 if (process_is_valid)
763 {
Greg Claytonc749eb82011-07-11 05:12:02764 if (load_addr == LLDB_INVALID_ADDRESS)
765 load_addr = resolved_addr.GetLoadAddress (this);
766
Greg Claytondda4f7b2010-06-30 23:03:03767 if (load_addr == LLDB_INVALID_ADDRESS)
768 {
769 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
770 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
771 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda7e589a62011-09-20 00:26:08772 resolved_addr.GetFileAddress(),
773 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Claytondda4f7b2010-06-30 23:03:03774 else
775 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
776 }
777 else
778 {
Greg Claytondb598232011-01-07 01:57:07779 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24780 if (bytes_read != dst_len)
781 {
782 if (error.Success())
783 {
784 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03785 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24786 else
Greg Claytondda4f7b2010-06-30 23:03:03787 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24788 }
789 }
Greg Claytondda4f7b2010-06-30 23:03:03790 if (bytes_read)
Enrico Granata9128ee2f2011-09-06 19:20:51791 {
792 if (load_addr_ptr)
793 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03794 return bytes_read;
Enrico Granata9128ee2f2011-09-06 19:20:51795 }
Greg Claytondda4f7b2010-06-30 23:03:03796 // If the address is not section offset we have an address that
797 // doesn't resolve to any address in any currently loaded shared
798 // libaries and we failed to read memory so there isn't anything
799 // more we can do. If it is section offset, we might be able to
800 // read cached memory from the object file.
801 if (!resolved_addr.IsSectionOffset())
802 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24803 }
Chris Lattner30fdc8d2010-06-08 16:52:24804 }
Greg Claytondda4f7b2010-06-30 23:03:03805
Greg Claytonc749eb82011-07-11 05:12:02806 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03807 {
Greg Claytondb598232011-01-07 01:57:07808 // If we didn't already try and read from the object file cache, then
809 // try it after failing to read from the process.
810 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03811 }
812 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24813}
814
Greg Claytond16e1e52011-07-12 17:06:17815size_t
816Target::ReadScalarIntegerFromMemory (const Address& addr,
817 bool prefer_file_cache,
818 uint32_t byte_size,
819 bool is_signed,
820 Scalar &scalar,
821 Error &error)
822{
823 uint64_t uval;
824
825 if (byte_size <= sizeof(uval))
826 {
827 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
828 if (bytes_read == byte_size)
829 {
830 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
831 uint32_t offset = 0;
832 if (byte_size <= 4)
833 scalar = data.GetMaxU32 (&offset, byte_size);
834 else
835 scalar = data.GetMaxU64 (&offset, byte_size);
836
837 if (is_signed)
838 scalar.SignExtend(byte_size * 8);
839 return bytes_read;
840 }
841 }
842 else
843 {
844 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
845 }
846 return 0;
847}
848
849uint64_t
850Target::ReadUnsignedIntegerFromMemory (const Address& addr,
851 bool prefer_file_cache,
852 size_t integer_byte_size,
853 uint64_t fail_value,
854 Error &error)
855{
856 Scalar scalar;
857 if (ReadScalarIntegerFromMemory (addr,
858 prefer_file_cache,
859 integer_byte_size,
860 false,
861 scalar,
862 error))
863 return scalar.ULongLong(fail_value);
864 return fail_value;
865}
866
867bool
868Target::ReadPointerFromMemory (const Address& addr,
869 bool prefer_file_cache,
870 Error &error,
871 Address &pointer_addr)
872{
873 Scalar scalar;
874 if (ReadScalarIntegerFromMemory (addr,
875 prefer_file_cache,
876 m_arch.GetAddressByteSize(),
877 false,
878 scalar,
879 error))
880 {
881 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
882 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
883 {
884 if (m_section_load_list.IsEmpty())
885 {
886 // No sections are loaded, so we must assume we are not running
887 // yet and anything we are given is a file address.
888 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
889 }
890 else
891 {
892 // We have at least one section loaded. This can be becuase
893 // we have manually loaded some sections with "target modules load ..."
894 // or because we have have a live process that has sections loaded
895 // through the dynamic loader
896 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
897 }
898 // We weren't able to resolve the pointer value, so just return
899 // an address with no section
900 if (!pointer_addr.IsValid())
901 pointer_addr.SetOffset (pointer_vm_addr);
902 return true;
903
904 }
905 }
906 return false;
907}
Chris Lattner30fdc8d2010-06-08 16:52:24908
909ModuleSP
910Target::GetSharedModule
911(
912 const FileSpec& file_spec,
913 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:10914 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:24915 const ConstString *object_name,
916 off_t object_offset,
917 Error *error_ptr
918)
919{
920 // Don't pass in the UUID so we can tell if we have a stale value in our list
921 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
922 bool did_create_module = false;
923 ModuleSP module_sp;
924
Chris Lattner30fdc8d2010-06-08 16:52:24925 Error error;
926
Greg Clayton32e0a752011-03-30 18:16:51927 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner30fdc8d2010-06-08 16:52:24928 if (m_image_search_paths.GetSize())
929 {
930 FileSpec transformed_spec;
931 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
932 {
933 transformed_spec.GetFilename() = file_spec.GetFilename();
934 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
935 }
936 }
937
Greg Clayton32e0a752011-03-30 18:16:51938 // The platform is responsible for finding and caching an appropriate
939 // module in the shared module cache.
940 if (m_platform_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24941 {
Greg Clayton32e0a752011-03-30 18:16:51942 FileSpec platform_file_spec;
943 error = m_platform_sp->GetSharedModule (file_spec,
944 arch,
945 uuid_ptr,
946 object_name,
947 object_offset,
948 module_sp,
949 &old_module_sp,
950 &did_create_module);
951 }
952 else
953 {
954 error.SetErrorString("no platform is currently set");
Chris Lattner30fdc8d2010-06-08 16:52:24955 }
956
Greg Clayton32e0a752011-03-30 18:16:51957 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner30fdc8d2010-06-08 16:52:24958 if (module_sp)
959 {
960 m_images.Append (module_sp);
961 if (did_create_module)
962 {
963 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
964 ModuleUpdated(old_module_sp, module_sp);
965 else
966 ModuleAdded(module_sp);
967 }
968 }
969 if (error_ptr)
970 *error_ptr = error;
971 return module_sp;
972}
973
974
975Target *
976Target::CalculateTarget ()
977{
978 return this;
979}
980
981Process *
982Target::CalculateProcess ()
983{
984 return NULL;
985}
986
987Thread *
988Target::CalculateThread ()
989{
990 return NULL;
991}
992
993StackFrame *
994Target::CalculateStackFrame ()
995{
996 return NULL;
997}
998
999void
Greg Clayton0603aa92010-10-04 01:05:561000Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:241001{
1002 exe_ctx.target = this;
1003 exe_ctx.process = NULL; // Do NOT fill in process...
1004 exe_ctx.thread = NULL;
1005 exe_ctx.frame = NULL;
1006}
1007
1008PathMappingList &
1009Target::GetImageSearchPathList ()
1010{
1011 return m_image_search_paths;
1012}
1013
1014void
1015Target::ImageSearchPathsChanged
1016(
1017 const PathMappingList &path_list,
1018 void *baton
1019)
1020{
1021 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:451022 ModuleSP exe_module_sp (target->GetExecutableModule());
1023 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241024 {
Greg Claytonaa149cb2011-08-11 02:48:451025 target->m_images.Clear();
1026 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:241027 }
1028}
1029
1030ClangASTContext *
1031Target::GetScratchClangASTContext()
1032{
Greg Clayton73da2442011-08-03 01:23:551033 // Now see if we know the target triple, and if so, create our scratch AST context:
1034 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1035 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:241036 return m_scratch_ast_context_ap.get();
1037}
Caroline Ticedaccaa92010-09-20 20:44:431038
Greg Clayton99d0faf2010-11-18 23:32:351039void
Caroline Tice20bd37f2011-03-10 22:14:101040Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:431041{
Greg Clayton99d0faf2010-11-18 23:32:351042 UserSettingsControllerSP &usc = GetSettingsController();
1043 usc.reset (new SettingsController);
1044 UserSettingsController::InitializeSettingsController (usc,
1045 SettingsController::global_settings_table,
1046 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:101047
1048 // Now call SettingsInitialize() on each 'child' setting of Target
1049 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:351050}
Caroline Ticedaccaa92010-09-20 20:44:431051
Greg Clayton99d0faf2010-11-18 23:32:351052void
Caroline Tice20bd37f2011-03-10 22:14:101053Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:351054{
Caroline Tice20bd37f2011-03-10 22:14:101055
1056 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1057
1058 Process::SettingsTerminate ();
1059
1060 // Now terminate Target Settings.
1061
Greg Clayton99d0faf2010-11-18 23:32:351062 UserSettingsControllerSP &usc = GetSettingsController();
1063 UserSettingsController::FinalizeSettingsController (usc);
1064 usc.reset();
1065}
Caroline Ticedaccaa92010-09-20 20:44:431066
Greg Clayton99d0faf2010-11-18 23:32:351067UserSettingsControllerSP &
1068Target::GetSettingsController ()
1069{
1070 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:431071 return g_settings_controller;
1072}
1073
1074ArchSpec
1075Target::GetDefaultArchitecture ()
1076{
Greg Clayton64195a22011-02-23 00:35:021077 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1078
1079 if (settings_controller_sp)
1080 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1081 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:431082}
1083
1084void
Greg Clayton64195a22011-02-23 00:35:021085Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:431086{
Greg Clayton64195a22011-02-23 00:35:021087 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1088
1089 if (settings_controller_sp)
1090 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:431091}
1092
Greg Clayton0603aa92010-10-04 01:05:561093Target *
1094Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1095{
1096 // The target can either exist in the "process" of ExecutionContext, or in
1097 // the "target_sp" member of SymbolContext. This accessor helper function
1098 // will get the target from one of these locations.
1099
1100 Target *target = NULL;
1101 if (sc_ptr != NULL)
1102 target = sc_ptr->target_sp.get();
1103 if (target == NULL)
1104 {
1105 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
1106 target = &exe_ctx_ptr->process->GetTarget();
1107 }
1108 return target;
1109}
1110
1111
Caroline Tice1559a462010-09-27 00:30:101112void
1113Target::UpdateInstanceName ()
1114{
1115 StreamString sstr;
1116
Greg Claytonaa149cb2011-08-11 02:48:451117 Module *exe_module = GetExecutableModulePointer();
1118 if (exe_module)
Caroline Tice1559a462010-09-27 00:30:101119 {
Greg Clayton307de252010-10-27 02:06:371120 sstr.Printf ("%s_%s",
Greg Claytonaa149cb2011-08-11 02:48:451121 exe_module->GetFileSpec().GetFilename().AsCString(),
1122 exe_module->GetArchitecture().GetArchitectureName());
1123 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:101124 }
1125}
1126
Sean Callanan322f5292010-10-29 00:29:031127const char *
1128Target::GetExpressionPrefixContentsAsCString ()
1129{
Greg Clayton7e14f912011-04-23 02:04:551130 if (m_expr_prefix_contents_sp)
1131 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1132 return NULL;
Sean Callanan322f5292010-10-29 00:29:031133}
1134
Greg Clayton8b2fe6d2010-12-14 02:59:591135ExecutionResults
1136Target::EvaluateExpression
1137(
1138 const char *expr_cstr,
1139 StackFrame *frame,
Sean Callanan3bfdaa22011-09-15 02:13:071140 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton8b2fe6d2010-12-14 02:59:591141 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:351142 bool keep_in_memory,
Jim Ingham2837b762011-05-04 03:43:181143 lldb::DynamicValueType use_dynamic,
Greg Clayton8b2fe6d2010-12-14 02:59:591144 lldb::ValueObjectSP &result_valobj_sp
1145)
1146{
1147 ExecutionResults execution_results = eExecutionSetupError;
1148
1149 result_valobj_sp.reset();
Jim Ingham6026ca32011-05-12 02:06:141150
1151 // We shouldn't run stop hooks in expressions.
1152 // Be sure to reset this if you return anywhere within this function.
1153 bool old_suppress_value = m_suppress_stop_hooks;
1154 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:591155
1156 ExecutionContext exe_ctx;
1157 if (frame)
1158 {
1159 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:081160 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:181161 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granata27b625e2011-08-09 01:04:561162 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1163 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham2837b762011-05-04 03:43:181164 lldb::VariableSP var_sp;
1165 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1166 use_dynamic,
1167 expr_path_options,
1168 var_sp,
1169 error);
Greg Clayton8b2fe6d2010-12-14 02:59:591170 }
1171 else if (m_process_sp)
1172 {
1173 m_process_sp->CalculateExecutionContext(exe_ctx);
1174 }
1175 else
1176 {
1177 CalculateExecutionContext(exe_ctx);
1178 }
1179
1180 if (result_valobj_sp)
1181 {
1182 execution_results = eExecutionCompleted;
1183 // We got a result from the frame variable expression path above...
1184 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1185
1186 lldb::ValueObjectSP const_valobj_sp;
1187
1188 // Check in case our value is already a constant value
1189 if (result_valobj_sp->GetIsConstant())
1190 {
1191 const_valobj_sp = result_valobj_sp;
1192 const_valobj_sp->SetName (persistent_variable_name);
1193 }
1194 else
Jim Ingham78a685a2011-04-16 00:01:131195 {
Jim Ingham2837b762011-05-04 03:43:181196 if (use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:131197 {
Jim Ingham2837b762011-05-04 03:43:181198 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:131199 if (dynamic_sp)
1200 result_valobj_sp = dynamic_sp;
1201 }
1202
Jim Ingham6035b672011-03-31 00:19:251203 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:131204 }
Greg Clayton8b2fe6d2010-12-14 02:59:591205
Sean Callanan92adcac2011-01-13 08:53:351206 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1207
Greg Clayton8b2fe6d2010-12-14 02:59:591208 result_valobj_sp = const_valobj_sp;
1209
Sean Callanan92adcac2011-01-13 08:53:351210 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1211 assert (clang_expr_variable_sp.get());
1212
1213 // Set flags and live data as appropriate
1214
1215 const Value &result_value = live_valobj_sp->GetValue();
1216
1217 switch (result_value.GetValueType())
1218 {
1219 case Value::eValueTypeHostAddress:
1220 case Value::eValueTypeFileAddress:
1221 // we don't do anything with these for now
1222 break;
1223 case Value::eValueTypeScalar:
1224 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1225 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1226 break;
1227 case Value::eValueTypeLoadAddress:
1228 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1229 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1230 break;
1231 }
Greg Clayton8b2fe6d2010-12-14 02:59:591232 }
1233 else
1234 {
1235 // Make sure we aren't just trying to see the value of a persistent
1236 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:351237 lldb::ClangExpressionVariableSP persistent_var_sp;
1238 // Only check for persistent variables the expression starts with a '$'
1239 if (expr_cstr[0] == '$')
1240 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1241
Greg Clayton8b2fe6d2010-12-14 02:59:591242 if (persistent_var_sp)
1243 {
1244 result_valobj_sp = persistent_var_sp->GetValueObject ();
1245 execution_results = eExecutionCompleted;
1246 }
1247 else
1248 {
1249 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan3bfdaa22011-09-15 02:13:071250
Greg Clayton8b2fe6d2010-12-14 02:59:591251 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:071252 execution_policy,
Sean Callanan92adcac2011-01-13 08:53:351253 unwind_on_error,
Greg Clayton8b2fe6d2010-12-14 02:59:591254 expr_cstr,
1255 prefix,
1256 result_valobj_sp);
1257 }
1258 }
Jim Ingham6026ca32011-05-12 02:06:141259
1260 m_suppress_stop_hooks = old_suppress_value;
1261
Greg Clayton8b2fe6d2010-12-14 02:59:591262 return execution_results;
1263}
1264
Greg Claytonf3ef3d22011-05-22 22:46:531265lldb::addr_t
1266Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1267{
1268 addr_t code_addr = load_addr;
1269 switch (m_arch.GetMachine())
1270 {
1271 case llvm::Triple::arm:
1272 case llvm::Triple::thumb:
1273 switch (addr_class)
1274 {
1275 case eAddressClassData:
1276 case eAddressClassDebug:
1277 return LLDB_INVALID_ADDRESS;
1278
1279 case eAddressClassUnknown:
1280 case eAddressClassInvalid:
1281 case eAddressClassCode:
1282 case eAddressClassCodeAlternateISA:
1283 case eAddressClassRuntime:
1284 // Check if bit zero it no set?
1285 if ((code_addr & 1ull) == 0)
1286 {
1287 // Bit zero isn't set, check if the address is a multiple of 2?
1288 if (code_addr & 2ull)
1289 {
1290 // The address is a multiple of 2 so it must be thumb, set bit zero
1291 code_addr |= 1ull;
1292 }
1293 else if (addr_class == eAddressClassCodeAlternateISA)
1294 {
1295 // We checked the address and the address claims to be the alternate ISA
1296 // which means thumb, so set bit zero.
1297 code_addr |= 1ull;
1298 }
1299 }
1300 break;
1301 }
1302 break;
1303
1304 default:
1305 break;
1306 }
1307 return code_addr;
1308}
1309
1310lldb::addr_t
1311Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1312{
1313 addr_t opcode_addr = load_addr;
1314 switch (m_arch.GetMachine())
1315 {
1316 case llvm::Triple::arm:
1317 case llvm::Triple::thumb:
1318 switch (addr_class)
1319 {
1320 case eAddressClassData:
1321 case eAddressClassDebug:
1322 return LLDB_INVALID_ADDRESS;
1323
1324 case eAddressClassInvalid:
1325 case eAddressClassUnknown:
1326 case eAddressClassCode:
1327 case eAddressClassCodeAlternateISA:
1328 case eAddressClassRuntime:
1329 opcode_addr &= ~(1ull);
1330 break;
1331 }
1332 break;
1333
1334 default:
1335 break;
1336 }
1337 return opcode_addr;
1338}
1339
Jim Ingham9575d842011-03-11 03:53:591340lldb::user_id_t
1341Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1342{
1343 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1344 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1345 m_stop_hooks[new_uid] = new_hook_sp;
1346 return new_uid;
1347}
1348
1349bool
1350Target::RemoveStopHookByID (lldb::user_id_t user_id)
1351{
1352 size_t num_removed;
1353 num_removed = m_stop_hooks.erase (user_id);
1354 if (num_removed == 0)
1355 return false;
1356 else
1357 return true;
1358}
1359
1360void
1361Target::RemoveAllStopHooks ()
1362{
1363 m_stop_hooks.clear();
1364}
1365
1366Target::StopHookSP
1367Target::GetStopHookByID (lldb::user_id_t user_id)
1368{
1369 StopHookSP found_hook;
1370
1371 StopHookCollection::iterator specified_hook_iter;
1372 specified_hook_iter = m_stop_hooks.find (user_id);
1373 if (specified_hook_iter != m_stop_hooks.end())
1374 found_hook = (*specified_hook_iter).second;
1375 return found_hook;
1376}
1377
1378bool
1379Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1380{
1381 StopHookCollection::iterator specified_hook_iter;
1382 specified_hook_iter = m_stop_hooks.find (user_id);
1383 if (specified_hook_iter == m_stop_hooks.end())
1384 return false;
1385
1386 (*specified_hook_iter).second->SetIsActive (active_state);
1387 return true;
1388}
1389
1390void
1391Target::SetAllStopHooksActiveState (bool active_state)
1392{
1393 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1394 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1395 {
1396 (*pos).second->SetIsActive (active_state);
1397 }
1398}
1399
1400void
1401Target::RunStopHooks ()
1402{
Jim Ingham6026ca32011-05-12 02:06:141403 if (m_suppress_stop_hooks)
1404 return;
1405
Jim Ingham9575d842011-03-11 03:53:591406 if (!m_process_sp)
1407 return;
1408
1409 if (m_stop_hooks.empty())
1410 return;
1411
1412 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1413
1414 // If there aren't any active stop hooks, don't bother either:
1415 bool any_active_hooks = false;
1416 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1417 {
1418 if ((*pos).second->IsActive())
1419 {
1420 any_active_hooks = true;
1421 break;
1422 }
1423 }
1424 if (!any_active_hooks)
1425 return;
1426
1427 CommandReturnObject result;
1428
1429 std::vector<ExecutionContext> exc_ctx_with_reasons;
1430 std::vector<SymbolContext> sym_ctx_with_reasons;
1431
1432 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1433 size_t num_threads = cur_threadlist.GetSize();
1434 for (size_t i = 0; i < num_threads; i++)
1435 {
1436 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1437 if (cur_thread_sp->ThreadStoppedForAReason())
1438 {
1439 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1440 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1441 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1442 }
1443 }
1444
1445 // If no threads stopped for a reason, don't run the stop-hooks.
1446 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1447 if (num_exe_ctx == 0)
1448 return;
1449
Jim Ingham5b52f0c2011-06-02 23:58:261450 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1451 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:591452
1453 bool keep_going = true;
1454 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271455 bool print_hook_header;
1456 bool print_thread_header;
1457
1458 if (num_exe_ctx == 1)
1459 print_thread_header = false;
1460 else
1461 print_thread_header = true;
1462
1463 if (m_stop_hooks.size() == 1)
1464 print_hook_header = false;
1465 else
1466 print_hook_header = true;
1467
Jim Ingham9575d842011-03-11 03:53:591468 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1469 {
1470 // result.Clear();
1471 StopHookSP cur_hook_sp = (*pos).second;
1472 if (!cur_hook_sp->IsActive())
1473 continue;
1474
1475 bool any_thread_matched = false;
1476 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1477 {
1478 if ((cur_hook_sp->GetSpecifier () == NULL
1479 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1480 && (cur_hook_sp->GetThreadSpecifier() == NULL
1481 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1482 {
1483 if (!hooks_ran)
1484 {
Jim Ingham381e25b2011-03-22 01:47:271485 result.AppendMessage("\n** Stop Hooks **");
Jim Ingham9575d842011-03-11 03:53:591486 hooks_ran = true;
1487 }
Jim Ingham381e25b2011-03-22 01:47:271488 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591489 {
1490 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1491 any_thread_matched = true;
1492 }
1493
Jim Ingham381e25b2011-03-22 01:47:271494 if (print_thread_header)
1495 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591496
1497 bool stop_on_continue = true;
1498 bool stop_on_error = true;
1499 bool echo_commands = false;
1500 bool print_results = true;
1501 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:511502 &exc_ctx_with_reasons[i],
1503 stop_on_continue,
1504 stop_on_error,
1505 echo_commands,
1506 print_results,
1507 result);
Jim Ingham9575d842011-03-11 03:53:591508
1509 // If the command started the target going again, we should bag out of
1510 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:511511 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1512 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:591513 {
1514 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1515 keep_going = false;
1516 }
1517 }
1518 }
1519 }
1520 if (hooks_ran)
1521 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice969ed3d12011-05-02 20:41:461522
1523 result.GetImmediateOutputStream()->Flush();
1524 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:591525}
1526
Greg Clayton7b242382011-07-08 00:48:091527bool
1528Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1529{
1530 bool changed = false;
1531 if (module)
1532 {
1533 ObjectFile *object_file = module->GetObjectFile();
1534 if (object_file)
1535 {
1536 SectionList *section_list = object_file->GetSectionList ();
1537 if (section_list)
1538 {
1539 // All sections listed in the dyld image info structure will all
1540 // either be fixed up already, or they will all be off by a single
1541 // slide amount that is determined by finding the first segment
1542 // that is at file offset zero which also has bytes (a file size
1543 // that is greater than zero) in the object file.
1544
1545 // Determine the slide amount (if any)
1546 const size_t num_sections = section_list->GetSize();
1547 size_t sect_idx = 0;
1548 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1549 {
1550 // Iterate through the object file sections to find the
1551 // first section that starts of file offset zero and that
1552 // has bytes in the file...
1553 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1554 if (section)
1555 {
1556 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1557 changed = true;
1558 }
1559 }
1560 }
1561 }
1562 }
1563 return changed;
1564}
1565
1566
Jim Ingham9575d842011-03-11 03:53:591567//--------------------------------------------------------------
1568// class Target::StopHook
1569//--------------------------------------------------------------
1570
1571
1572Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1573 UserID (uid),
1574 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:591575 m_commands (),
1576 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:401577 m_thread_spec_ap(NULL),
1578 m_active (true)
Jim Ingham9575d842011-03-11 03:53:591579{
1580}
1581
1582Target::StopHook::StopHook (const StopHook &rhs) :
1583 UserID (rhs.GetID()),
1584 m_target_sp (rhs.m_target_sp),
1585 m_commands (rhs.m_commands),
1586 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:401587 m_thread_spec_ap (NULL),
1588 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:591589{
1590 if (rhs.m_thread_spec_ap.get() != NULL)
1591 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1592}
1593
1594
1595Target::StopHook::~StopHook ()
1596{
1597}
1598
1599void
1600Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1601{
1602 m_thread_spec_ap.reset (specifier);
1603}
1604
1605
1606void
1607Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1608{
1609 int indent_level = s->GetIndentLevel();
1610
1611 s->SetIndentLevel(indent_level + 2);
1612
1613 s->Printf ("Hook: %d\n", GetID());
1614 if (m_active)
1615 s->Indent ("State: enabled\n");
1616 else
1617 s->Indent ("State: disabled\n");
1618
1619 if (m_specifier_sp)
1620 {
1621 s->Indent();
1622 s->PutCString ("Specifier:\n");
1623 s->SetIndentLevel (indent_level + 4);
1624 m_specifier_sp->GetDescription (s, level);
1625 s->SetIndentLevel (indent_level + 2);
1626 }
1627
1628 if (m_thread_spec_ap.get() != NULL)
1629 {
1630 StreamString tmp;
1631 s->Indent("Thread:\n");
1632 m_thread_spec_ap->GetDescription (&tmp, level);
1633 s->SetIndentLevel (indent_level + 4);
1634 s->Indent (tmp.GetData());
1635 s->PutCString ("\n");
1636 s->SetIndentLevel (indent_level + 2);
1637 }
1638
1639 s->Indent ("Commands: \n");
1640 s->SetIndentLevel (indent_level + 4);
1641 uint32_t num_commands = m_commands.GetSize();
1642 for (uint32_t i = 0; i < num_commands; i++)
1643 {
1644 s->Indent(m_commands.GetStringAtIndex(i));
1645 s->PutCString ("\n");
1646 }
1647 s->SetIndentLevel (indent_level);
1648}
1649
1650
Caroline Ticedaccaa92010-09-20 20:44:431651//--------------------------------------------------------------
1652// class Target::SettingsController
1653//--------------------------------------------------------------
1654
1655Target::SettingsController::SettingsController () :
1656 UserSettingsController ("target", Debugger::GetSettingsController()),
1657 m_default_architecture ()
1658{
1659 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1660 InstanceSettings::GetDefaultName().AsCString()));
1661}
1662
1663Target::SettingsController::~SettingsController ()
1664{
1665}
1666
1667lldb::InstanceSettingsSP
1668Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1669{
Greg Claytondbe54502010-11-19 03:46:011670 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1671 false,
1672 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431673 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1674 return new_settings_sp;
1675}
1676
Caroline Ticedaccaa92010-09-20 20:44:431677
Jim Ingham78a685a2011-04-16 00:01:131678#define TSC_DEFAULT_ARCH "default-arch"
1679#define TSC_EXPR_PREFIX "expr-prefix"
Jim Ingham78a685a2011-04-16 00:01:131680#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton385aa282011-04-22 03:55:061681#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Clayton7e14f912011-04-23 02:04:551682#define TSC_SOURCE_MAP "source-map"
Enrico Granata22c55d12011-08-12 02:00:061683#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata9128ee2f2011-09-06 19:20:511684#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytonbfe5f3b2011-02-18 01:44:251685
1686
1687static const ConstString &
1688GetSettingNameForDefaultArch ()
1689{
1690 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytonbfe5f3b2011-02-18 01:44:251691 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431692}
1693
Greg Claytonbfe5f3b2011-02-18 01:44:251694static const ConstString &
1695GetSettingNameForExpressionPrefix ()
1696{
1697 static ConstString g_const_string (TSC_EXPR_PREFIX);
1698 return g_const_string;
1699}
1700
1701static const ConstString &
Jim Ingham78a685a2011-04-16 00:01:131702GetSettingNameForPreferDynamicValue ()
1703{
1704 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1705 return g_const_string;
1706}
1707
Greg Clayton7e14f912011-04-23 02:04:551708static const ConstString &
1709GetSettingNameForSourcePathMap ()
1710{
1711 static ConstString g_const_string (TSC_SOURCE_MAP);
1712 return g_const_string;
1713}
Greg Claytonbfe5f3b2011-02-18 01:44:251714
Greg Clayton385aa282011-04-22 03:55:061715static const ConstString &
1716GetSettingNameForSkipPrologue ()
1717{
1718 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1719 return g_const_string;
1720}
1721
Enrico Granata22c55d12011-08-12 02:00:061722static const ConstString &
1723GetSettingNameForMaxChildren ()
1724{
1725 static ConstString g_const_string (TSC_MAX_CHILDREN);
1726 return g_const_string;
1727}
Greg Clayton385aa282011-04-22 03:55:061728
Enrico Granata9128ee2f2011-09-06 19:20:511729static const ConstString &
1730GetSettingNameForMaxStringSummaryLength ()
1731{
1732 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1733 return g_const_string;
1734}
Greg Clayton385aa282011-04-22 03:55:061735
Caroline Ticedaccaa92010-09-20 20:44:431736bool
1737Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1738 const char *index_value,
1739 const char *value,
1740 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541741 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431742 Error&err)
1743{
Greg Claytonbfe5f3b2011-02-18 01:44:251744 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431745 {
Greg Claytoneb0103f2011-04-07 22:46:351746 m_default_architecture.SetTriple (value, NULL);
Greg Clayton64195a22011-02-23 00:35:021747 if (!m_default_architecture.IsValid())
1748 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431749 }
1750 return true;
1751}
1752
1753
1754bool
1755Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1756 StringList &value,
1757 Error &err)
1758{
Greg Claytonbfe5f3b2011-02-18 01:44:251759 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431760 {
Greg Clayton307de252010-10-27 02:06:371761 // If the arch is invalid (the default), don't show a string for it
1762 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021763 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431764 return true;
1765 }
1766 else
1767 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1768
1769 return false;
1770}
1771
1772//--------------------------------------------------------------
1773// class TargetInstanceSettings
1774//--------------------------------------------------------------
1775
Greg Clayton85851dd2010-12-04 00:10:171776TargetInstanceSettings::TargetInstanceSettings
1777(
1778 UserSettingsController &owner,
1779 bool live_instance,
1780 const char *name
1781) :
Greg Claytonbfe5f3b2011-02-18 01:44:251782 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Clayton7e14f912011-04-23 02:04:551783 m_expr_prefix_file (),
1784 m_expr_prefix_contents_sp (),
Jim Ingham2837b762011-05-04 03:43:181785 m_prefer_dynamic_value (2),
Greg Clayton7e14f912011-04-23 02:04:551786 m_skip_prologue (true, true),
Enrico Granata22c55d12011-08-12 02:00:061787 m_source_map (NULL, NULL),
Enrico Granata9128ee2f2011-09-06 19:20:511788 m_max_children_display(256),
1789 m_max_strlen_length(1024)
Caroline Ticedaccaa92010-09-20 20:44:431790{
1791 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1792 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1793 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1794 // This is true for CreateInstanceName() too.
1795
1796 if (GetInstanceName () == InstanceSettings::InvalidName())
1797 {
1798 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1799 m_owner.RegisterInstanceSettings (this);
1800 }
1801
1802 if (live_instance)
1803 {
1804 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1805 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:431806 }
1807}
1808
1809TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton7e14f912011-04-23 02:04:551810 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1811 m_expr_prefix_file (rhs.m_expr_prefix_file),
1812 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1813 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1814 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata22c55d12011-08-12 02:00:061815 m_source_map (rhs.m_source_map),
Enrico Granata9128ee2f2011-09-06 19:20:511816 m_max_children_display(rhs.m_max_children_display),
1817 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Ticedaccaa92010-09-20 20:44:431818{
1819 if (m_instance_name != InstanceSettings::GetDefaultName())
1820 {
1821 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1822 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:431823 }
1824}
1825
1826TargetInstanceSettings::~TargetInstanceSettings ()
1827{
1828}
1829
1830TargetInstanceSettings&
1831TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1832{
1833 if (this != &rhs)
1834 {
1835 }
1836
1837 return *this;
1838}
1839
Caroline Ticedaccaa92010-09-20 20:44:431840void
1841TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1842 const char *index_value,
1843 const char *value,
1844 const ConstString &instance_name,
1845 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541846 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431847 Error &err,
1848 bool pending)
1849{
Greg Claytonbfe5f3b2011-02-18 01:44:251850 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031851 {
Greg Clayton7e14f912011-04-23 02:04:551852 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1853 if (err.Success())
Sean Callanan322f5292010-10-29 00:29:031854 {
Greg Clayton7e14f912011-04-23 02:04:551855 switch (op)
Sean Callanan322f5292010-10-29 00:29:031856 {
Greg Clayton7e14f912011-04-23 02:04:551857 default:
1858 break;
1859 case eVarSetOperationAssign:
1860 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:031861 {
Greg Clayton7e14f912011-04-23 02:04:551862 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1863 {
1864 err.SetErrorToGenericError ();
1865 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1866 return;
1867 }
1868
1869 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1870
1871 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1872 {
1873 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1874 m_expr_prefix_contents_sp.reset();
1875 }
Sean Callanan322f5292010-10-29 00:29:031876 }
Greg Clayton7e14f912011-04-23 02:04:551877 break;
1878 case eVarSetOperationClear:
1879 m_expr_prefix_contents_sp.reset();
Sean Callanan322f5292010-10-29 00:29:031880 }
Sean Callanan322f5292010-10-29 00:29:031881 }
1882 }
Jim Ingham78a685a2011-04-16 00:01:131883 else if (var_name == GetSettingNameForPreferDynamicValue())
1884 {
Jim Ingham2837b762011-05-04 03:43:181885 int new_value;
1886 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1887 if (err.Success())
1888 m_prefer_dynamic_value = new_value;
Greg Clayton385aa282011-04-22 03:55:061889 }
1890 else if (var_name == GetSettingNameForSkipPrologue())
1891 {
Greg Clayton7e14f912011-04-23 02:04:551892 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1893 }
Enrico Granata22c55d12011-08-12 02:00:061894 else if (var_name == GetSettingNameForMaxChildren())
1895 {
1896 bool ok;
1897 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1898 if (ok)
1899 m_max_children_display = new_value;
1900 }
Enrico Granata9128ee2f2011-09-06 19:20:511901 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1902 {
1903 bool ok;
1904 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1905 if (ok)
1906 m_max_strlen_length = new_value;
1907 }
Greg Clayton7e14f912011-04-23 02:04:551908 else if (var_name == GetSettingNameForSourcePathMap ())
1909 {
1910 switch (op)
1911 {
1912 case eVarSetOperationReplace:
1913 case eVarSetOperationInsertBefore:
1914 case eVarSetOperationInsertAfter:
1915 case eVarSetOperationRemove:
1916 default:
1917 break;
1918 case eVarSetOperationAssign:
1919 m_source_map.Clear(true);
1920 // Fall through to append....
1921 case eVarSetOperationAppend:
1922 {
1923 Args args(value);
1924 const uint32_t argc = args.GetArgumentCount();
1925 if (argc & 1 || argc == 0)
1926 {
1927 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1928 }
1929 else
1930 {
1931 char resolved_new_path[PATH_MAX];
1932 FileSpec file_spec;
1933 const char *old_path;
1934 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1935 {
1936 const char *new_path = args.GetArgumentAtIndex(idx+1);
1937 assert (new_path); // We have an even number of paths, this shouldn't happen!
1938
1939 file_spec.SetFile(new_path, true);
1940 if (file_spec.Exists())
1941 {
1942 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1943 {
1944 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1945 return;
1946 }
1947 }
1948 else
1949 {
1950 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1951 return;
1952 }
1953 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1954 }
1955 }
1956 }
1957 break;
1958
1959 case eVarSetOperationClear:
1960 m_source_map.Clear(true);
1961 break;
1962 }
Jim Ingham78a685a2011-04-16 00:01:131963 }
Caroline Ticedaccaa92010-09-20 20:44:431964}
1965
1966void
Greg Claytonbfe5f3b2011-02-18 01:44:251967TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431968{
Sean Callanan322f5292010-10-29 00:29:031969 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1970
1971 if (!new_settings_ptr)
1972 return;
1973
Greg Clayton7e14f912011-04-23 02:04:551974 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1975 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1976 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1977 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata22c55d12011-08-12 02:00:061978 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata9128ee2f2011-09-06 19:20:511979 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Ticedaccaa92010-09-20 20:44:431980}
1981
Caroline Tice12cecd72010-09-20 21:37:421982bool
Caroline Ticedaccaa92010-09-20 20:44:431983TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1984 const ConstString &var_name,
1985 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421986 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431987{
Greg Claytonbfe5f3b2011-02-18 01:44:251988 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031989 {
Greg Clayton7e14f912011-04-23 02:04:551990 char path[PATH_MAX];
1991 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1992 if (path_len > 0)
1993 value.AppendString (path, path_len);
Sean Callanan322f5292010-10-29 00:29:031994 }
Jim Ingham78a685a2011-04-16 00:01:131995 else if (var_name == GetSettingNameForPreferDynamicValue())
1996 {
Jim Ingham2837b762011-05-04 03:43:181997 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Ingham78a685a2011-04-16 00:01:131998 }
Greg Clayton385aa282011-04-22 03:55:061999 else if (var_name == GetSettingNameForSkipPrologue())
2000 {
2001 if (m_skip_prologue)
2002 value.AppendString ("true");
2003 else
2004 value.AppendString ("false");
2005 }
Greg Clayton7e14f912011-04-23 02:04:552006 else if (var_name == GetSettingNameForSourcePathMap ())
2007 {
2008 }
Enrico Granata22c55d12011-08-12 02:00:062009 else if (var_name == GetSettingNameForMaxChildren())
2010 {
2011 StreamString count_str;
2012 count_str.Printf ("%d", m_max_children_display);
2013 value.AppendString (count_str.GetData());
2014 }
Enrico Granata9128ee2f2011-09-06 19:20:512015 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2016 {
2017 StreamString count_str;
2018 count_str.Printf ("%d", m_max_strlen_length);
2019 value.AppendString (count_str.GetData());
2020 }
Sean Callanan322f5292010-10-29 00:29:032021 else
2022 {
2023 if (err)
2024 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2025 return false;
2026 }
2027
2028 return true;
Caroline Ticedaccaa92010-09-20 20:44:432029}
2030
2031const ConstString
2032TargetInstanceSettings::CreateInstanceName ()
2033{
Caroline Ticedaccaa92010-09-20 20:44:432034 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:102035 static int instance_count = 1;
2036
Caroline Ticedaccaa92010-09-20 20:44:432037 sstr.Printf ("target_%d", instance_count);
2038 ++instance_count;
2039
2040 const ConstString ret_val (sstr.GetData());
2041 return ret_val;
2042}
2043
2044//--------------------------------------------------
2045// Target::SettingsController Variable Tables
2046//--------------------------------------------------
Jim Ingham2837b762011-05-04 03:43:182047OptionEnumValueElement
2048TargetInstanceSettings::g_dynamic_value_types[] =
2049{
Greg Clayton5d2fbfe2011-05-30 00:39:482050{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2051{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2052{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham2837b762011-05-04 03:43:182053{ 0, NULL, NULL }
2054};
Caroline Ticedaccaa92010-09-20 20:44:432055
2056SettingEntry
2057Target::SettingsController::global_settings_table[] =
2058{
Greg Claytonbfe5f3b2011-02-18 01:44:252059 // var-name var-type default enum init'd hidden help-text
2060 // ================= ================== =========== ==== ====== ====== =========================================================================
2061 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2062 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2063};
2064
Caroline Ticedaccaa92010-09-20 20:44:432065SettingEntry
2066Target::SettingsController::instance_settings_table[] =
2067{
Enrico Granata9128ee2f2011-09-06 19:20:512068 // var-name var-type default enum init'd hidden help-text
2069 // ================= ================== =============== ======================= ====== ====== =========================================================================
2070 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2071 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2072 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2073 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2074 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2075 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2076 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:432077};