Module.cpp revision a334c9d5f5a563bb55b424173c5c52b9295c11ba
128c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar//===-- Module.cpp ----------------------------------------------*- C++ -*-===//
2fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar//
3fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar//                     The LLVM Compiler Infrastructure
4fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar//
5fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar// This file is distributed under the University of Illinois Open Source
6fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar// License. See LICENSE.TXT for details.
7fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar//
8fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar//===----------------------------------------------------------------------===//
9fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar
1028c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar#include "lldb/Core/Module.h"
1128c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar#include "lldb/Core/Log.h"
12fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar#include "lldb/Core/ModuleList.h"
13fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar#include "lldb/Core/RegularExpression.h"
148b67f774e9c38b7718b2b300b628388f966df4e0Chandler Carruth#include "lldb/Core/StreamString.h"
15fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar#include "lldb/Core/Timer.h"
16fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar#include "lldb/Host/Host.h"
17684c593d05db0bd277268fc9d8c05bce138c745aChris Lattner#include "lldb/lldb-private-log.h"
18fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar#include "lldb/Symbol/ObjectFile.h"
19fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar#include "lldb/Symbol/SymbolContext.h"
2015d170709608e2f1efcada74c297c10c8c71fdcfDaniel Dunbar#include "lldb/Symbol/SymbolVendor.h"
2187392fde1f261fea161b48886fafbedddb18dcceDaniel Dunbar
224f3e7aa154577c86791908e73a9fec075fdea0baChris Lattnerusing namespace lldb;
23fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbarusing namespace lldb_private;
2428c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar
259643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar// Shared pointers to modules track module lifetimes in
2628c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar// targets and in the global module, but this collection
27fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar// will track all module objects that are still alive
2828c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbartypedef std::vector<Module *> ModuleCollection;
291aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar
301aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbarstatic ModuleCollection &
311aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel DunbarGetModuleCollection()
325d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner{
335d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner    // This module collection needs to live past any module, so we could either make it a
34fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    // shared pointer in each module or just leak is.  Since it is only an empty vector by
359643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // the time all the modules have gone away, we just leak it for now.  If we decide this
36fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    // is a big problem we can introduce a Finalize method that will tear everything down in
3728c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    // a predictable order.
389643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
399643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    static ModuleCollection *g_module_collection = NULL;
409643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (g_module_collection == NULL)
419643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        g_module_collection = new ModuleCollection();
42fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar
43159f527cb269002de85e671023b9231a2c8792e9Dan Gohman    return *g_module_collection;
449643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar}
45fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar
469643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarMutex &
479643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule::GetAllocationModuleCollectionMutex()
48fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar{
4928c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    static Mutex g_module_collection_mutex(Mutex::eMutexTypeRecursive);
50fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    return g_module_collection_mutex;
519643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar}
5287392fde1f261fea161b48886fafbedddb18dcceDaniel Dunbar
5387392fde1f261fea161b48886fafbedddb18dcceDaniel Dunbarsize_t
5487392fde1f261fea161b48886fafbedddb18dcceDaniel DunbarModule::GetNumberAllocatedModules ()
558cb9a3b13f3226b7e741768b69d26ecd6b5231f1Chris Lattner{
5687392fde1f261fea161b48886fafbedddb18dcceDaniel Dunbar    Mutex::Locker locker (GetAllocationModuleCollectionMutex());
5787392fde1f261fea161b48886fafbedddb18dcceDaniel Dunbar    return GetModuleCollection().size();
5887392fde1f261fea161b48886fafbedddb18dcceDaniel Dunbar}
599643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
609643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule *
619643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule::GetAllocatedModuleAtIndex (size_t idx)
62fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar{
63fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    Mutex::Locker locker (GetAllocationModuleCollectionMutex());
6415d170709608e2f1efcada74c297c10c8c71fdcfDaniel Dunbar    ModuleCollection &modules = GetModuleCollection();
65fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    if (idx < modules.size())
66e00b011e6a2597fcc3da88da91a8ffda6eebfcdaDaniel Dunbar        return modules[idx];
67fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    return NULL;
6815d170709608e2f1efcada74c297c10c8c71fdcfDaniel Dunbar}
691aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar
7015d170709608e2f1efcada74c297c10c8c71fdcfDaniel Dunbar
7115d170709608e2f1efcada74c297c10c8c71fdcfDaniel Dunbar
7215d170709608e2f1efcada74c297c10c8c71fdcfDaniel Dunbar
73e00b011e6a2597fcc3da88da91a8ffda6eebfcdaDaniel DunbarModule::Module(const FileSpec& file_spec, const ArchSpec& arch, const ConstString *object_name, off_t object_offset) :
7415d170709608e2f1efcada74c297c10c8c71fdcfDaniel Dunbar    m_mutex (Mutex::eMutexTypeRecursive),
759643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    m_mod_time (file_spec.GetModificationTime()),
769643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    m_arch (arch),
7728c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    m_uuid (),
78fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    m_file (file_spec),
798cb9a3b13f3226b7e741768b69d26ecd6b5231f1Chris Lattner    m_platform_file(),
808cb9a3b13f3226b7e741768b69d26ecd6b5231f1Chris Lattner    m_object_name (),
818cb9a3b13f3226b7e741768b69d26ecd6b5231f1Chris Lattner    m_object_offset (object_offset),
828cb9a3b13f3226b7e741768b69d26ecd6b5231f1Chris Lattner    m_objfile_sp (),
838cb9a3b13f3226b7e741768b69d26ecd6b5231f1Chris Lattner    m_symfile_ap (),
84fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    m_ast (),
8528c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    m_did_load_objfile (false),
8628c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    m_did_load_symbol_vendor (false),
87fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    m_did_parse_uuid (false),
88fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    m_did_init_ast (false),
89159f527cb269002de85e671023b9231a2c8792e9Dan Gohman    m_is_dynamic_loader_module (false),
9028c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    m_was_modified (false)
919643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar{
929643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // Scope for locker below...
939643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    {
949643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        Mutex::Locker locker (GetAllocationModuleCollectionMutex());
959643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        GetModuleCollection().push_back(this);
969643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    }
979643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
989643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (object_name)
999643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        m_object_name = *object_name;
1009643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
1019643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (log)
102fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar        log->Printf ("%p Module::Module((%s) '%s/%s%s%s%s')",
103fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar                     this,
1049643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                     m_arch.GetArchitectureName(),
1059643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                     m_file.GetDirectory().AsCString(""),
1069643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                     m_file.GetFilename().AsCString(""),
1079643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                     m_object_name.IsEmpty() ? "" : "(",
108fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
10928c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar                     m_object_name.IsEmpty() ? "" : ")");
110fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar}
111fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar
11228c251b54b0b311749f07babe0f6909e71e877bcDaniel DunbarModule::~Module()
1131aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar{
1141aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar    // Scope for locker below...
1151aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar    {
1161aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar        Mutex::Locker locker (GetAllocationModuleCollectionMutex());
1171aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar        ModuleCollection &modules = GetModuleCollection();
11828c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar        ModuleCollection::iterator end = modules.end();
1199643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
120fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar        if (pos != end)
121159f527cb269002de85e671023b9231a2c8792e9Dan Gohman            modules.erase(pos);
12228c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    }
123fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
1249643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (log)
1259643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        log->Printf ("%p Module::~Module((%s) '%s/%s%s%s%s')",
1269643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                     this,
1279643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                     m_arch.GetArchitectureName(),
1289643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                     m_file.GetDirectory().AsCString(""),
1292928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar                     m_file.GetFilename().AsCString(""),
1307eb85194f2b07bc7ba3f274fc00dc389b77b63bfDaniel Dunbar                     m_object_name.IsEmpty() ? "" : "(",
1319643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                     m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
1329643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                     m_object_name.IsEmpty() ? "" : ")");
1339643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // Release any auto pointers before we start tearing down our member
1349643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // variables since the object file and symbol files might need to make
1359643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // function calls back into this module object. The ordering is important
1369643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // here because symbol files can require the module object file. So we tear
1379643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // down the symbol file first, then the object file.
1389643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    m_symfile_ap.reset();
1399643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    m_objfile_sp.reset();
1409643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar}
141fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar
14228c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar
143fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbarconst lldb_private::UUID&
144fc6877aec9826fa830204d49eba7fac7412b841eDaniel DunbarModule::GetUUID()
14528c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar{
14628c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    Mutex::Locker locker (m_mutex);
147fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    if (m_did_parse_uuid == false)
148fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    {
1491aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar        ObjectFile * obj_file = GetObjectFile ();
1501aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar
1511aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar        if (obj_file != NULL)
1521aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar        {
153fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar            obj_file->GetUUID(&m_uuid);
154fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar            m_did_parse_uuid = true;
155fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar        }
156fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    }
1579643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    return m_uuid;
158fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar}
1599643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
16028c251b54b0b311749f07babe0f6909e71e877bcDaniel DunbarClangASTContext &
1619643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule::GetClangASTContext ()
1629643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar{
1639643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    Mutex::Locker locker (m_mutex);
1649643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (m_did_init_ast == false)
1659643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    {
1669643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        ObjectFile * objfile = GetObjectFile();
1679643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        ArchSpec object_arch;
1689643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        if (objfile && objfile->GetArchitecture(object_arch))
1699643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        {
1709643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            m_did_init_ast = true;
1719643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            m_ast.SetArchitecture (object_arch);
1729643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        }
1739643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    }
1749643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    return m_ast;
1759643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar}
176fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar
1779643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbarvoid
1789643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule::ParseAllDebugSymbols()
1799643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar{
1809643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    Mutex::Locker locker (m_mutex);
1819643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    uint32_t num_comp_units = GetNumCompileUnits();
1829643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (num_comp_units == 0)
1839643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        return;
184fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar
1859643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    SymbolContext sc;
186fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    sc.module_sp = this;
187fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    uint32_t cu_idx;
1889643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    SymbolVendor *symbols = GetSymbolVendor ();
1899643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
190fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    for (cu_idx = 0; cu_idx < num_comp_units; cu_idx++)
1919643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    {
1929643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
1939643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        if (sc.comp_unit)
1949643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        {
195fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar            sc.function = NULL;
19628c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar            symbols->ParseVariablesForContext(sc);
197fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar
198fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar            symbols->ParseCompileUnitFunctions(sc);
19928c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar
20028c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar            uint32_t func_idx;
201fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar            for (func_idx = 0; (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) != NULL; ++func_idx)
202fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar            {
2031aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar                symbols->ParseFunctionBlocks(sc);
2041aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar
205036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman                // Parse the variables for this function and all its blocks
2061aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar                symbols->ParseVariablesForContext(sc);
207036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman            }
208036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman
209036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman
210036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman            // Parse all types for this compile unit
2111aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar            sc.function = NULL;
2121aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar            symbols->ParseTypes(sc);
213036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman        }
214036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman    }
215036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman}
216036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman
217036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohmanvoid
2181aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel DunbarModule::CalculateSymbolContext(SymbolContext* sc)
2191aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar{
2201aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar    sc->module_sp = this;
221036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman}
222036c130e90eb5c93b0dc0a70ad07b9343623c2a8Dan Gohman
2231aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel DunbarModule *
2241aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel DunbarModule::CalculateSymbolContextModule ()
225fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar{
226fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    return this;
227fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar}
228fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar
2299643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbarvoid
230fc6877aec9826fa830204d49eba7fac7412b841eDaniel DunbarModule::DumpSymbolContext(Stream *s)
2319643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar{
23228c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    s->Printf(", Module{%p}", this);
2339643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar}
2349643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
2359643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbaruint32_t
2369643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule::GetNumCompileUnits()
2379643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar{
2389643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    Mutex::Locker locker (m_mutex);
2399643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    Timer scoped_timer(__PRETTY_FUNCTION__, "Module::GetNumCompileUnits (module = %p)", this);
2409643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    SymbolVendor *symbols = GetSymbolVendor ();
2419643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (symbols)
2429643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        return symbols->GetNumCompileUnits();
2439643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    return 0;
2449643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar}
2459643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
2469643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarCompUnitSP
2479643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule::GetCompileUnitAtIndex (uint32_t index)
2489643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar{
2499643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    Mutex::Locker locker (m_mutex);
2509643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    uint32_t num_comp_units = GetNumCompileUnits ();
2519643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    CompUnitSP cu_sp;
2529643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
2539643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (index < num_comp_units)
2549643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    {
2559643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        SymbolVendor *symbols = GetSymbolVendor ();
2569643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        if (symbols)
2579643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            cu_sp = symbols->GetCompileUnitAtIndex(index);
2589643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    }
2599643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    return cu_sp;
2609643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar}
2619643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
2629643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbarbool
2639643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr)
2649643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar{
2659643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    Mutex::Locker locker (m_mutex);
2669643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    Timer scoped_timer(__PRETTY_FUNCTION__, "Module::ResolveFileAddress (vm_addr = 0x%llx)", vm_addr);
267fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    ObjectFile* ofile = GetObjectFile();
2689643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (ofile)
2699643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        return so_addr.ResolveAddressUsingFileSections(vm_addr, ofile->GetSectionList());
2709643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    return false;
2719643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar}
2729643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
2739643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbaruint32_t
2749643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2759643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar{
2769643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    Mutex::Locker locker (m_mutex);
2779643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    uint32_t resolved_flags = 0;
2789643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
2799643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // Clear the result symbol context in case we don't find anything
2809643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    sc.Clear();
2819643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
2829643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // Get the section from the section/offset address.
2839643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    const Section *section = so_addr.GetSection();
2849643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
2859643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    // Make sure the section matches this module before we try and match anything
2869643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    if (section && section->GetModule() == this)
2879643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    {
2889643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        // If the section offset based address resolved itself, then this
2899643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        // is the right module.
2909643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        sc.module_sp = this;
2919643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        resolved_flags |= eSymbolContextModule;
2929643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
2939643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        // Resolve the compile unit, function, block, line table or line
2949643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        // entry if requested.
2959643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        if (resolve_scope & eSymbolContextCompUnit    ||
2969643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            resolve_scope & eSymbolContextFunction    ||
2979643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            resolve_scope & eSymbolContextBlock       ||
2989643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            resolve_scope & eSymbolContextLineEntry   )
2999643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        {
3009643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            SymbolVendor *symbols = GetSymbolVendor ();
3019643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            if (symbols)
3029643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                resolved_flags |= symbols->ResolveSymbolContext (so_addr, resolve_scope, sc);
3039643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        }
3049643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
3059643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        // Resolve the symbol if requested, but don't re-look it up if we've already found it.
3069643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        if (resolve_scope & eSymbolContextSymbol && !(resolved_flags & eSymbolContextSymbol))
3079643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar        {
3089643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            ObjectFile* ofile = GetObjectFile();
3099643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            if (ofile)
3109643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            {
3119643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                Symtab *symtab = ofile->GetSymtab();
3129643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                if (symtab)
3139643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                {
3149643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                    if (so_addr.IsSectionOffset())
3159643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                    {
316fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar                        sc.symbol = symtab->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
3179643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar                        if (sc.symbol)
318fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar                            resolved_flags |= eSymbolContextSymbol;
319fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar                    }
3201aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar                }
3219643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar            }
3221aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar        }
3231aa14aac4198bca0f44e4adad42bf6238cbf9757Daniel Dunbar    }
3249643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar    return resolved_flags;
3259643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar}
3269643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar
327fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbaruint32_t
3289643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel DunbarModule::ResolveSymbolContextForFilePath
3299643ac55142d40da404caa8e5fedfef2cd7b4afcDaniel Dunbar(
330fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    const char *file_path,
33128c251b54b0b311749f07babe0f6909e71e877bcDaniel Dunbar    uint32_t line,
332fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    bool check_inlines,
333fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    uint32_t resolve_scope,
3345d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner    SymbolContextList& sc_list
3355d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner)
3365d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner{
3375d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner    FileSpec file_spec(file_path, false);
3385d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner    return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list);
3395d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner}
340df9c4380ee7e60c1de5cae32685b113170b1faa2Chris Lattner
3415d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattneruint32_t
3425d917a8952c09a345180ec36f0df4ee5dd5eddeaChris LattnerModule::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
343e3e7a369f20af66a96830d8bfe52668c9e2e1fa1Chris Lattner{
3445d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner    Mutex::Locker locker (m_mutex);
3455d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner    Timer scoped_timer(__PRETTY_FUNCTION__,
3465d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner                       "Module::ResolveSymbolContextForFilePath (%s%s%s:%u, check_inlines = %s, resolve_scope = 0x%8.8x)",
3475d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner                       file_spec.GetDirectory().AsCString(""),
3485d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner                       file_spec.GetDirectory() ? "/" : "",
3495d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner                       file_spec.GetFilename().AsCString(""),
3505d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner                       line,
3515d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner                       check_inlines ? "yes" : "no",
3525d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner                       resolve_scope);
3535d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner
3545d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner    const uint32_t initial_count = sc_list.GetSize();
3555d917a8952c09a345180ec36f0df4ee5dd5eddeaChris Lattner
356fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    SymbolVendor *symbols = GetSymbolVendor  ();
357fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar    if (symbols)
358fc6877aec9826fa830204d49eba7fac7412b841eDaniel Dunbar        symbols->ResolveSymbolContext (file_spec, line, check_inlines, resolve_scope, sc_list);
359
360    return sc_list.GetSize() - initial_count;
361}
362
363
364uint32_t
365Module::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
366{
367    SymbolVendor *symbols = GetSymbolVendor ();
368    if (symbols)
369        return symbols->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
370    return 0;
371}
372uint32_t
373Module::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
374{
375    SymbolVendor *symbols = GetSymbolVendor ();
376    if (symbols)
377        return symbols->FindGlobalVariables(regex, append, max_matches, variables);
378    return 0;
379}
380
381uint32_t
382Module::FindCompileUnits (const FileSpec &path,
383                          bool append,
384                          SymbolContextList &sc_list)
385{
386    if (!append)
387        sc_list.Clear();
388
389    const uint32_t start_size = sc_list.GetSize();
390    const uint32_t num_compile_units = GetNumCompileUnits();
391    SymbolContext sc;
392    sc.module_sp = this;
393    const bool compare_directory = path.GetDirectory();
394    for (uint32_t i=0; i<num_compile_units; ++i)
395    {
396        sc.comp_unit = GetCompileUnitAtIndex(i).get();
397        if (FileSpec::Equal (*sc.comp_unit, path, compare_directory))
398            sc_list.Append(sc);
399    }
400    return sc_list.GetSize() - start_size;
401}
402
403uint32_t
404Module::FindFunctions (const ConstString &name,
405                       const ClangNamespaceDecl *namespace_decl,
406                       uint32_t name_type_mask,
407                       bool include_symbols,
408                       bool append,
409                       SymbolContextList& sc_list)
410{
411    if (!append)
412        sc_list.Clear();
413
414    const uint32_t start_size = sc_list.GetSize();
415
416    // Find all the functions (not symbols, but debug information functions...
417    SymbolVendor *symbols = GetSymbolVendor ();
418    if (symbols)
419        symbols->FindFunctions(name, namespace_decl, name_type_mask, append, sc_list);
420
421    // Now check our symbol table for symbols that are code symbols if requested
422    if (include_symbols)
423    {
424        ObjectFile *objfile = GetObjectFile();
425        if (objfile)
426        {
427            Symtab *symtab = objfile->GetSymtab();
428            if (symtab)
429            {
430                std::vector<uint32_t> symbol_indexes;
431                symtab->FindAllSymbolsWithNameAndType (name, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
432                const uint32_t num_matches = symbol_indexes.size();
433                if (num_matches)
434                {
435                    const bool merge_symbol_into_function = true;
436                    SymbolContext sc(this);
437                    for (uint32_t i=0; i<num_matches; i++)
438                    {
439                        sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
440                        sc_list.AppendIfUnique (sc, merge_symbol_into_function);
441                    }
442                }
443            }
444        }
445    }
446    return sc_list.GetSize() - start_size;
447}
448
449uint32_t
450Module::FindFunctions (const RegularExpression& regex,
451                       bool include_symbols,
452                       bool append,
453                       SymbolContextList& sc_list)
454{
455    if (!append)
456        sc_list.Clear();
457
458    const uint32_t start_size = sc_list.GetSize();
459
460    SymbolVendor *symbols = GetSymbolVendor ();
461    if (symbols)
462        symbols->FindFunctions(regex, append, sc_list);
463    // Now check our symbol table for symbols that are code symbols if requested
464    if (include_symbols)
465    {
466        ObjectFile *objfile = GetObjectFile();
467        if (objfile)
468        {
469            Symtab *symtab = objfile->GetSymtab();
470            if (symtab)
471            {
472                std::vector<uint32_t> symbol_indexes;
473                symtab->AppendSymbolIndexesMatchingRegExAndType (regex, eSymbolTypeCode, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
474                const uint32_t num_matches = symbol_indexes.size();
475                if (num_matches)
476                {
477                    const bool merge_symbol_into_function = true;
478                    SymbolContext sc(this);
479                    for (uint32_t i=0; i<num_matches; i++)
480                    {
481                        sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
482                        sc_list.AppendIfUnique (sc, merge_symbol_into_function);
483                    }
484                }
485            }
486        }
487    }
488    return sc_list.GetSize() - start_size;
489}
490
491uint32_t
492Module::FindTypes_Impl (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types)
493{
494    Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
495    if (sc.module_sp.get() == NULL || sc.module_sp.get() == this)
496    {
497        SymbolVendor *symbols = GetSymbolVendor ();
498        if (symbols)
499            return symbols->FindTypes(sc, name, namespace_decl, append, max_matches, types);
500    }
501    return 0;
502}
503
504// depending on implementation details, type lookup might fail because of
505// embedded spurious namespace:: prefixes. this call strips them, paying
506// attention to the fact that a type might have namespace'd type names as
507// arguments to templates, and those must not be stripped off
508static const char*
509StripTypeName(const char* name_cstr)
510{
511    // Protect against null c string.
512    if (!name_cstr)
513        return name_cstr;
514    const char* skip_namespace = strstr(name_cstr, "::");
515    const char* template_arg_char = strchr(name_cstr, '<');
516    while (skip_namespace != NULL)
517    {
518        if (template_arg_char != NULL &&
519            skip_namespace > template_arg_char) // but namespace'd template arguments are still good to go
520            break;
521        name_cstr = skip_namespace+2;
522        skip_namespace = strstr(name_cstr, "::");
523    }
524    return name_cstr;
525}
526
527uint32_t
528Module::FindTypes (const SymbolContext& sc,  const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types)
529{
530    uint32_t retval = FindTypes_Impl(sc, name, namespace_decl, append, max_matches, types);
531
532    if (retval == 0)
533    {
534        const char *orig_name = name.GetCString();
535        const char *stripped = StripTypeName(orig_name);
536        // Only do this lookup if StripTypeName has stripped the name:
537        if (stripped != orig_name)
538           return FindTypes_Impl(sc, ConstString(stripped), namespace_decl, append, max_matches, types);
539        else
540            return 0;
541    }
542    else
543        return retval;
544
545}
546
547//uint32_t
548//Module::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, const char *udt_name, TypeList& types)
549//{
550//  Timer scoped_timer(__PRETTY_FUNCTION__);
551//  SymbolVendor *symbols = GetSymbolVendor ();
552//  if (symbols)
553//      return symbols->FindTypes(sc, regex, append, max_matches, encoding, udt_name, types);
554//  return 0;
555//
556//}
557
558SymbolVendor*
559Module::GetSymbolVendor (bool can_create)
560{
561    Mutex::Locker locker (m_mutex);
562    if (m_did_load_symbol_vendor == false && can_create)
563    {
564        ObjectFile *obj_file = GetObjectFile ();
565        if (obj_file != NULL)
566        {
567            Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
568            m_symfile_ap.reset(SymbolVendor::FindPlugin(this));
569            m_did_load_symbol_vendor = true;
570        }
571    }
572    return m_symfile_ap.get();
573}
574
575void
576Module::SetFileSpecAndObjectName (const FileSpec &file, const ConstString &object_name)
577{
578    // Container objects whose paths do not specify a file directly can call
579    // this function to correct the file and object names.
580    m_file = file;
581    m_mod_time = file.GetModificationTime();
582    m_object_name = object_name;
583}
584
585const ArchSpec&
586Module::GetArchitecture () const
587{
588    return m_arch;
589}
590
591void
592Module::GetDescription (Stream *s, lldb::DescriptionLevel level)
593{
594    Mutex::Locker locker (m_mutex);
595
596    if (level >= eDescriptionLevelFull)
597    {
598        if (m_arch.IsValid())
599            s->Printf("(%s) ", m_arch.GetArchitectureName());
600    }
601
602    if (level == eDescriptionLevelBrief)
603    {
604        const char *filename = m_file.GetFilename().GetCString();
605        if (filename)
606            s->PutCString (filename);
607    }
608    else
609    {
610        char path[PATH_MAX];
611        if (m_file.GetPath(path, sizeof(path)))
612            s->PutCString(path);
613    }
614
615    const char *object_name = m_object_name.GetCString();
616    if (object_name)
617        s->Printf("(%s)", object_name);
618}
619
620void
621Module::ReportError (const char *format, ...)
622{
623    if (format && format[0])
624    {
625        StreamString strm;
626        strm.PutCString("error: ");
627        GetDescription(&strm, lldb::eDescriptionLevelBrief);
628        strm.PutChar (' ');
629        va_list args;
630        va_start (args, format);
631        strm.PrintfVarArg(format, args);
632        va_end (args);
633
634        const int format_len = strlen(format);
635        if (format_len > 0)
636        {
637            const char last_char = format[format_len-1];
638            if (last_char != '\n' || last_char != '\r')
639                strm.EOL();
640        }
641        Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
642
643    }
644}
645
646void
647Module::ReportErrorIfModifyDetected (const char *format, ...)
648{
649    if (!GetModified(true) && GetModified(false))
650    {
651        if (format)
652        {
653            StreamString strm;
654            strm.PutCString("error: the object file ");
655            GetDescription(&strm, lldb::eDescriptionLevelFull);
656            strm.PutCString (" has been modified\n");
657
658            va_list args;
659            va_start (args, format);
660            strm.PrintfVarArg(format, args);
661            va_end (args);
662
663            const int format_len = strlen(format);
664            if (format_len > 0)
665            {
666                const char last_char = format[format_len-1];
667                if (last_char != '\n' || last_char != '\r')
668                    strm.EOL();
669            }
670            strm.PutCString("The debug session should be aborted as the original debug information has been overwritten.\n");
671            Host::SystemLog (Host::eSystemLogError, "%s", strm.GetString().c_str());
672        }
673    }
674}
675
676void
677Module::ReportWarning (const char *format, ...)
678{
679    if (format && format[0])
680    {
681        StreamString strm;
682        strm.PutCString("warning: ");
683        GetDescription(&strm, lldb::eDescriptionLevelFull);
684        strm.PutChar (' ');
685
686        va_list args;
687        va_start (args, format);
688        strm.PrintfVarArg(format, args);
689        va_end (args);
690
691        const int format_len = strlen(format);
692        if (format_len > 0)
693        {
694            const char last_char = format[format_len-1];
695            if (last_char != '\n' || last_char != '\r')
696                strm.EOL();
697        }
698        Host::SystemLog (Host::eSystemLogWarning, "%s", strm.GetString().c_str());
699    }
700}
701
702void
703Module::LogMessage (Log *log, const char *format, ...)
704{
705    if (log)
706    {
707        StreamString log_message;
708        GetDescription(&log_message, lldb::eDescriptionLevelFull);
709        log_message.PutCString (": ");
710        va_list args;
711        va_start (args, format);
712        log_message.PrintfVarArg (format, args);
713        va_end (args);
714        log->PutCString(log_message.GetString().c_str());
715    }
716}
717
718bool
719Module::GetModified (bool use_cached_only)
720{
721    if (m_was_modified == false && use_cached_only == false)
722    {
723        TimeValue curr_mod_time (m_file.GetModificationTime());
724        m_was_modified = curr_mod_time != m_mod_time;
725    }
726    return m_was_modified;
727}
728
729bool
730Module::SetModified (bool b)
731{
732    const bool prev_value = m_was_modified;
733    m_was_modified = b;
734    return prev_value;
735}
736
737
738void
739Module::Dump(Stream *s)
740{
741    Mutex::Locker locker (m_mutex);
742    //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
743    s->Indent();
744    s->Printf("Module %s/%s%s%s%s\n",
745              m_file.GetDirectory().AsCString(),
746              m_file.GetFilename().AsCString(),
747              m_object_name ? "(" : "",
748              m_object_name ? m_object_name.GetCString() : "",
749              m_object_name ? ")" : "");
750
751    s->IndentMore();
752    ObjectFile *objfile = GetObjectFile ();
753
754    if (objfile)
755        objfile->Dump(s);
756
757    SymbolVendor *symbols = GetSymbolVendor ();
758
759    if (symbols)
760        symbols->Dump(s);
761
762    s->IndentLess();
763}
764
765
766TypeList*
767Module::GetTypeList ()
768{
769    SymbolVendor *symbols = GetSymbolVendor ();
770    if (symbols)
771        return &symbols->GetTypeList();
772    return NULL;
773}
774
775const ConstString &
776Module::GetObjectName() const
777{
778    return m_object_name;
779}
780
781ObjectFile *
782Module::GetObjectFile()
783{
784    Mutex::Locker locker (m_mutex);
785    if (m_did_load_objfile == false)
786    {
787        m_did_load_objfile = true;
788        Timer scoped_timer(__PRETTY_FUNCTION__,
789                           "Module::GetObjectFile () module = %s", GetFileSpec().GetFilename().AsCString(""));
790        DataBufferSP file_data_sp;
791        m_objfile_sp = ObjectFile::FindPlugin(this, &m_file, m_object_offset, m_file.GetByteSize(), file_data_sp);
792        if (m_objfile_sp)
793        {
794			// Once we get the object file, update our module with the object file's
795			// architecture since it might differ in vendor/os if some parts were
796			// unknown.
797            m_objfile_sp->GetArchitecture (m_arch);
798        }
799    }
800    return m_objfile_sp.get();
801}
802
803
804const Symbol *
805Module::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
806{
807    Timer scoped_timer(__PRETTY_FUNCTION__,
808                       "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
809                       name.AsCString(),
810                       symbol_type);
811    ObjectFile *objfile = GetObjectFile();
812    if (objfile)
813    {
814        Symtab *symtab = objfile->GetSymtab();
815        if (symtab)
816            return symtab->FindFirstSymbolWithNameAndType (name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
817    }
818    return NULL;
819}
820void
821Module::SymbolIndicesToSymbolContextList (Symtab *symtab, std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
822{
823    // No need to protect this call using m_mutex all other method calls are
824    // already thread safe.
825
826    size_t num_indices = symbol_indexes.size();
827    if (num_indices > 0)
828    {
829        SymbolContext sc;
830        CalculateSymbolContext (&sc);
831        for (size_t i = 0; i < num_indices; i++)
832        {
833            sc.symbol = symtab->SymbolAtIndex (symbol_indexes[i]);
834            if (sc.symbol)
835                sc_list.Append (sc);
836        }
837    }
838}
839
840size_t
841Module::FindSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, SymbolContextList &sc_list)
842{
843    // No need to protect this call using m_mutex all other method calls are
844    // already thread safe.
845
846
847    Timer scoped_timer(__PRETTY_FUNCTION__,
848                       "Module::FindSymbolsWithNameAndType (name = %s, type = %i)",
849                       name.AsCString(),
850                       symbol_type);
851    const size_t initial_size = sc_list.GetSize();
852    ObjectFile *objfile = GetObjectFile ();
853    if (objfile)
854    {
855        Symtab *symtab = objfile->GetSymtab();
856        if (symtab)
857        {
858            std::vector<uint32_t> symbol_indexes;
859            symtab->FindAllSymbolsWithNameAndType (name, symbol_type, symbol_indexes);
860            SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
861        }
862    }
863    return sc_list.GetSize() - initial_size;
864}
865
866size_t
867Module::FindSymbolsMatchingRegExAndType (const RegularExpression &regex, SymbolType symbol_type, SymbolContextList &sc_list)
868{
869    // No need to protect this call using m_mutex all other method calls are
870    // already thread safe.
871
872    Timer scoped_timer(__PRETTY_FUNCTION__,
873                       "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
874                       regex.GetText(),
875                       symbol_type);
876    const size_t initial_size = sc_list.GetSize();
877    ObjectFile *objfile = GetObjectFile ();
878    if (objfile)
879    {
880        Symtab *symtab = objfile->GetSymtab();
881        if (symtab)
882        {
883            std::vector<uint32_t> symbol_indexes;
884            symtab->FindAllSymbolsMatchingRexExAndType (regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny, symbol_indexes);
885            SymbolIndicesToSymbolContextList (symtab, symbol_indexes, sc_list);
886        }
887    }
888    return sc_list.GetSize() - initial_size;
889}
890
891const TimeValue &
892Module::GetModificationTime () const
893{
894    return m_mod_time;
895}
896
897bool
898Module::IsExecutable ()
899{
900    if (GetObjectFile() == NULL)
901        return false;
902    else
903        return GetObjectFile()->IsExecutable();
904}
905
906bool
907Module::IsLoadedInTarget (Target *target)
908{
909    ObjectFile *obj_file = GetObjectFile();
910    if (obj_file)
911    {
912        SectionList *sections = obj_file->GetSectionList();
913        if (sections != NULL)
914        {
915            size_t num_sections = sections->GetSize();
916            for (size_t sect_idx = 0; sect_idx < num_sections; sect_idx++)
917            {
918                SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
919                if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS)
920                {
921                    return true;
922                }
923            }
924        }
925    }
926    return false;
927}
928bool
929Module::SetArchitecture (const ArchSpec &new_arch)
930{
931    if (!m_arch.IsValid())
932    {
933        m_arch = new_arch;
934        return true;
935    }
936    return m_arch == new_arch;
937}
938
939