blob: 60f2e2d7d5f03c49dc95887722c974c5abe1d583 [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"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/StreamString.h"
Greg Clayton427f2902010-12-14 02:59:59 +000024#include "lldb/Core/Timer.h"
25#include "lldb/Core/ValueObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Host/Host.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000027#include "lldb/Interpreter/CommandInterpreter.h"
28#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/lldb-private-log.h"
30#include "lldb/Symbol/ObjectFile.h"
31#include "lldb/Target/Process.h"
Greg Clayton427f2902010-12-14 02:59:59 +000032#include "lldb/Target/StackFrame.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000033#include "lldb/Target/Thread.h"
34#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035
36using namespace lldb;
37using namespace lldb_private;
38
39//----------------------------------------------------------------------
40// Target constructor
41//----------------------------------------------------------------------
Greg Claytonb1888f22011-03-19 01:12:21 +000042Target::Target(Debugger &debugger, const lldb::PlatformSP &platform_sp) :
Greg Clayton49ce6822010-10-31 03:01:06 +000043 Broadcaster("lldb.target"),
Greg Claytonb1888f22011-03-19 01:12:21 +000044 m_platform_sp (platform_sp),
Greg Claytonc0c1b0c2010-11-19 03:46:01 +000045 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton63094e02010-06-23 01:19:29 +000046 m_debugger (debugger),
Greg Claytonbdcda462010-12-20 20:49:23 +000047 m_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +000048 m_images(),
Greg Claytoneea26402010-09-14 23:36:40 +000049 m_section_load_list (),
Chris Lattner24943d22010-06-08 16:52:24 +000050 m_breakpoint_list (false),
51 m_internal_breakpoint_list (true),
52 m_process_sp(),
Chris Lattner24943d22010-06-08 16:52:24 +000053 m_search_filter_sp(),
54 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton427f2902010-12-14 02:59:59 +000055 m_scratch_ast_context_ap (NULL),
Jim Inghamd60d94a2011-03-11 03:53:59 +000056 m_persistent_variables (),
57 m_stop_hook_next_id(0)
Chris Lattner24943d22010-06-08 16:52:24 +000058{
Greg Clayton49ce6822010-10-31 03:01:06 +000059 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
60 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
61 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
62
Greg Claytone005f2c2010-11-06 01:53:30 +000063 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000064 if (log)
65 log->Printf ("%p Target::Target()", this);
66}
67
68//----------------------------------------------------------------------
69// Destructor
70//----------------------------------------------------------------------
71Target::~Target()
72{
Greg Claytone005f2c2010-11-06 01:53:30 +000073 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +000074 if (log)
75 log->Printf ("%p Target::~Target()", this);
76 DeleteCurrentProcess ();
77}
78
79void
Caroline Tice7826c882010-10-26 03:11:13 +000080Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner24943d22010-06-08 16:52:24 +000081{
Greg Clayton3fed8b92010-10-08 00:21:05 +000082// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Tice7826c882010-10-26 03:11:13 +000083 if (description_level != lldb::eDescriptionLevelBrief)
84 {
85 s->Indent();
86 s->PutCString("Target\n");
87 s->IndentMore();
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000088 m_images.Dump(s);
89 m_breakpoint_list.Dump(s);
90 m_internal_breakpoint_list.Dump(s);
91 s->IndentLess();
Caroline Tice7826c882010-10-26 03:11:13 +000092 }
93 else
94 {
Greg Claytona66ba462010-10-30 04:51:46 +000095 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Tice7826c882010-10-26 03:11:13 +000096 }
Chris Lattner24943d22010-06-08 16:52:24 +000097}
98
99void
100Target::DeleteCurrentProcess ()
101{
102 if (m_process_sp.get())
103 {
Greg Clayton49480b12010-09-14 23:52:43 +0000104 m_section_load_list.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000105 if (m_process_sp->IsAlive())
106 m_process_sp->Destroy();
Jim Ingham88fa7bd2011-02-16 17:54:55 +0000107
108 m_process_sp->Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000109
110 // Do any cleanup of the target we need to do between process instances.
111 // NB It is better to do this before destroying the process in case the
112 // clean up needs some help from the process.
113 m_breakpoint_list.ClearAllBreakpointSites();
114 m_internal_breakpoint_list.ClearAllBreakpointSites();
115 m_process_sp.reset();
116 }
117}
118
119const lldb::ProcessSP &
120Target::CreateProcess (Listener &listener, const char *plugin_name)
121{
122 DeleteCurrentProcess ();
123 m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener));
124 return m_process_sp;
125}
126
127const lldb::ProcessSP &
128Target::GetProcessSP () const
129{
130 return m_process_sp;
131}
132
133lldb::TargetSP
134Target::GetSP()
135{
Greg Clayton63094e02010-06-23 01:19:29 +0000136 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner24943d22010-06-08 16:52:24 +0000137}
138
139BreakpointList &
140Target::GetBreakpointList(bool internal)
141{
142 if (internal)
143 return m_internal_breakpoint_list;
144 else
145 return m_breakpoint_list;
146}
147
148const BreakpointList &
149Target::GetBreakpointList(bool internal) const
150{
151 if (internal)
152 return m_internal_breakpoint_list;
153 else
154 return m_breakpoint_list;
155}
156
157BreakpointSP
158Target::GetBreakpointByID (break_id_t break_id)
159{
160 BreakpointSP bp_sp;
161
162 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
163 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
164 else
165 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
166
167 return bp_sp;
168}
169
170BreakpointSP
171Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal)
172{
173 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
174 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines));
175 return CreateBreakpoint (filter_sp, resolver_sp, internal);
176}
177
178
179BreakpointSP
Greg Clayton33ed1702010-08-24 00:45:41 +0000180Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000181{
Chris Lattner24943d22010-06-08 16:52:24 +0000182 Address so_addr;
183 // Attempt to resolve our load address if possible, though it is ok if
184 // it doesn't resolve to section/offset.
185
Greg Clayton33ed1702010-08-24 00:45:41 +0000186 // Try and resolve as a load address if possible
Greg Claytoneea26402010-09-14 23:36:40 +0000187 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton33ed1702010-08-24 00:45:41 +0000188 if (!so_addr.IsValid())
189 {
190 // The address didn't resolve, so just set this as an absolute address
191 so_addr.SetOffset (addr);
192 }
193 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal));
Chris Lattner24943d22010-06-08 16:52:24 +0000194 return bp_sp;
195}
196
197BreakpointSP
198Target::CreateBreakpoint (Address &addr, bool internal)
199{
200 TargetSP target_sp = this->GetSP();
201 SearchFilterSP filter_sp(new SearchFilter (target_sp));
202 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
203 return CreateBreakpoint (filter_sp, resolver_sp, internal);
204}
205
206BreakpointSP
Greg Clayton12bec712010-06-28 21:30:43 +0000207Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner24943d22010-06-08 16:52:24 +0000208{
Greg Clayton12bec712010-06-28 21:30:43 +0000209 BreakpointSP bp_sp;
210 if (func_name)
211 {
212 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
213 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact));
214 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal);
215 }
216 return bp_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000217}
218
219
220SearchFilterSP
221Target::GetSearchFilterForModule (const FileSpec *containingModule)
222{
223 SearchFilterSP filter_sp;
224 lldb::TargetSP target_sp = this->GetSP();
225 if (containingModule != NULL)
226 {
227 // TODO: We should look into sharing module based search filters
228 // across many breakpoints like we do for the simple target based one
229 filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule));
230 }
231 else
232 {
233 if (m_search_filter_sp.get() == NULL)
234 m_search_filter_sp.reset (new SearchFilter (target_sp));
235 filter_sp = m_search_filter_sp;
236 }
237 return filter_sp;
238}
239
240BreakpointSP
241Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal)
242{
243 SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule));
244 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex));
245
246 return CreateBreakpoint (filter_sp, resolver_sp, internal);
247}
248
249BreakpointSP
250Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal)
251{
252 BreakpointSP bp_sp;
253 if (filter_sp && resolver_sp)
254 {
255 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp));
256 resolver_sp->SetBreakpoint (bp_sp.get());
257
258 if (internal)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000259 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000260 else
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000261 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000262
Greg Claytone005f2c2010-11-06 01:53:30 +0000263 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000264 if (log)
265 {
266 StreamString s;
267 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
268 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData());
269 }
270
Chris Lattner24943d22010-06-08 16:52:24 +0000271 bp_sp->ResolveBreakpoint();
272 }
Jim Inghamd1686902010-10-14 23:45:03 +0000273
274 if (!internal && bp_sp)
275 {
276 m_last_created_breakpoint = bp_sp;
277 }
278
Chris Lattner24943d22010-06-08 16:52:24 +0000279 return bp_sp;
280}
281
282void
283Target::RemoveAllBreakpoints (bool internal_also)
284{
Greg Claytone005f2c2010-11-06 01:53:30 +0000285 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000286 if (log)
287 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
288
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000289 m_breakpoint_list.RemoveAll (true);
Chris Lattner24943d22010-06-08 16:52:24 +0000290 if (internal_also)
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000291 m_internal_breakpoint_list.RemoveAll (false);
Jim Inghamd1686902010-10-14 23:45:03 +0000292
293 m_last_created_breakpoint.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000294}
295
296void
297Target::DisableAllBreakpoints (bool internal_also)
298{
Greg Claytone005f2c2010-11-06 01:53:30 +0000299 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000300 if (log)
301 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
302
303 m_breakpoint_list.SetEnabledAll (false);
304 if (internal_also)
305 m_internal_breakpoint_list.SetEnabledAll (false);
306}
307
308void
309Target::EnableAllBreakpoints (bool internal_also)
310{
Greg Claytone005f2c2010-11-06 01:53:30 +0000311 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000312 if (log)
313 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
314
315 m_breakpoint_list.SetEnabledAll (true);
316 if (internal_also)
317 m_internal_breakpoint_list.SetEnabledAll (true);
318}
319
320bool
321Target::RemoveBreakpointByID (break_id_t break_id)
322{
Greg Claytone005f2c2010-11-06 01:53:30 +0000323 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000324 if (log)
325 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
326
327 if (DisableBreakpointByID (break_id))
328 {
329 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000330 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000331 else
Jim Inghamd1686902010-10-14 23:45:03 +0000332 {
Greg Clayton22c9e0d2011-01-24 23:35:47 +0000333 if (m_last_created_breakpoint)
334 {
335 if (m_last_created_breakpoint->GetID() == break_id)
336 m_last_created_breakpoint.reset();
337 }
Greg Claytonc7f5d5c2010-07-23 23:33:17 +0000338 m_breakpoint_list.Remove(break_id, true);
Jim Inghamd1686902010-10-14 23:45:03 +0000339 }
Chris Lattner24943d22010-06-08 16:52:24 +0000340 return true;
341 }
342 return false;
343}
344
345bool
346Target::DisableBreakpointByID (break_id_t break_id)
347{
Greg Claytone005f2c2010-11-06 01:53:30 +0000348 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000349 if (log)
350 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
351
352 BreakpointSP bp_sp;
353
354 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
355 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
356 else
357 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
358 if (bp_sp)
359 {
360 bp_sp->SetEnabled (false);
361 return true;
362 }
363 return false;
364}
365
366bool
367Target::EnableBreakpointByID (break_id_t break_id)
368{
Greg Claytone005f2c2010-11-06 01:53:30 +0000369 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner24943d22010-06-08 16:52:24 +0000370 if (log)
371 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
372 __FUNCTION__,
373 break_id,
374 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
375
376 BreakpointSP bp_sp;
377
378 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
379 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
380 else
381 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
382
383 if (bp_sp)
384 {
385 bp_sp->SetEnabled (true);
386 return true;
387 }
388 return false;
389}
390
391ModuleSP
392Target::GetExecutableModule ()
393{
394 ModuleSP executable_sp;
395 if (m_images.GetSize() > 0)
396 executable_sp = m_images.GetModuleAtIndex(0);
397 return executable_sp;
398}
399
400void
401Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
402{
403 m_images.Clear();
404 m_scratch_ast_context_ap.reset();
405
406 if (executable_sp.get())
407 {
408 Timer scoped_timer (__PRETTY_FUNCTION__,
409 "Target::SetExecutableModule (executable = '%s/%s')",
410 executable_sp->GetFileSpec().GetDirectory().AsCString(),
411 executable_sp->GetFileSpec().GetFilename().AsCString());
412
413 m_images.Append(executable_sp); // The first image is our exectuable file
414
415 ArchSpec exe_arch = executable_sp->GetArchitecture();
Jim Ingham7508e732010-08-09 23:31:02 +0000416 // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module.
417 if (!m_arch_spec.IsValid())
418 m_arch_spec = exe_arch;
419
Chris Lattner24943d22010-06-08 16:52:24 +0000420 FileSpecList dependent_files;
Greg Claytone4b9c1f2011-03-08 22:40:15 +0000421 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner24943d22010-06-08 16:52:24 +0000422
423 if (executable_objfile)
424 {
425 executable_objfile->GetDependentModules(dependent_files);
426 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
427 {
Greg Claytonb1888f22011-03-19 01:12:21 +0000428 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
429 FileSpec platform_dependent_file_spec;
430 if (m_platform_sp)
431 m_platform_sp->GetFile (dependent_file_spec, platform_dependent_file_spec);
432 else
433 platform_dependent_file_spec = dependent_file_spec;
434
435 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
436 exe_arch));
Chris Lattner24943d22010-06-08 16:52:24 +0000437 if (image_module_sp.get())
438 {
439 //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY
440 ObjectFile *objfile = image_module_sp->GetObjectFile();
441 if (objfile)
442 objfile->GetDependentModules(dependent_files);
443 }
444 }
445 }
446
447 // Now see if we know the target triple, and if so, create our scratch AST context:
Greg Clayton395fc332011-02-15 21:59:32 +0000448 if (m_arch_spec.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000449 {
Greg Clayton395fc332011-02-15 21:59:32 +0000450 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch_spec.GetTriple().str().c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000451 }
452 }
Caroline Tice1ebef442010-09-27 00:30:10 +0000453
454 UpdateInstanceName();
Chris Lattner24943d22010-06-08 16:52:24 +0000455}
456
457
Jim Ingham7508e732010-08-09 23:31:02 +0000458bool
459Target::SetArchitecture (const ArchSpec &arch_spec)
460{
461 if (m_arch_spec == arch_spec)
462 {
463 // If we're setting the architecture to our current architecture, we
464 // don't need to do anything.
465 return true;
466 }
467 else if (!m_arch_spec.IsValid())
468 {
469 // If we haven't got a valid arch spec, then we just need to set it.
470 m_arch_spec = arch_spec;
471 return true;
472 }
473 else
474 {
475 // If we have an executable file, try to reset the executable to the desired architecture
476 m_arch_spec = arch_spec;
477 ModuleSP executable_sp = GetExecutableModule ();
478 m_images.Clear();
479 m_scratch_ast_context_ap.reset();
Jim Ingham7508e732010-08-09 23:31:02 +0000480 // Need to do something about unsetting breakpoints.
481
482 if (executable_sp)
483 {
484 FileSpec exec_file_spec = executable_sp->GetFileSpec();
485 Error error = ModuleList::GetSharedModule(exec_file_spec,
486 arch_spec,
487 NULL,
488 NULL,
489 0,
490 executable_sp,
491 NULL,
492 NULL);
493
494 if (!error.Fail() && executable_sp)
495 {
496 SetExecutableModule (executable_sp, true);
497 return true;
498 }
499 else
500 {
501 return false;
502 }
503 }
504 else
505 {
506 return false;
507 }
508 }
509}
Chris Lattner24943d22010-06-08 16:52:24 +0000510
Chris Lattner24943d22010-06-08 16:52:24 +0000511void
512Target::ModuleAdded (ModuleSP &module_sp)
513{
514 // A module is being added to this target for the first time
515 ModuleList module_list;
516 module_list.Append(module_sp);
517 ModulesDidLoad (module_list);
518}
519
520void
521Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp)
522{
523 // A module is being added to this target for the first time
524 ModuleList module_list;
525 module_list.Append (old_module_sp);
526 ModulesDidUnload (module_list);
527 module_list.Clear ();
528 module_list.Append (new_module_sp);
529 ModulesDidLoad (module_list);
530}
531
532void
533Target::ModulesDidLoad (ModuleList &module_list)
534{
535 m_breakpoint_list.UpdateBreakpoints (module_list, true);
536 // TODO: make event data that packages up the module_list
537 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
538}
539
540void
541Target::ModulesDidUnload (ModuleList &module_list)
542{
543 m_breakpoint_list.UpdateBreakpoints (module_list, false);
Greg Clayton7b9fcc02010-12-06 23:51:26 +0000544
545 // Remove the images from the target image list
546 m_images.Remove(module_list);
547
Chris Lattner24943d22010-06-08 16:52:24 +0000548 // TODO: make event data that packages up the module_list
549 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
550}
551
552size_t
Greg Clayton26100dc2011-01-07 01:57:07 +0000553Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
554{
555 const Section *section = addr.GetSection();
556 if (section && section->GetModule())
557 {
558 ObjectFile *objfile = section->GetModule()->GetObjectFile();
559 if (objfile)
560 {
561 size_t bytes_read = section->ReadSectionDataFromObjectFile (objfile,
562 addr.GetOffset(),
563 dst,
564 dst_len);
565 if (bytes_read > 0)
566 return bytes_read;
567 else
568 error.SetErrorStringWithFormat("error reading data from section %s", section->GetName().GetCString());
569 }
570 else
571 {
572 error.SetErrorString("address isn't from a object file");
573 }
574 }
575 else
576 {
577 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
578 }
579 return 0;
580}
581
582size_t
583Target::ReadMemory (const Address& addr, bool prefer_file_cache, void *dst, size_t dst_len, Error &error)
Chris Lattner24943d22010-06-08 16:52:24 +0000584{
Chris Lattner24943d22010-06-08 16:52:24 +0000585 error.Clear();
Greg Clayton26100dc2011-01-07 01:57:07 +0000586
Greg Clayton70436352010-06-30 23:03:03 +0000587 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
588
Greg Clayton26100dc2011-01-07 01:57:07 +0000589 size_t bytes_read = 0;
Greg Clayton70436352010-06-30 23:03:03 +0000590 Address resolved_addr(addr);
591 if (!resolved_addr.IsSectionOffset())
592 {
593 if (process_is_valid)
Chris Lattner24943d22010-06-08 16:52:24 +0000594 {
Greg Claytoneea26402010-09-14 23:36:40 +0000595 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Clayton70436352010-06-30 23:03:03 +0000596 }
597 else
598 {
599 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
600 }
601 }
602
Greg Clayton26100dc2011-01-07 01:57:07 +0000603 if (prefer_file_cache)
604 {
605 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
606 if (bytes_read > 0)
607 return bytes_read;
608 }
Greg Clayton70436352010-06-30 23:03:03 +0000609
610 if (process_is_valid)
611 {
Greg Claytoneea26402010-09-14 23:36:40 +0000612 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Clayton70436352010-06-30 23:03:03 +0000613 if (load_addr == LLDB_INVALID_ADDRESS)
614 {
615 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
616 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
617 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
618 resolved_addr.GetFileAddress());
619 else
620 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
621 }
622 else
623 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000624 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner24943d22010-06-08 16:52:24 +0000625 if (bytes_read != dst_len)
626 {
627 if (error.Success())
628 {
629 if (bytes_read == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000630 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000631 else
Greg Clayton70436352010-06-30 23:03:03 +0000632 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 +0000633 }
634 }
Greg Clayton70436352010-06-30 23:03:03 +0000635 if (bytes_read)
636 return bytes_read;
637 // If the address is not section offset we have an address that
638 // doesn't resolve to any address in any currently loaded shared
639 // libaries and we failed to read memory so there isn't anything
640 // more we can do. If it is section offset, we might be able to
641 // read cached memory from the object file.
642 if (!resolved_addr.IsSectionOffset())
643 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000644 }
Chris Lattner24943d22010-06-08 16:52:24 +0000645 }
Greg Clayton70436352010-06-30 23:03:03 +0000646
Greg Clayton26100dc2011-01-07 01:57:07 +0000647 if (!prefer_file_cache)
Greg Clayton70436352010-06-30 23:03:03 +0000648 {
Greg Clayton26100dc2011-01-07 01:57:07 +0000649 // If we didn't already try and read from the object file cache, then
650 // try it after failing to read from the process.
651 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Clayton70436352010-06-30 23:03:03 +0000652 }
653 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000654}
655
656
657ModuleSP
658Target::GetSharedModule
659(
660 const FileSpec& file_spec,
661 const ArchSpec& arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000662 const lldb_private::UUID *uuid_ptr,
Chris Lattner24943d22010-06-08 16:52:24 +0000663 const ConstString *object_name,
664 off_t object_offset,
665 Error *error_ptr
666)
667{
668 // Don't pass in the UUID so we can tell if we have a stale value in our list
669 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
670 bool did_create_module = false;
671 ModuleSP module_sp;
672
673 // If there are image search path entries, try to use them first to acquire a suitable image.
674
675 Error error;
676
677 if (m_image_search_paths.GetSize())
678 {
679 FileSpec transformed_spec;
680 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
681 {
682 transformed_spec.GetFilename() = file_spec.GetFilename();
683 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
684 }
685 }
686
687 // If a module hasn't been found yet, use the unmodified path.
688
689 if (!module_sp)
690 {
691 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
692 }
693
694 if (module_sp)
695 {
696 m_images.Append (module_sp);
697 if (did_create_module)
698 {
699 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
700 ModuleUpdated(old_module_sp, module_sp);
701 else
702 ModuleAdded(module_sp);
703 }
704 }
705 if (error_ptr)
706 *error_ptr = error;
707 return module_sp;
708}
709
710
711Target *
712Target::CalculateTarget ()
713{
714 return this;
715}
716
717Process *
718Target::CalculateProcess ()
719{
720 return NULL;
721}
722
723Thread *
724Target::CalculateThread ()
725{
726 return NULL;
727}
728
729StackFrame *
730Target::CalculateStackFrame ()
731{
732 return NULL;
733}
734
735void
Greg Claytona830adb2010-10-04 01:05:56 +0000736Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000737{
738 exe_ctx.target = this;
739 exe_ctx.process = NULL; // Do NOT fill in process...
740 exe_ctx.thread = NULL;
741 exe_ctx.frame = NULL;
742}
743
744PathMappingList &
745Target::GetImageSearchPathList ()
746{
747 return m_image_search_paths;
748}
749
750void
751Target::ImageSearchPathsChanged
752(
753 const PathMappingList &path_list,
754 void *baton
755)
756{
757 Target *target = (Target *)baton;
758 if (target->m_images.GetSize() > 1)
759 {
760 ModuleSP exe_module_sp (target->GetExecutableModule());
761 if (exe_module_sp)
762 {
763 target->m_images.Clear();
764 target->SetExecutableModule (exe_module_sp, true);
765 }
766 }
767}
768
769ClangASTContext *
770Target::GetScratchClangASTContext()
771{
772 return m_scratch_ast_context_ap.get();
773}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000774
Greg Clayton990de7b2010-11-18 23:32:35 +0000775void
Caroline Tice2a456812011-03-10 22:14:10 +0000776Target::SettingsInitialize ()
Caroline Tice5bc8c972010-09-20 20:44:43 +0000777{
Greg Clayton990de7b2010-11-18 23:32:35 +0000778 UserSettingsControllerSP &usc = GetSettingsController();
779 usc.reset (new SettingsController);
780 UserSettingsController::InitializeSettingsController (usc,
781 SettingsController::global_settings_table,
782 SettingsController::instance_settings_table);
Caroline Tice2a456812011-03-10 22:14:10 +0000783
784 // Now call SettingsInitialize() on each 'child' setting of Target
785 Process::SettingsInitialize ();
Greg Clayton990de7b2010-11-18 23:32:35 +0000786}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000787
Greg Clayton990de7b2010-11-18 23:32:35 +0000788void
Caroline Tice2a456812011-03-10 22:14:10 +0000789Target::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +0000790{
Caroline Tice2a456812011-03-10 22:14:10 +0000791
792 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
793
794 Process::SettingsTerminate ();
795
796 // Now terminate Target Settings.
797
Greg Clayton990de7b2010-11-18 23:32:35 +0000798 UserSettingsControllerSP &usc = GetSettingsController();
799 UserSettingsController::FinalizeSettingsController (usc);
800 usc.reset();
801}
Caroline Tice5bc8c972010-09-20 20:44:43 +0000802
Greg Clayton990de7b2010-11-18 23:32:35 +0000803UserSettingsControllerSP &
804Target::GetSettingsController ()
805{
806 static UserSettingsControllerSP g_settings_controller;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000807 return g_settings_controller;
808}
809
810ArchSpec
811Target::GetDefaultArchitecture ()
812{
Greg Clayton940b1032011-02-23 00:35:02 +0000813 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
814
815 if (settings_controller_sp)
816 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
817 return ArchSpec();
Caroline Tice5bc8c972010-09-20 20:44:43 +0000818}
819
820void
Greg Clayton940b1032011-02-23 00:35:02 +0000821Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Tice5bc8c972010-09-20 20:44:43 +0000822{
Greg Clayton940b1032011-02-23 00:35:02 +0000823 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
824
825 if (settings_controller_sp)
826 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Tice5bc8c972010-09-20 20:44:43 +0000827}
828
Greg Claytona830adb2010-10-04 01:05:56 +0000829Target *
830Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
831{
832 // The target can either exist in the "process" of ExecutionContext, or in
833 // the "target_sp" member of SymbolContext. This accessor helper function
834 // will get the target from one of these locations.
835
836 Target *target = NULL;
837 if (sc_ptr != NULL)
838 target = sc_ptr->target_sp.get();
839 if (target == NULL)
840 {
841 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
842 target = &exe_ctx_ptr->process->GetTarget();
843 }
844 return target;
845}
846
847
Caroline Tice1ebef442010-09-27 00:30:10 +0000848void
849Target::UpdateInstanceName ()
850{
851 StreamString sstr;
852
853 ModuleSP module_sp = GetExecutableModule();
854 if (module_sp)
855 {
Greg Claytonbf6e2102010-10-27 02:06:37 +0000856 sstr.Printf ("%s_%s",
857 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton940b1032011-02-23 00:35:02 +0000858 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytonc0c1b0c2010-11-19 03:46:01 +0000859 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
860 sstr.GetData());
Caroline Tice1ebef442010-09-27 00:30:10 +0000861 }
862}
863
Sean Callanan77e93942010-10-29 00:29:03 +0000864const char *
865Target::GetExpressionPrefixContentsAsCString ()
866{
867 return m_expr_prefix_contents.c_str();
868}
869
Greg Clayton427f2902010-12-14 02:59:59 +0000870ExecutionResults
871Target::EvaluateExpression
872(
873 const char *expr_cstr,
874 StackFrame *frame,
875 bool unwind_on_error,
Sean Callanan6a925532011-01-13 08:53:35 +0000876 bool keep_in_memory,
Greg Clayton427f2902010-12-14 02:59:59 +0000877 lldb::ValueObjectSP &result_valobj_sp
878)
879{
880 ExecutionResults execution_results = eExecutionSetupError;
881
882 result_valobj_sp.reset();
883
884 ExecutionContext exe_ctx;
885 if (frame)
886 {
887 frame->CalculateExecutionContext(exe_ctx);
Greg Claytonc3b61d22010-12-15 05:08:08 +0000888 Error error;
Greg Claytonc67efa42011-01-20 19:27:18 +0000889 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
890 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
891 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton427f2902010-12-14 02:59:59 +0000892 }
893 else if (m_process_sp)
894 {
895 m_process_sp->CalculateExecutionContext(exe_ctx);
896 }
897 else
898 {
899 CalculateExecutionContext(exe_ctx);
900 }
901
902 if (result_valobj_sp)
903 {
904 execution_results = eExecutionCompleted;
905 // We got a result from the frame variable expression path above...
906 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
907
908 lldb::ValueObjectSP const_valobj_sp;
909
910 // Check in case our value is already a constant value
911 if (result_valobj_sp->GetIsConstant())
912 {
913 const_valobj_sp = result_valobj_sp;
914 const_valobj_sp->SetName (persistent_variable_name);
915 }
916 else
917 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(),
918 persistent_variable_name);
919
Sean Callanan6a925532011-01-13 08:53:35 +0000920 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
921
Greg Clayton427f2902010-12-14 02:59:59 +0000922 result_valobj_sp = const_valobj_sp;
923
Sean Callanan6a925532011-01-13 08:53:35 +0000924 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
925 assert (clang_expr_variable_sp.get());
926
927 // Set flags and live data as appropriate
928
929 const Value &result_value = live_valobj_sp->GetValue();
930
931 switch (result_value.GetValueType())
932 {
933 case Value::eValueTypeHostAddress:
934 case Value::eValueTypeFileAddress:
935 // we don't do anything with these for now
936 break;
937 case Value::eValueTypeScalar:
938 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
939 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
940 break;
941 case Value::eValueTypeLoadAddress:
942 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
943 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
944 break;
945 }
Greg Clayton427f2902010-12-14 02:59:59 +0000946 }
947 else
948 {
949 // Make sure we aren't just trying to see the value of a persistent
950 // variable (something like "$0")
Greg Claytona875b642011-01-09 21:07:35 +0000951 lldb::ClangExpressionVariableSP persistent_var_sp;
952 // Only check for persistent variables the expression starts with a '$'
953 if (expr_cstr[0] == '$')
954 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
955
Greg Clayton427f2902010-12-14 02:59:59 +0000956 if (persistent_var_sp)
957 {
958 result_valobj_sp = persistent_var_sp->GetValueObject ();
959 execution_results = eExecutionCompleted;
960 }
961 else
962 {
963 const char *prefix = GetExpressionPrefixContentsAsCString();
964
965 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan6a925532011-01-13 08:53:35 +0000966 unwind_on_error,
967 keep_in_memory,
Greg Clayton427f2902010-12-14 02:59:59 +0000968 expr_cstr,
969 prefix,
970 result_valobj_sp);
971 }
972 }
973 return execution_results;
974}
975
Jim Inghamd60d94a2011-03-11 03:53:59 +0000976lldb::user_id_t
977Target::AddStopHook (Target::StopHookSP &new_hook_sp)
978{
979 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
980 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
981 m_stop_hooks[new_uid] = new_hook_sp;
982 return new_uid;
983}
984
985bool
986Target::RemoveStopHookByID (lldb::user_id_t user_id)
987{
988 size_t num_removed;
989 num_removed = m_stop_hooks.erase (user_id);
990 if (num_removed == 0)
991 return false;
992 else
993 return true;
994}
995
996void
997Target::RemoveAllStopHooks ()
998{
999 m_stop_hooks.clear();
1000}
1001
1002Target::StopHookSP
1003Target::GetStopHookByID (lldb::user_id_t user_id)
1004{
1005 StopHookSP found_hook;
1006
1007 StopHookCollection::iterator specified_hook_iter;
1008 specified_hook_iter = m_stop_hooks.find (user_id);
1009 if (specified_hook_iter != m_stop_hooks.end())
1010 found_hook = (*specified_hook_iter).second;
1011 return found_hook;
1012}
1013
1014bool
1015Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1016{
1017 StopHookCollection::iterator specified_hook_iter;
1018 specified_hook_iter = m_stop_hooks.find (user_id);
1019 if (specified_hook_iter == m_stop_hooks.end())
1020 return false;
1021
1022 (*specified_hook_iter).second->SetIsActive (active_state);
1023 return true;
1024}
1025
1026void
1027Target::SetAllStopHooksActiveState (bool active_state)
1028{
1029 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1030 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1031 {
1032 (*pos).second->SetIsActive (active_state);
1033 }
1034}
1035
1036void
1037Target::RunStopHooks ()
1038{
1039 if (!m_process_sp)
1040 return;
1041
1042 if (m_stop_hooks.empty())
1043 return;
1044
1045 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1046
1047 // If there aren't any active stop hooks, don't bother either:
1048 bool any_active_hooks = false;
1049 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1050 {
1051 if ((*pos).second->IsActive())
1052 {
1053 any_active_hooks = true;
1054 break;
1055 }
1056 }
1057 if (!any_active_hooks)
1058 return;
1059
1060 CommandReturnObject result;
1061
1062 std::vector<ExecutionContext> exc_ctx_with_reasons;
1063 std::vector<SymbolContext> sym_ctx_with_reasons;
1064
1065 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1066 size_t num_threads = cur_threadlist.GetSize();
1067 for (size_t i = 0; i < num_threads; i++)
1068 {
1069 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1070 if (cur_thread_sp->ThreadStoppedForAReason())
1071 {
1072 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1073 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1074 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1075 }
1076 }
1077
1078 // If no threads stopped for a reason, don't run the stop-hooks.
1079 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1080 if (num_exe_ctx == 0)
1081 return;
1082
1083 result.SetImmediateOutputFile (m_debugger.GetOutputFile().GetStream());
1084 result.SetImmediateErrorFile (m_debugger.GetErrorFile().GetStream());
1085
1086 bool keep_going = true;
1087 bool hooks_ran = false;
1088 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1089 {
1090 // result.Clear();
1091 StopHookSP cur_hook_sp = (*pos).second;
1092 if (!cur_hook_sp->IsActive())
1093 continue;
1094
1095 bool any_thread_matched = false;
1096 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1097 {
1098 if ((cur_hook_sp->GetSpecifier () == NULL
1099 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1100 && (cur_hook_sp->GetThreadSpecifier() == NULL
1101 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1102 {
1103 if (!hooks_ran)
1104 {
1105 result.AppendMessage("\n** Stop Hooks **\n");
1106 hooks_ran = true;
1107 }
1108 if (!any_thread_matched)
1109 {
1110 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1111 any_thread_matched = true;
1112 }
1113
1114 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
1115
1116 bool stop_on_continue = true;
1117 bool stop_on_error = true;
1118 bool echo_commands = false;
1119 bool print_results = true;
1120 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
1121 &exc_ctx_with_reasons[i],
1122 stop_on_continue,
1123 stop_on_error,
1124 echo_commands,
1125 print_results,
1126 result);
1127
1128 // If the command started the target going again, we should bag out of
1129 // running the stop hooks.
1130 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
1131 || (result.GetStatus() == eReturnStatusSuccessContinuingResult))
1132 {
1133 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1134 keep_going = false;
1135 }
1136 }
1137 }
1138 }
1139 if (hooks_ran)
1140 result.AppendMessage ("\n** End Stop Hooks **\n");
1141}
1142
1143//--------------------------------------------------------------
1144// class Target::StopHook
1145//--------------------------------------------------------------
1146
1147
1148Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1149 UserID (uid),
1150 m_target_sp (target_sp),
1151 m_active (true),
1152 m_commands (),
1153 m_specifier_sp (),
1154 m_thread_spec_ap(NULL)
1155{
1156}
1157
1158Target::StopHook::StopHook (const StopHook &rhs) :
1159 UserID (rhs.GetID()),
1160 m_target_sp (rhs.m_target_sp),
1161 m_commands (rhs.m_commands),
1162 m_specifier_sp (rhs.m_specifier_sp),
1163 m_active (rhs.m_active),
1164 m_thread_spec_ap (NULL)
1165{
1166 if (rhs.m_thread_spec_ap.get() != NULL)
1167 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1168}
1169
1170
1171Target::StopHook::~StopHook ()
1172{
1173}
1174
1175void
1176Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1177{
1178 m_thread_spec_ap.reset (specifier);
1179}
1180
1181
1182void
1183Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1184{
1185 int indent_level = s->GetIndentLevel();
1186
1187 s->SetIndentLevel(indent_level + 2);
1188
1189 s->Printf ("Hook: %d\n", GetID());
1190 if (m_active)
1191 s->Indent ("State: enabled\n");
1192 else
1193 s->Indent ("State: disabled\n");
1194
1195 if (m_specifier_sp)
1196 {
1197 s->Indent();
1198 s->PutCString ("Specifier:\n");
1199 s->SetIndentLevel (indent_level + 4);
1200 m_specifier_sp->GetDescription (s, level);
1201 s->SetIndentLevel (indent_level + 2);
1202 }
1203
1204 if (m_thread_spec_ap.get() != NULL)
1205 {
1206 StreamString tmp;
1207 s->Indent("Thread:\n");
1208 m_thread_spec_ap->GetDescription (&tmp, level);
1209 s->SetIndentLevel (indent_level + 4);
1210 s->Indent (tmp.GetData());
1211 s->PutCString ("\n");
1212 s->SetIndentLevel (indent_level + 2);
1213 }
1214
1215 s->Indent ("Commands: \n");
1216 s->SetIndentLevel (indent_level + 4);
1217 uint32_t num_commands = m_commands.GetSize();
1218 for (uint32_t i = 0; i < num_commands; i++)
1219 {
1220 s->Indent(m_commands.GetStringAtIndex(i));
1221 s->PutCString ("\n");
1222 }
1223 s->SetIndentLevel (indent_level);
1224}
1225
1226
Caroline Tice5bc8c972010-09-20 20:44:43 +00001227//--------------------------------------------------------------
1228// class Target::SettingsController
1229//--------------------------------------------------------------
1230
1231Target::SettingsController::SettingsController () :
1232 UserSettingsController ("target", Debugger::GetSettingsController()),
1233 m_default_architecture ()
1234{
1235 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1236 InstanceSettings::GetDefaultName().AsCString()));
1237}
1238
1239Target::SettingsController::~SettingsController ()
1240{
1241}
1242
1243lldb::InstanceSettingsSP
1244Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1245{
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001246 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1247 false,
1248 instance_name);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001249 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1250 return new_settings_sp;
1251}
1252
Caroline Tice5bc8c972010-09-20 20:44:43 +00001253
Greg Claytond284b662011-02-18 01:44:25 +00001254#define TSC_DEFAULT_ARCH "default-arch"
1255#define TSC_EXPR_PREFIX "expr-prefix"
1256#define TSC_EXEC_LEVEL "execution-level"
1257#define TSC_EXEC_MODE "execution-mode"
1258#define TSC_EXEC_OS_TYPE "execution-os-type"
1259
1260
1261static const ConstString &
1262GetSettingNameForDefaultArch ()
1263{
1264 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1265
1266 return g_const_string;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001267}
1268
Greg Claytond284b662011-02-18 01:44:25 +00001269static const ConstString &
1270GetSettingNameForExpressionPrefix ()
1271{
1272 static ConstString g_const_string (TSC_EXPR_PREFIX);
1273 return g_const_string;
1274}
1275
1276static const ConstString &
1277GetSettingNameForExecutionLevel ()
1278{
1279 static ConstString g_const_string (TSC_EXEC_LEVEL);
1280 return g_const_string;
1281}
1282
1283static const ConstString &
1284GetSettingNameForExecutionMode ()
1285{
1286 static ConstString g_const_string (TSC_EXEC_MODE);
1287 return g_const_string;
1288}
1289
1290static const ConstString &
1291GetSettingNameForExecutionOSType ()
1292{
1293 static ConstString g_const_string (TSC_EXEC_OS_TYPE);
1294 return g_const_string;
1295}
1296
1297
Caroline Tice5bc8c972010-09-20 20:44:43 +00001298bool
1299Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1300 const char *index_value,
1301 const char *value,
1302 const SettingEntry &entry,
1303 const lldb::VarSetOperationType op,
1304 Error&err)
1305{
Greg Claytond284b662011-02-18 01:44:25 +00001306 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001307 {
Greg Clayton940b1032011-02-23 00:35:02 +00001308 m_default_architecture.SetTriple (value);
1309 if (!m_default_architecture.IsValid())
1310 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Tice5bc8c972010-09-20 20:44:43 +00001311 }
1312 return true;
1313}
1314
1315
1316bool
1317Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1318 StringList &value,
1319 Error &err)
1320{
Greg Claytond284b662011-02-18 01:44:25 +00001321 if (var_name == GetSettingNameForDefaultArch())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001322 {
Greg Claytonbf6e2102010-10-27 02:06:37 +00001323 // If the arch is invalid (the default), don't show a string for it
1324 if (m_default_architecture.IsValid())
Greg Clayton940b1032011-02-23 00:35:02 +00001325 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Tice5bc8c972010-09-20 20:44:43 +00001326 return true;
1327 }
1328 else
1329 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1330
1331 return false;
1332}
1333
1334//--------------------------------------------------------------
1335// class TargetInstanceSettings
1336//--------------------------------------------------------------
1337
Greg Clayton638351a2010-12-04 00:10:17 +00001338TargetInstanceSettings::TargetInstanceSettings
1339(
1340 UserSettingsController &owner,
1341 bool live_instance,
1342 const char *name
1343) :
Greg Claytond284b662011-02-18 01:44:25 +00001344 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1345 m_expr_prefix_path (),
1346 m_expr_prefix_contents (),
1347 m_execution_level (eExecutionLevelAuto),
1348 m_execution_mode (eExecutionModeAuto),
1349 m_execution_os_type (eExecutionOSTypeAuto)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001350{
1351 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1352 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1353 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1354 // This is true for CreateInstanceName() too.
1355
1356 if (GetInstanceName () == InstanceSettings::InvalidName())
1357 {
1358 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1359 m_owner.RegisterInstanceSettings (this);
1360 }
1361
1362 if (live_instance)
1363 {
1364 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1365 CopyInstanceSettings (pending_settings,false);
1366 //m_owner.RemovePendingSettings (m_instance_name);
1367 }
1368}
1369
1370TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytonc0c1b0c2010-11-19 03:46:01 +00001371 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Tice5bc8c972010-09-20 20:44:43 +00001372{
1373 if (m_instance_name != InstanceSettings::GetDefaultName())
1374 {
1375 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1376 CopyInstanceSettings (pending_settings,false);
1377 //m_owner.RemovePendingSettings (m_instance_name);
1378 }
1379}
1380
1381TargetInstanceSettings::~TargetInstanceSettings ()
1382{
1383}
1384
1385TargetInstanceSettings&
1386TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1387{
1388 if (this != &rhs)
1389 {
1390 }
1391
1392 return *this;
1393}
1394
Caroline Tice5bc8c972010-09-20 20:44:43 +00001395void
1396TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1397 const char *index_value,
1398 const char *value,
1399 const ConstString &instance_name,
1400 const SettingEntry &entry,
1401 lldb::VarSetOperationType op,
1402 Error &err,
1403 bool pending)
1404{
Greg Claytond284b662011-02-18 01:44:25 +00001405 int new_enum = -1;
1406
1407 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001408 {
1409 switch (op)
1410 {
1411 default:
1412 err.SetErrorToGenericError ();
1413 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1414 return;
1415 case lldb::eVarSetOperationAssign:
1416 {
1417 FileSpec file_spec(value, true);
1418
1419 if (!file_spec.Exists())
1420 {
1421 err.SetErrorToGenericError ();
1422 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1423 return;
1424 }
1425
Greg Clayton9f527042011-02-03 17:47:47 +00001426 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan77e93942010-10-29 00:29:03 +00001427
Greg Clayton9f527042011-02-03 17:47:47 +00001428 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan77e93942010-10-29 00:29:03 +00001429 {
1430 err.SetErrorToGenericError ();
Greg Clayton9f527042011-02-03 17:47:47 +00001431 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan77e93942010-10-29 00:29:03 +00001432 return;
1433 }
1434
1435 m_expr_prefix_path = value;
Greg Clayton9f527042011-02-03 17:47:47 +00001436 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan77e93942010-10-29 00:29:03 +00001437 }
1438 return;
1439 case lldb::eVarSetOperationAppend:
1440 err.SetErrorToGenericError ();
1441 err.SetErrorString ("Cannot append to a path.\n");
1442 return;
1443 case lldb::eVarSetOperationClear:
1444 m_expr_prefix_path.clear ();
1445 m_expr_prefix_contents.clear ();
1446 return;
1447 }
1448 }
Greg Claytond284b662011-02-18 01:44:25 +00001449 else if (var_name == GetSettingNameForExecutionLevel ())
1450 {
1451 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1452 if (err.Success())
1453 m_execution_level = (ExecutionLevel)new_enum;
1454 }
1455 else if (var_name == GetSettingNameForExecutionMode ())
1456 {
1457 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1458 if (err.Success())
1459 m_execution_mode = (ExecutionMode)new_enum;
1460 }
1461 else if (var_name == GetSettingNameForExecutionOSType ())
1462 {
1463 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1464 if (err.Success())
1465 m_execution_os_type = (ExecutionOSType)new_enum;
1466 }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001467}
1468
1469void
Greg Claytond284b662011-02-18 01:44:25 +00001470TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001471{
Sean Callanan77e93942010-10-29 00:29:03 +00001472 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1473
1474 if (!new_settings_ptr)
1475 return;
1476
Greg Claytond284b662011-02-18 01:44:25 +00001477 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1478 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
1479 m_execution_level = new_settings_ptr->m_execution_level;
1480 m_execution_mode = new_settings_ptr->m_execution_mode;
1481 m_execution_os_type = new_settings_ptr->m_execution_os_type;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001482}
1483
Caroline Ticebcb5b452010-09-20 21:37:42 +00001484bool
Caroline Tice5bc8c972010-09-20 20:44:43 +00001485TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1486 const ConstString &var_name,
1487 StringList &value,
Caroline Ticebcb5b452010-09-20 21:37:42 +00001488 Error *err)
Caroline Tice5bc8c972010-09-20 20:44:43 +00001489{
Greg Claytond284b662011-02-18 01:44:25 +00001490 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan77e93942010-10-29 00:29:03 +00001491 {
1492 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1493 }
Greg Claytond284b662011-02-18 01:44:25 +00001494 else if (var_name == GetSettingNameForExecutionLevel ())
1495 {
1496 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_level));
1497 }
1498 else if (var_name == GetSettingNameForExecutionMode ())
1499 {
1500 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_mode));
1501 }
1502 else if (var_name == GetSettingNameForExecutionOSType ())
1503 {
1504 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_os_type));
1505 }
Sean Callanan77e93942010-10-29 00:29:03 +00001506 else
1507 {
1508 if (err)
1509 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1510 return false;
1511 }
1512
1513 return true;
Caroline Tice5bc8c972010-09-20 20:44:43 +00001514}
1515
1516const ConstString
1517TargetInstanceSettings::CreateInstanceName ()
1518{
Caroline Tice5bc8c972010-09-20 20:44:43 +00001519 StreamString sstr;
Caroline Tice1ebef442010-09-27 00:30:10 +00001520 static int instance_count = 1;
1521
Caroline Tice5bc8c972010-09-20 20:44:43 +00001522 sstr.Printf ("target_%d", instance_count);
1523 ++instance_count;
1524
1525 const ConstString ret_val (sstr.GetData());
1526 return ret_val;
1527}
1528
1529//--------------------------------------------------
1530// Target::SettingsController Variable Tables
1531//--------------------------------------------------
1532
1533SettingEntry
1534Target::SettingsController::global_settings_table[] =
1535{
Greg Claytond284b662011-02-18 01:44:25 +00001536 // var-name var-type default enum init'd hidden help-text
1537 // ================= ================== =========== ==== ====== ====== =========================================================================
1538 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1539 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1540};
1541
1542static lldb::OptionEnumValueElement
1543g_execution_level_enums[] =
1544{
1545 { eExecutionLevelAuto , "auto" , "Automatically detect the execution level (user/kernel)." },
1546 { eExecutionLevelKernel , "kernel" , "Treat executables as kernel executables." },
1547 { eExecutionLevelUser , "user" , "Treat executables as user space executables." },
1548 { 0 , NULL , NULL }
1549};
1550
1551static lldb::OptionEnumValueElement
1552g_execution_mode_enums[] =
1553{
1554 { eExecutionModeAuto , "auto" , "Automatically detect the execution mode (static/dynamic)." },
1555 { eExecutionModeStatic , "static" , "All executables are static and addresses at the virtual addresses in the object files." },
1556 { eExecutionModeDynamic , "dynamic" , "Executables and shared libraries are dynamically loaded.." },
1557 { 0 , NULL , NULL }
1558};
1559
1560static lldb::OptionEnumValueElement
1561g_execution_os_enums[] =
1562{
1563 { eExecutionOSTypeAuto , "auto" , "Automatically detect the execution OS (none/halted/live)." },
1564 { eExecutionOSTypeNone , "none" , "There is no operating system available (no processes or threads)." },
1565 { eExecutionOSTypeHalted, "halted" , "There is an operating system, but it is halted when the debugger is halted. "
1566 "Processes and threads must be discovered by accessing symbols and reading "
1567 "memory." },
1568 { eExecutionOSTypeLive , "live" , "There is a live operating system with debug services that can be used to "
1569 "get processes, threads and theirs states." },
1570 { 0, NULL, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001571};
1572
1573SettingEntry
1574Target::SettingsController::instance_settings_table[] =
1575{
Greg Claytond284b662011-02-18 01:44:25 +00001576 // var-name var-type default enum-table init'd hidden help-text
1577 // ================= ================== =========== ========================= ====== ====== =========================================================================
1578 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL , false, false, "Path to a file containing expressions to be prepended to all expressions." },
1579 { TSC_EXEC_LEVEL , eSetVarTypeEnum , "auto" , g_execution_level_enums , false, false, "Sets the execution level for a target." },
1580 { TSC_EXEC_MODE , eSetVarTypeEnum , "auto" , g_execution_mode_enums , false, false, "Sets the execution mode for a target." },
1581 { TSC_EXEC_OS_TYPE , eSetVarTypeEnum , "auto" , g_execution_os_enums , false, false, "Sets the execution OS for a target." },
1582 { NULL , eSetVarTypeNone , NULL , NULL , false, false, NULL }
Caroline Tice5bc8c972010-09-20 20:44:43 +00001583};