blob: 154946206d98cf05dfbe1e87cbd0cc1a8e7c83ca [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton427f2902010-12-14 02:59:59 +000020#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Caroline Tice4a348082011-05-02 20:41:46 +000023#include "lldb/Core/StreamAsynchronousIO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000025#include "lldb/Core/Timer.h"
26#include "lldb/Core/ValueObject.h"
Greg Claytonf15996e2011-04-07 22:46:35 +000027#include "lldb/Expression/ClangUserExpression.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000029#include "lldb/Interpreter/CommandInterpreter.h"
30#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/lldb-private-log.h"
32#include "lldb/Symbol/ObjectFile.h"
33#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000034#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000035#include "lldb/Target/Thread.h"
36#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
41//----------------------------------------------------------------------
42// Target constructor
43//----------------------------------------------------------------------
Greg Clayton24bc5d92011-03-30 18:16:51 +000044Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp) :
45 Broadcaster ("lldb.target"),
46 ExecutionContextScope (),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000047 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000048 m_debugger (debugger),
Greg Clayton24bc5d92011-03-30 18:16:51 +000049 m_platform_sp (platform_sp),
Greg Claytonbdcda462010-12-20 20:49:23 +000050 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton24bc5d92011-03-30 18:16:51 +000051 m_arch (target_arch),
52 m_images (),
Greg Claytoneea26402010-09-14 23:36:40 +000053 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000054 m_breakpoint_list (false),
55 m_internal_breakpoint_list (true),
Johnny Chen9a3c2a52011-09-06 20:05:25 +000056 m_watchpoint_location_list (),
Greg Clayton24bc5d92011-03-30 18:16:51 +000057 m_process_sp (),
58 m_search_filter_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +000059 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000060 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000061 m_persistent_variables (),
Jim Inghamcc637462011-09-13 00:29:56 +000062 m_source_manager(*this),
Greg Clayton24bc5d92011-03-30 18:16:51 +000063 m_stop_hooks (),
Jim Ingham3613ae12011-05-12 02:06:14 +000064 m_stop_hook_next_id (0),
65 m_suppress_stop_hooks (false)
Chris Lattner24943d22010-06-08 16:52:24 +000066{
Greg Clayton49ce6822010-10-31 03:01:06 +000067 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
68 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
69 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
70
Greg Claytone005f2c2010-11-06 01:53:30 +000071 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000072 if (log)
73 log->Printf ("%p Target::Target()", this);
74}
75
76//----------------------------------------------------------------------
77// Destructor
78//----------------------------------------------------------------------
79Target::~Target()
80{
Greg Claytone005f2c2010-11-06 01:53:30 +000081 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000082 if (log)
83 log->Printf ("%p Target::~Target()", this);
84 DeleteCurrentProcess ();
85}
86
87void
Caroline Tice7826c882010-10-26 03:11:13 +000088Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000089{
Greg Clayton3fed8b92010-10-08 00:21:05 +000090// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000091 if (description_level != lldb::eDescriptionLevelBrief)
92 {
93 s->Indent();
94 s->PutCString("Target\n");
95 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000096 m_images.Dump(s);
97 m_breakpoint_list.Dump(s);
98 m_internal_breakpoint_list.Dump(s);
99 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +0000100 }
101 else
102 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000103 Module *exe_module = GetExecutableModulePointer();
104 if (exe_module)
105 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham53fe9cc2011-05-12 01:12:28 +0000106 else
107 s->PutCString ("No executable module.");
Caroline Tice7826c882010-10-26 03:11:13 +0000108 }
Chris Lattner24943d22010-06-08 16:52:24 +0000109}
110
111void
112Target::DeleteCurrentProcess ()
113{
114 if (m_process_sp.get())
115 {
Greg Clayton49480b12010-09-14 23:52:43 +0000116 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000117 if (m_process_sp->IsAlive())
118 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000119
120 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000121
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 Chenda5a8022011-09-20 23:28:55 +0000127 m_watchpoint_location_list.RemoveAll();
Chris Lattner24943d22010-06-08 16:52:24 +0000128 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 Clayton987c7eb2011-09-17 08:33:22 +0000149 // 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 Lattner24943d22010-06-08 16:52:24 +0000152}
153
Greg Clayton153ccd72011-08-10 02:10:13 +0000154void
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 Lattner24943d22010-06-08 16:52:24 +0000177BreakpointList &
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 Clayton33ed1702010-08-24 00:45:41 +0000218Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000219{
Chris Lattner24943d22010-06-08 16:52:24 +0000220 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 Clayton33ed1702010-08-24 00:45:41 +0000224 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000225 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000226 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 Lattner24943d22010-06-08 16:52:24 +0000232 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 Clayton7dd98df2011-07-12 17:06:17 +0000245Target::CreateBreakpoint (const FileSpec *containingModule,
246 const char *func_name,
247 uint32_t func_name_type_mask,
248 bool internal,
249 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000250{
Greg Clayton12bec712010-06-28 21:30:43 +0000251 BreakpointSP bp_sp;
252 if (func_name)
253 {
254 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000255
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 Clayton12bec712010-06-28 21:30:43 +0000261 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
262 }
263 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000264}
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 Clayton7dd98df2011-07-12 17:06:17 +0000288Target::CreateBreakpoint (const FileSpec *containingModule,
289 RegularExpression &func_regex,
290 bool internal,
291 LazyBool skip_prologue)
Chris Lattner24943d22010-06-08 16:52:24 +0000292{
293 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
Greg Clayton7dd98df2011-07-12 17:06:17 +0000294 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
295 func_regex,
296 skip_prologue == eLazyBoolCalculate ? GetSkipPrologue() : skip_prologue));
Chris Lattner24943d22010-06-08 16:52:24 +0000297
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 Claytonc7f5d5c2010-07-23 23:33:17 +0000311 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000312 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000313 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000314
Greg Claytone005f2c2010-11-06 01:53:30 +0000315 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000316 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 Lattner24943d22010-06-08 16:52:24 +0000323 bp_sp->ResolveBreakpoint();
324 }
Jim Inghamd1686902010-10-14 23:45:03 +0000325
326 if (!internal && bp_sp)
327 {
328 m_last_created_breakpoint = bp_sp;
329 }
330
Chris Lattner24943d22010-06-08 16:52:24 +0000331 return bp_sp;
332}
333
Johnny Chenda5a8022011-09-20 23:28:55 +0000334bool
335Target::ProcessIsValid()
336{
337 return (m_process_sp && m_process_sp->IsAlive());
338}
339
Johnny Chen87ff53b2011-09-14 00:26:03 +0000340// See also WatchpointLocation::SetWatchpointType(uint32_t type) and
341// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen34bbf852011-09-12 23:38:44 +0000342WatchpointLocationSP
343Target::CreateWatchpointLocation(lldb::addr_t addr, size_t size, uint32_t type)
344{
Johnny Chen5b2fc572011-09-14 20:23:45 +0000345 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
346 if (log)
347 log->Printf("Target::%s (addr = 0x%8.8llx size = %zu type = %u)\n",
348 __FUNCTION__, addr, size, type);
349
Johnny Chen34bbf852011-09-12 23:38:44 +0000350 WatchpointLocationSP wp_loc_sp;
Johnny Chenda5a8022011-09-20 23:28:55 +0000351 if (!ProcessIsValid())
Johnny Chen61286a52011-09-13 18:30:59 +0000352 return wp_loc_sp;
Johnny Chen22a56cc2011-09-14 22:20:15 +0000353 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chen34bbf852011-09-12 23:38:44 +0000354 return wp_loc_sp;
Johnny Chen9bf11992011-09-13 01:15:36 +0000355
Johnny Chen87ff53b2011-09-14 00:26:03 +0000356 // Currently we only support one watchpoint location per address, with total
357 // number of watchpoint locations limited by the hardware which the inferior
358 // is running on.
Johnny Chen69b6ec82011-09-13 23:29:31 +0000359 WatchpointLocationSP matched_sp = m_watchpoint_location_list.FindByAddress(addr);
360 if (matched_sp)
361 {
Johnny Chen5b2fc572011-09-14 20:23:45 +0000362 size_t old_size = matched_sp->GetByteSize();
Johnny Chen69b6ec82011-09-13 23:29:31 +0000363 uint32_t old_type =
Johnny Chen5b2fc572011-09-14 20:23:45 +0000364 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
365 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen22a56cc2011-09-14 22:20:15 +0000366 // Return the existing watchpoint location if both size and type match.
367 if (size == old_size && type == old_type) {
368 wp_loc_sp = matched_sp;
369 wp_loc_sp->SetEnabled(false);
370 } else {
371 // Nil the matched watchpoint location; we will be creating a new one.
372 m_process_sp->DisableWatchpoint(matched_sp.get());
373 m_watchpoint_location_list.Remove(matched_sp->GetID());
374 }
Johnny Chen69b6ec82011-09-13 23:29:31 +0000375 }
376
Johnny Chen22a56cc2011-09-14 22:20:15 +0000377 if (!wp_loc_sp) {
378 WatchpointLocation *new_loc = new WatchpointLocation(addr, size);
379 if (!new_loc) {
380 printf("WatchpointLocation ctor failed, out of memory?\n");
381 return wp_loc_sp;
382 }
383 new_loc->SetWatchpointType(type);
384 wp_loc_sp.reset(new_loc);
385 m_watchpoint_location_list.Add(wp_loc_sp);
386 }
Johnny Chen5b2fc572011-09-14 20:23:45 +0000387
Johnny Chen5b2fc572011-09-14 20:23:45 +0000388 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
Johnny Chen5b2fc572011-09-14 20:23:45 +0000389 if (log)
390 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
391 __FUNCTION__,
392 rc.Success() ? "succeeded" : "failed",
393 wp_loc_sp->GetID());
394
Johnny Chen22a56cc2011-09-14 22:20:15 +0000395 if (rc.Fail()) wp_loc_sp.reset();
Johnny Chen9bf11992011-09-13 01:15:36 +0000396 return wp_loc_sp;
Johnny Chen34bbf852011-09-12 23:38:44 +0000397}
398
Chris Lattner24943d22010-06-08 16:52:24 +0000399void
400Target::RemoveAllBreakpoints (bool internal_also)
401{
Greg Claytone005f2c2010-11-06 01:53:30 +0000402 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000403 if (log)
404 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
405
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000406 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000407 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000408 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000409
410 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000411}
412
413void
414Target::DisableAllBreakpoints (bool internal_also)
415{
Greg Claytone005f2c2010-11-06 01:53:30 +0000416 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000417 if (log)
418 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
419
420 m_breakpoint_list.SetEnabledAll (false);
421 if (internal_also)
422 m_internal_breakpoint_list.SetEnabledAll (false);
423}
424
425void
426Target::EnableAllBreakpoints (bool internal_also)
427{
Greg Claytone005f2c2010-11-06 01:53:30 +0000428 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000429 if (log)
430 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
431
432 m_breakpoint_list.SetEnabledAll (true);
433 if (internal_also)
434 m_internal_breakpoint_list.SetEnabledAll (true);
435}
436
437bool
438Target::RemoveBreakpointByID (break_id_t break_id)
439{
Greg Claytone005f2c2010-11-06 01:53:30 +0000440 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000441 if (log)
442 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
443
444 if (DisableBreakpointByID (break_id))
445 {
446 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000447 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000448 else
Jim Inghamd1686902010-10-14 23:45:03 +0000449 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000450 if (m_last_created_breakpoint)
451 {
452 if (m_last_created_breakpoint->GetID() == break_id)
453 m_last_created_breakpoint.reset();
454 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000455 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000456 }
Chris Lattner24943d22010-06-08 16:52:24 +0000457 return true;
458 }
459 return false;
460}
461
462bool
463Target::DisableBreakpointByID (break_id_t break_id)
464{
Greg Claytone005f2c2010-11-06 01:53:30 +0000465 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000466 if (log)
467 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
468
469 BreakpointSP bp_sp;
470
471 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
472 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
473 else
474 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
475 if (bp_sp)
476 {
477 bp_sp->SetEnabled (false);
478 return true;
479 }
480 return false;
481}
482
483bool
484Target::EnableBreakpointByID (break_id_t break_id)
485{
Greg Claytone005f2c2010-11-06 01:53:30 +0000486 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000487 if (log)
488 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
489 __FUNCTION__,
490 break_id,
491 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
492
493 BreakpointSP bp_sp;
494
495 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
496 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
497 else
498 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
499
500 if (bp_sp)
501 {
502 bp_sp->SetEnabled (true);
503 return true;
504 }
505 return false;
506}
507
Johnny Chenda5a8022011-09-20 23:28:55 +0000508// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
509bool
510Target::RemoveAllWatchpointLocations ()
511{
512 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
513 if (log)
514 log->Printf ("Target::%s\n", __FUNCTION__);
515
516 if (!ProcessIsValid())
517 return false;
518
519 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
520 for (size_t i = 0; i < num_watchpoints; ++i)
521 {
522 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
523 if (!wp_loc_sp)
524 return false;
525
526 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
527 if (rc.Fail())
528 return false;
529 }
530 m_watchpoint_location_list.RemoveAll ();
531 return true; // Success!
532}
533
534// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
535bool
536Target::DisableAllWatchpointLocations ()
537{
538 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
539 if (log)
540 log->Printf ("Target::%s\n", __FUNCTION__);
541
542 if (!ProcessIsValid())
543 return false;
544
545 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
546 for (size_t i = 0; i < num_watchpoints; ++i)
547 {
548 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
549 if (!wp_loc_sp)
550 return false;
551
552 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
553 if (rc.Fail())
554 return false;
555 }
556 m_watchpoint_location_list.SetEnabledAll (false);
557 return true; // Success!
558}
559
560// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
561bool
562Target::EnableAllWatchpointLocations ()
563{
564 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
565 if (log)
566 log->Printf ("Target::%s\n", __FUNCTION__);
567
568 if (!ProcessIsValid())
569 return false;
570
571 size_t num_watchpoints = m_watchpoint_location_list.GetSize();
572 for (size_t i = 0; i < num_watchpoints; ++i)
573 {
574 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.GetByIndex(i);
575 if (!wp_loc_sp)
576 return false;
577
578 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
579 if (rc.Fail())
580 return false;
581 }
582 m_watchpoint_location_list.SetEnabledAll (true);
583 return true; // Success!
584}
585
586// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
587bool
588Target::DisableWatchpointLocationByID (lldb::watch_id_t watch_id)
589{
590 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
591 if (log)
592 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
593
594 if (!ProcessIsValid())
595 return false;
596
597 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
598 if (wp_loc_sp)
599 {
600 Error rc = m_process_sp->DisableWatchpoint(wp_loc_sp.get());
601 if (rc.Fail())
602 return false;
603
604 wp_loc_sp->SetEnabled (false);
605 return true;
606 }
607 return false;
608}
609
610// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
611bool
612Target::EnableWatchpointLocationByID (lldb::watch_id_t watch_id)
613{
614 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
615 if (log)
616 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
617
618 if (!ProcessIsValid())
619 return false;
620
621 WatchpointLocationSP wp_loc_sp = m_watchpoint_location_list.FindByID (watch_id);
622 if (wp_loc_sp)
623 {
624 Error rc = m_process_sp->EnableWatchpoint(wp_loc_sp.get());
625 if (rc.Fail())
626 return false;
627
628 wp_loc_sp->SetEnabled (true);
629 return true;
630 }
631 return false;
632}
633
634// Assumption: caller holds the list mutex lock for m_watchpoint_location_list.
635bool
636Target::RemoveWatchpointLocationByID (lldb::watch_id_t watch_id)
637{
638 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
639 if (log)
640 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
641
642 if (DisableWatchpointLocationByID (watch_id))
643 {
644 m_watchpoint_location_list.Remove(watch_id);
645 return true;
646 }
647 return false;
648}
649
Chris Lattner24943d22010-06-08 16:52:24 +0000650ModuleSP
651Target::GetExecutableModule ()
652{
Greg Clayton5beb99d2011-08-11 02:48:45 +0000653 return m_images.GetModuleAtIndex(0);
654}
655
656Module*
657Target::GetExecutableModulePointer ()
658{
659 return m_images.GetModulePointerAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000660}
661
662void
663Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
664{
665 m_images.Clear();
666 m_scratch_ast_context_ap.reset();
667
668 if (executable_sp.get())
669 {
670 Timer scoped_timer (__PRETTY_FUNCTION__,
671 "Target::SetExecutableModule (executable = '%s/%s')",
672 executable_sp->GetFileSpec().GetDirectory().AsCString(),
673 executable_sp->GetFileSpec().GetFilename().AsCString());
674
675 m_images.Append(executable_sp); // The first image is our exectuable file
676
Jim Ingham7508e732010-08-09 23:31:02 +0000677 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000678 if (!m_arch.IsValid())
679 m_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000680
Chris Lattner24943d22010-06-08 16:52:24 +0000681 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000682 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jim Inghamfdf24ef2011-09-08 22:13:49 +0000683 // Let's find the file & line for main and set the default source file from there.
684 if (!m_source_manager.DefaultFileAndLineSet())
685 {
686 SymbolContextList sc_list;
687 uint32_t num_matches;
688 ConstString main_name("main");
689 bool symbols_okay = false; // Force it to be a debug symbol.
690 bool append = false;
691 num_matches = executable_sp->FindFunctions (main_name, eFunctionNameTypeBase, symbols_okay, append, sc_list);
692 for (uint32_t idx = 0; idx < num_matches; idx++)
693 {
694 SymbolContext sc;
695 sc_list.GetContextAtIndex(idx, sc);
696 if (sc.line_entry.file)
697 {
698 m_source_manager.SetDefaultFileAndLine(sc.line_entry.file, sc.line_entry.line);
699 break;
700 }
701 }
702 }
Chris Lattner24943d22010-06-08 16:52:24 +0000703
704 if (executable_objfile)
705 {
706 executable_objfile->GetDependentModules(dependent_files);
707 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
708 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000709 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
710 FileSpec platform_dependent_file_spec;
711 if (m_platform_sp)
Greg Claytoncb8977d2011-03-23 00:09:55 +0000712 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonb1888f22011-03-19 01:12:21 +0000713 else
714 platform_dependent_file_spec = dependent_file_spec;
715
716 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000717 m_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000718 if (image_module_sp.get())
719 {
Chris Lattner24943d22010-06-08 16:52:24 +0000720 ObjectFile *objfile = image_module_sp->GetObjectFile();
721 if (objfile)
722 objfile->GetDependentModules(dependent_files);
723 }
724 }
725 }
726
Chris Lattner24943d22010-06-08 16:52:24 +0000727 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000728
729 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000730}
731
732
Jim Ingham7508e732010-08-09 23:31:02 +0000733bool
734Target::SetArchitecture (const ArchSpec &arch_spec)
735{
Greg Clayton24bc5d92011-03-30 18:16:51 +0000736 if (m_arch == arch_spec)
Jim Ingham7508e732010-08-09 23:31:02 +0000737 {
738 // If we're setting the architecture to our current architecture, we
739 // don't need to do anything.
740 return true;
741 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000742 else if (!m_arch.IsValid())
Jim Ingham7508e732010-08-09 23:31:02 +0000743 {
744 // If we haven't got a valid arch spec, then we just need to set it.
Greg Clayton24bc5d92011-03-30 18:16:51 +0000745 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000746 return true;
747 }
748 else
749 {
750 // If we have an executable file, try to reset the executable to the desired architecture
Greg Clayton24bc5d92011-03-30 18:16:51 +0000751 m_arch = arch_spec;
Jim Ingham7508e732010-08-09 23:31:02 +0000752 ModuleSP executable_sp = GetExecutableModule ();
753 m_images.Clear();
754 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000755 // Need to do something about unsetting breakpoints.
756
757 if (executable_sp)
758 {
759 FileSpec exec_file_spec = executable_sp->GetFileSpec();
760 Error error = ModuleList::GetSharedModule(exec_file_spec,
761 arch_spec,
762 NULL,
763 NULL,
764 0,
765 executable_sp,
766 NULL,
767 NULL);
768
769 if (!error.Fail() && executable_sp)
770 {
771 SetExecutableModule (executable_sp, true);
772 return true;
773 }
774 else
775 {
776 return false;
777 }
778 }
779 else
780 {
781 return false;
782 }
783 }
784}
Chris Lattner24943d22010-06-08 16:52:24 +0000785
Chris Lattner24943d22010-06-08 16:52:24 +0000786void
787Target::ModuleAdded (ModuleSP &module_sp)
788{
789 // A module is being added to this target for the first time
790 ModuleList module_list;
791 module_list.Append(module_sp);
792 ModulesDidLoad (module_list);
793}
794
795void
796Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
797{
Jim Ingham3b8a6052011-08-03 01:00:06 +0000798 // A module is replacing an already added module
Chris Lattner24943d22010-06-08 16:52:24 +0000799 ModuleList module_list;
800 module_list.Append (old_module_sp);
801 ModulesDidUnload (module_list);
802 module_list.Clear ();
803 module_list.Append (new_module_sp);
804 ModulesDidLoad (module_list);
805}
806
807void
808Target::ModulesDidLoad (ModuleList &module_list)
809{
810 m_breakpoint_list.UpdateBreakpoints (module_list, true);
811 // TODO: make event data that packages up the module_list
812 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
813}
814
815void
816Target::ModulesDidUnload (ModuleList &module_list)
817{
818 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000819
820 // Remove the images from the target image list
821 m_images.Remove(module_list);
822
Chris Lattner24943d22010-06-08 16:52:24 +0000823 // TODO: make event data that packages up the module_list
824 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
825}
826
827size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000828Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
829{
830 const Section *section = addr.GetSection();
831 if (section && section->GetModule())
832 {
833 ObjectFile *objfile = section->GetModule()->GetObjectFile();
834 if (objfile)
835 {
836 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
837 addr.GetOffset(),
838 dst,
839 dst_len);
840 if (bytes_read > 0)
841 return bytes_read;
842 else
843 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
844 }
845 else
846 {
847 error.SetErrorString("address isn't from a object file");
848 }
849 }
850 else
851 {
852 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
853 }
854 return 0;
855}
856
857size_t
Enrico Granata91544802011-09-06 19:20:51 +0000858Target::ReadMemory (const Address& addr,
859 bool prefer_file_cache,
860 void *dst,
861 size_t dst_len,
862 Error &error,
863 lldb::addr_t *load_addr_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000864{
Chris Lattner24943d22010-06-08 16:52:24 +0000865 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000866
Enrico Granata91544802011-09-06 19:20:51 +0000867 // if we end up reading this from process memory, we will fill this
868 // with the actual load address
869 if (load_addr_ptr)
870 *load_addr_ptr = LLDB_INVALID_ADDRESS;
871
Greg Clayton26100dc2011-01-07 01:57:07 +0000872 size_t bytes_read = 0;
Greg Clayton9b82f862011-07-11 05:12:02 +0000873
874 addr_t load_addr = LLDB_INVALID_ADDRESS;
875 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton889fbd02011-03-26 19:14:58 +0000876 Address resolved_addr;
877 if (!addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000878 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000879 if (m_section_load_list.IsEmpty())
Greg Clayton9b82f862011-07-11 05:12:02 +0000880 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000881 // No sections are loaded, so we must assume we are not running
882 // yet and anything we are given is a file address.
883 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
884 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000885 }
Greg Clayton70436352010-06-30 23:03:03 +0000886 else
Greg Clayton9b82f862011-07-11 05:12:02 +0000887 {
Greg Clayton7dd98df2011-07-12 17:06:17 +0000888 // 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 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
893 m_section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Clayton9b82f862011-07-11 05:12:02 +0000894 }
Greg Clayton70436352010-06-30 23:03:03 +0000895 }
Greg Clayton889fbd02011-03-26 19:14:58 +0000896 if (!resolved_addr.IsValid())
897 resolved_addr = addr;
Greg Clayton70436352010-06-30 23:03:03 +0000898
Greg Clayton9b82f862011-07-11 05:12:02 +0000899
Greg Clayton26100dc2011-01-07 01:57:07 +0000900 if (prefer_file_cache)
901 {
902 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
903 if (bytes_read > 0)
904 return bytes_read;
905 }
Greg Clayton70436352010-06-30 23:03:03 +0000906
Johnny Chenda5a8022011-09-20 23:28:55 +0000907 if (ProcessIsValid())
Greg Clayton70436352010-06-30 23:03:03 +0000908 {
Greg Clayton9b82f862011-07-11 05:12:02 +0000909 if (load_addr == LLDB_INVALID_ADDRESS)
910 load_addr = resolved_addr.GetLoadAddress (this);
911
Greg Clayton70436352010-06-30 23:03:03 +0000912 if (load_addr == LLDB_INVALID_ADDRESS)
913 {
914 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
915 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
916 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
Jason Molenda95b7b432011-09-20 00:26:08 +0000917 resolved_addr.GetFileAddress(),
918 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString());
Greg Clayton70436352010-06-30 23:03:03 +0000919 else
920 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
921 }
922 else
923 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000924 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000925 if (bytes_read != dst_len)
926 {
927 if (error.Success())
928 {
929 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000930 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000931 else
Greg Clayton70436352010-06-30 23:03:03 +0000932 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000933 }
934 }
Greg Clayton70436352010-06-30 23:03:03 +0000935 if (bytes_read)
Enrico Granata91544802011-09-06 19:20:51 +0000936 {
937 if (load_addr_ptr)
938 *load_addr_ptr = load_addr;
Greg Clayton70436352010-06-30 23:03:03 +0000939 return bytes_read;
Enrico Granata91544802011-09-06 19:20:51 +0000940 }
Greg Clayton70436352010-06-30 23:03:03 +0000941 // If the address is not section offset we have an address that
942 // doesn't resolve to any address in any currently loaded shared
943 // libaries and we failed to read memory so there isn't anything
944 // more we can do. If it is section offset, we might be able to
945 // read cached memory from the object file.
946 if (!resolved_addr.IsSectionOffset())
947 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000948 }
Chris Lattner24943d22010-06-08 16:52:24 +0000949 }
Greg Clayton70436352010-06-30 23:03:03 +0000950
Greg Clayton9b82f862011-07-11 05:12:02 +0000951 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Clayton70436352010-06-30 23:03:03 +0000952 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000953 // If we didn't already try and read from the object file cache, then
954 // try it after failing to read from the process.
955 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000956 }
957 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000958}
959
Greg Clayton7dd98df2011-07-12 17:06:17 +0000960size_t
961Target::ReadScalarIntegerFromMemory (const Address& addr,
962 bool prefer_file_cache,
963 uint32_t byte_size,
964 bool is_signed,
965 Scalar &scalar,
966 Error &error)
967{
968 uint64_t uval;
969
970 if (byte_size <= sizeof(uval))
971 {
972 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
973 if (bytes_read == byte_size)
974 {
975 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
976 uint32_t offset = 0;
977 if (byte_size <= 4)
978 scalar = data.GetMaxU32 (&offset, byte_size);
979 else
980 scalar = data.GetMaxU64 (&offset, byte_size);
981
982 if (is_signed)
983 scalar.SignExtend(byte_size * 8);
984 return bytes_read;
985 }
986 }
987 else
988 {
989 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
990 }
991 return 0;
992}
993
994uint64_t
995Target::ReadUnsignedIntegerFromMemory (const Address& addr,
996 bool prefer_file_cache,
997 size_t integer_byte_size,
998 uint64_t fail_value,
999 Error &error)
1000{
1001 Scalar scalar;
1002 if (ReadScalarIntegerFromMemory (addr,
1003 prefer_file_cache,
1004 integer_byte_size,
1005 false,
1006 scalar,
1007 error))
1008 return scalar.ULongLong(fail_value);
1009 return fail_value;
1010}
1011
1012bool
1013Target::ReadPointerFromMemory (const Address& addr,
1014 bool prefer_file_cache,
1015 Error &error,
1016 Address &pointer_addr)
1017{
1018 Scalar scalar;
1019 if (ReadScalarIntegerFromMemory (addr,
1020 prefer_file_cache,
1021 m_arch.GetAddressByteSize(),
1022 false,
1023 scalar,
1024 error))
1025 {
1026 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1027 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1028 {
1029 if (m_section_load_list.IsEmpty())
1030 {
1031 // No sections are loaded, so we must assume we are not running
1032 // yet and anything we are given is a file address.
1033 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1034 }
1035 else
1036 {
1037 // We have at least one section loaded. This can be becuase
1038 // we have manually loaded some sections with "target modules load ..."
1039 // or because we have have a live process that has sections loaded
1040 // through the dynamic loader
1041 m_section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
1042 }
1043 // We weren't able to resolve the pointer value, so just return
1044 // an address with no section
1045 if (!pointer_addr.IsValid())
1046 pointer_addr.SetOffset (pointer_vm_addr);
1047 return true;
1048
1049 }
1050 }
1051 return false;
1052}
Chris Lattner24943d22010-06-08 16:52:24 +00001053
1054ModuleSP
1055Target::GetSharedModule
1056(
1057 const FileSpec& file_spec,
1058 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +00001059 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +00001060 const ConstString *object_name,
1061 off_t object_offset,
1062 Error *error_ptr
1063)
1064{
1065 // Don't pass in the UUID so we can tell if we have a stale value in our list
1066 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1067 bool did_create_module = false;
1068 ModuleSP module_sp;
1069
Chris Lattner24943d22010-06-08 16:52:24 +00001070 Error error;
1071
Greg Clayton24bc5d92011-03-30 18:16:51 +00001072 // If there are image search path entries, try to use them first to acquire a suitable image.
Chris Lattner24943d22010-06-08 16:52:24 +00001073 if (m_image_search_paths.GetSize())
1074 {
1075 FileSpec transformed_spec;
1076 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
1077 {
1078 transformed_spec.GetFilename() = file_spec.GetFilename();
1079 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
1080 }
1081 }
1082
Greg Clayton24bc5d92011-03-30 18:16:51 +00001083 // The platform is responsible for finding and caching an appropriate
1084 // module in the shared module cache.
1085 if (m_platform_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001086 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001087 FileSpec platform_file_spec;
1088 error = m_platform_sp->GetSharedModule (file_spec,
1089 arch,
1090 uuid_ptr,
1091 object_name,
1092 object_offset,
1093 module_sp,
1094 &old_module_sp,
1095 &did_create_module);
1096 }
1097 else
1098 {
1099 error.SetErrorString("no platform is currently set");
Chris Lattner24943d22010-06-08 16:52:24 +00001100 }
1101
Greg Clayton24bc5d92011-03-30 18:16:51 +00001102 // If a module hasn't been found yet, use the unmodified path.
Chris Lattner24943d22010-06-08 16:52:24 +00001103 if (module_sp)
1104 {
1105 m_images.Append (module_sp);
1106 if (did_create_module)
1107 {
1108 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1109 ModuleUpdated(old_module_sp, module_sp);
1110 else
1111 ModuleAdded(module_sp);
1112 }
1113 }
1114 if (error_ptr)
1115 *error_ptr = error;
1116 return module_sp;
1117}
1118
1119
1120Target *
1121Target::CalculateTarget ()
1122{
1123 return this;
1124}
1125
1126Process *
1127Target::CalculateProcess ()
1128{
1129 return NULL;
1130}
1131
1132Thread *
1133Target::CalculateThread ()
1134{
1135 return NULL;
1136}
1137
1138StackFrame *
1139Target::CalculateStackFrame ()
1140{
1141 return NULL;
1142}
1143
1144void
Greg Claytona830adb2010-10-04 01:05:56 +00001145Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001146{
1147 exe_ctx.target = this;
1148 exe_ctx.process = NULL; // Do NOT fill in process...
1149 exe_ctx.thread = NULL;
1150 exe_ctx.frame = NULL;
1151}
1152
1153PathMappingList &
1154Target::GetImageSearchPathList ()
1155{
1156 return m_image_search_paths;
1157}
1158
1159void
1160Target::ImageSearchPathsChanged
1161(
1162 const PathMappingList &path_list,
1163 void *baton
1164)
1165{
1166 Target *target = (Target *)baton;
Greg Clayton5beb99d2011-08-11 02:48:45 +00001167 ModuleSP exe_module_sp (target->GetExecutableModule());
1168 if (exe_module_sp)
Chris Lattner24943d22010-06-08 16:52:24 +00001169 {
Greg Clayton5beb99d2011-08-11 02:48:45 +00001170 target->m_images.Clear();
1171 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +00001172 }
1173}
1174
1175ClangASTContext *
1176Target::GetScratchClangASTContext()
1177{
Greg Clayton34ce4ea2011-08-03 01:23:55 +00001178 // Now see if we know the target triple, and if so, create our scratch AST context:
1179 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid())
1180 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +00001181 return m_scratch_ast_context_ap.get();
1182}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001183
Greg Clayton990de7b2010-11-18 23:32:35 +00001184void
Caroline Tice2a456812011-03-10 22:14:10 +00001185Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +00001186{
Greg Clayton990de7b2010-11-18 23:32:35 +00001187 UserSettingsControllerSP &usc = GetSettingsController();
1188 usc.reset (new SettingsController);
1189 UserSettingsController::InitializeSettingsController (usc,
1190 SettingsController::global_settings_table,
1191 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +00001192
1193 // Now call SettingsInitialize() on each 'child' setting of Target
1194 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +00001195}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001196
Greg Clayton990de7b2010-11-18 23:32:35 +00001197void
Caroline Tice2a456812011-03-10 22:14:10 +00001198Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001199{
Caroline Tice2a456812011-03-10 22:14:10 +00001200
1201 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
1202
1203 Process::SettingsTerminate ();
1204
1205 // Now terminate Target Settings.
1206
Greg Clayton990de7b2010-11-18 23:32:35 +00001207 UserSettingsControllerSP &usc = GetSettingsController();
1208 UserSettingsController::FinalizeSettingsController (usc);
1209 usc.reset();
1210}
Caroline Tice5bc8c972010-09-20 20:44:43 +00001211
Greg Clayton990de7b2010-11-18 23:32:35 +00001212UserSettingsControllerSP &
1213Target::GetSettingsController ()
1214{
1215 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001216 return g_settings_controller;
1217}
1218
1219ArchSpec
1220Target::GetDefaultArchitecture ()
1221{
Greg Clayton940b1032011-02-23 00:35:02 +00001222 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1223
1224 if (settings_controller_sp)
1225 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
1226 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +00001227}
1228
1229void
Greg Clayton940b1032011-02-23 00:35:02 +00001230Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001231{
Greg Clayton940b1032011-02-23 00:35:02 +00001232 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
1233
1234 if (settings_controller_sp)
1235 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001236}
1237
Greg Claytona830adb2010-10-04 01:05:56 +00001238Target *
1239Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1240{
1241 // The target can either exist in the "process" of ExecutionContext, or in
1242 // the "target_sp" member of SymbolContext. This accessor helper function
1243 // will get the target from one of these locations.
1244
1245 Target *target = NULL;
1246 if (sc_ptr != NULL)
1247 target = sc_ptr->target_sp.get();
1248 if (target == NULL)
1249 {
1250 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
1251 target = &exe_ctx_ptr->process->GetTarget();
1252 }
1253 return target;
1254}
1255
1256
Caroline Tice1ebef442010-09-27 00:30:10 +00001257void
1258Target::UpdateInstanceName ()
1259{
1260 StreamString sstr;
1261
Greg Clayton5beb99d2011-08-11 02:48:45 +00001262 Module *exe_module = GetExecutableModulePointer();
1263 if (exe_module)
Caroline Tice1ebef442010-09-27 00:30:10 +00001264 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001265 sstr.Printf ("%s_%s",
Greg Clayton5beb99d2011-08-11 02:48:45 +00001266 exe_module->GetFileSpec().GetFilename().AsCString(),
1267 exe_module->GetArchitecture().GetArchitectureName());
1268 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +00001269 }
1270}
1271
Sean Callanan77e93942010-10-29 00:29:03 +00001272const char *
1273Target::GetExpressionPrefixContentsAsCString ()
1274{
Greg Claytonff44ab42011-04-23 02:04:55 +00001275 if (m_expr_prefix_contents_sp)
1276 return (const char *)m_expr_prefix_contents_sp->GetBytes();
1277 return NULL;
Sean Callanan77e93942010-10-29 00:29:03 +00001278}
1279
Greg Clayton427f2902010-12-14 02:59:59 +00001280ExecutionResults
1281Target::EvaluateExpression
1282(
1283 const char *expr_cstr,
1284 StackFrame *frame,
Sean Callanan47dc4572011-09-15 02:13:07 +00001285 lldb_private::ExecutionPolicy execution_policy,
Greg Clayton427f2902010-12-14 02:59:59 +00001286 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +00001287 bool keep_in_memory,
Jim Ingham10de7d12011-05-04 03:43:18 +00001288 lldb::DynamicValueType use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +00001289 lldb::ValueObjectSP &result_valobj_sp
1290)
1291{
1292 ExecutionResults execution_results = eExecutionSetupError;
1293
1294 result_valobj_sp.reset();
Jim Ingham3613ae12011-05-12 02:06:14 +00001295
1296 // We shouldn't run stop hooks in expressions.
1297 // Be sure to reset this if you return anywhere within this function.
1298 bool old_suppress_value = m_suppress_stop_hooks;
1299 m_suppress_stop_hooks = true;
Greg Clayton427f2902010-12-14 02:59:59 +00001300
1301 ExecutionContext exe_ctx;
1302 if (frame)
1303 {
1304 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +00001305 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +00001306 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
Enrico Granataf6698502011-08-09 01:04:56 +00001307 StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
1308 StackFrame::eExpressionPathOptionsNoSyntheticChildren;
Jim Ingham10de7d12011-05-04 03:43:18 +00001309 lldb::VariableSP var_sp;
1310 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr,
1311 use_dynamic,
1312 expr_path_options,
1313 var_sp,
1314 error);
Greg Clayton427f2902010-12-14 02:59:59 +00001315 }
1316 else if (m_process_sp)
1317 {
1318 m_process_sp->CalculateExecutionContext(exe_ctx);
1319 }
1320 else
1321 {
1322 CalculateExecutionContext(exe_ctx);
1323 }
1324
1325 if (result_valobj_sp)
1326 {
1327 execution_results = eExecutionCompleted;
1328 // We got a result from the frame variable expression path above...
1329 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
1330
1331 lldb::ValueObjectSP const_valobj_sp;
1332
1333 // Check in case our value is already a constant value
1334 if (result_valobj_sp->GetIsConstant())
1335 {
1336 const_valobj_sp = result_valobj_sp;
1337 const_valobj_sp->SetName (persistent_variable_name);
1338 }
1339 else
Jim Inghame41494a2011-04-16 00:01:13 +00001340 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001341 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +00001342 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001343 ValueObjectSP dynamic_sp = result_valobj_sp->GetDynamicValue(use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001344 if (dynamic_sp)
1345 result_valobj_sp = dynamic_sp;
1346 }
1347
Jim Inghamfa3a16a2011-03-31 00:19:25 +00001348 const_valobj_sp = result_valobj_sp->CreateConstantValue (persistent_variable_name);
Jim Inghame41494a2011-04-16 00:01:13 +00001349 }
Greg Clayton427f2902010-12-14 02:59:59 +00001350
Sean Callanan6a925532011-01-13 08:53:35 +00001351 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
1352
Greg Clayton427f2902010-12-14 02:59:59 +00001353 result_valobj_sp = const_valobj_sp;
1354
Sean Callanan6a925532011-01-13 08:53:35 +00001355 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
1356 assert (clang_expr_variable_sp.get());
1357
1358 // Set flags and live data as appropriate
1359
1360 const Value &result_value = live_valobj_sp->GetValue();
1361
1362 switch (result_value.GetValueType())
1363 {
1364 case Value::eValueTypeHostAddress:
1365 case Value::eValueTypeFileAddress:
1366 // we don't do anything with these for now
1367 break;
1368 case Value::eValueTypeScalar:
1369 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
1370 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
1371 break;
1372 case Value::eValueTypeLoadAddress:
1373 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
1374 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
1375 break;
1376 }
Greg Clayton427f2902010-12-14 02:59:59 +00001377 }
1378 else
1379 {
1380 // Make sure we aren't just trying to see the value of a persistent
1381 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +00001382 lldb::ClangExpressionVariableSP persistent_var_sp;
1383 // Only check for persistent variables the expression starts with a '$'
1384 if (expr_cstr[0] == '$')
1385 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
1386
Greg Clayton427f2902010-12-14 02:59:59 +00001387 if (persistent_var_sp)
1388 {
1389 result_valobj_sp = persistent_var_sp->GetValueObject ();
1390 execution_results = eExecutionCompleted;
1391 }
1392 else
1393 {
1394 const char *prefix = GetExpressionPrefixContentsAsCString();
Sean Callanan47dc4572011-09-15 02:13:07 +00001395
Greg Clayton427f2902010-12-14 02:59:59 +00001396 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan47dc4572011-09-15 02:13:07 +00001397 execution_policy,
Sean Callanan6a925532011-01-13 08:53:35 +00001398 unwind_on_error,
Greg Clayton427f2902010-12-14 02:59:59 +00001399 expr_cstr,
1400 prefix,
1401 result_valobj_sp);
1402 }
1403 }
Jim Ingham3613ae12011-05-12 02:06:14 +00001404
1405 m_suppress_stop_hooks = old_suppress_value;
1406
Greg Clayton427f2902010-12-14 02:59:59 +00001407 return execution_results;
1408}
1409
Greg Claytonc0fa5332011-05-22 22:46:53 +00001410lldb::addr_t
1411Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1412{
1413 addr_t code_addr = load_addr;
1414 switch (m_arch.GetMachine())
1415 {
1416 case llvm::Triple::arm:
1417 case llvm::Triple::thumb:
1418 switch (addr_class)
1419 {
1420 case eAddressClassData:
1421 case eAddressClassDebug:
1422 return LLDB_INVALID_ADDRESS;
1423
1424 case eAddressClassUnknown:
1425 case eAddressClassInvalid:
1426 case eAddressClassCode:
1427 case eAddressClassCodeAlternateISA:
1428 case eAddressClassRuntime:
1429 // Check if bit zero it no set?
1430 if ((code_addr & 1ull) == 0)
1431 {
1432 // Bit zero isn't set, check if the address is a multiple of 2?
1433 if (code_addr & 2ull)
1434 {
1435 // The address is a multiple of 2 so it must be thumb, set bit zero
1436 code_addr |= 1ull;
1437 }
1438 else if (addr_class == eAddressClassCodeAlternateISA)
1439 {
1440 // We checked the address and the address claims to be the alternate ISA
1441 // which means thumb, so set bit zero.
1442 code_addr |= 1ull;
1443 }
1444 }
1445 break;
1446 }
1447 break;
1448
1449 default:
1450 break;
1451 }
1452 return code_addr;
1453}
1454
1455lldb::addr_t
1456Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
1457{
1458 addr_t opcode_addr = load_addr;
1459 switch (m_arch.GetMachine())
1460 {
1461 case llvm::Triple::arm:
1462 case llvm::Triple::thumb:
1463 switch (addr_class)
1464 {
1465 case eAddressClassData:
1466 case eAddressClassDebug:
1467 return LLDB_INVALID_ADDRESS;
1468
1469 case eAddressClassInvalid:
1470 case eAddressClassUnknown:
1471 case eAddressClassCode:
1472 case eAddressClassCodeAlternateISA:
1473 case eAddressClassRuntime:
1474 opcode_addr &= ~(1ull);
1475 break;
1476 }
1477 break;
1478
1479 default:
1480 break;
1481 }
1482 return opcode_addr;
1483}
1484
Jim Inghamd60d94a2011-03-11 03:53:59 +00001485lldb::user_id_t
1486Target::AddStopHook (Target::StopHookSP &new_hook_sp)
1487{
1488 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
1489 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
1490 m_stop_hooks[new_uid] = new_hook_sp;
1491 return new_uid;
1492}
1493
1494bool
1495Target::RemoveStopHookByID (lldb::user_id_t user_id)
1496{
1497 size_t num_removed;
1498 num_removed = m_stop_hooks.erase (user_id);
1499 if (num_removed == 0)
1500 return false;
1501 else
1502 return true;
1503}
1504
1505void
1506Target::RemoveAllStopHooks ()
1507{
1508 m_stop_hooks.clear();
1509}
1510
1511Target::StopHookSP
1512Target::GetStopHookByID (lldb::user_id_t user_id)
1513{
1514 StopHookSP found_hook;
1515
1516 StopHookCollection::iterator specified_hook_iter;
1517 specified_hook_iter = m_stop_hooks.find (user_id);
1518 if (specified_hook_iter != m_stop_hooks.end())
1519 found_hook = (*specified_hook_iter).second;
1520 return found_hook;
1521}
1522
1523bool
1524Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1525{
1526 StopHookCollection::iterator specified_hook_iter;
1527 specified_hook_iter = m_stop_hooks.find (user_id);
1528 if (specified_hook_iter == m_stop_hooks.end())
1529 return false;
1530
1531 (*specified_hook_iter).second->SetIsActive (active_state);
1532 return true;
1533}
1534
1535void
1536Target::SetAllStopHooksActiveState (bool active_state)
1537{
1538 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1539 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1540 {
1541 (*pos).second->SetIsActive (active_state);
1542 }
1543}
1544
1545void
1546Target::RunStopHooks ()
1547{
Jim Ingham3613ae12011-05-12 02:06:14 +00001548 if (m_suppress_stop_hooks)
1549 return;
1550
Jim Inghamd60d94a2011-03-11 03:53:59 +00001551 if (!m_process_sp)
1552 return;
1553
1554 if (m_stop_hooks.empty())
1555 return;
1556
1557 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1558
1559 // If there aren't any active stop hooks, don't bother either:
1560 bool any_active_hooks = false;
1561 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1562 {
1563 if ((*pos).second->IsActive())
1564 {
1565 any_active_hooks = true;
1566 break;
1567 }
1568 }
1569 if (!any_active_hooks)
1570 return;
1571
1572 CommandReturnObject result;
1573
1574 std::vector<ExecutionContext> exc_ctx_with_reasons;
1575 std::vector<SymbolContext> sym_ctx_with_reasons;
1576
1577 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1578 size_t num_threads = cur_threadlist.GetSize();
1579 for (size_t i = 0; i < num_threads; i++)
1580 {
1581 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1582 if (cur_thread_sp->ThreadStoppedForAReason())
1583 {
1584 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1585 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1586 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1587 }
1588 }
1589
1590 // If no threads stopped for a reason, don't run the stop-hooks.
1591 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1592 if (num_exe_ctx == 0)
1593 return;
1594
Jim Inghame5ed8e92011-06-02 23:58:26 +00001595 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
1596 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001597
1598 bool keep_going = true;
1599 bool hooks_ran = false;
Jim Inghamc54840c2011-03-22 01:47:27 +00001600 bool print_hook_header;
1601 bool print_thread_header;
1602
1603 if (num_exe_ctx == 1)
1604 print_thread_header = false;
1605 else
1606 print_thread_header = true;
1607
1608 if (m_stop_hooks.size() == 1)
1609 print_hook_header = false;
1610 else
1611 print_hook_header = true;
1612
Jim Inghamd60d94a2011-03-11 03:53:59 +00001613 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1614 {
1615 // result.Clear();
1616 StopHookSP cur_hook_sp = (*pos).second;
1617 if (!cur_hook_sp->IsActive())
1618 continue;
1619
1620 bool any_thread_matched = false;
1621 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1622 {
1623 if ((cur_hook_sp->GetSpecifier () == NULL
1624 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1625 && (cur_hook_sp->GetThreadSpecifier() == NULL
1626 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1627 {
1628 if (!hooks_ran)
1629 {
Jim Inghamc54840c2011-03-22 01:47:27 +00001630 result.AppendMessage("\n** Stop Hooks **");
Jim Inghamd60d94a2011-03-11 03:53:59 +00001631 hooks_ran = true;
1632 }
Jim Inghamc54840c2011-03-22 01:47:27 +00001633 if (print_hook_header && !any_thread_matched)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001634 {
1635 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1636 any_thread_matched = true;
1637 }
1638
Jim Inghamc54840c2011-03-22 01:47:27 +00001639 if (print_thread_header)
1640 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00001641
1642 bool stop_on_continue = true;
1643 bool stop_on_error = true;
1644 bool echo_commands = false;
1645 bool print_results = true;
1646 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
Greg Clayton24bc5d92011-03-30 18:16:51 +00001647 &exc_ctx_with_reasons[i],
1648 stop_on_continue,
1649 stop_on_error,
1650 echo_commands,
1651 print_results,
1652 result);
Jim Inghamd60d94a2011-03-11 03:53:59 +00001653
1654 // If the command started the target going again, we should bag out of
1655 // running the stop hooks.
Greg Clayton24bc5d92011-03-30 18:16:51 +00001656 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
1657 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Inghamd60d94a2011-03-11 03:53:59 +00001658 {
1659 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1660 keep_going = false;
1661 }
1662 }
1663 }
1664 }
1665 if (hooks_ran)
1666 result.AppendMessage ("\n** End Stop Hooks **\n");
Caroline Tice4a348082011-05-02 20:41:46 +00001667
1668 result.GetImmediateOutputStream()->Flush();
1669 result.GetImmediateErrorStream()->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00001670}
1671
Greg Claytonbbea1332011-07-08 00:48:09 +00001672bool
1673Target::LoadModuleWithSlide (Module *module, lldb::addr_t slide)
1674{
1675 bool changed = false;
1676 if (module)
1677 {
1678 ObjectFile *object_file = module->GetObjectFile();
1679 if (object_file)
1680 {
1681 SectionList *section_list = object_file->GetSectionList ();
1682 if (section_list)
1683 {
1684 // All sections listed in the dyld image info structure will all
1685 // either be fixed up already, or they will all be off by a single
1686 // slide amount that is determined by finding the first segment
1687 // that is at file offset zero which also has bytes (a file size
1688 // that is greater than zero) in the object file.
1689
1690 // Determine the slide amount (if any)
1691 const size_t num_sections = section_list->GetSize();
1692 size_t sect_idx = 0;
1693 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1694 {
1695 // Iterate through the object file sections to find the
1696 // first section that starts of file offset zero and that
1697 // has bytes in the file...
1698 Section *section = section_list->GetSectionAtIndex (sect_idx).get();
1699 if (section)
1700 {
1701 if (m_section_load_list.SetSectionLoadAddress (section, section->GetFileAddress() + slide))
1702 changed = true;
1703 }
1704 }
1705 }
1706 }
1707 }
1708 return changed;
1709}
1710
1711
Jim Inghamd60d94a2011-03-11 03:53:59 +00001712//--------------------------------------------------------------
1713// class Target::StopHook
1714//--------------------------------------------------------------
1715
1716
1717Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1718 UserID (uid),
1719 m_target_sp (target_sp),
Jim Inghamd60d94a2011-03-11 03:53:59 +00001720 m_commands (),
1721 m_specifier_sp (),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001722 m_thread_spec_ap(NULL),
1723 m_active (true)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001724{
1725}
1726
1727Target::StopHook::StopHook (const StopHook &rhs) :
1728 UserID (rhs.GetID()),
1729 m_target_sp (rhs.m_target_sp),
1730 m_commands (rhs.m_commands),
1731 m_specifier_sp (rhs.m_specifier_sp),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +00001732 m_thread_spec_ap (NULL),
1733 m_active (rhs.m_active)
Jim Inghamd60d94a2011-03-11 03:53:59 +00001734{
1735 if (rhs.m_thread_spec_ap.get() != NULL)
1736 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1737}
1738
1739
1740Target::StopHook::~StopHook ()
1741{
1742}
1743
1744void
1745Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1746{
1747 m_thread_spec_ap.reset (specifier);
1748}
1749
1750
1751void
1752Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1753{
1754 int indent_level = s->GetIndentLevel();
1755
1756 s->SetIndentLevel(indent_level + 2);
1757
1758 s->Printf ("Hook: %d\n", GetID());
1759 if (m_active)
1760 s->Indent ("State: enabled\n");
1761 else
1762 s->Indent ("State: disabled\n");
1763
1764 if (m_specifier_sp)
1765 {
1766 s->Indent();
1767 s->PutCString ("Specifier:\n");
1768 s->SetIndentLevel (indent_level + 4);
1769 m_specifier_sp->GetDescription (s, level);
1770 s->SetIndentLevel (indent_level + 2);
1771 }
1772
1773 if (m_thread_spec_ap.get() != NULL)
1774 {
1775 StreamString tmp;
1776 s->Indent("Thread:\n");
1777 m_thread_spec_ap->GetDescription (&tmp, level);
1778 s->SetIndentLevel (indent_level + 4);
1779 s->Indent (tmp.GetData());
1780 s->PutCString ("\n");
1781 s->SetIndentLevel (indent_level + 2);
1782 }
1783
1784 s->Indent ("Commands: \n");
1785 s->SetIndentLevel (indent_level + 4);
1786 uint32_t num_commands = m_commands.GetSize();
1787 for (uint32_t i = 0; i < num_commands; i++)
1788 {
1789 s->Indent(m_commands.GetStringAtIndex(i));
1790 s->PutCString ("\n");
1791 }
1792 s->SetIndentLevel (indent_level);
1793}
1794
1795
Caroline Tice5bc8c972010-09-20 20:44:43 +00001796//--------------------------------------------------------------
1797// class Target::SettingsController
1798//--------------------------------------------------------------
1799
1800Target::SettingsController::SettingsController () :
1801 UserSettingsController ("target", Debugger::GetSettingsController()),
1802 m_default_architecture ()
1803{
1804 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1805 InstanceSettings::GetDefaultName().AsCString()));
1806}
1807
1808Target::SettingsController::~SettingsController ()
1809{
1810}
1811
1812lldb::InstanceSettingsSP
1813Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1814{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001815 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1816 false,
1817 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001818 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1819 return new_settings_sp;
1820}
1821
Caroline Tice5bc8c972010-09-20 20:44:43 +00001822
Jim Inghame41494a2011-04-16 00:01:13 +00001823#define TSC_DEFAULT_ARCH "default-arch"
1824#define TSC_EXPR_PREFIX "expr-prefix"
Jim Inghame41494a2011-04-16 00:01:13 +00001825#define TSC_PREFER_DYNAMIC "prefer-dynamic-value"
Greg Clayton17cd9952011-04-22 03:55:06 +00001826#define TSC_SKIP_PROLOGUE "skip-prologue"
Greg Claytonff44ab42011-04-23 02:04:55 +00001827#define TSC_SOURCE_MAP "source-map"
Enrico Granata018921d2011-08-12 02:00:06 +00001828#define TSC_MAX_CHILDREN "max-children-count"
Enrico Granata91544802011-09-06 19:20:51 +00001829#define TSC_MAX_STRLENSUMMARY "max-string-summary-length"
Greg Claytond284b662011-02-18 01:44:25 +00001830
1831
1832static const ConstString &
1833GetSettingNameForDefaultArch ()
1834{
1835 static ConstString g_const_string (TSC_DEFAULT_ARCH);
Greg Claytond284b662011-02-18 01:44:25 +00001836 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001837}
1838
Greg Claytond284b662011-02-18 01:44:25 +00001839static const ConstString &
1840GetSettingNameForExpressionPrefix ()
1841{
1842 static ConstString g_const_string (TSC_EXPR_PREFIX);
1843 return g_const_string;
1844}
1845
1846static const ConstString &
Jim Inghame41494a2011-04-16 00:01:13 +00001847GetSettingNameForPreferDynamicValue ()
1848{
1849 static ConstString g_const_string (TSC_PREFER_DYNAMIC);
1850 return g_const_string;
1851}
1852
Greg Claytonff44ab42011-04-23 02:04:55 +00001853static const ConstString &
1854GetSettingNameForSourcePathMap ()
1855{
1856 static ConstString g_const_string (TSC_SOURCE_MAP);
1857 return g_const_string;
1858}
Greg Claytond284b662011-02-18 01:44:25 +00001859
Greg Clayton17cd9952011-04-22 03:55:06 +00001860static const ConstString &
1861GetSettingNameForSkipPrologue ()
1862{
1863 static ConstString g_const_string (TSC_SKIP_PROLOGUE);
1864 return g_const_string;
1865}
1866
Enrico Granata018921d2011-08-12 02:00:06 +00001867static const ConstString &
1868GetSettingNameForMaxChildren ()
1869{
1870 static ConstString g_const_string (TSC_MAX_CHILDREN);
1871 return g_const_string;
1872}
Greg Clayton17cd9952011-04-22 03:55:06 +00001873
Enrico Granata91544802011-09-06 19:20:51 +00001874static const ConstString &
1875GetSettingNameForMaxStringSummaryLength ()
1876{
1877 static ConstString g_const_string (TSC_MAX_STRLENSUMMARY);
1878 return g_const_string;
1879}
Greg Clayton17cd9952011-04-22 03:55:06 +00001880
Caroline Tice5bc8c972010-09-20 20:44:43 +00001881bool
1882Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1883 const char *index_value,
1884 const char *value,
1885 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001886 const VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001887 Error&err)
1888{
Greg Claytond284b662011-02-18 01:44:25 +00001889 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001890 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001891 m_default_architecture.SetTriple (value, NULL);
Greg Clayton940b1032011-02-23 00:35:02 +00001892 if (!m_default_architecture.IsValid())
1893 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001894 }
1895 return true;
1896}
1897
1898
1899bool
1900Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1901 StringList &value,
1902 Error &err)
1903{
Greg Claytond284b662011-02-18 01:44:25 +00001904 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001905 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001906 // If the arch is invalid (the default), don't show a string for it
1907 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001908 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001909 return true;
1910 }
1911 else
1912 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1913
1914 return false;
1915}
1916
1917//--------------------------------------------------------------
1918// class TargetInstanceSettings
1919//--------------------------------------------------------------
1920
Greg Clayton638351a2010-12-04 00:10:17 +00001921TargetInstanceSettings::TargetInstanceSettings
1922(
1923 UserSettingsController &owner,
1924 bool live_instance,
1925 const char *name
1926) :
Greg Claytond284b662011-02-18 01:44:25 +00001927 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
Greg Claytonff44ab42011-04-23 02:04:55 +00001928 m_expr_prefix_file (),
1929 m_expr_prefix_contents_sp (),
Jim Ingham10de7d12011-05-04 03:43:18 +00001930 m_prefer_dynamic_value (2),
Greg Claytonff44ab42011-04-23 02:04:55 +00001931 m_skip_prologue (true, true),
Enrico Granata018921d2011-08-12 02:00:06 +00001932 m_source_map (NULL, NULL),
Enrico Granata91544802011-09-06 19:20:51 +00001933 m_max_children_display(256),
1934 m_max_strlen_length(1024)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001935{
1936 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1937 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1938 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1939 // This is true for CreateInstanceName() too.
1940
1941 if (GetInstanceName () == InstanceSettings::InvalidName())
1942 {
1943 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1944 m_owner.RegisterInstanceSettings (this);
1945 }
1946
1947 if (live_instance)
1948 {
1949 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1950 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001951 }
1952}
1953
1954TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonff44ab42011-04-23 02:04:55 +00001955 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString()),
1956 m_expr_prefix_file (rhs.m_expr_prefix_file),
1957 m_expr_prefix_contents_sp (rhs.m_expr_prefix_contents_sp),
1958 m_prefer_dynamic_value (rhs.m_prefer_dynamic_value),
1959 m_skip_prologue (rhs.m_skip_prologue),
Enrico Granata018921d2011-08-12 02:00:06 +00001960 m_source_map (rhs.m_source_map),
Enrico Granata91544802011-09-06 19:20:51 +00001961 m_max_children_display(rhs.m_max_children_display),
1962 m_max_strlen_length(rhs.m_max_strlen_length)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001963{
1964 if (m_instance_name != InstanceSettings::GetDefaultName())
1965 {
1966 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1967 CopyInstanceSettings (pending_settings,false);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001968 }
1969}
1970
1971TargetInstanceSettings::~TargetInstanceSettings ()
1972{
1973}
1974
1975TargetInstanceSettings&
1976TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1977{
1978 if (this != &rhs)
1979 {
1980 }
1981
1982 return *this;
1983}
1984
Caroline Tice5bc8c972010-09-20 20:44:43 +00001985void
1986TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1987 const char *index_value,
1988 const char *value,
1989 const ConstString &instance_name,
1990 const SettingEntry &entry,
Greg Claytonb3448432011-03-24 21:19:54 +00001991 VarSetOperationType op,
Caroline Tice5bc8c972010-09-20 20:44:43 +00001992 Error &err,
1993 bool pending)
1994{
Greg Claytond284b662011-02-18 01:44:25 +00001995 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001996 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001997 err = UserSettingsController::UpdateFileSpecOptionValue (value, op, m_expr_prefix_file);
1998 if (err.Success())
Sean Callanan77e93942010-10-29 00:29:03 +00001999 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002000 switch (op)
Sean Callanan77e93942010-10-29 00:29:03 +00002001 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002002 default:
2003 break;
2004 case eVarSetOperationAssign:
2005 case eVarSetOperationAppend:
Sean Callanan77e93942010-10-29 00:29:03 +00002006 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002007 if (!m_expr_prefix_file.GetCurrentValue().Exists())
2008 {
2009 err.SetErrorToGenericError ();
2010 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
2011 return;
2012 }
2013
2014 m_expr_prefix_contents_sp = m_expr_prefix_file.GetCurrentValue().ReadFileContents();
2015
2016 if (!m_expr_prefix_contents_sp && m_expr_prefix_contents_sp->GetByteSize() == 0)
2017 {
2018 err.SetErrorStringWithFormat ("Couldn't read data from '%s'\n", value);
2019 m_expr_prefix_contents_sp.reset();
2020 }
Sean Callanan77e93942010-10-29 00:29:03 +00002021 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002022 break;
2023 case eVarSetOperationClear:
2024 m_expr_prefix_contents_sp.reset();
Sean Callanan77e93942010-10-29 00:29:03 +00002025 }
Sean Callanan77e93942010-10-29 00:29:03 +00002026 }
2027 }
Jim Inghame41494a2011-04-16 00:01:13 +00002028 else if (var_name == GetSettingNameForPreferDynamicValue())
2029 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002030 int new_value;
2031 UserSettingsController::UpdateEnumVariable (g_dynamic_value_types, &new_value, value, err);
2032 if (err.Success())
2033 m_prefer_dynamic_value = new_value;
Greg Clayton17cd9952011-04-22 03:55:06 +00002034 }
2035 else if (var_name == GetSettingNameForSkipPrologue())
2036 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002037 err = UserSettingsController::UpdateBooleanOptionValue (value, op, m_skip_prologue);
2038 }
Enrico Granata018921d2011-08-12 02:00:06 +00002039 else if (var_name == GetSettingNameForMaxChildren())
2040 {
2041 bool ok;
2042 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2043 if (ok)
2044 m_max_children_display = new_value;
2045 }
Enrico Granata91544802011-09-06 19:20:51 +00002046 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2047 {
2048 bool ok;
2049 uint32_t new_value = Args::StringToUInt32(value, 0, 10, &ok);
2050 if (ok)
2051 m_max_strlen_length = new_value;
2052 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002053 else if (var_name == GetSettingNameForSourcePathMap ())
2054 {
2055 switch (op)
2056 {
2057 case eVarSetOperationReplace:
2058 case eVarSetOperationInsertBefore:
2059 case eVarSetOperationInsertAfter:
2060 case eVarSetOperationRemove:
2061 default:
2062 break;
2063 case eVarSetOperationAssign:
2064 m_source_map.Clear(true);
2065 // Fall through to append....
2066 case eVarSetOperationAppend:
2067 {
2068 Args args(value);
2069 const uint32_t argc = args.GetArgumentCount();
2070 if (argc & 1 || argc == 0)
2071 {
2072 err.SetErrorStringWithFormat ("an even number of paths must be supplied to to the source-map setting: %u arguments given", argc);
2073 }
2074 else
2075 {
2076 char resolved_new_path[PATH_MAX];
2077 FileSpec file_spec;
2078 const char *old_path;
2079 for (uint32_t idx = 0; (old_path = args.GetArgumentAtIndex(idx)) != NULL; idx += 2)
2080 {
2081 const char *new_path = args.GetArgumentAtIndex(idx+1);
2082 assert (new_path); // We have an even number of paths, this shouldn't happen!
2083
2084 file_spec.SetFile(new_path, true);
2085 if (file_spec.Exists())
2086 {
2087 if (file_spec.GetPath (resolved_new_path, sizeof(resolved_new_path)) >= sizeof(resolved_new_path))
2088 {
2089 err.SetErrorStringWithFormat("new path '%s' is too long", new_path);
2090 return;
2091 }
2092 }
2093 else
2094 {
2095 err.SetErrorStringWithFormat("new path '%s' doesn't exist", new_path);
2096 return;
2097 }
2098 m_source_map.Append(ConstString (old_path), ConstString (resolved_new_path), true);
2099 }
2100 }
2101 }
2102 break;
2103
2104 case eVarSetOperationClear:
2105 m_source_map.Clear(true);
2106 break;
2107 }
Jim Inghame41494a2011-04-16 00:01:13 +00002108 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002109}
2110
2111void
Greg Claytond284b662011-02-18 01:44:25 +00002112TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002113{
Sean Callanan77e93942010-10-29 00:29:03 +00002114 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
2115
2116 if (!new_settings_ptr)
2117 return;
2118
Greg Claytonff44ab42011-04-23 02:04:55 +00002119 m_expr_prefix_file = new_settings_ptr->m_expr_prefix_file;
2120 m_expr_prefix_contents_sp = new_settings_ptr->m_expr_prefix_contents_sp;
2121 m_prefer_dynamic_value = new_settings_ptr->m_prefer_dynamic_value;
2122 m_skip_prologue = new_settings_ptr->m_skip_prologue;
Enrico Granata018921d2011-08-12 02:00:06 +00002123 m_max_children_display = new_settings_ptr->m_max_children_display;
Enrico Granata91544802011-09-06 19:20:51 +00002124 m_max_strlen_length = new_settings_ptr->m_max_strlen_length;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002125}
2126
Caroline Ticebcb5b452010-09-20 21:37:42 +00002127bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00002128TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
2129 const ConstString &var_name,
2130 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00002131 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00002132{
Greg Claytond284b662011-02-18 01:44:25 +00002133 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00002134 {
Greg Claytonff44ab42011-04-23 02:04:55 +00002135 char path[PATH_MAX];
2136 const size_t path_len = m_expr_prefix_file.GetCurrentValue().GetPath (path, sizeof(path));
2137 if (path_len > 0)
2138 value.AppendString (path, path_len);
Sean Callanan77e93942010-10-29 00:29:03 +00002139 }
Jim Inghame41494a2011-04-16 00:01:13 +00002140 else if (var_name == GetSettingNameForPreferDynamicValue())
2141 {
Jim Ingham10de7d12011-05-04 03:43:18 +00002142 value.AppendString (g_dynamic_value_types[m_prefer_dynamic_value].string_value);
Jim Inghame41494a2011-04-16 00:01:13 +00002143 }
Greg Clayton17cd9952011-04-22 03:55:06 +00002144 else if (var_name == GetSettingNameForSkipPrologue())
2145 {
2146 if (m_skip_prologue)
2147 value.AppendString ("true");
2148 else
2149 value.AppendString ("false");
2150 }
Greg Claytonff44ab42011-04-23 02:04:55 +00002151 else if (var_name == GetSettingNameForSourcePathMap ())
2152 {
2153 }
Enrico Granata018921d2011-08-12 02:00:06 +00002154 else if (var_name == GetSettingNameForMaxChildren())
2155 {
2156 StreamString count_str;
2157 count_str.Printf ("%d", m_max_children_display);
2158 value.AppendString (count_str.GetData());
2159 }
Enrico Granata91544802011-09-06 19:20:51 +00002160 else if (var_name == GetSettingNameForMaxStringSummaryLength())
2161 {
2162 StreamString count_str;
2163 count_str.Printf ("%d", m_max_strlen_length);
2164 value.AppendString (count_str.GetData());
2165 }
Sean Callanan77e93942010-10-29 00:29:03 +00002166 else
2167 {
2168 if (err)
2169 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
2170 return false;
2171 }
2172
2173 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00002174}
2175
2176const ConstString
2177TargetInstanceSettings::CreateInstanceName ()
2178{
Caroline Tice5bc8c972010-09-20 20:44:43 +00002179 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00002180 static int instance_count = 1;
2181
Caroline Tice5bc8c972010-09-20 20:44:43 +00002182 sstr.Printf ("target_%d", instance_count);
2183 ++instance_count;
2184
2185 const ConstString ret_val (sstr.GetData());
2186 return ret_val;
2187}
2188
2189//--------------------------------------------------
2190// Target::SettingsController Variable Tables
2191//--------------------------------------------------
Jim Ingham10de7d12011-05-04 03:43:18 +00002192OptionEnumValueElement
2193TargetInstanceSettings::g_dynamic_value_types[] =
2194{
Greg Clayton577fbc32011-05-30 00:39:48 +00002195{ eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2196{ eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2197{ eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Jim Ingham10de7d12011-05-04 03:43:18 +00002198{ 0, NULL, NULL }
2199};
Caroline Tice5bc8c972010-09-20 20:44:43 +00002200
2201SettingEntry
2202Target::SettingsController::global_settings_table[] =
2203{
Greg Claytond284b662011-02-18 01:44:25 +00002204 // var-name var-type default enum init'd hidden help-text
2205 // ================= ================== =========== ==== ====== ====== =========================================================================
2206 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
2207 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
2208};
2209
Caroline Tice5bc8c972010-09-20 20:44:43 +00002210SettingEntry
2211Target::SettingsController::instance_settings_table[] =
2212{
Enrico Granata91544802011-09-06 19:20:51 +00002213 // var-name var-type default enum init'd hidden help-text
2214 // ================= ================== =============== ======================= ====== ====== =========================================================================
2215 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
2216 { TSC_PREFER_DYNAMIC , eSetVarTypeEnum , NULL , g_dynamic_value_types, false, false, "Should printed values be shown as their dynamic value." },
2217 { TSC_SKIP_PROLOGUE , eSetVarTypeBoolean, "true" , NULL, false, false, "Skip function prologues when setting breakpoints by name." },
2218 { TSC_SOURCE_MAP , eSetVarTypeArray , NULL , NULL, false, false, "Source path remappings to use when locating source files from debug information." },
2219 { TSC_MAX_CHILDREN , eSetVarTypeInt , "256" , NULL, true, false, "Maximum number of children to expand in any level of depth." },
2220 { TSC_MAX_STRLENSUMMARY , eSetVarTypeInt , "1024" , NULL, true, false, "Maximum number of characters to show when using %s in summary strings." },
2221 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00002222};