blob: 46aa45042e499790fbec2b1708a8dce7f097eb7e [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)
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 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 Claytondda4f7b2010-06-30 23:03:03590 Address resolved_addr(addr);
591 if (!resolved_addr.IsSectionOffset())
592 {
593 if (process_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24594 {
Greg Claytonf5e56de2010-09-14 23:36:40595 m_section_load_list.ResolveLoadAddress (addr.GetOffset(), resolved_addr);
Greg Claytondda4f7b2010-06-30 23:03:03596 }
597 else
598 {
599 m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr);
600 }
601 }
602
Greg Claytondb598232011-01-07 01:57:07603 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 Claytondda4f7b2010-06-30 23:03:03609
610 if (process_is_valid)
611 {
Greg Claytonf5e56de2010-09-14 23:36:40612 lldb::addr_t load_addr = resolved_addr.GetLoadAddress (this);
Greg Claytondda4f7b2010-06-30 23:03:03613 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 Claytondb598232011-01-07 01:57:07624 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24625 if (bytes_read != dst_len)
626 {
627 if (error.Success())
628 {
629 if (bytes_read == 0)
Greg Claytondda4f7b2010-06-30 23:03:03630 error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24631 else
Greg Claytondda4f7b2010-06-30 23:03:03632 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:24633 }
634 }
Greg Claytondda4f7b2010-06-30 23:03:03635 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 Lattner30fdc8d2010-06-08 16:52:24644 }
Chris Lattner30fdc8d2010-06-08 16:52:24645 }
Greg Claytondda4f7b2010-06-30 23:03:03646
Greg Claytondb598232011-01-07 01:57:07647 if (!prefer_file_cache)
Greg Claytondda4f7b2010-06-30 23:03:03648 {
Greg Claytondb598232011-01-07 01:57:07649 // 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 Claytondda4f7b2010-06-30 23:03:03652 }
653 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24654}
655
656
657ModuleSP
658Target::GetSharedModule
659(
660 const FileSpec& file_spec,
661 const ArchSpec& arch,
Greg Clayton60830262011-02-04 18:53:10662 const lldb_private::UUID *uuid_ptr,
Chris Lattner30fdc8d2010-06-08 16:52:24663 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 Clayton0603aa92010-10-04 01:05:56736Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24737{
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 Ticedaccaa92010-09-20 20:44:43774
Greg Clayton99d0faf2010-11-18 23:32:35775void
Caroline Tice20bd37f2011-03-10 22:14:10776Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43777{
Greg Clayton99d0faf2010-11-18 23:32:35778 UserSettingsControllerSP &usc = GetSettingsController();
779 usc.reset (new SettingsController);
780 UserSettingsController::InitializeSettingsController (usc,
781 SettingsController::global_settings_table,
782 SettingsController::instance_settings_table);
Caroline Tice20bd37f2011-03-10 22:14:10783
784 // Now call SettingsInitialize() on each 'child' setting of Target
785 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35786}
Caroline Ticedaccaa92010-09-20 20:44:43787
Greg Clayton99d0faf2010-11-18 23:32:35788void
Caroline Tice20bd37f2011-03-10 22:14:10789Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35790{
Caroline Tice20bd37f2011-03-10 22:14:10791
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 Clayton99d0faf2010-11-18 23:32:35798 UserSettingsControllerSP &usc = GetSettingsController();
799 UserSettingsController::FinalizeSettingsController (usc);
800 usc.reset();
801}
Caroline Ticedaccaa92010-09-20 20:44:43802
Greg Clayton99d0faf2010-11-18 23:32:35803UserSettingsControllerSP &
804Target::GetSettingsController ()
805{
806 static UserSettingsControllerSP g_settings_controller;
Caroline Ticedaccaa92010-09-20 20:44:43807 return g_settings_controller;
808}
809
810ArchSpec
811Target::GetDefaultArchitecture ()
812{
Greg Clayton64195a22011-02-23 00:35:02813 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 Ticedaccaa92010-09-20 20:44:43818}
819
820void
Greg Clayton64195a22011-02-23 00:35:02821Target::SetDefaultArchitecture (const ArchSpec& arch)
Caroline Ticedaccaa92010-09-20 20:44:43822{
Greg Clayton64195a22011-02-23 00:35:02823 lldb::UserSettingsControllerSP settings_controller_sp (GetSettingsController());
824
825 if (settings_controller_sp)
826 static_cast<Target::SettingsController *>(settings_controller_sp.get())->GetArchitecture () = arch;
Caroline Ticedaccaa92010-09-20 20:44:43827}
828
Greg Clayton0603aa92010-10-04 01:05:56829Target *
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 Tice1559a462010-09-27 00:30:10848void
849Target::UpdateInstanceName ()
850{
851 StreamString sstr;
852
853 ModuleSP module_sp = GetExecutableModule();
854 if (module_sp)
855 {
Greg Clayton307de252010-10-27 02:06:37856 sstr.Printf ("%s_%s",
857 module_sp->GetFileSpec().GetFilename().AsCString(),
Greg Clayton64195a22011-02-23 00:35:02858 module_sp->GetArchitecture().GetArchitectureName());
Greg Claytondbe54502010-11-19 03:46:01859 GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(),
860 sstr.GetData());
Caroline Tice1559a462010-09-27 00:30:10861 }
862}
863
Sean Callanan322f5292010-10-29 00:29:03864const char *
865Target::GetExpressionPrefixContentsAsCString ()
866{
867 return m_expr_prefix_contents.c_str();
868}
869
Greg Clayton8b2fe6d2010-12-14 02:59:59870ExecutionResults
871Target::EvaluateExpression
872(
873 const char *expr_cstr,
874 StackFrame *frame,
875 bool unwind_on_error,
Sean Callanan92adcac2011-01-13 08:53:35876 bool keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59877 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 Clayton54979cd2010-12-15 05:08:08888 Error error;
Greg Clayton6d5e68e2011-01-20 19:27:18889 const uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
890 StackFrame::eExpressionPathOptionsNoFragileObjcIvar;
891 result_valobj_sp = frame->GetValueForVariableExpressionPath (expr_cstr, expr_path_options, error);
Greg Clayton8b2fe6d2010-12-14 02:59:59892 }
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 Callanan92adcac2011-01-13 08:53:35920 lldb::ValueObjectSP live_valobj_sp = result_valobj_sp;
921
Greg Clayton8b2fe6d2010-12-14 02:59:59922 result_valobj_sp = const_valobj_sp;
923
Sean Callanan92adcac2011-01-13 08:53:35924 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 Clayton8b2fe6d2010-12-14 02:59:59946 }
947 else
948 {
949 // Make sure we aren't just trying to see the value of a persistent
950 // variable (something like "$0")
Greg Clayton3e06bd92011-01-09 21:07:35951 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 Clayton8b2fe6d2010-12-14 02:59:59956 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 Callanan92adcac2011-01-13 08:53:35966 unwind_on_error,
967 keep_in_memory,
Greg Clayton8b2fe6d2010-12-14 02:59:59968 expr_cstr,
969 prefix,
970 result_valobj_sp);
971 }
972 }
973 return execution_results;
974}
975
Jim Ingham9575d842011-03-11 03:53:59976lldb::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;
Jim Ingham381e25b2011-03-22 01:47:271088 bool print_hook_header;
1089 bool print_thread_header;
1090
1091 if (num_exe_ctx == 1)
1092 print_thread_header = false;
1093 else
1094 print_thread_header = true;
1095
1096 if (m_stop_hooks.size() == 1)
1097 print_hook_header = false;
1098 else
1099 print_hook_header = true;
1100
Jim Ingham9575d842011-03-11 03:53:591101 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
1102 {
1103 // result.Clear();
1104 StopHookSP cur_hook_sp = (*pos).second;
1105 if (!cur_hook_sp->IsActive())
1106 continue;
1107
1108 bool any_thread_matched = false;
1109 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
1110 {
1111 if ((cur_hook_sp->GetSpecifier () == NULL
1112 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
1113 && (cur_hook_sp->GetThreadSpecifier() == NULL
1114 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].thread)))
1115 {
1116 if (!hooks_ran)
1117 {
Jim Ingham381e25b2011-03-22 01:47:271118 result.AppendMessage("\n** Stop Hooks **");
Jim Ingham9575d842011-03-11 03:53:591119 hooks_ran = true;
1120 }
Jim Ingham381e25b2011-03-22 01:47:271121 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:591122 {
1123 result.AppendMessageWithFormat("\n- Hook %d\n", cur_hook_sp->GetID());
1124 any_thread_matched = true;
1125 }
1126
Jim Ingham381e25b2011-03-22 01:47:271127 if (print_thread_header)
1128 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].thread->GetIndexID());
Jim Ingham9575d842011-03-11 03:53:591129
1130 bool stop_on_continue = true;
1131 bool stop_on_error = true;
1132 bool echo_commands = false;
1133 bool print_results = true;
1134 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
1135 &exc_ctx_with_reasons[i],
1136 stop_on_continue,
1137 stop_on_error,
1138 echo_commands,
1139 print_results,
1140 result);
1141
1142 // If the command started the target going again, we should bag out of
1143 // running the stop hooks.
1144 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
1145 || (result.GetStatus() == eReturnStatusSuccessContinuingResult))
1146 {
1147 result.AppendMessageWithFormat ("Aborting stop hooks, hook %d set the program running.", cur_hook_sp->GetID());
1148 keep_going = false;
1149 }
1150 }
1151 }
1152 }
1153 if (hooks_ran)
1154 result.AppendMessage ("\n** End Stop Hooks **\n");
1155}
1156
1157//--------------------------------------------------------------
1158// class Target::StopHook
1159//--------------------------------------------------------------
1160
1161
1162Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
1163 UserID (uid),
1164 m_target_sp (target_sp),
1165 m_active (true),
1166 m_commands (),
1167 m_specifier_sp (),
1168 m_thread_spec_ap(NULL)
1169{
1170}
1171
1172Target::StopHook::StopHook (const StopHook &rhs) :
1173 UserID (rhs.GetID()),
1174 m_target_sp (rhs.m_target_sp),
1175 m_commands (rhs.m_commands),
1176 m_specifier_sp (rhs.m_specifier_sp),
1177 m_active (rhs.m_active),
1178 m_thread_spec_ap (NULL)
1179{
1180 if (rhs.m_thread_spec_ap.get() != NULL)
1181 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
1182}
1183
1184
1185Target::StopHook::~StopHook ()
1186{
1187}
1188
1189void
1190Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
1191{
1192 m_thread_spec_ap.reset (specifier);
1193}
1194
1195
1196void
1197Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
1198{
1199 int indent_level = s->GetIndentLevel();
1200
1201 s->SetIndentLevel(indent_level + 2);
1202
1203 s->Printf ("Hook: %d\n", GetID());
1204 if (m_active)
1205 s->Indent ("State: enabled\n");
1206 else
1207 s->Indent ("State: disabled\n");
1208
1209 if (m_specifier_sp)
1210 {
1211 s->Indent();
1212 s->PutCString ("Specifier:\n");
1213 s->SetIndentLevel (indent_level + 4);
1214 m_specifier_sp->GetDescription (s, level);
1215 s->SetIndentLevel (indent_level + 2);
1216 }
1217
1218 if (m_thread_spec_ap.get() != NULL)
1219 {
1220 StreamString tmp;
1221 s->Indent("Thread:\n");
1222 m_thread_spec_ap->GetDescription (&tmp, level);
1223 s->SetIndentLevel (indent_level + 4);
1224 s->Indent (tmp.GetData());
1225 s->PutCString ("\n");
1226 s->SetIndentLevel (indent_level + 2);
1227 }
1228
1229 s->Indent ("Commands: \n");
1230 s->SetIndentLevel (indent_level + 4);
1231 uint32_t num_commands = m_commands.GetSize();
1232 for (uint32_t i = 0; i < num_commands; i++)
1233 {
1234 s->Indent(m_commands.GetStringAtIndex(i));
1235 s->PutCString ("\n");
1236 }
1237 s->SetIndentLevel (indent_level);
1238}
1239
1240
Caroline Ticedaccaa92010-09-20 20:44:431241//--------------------------------------------------------------
1242// class Target::SettingsController
1243//--------------------------------------------------------------
1244
1245Target::SettingsController::SettingsController () :
1246 UserSettingsController ("target", Debugger::GetSettingsController()),
1247 m_default_architecture ()
1248{
1249 m_default_settings.reset (new TargetInstanceSettings (*this, false,
1250 InstanceSettings::GetDefaultName().AsCString()));
1251}
1252
1253Target::SettingsController::~SettingsController ()
1254{
1255}
1256
1257lldb::InstanceSettingsSP
1258Target::SettingsController::CreateInstanceSettings (const char *instance_name)
1259{
Greg Claytondbe54502010-11-19 03:46:011260 TargetInstanceSettings *new_settings = new TargetInstanceSettings (*GetSettingsController(),
1261 false,
1262 instance_name);
Caroline Ticedaccaa92010-09-20 20:44:431263 lldb::InstanceSettingsSP new_settings_sp (new_settings);
1264 return new_settings_sp;
1265}
1266
Caroline Ticedaccaa92010-09-20 20:44:431267
Greg Claytonbfe5f3b2011-02-18 01:44:251268#define TSC_DEFAULT_ARCH "default-arch"
1269#define TSC_EXPR_PREFIX "expr-prefix"
1270#define TSC_EXEC_LEVEL "execution-level"
1271#define TSC_EXEC_MODE "execution-mode"
1272#define TSC_EXEC_OS_TYPE "execution-os-type"
1273
1274
1275static const ConstString &
1276GetSettingNameForDefaultArch ()
1277{
1278 static ConstString g_const_string (TSC_DEFAULT_ARCH);
1279
1280 return g_const_string;
Caroline Ticedaccaa92010-09-20 20:44:431281}
1282
Greg Claytonbfe5f3b2011-02-18 01:44:251283static const ConstString &
1284GetSettingNameForExpressionPrefix ()
1285{
1286 static ConstString g_const_string (TSC_EXPR_PREFIX);
1287 return g_const_string;
1288}
1289
1290static const ConstString &
1291GetSettingNameForExecutionLevel ()
1292{
1293 static ConstString g_const_string (TSC_EXEC_LEVEL);
1294 return g_const_string;
1295}
1296
1297static const ConstString &
1298GetSettingNameForExecutionMode ()
1299{
1300 static ConstString g_const_string (TSC_EXEC_MODE);
1301 return g_const_string;
1302}
1303
1304static const ConstString &
1305GetSettingNameForExecutionOSType ()
1306{
1307 static ConstString g_const_string (TSC_EXEC_OS_TYPE);
1308 return g_const_string;
1309}
1310
1311
Caroline Ticedaccaa92010-09-20 20:44:431312bool
1313Target::SettingsController::SetGlobalVariable (const ConstString &var_name,
1314 const char *index_value,
1315 const char *value,
1316 const SettingEntry &entry,
1317 const lldb::VarSetOperationType op,
1318 Error&err)
1319{
Greg Claytonbfe5f3b2011-02-18 01:44:251320 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431321 {
Greg Clayton64195a22011-02-23 00:35:021322 m_default_architecture.SetTriple (value);
1323 if (!m_default_architecture.IsValid())
1324 err.SetErrorStringWithFormat ("'%s' is not a valid architecture or triple.", value);
Caroline Ticedaccaa92010-09-20 20:44:431325 }
1326 return true;
1327}
1328
1329
1330bool
1331Target::SettingsController::GetGlobalVariable (const ConstString &var_name,
1332 StringList &value,
1333 Error &err)
1334{
Greg Claytonbfe5f3b2011-02-18 01:44:251335 if (var_name == GetSettingNameForDefaultArch())
Caroline Ticedaccaa92010-09-20 20:44:431336 {
Greg Clayton307de252010-10-27 02:06:371337 // If the arch is invalid (the default), don't show a string for it
1338 if (m_default_architecture.IsValid())
Greg Clayton64195a22011-02-23 00:35:021339 value.AppendString (m_default_architecture.GetArchitectureName());
Caroline Ticedaccaa92010-09-20 20:44:431340 return true;
1341 }
1342 else
1343 err.SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1344
1345 return false;
1346}
1347
1348//--------------------------------------------------------------
1349// class TargetInstanceSettings
1350//--------------------------------------------------------------
1351
Greg Clayton85851dd2010-12-04 00:10:171352TargetInstanceSettings::TargetInstanceSettings
1353(
1354 UserSettingsController &owner,
1355 bool live_instance,
1356 const char *name
1357) :
Greg Claytonbfe5f3b2011-02-18 01:44:251358 InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance),
1359 m_expr_prefix_path (),
1360 m_expr_prefix_contents (),
1361 m_execution_level (eExecutionLevelAuto),
1362 m_execution_mode (eExecutionModeAuto),
1363 m_execution_os_type (eExecutionOSTypeAuto)
Caroline Ticedaccaa92010-09-20 20:44:431364{
1365 // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called
1366 // until the vtables for TargetInstanceSettings are properly set up, i.e. AFTER all the initializers.
1367 // For this reason it has to be called here, rather than in the initializer or in the parent constructor.
1368 // This is true for CreateInstanceName() too.
1369
1370 if (GetInstanceName () == InstanceSettings::InvalidName())
1371 {
1372 ChangeInstanceName (std::string (CreateInstanceName().AsCString()));
1373 m_owner.RegisterInstanceSettings (this);
1374 }
1375
1376 if (live_instance)
1377 {
1378 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1379 CopyInstanceSettings (pending_settings,false);
1380 //m_owner.RemovePendingSettings (m_instance_name);
1381 }
1382}
1383
1384TargetInstanceSettings::TargetInstanceSettings (const TargetInstanceSettings &rhs) :
Greg Claytondbe54502010-11-19 03:46:011385 InstanceSettings (*Target::GetSettingsController(), CreateInstanceName().AsCString())
Caroline Ticedaccaa92010-09-20 20:44:431386{
1387 if (m_instance_name != InstanceSettings::GetDefaultName())
1388 {
1389 const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name);
1390 CopyInstanceSettings (pending_settings,false);
1391 //m_owner.RemovePendingSettings (m_instance_name);
1392 }
1393}
1394
1395TargetInstanceSettings::~TargetInstanceSettings ()
1396{
1397}
1398
1399TargetInstanceSettings&
1400TargetInstanceSettings::operator= (const TargetInstanceSettings &rhs)
1401{
1402 if (this != &rhs)
1403 {
1404 }
1405
1406 return *this;
1407}
1408
Caroline Ticedaccaa92010-09-20 20:44:431409void
1410TargetInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name,
1411 const char *index_value,
1412 const char *value,
1413 const ConstString &instance_name,
1414 const SettingEntry &entry,
1415 lldb::VarSetOperationType op,
1416 Error &err,
1417 bool pending)
1418{
Greg Claytonbfe5f3b2011-02-18 01:44:251419 int new_enum = -1;
1420
1421 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031422 {
1423 switch (op)
1424 {
1425 default:
1426 err.SetErrorToGenericError ();
1427 err.SetErrorString ("Unrecognized operation. Cannot update value.\n");
1428 return;
1429 case lldb::eVarSetOperationAssign:
1430 {
1431 FileSpec file_spec(value, true);
1432
1433 if (!file_spec.Exists())
1434 {
1435 err.SetErrorToGenericError ();
1436 err.SetErrorStringWithFormat ("%s does not exist.\n", value);
1437 return;
1438 }
1439
Greg Claytonc3f381b2011-02-03 17:47:471440 DataBufferSP data_sp (file_spec.ReadFileContents());
Sean Callanan322f5292010-10-29 00:29:031441
Greg Claytonc3f381b2011-02-03 17:47:471442 if (!data_sp && data_sp->GetByteSize() == 0)
Sean Callanan322f5292010-10-29 00:29:031443 {
1444 err.SetErrorToGenericError ();
Greg Claytonc3f381b2011-02-03 17:47:471445 err.SetErrorStringWithFormat ("Couldn't read from %s\n", value);
Sean Callanan322f5292010-10-29 00:29:031446 return;
1447 }
1448
1449 m_expr_prefix_path = value;
Greg Claytonc3f381b2011-02-03 17:47:471450 m_expr_prefix_contents.assign(reinterpret_cast<const char *>(data_sp->GetBytes()), data_sp->GetByteSize());
Sean Callanan322f5292010-10-29 00:29:031451 }
1452 return;
1453 case lldb::eVarSetOperationAppend:
1454 err.SetErrorToGenericError ();
1455 err.SetErrorString ("Cannot append to a path.\n");
1456 return;
1457 case lldb::eVarSetOperationClear:
1458 m_expr_prefix_path.clear ();
1459 m_expr_prefix_contents.clear ();
1460 return;
1461 }
1462 }
Greg Claytonbfe5f3b2011-02-18 01:44:251463 else if (var_name == GetSettingNameForExecutionLevel ())
1464 {
1465 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1466 if (err.Success())
1467 m_execution_level = (ExecutionLevel)new_enum;
1468 }
1469 else if (var_name == GetSettingNameForExecutionMode ())
1470 {
1471 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1472 if (err.Success())
1473 m_execution_mode = (ExecutionMode)new_enum;
1474 }
1475 else if (var_name == GetSettingNameForExecutionOSType ())
1476 {
1477 UserSettingsController::UpdateEnumVariable (entry.enum_values, &new_enum, value, err);
1478 if (err.Success())
1479 m_execution_os_type = (ExecutionOSType)new_enum;
1480 }
Caroline Ticedaccaa92010-09-20 20:44:431481}
1482
1483void
Greg Claytonbfe5f3b2011-02-18 01:44:251484TargetInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, bool pending)
Caroline Ticedaccaa92010-09-20 20:44:431485{
Sean Callanan322f5292010-10-29 00:29:031486 TargetInstanceSettings *new_settings_ptr = static_cast <TargetInstanceSettings *> (new_settings.get());
1487
1488 if (!new_settings_ptr)
1489 return;
1490
Greg Claytonbfe5f3b2011-02-18 01:44:251491 m_expr_prefix_path = new_settings_ptr->m_expr_prefix_path;
1492 m_expr_prefix_contents = new_settings_ptr->m_expr_prefix_contents;
1493 m_execution_level = new_settings_ptr->m_execution_level;
1494 m_execution_mode = new_settings_ptr->m_execution_mode;
1495 m_execution_os_type = new_settings_ptr->m_execution_os_type;
Caroline Ticedaccaa92010-09-20 20:44:431496}
1497
Caroline Tice12cecd72010-09-20 21:37:421498bool
Caroline Ticedaccaa92010-09-20 20:44:431499TargetInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry,
1500 const ConstString &var_name,
1501 StringList &value,
Caroline Tice12cecd72010-09-20 21:37:421502 Error *err)
Caroline Ticedaccaa92010-09-20 20:44:431503{
Greg Claytonbfe5f3b2011-02-18 01:44:251504 if (var_name == GetSettingNameForExpressionPrefix ())
Sean Callanan322f5292010-10-29 00:29:031505 {
1506 value.AppendString (m_expr_prefix_path.c_str(), m_expr_prefix_path.size());
1507 }
Greg Claytonbfe5f3b2011-02-18 01:44:251508 else if (var_name == GetSettingNameForExecutionLevel ())
1509 {
1510 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_level));
1511 }
1512 else if (var_name == GetSettingNameForExecutionMode ())
1513 {
1514 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_mode));
1515 }
1516 else if (var_name == GetSettingNameForExecutionOSType ())
1517 {
1518 value.AppendString (UserSettingsController::EnumToString (entry.enum_values, m_execution_os_type));
1519 }
Sean Callanan322f5292010-10-29 00:29:031520 else
1521 {
1522 if (err)
1523 err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString());
1524 return false;
1525 }
1526
1527 return true;
Caroline Ticedaccaa92010-09-20 20:44:431528}
1529
1530const ConstString
1531TargetInstanceSettings::CreateInstanceName ()
1532{
Caroline Ticedaccaa92010-09-20 20:44:431533 StreamString sstr;
Caroline Tice1559a462010-09-27 00:30:101534 static int instance_count = 1;
1535
Caroline Ticedaccaa92010-09-20 20:44:431536 sstr.Printf ("target_%d", instance_count);
1537 ++instance_count;
1538
1539 const ConstString ret_val (sstr.GetData());
1540 return ret_val;
1541}
1542
1543//--------------------------------------------------
1544// Target::SettingsController Variable Tables
1545//--------------------------------------------------
1546
1547SettingEntry
1548Target::SettingsController::global_settings_table[] =
1549{
Greg Claytonbfe5f3b2011-02-18 01:44:251550 // var-name var-type default enum init'd hidden help-text
1551 // ================= ================== =========== ==== ====== ====== =========================================================================
1552 { TSC_DEFAULT_ARCH , eSetVarTypeString , NULL , NULL, false, false, "Default architecture to choose, when there's a choice." },
1553 { NULL , eSetVarTypeNone , NULL , NULL, false, false, NULL }
1554};
1555
1556static lldb::OptionEnumValueElement
1557g_execution_level_enums[] =
1558{
1559 { eExecutionLevelAuto , "auto" , "Automatically detect the execution level (user/kernel)." },
1560 { eExecutionLevelKernel , "kernel" , "Treat executables as kernel executables." },
1561 { eExecutionLevelUser , "user" , "Treat executables as user space executables." },
1562 { 0 , NULL , NULL }
1563};
1564
1565static lldb::OptionEnumValueElement
1566g_execution_mode_enums[] =
1567{
1568 { eExecutionModeAuto , "auto" , "Automatically detect the execution mode (static/dynamic)." },
1569 { eExecutionModeStatic , "static" , "All executables are static and addresses at the virtual addresses in the object files." },
1570 { eExecutionModeDynamic , "dynamic" , "Executables and shared libraries are dynamically loaded.." },
1571 { 0 , NULL , NULL }
1572};
1573
1574static lldb::OptionEnumValueElement
1575g_execution_os_enums[] =
1576{
1577 { eExecutionOSTypeAuto , "auto" , "Automatically detect the execution OS (none/halted/live)." },
1578 { eExecutionOSTypeNone , "none" , "There is no operating system available (no processes or threads)." },
1579 { eExecutionOSTypeHalted, "halted" , "There is an operating system, but it is halted when the debugger is halted. "
1580 "Processes and threads must be discovered by accessing symbols and reading "
1581 "memory." },
1582 { eExecutionOSTypeLive , "live" , "There is a live operating system with debug services that can be used to "
1583 "get processes, threads and theirs states." },
1584 { 0, NULL, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431585};
1586
1587SettingEntry
1588Target::SettingsController::instance_settings_table[] =
1589{
Greg Claytonbfe5f3b2011-02-18 01:44:251590 // var-name var-type default enum-table init'd hidden help-text
1591 // ================= ================== =========== ========================= ====== ====== =========================================================================
1592 { TSC_EXPR_PREFIX , eSetVarTypeString , NULL , NULL , false, false, "Path to a file containing expressions to be prepended to all expressions." },
1593 { TSC_EXEC_LEVEL , eSetVarTypeEnum , "auto" , g_execution_level_enums , false, false, "Sets the execution level for a target." },
1594 { TSC_EXEC_MODE , eSetVarTypeEnum , "auto" , g_execution_mode_enums , false, false, "Sets the execution mode for a target." },
1595 { TSC_EXEC_OS_TYPE , eSetVarTypeEnum , "auto" , g_execution_os_enums , false, false, "Sets the execution OS for a target." },
1596 { NULL , eSetVarTypeNone , NULL , NULL , false, false, NULL }
Caroline Ticedaccaa92010-09-20 20:44:431597};