Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 1 | //===-- 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" |
| 20 | #include "lldb/Core/Event.h" |
| 21 | #include "lldb/Core/Log.h" |
| 22 | #include "lldb/Core/Timer.h" |
| 23 | #include "lldb/Core/StreamString.h" |
| 24 | #include "lldb/Host/Host.h" |
| 25 | #include "lldb/lldb-private-log.h" |
| 26 | #include "lldb/Symbol/ObjectFile.h" |
| 27 | #include "lldb/Target/Process.h" |
| 28 | #include "lldb/Core/Debugger.h" |
| 29 | |
| 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
| 33 | //---------------------------------------------------------------------- |
| 34 | // Target constructor |
| 35 | //---------------------------------------------------------------------- |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 | [diff] [blame] | 36 | Target::Target(Debugger &debugger) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 37 | Broadcaster("Target"), |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 | [diff] [blame] | 38 | m_debugger (debugger), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 39 | m_images(), |
| 40 | m_breakpoint_list (false), |
| 41 | m_internal_breakpoint_list (true), |
| 42 | m_process_sp(), |
| 43 | m_triple(), |
| 44 | m_search_filter_sp(), |
| 45 | m_image_search_paths (ImageSearchPathsChanged, this), |
| 46 | m_scratch_ast_context_ap(NULL) |
| 47 | { |
| 48 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); |
| 49 | if (log) |
| 50 | log->Printf ("%p Target::Target()", this); |
| 51 | } |
| 52 | |
| 53 | //---------------------------------------------------------------------- |
| 54 | // Destructor |
| 55 | //---------------------------------------------------------------------- |
| 56 | Target::~Target() |
| 57 | { |
| 58 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); |
| 59 | if (log) |
| 60 | log->Printf ("%p Target::~Target()", this); |
| 61 | DeleteCurrentProcess (); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | Target::Dump (Stream *s) |
| 66 | { |
| 67 | s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
| 68 | s->Indent(); |
| 69 | s->PutCString("Target\n"); |
| 70 | s->IndentMore(); |
| 71 | m_images.Dump(s); |
| 72 | m_breakpoint_list.Dump(s); |
| 73 | m_internal_breakpoint_list.Dump(s); |
| 74 | // if (m_process_sp.get()) |
| 75 | // m_process_sp->Dump(s); |
| 76 | s->IndentLess(); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | Target::DeleteCurrentProcess () |
| 81 | { |
| 82 | if (m_process_sp.get()) |
| 83 | { |
| 84 | if (m_process_sp->IsAlive()) |
| 85 | m_process_sp->Destroy(); |
| 86 | else |
| 87 | m_process_sp->Finalize(); |
| 88 | |
| 89 | // Do any cleanup of the target we need to do between process instances. |
| 90 | // NB It is better to do this before destroying the process in case the |
| 91 | // clean up needs some help from the process. |
| 92 | m_breakpoint_list.ClearAllBreakpointSites(); |
| 93 | m_internal_breakpoint_list.ClearAllBreakpointSites(); |
| 94 | m_process_sp.reset(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | const lldb::ProcessSP & |
| 99 | Target::CreateProcess (Listener &listener, const char *plugin_name) |
| 100 | { |
| 101 | DeleteCurrentProcess (); |
| 102 | m_process_sp.reset(Process::FindPlugin(*this, plugin_name, listener)); |
| 103 | return m_process_sp; |
| 104 | } |
| 105 | |
| 106 | const lldb::ProcessSP & |
| 107 | Target::GetProcessSP () const |
| 108 | { |
| 109 | return m_process_sp; |
| 110 | } |
| 111 | |
| 112 | lldb::TargetSP |
| 113 | Target::GetSP() |
| 114 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 | [diff] [blame] | 115 | return m_debugger.GetTargetList().GetTargetSP(this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | BreakpointList & |
| 119 | Target::GetBreakpointList(bool internal) |
| 120 | { |
| 121 | if (internal) |
| 122 | return m_internal_breakpoint_list; |
| 123 | else |
| 124 | return m_breakpoint_list; |
| 125 | } |
| 126 | |
| 127 | const BreakpointList & |
| 128 | Target::GetBreakpointList(bool internal) const |
| 129 | { |
| 130 | if (internal) |
| 131 | return m_internal_breakpoint_list; |
| 132 | else |
| 133 | return m_breakpoint_list; |
| 134 | } |
| 135 | |
| 136 | BreakpointSP |
| 137 | Target::GetBreakpointByID (break_id_t break_id) |
| 138 | { |
| 139 | BreakpointSP bp_sp; |
| 140 | |
| 141 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
| 142 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); |
| 143 | else |
| 144 | bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); |
| 145 | |
| 146 | return bp_sp; |
| 147 | } |
| 148 | |
| 149 | BreakpointSP |
| 150 | Target::CreateBreakpoint (const FileSpec *containingModule, const FileSpec &file, uint32_t line_no, bool check_inlines, bool internal) |
| 151 | { |
| 152 | SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); |
| 153 | BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL, file, line_no, check_inlines)); |
| 154 | return CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | BreakpointSP |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 | [diff] [blame^] | 159 | Target::CreateBreakpoint (lldb::addr_t addr, bool internal) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 160 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 161 | Address so_addr; |
| 162 | // Attempt to resolve our load address if possible, though it is ok if |
| 163 | // it doesn't resolve to section/offset. |
| 164 | |
| 165 | Process *process = GetProcessSP().get(); |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 | [diff] [blame^] | 166 | // Try and resolve as a load address if possible |
| 167 | if (process) |
| 168 | process->ResolveLoadAddress(addr, so_addr); |
| 169 | if (!so_addr.IsValid()) |
| 170 | { |
| 171 | // The address didn't resolve, so just set this as an absolute address |
| 172 | so_addr.SetOffset (addr); |
| 173 | } |
| 174 | BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 175 | return bp_sp; |
| 176 | } |
| 177 | |
| 178 | BreakpointSP |
| 179 | Target::CreateBreakpoint (Address &addr, bool internal) |
| 180 | { |
| 181 | TargetSP target_sp = this->GetSP(); |
| 182 | SearchFilterSP filter_sp(new SearchFilter (target_sp)); |
| 183 | BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr)); |
| 184 | return CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 185 | } |
| 186 | |
| 187 | BreakpointSP |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 | [diff] [blame] | 188 | Target::CreateBreakpoint (FileSpec *containingModule, const char *func_name, uint32_t func_name_type_mask, bool internal) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 189 | { |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 | [diff] [blame] | 190 | BreakpointSP bp_sp; |
| 191 | if (func_name) |
| 192 | { |
| 193 | SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); |
| 194 | BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL, func_name, func_name_type_mask, Breakpoint::Exact)); |
| 195 | bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 196 | } |
| 197 | return bp_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
| 201 | SearchFilterSP |
| 202 | Target::GetSearchFilterForModule (const FileSpec *containingModule) |
| 203 | { |
| 204 | SearchFilterSP filter_sp; |
| 205 | lldb::TargetSP target_sp = this->GetSP(); |
| 206 | if (containingModule != NULL) |
| 207 | { |
| 208 | // TODO: We should look into sharing module based search filters |
| 209 | // across many breakpoints like we do for the simple target based one |
| 210 | filter_sp.reset (new SearchFilterByModule (target_sp, *containingModule)); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | if (m_search_filter_sp.get() == NULL) |
| 215 | m_search_filter_sp.reset (new SearchFilter (target_sp)); |
| 216 | filter_sp = m_search_filter_sp; |
| 217 | } |
| 218 | return filter_sp; |
| 219 | } |
| 220 | |
| 221 | BreakpointSP |
| 222 | Target::CreateBreakpoint (FileSpec *containingModule, RegularExpression &func_regex, bool internal) |
| 223 | { |
| 224 | SearchFilterSP filter_sp(GetSearchFilterForModule (containingModule)); |
| 225 | BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL, func_regex)); |
| 226 | |
| 227 | return CreateBreakpoint (filter_sp, resolver_sp, internal); |
| 228 | } |
| 229 | |
| 230 | BreakpointSP |
| 231 | Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal) |
| 232 | { |
| 233 | BreakpointSP bp_sp; |
| 234 | if (filter_sp && resolver_sp) |
| 235 | { |
| 236 | bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp)); |
| 237 | resolver_sp->SetBreakpoint (bp_sp.get()); |
| 238 | |
| 239 | if (internal) |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 240 | m_internal_breakpoint_list.Add (bp_sp, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 241 | else |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 242 | m_breakpoint_list.Add (bp_sp, true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 243 | |
| 244 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 245 | if (log) |
| 246 | { |
| 247 | StreamString s; |
| 248 | bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
| 249 | log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, internal ? "yes" : "no", s.GetData()); |
| 250 | } |
| 251 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 252 | bp_sp->ResolveBreakpoint(); |
| 253 | } |
| 254 | return bp_sp; |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | Target::RemoveAllBreakpoints (bool internal_also) |
| 259 | { |
| 260 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 261 | if (log) |
| 262 | log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); |
| 263 | |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 264 | m_breakpoint_list.RemoveAll (true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 265 | if (internal_also) |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 266 | m_internal_breakpoint_list.RemoveAll (false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void |
| 270 | Target::DisableAllBreakpoints (bool internal_also) |
| 271 | { |
| 272 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 273 | if (log) |
| 274 | log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); |
| 275 | |
| 276 | m_breakpoint_list.SetEnabledAll (false); |
| 277 | if (internal_also) |
| 278 | m_internal_breakpoint_list.SetEnabledAll (false); |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | Target::EnableAllBreakpoints (bool internal_also) |
| 283 | { |
| 284 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 285 | if (log) |
| 286 | log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no"); |
| 287 | |
| 288 | m_breakpoint_list.SetEnabledAll (true); |
| 289 | if (internal_also) |
| 290 | m_internal_breakpoint_list.SetEnabledAll (true); |
| 291 | } |
| 292 | |
| 293 | bool |
| 294 | Target::RemoveBreakpointByID (break_id_t break_id) |
| 295 | { |
| 296 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 297 | if (log) |
| 298 | log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); |
| 299 | |
| 300 | if (DisableBreakpointByID (break_id)) |
| 301 | { |
| 302 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 303 | m_internal_breakpoint_list.Remove(break_id, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 304 | else |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 | [diff] [blame] | 305 | m_breakpoint_list.Remove(break_id, true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 306 | return true; |
| 307 | } |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | bool |
| 312 | Target::DisableBreakpointByID (break_id_t break_id) |
| 313 | { |
| 314 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 315 | if (log) |
| 316 | log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); |
| 317 | |
| 318 | BreakpointSP bp_sp; |
| 319 | |
| 320 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
| 321 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); |
| 322 | else |
| 323 | bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); |
| 324 | if (bp_sp) |
| 325 | { |
| 326 | bp_sp->SetEnabled (false); |
| 327 | return true; |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | bool |
| 333 | Target::EnableBreakpointByID (break_id_t break_id) |
| 334 | { |
| 335 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 336 | if (log) |
| 337 | log->Printf ("Target::%s (break_id = %i, internal = %s)\n", |
| 338 | __FUNCTION__, |
| 339 | break_id, |
| 340 | LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no"); |
| 341 | |
| 342 | BreakpointSP bp_sp; |
| 343 | |
| 344 | if (LLDB_BREAK_ID_IS_INTERNAL (break_id)) |
| 345 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id); |
| 346 | else |
| 347 | bp_sp = m_breakpoint_list.FindBreakpointByID (break_id); |
| 348 | |
| 349 | if (bp_sp) |
| 350 | { |
| 351 | bp_sp->SetEnabled (true); |
| 352 | return true; |
| 353 | } |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | ModuleSP |
| 358 | Target::GetExecutableModule () |
| 359 | { |
| 360 | ModuleSP executable_sp; |
| 361 | if (m_images.GetSize() > 0) |
| 362 | executable_sp = m_images.GetModuleAtIndex(0); |
| 363 | return executable_sp; |
| 364 | } |
| 365 | |
| 366 | void |
| 367 | Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files) |
| 368 | { |
| 369 | m_images.Clear(); |
| 370 | m_scratch_ast_context_ap.reset(); |
| 371 | |
| 372 | if (executable_sp.get()) |
| 373 | { |
| 374 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 375 | "Target::SetExecutableModule (executable = '%s/%s')", |
| 376 | executable_sp->GetFileSpec().GetDirectory().AsCString(), |
| 377 | executable_sp->GetFileSpec().GetFilename().AsCString()); |
| 378 | |
| 379 | m_images.Append(executable_sp); // The first image is our exectuable file |
| 380 | |
| 381 | ArchSpec exe_arch = executable_sp->GetArchitecture(); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 | [diff] [blame] | 382 | // If we haven't set an architecture yet, reset our architecture based on what we found in the executable module. |
| 383 | if (!m_arch_spec.IsValid()) |
| 384 | m_arch_spec = exe_arch; |
| 385 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 386 | FileSpecList dependent_files; |
| 387 | ObjectFile * executable_objfile = executable_sp->GetObjectFile(); |
| 388 | if (executable_objfile == NULL) |
| 389 | { |
| 390 | |
| 391 | FileSpec bundle_executable(executable_sp->GetFileSpec()); |
| 392 | if (Host::ResolveExecutableInBundle (&bundle_executable)) |
| 393 | { |
| 394 | ModuleSP bundle_exe_module_sp(GetSharedModule(bundle_executable, |
| 395 | exe_arch)); |
| 396 | SetExecutableModule (bundle_exe_module_sp, get_dependent_files); |
| 397 | if (bundle_exe_module_sp->GetObjectFile() != NULL) |
| 398 | executable_sp = bundle_exe_module_sp; |
| 399 | return; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (executable_objfile) |
| 404 | { |
| 405 | executable_objfile->GetDependentModules(dependent_files); |
| 406 | for (uint32_t i=0; i<dependent_files.GetSize(); i++) |
| 407 | { |
| 408 | ModuleSP image_module_sp(GetSharedModule(dependent_files.GetFileSpecPointerAtIndex(i), |
| 409 | exe_arch)); |
| 410 | if (image_module_sp.get()) |
| 411 | { |
| 412 | //image_module_sp->Dump(&s);// REMOVE THIS, DEBUG ONLY |
| 413 | ObjectFile *objfile = image_module_sp->GetObjectFile(); |
| 414 | if (objfile) |
| 415 | objfile->GetDependentModules(dependent_files); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // Now see if we know the target triple, and if so, create our scratch AST context: |
| 421 | ConstString target_triple; |
| 422 | if (GetTargetTriple(target_triple)) |
| 423 | { |
| 424 | m_scratch_ast_context_ap.reset (new ClangASTContext(target_triple.GetCString())); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | |
| 430 | ModuleList& |
| 431 | Target::GetImages () |
| 432 | { |
| 433 | return m_images; |
| 434 | } |
| 435 | |
| 436 | ArchSpec |
| 437 | Target::GetArchitecture () const |
| 438 | { |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 | [diff] [blame] | 439 | return m_arch_spec; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 440 | } |
| 441 | |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 | [diff] [blame] | 442 | bool |
| 443 | Target::SetArchitecture (const ArchSpec &arch_spec) |
| 444 | { |
| 445 | if (m_arch_spec == arch_spec) |
| 446 | { |
| 447 | // If we're setting the architecture to our current architecture, we |
| 448 | // don't need to do anything. |
| 449 | return true; |
| 450 | } |
| 451 | else if (!m_arch_spec.IsValid()) |
| 452 | { |
| 453 | // If we haven't got a valid arch spec, then we just need to set it. |
| 454 | m_arch_spec = arch_spec; |
| 455 | return true; |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | // If we have an executable file, try to reset the executable to the desired architecture |
| 460 | m_arch_spec = arch_spec; |
| 461 | ModuleSP executable_sp = GetExecutableModule (); |
| 462 | m_images.Clear(); |
| 463 | m_scratch_ast_context_ap.reset(); |
| 464 | m_triple.Clear(); |
| 465 | // Need to do something about unsetting breakpoints. |
| 466 | |
| 467 | if (executable_sp) |
| 468 | { |
| 469 | FileSpec exec_file_spec = executable_sp->GetFileSpec(); |
| 470 | Error error = ModuleList::GetSharedModule(exec_file_spec, |
| 471 | arch_spec, |
| 472 | NULL, |
| 473 | NULL, |
| 474 | 0, |
| 475 | executable_sp, |
| 476 | NULL, |
| 477 | NULL); |
| 478 | |
| 479 | if (!error.Fail() && executable_sp) |
| 480 | { |
| 481 | SetExecutableModule (executable_sp, true); |
| 482 | return true; |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | return false; |
| 487 | } |
| 488 | } |
| 489 | else |
| 490 | { |
| 491 | return false; |
| 492 | } |
| 493 | } |
| 494 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 495 | |
| 496 | bool |
| 497 | Target::GetTargetTriple(ConstString &triple) |
| 498 | { |
| 499 | triple.Clear(); |
| 500 | |
| 501 | if (m_triple) |
| 502 | { |
| 503 | triple = m_triple; |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | Module *exe_module = GetExecutableModule().get(); |
| 508 | if (exe_module) |
| 509 | { |
| 510 | ObjectFile *objfile = exe_module->GetObjectFile(); |
| 511 | if (objfile) |
| 512 | { |
| 513 | objfile->GetTargetTriple(m_triple); |
| 514 | triple = m_triple; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return !triple.IsEmpty(); |
| 519 | } |
| 520 | |
| 521 | void |
| 522 | Target::ModuleAdded (ModuleSP &module_sp) |
| 523 | { |
| 524 | // A module is being added to this target for the first time |
| 525 | ModuleList module_list; |
| 526 | module_list.Append(module_sp); |
| 527 | ModulesDidLoad (module_list); |
| 528 | } |
| 529 | |
| 530 | void |
| 531 | Target::ModuleUpdated (ModuleSP &old_module_sp, ModuleSP &new_module_sp) |
| 532 | { |
| 533 | // A module is being added to this target for the first time |
| 534 | ModuleList module_list; |
| 535 | module_list.Append (old_module_sp); |
| 536 | ModulesDidUnload (module_list); |
| 537 | module_list.Clear (); |
| 538 | module_list.Append (new_module_sp); |
| 539 | ModulesDidLoad (module_list); |
| 540 | } |
| 541 | |
| 542 | void |
| 543 | Target::ModulesDidLoad (ModuleList &module_list) |
| 544 | { |
| 545 | m_breakpoint_list.UpdateBreakpoints (module_list, true); |
| 546 | // TODO: make event data that packages up the module_list |
| 547 | BroadcastEvent (eBroadcastBitModulesLoaded, NULL); |
| 548 | } |
| 549 | |
| 550 | void |
| 551 | Target::ModulesDidUnload (ModuleList &module_list) |
| 552 | { |
| 553 | m_breakpoint_list.UpdateBreakpoints (module_list, false); |
| 554 | // TODO: make event data that packages up the module_list |
| 555 | BroadcastEvent (eBroadcastBitModulesUnloaded, NULL); |
| 556 | } |
| 557 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 558 | size_t |
Greg Clayton | 35f3dd2 | 2010-06-30 23:04:24 | [diff] [blame] | 559 | Target::ReadMemory (const Address& addr, void *dst, size_t dst_len, Error &error) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 560 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 561 | error.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 562 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 563 | bool process_is_valid = m_process_sp && m_process_sp->IsAlive(); |
| 564 | |
| 565 | Address resolved_addr(addr); |
| 566 | if (!resolved_addr.IsSectionOffset()) |
| 567 | { |
| 568 | if (process_is_valid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 569 | { |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 570 | m_process_sp->ResolveLoadAddress (addr.GetOffset(), resolved_addr); |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | m_images.ResolveFileAddress(addr.GetOffset(), resolved_addr); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | |
| 579 | if (process_is_valid) |
| 580 | { |
| 581 | lldb::addr_t load_addr = resolved_addr.GetLoadAddress(m_process_sp.get()); |
| 582 | if (load_addr == LLDB_INVALID_ADDRESS) |
| 583 | { |
| 584 | if (resolved_addr.GetModule() && resolved_addr.GetModule()->GetFileSpec()) |
| 585 | error.SetErrorStringWithFormat("%s[0x%llx] can't be resolved, %s in not currently loaded.\n", |
| 586 | resolved_addr.GetModule()->GetFileSpec().GetFilename().AsCString(), |
| 587 | resolved_addr.GetFileAddress()); |
| 588 | else |
| 589 | error.SetErrorStringWithFormat("0x%llx can't be resolved.\n", resolved_addr.GetFileAddress()); |
| 590 | } |
| 591 | else |
| 592 | { |
| 593 | size_t bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 594 | if (bytes_read != dst_len) |
| 595 | { |
| 596 | if (error.Success()) |
| 597 | { |
| 598 | if (bytes_read == 0) |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 599 | error.SetErrorStringWithFormat("Read memory from 0x%llx failed.\n", load_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 600 | else |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 601 | error.SetErrorStringWithFormat("Only %zu of %zu bytes were read from memory at 0x%llx.\n", bytes_read, dst_len, load_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 602 | } |
| 603 | } |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 604 | if (bytes_read) |
| 605 | return bytes_read; |
| 606 | // If the address is not section offset we have an address that |
| 607 | // doesn't resolve to any address in any currently loaded shared |
| 608 | // libaries and we failed to read memory so there isn't anything |
| 609 | // more we can do. If it is section offset, we might be able to |
| 610 | // read cached memory from the object file. |
| 611 | if (!resolved_addr.IsSectionOffset()) |
| 612 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 613 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 614 | } |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 | [diff] [blame] | 615 | |
| 616 | const Section *section = resolved_addr.GetSection(); |
| 617 | if (section && section->GetModule()) |
| 618 | { |
| 619 | ObjectFile *objfile = section->GetModule()->GetObjectFile(); |
| 620 | return section->ReadSectionDataFromObjectFile (objfile, |
| 621 | resolved_addr.GetOffset(), |
| 622 | dst, |
| 623 | dst_len); |
| 624 | } |
| 625 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | |
| 629 | ModuleSP |
| 630 | Target::GetSharedModule |
| 631 | ( |
| 632 | const FileSpec& file_spec, |
| 633 | const ArchSpec& arch, |
| 634 | const UUID *uuid_ptr, |
| 635 | const ConstString *object_name, |
| 636 | off_t object_offset, |
| 637 | Error *error_ptr |
| 638 | ) |
| 639 | { |
| 640 | // Don't pass in the UUID so we can tell if we have a stale value in our list |
| 641 | ModuleSP old_module_sp; // This will get filled in if we have a new version of the library |
| 642 | bool did_create_module = false; |
| 643 | ModuleSP module_sp; |
| 644 | |
| 645 | // If there are image search path entries, try to use them first to acquire a suitable image. |
| 646 | |
| 647 | Error error; |
| 648 | |
| 649 | if (m_image_search_paths.GetSize()) |
| 650 | { |
| 651 | FileSpec transformed_spec; |
| 652 | if (m_image_search_paths.RemapPath (file_spec.GetDirectory(), transformed_spec.GetDirectory())) |
| 653 | { |
| 654 | transformed_spec.GetFilename() = file_spec.GetFilename(); |
| 655 | error = ModuleList::GetSharedModule (transformed_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // If a module hasn't been found yet, use the unmodified path. |
| 660 | |
| 661 | if (!module_sp) |
| 662 | { |
| 663 | error = (ModuleList::GetSharedModule (file_spec, arch, uuid_ptr, object_name, object_offset, module_sp, &old_module_sp, &did_create_module)); |
| 664 | } |
| 665 | |
| 666 | if (module_sp) |
| 667 | { |
| 668 | m_images.Append (module_sp); |
| 669 | if (did_create_module) |
| 670 | { |
| 671 | if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32) |
| 672 | ModuleUpdated(old_module_sp, module_sp); |
| 673 | else |
| 674 | ModuleAdded(module_sp); |
| 675 | } |
| 676 | } |
| 677 | if (error_ptr) |
| 678 | *error_ptr = error; |
| 679 | return module_sp; |
| 680 | } |
| 681 | |
| 682 | |
| 683 | Target * |
| 684 | Target::CalculateTarget () |
| 685 | { |
| 686 | return this; |
| 687 | } |
| 688 | |
| 689 | Process * |
| 690 | Target::CalculateProcess () |
| 691 | { |
| 692 | return NULL; |
| 693 | } |
| 694 | |
| 695 | Thread * |
| 696 | Target::CalculateThread () |
| 697 | { |
| 698 | return NULL; |
| 699 | } |
| 700 | |
| 701 | StackFrame * |
| 702 | Target::CalculateStackFrame () |
| 703 | { |
| 704 | return NULL; |
| 705 | } |
| 706 | |
| 707 | void |
| 708 | Target::Calculate (ExecutionContext &exe_ctx) |
| 709 | { |
| 710 | exe_ctx.target = this; |
| 711 | exe_ctx.process = NULL; // Do NOT fill in process... |
| 712 | exe_ctx.thread = NULL; |
| 713 | exe_ctx.frame = NULL; |
| 714 | } |
| 715 | |
| 716 | PathMappingList & |
| 717 | Target::GetImageSearchPathList () |
| 718 | { |
| 719 | return m_image_search_paths; |
| 720 | } |
| 721 | |
| 722 | void |
| 723 | Target::ImageSearchPathsChanged |
| 724 | ( |
| 725 | const PathMappingList &path_list, |
| 726 | void *baton |
| 727 | ) |
| 728 | { |
| 729 | Target *target = (Target *)baton; |
| 730 | if (target->m_images.GetSize() > 1) |
| 731 | { |
| 732 | ModuleSP exe_module_sp (target->GetExecutableModule()); |
| 733 | if (exe_module_sp) |
| 734 | { |
| 735 | target->m_images.Clear(); |
| 736 | target->SetExecutableModule (exe_module_sp, true); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | ClangASTContext * |
| 742 | Target::GetScratchClangASTContext() |
| 743 | { |
| 744 | return m_scratch_ast_context_ap.get(); |
| 745 | } |