blob: 607bb90b4cccbbbf80e1a33b9ea88513561cf278 [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();
127 m_process_sp.reset();
128 }
129}
130
131const lldb::ProcessSP &
132Target::CreateProcess (Listener &listener, const char *plugin_name)
133{
134 DeleteCurrentProcess ();
135 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
136 return m_process_sp;
137}
138
139const lldb::ProcessSP &
140Target::GetProcessSP () const
141{
142 return m_process_sp;
143}
144
145lldb::TargetSP
146Target::GetSP()
147{
Greg Clayton66111032010-06-23 01:19:29148 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24149}
150
Greg Clayton3418c852011-08-10 02:10:13151void
152Target::Destroy()
153{
154 Mutex::Locker locker (m_mutex);
155 DeleteCurrentProcess ();
156 m_platform_sp.reset();
157 m_arch.Clear();
158 m_images.Clear();
159 m_section_load_list.Clear();
160 const bool notify = false;
161 m_breakpoint_list.RemoveAll(notify);
162 m_internal_breakpoint_list.RemoveAll(notify);
163 m_last_created_breakpoint.reset();
164 m_search_filter_sp.reset();
165 m_image_search_paths.Clear(notify);
166 m_scratch_ast_context_ap.reset();
167 m_persistent_variables.Clear();
168 m_stop_hooks.clear();
169 m_stop_hook_next_id = 0;
170 m_suppress_stop_hooks = false;
171}
172
173
Chris Lattner30fdc8d2010-06-08 16:52:24174BreakpointList &
175Target::GetBreakpointList(bool internal)
176{
177 if (internal)
178 return m_internal_breakpoint_list;
179 else
180 return m_breakpoint_list;
181}
182
183const BreakpointList &
184Target::GetBreakpointList(bool internal) const
185{
186 if (internal)
187 return m_internal_breakpoint_list;
188 else
189 return m_breakpoint_list;
190}
191
192BreakpointSP
193Target::GetBreakpointByID (break_id_t break_id)
194{
195 BreakpointSP bp_sp;
196
197 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
198 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
199 else
200 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
201
202 return bp_sp;
203}
204
205BreakpointSP
206Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
207{
208 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
209 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
210 return CreateBreakpoint (filter_sp, resolver_sp, internal);
211}
212
213
214BreakpointSP
Greg Clayton1b72fcb2010-08-24 00:45:41215Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24216{
Chris Lattner30fdc8d2010-06-08 16:52:24217 Address so_addr;
218 // Attempt to resolve our load address if possible, though it is ok if
219 // it doesn't resolve to section/offset.
220
Greg Clayton1b72fcb2010-08-24 00:45:41221 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40222 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41223 if (!so_addr.IsValid())
224 {
225 // The address didn't resolve, so just set this as an absolute address
226 so_addr.SetOffset (addr);
227 }
228 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner30fdc8d2010-06-08 16:52:24229 return bp_sp;
230}
231
232BreakpointSP
233Target::CreateBreakpoint (Address &addr, bool internal)
234{
235 TargetSP target_sp = this->GetSP();
236 SearchFilterSP filter_sp(new SearchFilter (target_sp));
237 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
238 return CreateBreakpoint (filter_sp, resolver_sp, internal);
239}
240
241BreakpointSP
Greg Claytond16e1e52011-07-12 17:06:17242Target::CreateBreakpoint (const FileSpec *containingModule,
243 const char *func_name,
244 uint32_t func_name_type_mask,
245 bool internal,
246 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24247{
Greg Clayton0c5cd902010-06-28 21:30:43248 BreakpointSP bp_sp;
249 if (func_name)
250 {
251 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Claytond16e1e52011-07-12 17:06:17252
253 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
254 func_name,
255 func_name_type_mask,
256 Breakpoint::Exact,
257 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Greg Clayton0c5cd902010-06-28 21:30:43258 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
259 }
260 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24261}
262
263
264SearchFilterSP
265Target::GetSearchFilterForModule (const FileSpec *containingModule)
266{
267 SearchFilterSP filter_sp;
268 lldb::TargetSP target_sp = this->GetSP();
269 if (containingModule != NULL)
270 {
271 // TODO: We should look into sharing module based search filters
272 // across many breakpoints like we do for the simple target based one
273 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
274 }
275 else
276 {
277 if (m_search_filter_sp.get() == NULL)
278 m_search_filter_sp.reset (new SearchFilter (target_sp));
279 filter_sp = m_search_filter_sp;
280 }
281 return filter_sp;
282}
283
284BreakpointSP
Greg Claytond16e1e52011-07-12 17:06:17285Target::CreateBreakpoint (const FileSpec *containingModule,
286 RegularExpression &func_regex,
287 bool internal,
288 LazyBool skip_prologue)
Chris Lattner30fdc8d2010-06-08 16:52:24289{
290 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Claytond16e1e52011-07-12 17:06:17291 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
292 func_regex,
293 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner30fdc8d2010-06-08 16:52:24294
295 return CreateBreakpoint (filter_sp, resolver_sp, internal);
296}
297
298BreakpointSP
299Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
300{
301 BreakpointSP bp_sp;
302 if (filter_sp && resolver_sp)
303 {
304 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
305 resolver_sp->SetBreakpoint (bp_sp.get());
306
307 if (internal)
Greg Clayton9fed0d82010-07-23 23:33:17308 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24309 else
Greg Clayton9fed0d82010-07-23 23:33:17310 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24311
Greg Clayton2d4edfb2010-11-06 01:53:30312 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24313 if (log)
314 {
315 StreamString s;
316 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
317 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
318 }
319
Chris Lattner30fdc8d2010-06-08 16:52:24320 bp_sp->ResolveBreakpoint();
321 }
Jim Ingham36f3b362010-10-14 23:45:03322
323 if (!internal && bp_sp)
324 {
325 m_last_created_breakpoint = bp_sp;
326 }
327
Chris Lattner30fdc8d2010-06-08 16:52:24328 return bp_sp;
329}
330
Johnny Chenab9ee762011-09-14 00:26:03331// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
332// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen887062a2011-09-12 23:38:44333WatchpointLocationSP
334Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
335{
Johnny Chen0c406372011-09-14 20:23:45336 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
337 if (log)
338 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
339 __FUNCTION__, addr, size, type);
340
Johnny Chen887062a2011-09-12 23:38:44341 WatchpointLocationSP wp_loc_sp;
Johnny Chen2fd89a02011-09-13 18:30:59342 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
343 if (!process_is_valid)
344 return wp_loc_sp;
Johnny Chen45e541f2011-09-14 22:20:15345 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen887062a2011-09-12 23:38:44346 return wp_loc_sp;
Johnny Chen7313a642011-09-13 01:15:36347
Johnny Chenab9ee762011-09-14 00:26:03348 // Currently we only support one watchpoint location per address, with total
349 // number of watchpoint locations limited by the hardware which the inferior
350 // is running on.
Johnny Chen3c532582011-09-13 23:29:31351 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
352 if (matched_sp)
353 {
Johnny Chen0c406372011-09-14 20:23:45354 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31355 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45356 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
357 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen45e541f2011-09-14 22:20:15358 // Return the existing watchpoint location if both size and type match.
359 if (size == old_size && type == old_type) {
360 wp_loc_sp = matched_sp;
361 wp_loc_sp->SetEnabled(false);
362 } else {
363 // Nil the matched watchpoint location; we will be creating a new one.
364 m_process_sp->DisableWatchpoint(matched_sp.get());
365 m_watchpoint_location_list.Remove(matched_sp->GetID());
366 }
Johnny Chen3c532582011-09-13 23:29:31367 }
368
Johnny Chen45e541f2011-09-14 22:20:15369 if (!wp_loc_sp) {
370 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
371 if (!new_loc) {
372 printf("WatchpointLocation ctor failed, out of memory?\n");
373 return wp_loc_sp;
374 }
375 new_loc->SetWatchpointType(type);
376 wp_loc_sp.reset(new_loc);
377 m_watchpoint_location_list.Add(wp_loc_sp);
378 }
Johnny Chen0c406372011-09-14 20:23:45379
Johnny Chen0c406372011-09-14 20:23:45380 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen0c406372011-09-14 20:23:45381 if (log)
382 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
383 __FUNCTION__,
384 rc.Success() ? "succeeded" : "failed",
385 wp_loc_sp->GetID());
386
Johnny Chen45e541f2011-09-14 22:20:15387 if (rc.Fail()) wp_loc_sp.reset();
Johnny Chen7313a642011-09-13 01:15:36388 return wp_loc_sp;
Johnny Chen887062a2011-09-12 23:38:44389}
390
Chris Lattner30fdc8d2010-06-08 16:52:24391void
392Target::RemoveAllBreakpoints (bool internal_also)
393{
Greg Clayton2d4edfb2010-11-06 01:53:30394 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24395 if (log)
396 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
397
Greg Clayton9fed0d82010-07-23 23:33:17398 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24399 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17400 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03401
402 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24403}
404
405void
406Target::DisableAllBreakpoints (bool internal_also)
407{
Greg Clayton2d4edfb2010-11-06 01:53:30408 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24409 if (log)
410 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
411
412 m_breakpoint_list.SetEnabledAll (false);
413 if (internal_also)
414 m_internal_breakpoint_list.SetEnabledAll (false);
415}
416
417void
418Target::EnableAllBreakpoints (bool internal_also)
419{
Greg Clayton2d4edfb2010-11-06 01:53:30420 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24421 if (log)
422 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
423
424 m_breakpoint_list.SetEnabledAll (true);
425 if (internal_also)
426 m_internal_breakpoint_list.SetEnabledAll (true);
427}
428
429bool
430Target::RemoveBreakpointByID (break_id_t break_id)
431{
Greg Clayton2d4edfb2010-11-06 01:53:30432 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24433 if (log)
434 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
435
436 if (DisableBreakpointByID (break_id))
437 {
438 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17439 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24440 else
Jim Ingham36f3b362010-10-14 23:45:03441 {
Greg Claytonaa1c5872011-01-24 23:35:47442 if (m_last_created_breakpoint)
443 {
444 if (m_last_created_breakpoint->GetID() == break_id)
445 m_last_created_breakpoint.reset();
446 }
Greg Clayton9fed0d82010-07-23 23:33:17447 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03448 }
Chris Lattner30fdc8d2010-06-08 16:52:24449 return true;
450 }
451 return false;
452}
453
454bool
455Target::DisableBreakpointByID (break_id_t break_id)
456{
Greg Clayton2d4edfb2010-11-06 01:53:30457 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24458 if (log)
459 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
460
461 BreakpointSP bp_sp;
462
463 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
464 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
465 else
466 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
467 if (bp_sp)
468 {
469 bp_sp->SetEnabled (false);
470 return true;
471 }
472 return false;
473}
474
475bool
476Target::EnableBreakpointByID (break_id_t break_id)
477{
Greg Clayton2d4edfb2010-11-06 01:53:30478 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24479 if (log)
480 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
481 __FUNCTION__,
482 break_id,
483 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
484
485 BreakpointSP bp_sp;
486
487 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
488 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
489 else
490 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
491
492 if (bp_sp)
493 {
494 bp_sp->SetEnabled (true);
495 return true;
496 }
497 return false;
498}
499
500ModuleSP
501Target::GetExecutableModule ()
502{
Greg Claytonaa149cb2011-08-11 02:48:45503 return m_images.GetModuleAtIndex(0);
504}
505
506Module*
507Target::GetExecutableModulePointer ()
508{
509 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24510}
511
512void
513Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
514{
515 m_images.Clear();
516 m_scratch_ast_context_ap.reset();
517
518 if (executable_sp.get())
519 {
520 Timer scoped_timer (__PRETTY_FUNCTION__,
521 "Target::SetExecutableModule (executable = '%s/%s')",
522 executable_sp->GetFileSpec().GetDirectory().AsCString(),
523 executable_sp->GetFileSpec().GetFilename().AsCString());
524
525 m_images.Append(executable_sp); // The first image is our exectuable file
526
Jim Ingham5aee1622010-08-09 23:31:02527 // 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:51528 if (!m_arch.IsValid())
529 m_arch = executable_sp->GetArchitecture();
Jim Ingham5aee1622010-08-09 23:31:02530
Chris Lattner30fdc8d2010-06-08 16:52:24531 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15532 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamb7f6b2f2011-09-08 22:13:49533 // Let's find the file & line for main and set the default source file from there.
534 if (!m_source_manager.DefaultFileAndLineSet())
535 {
536 SymbolContextList sc_list;
537 uint32_t num_matches;
538 ConstString main_name("main");
539 bool symbols_okay = false; // Force it to be a debug symbol.
540 bool append = false;
541 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
542 for (uint32_t idx = 0; idx < num_matches; idx++)
543 {
544 SymbolContext sc;
545 sc_list.GetContextAtIndex(idx, sc);
546 if (sc.line_entry.file)
547 {
548 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
549 break;
550 }
551 }
552 }
Chris Lattner30fdc8d2010-06-08 16:52:24553
554 if (executable_objfile)
555 {
556 executable_objfile->GetDependentModules(dependent_files);
557 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
558 {
Greg Claytonded470d2011-03-19 01:12:21559 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
560 FileSpec platform_dependent_file_spec;
561 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55562 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21563 else
564 platform_dependent_file_spec = dependent_file_spec;
565
566 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton32e0a752011-03-30 18:16:51567 m_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24568 if (image_module_sp.get())
569 {
Chris Lattner30fdc8d2010-06-08 16:52:24570 ObjectFile *objfile = image_module_sp->GetObjectFile();
571 if (objfile)
572 objfile->GetDependentModules(dependent_files);
573 }
574 }
575 }
576
Chris Lattner30fdc8d2010-06-08 16:52:24577 }
Caroline Tice1559a462010-09-27 00:30:10578
579 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24580}
581
582
Jim Ingham5aee1622010-08-09 23:31:02583bool
584Target::SetArchitecture (const ArchSpec &arch_spec)
585{
Greg Clayton32e0a752011-03-30 18:16:51586 if (m_arch == arch_spec)
Jim Ingham5aee1622010-08-09 23:31:02587 {
588 // If we're setting the architecture to our current architecture, we
589 // don't need to do anything.
590 return true;
591 }
Greg Clayton32e0a752011-03-30 18:16:51592 else if (!m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02593 {
594 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton32e0a752011-03-30 18:16:51595 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02596 return true;
597 }
598 else
599 {
600 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton32e0a752011-03-30 18:16:51601 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02602 ModuleSP executable_sp = GetExecutableModule ();
603 m_images.Clear();
604 m_scratch_ast_context_ap.reset();
Jim Ingham5aee1622010-08-09 23:31:02605 // Need to do something about unsetting breakpoints.
606
607 if (executable_sp)
608 {
609 FileSpec exec_file_spec = executable_sp->GetFileSpec();
610 Error error = ModuleList::GetSharedModule(exec_file_spec,
611 arch_spec,
612 NULL,
613 NULL,
614 0,
615 executable_sp,
616 NULL,
617 NULL);
618
619 if (!error.Fail() && executable_sp)
620 {
621 SetExecutableModule (executable_sp, true);
622 return true;
623 }
624 else
625 {
626 return false;
627 }
628 }
629 else
630 {
631 return false;
632 }
633 }
634}
Chris Lattner30fdc8d2010-06-08 16:52:24635
Chris Lattner30fdc8d2010-06-08 16:52:24636void
637Target::ModuleAdded (ModuleSP &module_sp)
638{
639 // A module is being added to this target for the first time
640 ModuleList module_list;
641 module_list.Append(module_sp);
642 ModulesDidLoad (module_list);
643}
644
645void
646Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
647{
Jim Inghame716ae02011-08-03 01:00:06648 // A module is replacing an already added module
Chris Lattner30fdc8d2010-06-08 16:52:24649 ModuleList module_list;
650 module_list.Append (old_module_sp);
651 ModulesDidUnload (module_list);
652 module_list.Clear ();
653 module_list.Append (new_module_sp);
654 ModulesDidLoad (module_list);
655}
656
657void
658Target::ModulesDidLoad (ModuleList &module_list)
659{
660 m_breakpoint_list.UpdateBreakpoints (module_list, true);
661 // TODO: make event data that packages up the module_list
662 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
663}
664
665void
666Target::ModulesDidUnload (ModuleList &module_list)
667{
668 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Claytona4d78302010-12-06 23:51:26669
670 // Remove the images from the target image list
671 m_images.Remove(module_list);
672
Chris Lattner30fdc8d2010-06-08 16:52:24673 // TODO: make event data that packages up the module_list
674 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
675}
676
677size_t
Greg Claytondb598232011-01-07 01:57:07678Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
679{
680 const Section *section = addr.GetSection();
681 if (section && section->GetModule())
682 {
683 ObjectFile *objfile = section->GetModule()->GetObjectFile();
684 if (objfile)
685 {
686 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
687 addr.GetOffset(),
688 dst,
689 dst_len);
690 if (bytes_read > 0)
691 return bytes_read;
692 else
693 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
694 }
695 else
696 {
697 error.SetErrorString("address isn't from a object file");
698 }
699 }
700 else
701 {
702 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
703 }
704 return 0;
705}
706
707size_t
Enrico Granata9128ee2f2011-09-06 19:20:51708Target::ReadMemory (const Address& addr,
709 bool prefer_file_cache,
710 void *dst,
711 size_t dst_len,
712 Error &error,
713 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24714{
Chris Lattner30fdc8d2010-06-08 16:52:24715 error.Clear();
Greg Claytondb598232011-01-07 01:57:07716
Enrico Granata9128ee2f2011-09-06 19:20:51717 // if we end up reading this from process memory, we will fill this
718 // with the actual load address
719 if (load_addr_ptr)
720 *load_addr_ptr = LLDB_INVALID_ADDRESS;
721
Greg Claytondda4f7b2010-06-30 23:03:03722 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
723
Greg Claytondb598232011-01-07 01:57:07724 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02725
726 addr_t load_addr = LLDB_INVALID_ADDRESS;
727 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58728 Address resolved_addr;
729 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03730 {
Greg Claytond16e1e52011-07-12 17:06:17731 if (m_section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02732 {
Greg Claytond16e1e52011-07-12 17:06:17733 // No sections are loaded, so we must assume we are not running
734 // yet and anything we are given is a file address.
735 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
736 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02737 }
Greg Claytondda4f7b2010-06-30 23:03:03738 else
Greg Claytonc749eb82011-07-11 05:12:02739 {
Greg Claytond16e1e52011-07-12 17:06:17740 // We have at least one section loaded. This can be becuase
741 // we have manually loaded some sections with "target modules load ..."
742 // or because we have have a live process that has sections loaded
743 // through the dynamic loader
744 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
745 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02746 }
Greg Claytondda4f7b2010-06-30 23:03:03747 }
Greg Clayton357132e2011-03-26 19:14:58748 if (!resolved_addr.IsValid())
749 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03750
Greg Claytonc749eb82011-07-11 05:12:02751
Greg Claytondb598232011-01-07 01:57:07752 if (prefer_file_cache)
753 {
754 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
755 if (bytes_read > 0)
756 return bytes_read;
757 }
Greg Claytondda4f7b2010-06-30 23:03:03758
759 if (process_is_valid)
760 {
Greg Claytonc749eb82011-07-11 05:12:02761 if (load_addr == LLDB_INVALID_ADDRESS)
762 load_addr = resolved_addr.GetLoadAddress (this);
763
Greg Claytondda4f7b2010-06-30 23:03:03764 if (load_addr == LLDB_INVALID_ADDRESS)
765 {
766 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
767 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
768 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
769 resolved_addr.GetFileAddress());
770 else
771 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
772 }
773 else
774 {
Greg Claytondb598232011-01-07 01:57:07775 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24776 if (bytes_read != dst_len)
777 {
778 if (error.Success())
779 {
780 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03781 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24782 else
Greg Claytondda4f7b2010-06-30 23:03:03783 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:24784 }
785 }
Greg Claytondda4f7b2010-06-30 23:03:03786 if (bytes_read)
Enrico Granata9128ee2f2011-09-06 19:20:51787 {
788 if (load_addr_ptr)
789 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03790 return bytes_read;
Enrico Granata9128ee2f2011-09-06 19:20:51791 }
Greg Claytondda4f7b2010-06-30 23:03:03792 // If the address is not section offset we have an address that
793 // doesn't resolve to any address in any currently loaded shared
794 // libaries and we failed to read memory so there isn't anything
795 // more we can do. If it is section offset, we might be able to
796 // read cached memory from the object file.
797 if (!resolved_addr.IsSectionOffset())
798 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24799 }
Chris Lattner30fdc8d2010-06-08 16:52:24800 }
Greg Claytondda4f7b2010-06-30 23:03:03801
Greg Claytonc749eb82011-07-11 05:12:02802 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03803 {
Greg Claytondb598232011-01-07 01:57:07804 // If we didn't already try and read from the object file cache, then
805 // try it after failing to read from the process.
806 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03807 }
808 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24809}
810
Greg Claytond16e1e52011-07-12 17:06:17811size_t
812Target::ReadScalarIntegerFromMemory (const Address& addr,
813 bool prefer_file_cache,
814 uint32_t byte_size,
815 bool is_signed,
816 Scalar &scalar,
817 Error &error)
818{
819 uint64_t uval;
820
821 if (byte_size <= sizeof(uval))
822 {
823 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
824 if (bytes_read == byte_size)
825 {
826 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
827 uint32_t offset = 0;
828 if (byte_size <= 4)
829 scalar = data.GetMaxU32 (&offset, byte_size);
830 else
831 scalar = data.GetMaxU64 (&offset, byte_size);
832
833 if (is_signed)
834 scalar.SignExtend(byte_size * 8);
835 return bytes_read;
836 }
837 }
838 else
839 {
840 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
841 }
842 return 0;
843}
844
845uint64_t
846Target::ReadUnsignedIntegerFromMemory (const Address& addr,
847 bool prefer_file_cache,
848 size_t integer_byte_size,
849 uint64_t fail_value,
850 Error &error)
851{
852 Scalar scalar;
853 if (ReadScalarIntegerFromMemory (addr,
854 prefer_file_cache,
855 integer_byte_size,
856 false,
857 scalar,
858 error))
859 return scalar.ULongLong(fail_value);
860 return fail_value;
861}
862
863bool
864Target::ReadPointerFromMemory (const Address& addr,
865 bool prefer_file_cache,
866 Error &error,
867 Address &pointer_addr)
868{
869 Scalar scalar;
870 if (ReadScalarIntegerFromMemory (addr,
871 prefer_file_cache,
872 m_arch.GetAddressByteSize(),
873 false,
874 scalar,
875 error))
876 {
877 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
878 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
879 {
880 if (m_section_load_list.IsEmpty())
881 {
882 // No sections are loaded, so we must assume we are not running
883 // yet and anything we are given is a file address.
884 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
885 }
886 else
887 {
888 // We have at least one section loaded. This can be becuase
889 // we have manually loaded some sections with "target modules load ..."
890 // or because we have have a live process that has sections loaded
891 // through the dynamic loader
892 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
893 }
894 // We weren't able to resolve the pointer value, so just return
895 // an address with no section
896 if (!pointer_addr.IsValid())
897 pointer_addr.SetOffset (pointer_vm_addr);
898 return true;
899
900 }
901 }
902 return false;
903}
Chris Lattner30fdc8d2010-06-08 16:52:24904
905ModuleSP
906Target::GetSharedModule
907(
908 const FileSpec& file_spec,
909 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:10910 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:24911 const ConstString *object_name,
912 off_t object_offset,
913 Error *error_ptr
914)
915{
916 // Don't pass in the UUID so we can tell if we have a stale value in our list
917 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
918 bool did_create_module = false;
919 ModuleSP module_sp;
920
Chris Lattner30fdc8d2010-06-08 16:52:24921 Error error;
922
Greg Clayton32e0a752011-03-30 18:16:51923 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner30fdc8d2010-06-08 16:52:24924 if (m_image_search_paths.GetSize())
925 {
926 FileSpec transformed_spec;
927 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
928 {
929 transformed_spec.GetFilename() = file_spec.GetFilename();
930 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
931 }
932 }
933
Greg Clayton32e0a752011-03-30 18:16:51934 // The platform is responsible for finding and caching an appropriate
935 // module in the shared module cache.
936 if (m_platform_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24937 {
Greg Clayton32e0a752011-03-30 18:16:51938 FileSpec platform_file_spec;
939 error = m_platform_sp->GetSharedModule (file_spec,
940 arch,
941 uuid_ptr,
942 object_name,
943 object_offset,
944 module_sp,
945 &old_module_sp,
946 &did_create_module);
947 }
948 else
949 {
950 error.SetErrorString("no platform is currently set");
Chris Lattner30fdc8d2010-06-08 16:52:24951 }
952
Greg Clayton32e0a752011-03-30 18:16:51953 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner30fdc8d2010-06-08 16:52:24954 if (module_sp)
955 {
956 m_images.Append (module_sp);
957 if (did_create_module)
958 {
959 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
960 ModuleUpdated(old_module_sp, module_sp);
961 else
962 ModuleAdded(module_sp);
963 }
964 }
965 if (error_ptr)
966 *error_ptr = error;
967 return module_sp;
968}
969
970
971Target *
972Target::CalculateTarget ()
973{
974 return this;
975}
976
977Process *
978Target::CalculateProcess ()
979{
980 return NULL;
981}
982
983Thread *
984Target::CalculateThread ()
985{
986 return NULL;
987}
988
989StackFrame *
990Target::CalculateStackFrame ()
991{
992 return NULL;
993}
994
995void
Greg Clayton0603aa92010-10-04 01:05:56996Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24997{
998 exe_ctx.target = this;
999 exe_ctx.process = NULL; // Do NOT fill in process...
1000 exe_ctx.thread = NULL;
1001 exe_ctx.frame = NULL;
1002}
1003
1004PathMappingList &
1005Target::GetImageSearchPathList ()
1006{
1007 return m_image_search_paths;
1008}
1009
1010void
1011Target::ImageSearchPathsChanged
1012(
1013 const PathMappingList &path_list,
1014 void *baton
1015)
1016{
1017 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:451018 ModuleSP exe_module_sp (target->GetExecutableModule());
1019 if (exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:241020 {
Greg Claytonaa149cb2011-08-11 02:48:451021 target->m_images.Clear();
1022 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:241023 }
1024}
1025
1026ClangASTContext *
1027Target::GetScratchClangASTContext()
1028{
Greg Clayton73da2442011-08-03 01:23:551029 // Now see if we know the target triple, and if so, create our scratch AST context:
1030 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1031 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:241032 return m_scratch_ast_context_ap.get();
1033}
Caroline Ticedaccaa92010-09-20 20:44:431034
Greg Clayton99d0faf2010-11-18 23:32:351035void
Caroline Tice20bd37f2011-03-10 22:14:101036Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:431037{
Greg Clayton99d0faf2010-11-18 23:32:351038 UserSettingsControllerSP &usc = GetSettingsController();
1039 usc.reset (new SettingsController);
1040 UserSettingsController::InitializeSettingsController (usc,
1041 SettingsController::global_settings_table,
1042 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:101043
1044 // Now call SettingsInitialize() on each 'child' setting of Target
1045 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:351046}
Caroline Ticedaccaa92010-09-20 20:44:431047
Greg Clayton99d0faf2010-11-18 23:32:351048void
Caroline Tice20bd37f2011-03-10 22:14:101049Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:351050{
Caroline Tice20bd37f2011-03-10 22:14:101051
1052 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1053
1054 Process::SettingsTerminate ();
1055
1056 // Now terminate Target Settings.
1057
Greg Clayton99d0faf2010-11-18 23:32:351058 UserSettingsControllerSP &usc = GetSettingsController();
1059 UserSettingsController::FinalizeSettingsController (usc);
1060 usc.reset();
1061}
Caroline Ticedaccaa92010-09-20 20:44:431062
Greg Clayton99d0faf2010-11-18 23:32:351063UserSettingsControllerSP &
1064Target::GetSettingsController ()
1065{
1066 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:431067 return g_settings_controller;
1068}
1069
1070ArchSpec
1071Target::GetDefaultArchitecture ()
1072{
Greg Clayton64195a22011-02-23 00:35:021073 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1074
1075 if (settings_controller_sp)
1076 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1077 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:431078}
1079
1080void
Greg Clayton64195a22011-02-23 00:35:021081Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:431082{
Greg Clayton64195a22011-02-23 00:35:021083 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1084
1085 if (settings_controller_sp)
1086 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:431087}
1088
Greg Clayton0603aa92010-10-04 01:05:561089Target *
1090Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1091{
1092 // The target can either exist in the "process" of ExecutionContext, or in
1093 // the "target_sp" member of SymbolContext. This accessor helper function
1094 // will get the target from one of these locations.
1095
1096 Target *target = NULL;
1097 if (sc_ptr != NULL)
1098 target = sc_ptr->target_sp.get();
1099 if (target == NULL)
1100 {
1101 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
1102 target = &exe_ctx_ptr->process->GetTarget();
1103 }
1104 return target;
1105}
1106
1107
Caroline Tice1559a462010-09-27 00:30:101108void
1109Target::UpdateInstanceName ()
1110{
1111 StreamString sstr;
1112
Greg Claytonaa149cb2011-08-11 02:48:451113 Module *exe_module = GetExecutableModulePointer();
1114 if (exe_module)
Caroline Tice1559a462010-09-27 00:30:101115 {
Greg Clayton307de252010-10-27 02:06:371116 sstr.Printf ("%s_%s",
Greg Claytonaa149cb2011-08-11 02:48:451117 exe_module->GetFileSpec().GetFilename().AsCString(),
1118 exe_module->GetArchitecture().GetArchitectureName());
1119 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:101120 }
1121}
1122
Sean Callanan322f5292010-10-29 00:29:031123const char *
1124Target::GetExpressionPrefixContentsAsCString ()
1125{
Greg Clayton7e14f912011-04-23 02:04:551126 if (m_expr_prefix_contents_sp)
1127 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1128 return NULL;
Sean Callanan322f5292010-10-29 00:29:031129}
1130
Greg Clayton8b2fe6d2010-12-14 02:59:591131ExecutionResults
1132Target::EvaluateExpression
1133(
1134 const char *expr_cstr,
1135 StackFrame *frame,
Sean Callanan3bfdaa22011-09-15 02:13:071136 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton8b2fe6d2010-12-14 02:59:591137 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:351138 bool keep_in_memory,
Jim Ingham2837b762011-05-04 03:43:181139 lldb::DynamicValueType use_dynamic,
Greg Clayton8b2fe6d2010-12-14 02:59:591140 lldb::ValueObjectSP &result_valobj_sp
1141)
1142{
1143 ExecutionResults execution_results = eExecutionSetupError;
1144
1145 result_valobj_sp.reset();
Jim Ingham6026ca32011-05-12 02:06:141146
1147 // We shouldn't run stop hooks in expressions.
1148 // Be sure to reset this if you return anywhere within this function.
1149 bool old_suppress_value = m_suppress_stop_hooks;
1150 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:591151
1152 ExecutionContext exe_ctx;
1153 if (frame)
1154 {
1155 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:081156 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:181157 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granata27b625e2011-08-09 01:04:561158 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1159 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham2837b762011-05-04 03:43:181160 lldb::VariableSP var_sp;
1161 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1162 use_dynamic,
1163 expr_path_options,
1164 var_sp,
1165 error);
Greg Clayton8b2fe6d2010-12-14 02:59:591166 }
1167 else if (m_process_sp)
1168 {
1169 m_process_sp->CalculateExecutionContext(exe_ctx);
1170 }
1171 else
1172 {
1173 CalculateExecutionContext(exe_ctx);
1174 }
1175
1176 if (result_valobj_sp)
1177 {
1178 execution_results = eExecutionCompleted;
1179 // We got a result from the frame variable expression path above...
1180 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1181
1182 lldb::ValueObjectSP const_valobj_sp;
1183
1184 // Check in case our value is already a constant value
1185 if (result_valobj_sp->GetIsConstant())
1186 {
1187 const_valobj_sp = result_valobj_sp;
1188 const_valobj_sp->SetName (persistent_variable_name);
1189 }
1190 else
Jim Ingham78a685a2011-04-16 00:01:131191 {
Jim Ingham2837b762011-05-04 03:43:181192 if (use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:131193 {
Jim Ingham2837b762011-05-04 03:43:181194 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:131195 if (dynamic_sp)
1196 result_valobj_sp = dynamic_sp;
1197 }
1198
Jim Ingham6035b672011-03-31 00:19:251199 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Ingham78a685a2011-04-16 00:01:131200 }
Greg Clayton8b2fe6d2010-12-14 02:59:591201
Sean Callanan92adcac2011-01-13 08:53:351202 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1203
Greg Clayton8b2fe6d2010-12-14 02:59:591204 result_valobj_sp = const_valobj_sp;
1205
Sean Callanan92adcac2011-01-13 08:53:351206 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1207 assert (clang_expr_variable_sp.get());
1208
1209 // Set flags and live data as appropriate
1210
1211 const Value &result_value = live_valobj_sp->GetValue();
1212
1213 switch (result_value.GetValueType())
1214 {
1215 case Value::eValueTypeHostAddress:
1216 case Value::eValueTypeFileAddress:
1217 // we don't do anything with these for now
1218 break;
1219 case Value::eValueTypeScalar:
1220 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1221 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1222 break;
1223 case Value::eValueTypeLoadAddress:
1224 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1225 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1226 break;
1227 }
Greg Clayton8b2fe6d2010-12-14 02:59:591228 }
1229 else
1230 {
1231 // Make sure we aren't just trying to see the value of a persistent
1232 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:351233 lldb::ClangExpressionVariableSP persistent_var_sp;
1234 // Only check for persistent variables the expression starts with a '$'
1235 if (expr_cstr[0] == '$')
1236 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1237
Greg Clayton8b2fe6d2010-12-14 02:59:591238 if (persistent_var_sp)
1239 {
1240 result_valobj_sp = persistent_var_sp->GetValueObject ();
1241 execution_results = eExecutionCompleted;
1242 }
1243 else
1244 {
1245 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan3bfdaa22011-09-15 02:13:071246
Greg Clayton8b2fe6d2010-12-14 02:59:591247 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:071248 execution_policy,
Sean Callanan92adcac2011-01-13 08:53:351249 unwind_on_error,
Greg Clayton8b2fe6d2010-12-14 02:59:591250 expr_cstr,
1251 prefix,
1252 result_valobj_sp);
1253 }
1254 }
Jim Ingham6026ca32011-05-12 02:06:141255
1256 m_suppress_stop_hooks = old_suppress_value;
1257
Greg Clayton8b2fe6d2010-12-14 02:59:591258 return execution_results;
1259}
1260
Greg Claytonf3ef3d22011-05-22 22:46:531261lldb::addr_t
1262Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1263{
1264 addr_t code_addr = load_addr;
1265 switch (m_arch.GetMachine())
1266 {
1267 case llvm::Triple::arm:
1268 case llvm::Triple::thumb:
1269 switch (addr_class)
1270 {
1271 case eAddressClassData:
1272 case eAddressClassDebug:
1273 return LLDB_INVALID_ADDRESS;
1274
1275 case eAddressClassUnknown:
1276 case eAddressClassInvalid:
1277 case eAddressClassCode:
1278 case eAddressClassCodeAlternateISA:
1279 case eAddressClassRuntime:
1280 // Check if bit zero it no set?
1281 if ((code_addr & 1ull) == 0)
1282 {
1283 // Bit zero isn't set, check if the address is a multiple of 2?
1284 if (code_addr & 2ull)
1285 {
1286 // The address is a multiple of 2 so it must be thumb, set bit zero
1287 code_addr |= 1ull;
1288 }
1289 else if (addr_class == eAddressClassCodeAlternateISA)
1290 {
1291 // We checked the address and the address claims to be the alternate ISA
1292 // which means thumb, so set bit zero.
1293 code_addr |= 1ull;
1294 }
1295 }
1296 break;
1297 }
1298 break;
1299
1300 default:
1301 break;
1302 }
1303 return code_addr;
1304}
1305
1306lldb::addr_t
1307Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1308{
1309 addr_t opcode_addr = load_addr;
1310 switch (m_arch.GetMachine())
1311 {
1312 case llvm::Triple::arm:
1313 case llvm::Triple::thumb:
1314 switch (addr_class)
1315 {
1316 case eAddressClassData:
1317 case eAddressClassDebug:
1318 return LLDB_INVALID_ADDRESS;
1319
1320 case eAddressClassInvalid:
1321 case eAddressClassUnknown:
1322 case eAddressClassCode:
1323 case eAddressClassCodeAlternateISA:
1324 case eAddressClassRuntime:
1325 opcode_addr &= ~(1ull);
1326 break;
1327 }
1328 break;
1329
1330 default:
1331 break;
1332 }
1333 return opcode_addr;
1334}
1335
Jim Ingham9575d842011-03-11 03:53:591336lldb::user_id_t
1337Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1338{
1339 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1340 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1341 m_stop_hooks[new_uid] = new_hook_sp;
1342 return new_uid;
1343}
1344
1345bool
1346Target::RemoveStopHookByID (lldb::user_id_t user_id)
1347{
1348 size_t num_removed;
1349 num_removed = m_stop_hooks.erase (user_id);
1350 if (num_removed == 0)
1351 return false;
1352 else
1353 return true;
1354}
1355
1356void
1357Target::RemoveAllStopHooks ()
1358{
1359 m_stop_hooks.clear();
1360}
1361
1362Target::StopHookSP
1363Target::GetStopHookByID (lldb::user_id_t user_id)
1364{
1365 StopHookSP found_hook;
1366
1367 StopHookCollection::iterator specified_hook_iter;
1368 specified_hook_iter = m_stop_hooks.find (user_id);
1369 if (specified_hook_iter != m_stop_hooks.end())
1370 found_hook = (*specified_hook_iter).second;
1371 return found_hook;
1372}
1373
1374bool
1375Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1376{
1377 StopHookCollection::iterator specified_hook_iter;
1378 specified_hook_iter = m_stop_hooks.find (user_id);
1379 if (specified_hook_iter == m_stop_hooks.end())
1380 return false;
1381
1382 (*specified_hook_iter).second->SetIsActive (active_state);
1383 return true;
1384}
1385
1386void
1387Target::SetAllStopHooksActiveState (bool active_state)
1388{
1389 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1390 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1391 {
1392 (*pos).second->SetIsActive (active_state);
1393 }
1394}
1395
1396void
1397Target::RunStopHooks ()
1398{
Jim Ingham6026ca32011-05-12 02:06:141399 if (m_suppress_stop_hooks)
1400 return;
1401
Jim Ingham9575d842011-03-11 03:53:591402 if (!m_process_sp)
1403 return;
1404
1405 if (m_stop_hooks.empty())
1406 return;
1407
1408 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1409
1410 // If there aren't any active stop hooks, don't bother either:
1411 bool any_active_hooks = false;
1412 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1413 {
1414 if ((*pos).second->IsActive())
1415 {
1416 any_active_hooks = true;
1417 break;
1418 }
1419 }
1420 if (!any_active_hooks)
1421 return;
1422
1423 CommandReturnObject result;
1424
1425 std::vector<ExecutionContext> exc_ctx_with_reasons;
1426 std::vector<SymbolContext> sym_ctx_with_reasons;
1427
1428 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1429 size_t num_threads = cur_threadlist.GetSize();
1430 for (size_t i = 0; i < num_threads; i++)
1431 {
1432 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1433 if (cur_thread_sp->ThreadStoppedForAReason())
1434 {
1435 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1436 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1437 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1438 }
1439 }
1440
1441 // If no threads stopped for a reason, don't run the stop-hooks.
1442 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1443 if (num_exe_ctx == 0)
1444 return;
1445
Jim Ingham5b52f0c2011-06-02 23:58:261446 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1447 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:591448
1449 bool keep_going = true;
1450 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271451 bool print_hook_header;
1452 bool print_thread_header;
1453
1454 if (num_exe_ctx == 1)
1455 print_thread_header = false;
1456 else
1457 print_thread_header = true;
1458
1459 if (m_stop_hooks.size() == 1)
1460 print_hook_header = false;
1461 else
1462 print_hook_header = true;
1463
Jim Ingham9575d842011-03-11 03:53:591464 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1465 {
1466 // result.Clear();
1467 StopHookSP cur_hook_sp = (*pos).second;
1468 if (!cur_hook_sp->IsActive())
1469 continue;
1470
1471 bool any_thread_matched = false;
1472 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1473 {
1474 if ((cur_hook_sp->GetSpecifier () == NULL
1475 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1476 && (cur_hook_sp->GetThreadSpecifier() == NULL
1477 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1478 {
1479 if (!hooks_ran)
1480 {
Jim Ingham381e25b2011-03-22 01:47:271481 result.AppendMessage("\n** Stop Hooks **");
Jim Ingham9575d842011-03-11 03:53:591482 hooks_ran = true;
1483 }
Jim Ingham381e25b2011-03-22 01:47:271484 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591485 {
1486 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1487 any_thread_matched = true;
1488 }
1489
Jim Ingham381e25b2011-03-22 01:47:271490 if (print_thread_header)
1491 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591492
1493 bool stop_on_continue = true;
1494 bool stop_on_error = true;
1495 bool echo_commands = false;
1496 bool print_results = true;
1497 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton32e0a752011-03-30 18:16:511498 &exc_ctx_with_reasons[i],
1499 stop_on_continue,
1500 stop_on_error,
1501 echo_commands,
1502 print_results,
1503 result);
Jim Ingham9575d842011-03-11 03:53:591504
1505 // If the command started the target going again, we should bag out of
1506 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:511507 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1508 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:591509 {
1510 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1511 keep_going = false;
1512 }
1513 }
1514 }
1515 }
1516 if (hooks_ran)
1517 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice969ed3d12011-05-02 20:41:461518
1519 result.GetImmediateOutputStream()->Flush();
1520 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:591521}
1522
Greg Clayton7b242382011-07-08 00:48:091523bool
1524Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1525{
1526 bool changed = false;
1527 if (module)
1528 {
1529 ObjectFile *object_file = module->GetObjectFile();
1530 if (object_file)
1531 {
1532 SectionList *section_list = object_file->GetSectionList ();
1533 if (section_list)
1534 {
1535 // All sections listed in the dyld image info structure will all
1536 // either be fixed up already, or they will all be off by a single
1537 // slide amount that is determined by finding the first segment
1538 // that is at file offset zero which also has bytes (a file size
1539 // that is greater than zero) in the object file.
1540
1541 // Determine the slide amount (if any)
1542 const size_t num_sections = section_list->GetSize();
1543 size_t sect_idx = 0;
1544 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1545 {
1546 // Iterate through the object file sections to find the
1547 // first section that starts of file offset zero and that
1548 // has bytes in the file...
1549 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1550 if (section)
1551 {
1552 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1553 changed = true;
1554 }
1555 }
1556 }
1557 }
1558 }
1559 return changed;
1560}
1561
1562
Jim Ingham9575d842011-03-11 03:53:591563//--------------------------------------------------------------
1564// class Target::StopHook
1565//--------------------------------------------------------------
1566
1567
1568Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1569 UserID (uid),
1570 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:591571 m_commands (),
1572 m_specifier_sp (),
Stephen Wilson71c21d12011-04-11 19:41:401573 m_thread_spec_ap(NULL),
1574 m_active (true)
Jim Ingham9575d842011-03-11 03:53:591575{
1576}
1577
1578Target::StopHook::StopHook (const StopHook &rhs) :
1579 UserID (rhs.GetID()),
1580 m_target_sp (rhs.m_target_sp),
1581 m_commands (rhs.m_commands),
1582 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilson71c21d12011-04-11 19:41:401583 m_thread_spec_ap (NULL),
1584 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:591585{
1586 if (rhs.m_thread_spec_ap.get() != NULL)
1587 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1588}
1589
1590
1591Target::StopHook::~StopHook ()
1592{
1593}
1594
1595void
1596Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1597{
1598 m_thread_spec_ap.reset (specifier);
1599}
1600
1601
1602void
1603Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1604{
1605 int indent_level = s->GetIndentLevel();
1606
1607 s->SetIndentLevel(indent_level + 2);
1608
1609 s->Printf ("Hook: %d\n", GetID());
1610 if (m_active)
1611 s->Indent ("State: enabled\n");
1612 else
1613 s->Indent ("State: disabled\n");
1614
1615 if (m_specifier_sp)
1616 {
1617 s->Indent();
1618 s->PutCString ("Specifier:\n");
1619 s->SetIndentLevel (indent_level + 4);
1620 m_specifier_sp->GetDescription (s, level);
1621 s->SetIndentLevel (indent_level + 2);
1622 }
1623
1624 if (m_thread_spec_ap.get() != NULL)
1625 {
1626 StreamString tmp;
1627 s->Indent("Thread:\n");
1628 m_thread_spec_ap->GetDescription (&tmp, level);
1629 s->SetIndentLevel (indent_level + 4);
1630 s->Indent (tmp.GetData());
1631 s->PutCString ("\n");
1632 s->SetIndentLevel (indent_level + 2);
1633 }
1634
1635 s->Indent ("Commands: \n");
1636 s->SetIndentLevel (indent_level + 4);
1637 uint32_t num_commands = m_commands.GetSize();
1638 for (uint32_t i = 0; i < num_commands; i++)
1639 {
1640 s->Indent(m_commands.GetStringAtIndex(i));
1641 s->PutCString ("\n");
1642 }
1643 s->SetIndentLevel (indent_level);
1644}
1645
1646
Caroline Ticedaccaa92010-09-20 20:44:431647//--------------------------------------------------------------
1648// class Target::SettingsController
1649//--------------------------------------------------------------
1650
1651Target::SettingsController::SettingsController () :
1652 UserSettingsController ("target", Debugger::GetSettingsController()),
1653 m_default_architecture ()
1654{
1655 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1656 InstanceSettings::GetDefaultName().AsCString()));
1657}
1658
1659Target::SettingsController::~SettingsController ()
1660{
1661}
1662
1663lldb::InstanceSettingsSP
1664Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1665{
Greg Claytondbe54502010-11-19 03:46:011666 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1667 false,
1668 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431669 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1670 return new_settings_sp;
1671}
1672
Caroline Ticedaccaa92010-09-20 20:44:431673
Jim Ingham78a685a2011-04-16 00:01:131674#define TSC_DEFAULT_ARCH "default-arch"
1675#define TSC_EXPR_PREFIX "expr-prefix"
Jim Ingham78a685a2011-04-16 00:01:131676#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton385aa282011-04-22 03:55:061677#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Clayton7e14f912011-04-23 02:04:551678#define TSC_SOURCE_MAP "source-map"
Enrico Granata22c55d12011-08-12 02:00:061679#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata9128ee2f2011-09-06 19:20:511680#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytonbfe5f3b2011-02-18 01:44:251681
1682
1683static const ConstString &
1684GetSettingNameForDefaultArch ()
1685{
1686 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytonbfe5f3b2011-02-18 01:44:251687 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431688}
1689
Greg Claytonbfe5f3b2011-02-18 01:44:251690static const ConstString &
1691GetSettingNameForExpressionPrefix ()
1692{
1693 static ConstString g_const_string (TSC_EXPR_PREFIX);
1694 return g_const_string;
1695}
1696
1697static const ConstString &
Jim Ingham78a685a2011-04-16 00:01:131698GetSettingNameForPreferDynamicValue ()
1699{
1700 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1701 return g_const_string;
1702}
1703
Greg Clayton7e14f912011-04-23 02:04:551704static const ConstString &
1705GetSettingNameForSourcePathMap ()
1706{
1707 static ConstString g_const_string (TSC_SOURCE_MAP);
1708 return g_const_string;
1709}
Greg Claytonbfe5f3b2011-02-18 01:44:251710
Greg Clayton385aa282011-04-22 03:55:061711static const ConstString &
1712GetSettingNameForSkipPrologue ()
1713{
1714 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1715 return g_const_string;
1716}
1717
Enrico Granata22c55d12011-08-12 02:00:061718static const ConstString &
1719GetSettingNameForMaxChildren ()
1720{
1721 static ConstString g_const_string (TSC_MAX_CHILDREN);
1722 return g_const_string;
1723}
Greg Clayton385aa282011-04-22 03:55:061724
Enrico Granata9128ee2f2011-09-06 19:20:511725static const ConstString &
1726GetSettingNameForMaxStringSummaryLength ()
1727{
1728 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1729 return g_const_string;
1730}
Greg Clayton385aa282011-04-22 03:55:061731
Caroline Ticedaccaa92010-09-20 20:44:431732bool
1733Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1734 const char *index_value,
1735 const char *value,
1736 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541737 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431738 Error&err)
1739{
Greg Claytonbfe5f3b2011-02-18 01:44:251740 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431741 {
Greg Claytoneb0103f2011-04-07 22:46:351742 m_default_architecture.SetTriple (value, NULL);
Greg Clayton64195a22011-02-23 00:35:021743 if (!m_default_architecture.IsValid())
1744 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431745 }
1746 return true;
1747}
1748
1749
1750bool
1751Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1752 StringList &value,
1753 Error &err)
1754{
Greg Claytonbfe5f3b2011-02-18 01:44:251755 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431756 {
Greg Clayton307de252010-10-27 02:06:371757 // If the arch is invalid (the default), don't show a string for it
1758 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021759 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431760 return true;
1761 }
1762 else
1763 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1764
1765 return false;
1766}
1767
1768//--------------------------------------------------------------
1769// class TargetInstanceSettings
1770//--------------------------------------------------------------
1771
Greg Clayton85851dd2010-12-04 00:10:171772TargetInstanceSettings::TargetInstanceSettings
1773(
1774 UserSettingsController &owner,
1775 bool live_instance,
1776 const char *name
1777) :
Greg Claytonbfe5f3b2011-02-18 01:44:251778 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Clayton7e14f912011-04-23 02:04:551779 m_expr_prefix_file (),
1780 m_expr_prefix_contents_sp (),
Jim Ingham2837b762011-05-04 03:43:181781 m_prefer_dynamic_value (2),
Greg Clayton7e14f912011-04-23 02:04:551782 m_skip_prologue (true, true),
Enrico Granata22c55d12011-08-12 02:00:061783 m_source_map (NULL, NULL),
Enrico Granata9128ee2f2011-09-06 19:20:511784 m_max_children_display(256),
1785 m_max_strlen_length(1024)
Caroline Ticedaccaa92010-09-20 20:44:431786{
1787 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1788 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1789 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1790 // This is true for CreateInstanceName() too.
1791
1792 if (GetInstanceName () == InstanceSettings::InvalidName())
1793 {
1794 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1795 m_owner.RegisterInstanceSettings (this);
1796 }
1797
1798 if (live_instance)
1799 {
1800 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1801 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:431802 }
1803}
1804
1805TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Clayton7e14f912011-04-23 02:04:551806 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1807 m_expr_prefix_file (rhs.m_expr_prefix_file),
1808 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1809 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1810 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata22c55d12011-08-12 02:00:061811 m_source_map (rhs.m_source_map),
Enrico Granata9128ee2f2011-09-06 19:20:511812 m_max_children_display(rhs.m_max_children_display),
1813 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Ticedaccaa92010-09-20 20:44:431814{
1815 if (m_instance_name != InstanceSettings::GetDefaultName())
1816 {
1817 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1818 CopyInstanceSettings (pending_settings,false);
Caroline Ticedaccaa92010-09-20 20:44:431819 }
1820}
1821
1822TargetInstanceSettings::~TargetInstanceSettings ()
1823{
1824}
1825
1826TargetInstanceSettings&
1827TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1828{
1829 if (this != &rhs)
1830 {
1831 }
1832
1833 return *this;
1834}
1835
Caroline Ticedaccaa92010-09-20 20:44:431836void
1837TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1838 const char *index_value,
1839 const char *value,
1840 const ConstString &instance_name,
1841 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541842 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431843 Error &err,
1844 bool pending)
1845{
Greg Claytonbfe5f3b2011-02-18 01:44:251846 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031847 {
Greg Clayton7e14f912011-04-23 02:04:551848 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1849 if (err.Success())
Sean Callanan322f5292010-10-29 00:29:031850 {
Greg Clayton7e14f912011-04-23 02:04:551851 switch (op)
Sean Callanan322f5292010-10-29 00:29:031852 {
Greg Clayton7e14f912011-04-23 02:04:551853 default:
1854 break;
1855 case eVarSetOperationAssign:
1856 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:031857 {
Greg Clayton7e14f912011-04-23 02:04:551858 if (!m_expr_prefix_file.GetCurrentValue().Exists())
1859 {
1860 err.SetErrorToGenericError ();
1861 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1862 return;
1863 }
1864
1865 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
1866
1867 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
1868 {
1869 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
1870 m_expr_prefix_contents_sp.reset();
1871 }
Sean Callanan322f5292010-10-29 00:29:031872 }
Greg Clayton7e14f912011-04-23 02:04:551873 break;
1874 case eVarSetOperationClear:
1875 m_expr_prefix_contents_sp.reset();
Sean Callanan322f5292010-10-29 00:29:031876 }
Sean Callanan322f5292010-10-29 00:29:031877 }
1878 }
Jim Ingham78a685a2011-04-16 00:01:131879 else if (var_name == GetSettingNameForPreferDynamicValue())
1880 {
Jim Ingham2837b762011-05-04 03:43:181881 int new_value;
1882 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
1883 if (err.Success())
1884 m_prefer_dynamic_value = new_value;
Greg Clayton385aa282011-04-22 03:55:061885 }
1886 else if (var_name == GetSettingNameForSkipPrologue())
1887 {
Greg Clayton7e14f912011-04-23 02:04:551888 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
1889 }
Enrico Granata22c55d12011-08-12 02:00:061890 else if (var_name == GetSettingNameForMaxChildren())
1891 {
1892 bool ok;
1893 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1894 if (ok)
1895 m_max_children_display = new_value;
1896 }
Enrico Granata9128ee2f2011-09-06 19:20:511897 else if (var_name == GetSettingNameForMaxStringSummaryLength())
1898 {
1899 bool ok;
1900 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
1901 if (ok)
1902 m_max_strlen_length = new_value;
1903 }
Greg Clayton7e14f912011-04-23 02:04:551904 else if (var_name == GetSettingNameForSourcePathMap ())
1905 {
1906 switch (op)
1907 {
1908 case eVarSetOperationReplace:
1909 case eVarSetOperationInsertBefore:
1910 case eVarSetOperationInsertAfter:
1911 case eVarSetOperationRemove:
1912 default:
1913 break;
1914 case eVarSetOperationAssign:
1915 m_source_map.Clear(true);
1916 // Fall through to append....
1917 case eVarSetOperationAppend:
1918 {
1919 Args args(value);
1920 const uint32_t argc = args.GetArgumentCount();
1921 if (argc & 1 || argc == 0)
1922 {
1923 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
1924 }
1925 else
1926 {
1927 char resolved_new_path[PATH_MAX];
1928 FileSpec file_spec;
1929 const char *old_path;
1930 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
1931 {
1932 const char *new_path = args.GetArgumentAtIndex(idx+1);
1933 assert (new_path); // We have an even number of paths, this shouldn't happen!
1934
1935 file_spec.SetFile(new_path, true);
1936 if (file_spec.Exists())
1937 {
1938 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
1939 {
1940 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
1941 return;
1942 }
1943 }
1944 else
1945 {
1946 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
1947 return;
1948 }
1949 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
1950 }
1951 }
1952 }
1953 break;
1954
1955 case eVarSetOperationClear:
1956 m_source_map.Clear(true);
1957 break;
1958 }
Jim Ingham78a685a2011-04-16 00:01:131959 }
Caroline Ticedaccaa92010-09-20 20:44:431960}
1961
1962void
Greg Claytonbfe5f3b2011-02-18 01:44:251963TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431964{
Sean Callanan322f5292010-10-29 00:29:031965 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1966
1967 if (!new_settings_ptr)
1968 return;
1969
Greg Clayton7e14f912011-04-23 02:04:551970 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
1971 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
1972 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
1973 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata22c55d12011-08-12 02:00:061974 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata9128ee2f2011-09-06 19:20:511975 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Ticedaccaa92010-09-20 20:44:431976}
1977
Caroline Tice12cecd72010-09-20 21:37:421978bool
Caroline Ticedaccaa92010-09-20 20:44:431979TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1980 const ConstString &var_name,
1981 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421982 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431983{
Greg Claytonbfe5f3b2011-02-18 01:44:251984 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031985 {
Greg Clayton7e14f912011-04-23 02:04:551986 char path[PATH_MAX];
1987 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
1988 if (path_len > 0)
1989 value.AppendString (path, path_len);
Sean Callanan322f5292010-10-29 00:29:031990 }
Jim Ingham78a685a2011-04-16 00:01:131991 else if (var_name == GetSettingNameForPreferDynamicValue())
1992 {
Jim Ingham2837b762011-05-04 03:43:181993 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Ingham78a685a2011-04-16 00:01:131994 }
Greg Clayton385aa282011-04-22 03:55:061995 else if (var_name == GetSettingNameForSkipPrologue())
1996 {
1997 if (m_skip_prologue)
1998 value.AppendString ("true");
1999 else
2000 value.AppendString ("false");
2001 }
Greg Clayton7e14f912011-04-23 02:04:552002 else if (var_name == GetSettingNameForSourcePathMap ())
2003 {
2004 }
Enrico Granata22c55d12011-08-12 02:00:062005 else if (var_name == GetSettingNameForMaxChildren())
2006 {
2007 StreamString count_str;
2008 count_str.Printf ("%d", m_max_children_display);
2009 value.AppendString (count_str.GetData());
2010 }
Enrico Granata9128ee2f2011-09-06 19:20:512011 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2012 {
2013 StreamString count_str;
2014 count_str.Printf ("%d", m_max_strlen_length);
2015 value.AppendString (count_str.GetData());
2016 }
Sean Callanan322f5292010-10-29 00:29:032017 else
2018 {
2019 if (err)
2020 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2021 return false;
2022 }
2023
2024 return true;
Caroline Ticedaccaa92010-09-20 20:44:432025}
2026
2027const ConstString
2028TargetInstanceSettings::CreateInstanceName ()
2029{
Caroline Ticedaccaa92010-09-20 20:44:432030 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:102031 static int instance_count = 1;
2032
Caroline Ticedaccaa92010-09-20 20:44:432033 sstr.Printf ("target_%d", instance_count);
2034 ++instance_count;
2035
2036 const ConstString ret_val (sstr.GetData());
2037 return ret_val;
2038}
2039
2040//--------------------------------------------------
2041// Target::SettingsController Variable Tables
2042//--------------------------------------------------
Jim Ingham2837b762011-05-04 03:43:182043OptionEnumValueElement
2044TargetInstanceSettings::g_dynamic_value_types[] =
2045{
Greg Clayton5d2fbfe2011-05-30 00:39:482046{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2047{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2048{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham2837b762011-05-04 03:43:182049{ 0, NULL, NULL }
2050};
Caroline Ticedaccaa92010-09-20 20:44:432051
2052SettingEntry
2053Target::SettingsController::global_settings_table[] =
2054{
Greg Claytonbfe5f3b2011-02-18 01:44:252055 // var-name var-type default enum init'd hidden help-text
2056 // ================= ================== =========== ==== ====== ====== =========================================================================
2057 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2058 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2059};
2060
Caroline Ticedaccaa92010-09-20 20:44:432061SettingEntry
2062Target::SettingsController::instance_settings_table[] =
2063{
Enrico Granata9128ee2f2011-09-06 19:20:512064 // var-name var-type default enum init'd hidden help-text
2065 // ================= ================== =============== ======================= ====== ====== =========================================================================
2066 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2067 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2068 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2069 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2070 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2071 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2072 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:432073};