Eli Friedman | 5423ebf | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 1 | //===-- Symbols.cpp ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Host/Symbols.h" |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 11 | #include "lldb/Core/ArchSpec.h" |
| 12 | #include "lldb/Core/DataBuffer.h" |
| 13 | #include "lldb/Core/DataExtractor.h" |
| 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/ModuleSpec.h" |
| 16 | #include "lldb/Core/StreamString.h" |
| 17 | #include "lldb/Core/Timer.h" |
| 18 | #include "lldb/Core/UUID.h" |
| 19 | #include "lldb/Symbol/ObjectFile.h" |
| 20 | #include "lldb/Target/Target.h" |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 21 | #include "lldb/Utility/SafeMachO.h" |
Eli Friedman | 5423ebf | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 22 | |
Zachary Turner | 88c6b62 | 2015-03-03 18:34:26 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
| 24 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 25 | // From MacOSX system header "mach/machine.h" |
| 26 | typedef int cpu_type_t; |
| 27 | typedef int cpu_subtype_t; |
| 28 | |
Eli Friedman | 5423ebf | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 29 | using namespace lldb; |
| 30 | using namespace lldb_private; |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 31 | using namespace llvm::MachO; |
Eli Friedman | 5423ebf | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 32 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 33 | #if defined(__APPLE__) |
| 34 | |
| 35 | // Forward declaration of method defined in source/Host/macosx/Symbols.cpp |
| 36 | int |
| 37 | LocateMacOSXFilesUsingDebugSymbols |
| 38 | ( |
| 39 | const ModuleSpec &module_spec, |
| 40 | FileSpec *out_exec_fspec, // If non-NULL, try and find the executable |
| 41 | FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file |
| 42 | ); |
| 43 | |
| 44 | #else |
| 45 | |
| 46 | int |
| 47 | LocateMacOSXFilesUsingDebugSymbols |
| 48 | ( |
| 49 | const ModuleSpec &module_spec, |
| 50 | FileSpec *out_exec_fspec, // If non-NULL, try and find the executable |
| 51 | FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file |
| 52 | ) { |
| 53 | // Cannot find MacOSX files using debug symbols on non MacOSX. |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | #endif |
| 58 | |
| 59 | static bool |
| 60 | LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec) |
| 61 | { |
| 62 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
| 63 | if (exec_fspec) |
| 64 | { |
| 65 | char path[PATH_MAX]; |
| 66 | if (exec_fspec->GetPath(path, sizeof(path))) |
| 67 | { |
| 68 | // Make sure the module isn't already just a dSYM file... |
| 69 | if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL) |
| 70 | { |
| 71 | size_t obj_file_path_length = strlen(path); |
| 72 | ::strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path) - strlen(path) - 1); |
| 73 | ::strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path) - strlen(path) - 1); |
| 74 | |
| 75 | dsym_fspec.SetFile(path, false); |
| 76 | |
| 77 | ModuleSpecList module_specs; |
| 78 | ModuleSpec matched_module_spec; |
| 79 | if (dsym_fspec.Exists() && |
| 80 | ObjectFile::GetModuleSpecifications(dsym_fspec, 0, 0, module_specs) && |
| 81 | module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) |
| 82 | { |
| 83 | return true; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | path[obj_file_path_length] = '\0'; |
| 88 | |
| 89 | char *last_dot = strrchr(path, '.'); |
| 90 | while (last_dot != NULL && last_dot[0]) |
| 91 | { |
| 92 | char *next_slash = strchr(last_dot, '/'); |
| 93 | if (next_slash != NULL) |
| 94 | { |
| 95 | *next_slash = '\0'; |
| 96 | ::strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path) - strlen(path) - 1); |
| 97 | ::strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path) - strlen(path) - 1); |
| 98 | dsym_fspec.SetFile(path, false); |
| 99 | if (dsym_fspec.Exists() && |
| 100 | ObjectFile::GetModuleSpecifications(dsym_fspec, 0, 0, module_specs) && |
| 101 | module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) |
| 102 | { |
| 103 | return true; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | *last_dot = '\0'; |
| 108 | char *prev_slash = strrchr(path, '/'); |
| 109 | if (prev_slash != NULL) |
| 110 | *prev_slash = '\0'; |
| 111 | else |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | dsym_fspec.Clear(); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | FileSpec |
| 129 | LocateExecutableSymbolFileDsym (const ModuleSpec &module_spec) |
| 130 | { |
| 131 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
| 132 | const ArchSpec *arch = module_spec.GetArchitecturePtr(); |
| 133 | const UUID *uuid = module_spec.GetUUIDPtr(); |
| 134 | |
| 135 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 136 | "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)", |
| 137 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
| 138 | arch ? arch->GetArchitectureName() : "<NULL>", |
| 139 | uuid); |
| 140 | |
| 141 | FileSpec symbol_fspec; |
| 142 | // First try and find the dSYM in the same directory as the executable or in |
| 143 | // an appropriate parent directory |
| 144 | if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false) |
| 145 | { |
| 146 | // We failed to easily find the dSYM above, so use DebugSymbols |
| 147 | LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec); |
| 148 | } |
| 149 | return symbol_fspec; |
| 150 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 151 | |
| 152 | FileSpec |
| 153 | Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec) |
| 154 | { |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 155 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
| 156 | const ArchSpec *arch = module_spec.GetArchitecturePtr(); |
| 157 | const UUID *uuid = module_spec.GetUUIDPtr(); |
| 158 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 159 | "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)", |
| 160 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
| 161 | arch ? arch->GetArchitectureName() : "<NULL>", |
| 162 | uuid); |
| 163 | |
| 164 | FileSpec objfile_fspec; |
| 165 | ModuleSpecList module_specs; |
| 166 | ModuleSpec matched_module_spec; |
| 167 | if (exec_fspec && |
| 168 | ObjectFile::GetModuleSpecifications(*exec_fspec, 0, 0, module_specs) && |
| 169 | module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) |
| 170 | { |
| 171 | objfile_fspec = exec_fspec; |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL); |
| 176 | } |
| 177 | return objfile_fspec; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | FileSpec |
| 181 | Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec) |
| 182 | { |
| 183 | const char *symbol_filename = module_spec.GetSymbolFileSpec().GetFilename().AsCString(); |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 184 | if (symbol_filename && symbol_filename[0]) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 185 | { |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 186 | FileSpecList debug_file_search_paths (Target::GetDefaultDebugFileSearchPaths()); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 187 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 188 | // Add module directory. |
| 189 | const ConstString &file_dir = module_spec.GetFileSpec().GetDirectory(); |
| 190 | debug_file_search_paths.AppendIfUnique (FileSpec(file_dir.AsCString("."), true)); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 191 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 192 | // Add current working directory. |
| 193 | debug_file_search_paths.AppendIfUnique (FileSpec(".", true)); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 194 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 195 | // Add /usr/lib/debug directory. |
| 196 | debug_file_search_paths.AppendIfUnique (FileSpec("/usr/lib/debug", true)); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 197 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 198 | std::string uuid_str; |
| 199 | const UUID &module_uuid = module_spec.GetUUID(); |
| 200 | if (module_uuid.IsValid()) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 201 | { |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 202 | // Some debug files are stored in the .build-id directory like this: |
| 203 | // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug |
| 204 | uuid_str = module_uuid.GetAsString(""); |
| 205 | uuid_str.insert (2, 1, '/'); |
| 206 | uuid_str = uuid_str + ".debug"; |
| 207 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 208 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 209 | // Get directory of our module. Needed to check debug files like this: |
| 210 | // /usr/lib/debug/usr/lib/library.so.debug |
| 211 | std::string module_directory = module_spec.GetFileSpec().GetDirectory().AsCString(); |
| 212 | |
| 213 | size_t num_directories = debug_file_search_paths.GetSize(); |
| 214 | for (size_t idx = 0; idx < num_directories; ++idx) |
| 215 | { |
| 216 | FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex (idx); |
| 217 | dirspec.ResolvePath(); |
| 218 | if (!dirspec.Exists() || !dirspec.IsDirectory()) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 219 | continue; |
| 220 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 221 | std::vector<std::string> files; |
| 222 | std::string dirname = dirspec.GetPath(); |
| 223 | |
| 224 | files.push_back (dirname + "/" + symbol_filename); |
| 225 | files.push_back (dirname + "/.debug/" + symbol_filename); |
| 226 | files.push_back (dirname + "/.build-id/" + uuid_str); |
| 227 | files.push_back (dirname + module_directory + "/" + symbol_filename); |
| 228 | |
| 229 | const uint32_t num_files = files.size(); |
| 230 | for (size_t idx_file = 0; idx_file < num_files; ++idx_file) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 231 | { |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 232 | const std::string &filename = files[idx_file]; |
| 233 | FileSpec file_spec (filename.c_str(), true); |
| 234 | |
| 235 | if (llvm::sys::fs::equivalent (file_spec.GetPath(), module_spec.GetFileSpec().GetPath())) |
| 236 | continue; |
| 237 | |
| 238 | if (file_spec.Exists()) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 239 | { |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 240 | lldb_private::ModuleSpecList specs; |
| 241 | const size_t num_specs = ObjectFile::GetModuleSpecifications (file_spec, 0, 0, specs); |
| 242 | assert (num_specs <= 1 && "Symbol Vendor supports only a single architecture"); |
| 243 | if (num_specs == 1) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 244 | { |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 245 | ModuleSpec mspec; |
| 246 | if (specs.GetModuleSpecAtIndex (0, mspec)) |
| 247 | { |
| 248 | if (mspec.GetUUID() == module_uuid) |
| 249 | return file_spec; |
| 250 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 257 | return LocateExecutableSymbolFileDsym(module_spec); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame^] | 260 | #if !defined (__APPLE__) |
| 261 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 262 | FileSpec |
| 263 | Symbols::FindSymbolFileInBundle (const FileSpec& symfile_bundle, |
| 264 | const lldb_private::UUID *uuid, |
| 265 | const ArchSpec *arch) |
| 266 | { |
| 267 | // FIXME |
| 268 | return FileSpec(); |
| 269 | } |
| 270 | |
| 271 | bool |
| 272 | Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup) |
| 273 | { |
| 274 | // Fill in the module_spec.GetFileSpec() for the object file and/or the |
| 275 | // module_spec.GetSymbolFileSpec() for the debug symbols file. |
| 276 | return false; |
| 277 | } |
| 278 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 279 | #endif |