1254721Semaste//===-- Symbols.cpp ---------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/Host/Symbols.h"
11254721Semaste#include "lldb/Core/ArchSpec.h"
12254721Semaste#include "lldb/Core/DataBuffer.h"
13254721Semaste#include "lldb/Core/DataExtractor.h"
14254721Semaste#include "lldb/Core/Module.h"
15254721Semaste#include "lldb/Core/ModuleSpec.h"
16254721Semaste#include "lldb/Core/StreamString.h"
17254721Semaste#include "lldb/Core/Timer.h"
18254721Semaste#include "lldb/Core/UUID.h"
19254721Semaste#include "lldb/Symbol/ObjectFile.h"
20254721Semaste#include "lldb/Target/Target.h"
21254721Semaste
22254721Semasteusing namespace lldb;
23254721Semasteusing namespace lldb_private;
24254721Semaste
25254721Semaste#if defined (__linux__) || defined (__FreeBSD__)
26254721Semaste
27254721SemasteFileSpec
28254721SemasteSymbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
29254721Semaste{
30254721Semaste    // FIXME
31254721Semaste    return FileSpec();
32254721Semaste}
33254721Semaste
34254721SemasteFileSpec
35254721SemasteSymbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
36254721Semaste{
37254721Semaste    const char *symbol_filename = module_spec.GetSymbolFileSpec().GetFilename().AsCString();
38254721Semaste    if (!symbol_filename || !symbol_filename[0])
39254721Semaste        return FileSpec();
40254721Semaste
41254721Semaste    FileSpecList debug_file_search_paths (Target::GetDefaultDebugFileSearchPaths());
42254721Semaste
43254721Semaste    // Add module directory.
44254721Semaste    const ConstString &file_dir = module_spec.GetFileSpec().GetDirectory();
45254721Semaste    debug_file_search_paths.AppendIfUnique (FileSpec(file_dir.AsCString("."), true));
46254721Semaste
47254721Semaste    // Add current working directory.
48254721Semaste    debug_file_search_paths.AppendIfUnique (FileSpec(".", true));
49254721Semaste
50254721Semaste    // Add /usr/lib/debug directory.
51254721Semaste    debug_file_search_paths.AppendIfUnique (FileSpec("/usr/lib/debug", true));
52254721Semaste
53254721Semaste    std::string uuid_str;
54254721Semaste    const UUID &module_uuid = module_spec.GetUUID();
55254721Semaste    if (module_uuid.IsValid())
56254721Semaste    {
57254721Semaste        // Some debug files are stored in the .build-id directory like this:
58254721Semaste        //   /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
59254721Semaste        uuid_str = module_uuid.GetAsString("");
60254721Semaste        uuid_str.insert (2, 1, '/');
61254721Semaste        uuid_str = uuid_str + ".debug";
62254721Semaste    }
63254721Semaste
64263364Semaste    // Get directory of our module. Needed to check debug files like this:
65263364Semaste    //   /usr/lib/debug/usr/lib/library.so.debug
66263364Semaste    std::string module_directory = module_spec.GetFileSpec().GetDirectory().AsCString();
67254721Semaste
68254721Semaste    size_t num_directories = debug_file_search_paths.GetSize();
69254721Semaste    for (size_t idx = 0; idx < num_directories; ++idx)
70254721Semaste    {
71254721Semaste        FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex (idx);
72254721Semaste        dirspec.ResolvePath();
73254721Semaste        if (!dirspec.Exists() || !dirspec.IsDirectory())
74254721Semaste            continue;
75254721Semaste
76254721Semaste        std::vector<std::string> files;
77254721Semaste        std::string dirname = dirspec.GetPath();
78254721Semaste
79254721Semaste        files.push_back (dirname + "/" + symbol_filename);
80254721Semaste        files.push_back (dirname + "/.debug/" + symbol_filename);
81254721Semaste        files.push_back (dirname + "/.build-id/" + uuid_str);
82263364Semaste        files.push_back (dirname + module_directory + "/" + symbol_filename);
83254721Semaste
84254721Semaste        const uint32_t num_files = files.size();
85254721Semaste        for (size_t idx_file = 0; idx_file < num_files; ++idx_file)
86254721Semaste        {
87254721Semaste            const std::string &filename = files[idx_file];
88254721Semaste            FileSpec file_spec (filename.c_str(), true);
89254721Semaste
90254721Semaste            if (file_spec == module_spec.GetFileSpec())
91254721Semaste                continue;
92254721Semaste
93254721Semaste            if (file_spec.Exists())
94254721Semaste            {
95254721Semaste                lldb_private::ModuleSpecList specs;
96254721Semaste                const size_t num_specs = ObjectFile::GetModuleSpecifications (file_spec, 0, 0, specs);
97254721Semaste                assert (num_specs <= 1 && "Symbol Vendor supports only a single architecture");
98254721Semaste                if (num_specs == 1)
99254721Semaste                {
100254721Semaste                    ModuleSpec mspec;
101254721Semaste                    if (specs.GetModuleSpecAtIndex (0, mspec))
102254721Semaste                    {
103254721Semaste                        if (mspec.GetUUID() == module_uuid)
104254721Semaste                            return file_spec;
105254721Semaste                    }
106254721Semaste                }
107254721Semaste            }
108254721Semaste        }
109254721Semaste    }
110254721Semaste
111254721Semaste    return FileSpec();
112254721Semaste}
113254721Semaste
114254721SemasteFileSpec
115254721SemasteSymbols::FindSymbolFileInBundle (const FileSpec& symfile_bundle,
116254721Semaste                                 const lldb_private::UUID *uuid,
117254721Semaste                                 const ArchSpec *arch)
118254721Semaste{
119254721Semaste    // FIXME
120254721Semaste    return FileSpec();
121254721Semaste}
122254721Semaste
123254721Semastebool
124254721SemasteSymbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup)
125254721Semaste{
126254721Semaste    // Fill in the module_spec.GetFileSpec() for the object file and/or the
127254721Semaste    // module_spec.GetSymbolFileSpec() for the debug symbols file.
128254721Semaste    return false;
129254721Semaste}
130254721Semaste
131254721Semaste#elif !defined (__APPLE__)
132254721Semaste
133254721SemasteFileSpec
134254721SemasteSymbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
135254721Semaste{
136254721Semaste    // FIXME
137254721Semaste    return FileSpec();
138254721Semaste}
139254721Semaste
140254721SemasteFileSpec
141254721SemasteSymbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
142254721Semaste{
143254721Semaste    // FIXME
144254721Semaste    return FileSpec();
145254721Semaste}
146254721Semaste
147254721SemasteFileSpec
148254721SemasteSymbols::FindSymbolFileInBundle (const FileSpec& symfile_bundle,
149254721Semaste                                 const lldb_private::UUID *uuid,
150254721Semaste                                 const ArchSpec *arch)
151254721Semaste{
152254721Semaste    return FileSpec();
153254721Semaste}
154254721Semaste
155254721Semastebool
156254721SemasteSymbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup)
157254721Semaste{
158254721Semaste    // Fill in the module_spec.GetFileSpec() for the object file and/or the
159254721Semaste    // module_spec.GetSymbolFileSpec() for the debug symbols file.
160254721Semaste    return false;
161254721Semaste}
162254721Semaste
163254721Semaste#endif
164