124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===-- Module.cpp ----------------------------------------------*- C++ -*-===//
224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//                     The LLVM Compiler Infrastructure
424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// This file is distributed under the University of Illinois Open Source
624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// License. See LICENSE.TXT for details.
724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//
824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===----------------------------------------------------------------------===//
924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
10d891f9b872103235cfd2ed452c6f14a4394d9b3aDaniel Malea#include "lldb/lldb-python.h"
11d891f9b872103235cfd2ed452c6f14a4394d9b3aDaniel Malea
12146d9522c95c0c8c5409539813b55e08b99196eeEnrico Granata#include "lldb/Core/Error.h"
1324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/Module.h"
14b5a8f1498e1ddaeed5187a878d57ea0b74af9c26Greg Clayton#include "lldb/Core/DataBuffer.h"
15b5a8f1498e1ddaeed5187a878d57ea0b74af9c26Greg Clayton#include "lldb/Core/DataBufferHeap.h"
1624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/Log.h"
1724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/ModuleList.h"
1849ce8969d3154e1560106cfe530444c09410f217Greg Clayton#include "lldb/Core/ModuleSpec.h"
1924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/RegularExpression.h"
2049ce8969d3154e1560106cfe530444c09410f217Greg Clayton#include "lldb/Core/Section.h"
2175d8c2591f6c56a09338bf4967a41510165a907eGreg Clayton#include "lldb/Core/StreamString.h"
2224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Core/Timer.h"
23df6dc88322f103b263092d752db4490e628a1cbdGreg Clayton#include "lldb/Host/Host.h"
24146d9522c95c0c8c5409539813b55e08b99196eeEnrico Granata#include "lldb/Host/Symbols.h"
25146d9522c95c0c8c5409539813b55e08b99196eeEnrico Granata#include "lldb/Interpreter/CommandInterpreter.h"
26146d9522c95c0c8c5409539813b55e08b99196eeEnrico Granata#include "lldb/Interpreter/ScriptInterpreter.h"
2724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/lldb-private-log.h"
2849ce8969d3154e1560106cfe530444c09410f217Greg Clayton#include "lldb/Symbol/CompileUnit.h"
2924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Symbol/ObjectFile.h"
3024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Symbol/SymbolContext.h"
3124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "lldb/Symbol/SymbolVendor.h"
32296b06d325413723f5aac5988eed977b278a7807Greg Clayton#include "lldb/Target/CPPLanguageRuntime.h"
33296b06d325413723f5aac5988eed977b278a7807Greg Clayton#include "lldb/Target/ObjCLanguageRuntime.h"
34b5a8f1498e1ddaeed5187a878d57ea0b74af9c26Greg Clayton#include "lldb/Target/Process.h"
35b5a8f1498e1ddaeed5187a878d57ea0b74af9c26Greg Clayton#include "lldb/Target/Target.h"
36a807ceef5dad2b24e5bae5c5a193ff03aa7ec8d9Michael Sartain#include "lldb/Symbol/SymbolFile.h"
3724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb;
3924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerusing namespace lldb_private;
4024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
41899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton// Shared pointers to modules track module lifetimes in
42899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton// targets and in the global module, but this collection
43899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton// will track all module objects that are still alive
44899025fb72c0430e3b02746ca11c9070e9ede951Greg Claytontypedef std::vector<Module *> ModuleCollection;
45899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton
46899025fb72c0430e3b02746ca11c9070e9ede951Greg Claytonstatic ModuleCollection &
47899025fb72c0430e3b02746ca11c9070e9ede951Greg ClaytonGetModuleCollection()
48899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton{
49b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham    // This module collection needs to live past any module, so we could either make it a
50b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham    // shared pointer in each module or just leak is.  Since it is only an empty vector by
51b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham    // the time all the modules have gone away, we just leak it for now.  If we decide this
52b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham    // is a big problem we can introduce a Finalize method that will tear everything down in
53b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham    // a predictable order.
54b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham
55b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham    static ModuleCollection *g_module_collection = NULL;
56b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham    if (g_module_collection == NULL)
57b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham        g_module_collection = new ModuleCollection();
58b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham
59b67c2a2f2a75fdba552a0eda0bf80df2095caa05Jim Ingham    return *g_module_collection;
60899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton}
61899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton
62c149c8b3a88e5c8a94febdf956551e378c531797Greg ClaytonMutex *
63899025fb72c0430e3b02746ca11c9070e9ede951Greg ClaytonModule::GetAllocationModuleCollectionMutex()
64899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton{
65c149c8b3a88e5c8a94febdf956551e378c531797Greg Clayton    // NOTE: The mutex below must be leaked since the global module list in
66c149c8b3a88e5c8a94febdf956551e378c531797Greg Clayton    // the ModuleList class will get torn at some point, and we can't know
67c149c8b3a88e5c8a94febdf956551e378c531797Greg Clayton    // if it will tear itself down before the "g_module_collection_mutex" below
68c149c8b3a88e5c8a94febdf956551e378c531797Greg Clayton    // will. So we leak a Mutex object below to safeguard against that
69c149c8b3a88e5c8a94febdf956551e378c531797Greg Clayton
70c149c8b3a88e5c8a94febdf956551e378c531797Greg Clayton    static Mutex *g_module_collection_mutex = NULL;
71c149c8b3a88e5c8a94febdf956551e378c531797Greg Clayton    if (g_module_collection_mutex == NULL)
72c149c8b3a88e5c8a94febdf956551e378c531797Greg Clayton        g_module_collection_mutex = new Mutex (Mutex::eMutexTypeRecursive); // NOTE: known leak
73c149c8b3a88e5c8a94febdf956551e378c531797Greg Clayton    return g_module_collection_mutex;
74899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton}
75899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton
76899025fb72c0430e3b02746ca11c9070e9ede951Greg Claytonsize_t
77899025fb72c0430e3b02746ca11c9070e9ede951Greg ClaytonModule::GetNumberAllocatedModules ()
78899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton{
79899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    Mutex::Locker locker (GetAllocationModuleCollectionMutex());
80899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    return GetModuleCollection().size();
81899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton}
82899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton
83899025fb72c0430e3b02746ca11c9070e9ede951Greg ClaytonModule *
84899025fb72c0430e3b02746ca11c9070e9ede951Greg ClaytonModule::GetAllocatedModuleAtIndex (size_t idx)
85899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton{
86899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    Mutex::Locker locker (GetAllocationModuleCollectionMutex());
87899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    ModuleCollection &modules = GetModuleCollection();
88899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    if (idx < modules.size())
89899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton        return modules[idx];
90899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    return NULL;
91899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton}
9208205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton#if 0
93899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton
9408205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton// These functions help us to determine if modules are still loaded, yet don't require that
9508205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton// you have a command interpreter and can easily be called from an external debugger.
9608205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Claytonnamespace lldb {
97899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton
9808205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton    void
9908205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton    ClearModuleInfo (void)
10008205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton    {
101860b9ea0038cdcea2dda00b1657f945363c96d00Greg Clayton        const bool mandatory = true;
102860b9ea0038cdcea2dda00b1657f945363c96d00Greg Clayton        ModuleList::RemoveOrphanSharedModules(mandatory);
10308205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton    }
10408205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton
10508205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton    void
10608205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton    DumpModuleInfo (void)
10708205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton    {
10808205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton        Mutex::Locker locker (Module::GetAllocationModuleCollectionMutex());
10908205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton        ModuleCollection &modules = GetModuleCollection();
11008205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton        const size_t count = modules.size();
1115f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea        printf ("%s: %" PRIu64 " modules:\n", __PRETTY_FUNCTION__, (uint64_t)count);
11208205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton        for (size_t i=0; i<count; ++i)
11308205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton        {
11408205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton
11508205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton            StreamString strm;
11608205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton            Module *module = modules[i];
11708205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton            const bool in_shared_module_list = ModuleList::ModuleIsInCache (module);
11808205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton            module->GetDescription(&strm, eDescriptionLevelFull);
11908205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton            printf ("%p: shared = %i, ref_count = %3u, module = %s\n",
12008205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton                    module,
12108205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton                    in_shared_module_list,
12208205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton                    (uint32_t)module->use_count(),
12308205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton                    strm.GetString().c_str());
12408205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton        }
12508205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton    }
12608205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton}
12708205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton
12808205a649cf8a94477f7f9ab8e42dd95ebfc9626Greg Clayton#endif
129f737d372a9672c9feaedf4b2cd7b16e31357d38eGreg Clayton
130444fe998bf707bd076a70c3a779db8575533695eGreg ClaytonModule::Module (const ModuleSpec &module_spec) :
131444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_mutex (Mutex::eMutexTypeRecursive),
132444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_mod_time (module_spec.GetFileSpec().GetModificationTime()),
133444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_arch (module_spec.GetArchitecture()),
134444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_uuid (),
135444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_file (module_spec.GetFileSpec()),
136444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_platform_file(module_spec.GetPlatformFileSpec()),
137444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_symfile_spec (module_spec.GetSymbolFileSpec()),
138444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_object_name (module_spec.GetObjectName()),
139444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_object_offset (module_spec.GetObjectOffset()),
1400e191607adcb0ea8ebd06c278be648a7f5c0097fGreg Clayton    m_object_mod_time (module_spec.GetObjectModificationTime()),
141444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_objfile_sp (),
142444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_symfile_ap (),
143444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_ast (),
144964deba8853eb794e59263322b59b09b43669618Greg Clayton    m_source_mappings (),
145444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_did_load_objfile (false),
146444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_did_load_symbol_vendor (false),
147444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_did_parse_uuid (false),
148444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_did_init_ast (false),
149444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    m_is_dynamic_loader_module (false),
150cbacba1f223ee4819a4b99924dd994c7e52fdf04Greg Clayton    m_file_has_changed (false),
151cbacba1f223ee4819a4b99924dd994c7e52fdf04Greg Clayton    m_first_file_changed_log (false)
152444fe998bf707bd076a70c3a779db8575533695eGreg Clayton{
153444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    // Scope for locker below...
154444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    {
155444fe998bf707bd076a70c3a779db8575533695eGreg Clayton        Mutex::Locker locker (GetAllocationModuleCollectionMutex());
156444fe998bf707bd076a70c3a779db8575533695eGreg Clayton        GetModuleCollection().push_back(this);
157444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    }
158444fe998bf707bd076a70c3a779db8575533695eGreg Clayton
159952e9dc874944fcdbbb224f3ec4fc2c859376f64Greg Clayton    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
160444fe998bf707bd076a70c3a779db8575533695eGreg Clayton    if (log)
16197a19b21dacd9063bb5475812df7781777262198Greg Clayton        log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
162444fe998bf707bd076a70c3a779db8575533695eGreg Clayton                     this,
163444fe998bf707bd076a70c3a779db8575533695eGreg Clayton                     m_arch.GetArchitectureName(),
16497a19b21dacd9063bb5475812df7781777262198Greg Clayton                     m_file.GetPath().c_str(),
165444fe998bf707bd076a70c3a779db8575533695eGreg Clayton                     m_object_name.IsEmpty() ? "" : "(",
166444fe998bf707bd076a70c3a779db8575533695eGreg Clayton                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
167444fe998bf707bd076a70c3a779db8575533695eGreg Clayton                     m_object_name.IsEmpty() ? "" : ")");
168444fe998bf707bd076a70c3a779db8575533695eGreg Clayton}
169444fe998bf707bd076a70c3a779db8575533695eGreg Clayton
1703508c387c3f0c9ecc439d98048fd7694d41bab1bGreg ClaytonModule::Module(const FileSpec& file_spec,
1713508c387c3f0c9ecc439d98048fd7694d41bab1bGreg Clayton               const ArchSpec& arch,
1723508c387c3f0c9ecc439d98048fd7694d41bab1bGreg Clayton               const ConstString *object_name,
1730e191607adcb0ea8ebd06c278be648a7f5c0097fGreg Clayton               off_t object_offset,
1740e191607adcb0ea8ebd06c278be648a7f5c0097fGreg Clayton               const TimeValue *object_mod_time_ptr) :
17524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    m_mutex (Mutex::eMutexTypeRecursive),
17624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    m_mod_time (file_spec.GetModificationTime()),
17724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    m_arch (arch),
17824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    m_uuid (),
17924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    m_file (file_spec),
18024bc5d9bfad2a1c562c27e7cf37e1c56d85c45e7Greg Clayton    m_platform_file(),
1813508c387c3f0c9ecc439d98048fd7694d41bab1bGreg Clayton    m_symfile_spec (),
18224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    m_object_name (),
183b72d0f098e45936fa72e26b1a026c603e17e2d6cGreg Clayton    m_object_offset (object_offset),
1840e191607adcb0ea8ebd06c278be648a7f5c0097fGreg Clayton    m_object_mod_time (),
185e40b6424d9e49306392bec4b44060da36414c382Greg Clayton    m_objfile_sp (),
186236c1c71fab5d56a4dbebaabb969a238f11d142eGreg Clayton    m_symfile_ap (),
187b01000fd063629facd45044f137446fb748ee179Greg Clayton    m_ast (),
188964deba8853eb794e59263322b59b09b43669618Greg Clayton    m_source_mappings (),
189236c1c71fab5d56a4dbebaabb969a238f11d142eGreg Clayton    m_did_load_objfile (false),
190236c1c71fab5d56a4dbebaabb969a238f11d142eGreg Clayton    m_did_load_symbol_vendor (false),
191236c1c71fab5d56a4dbebaabb969a238f11d142eGreg Clayton    m_did_parse_uuid (false),
192b01000fd063629facd45044f137446fb748ee179Greg Clayton    m_did_init_ast (false),
193df6dc88322f103b263092d752db4490e628a1cbdGreg Clayton    m_is_dynamic_loader_module (false),
194cbacba1f223ee4819a4b99924dd994c7e52fdf04Greg Clayton    m_file_has_changed (false),
195cbacba1f223ee4819a4b99924dd994c7e52fdf04Greg Clayton    m_first_file_changed_log (false)
19624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
197899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    // Scope for locker below...
198899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    {
199899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton        Mutex::Locker locker (GetAllocationModuleCollectionMutex());
200899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton        GetModuleCollection().push_back(this);
201899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    }
202899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton
20324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    if (object_name)
20424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        m_object_name = *object_name;
2050e191607adcb0ea8ebd06c278be648a7f5c0097fGreg Clayton
2060e191607adcb0ea8ebd06c278be648a7f5c0097fGreg Clayton    if (object_mod_time_ptr)
2070e191607adcb0ea8ebd06c278be648a7f5c0097fGreg Clayton        m_object_mod_time = *object_mod_time_ptr;
2080e191607adcb0ea8ebd06c278be648a7f5c0097fGreg Clayton
209952e9dc874944fcdbbb224f3ec4fc2c859376f64Greg Clayton    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
21024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    if (log)
21197a19b21dacd9063bb5475812df7781777262198Greg Clayton        log->Printf ("%p Module::Module((%s) '%s%s%s%s')",
21224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                     this,
213940b103224f3062578c7a7e6e76d8bf4a7956f2aGreg Clayton                     m_arch.GetArchitectureName(),
21497a19b21dacd9063bb5475812df7781777262198Greg Clayton                     m_file.GetPath().c_str(),
21524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                     m_object_name.IsEmpty() ? "" : "(",
21624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
21724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                     m_object_name.IsEmpty() ? "" : ")");
21824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
21924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
22024943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerModule::~Module()
22124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
222a8aa5aba2497ba4e49295b6c2acb68975ed8406eGreg Clayton    // Lock our module down while we tear everything down to make sure
223a8aa5aba2497ba4e49295b6c2acb68975ed8406eGreg Clayton    // we don't get any access to the module while it is being destroyed
224a8aa5aba2497ba4e49295b6c2acb68975ed8406eGreg Clayton    Mutex::Locker locker (m_mutex);
225899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    // Scope for locker below...
226899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    {
227899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton        Mutex::Locker locker (GetAllocationModuleCollectionMutex());
228899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton        ModuleCollection &modules = GetModuleCollection();
229899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton        ModuleCollection::iterator end = modules.end();
230899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton        ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
231f737d372a9672c9feaedf4b2cd7b16e31357d38eGreg Clayton        assert (pos != end);
232f737d372a9672c9feaedf4b2cd7b16e31357d38eGreg Clayton        modules.erase(pos);
233899025fb72c0430e3b02746ca11c9070e9ede951Greg Clayton    }
234952e9dc874944fcdbbb224f3ec4fc2c859376f64Greg Clayton    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT|LIBLLDB_LOG_MODULES));
23524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    if (log)
23697a19b21dacd9063bb5475812df7781777262198Greg Clayton        log->Printf ("%p Module::~Module((%s) '%s%s%s%s')",
23724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                     this,
238940b103224f3062578c7a7e6e76d8bf4a7956f2aGreg Clayton                     m_arch.GetArchitectureName(),
23997a19b21dacd9063bb5475812df7781777262198Greg Clayton                     m_file.GetPath().c_str(),
24024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                     m_object_name.IsEmpty() ? "" : "(",
24124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
24224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                     m_object_name.IsEmpty() ? "" : ")");
243b01000fd063629facd45044f137446fb748ee179Greg Clayton    // Release any auto pointers before we start tearing down our member
244b01000fd063629facd45044f137446fb748ee179Greg Clayton    // variables since the object file and symbol files might need to make
245b01000fd063629facd45044f137446fb748ee179Greg Clayton    // function calls back into this module object. The ordering is important
246b01000fd063629facd45044f137446fb748ee179Greg Clayton    // here because symbol files can require the module object file. So we tear
247b01000fd063629facd45044f137446fb748ee179Greg Clayton    // down the symbol file first, then the object file.
2487940069905bee0b2e5f0661bf37c9f906ddf8603Greg Clayton    m_sections_ap.reset();
249b01000fd063629facd45044f137446fb748ee179Greg Clayton    m_symfile_ap.reset();
250e40b6424d9e49306392bec4b44060da36414c382Greg Clayton    m_objfile_sp.reset();
25124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
25224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
2536c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg ClaytonObjectFile *
2546c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg ClaytonModule::GetMemoryObjectFile (const lldb::ProcessSP &process_sp, lldb::addr_t header_addr, Error &error)
2556c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton{
2566c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton    if (m_objfile_sp)
2576c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton    {
2586c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton        error.SetErrorString ("object file already exists");
2596c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton    }
2606c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton    else
2616c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton    {
2626c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton        Mutex::Locker locker (m_mutex);
2636c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton        if (process_sp)
2646c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton        {
2656c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            m_did_load_objfile = true;
266102b2c2681c9a830afe25bfea35557421905e42cGreg Clayton            std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (512, 0));
2676c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            Error readmem_error;
2686c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            const size_t bytes_read = process_sp->ReadMemory (header_addr,
2696c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                                                              data_ap->GetBytes(),
2706c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                                                              data_ap->GetByteSize(),
2716c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                                                              readmem_error);
2726c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            if (bytes_read == 512)
2736c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            {
2746c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                DataBufferSP data_sp(data_ap.release());
2756c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr, data_sp);
2766c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                if (m_objfile_sp)
2776c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                {
278808e92a12cf2b15ed5cb75e63de4ae0cbe997998Greg Clayton                    StreamString s;
2795f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea                    s.Printf("0x%16.16" PRIx64, header_addr);
280808e92a12cf2b15ed5cb75e63de4ae0cbe997998Greg Clayton                    m_object_name.SetCString (s.GetData());
281808e92a12cf2b15ed5cb75e63de4ae0cbe997998Greg Clayton
282808e92a12cf2b15ed5cb75e63de4ae0cbe997998Greg Clayton                    // Once we get the object file, update our module with the object file's
2836c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                    // architecture since it might differ in vendor/os if some parts were
2846c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                    // unknown.
2856c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                    m_objfile_sp->GetArchitecture (m_arch);
2866c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                }
2876c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                else
2886c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                {
2896c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                    error.SetErrorString ("unable to find suitable object file plug-in");
2906c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                }
2916c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            }
2926c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            else
2936c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            {
2946c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton                error.SetErrorStringWithFormat ("unable to read header from memory: %s", readmem_error.AsCString());
2956c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            }
2966c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton        }
2976c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton        else
2986c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton        {
2996c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton            error.SetErrorString ("invalid process");
3006c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton        }
3016c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton    }
3026c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton    return m_objfile_sp.get();
3036c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton}
3046c5438bf1d7af0f0ef1e81a30ad53e3fe93c35a8Greg Clayton
30524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
3060467c78e9a75eff9ec33d3c1f39fa83e1c5d9241Greg Claytonconst lldb_private::UUID&
30724943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerModule::GetUUID()
30824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
30924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Mutex::Locker locker (m_mutex);
310236c1c71fab5d56a4dbebaabb969a238f11d142eGreg Clayton    if (m_did_parse_uuid == false)
31124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
31224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        ObjectFile * obj_file = GetObjectFile ();
31324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
31424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (obj_file != NULL)
31524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
31624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            obj_file->GetUUID(&m_uuid);
317236c1c71fab5d56a4dbebaabb969a238f11d142eGreg Clayton            m_did_parse_uuid = true;
31824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
31924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
32024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return m_uuid;
32124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
32224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
323b01000fd063629facd45044f137446fb748ee179Greg ClaytonClangASTContext &
324b01000fd063629facd45044f137446fb748ee179Greg ClaytonModule::GetClangASTContext ()
325b01000fd063629facd45044f137446fb748ee179Greg Clayton{
326b01000fd063629facd45044f137446fb748ee179Greg Clayton    Mutex::Locker locker (m_mutex);
327b01000fd063629facd45044f137446fb748ee179Greg Clayton    if (m_did_init_ast == false)
328b01000fd063629facd45044f137446fb748ee179Greg Clayton    {
329b01000fd063629facd45044f137446fb748ee179Greg Clayton        ObjectFile * objfile = GetObjectFile();
330395fc33dc4b06c048ed35047ec461bc092ef2df3Greg Clayton        ArchSpec object_arch;
331395fc33dc4b06c048ed35047ec461bc092ef2df3Greg Clayton        if (objfile && objfile->GetArchitecture(object_arch))
332b01000fd063629facd45044f137446fb748ee179Greg Clayton        {
333b01000fd063629facd45044f137446fb748ee179Greg Clayton            m_did_init_ast = true;
3341b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda
3351b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda            // LLVM wants this to be set to iOS or MacOSX; if we're working on
3361b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda            // a bare-boards type image, change the triple for llvm's benefit.
3371b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda            if (object_arch.GetTriple().getVendor() == llvm::Triple::Apple
3381b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                && object_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
3391b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda            {
3401b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                if (object_arch.GetTriple().getArch() == llvm::Triple::arm ||
3411b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                    object_arch.GetTriple().getArch() == llvm::Triple::thumb)
3421b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                {
3431b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                    object_arch.GetTriple().setOS(llvm::Triple::IOS);
3441b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                }
3451b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                else
3461b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                {
3471b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                    object_arch.GetTriple().setOS(llvm::Triple::MacOSX);
3481b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda                }
3491b97a65e6ad77d797316a2f4b0aa3a1a793ec36fJason Molenda            }
350395fc33dc4b06c048ed35047ec461bc092ef2df3Greg Clayton            m_ast.SetArchitecture (object_arch);
351b01000fd063629facd45044f137446fb748ee179Greg Clayton        }
352b01000fd063629facd45044f137446fb748ee179Greg Clayton    }
353b01000fd063629facd45044f137446fb748ee179Greg Clayton    return m_ast;
354b01000fd063629facd45044f137446fb748ee179Greg Clayton}
355b01000fd063629facd45044f137446fb748ee179Greg Clayton
35624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnervoid
35724943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerModule::ParseAllDebugSymbols()
35824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
35924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Mutex::Locker locker (m_mutex);
36036da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton    size_t num_comp_units = GetNumCompileUnits();
36124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    if (num_comp_units == 0)
36224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        return;
36324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
36402e210cad27e36108bef493b798c2c1eb3b04cb8Greg Clayton    SymbolContext sc;
36513d24fb1817faa7ccc4cfd799113ba1a2b8968ebGreg Clayton    sc.module_sp = shared_from_this();
36624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    SymbolVendor *symbols = GetSymbolVendor ();
36724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
36836da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton    for (size_t cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
36924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
37024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
37124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (sc.comp_unit)
37224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
37324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            sc.function = NULL;
37424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            symbols->ParseVariablesForContext(sc);
37524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
37624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            symbols->ParseCompileUnitFunctions(sc);
37724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
37836da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton            for (size_t func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
37924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
38024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                symbols->ParseFunctionBlocks(sc);
38124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
38224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                // Parse the variables for this function and all its blocks
38324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                symbols->ParseVariablesForContext(sc);
38424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
38524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
38624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
38724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            // Parse all types for this compile unit
38824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            sc.function = NULL;
38924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            symbols->ParseTypes(sc);
39024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
39124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
39224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
39324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
39424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnervoid
39524943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerModule::CalculateSymbolContext(SymbolContext* sc)
39624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
39713d24fb1817faa7ccc4cfd799113ba1a2b8968ebGreg Clayton    sc->module_sp = shared_from_this();
39824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
39924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
4003508c387c3f0c9ecc439d98048fd7694d41bab1bGreg ClaytonModuleSP
401c51ffbf896e398ada5f7e89b2fa5aec6f2224f09Greg ClaytonModule::CalculateSymbolContextModule ()
402c51ffbf896e398ada5f7e89b2fa5aec6f2224f09Greg Clayton{
4033508c387c3f0c9ecc439d98048fd7694d41bab1bGreg Clayton    return shared_from_this();
404c51ffbf896e398ada5f7e89b2fa5aec6f2224f09Greg Clayton}
405c51ffbf896e398ada5f7e89b2fa5aec6f2224f09Greg Clayton
40624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnervoid
40724943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerModule::DumpSymbolContext(Stream *s)
40824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
4097e5fa7fc1f8efd24c078e063b2c4b5e13ba5be20Jason Molenda    s->Printf(", Module{%p}", this);
41024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
41124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
41236da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Claytonsize_t
41324943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerModule::GetNumCompileUnits()
41424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
41524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Mutex::Locker locker (m_mutex);
41624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
41724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    SymbolVendor *symbols = GetSymbolVendor ();
41824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    if (symbols)
41924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        return symbols->GetNumCompileUnits();
42024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return 0;
42124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
42224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
42324943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerCompUnitSP
42436da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg ClaytonModule::GetCompileUnitAtIndex (size_t index)
42524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
42624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Mutex::Locker locker (m_mutex);
42736da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Clayton    size_t num_comp_units = GetNumCompileUnits ();
42824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    CompUnitSP cu_sp;
42924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
43024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    if (index < num_comp_units)
43124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
43224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        SymbolVendor *symbols = GetSymbolVendor ();
43324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (symbols)
43424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            cu_sp = symbols->GetCompileUnitAtIndex(index);
43524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
43624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return cu_sp;
43724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
43824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
43924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnerbool
44024943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerModule::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
44124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
44224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Mutex::Locker locker (m_mutex);
4435f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea    Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%" PRIx64 ")", vm_addr);
4447940069905bee0b2e5f0661bf37c9f906ddf8603Greg Clayton    SectionList *section_list = GetSectionList();
4457940069905bee0b2e5f0661bf37c9f906ddf8603Greg Clayton    if (section_list)
4467940069905bee0b2e5f0661bf37c9f906ddf8603Greg Clayton        return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
44724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return false;
44824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
44924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
45024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattneruint32_t
45124943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerModule::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
45224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
45324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Mutex::Locker locker (m_mutex);
45424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    uint32_t resolved_flags = 0;
45524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
456a7e864cb0a450c7ef65b6f9f6c9bae839c405906Greg Clayton    // Clear the result symbol context in case we don't find anything, but don't clear the target
457a7e864cb0a450c7ef65b6f9f6c9bae839c405906Greg Clayton    sc.Clear(false);
45824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
45924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    // Get the section from the section/offset address.
4603508c387c3f0c9ecc439d98048fd7694d41bab1bGreg Clayton    SectionSP section_sp (so_addr.GetSection());
46124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
46224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    // Make sure the section matches this module before we try and match anything
4633508c387c3f0c9ecc439d98048fd7694d41bab1bGreg Clayton    if (section_sp && section_sp->GetModule().get() == this)
46424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    {
46524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        // If the section offset based address resolved itself, then this
46624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        // is the right module.
46713d24fb1817faa7ccc4cfd799113ba1a2b8968ebGreg Clayton        sc.module_sp = shared_from_this();
46824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        resolved_flags |= eSymbolContextModule;
46924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
47024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        // Resolve the compile unit, function, block, line table or line
47124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        // entry if requested.
47224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        if (resolve_scope & eSymbolContextCompUnit    ||
47324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            resolve_scope & eSymbolContextFunction    ||
47424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            resolve_scope & eSymbolContextBlock       ||
47524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            resolve_scope & eSymbolContextLineEntry   )
47624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
47724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            SymbolVendor *symbols = GetSymbolVendor ();
47824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            if (symbols)
47924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
48024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
48124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
482bddb78944a140c3ca1324d356aa83495331ff50dJim Ingham        // Resolve the symbol if requested, but don't re-look it up if we've already found it.
483bddb78944a140c3ca1324d356aa83495331ff50dJim Ingham        if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
48424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        {
485a807ceef5dad2b24e5bae5c5a193ff03aa7ec8d9Michael Sartain            SymbolVendor* sym_vendor = GetSymbolVendor();
486a807ceef5dad2b24e5bae5c5a193ff03aa7ec8d9Michael Sartain            if (sym_vendor)
48724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            {
488a807ceef5dad2b24e5bae5c5a193ff03aa7ec8d9Michael Sartain                Symtab *symtab = sym_vendor->GetSymtab();
48924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                if (symtab)
49024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                {
49124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    if (so_addr.IsSectionOffset())
49224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    {
49324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
49424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                        if (sc.symbol)
49524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                            resolved_flags |= eSymbolContextSymbol;
49624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                    }
49724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                }
49824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner            }
49924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        }
50024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    }
50124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return resolved_flags;
50224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
50324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
50424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattneruint32_t
505537a7a86687683fd403ce652d178fbc89e06ef9fGreg ClaytonModule::ResolveSymbolContextForFilePath
506537a7a86687683fd403ce652d178fbc89e06ef9fGreg Clayton(
507537a7a86687683fd403ce652d178fbc89e06ef9fGreg Clayton    const char *file_path,
508537a7a86687683fd403ce652d178fbc89e06ef9fGreg Clayton    uint32_t line,
509537a7a86687683fd403ce652d178fbc89e06ef9fGreg Clayton    bool check_inlines,
510537a7a86687683fd403ce652d178fbc89e06ef9fGreg Clayton    uint32_t resolve_scope,
511537a7a86687683fd403ce652d178fbc89e06ef9fGreg Clayton    SymbolContextList& sc_list
512537a7a86687683fd403ce652d178fbc89e06ef9fGreg Clayton)
51324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
514537a7a86687683fd403ce652d178fbc89e06ef9fGreg Clayton    FileSpec file_spec(file_path, false);
51524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
51624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
51724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
51824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattneruint32_t
51924943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerModule::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
52024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{
52124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Mutex::Locker locker (m_mutex);
52224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    Timer scoped_timer(__PRETTY_FUNCTION__,
52397a19b21dacd9063bb5475812df7781777262198Greg Clayton                       "Module::ResolveSymbolContextForFilePath (%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
52497a19b21dacd9063bb5475812df7781777262198Greg Clayton                       file_spec.GetPath().c_str(),
52524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                       line,
52624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                       check_inlines ? "yes" : "no",
52724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner                       resolve_scope);
52824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
52924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    const uint32_t initial_count = sc_list.GetSize();
53024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
53124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    SymbolVendor *symbols = GetSymbolVendor  ();
53224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    if (symbols)
53324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner        symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
53424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
53524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner    return sc_list.GetSize() - initial_count;
53624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner}
53724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
53824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner
53936da2aa6dc5ad9994b638ed09eb81c44cc05540bGreg Claytonsize_t
540