blob: 0b734c5344658831927cef6fe113e32968d36f65 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
19#include "lldb/Breakpoint/BreakpointResolverName.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5920#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:2421#include "lldb/Core/Event.h"
22#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:2423#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5924#include "lldb/Core/Timer.h"
25#include "lldb/Core/ValueObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2426#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:5927#include "lldb/Interpreter/CommandInterpreter.h"
28#include "lldb/Interpreter/CommandReturnObject.h"
Chris Lattner30fdc8d2010-06-08 16:52:2429#include "lldb/lldb-private-log.h"
30#include "lldb/Symbol/ObjectFile.h"
31#include "lldb/Target/Process.h"
Greg Clayton8b2fe6d2010-12-14 02:59:5932#include "lldb/Target/StackFrame.h"
Jim Ingham9575d842011-03-11 03:53:5933#include "lldb/Target/Thread.h"
34#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:2435
36using namespace lldb;
37using namespace lldb_private;
38
39//----------------------------------------------------------------------
40// Target constructor
41//----------------------------------------------------------------------
Greg Claytonded470d2011-03-19 01:12:2142Target::Target(Debugger &debugger, const lldb::PlatformSP &platform_sp) :
Greg Claytoncfd1ace2010-10-31 03:01:0643 Broadcaster("lldb.target"),
Greg Claytonded470d2011-03-19 01:12:2144 m_platform_sp (platform_sp),
Greg Claytondbe54502010-11-19 03:46:0145 TargetInstanceSettings (*GetSettingsController()),
Greg Clayton66111032010-06-23 01:19:2946 m_debugger (debugger),
Greg Claytonaf67cec2010-12-20 20:49:2347 m_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:2448 m_images(),
Greg Claytonf5e56de2010-09-14 23:36:4049 m_section_load_list (),
Chris Lattner30fdc8d2010-06-08 16:52:2450 m_breakpoint_list (false),
51 m_internal_breakpoint_list (true),
52 m_process_sp(),
Chris Lattner30fdc8d2010-06-08 16:52:2453 m_search_filter_sp(),
54 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Clayton8b2fe6d2010-12-14 02:59:5955 m_scratch_ast_context_ap (NULL),
Jim Ingham9575d842011-03-11 03:53:5956 m_persistent_variables (),
57 m_stop_hook_next_id(0)
Chris Lattner30fdc8d2010-06-08 16:52:2458{
Greg Claytoncfd1ace2010-10-31 03:01:0659 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
60 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
61 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
62
Greg Clayton2d4edfb2010-11-06 01:53:3063 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2464 if (log)
65 log->Printf ("%p Target::Target()", this);
66}
67
68//----------------------------------------------------------------------
69// Destructor
70//----------------------------------------------------------------------
71Target::~Target()
72{
Greg Clayton2d4edfb2010-11-06 01:53:3073 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:2474 if (log)
75 log->Printf ("%p Target::~Target()", this);
76 DeleteCurrentProcess ();
77}
78
79void
Caroline Ticeceb6b132010-10-26 03:11:1380Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:2481{
Greg Clayton89411422010-10-08 00:21:0582// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:1383 if (description_level != lldb::eDescriptionLevelBrief)
84 {
85 s->Indent();
86 s->PutCString("Target\n");
87 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:3588 m_images.Dump(s);
89 m_breakpoint_list.Dump(s);
90 m_internal_breakpoint_list.Dump(s);
91 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:1392 }
93 else
94 {
Greg Clayton48381312010-10-30 04:51:4695 s->PutCString (GetExecutableModule()->GetFileSpec().GetFilename().GetCString());
Caroline Ticeceb6b132010-10-26 03:11:1396 }
Chris Lattner30fdc8d2010-06-08 16:52:2497}
98
99void
100Target::DeleteCurrentProcess ()
101{
102 if (m_process_sp.get())
103 {
Greg Clayton17f69202010-09-14 23:52:43104 m_section_load_list.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24105 if (m_process_sp->IsAlive())
106 m_process_sp->Destroy();
Jim Inghamd0a3e12b2011-02-16 17:54:55107
108 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24109
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 Clayton66111032010-06-23 01:19:29136 return m_debugger.GetTargetList().GetTargetSP(this);
Chris Lattner30fdc8d2010-06-08 16:52:24137}
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 Clayton1b72fcb2010-08-24 00:45:41180Target::CreateBreakpoint (lldb::addr_t addr, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24181{
Chris Lattner30fdc8d2010-06-08 16:52:24182 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 Clayton1b72fcb2010-08-24 00:45:41186 // Try and resolve as a load address if possible
Greg Claytonf5e56de2010-09-14 23:36:40187 m_section_load_list.ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41188 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 Lattner30fdc8d2010-06-08 16:52:24194 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 Clayton0c5cd902010-06-28 21:30:43207Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal)
Chris Lattner30fdc8d2010-06-08 16:52:24208{
Greg Clayton0c5cd902010-06-28 21:30:43209 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 Lattner30fdc8d2010-06-08 16:52:24217}
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 Clayton9fed0d82010-07-23 23:33:17259 m_internal_breakpoint_list.Add (bp_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24260 else
Greg Clayton9fed0d82010-07-23 23:33:17261 m_breakpoint_list.Add (bp_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24262
Greg Clayton2d4edfb2010-11-06 01:53:30263 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24264 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 Lattner30fdc8d2010-06-08 16:52:24271 bp_sp->ResolveBreakpoint();
272 }
Jim Ingham36f3b362010-10-14 23:45:03273
274 if (!internal && bp_sp)
275 {
276 m_last_created_breakpoint = bp_sp;
277 }
278
Chris Lattner30fdc8d2010-06-08 16:52:24279 return bp_sp;
280}
281
282void
283Target::RemoveAllBreakpoints (bool internal_also)
284{
Greg Clayton2d4edfb2010-11-06 01:53:30285 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24286 if (log)
287 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
288
Greg Clayton9fed0d82010-07-23 23:33:17289 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24290 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17291 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03292
293 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24294}
295
296void
297Target::DisableAllBreakpoints (bool internal_also)
298{
Greg Clayton2d4edfb2010-11-06 01:53:30299 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24300 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 Clayton2d4edfb2010-11-06 01:53:30311 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24312 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 Clayton2d4edfb2010-11-06 01:53:30323 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24324 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 Clayton9fed0d82010-07-23 23:33:17330 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24331 else
Jim Ingham36f3b362010-10-14 23:45:03332 {
Greg Claytonaa1c5872011-01-24 23:35:47333 if (m_last_created_breakpoint)
334 {
335 if (m_last_created_breakpoint->GetID() == break_id)
336 m_last_created_breakpoint.reset();
337 }
Greg Clayton9fed0d82010-07-23 23:33:17338 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03339 }
Chris Lattner30fdc8d2010-06-08 16:52:24340 return true;
341 }
342 return false;
343}
344
345bool
346Target::DisableBreakpointByID (break_id_t break_id)
347{
Greg Clayton2d4edfb2010-11-06 01:53:30348 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24349 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 Clayton2d4edfb2010-11-06 01:53:30369 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24370 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 Ingham5aee1622010-08-09 23:31:02416 // 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 Lattner30fdc8d2010-06-08 16:52:24420 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15421 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24422
423 if (executable_objfile)
424 {
425 executable_objfile->GetDependentModules(dependent_files);
426 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
427 {
Greg Claytonded470d2011-03-19 01:12:21428 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
429 FileSpec platform_dependent_file_spec;
430 if (m_platform_sp)
Greg Claytond314e812011-03-23 00:09:55431 m_platform_sp->GetFile (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21432 else
433 platform_dependent_file_spec = dependent_file_spec;
434
435 ModuleSP image_module_sp(GetSharedModule (platform_dependent_file_spec,
436 exe_arch));
Chris Lattner30fdc8d2010-06-08 16:52:24437 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 Clayton514487e2011-02-15 21:59:32448 if (m_arch_spec.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24449 {
Greg Clayton514487e2011-02-15 21:59:32450 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch_spec.GetTriple().str().c_str()));
Chris Lattner30fdc8d2010-06-08 16:52:24451 }
452 }
Caroline Tice1559a462010-09-27 00:30:10453
454 UpdateInstanceName();
Chris Lattner30fdc8d2010-06-08 16:52:24455}
456
457
Jim Ingham5aee1622010-08-09 23:31:02458bool
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 Ingham5aee1622010-08-09 23:31:02480 // 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 Lattner30fdc8d2010-06-08 16:52:24510
Chris Lattner30fdc8d2010-06-08 16:52:24511void
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 Claytona4d78302010-12-06 23:51:26544
545 // Remove the images from the target image list
546 m_images.Remove(module_list);
547
Chris Lattner30fdc8d2010-06-08 16:52:24548 // TODO: make event data that packages up the module_list
549 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
550}
551
552size_t
Greg Claytondb598232011-01-07 01:57:07553Target::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 Lattner30fdc8d2010-06-08 16:52:24584{
Chris Lattner30fdc8d2010-06-08 16:52:24585 error.Clear();
Greg Claytondb598232011-01-07 01:57:07586
Greg Claytondda4f7b2010-06-30 23:03:03587 bool process_is_valid = m_process_sp && m_process_sp->IsAlive();
588
Greg Claytondb598232011-01-07 01:57:07589 size_t bytes_read = 0;
Greg Clayton357132e2011-03-26 19:14:58590 Address resolved_addr;
591 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03592 {
593 if (process_is_valid)
Greg Claytonf5e56de2010-09-14 23:36:40594 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03595 else
Greg Claytondda4f7b2010-06-30 23:03:03596 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03597 }
Greg Clayton357132e2011-03-26 19:14:58598 if (!resolved_addr.IsValid())
599 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03600
Greg Claytondb598232011-01-07 01:57:07601 if (prefer_file_cache)
602 {
603 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
604 if (bytes_read > 0)
605 return bytes_read;
606 }
Greg Claytondda4f7b2010-06-30 23:03:03607
608 if (process_is_valid)
609 {
Greg Claytonf5e56de2010-09-14 23:36:40610 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Claytondda4f7b2010-06-30 23:03:03611 if (load_addr == LLDB_INVALID_ADDRESS)
612 {
613 if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec())
614 error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n",
615 resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(),
616 resolved_addr.GetFileAddress());
617 else
618 error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress());
619 }
620 else
621 {
Greg Claytondb598232011-01-07 01:57:07622 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24623 if (bytes_read != dst_len)
624 {
625 if (error.Success())
626 {
627 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03628 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24629 else
Greg Claytondda4f7b2010-06-30 23:03:03630 error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24631 }
632 }
Greg Claytondda4f7b2010-06-30 23:03:03633 if (bytes_read)
634 return bytes_read;
635 // If the address is not section offset we have an address that
636 // doesn't resolve to any address in any currently loaded shared
637 // libaries and we failed to read memory so there isn't anything
638 // more we can do. If it is section offset, we might be able to
639 // read cached memory from the object file.
640 if (!resolved_addr.IsSectionOffset())
641 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24642 }
Chris Lattner30fdc8d2010-06-08 16:52:24643 }
Greg Claytondda4f7b2010-06-30 23:03:03644
Greg Claytondb598232011-01-07 01:57:07645 if (!prefer_file_cache)
Greg Claytondda4f7b2010-06-30 23:03:03646 {
Greg Claytondb598232011-01-07 01:57:07647 // If we didn't already try and read from the object file cache, then
648 // try it after failing to read from the process.
649 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03650 }
651 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24652}
653
654
655ModuleSP
656Target::GetSharedModule
657(
658 const FileSpec& file_spec,
659 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:10660 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:24661 const ConstString *object_name,
662 off_t object_offset,
663 Error *error_ptr
664)
665{
666 // Don't pass in the UUID so we can tell if we have a stale value in our list
667 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
668 bool did_create_module = false;
669 ModuleSP module_sp;
670
671 // If there are image search path entries, try to use them first to acquire a suitable image.
672
673 Error error;
674
675 if (m_image_search_paths.GetSize())
676 {
677 FileSpec transformed_spec;
678 if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory()))
679 {
680 transformed_spec.GetFilename() = file_spec.GetFilename();
681 error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module);
682 }
683 }
684
685 // If a module hasn't been found yet, use the unmodified path.
686
687 if (!module_sp)
688 {
689 error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module));
690 }
691
692 if (module_sp)
693 {
694 m_images.Append (module_sp);
695 if (did_create_module)
696 {
697 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
698 ModuleUpdated(old_module_sp, module_sp);
699 else
700 ModuleAdded(module_sp);
701 }
702 }
703 if (error_ptr)
704 *error_ptr = error;
705 return module_sp;
706}
707
708
709Target *
710Target::CalculateTarget ()
711{
712 return this;
713}
714
715Process *
716Target::CalculateProcess ()
717{
718 return NULL;
719}
720
721Thread *
722Target::CalculateThread ()
723{
724 return NULL;
725}
726
727StackFrame *
728Target::CalculateStackFrame ()
729{
730 return NULL;
731}
732
733void
Greg Clayton0603aa92010-10-04 01:05:56734Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24735{
736 exe_ctx.target = this;
737 exe_ctx.process = NULL; // Do NOT fill in process...
738 exe_ctx.thread = NULL;
739 exe_ctx.frame = NULL;
740}
741
742PathMappingList &
743Target::GetImageSearchPathList ()
744{
745 return m_image_search_paths;
746}
747
748void
749Target::ImageSearchPathsChanged
750(
751 const PathMappingList &path_list,
752 void *baton
753)
754{
755 Target *target = (Target *)baton;
756 if (target->m_images.GetSize() > 1)
757 {
758 ModuleSP exe_module_sp (target->GetExecutableModule());
759 if (exe_module_sp)
760 {
761 target->m_images.Clear();
762 target->SetExecutableModule (exe_module_sp, true);
763 }
764 }
765}
766
767ClangASTContext *
768Target::GetScratchClangASTContext()
769{
770 return m_scratch_ast_context_ap.get();
771}
Caroline Ticedaccaa92010-09-20 20:44:43772
Greg Clayton99d0faf2010-11-18 23:32:35773void
Caroline Tice20bd37f2011-03-10 22:14:10774Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43775{
Greg Clayton99d0faf2010-11-18 23:32:35776 UserSettingsControllerSP &usc = GetSettingsController();
777 usc.reset (new SettingsController);
778 UserSettingsController::InitializeSettingsController (usc,
779 SettingsController::global_settings_table,
780 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:10781
782 // Now call SettingsInitialize() on each 'child' setting of Target
783 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35784}
Caroline Ticedaccaa92010-09-20 20:44:43785
Greg Clayton99d0faf2010-11-18 23:32:35786void
Caroline Tice20bd37f2011-03-10 22:14:10787Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35788{
Caroline Tice20bd37f2011-03-10 22:14:10789
790 // Must call SettingsTerminate() on each settings 'child' of Target, before terminating Target's Settings.
791
792 Process::SettingsTerminate ();
793
794 // Now terminate Target Settings.
795
Greg Clayton99d0faf2010-11-18 23:32:35796 UserSettingsControllerSP &usc = GetSettingsController();
797 UserSettingsController::FinalizeSettingsController (usc);
798 usc.reset();
799}
Caroline Ticedaccaa92010-09-20 20:44:43800
Greg Clayton99d0faf2010-11-18 23:32:35801UserSettingsControllerSP &
802Target::GetSettingsController ()
803{
804 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:43805 return g_settings_controller;
806}
807
808ArchSpec
809Target::GetDefaultArchitecture ()
810{
Greg Clayton64195a22011-02-23 00:35:02811 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
812
813 if (settings_controller_sp)
814 return static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture ();
815 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43816}
817
818void
Greg Clayton64195a22011-02-23 00:35:02819Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:43820{
Greg Clayton64195a22011-02-23 00:35:02821 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
822
823 if (settings_controller_sp)
824 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:43825}
826
Greg Clayton0603aa92010-10-04 01:05:56827Target *
828Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
829{
830 // The target can either exist in the "process" of ExecutionContext, or in
831 // the "target_sp" member of SymbolContext. This accessor helper function
832 // will get the target from one of these locations.
833
834 Target *target = NULL;
835 if (sc_ptr != NULL)
836 target = sc_ptr->target_sp.get();
837 if (target == NULL)
838 {
839 if (exe_ctx_ptr != NULL && exe_ctx_ptr->process != NULL)
840 target = &exe_ctx_ptr->process->GetTarget();
841 }
842 return target;
843}
844
845
Caroline Tice1559a462010-09-27 00:30:10846void
847Target::UpdateInstanceName ()
848{
849 StreamString sstr;
850
851 ModuleSP module_sp = GetExecutableModule();
852 if (module_sp)
853 {
Greg Clayton307de252010-10-27 02:06:37854 sstr.Printf ("%s_%s",
855 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton64195a22011-02-23 00:35:02856 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytondbe54502010-11-19 03:46:01857 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
858 sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:10859 }
860}
861
Sean Callanan322f5292010-10-29 00:29:03862const char *
863Target::GetExpressionPrefixContentsAsCString ()
864{
865 return m_expr_prefix_contents.c_str();
866}
867
Greg Clayton8b2fe6d2010-12-14 02:59:59868ExecutionResults
869Target::EvaluateExpression
870(
871 const char *expr_cstr,
872 StackFrame *frame,
873 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:35874 bool keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59875 lldb::ValueObjectSP &result_valobj_sp
876)
877{
878 ExecutionResults execution_results = eExecutionSetupError;
879
880 result_valobj_sp.reset();
881
882 ExecutionContext exe_ctx;
883 if (frame)
884 {
885 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton54979cd2010-12-15 05:08:08886 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:18887 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
888 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
889 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton8b2fe6d2010-12-14 02:59:59890 }
891 else if (m_process_sp)
892 {
893 m_process_sp->CalculateExecutionContext(exe_ctx);
894 }
895 else
896 {
897 CalculateExecutionContext(exe_ctx);
898 }
899
900 if (result_valobj_sp)
901 {
902 execution_results = eExecutionCompleted;
903 // We got a result from the frame variable expression path above...
904 ConstString persistent_variable_name (m_persistent_variables.GetNextPersistentVariableName());
905
906 lldb::ValueObjectSP const_valobj_sp;
907
908 // Check in case our value is already a constant value
909 if (result_valobj_sp->GetIsConstant())
910 {
911 const_valobj_sp = result_valobj_sp;
912 const_valobj_sp->SetName (persistent_variable_name);
913 }
914 else
915 const_valobj_sp = result_valobj_sp->CreateConstantValue (exe_ctx.GetBestExecutionContextScope(),
916 persistent_variable_name);
917
Sean Callanan92adcac2011-01-13 08:53:35918 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
919
Greg Clayton8b2fe6d2010-12-14 02:59:59920 result_valobj_sp = const_valobj_sp;
921
Sean Callanan92adcac2011-01-13 08:53:35922 ClangExpressionVariableSP clang_expr_variable_sp(m_persistent_variables.CreatePersistentVariable(result_valobj_sp));
923 assert (clang_expr_variable_sp.get());
924
925 // Set flags and live data as appropriate
926
927 const Value &result_value = live_valobj_sp->GetValue();
928
929 switch (result_value.GetValueType())
930 {
931 case Value::eValueTypeHostAddress:
932 case Value::eValueTypeFileAddress:
933 // we don't do anything with these for now
934 break;
935 case Value::eValueTypeScalar:
936 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
937 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
938 break;
939 case Value::eValueTypeLoadAddress:
940 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
941 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
942 break;
943 }
Greg Clayton8b2fe6d2010-12-14 02:59:59944 }
945 else
946 {
947 // Make sure we aren't just trying to see the value of a persistent
948 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:35949 lldb::ClangExpressionVariableSP persistent_var_sp;
950 // Only check for persistent variables the expression starts with a '$'
951 if (expr_cstr[0] == '$')
952 persistent_var_sp = m_persistent_variables.GetVariable (expr_cstr);
953
Greg Clayton8b2fe6d2010-12-14 02:59:59954 if (persistent_var_sp)
955 {
956 result_valobj_sp = persistent_var_sp->GetValueObject ();
957 execution_results = eExecutionCompleted;
958 }
959 else
960 {
961 const char *prefix = GetExpressionPrefixContentsAsCString();
962
963 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Sean Callanan92adcac2011-01-13 08:53:35964 unwind_on_error,
965 keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59966 expr_cstr,
967 prefix,
968 result_valobj_sp);
969 }
970 }
971 return execution_results;
972}
973
Jim Ingham9575d842011-03-11 03:53:59974lldb::user_id_t
975Target::AddStopHook (Target::StopHookSP &new_hook_sp)
976{
977 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
978 new_hook_sp.reset (new StopHook(GetSP(), new_uid));
979 m_stop_hooks[new_uid] = new_hook_sp;
980 return new_uid;
981}
982
983bool
984Target::RemoveStopHookByID (lldb::user_id_t user_id)
985{
986 size_t num_removed;
987 num_removed = m_stop_hooks.erase (user_id);
988 if (num_removed == 0)
989 return false;
990 else
991 return true;
992}
993
994void
995Target::RemoveAllStopHooks ()
996{
997 m_stop_hooks.clear();
998}
999
1000Target::StopHookSP
1001Target::GetStopHookByID (lldb::user_id_t user_id)
1002{
1003 StopHookSP found_hook;
1004
1005 StopHookCollection::iterator specified_hook_iter;
1006 specified_hook_iter = m_stop_hooks.find (user_id);
1007 if (specified_hook_iter != m_stop_hooks.end())
1008 found_hook = (*specified_hook_iter).second;
1009 return found_hook;
1010}
1011
1012bool
1013Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
1014{
1015 StopHookCollection::iterator specified_hook_iter;
1016 specified_hook_iter = m_stop_hooks.find (user_id);
1017 if (specified_hook_iter == m_stop_hooks.end())
1018 return false;
1019
1020 (*specified_hook_iter).second->SetIsActive (active_state);
1021 return true;
1022}
1023
1024void
1025Target::SetAllStopHooksActiveState (bool active_state)
1026{
1027 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1028 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1029 {
1030 (*pos).second->SetIsActive (active_state);
1031 }
1032}
1033
1034void
1035Target::RunStopHooks ()
1036{
1037 if (!m_process_sp)
1038 return;
1039
1040 if (m_stop_hooks.empty())
1041 return;
1042
1043 StopHookCollection::iterator pos, end = m_stop_hooks.end();
1044
1045 // If there aren't any active stop hooks, don't bother either:
1046 bool any_active_hooks = false;
1047 for (pos = m_stop_hooks.begin(); pos != end; pos++)
1048 {
1049 if ((*pos).second->IsActive())
1050 {
1051 any_active_hooks = true;
1052 break;
1053 }
1054 }
1055 if (!any_active_hooks)
1056 return;
1057
1058 CommandReturnObject result;
1059
1060 std::vector<ExecutionContext> exc_ctx_with_reasons;
1061 std::vector<SymbolContext> sym_ctx_with_reasons;
1062
1063 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
1064 size_t num_threads = cur_threadlist.GetSize();
1065 for (size_t i = 0; i < num_threads; i++)
1066 {
1067 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
1068 if (cur_thread_sp->ThreadStoppedForAReason())
1069 {
1070 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
1071 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
1072 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
1073 }
1074 }
1075
1076 // If no threads stopped for a reason, don't run the stop-hooks.
1077 size_t num_exe_ctx = exc_ctx_with_reasons.size();
1078 if (num_exe_ctx == 0)
1079 return;
1080
1081 result.SetImmediateOutputFile (m_debugger.GetOutputFile().GetStream());
1082 result.SetImmediateErrorFile (m_debugger.GetErrorFile().GetStream());
1083
1084 bool keep_going = true;
1085 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:271086 bool print_hook_header;
1087 bool print_thread_header;
1088
1089 if (num_exe_ctx == 1)
1090 print_thread_header = false;
1091 else
1092 print_thread_header = true;
1093
1094 if (m_stop_hooks.size() == 1)
1095 print_hook_header = false;
1096 else
1097 print_hook_header = true;
1098
Jim Ingham9575d842011-03-11 03:53:591099 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1100 {
1101 // result.Clear();
1102 StopHookSP cur_hook_sp = (*pos).second;
1103 if (!cur_hook_sp->IsActive())
1104 continue;
1105
1106 bool any_thread_matched = false;
1107 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1108 {
1109 if ((cur_hook_sp->GetSpecifier () == NULL
1110 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1111 && (cur_hook_sp->GetThreadSpecifier() == NULL
1112 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1113 {
1114 if (!hooks_ran)
1115 {
Jim Ingham381e25b2011-03-22 01:47:271116 result.AppendMessage("\n** Stop Hooks **");
Jim Ingham9575d842011-03-11 03:53:591117 hooks_ran = true;
1118 }
Jim Ingham381e25b2011-03-22 01:47:271119 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591120 {
1121 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1122 any_thread_matched = true;
1123 }
1124
Jim Ingham381e25b2011-03-22 01:47:271125 if (print_thread_header)
1126 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591127
1128 bool stop_on_continue = true;
1129 bool stop_on_error = true;
1130 bool echo_commands = false;
1131 bool print_results = true;
1132 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
1133 &exc_ctx_with_reasons[i],
1134 stop_on_continue,
1135 stop_on_error,
1136 echo_commands,
1137 print_results,
1138 result);
1139
1140 // If the command started the target going again, we should bag out of
1141 // running the stop hooks.
1142 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
1143 || (result.GetStatus() == eReturnStatusSuccessContinuingResult))
1144 {
1145 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1146 keep_going = false;
1147 }
1148 }
1149 }
1150 }
1151 if (hooks_ran)
1152 result.AppendMessage ("\n** End Stop Hooks **\n");
1153}
1154
1155//--------------------------------------------------------------
1156// class Target::StopHook
1157//--------------------------------------------------------------
1158
1159
1160Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1161 UserID (uid),
1162 m_target_sp (target_sp),
1163 m_active (true),
1164 m_commands (),
1165 m_specifier_sp (),
1166 m_thread_spec_ap(NULL)
1167{
1168}
1169
1170Target::StopHook::StopHook (const StopHook &rhs) :
1171 UserID (rhs.GetID()),
1172 m_target_sp (rhs.m_target_sp),
1173 m_commands (rhs.m_commands),
1174 m_specifier_sp (rhs.m_specifier_sp),
1175 m_active (rhs.m_active),
1176 m_thread_spec_ap (NULL)
1177{
1178 if (rhs.m_thread_spec_ap.get() != NULL)
1179 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1180}
1181
1182
1183Target::StopHook::~StopHook ()
1184{
1185}
1186
1187void
1188Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1189{
1190 m_thread_spec_ap.reset (specifier);
1191}
1192
1193
1194void
1195Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1196{
1197 int indent_level = s->GetIndentLevel();
1198
1199 s->SetIndentLevel(indent_level + 2);
1200
1201 s->Printf ("Hook: %d\n", GetID());
1202 if (m_active)
1203 s->Indent ("State: enabled\n");
1204 else
1205 s->Indent ("State: disabled\n");
1206
1207 if (m_specifier_sp)
1208 {
1209 s->Indent();
1210 s->PutCString ("Specifier:\n");
1211 s->SetIndentLevel (indent_level + 4);
1212 m_specifier_sp->GetDescription (s, level);
1213 s->SetIndentLevel (indent_level + 2);
1214 }
1215
1216 if (m_thread_spec_ap.get() != NULL)
1217 {
1218 StreamString tmp;
1219 s->Indent("Thread:\n");
1220 m_thread_spec_ap->GetDescription (&tmp, level);
1221 s->SetIndentLevel (indent_level + 4);
1222 s->Indent (tmp.GetData());
1223 s->PutCString ("\n");
1224 s->SetIndentLevel (indent_level + 2);
1225 }
1226
1227 s->Indent ("Commands: \n");
1228 s->SetIndentLevel (indent_level + 4);
1229 uint32_t num_commands = m_commands.GetSize();
1230 for (uint32_t i = 0; i < num_commands; i++)
1231 {
1232 s->Indent(m_commands.GetStringAtIndex(i));
1233 s->PutCString ("\n");
1234 }
1235 s->SetIndentLevel (indent_level);
1236}
1237
1238
Caroline Ticedaccaa92010-09-20 20:44:431239//--------------------------------------------------------------
1240// class Target::SettingsController
1241//--------------------------------------------------------------
1242
1243Target::SettingsController::SettingsController () :
1244 UserSettingsController ("target", Debugger::GetSettingsController()),
1245 m_default_architecture ()
1246{
1247 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1248 InstanceSettings::GetDefaultName().AsCString()));
1249}
1250
1251Target::SettingsController::~SettingsController ()
1252{
1253}
1254
1255lldb::InstanceSettingsSP
1256Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1257{
Greg Claytondbe54502010-11-19 03:46:011258 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1259 false,
1260 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431261 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1262 return new_settings_sp;
1263}
1264
Caroline Ticedaccaa92010-09-20 20:44:431265
Greg Claytonbfe5f3b2011-02-18 01:44:251266#define TSC_DEFAULT_ARCH "default-arch"
1267#define TSC_EXPR_PREFIX "expr-prefix"
1268#define TSC_EXEC_LEVEL "execution-level"
1269#define TSC_EXEC_MODE "execution-mode"
1270#define TSC_EXEC_OS_TYPE "execution-os-type"
1271
1272
1273static const ConstString &
1274GetSettingNameForDefaultArch ()
1275{
1276 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1277
1278 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431279}
1280
Greg Claytonbfe5f3b2011-02-18 01:44:251281static const ConstString &
1282GetSettingNameForExpressionPrefix ()
1283{
1284 static ConstString g_const_string (TSC_EXPR_PREFIX);
1285 return g_const_string;
1286}
1287
1288static const ConstString &
1289GetSettingNameForExecutionLevel ()
1290{
1291 static ConstString g_const_string (TSC_EXEC_LEVEL);
1292 return g_const_string;
1293}
1294
1295static const ConstString &
1296GetSettingNameForExecutionMode ()
1297{
1298 static ConstString g_const_string (TSC_EXEC_MODE);
1299 return g_const_string;
1300}
1301
1302static const ConstString &
1303GetSettingNameForExecutionOSType ()
1304{
1305 static ConstString g_const_string (TSC_EXEC_OS_TYPE);
1306 return g_const_string;
1307}
1308
1309
Caroline Ticedaccaa92010-09-20 20:44:431310bool
1311Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1312 const char *index_value,
1313 const char *value,
1314 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541315 const VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431316 Error&err)
1317{
Greg Claytonbfe5f3b2011-02-18 01:44:251318 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431319 {
Greg Clayton64195a22011-02-23 00:35:021320 m_default_architecture.SetTriple (value);
1321 if (!m_default_architecture.IsValid())
1322 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431323 }
1324 return true;
1325}
1326
1327
1328bool
1329Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1330 StringList &value,
1331 Error &err)
1332{
Greg Claytonbfe5f3b2011-02-18 01:44:251333 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431334 {
Greg Clayton307de252010-10-27 02:06:371335 // If the arch is invalid (the default), don't show a string for it
1336 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021337 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431338 return true;
1339 }
1340 else
1341 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1342
1343 return false;
1344}
1345
1346//--------------------------------------------------------------
1347// class TargetInstanceSettings
1348//--------------------------------------------------------------
1349
Greg Clayton85851dd2010-12-04 00:10:171350TargetInstanceSettings::TargetInstanceSettings
1351(
1352 UserSettingsController &owner,
1353 bool live_instance,
1354 const char *name
1355) :
Greg Claytonbfe5f3b2011-02-18 01:44:251356 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1357 m_expr_prefix_path (),
Greg Claytone0d378b2011-03-24 21:19:541358 m_expr_prefix_contents ()
Caroline Ticedaccaa92010-09-20 20:44:431359{
1360 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1361 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1362 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1363 // This is true for CreateInstanceName() too.
1364
1365 if (GetInstanceName () == InstanceSettings::InvalidName())
1366 {
1367 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1368 m_owner.RegisterInstanceSettings (this);
1369 }
1370
1371 if (live_instance)
1372 {
1373 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1374 CopyInstanceSettings (pending_settings,false);
1375 //m_owner.RemovePendingSettings (m_instance_name);
1376 }
1377}
1378
1379TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytondbe54502010-11-19 03:46:011380 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Ticedaccaa92010-09-20 20:44:431381{
1382 if (m_instance_name != InstanceSettings::GetDefaultName())
1383 {
1384 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1385 CopyInstanceSettings (pending_settings,false);
1386 //m_owner.RemovePendingSettings (m_instance_name);
1387 }
1388}
1389
1390TargetInstanceSettings::~TargetInstanceSettings ()
1391{
1392}
1393
1394TargetInstanceSettings&
1395TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1396{
1397 if (this != &rhs)
1398 {
1399 }
1400
1401 return *this;
1402}
1403
Caroline Ticedaccaa92010-09-20 20:44:431404void
1405TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1406 const char *index_value,
1407 const char *value,
1408 const ConstString &instance_name,
1409 const SettingEntry &entry,
Greg Claytone0d378b2011-03-24 21:19:541410 VarSetOperationType op,
Caroline Ticedaccaa92010-09-20 20:44:431411 Error &err,
1412 bool pending)
1413{
Greg Claytonbfe5f3b2011-02-18 01:44:251414 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031415 {
1416 switch (op)
1417 {
1418 default:
1419 err.SetErrorToGenericError ();
1420 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1421 return;
Greg Claytone0d378b2011-03-24 21:19:541422 case eVarSetOperationAssign:
Sean Callanan322f5292010-10-29 00:29:031423 {
1424 FileSpec file_spec(value, true);
1425
1426 if (!file_spec.Exists())
1427 {
1428 err.SetErrorToGenericError ();
1429 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1430 return;
1431 }
1432
Greg Claytonc3f381b2011-02-03 17:47:471433 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan322f5292010-10-29 00:29:031434
Greg Claytonc3f381b2011-02-03 17:47:471435 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan322f5292010-10-29 00:29:031436 {
1437 err.SetErrorToGenericError ();
Greg Claytonc3f381b2011-02-03 17:47:471438 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan322f5292010-10-29 00:29:031439 return;
1440 }
1441
1442 m_expr_prefix_path = value;
Greg Claytonc3f381b2011-02-03 17:47:471443 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan322f5292010-10-29 00:29:031444 }
1445 return;
Greg Claytone0d378b2011-03-24 21:19:541446 case eVarSetOperationAppend:
Sean Callanan322f5292010-10-29 00:29:031447 err.SetErrorToGenericError ();
1448 err.SetErrorString ("Cannot append to a path.\n");
1449 return;
Greg Claytone0d378b2011-03-24 21:19:541450 case eVarSetOperationClear:
Sean Callanan322f5292010-10-29 00:29:031451 m_expr_prefix_path.clear ();
1452 m_expr_prefix_contents.clear ();
1453 return;
1454 }
1455 }
Caroline Ticedaccaa92010-09-20 20:44:431456}
1457
1458void
Greg Claytonbfe5f3b2011-02-18 01:44:251459TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431460{
Sean Callanan322f5292010-10-29 00:29:031461 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1462
1463 if (!new_settings_ptr)
1464 return;
1465
Greg Claytonbfe5f3b2011-02-18 01:44:251466 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1467 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
Caroline Ticedaccaa92010-09-20 20:44:431468}
1469
Caroline Tice12cecd72010-09-20 21:37:421470bool
Caroline Ticedaccaa92010-09-20 20:44:431471TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1472 const ConstString &var_name,
1473 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421474 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431475{
Greg Claytonbfe5f3b2011-02-18 01:44:251476 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031477 {
1478 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1479 }
Sean Callanan322f5292010-10-29 00:29:031480 else
1481 {
1482 if (err)
1483 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1484 return false;
1485 }
1486
1487 return true;
Caroline Ticedaccaa92010-09-20 20:44:431488}
1489
1490const ConstString
1491TargetInstanceSettings::CreateInstanceName ()
1492{
Caroline Ticedaccaa92010-09-20 20:44:431493 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:101494 static int instance_count = 1;
1495
Caroline Ticedaccaa92010-09-20 20:44:431496 sstr.Printf ("target_%d", instance_count);
1497 ++instance_count;
1498
1499 const ConstString ret_val (sstr.GetData());
1500 return ret_val;
1501}
1502
1503//--------------------------------------------------
1504// Target::SettingsController Variable Tables
1505//--------------------------------------------------
1506
1507SettingEntry
1508Target::SettingsController::global_settings_table[] =
1509{
Greg Claytonbfe5f3b2011-02-18 01:44:251510 // var-name var-type default enum init'd hidden help-text
1511 // ================= ================== =========== ==== ====== ====== =========================================================================
1512 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1513 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1514};
1515
Caroline Ticedaccaa92010-09-20 20:44:431516SettingEntry
1517Target::SettingsController::instance_settings_table[] =
1518{
Greg Claytone0d378b2011-03-24 21:19:541519 // var-name var-type default enum init'd hidden help-text
1520 // ================= ================== =========== ==== ====== ====== =========================================================================
1521 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL, false, false, "Path to a file containing expressions to be prepended to all expressions." },
1522 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431523};