Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/Module.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- Module.cpp --------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "lldb/Core/Module.h"
10
11
#include "lldb/Core/AddressRange.h"
12
#include "lldb/Core/AddressResolverFileLine.h"
13
#include "lldb/Core/DataFileCache.h"
14
#include "lldb/Core/Debugger.h"
15
#include "lldb/Core/FileSpecList.h"
16
#include "lldb/Core/Mangled.h"
17
#include "lldb/Core/ModuleSpec.h"
18
#include "lldb/Core/SearchFilter.h"
19
#include "lldb/Core/Section.h"
20
#include "lldb/Host/FileSystem.h"
21
#include "lldb/Host/Host.h"
22
#include "lldb/Host/HostInfo.h"
23
#include "lldb/Interpreter/CommandInterpreter.h"
24
#include "lldb/Interpreter/ScriptInterpreter.h"
25
#include "lldb/Symbol/CompileUnit.h"
26
#include "lldb/Symbol/Function.h"
27
#include "lldb/Symbol/ObjectFile.h"
28
#include "lldb/Symbol/Symbol.h"
29
#include "lldb/Symbol/SymbolContext.h"
30
#include "lldb/Symbol/SymbolFile.h"
31
#include "lldb/Symbol/SymbolVendor.h"
32
#include "lldb/Symbol/Symtab.h"
33
#include "lldb/Symbol/Type.h"
34
#include "lldb/Symbol/TypeList.h"
35
#include "lldb/Symbol/TypeMap.h"
36
#include "lldb/Symbol/TypeSystem.h"
37
#include "lldb/Target/Language.h"
38
#include "lldb/Target/Process.h"
39
#include "lldb/Target/Target.h"
40
#include "lldb/Utility/DataBufferHeap.h"
41
#include "lldb/Utility/LLDBAssert.h"
42
#include "lldb/Utility/Log.h"
43
#include "lldb/Utility/Logging.h"
44
#include "lldb/Utility/RegularExpression.h"
45
#include "lldb/Utility/Status.h"
46
#include "lldb/Utility/Stream.h"
47
#include "lldb/Utility/StreamString.h"
48
#include "lldb/Utility/Timer.h"
49
50
#if defined(_WIN32)
51
#include "lldb/Host/windows/PosixApi.h"
52
#endif
53
54
#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
55
#include "Plugins/Language/ObjC/ObjCLanguage.h"
56
57
#include "llvm/ADT/STLExtras.h"
58
#include "llvm/Support/Compiler.h"
59
#include "llvm/Support/DJB.h"
60
#include "llvm/Support/FileSystem.h"
61
#include "llvm/Support/FormatVariadic.h"
62
#include "llvm/Support/JSON.h"
63
#include "llvm/Support/Signals.h"
64
#include "llvm/Support/raw_ostream.h"
65
66
#include <cassert>
67
#include <cinttypes>
68
#include <cstdarg>
69
#include <cstdint>
70
#include <cstring>
71
#include <map>
72
#include <type_traits>
73
#include <utility>
74
75
namespace lldb_private {
76
class CompilerDeclContext;
77
}
78
namespace lldb_private {
79
class VariableList;
80
}
81
82
using namespace lldb;
83
using namespace lldb_private;
84
85
// Shared pointers to modules track module lifetimes in targets and in the
86
// global module, but this collection will track all module objects that are
87
// still alive
88
typedef std::vector<Module *> ModuleCollection;
89
90
293k
static ModuleCollection &GetModuleCollection() {
91
  // This module collection needs to live past any module, so we could either
92
  // make it a shared pointer in each module or just leak is.  Since it is only
93
  // an empty vector by the time all the modules have gone away, we just leak
94
  // it for now.  If we decide this is a big problem we can introduce a
95
  // Finalize method that will tear everything down in a predictable order.
96
97
293k
  static ModuleCollection *g_module_collection = nullptr;
98
293k
  if (g_module_collection == nullptr)
99
1.28k
    g_module_collection = new ModuleCollection();
100
101
293k
  return *g_module_collection;
102
293k
}
103
104
293k
std::recursive_mutex &Module::GetAllocationModuleCollectionMutex() {
105
  // NOTE: The mutex below must be leaked since the global module list in
106
  // the ModuleList class will get torn at some point, and we can't know if it
107
  // will tear itself down before the "g_module_collection_mutex" below will.
108
  // So we leak a Mutex object below to safeguard against that
109
110
293k
  static std::recursive_mutex *g_module_collection_mutex = nullptr;
111
293k
  if (g_module_collection_mutex == nullptr)
112
1.28k
    g_module_collection_mutex = new std::recursive_mutex; // NOTE: known leak
113
293k
  return *g_module_collection_mutex;
114
293k
}
115
116
3.53k
size_t Module::GetNumberAllocatedModules() {
117
3.53k
  std::lock_guard<std::recursive_mutex> guard(
118
3.53k
      GetAllocationModuleCollectionMutex());
119
3.53k
  return GetModuleCollection().size();
120
3.53k
}
121
122
4.34k
Module *Module::GetAllocatedModuleAtIndex(size_t idx) {
123
4.34k
  std::lock_guard<std::recursive_mutex> guard(
124
4.34k
      GetAllocationModuleCollectionMutex());
125
4.34k
  ModuleCollection &modules = GetModuleCollection();
126
4.34k
  if (idx < modules.size())
127
4.34k
    return modules[idx];
128
0
  return nullptr;
129
4.34k
}
130
131
Module::Module(const ModuleSpec &module_spec)
132
    : m_object_offset(0), m_file_has_changed(false),
133
137k
      m_first_file_changed_log(false) {
134
  // Scope for locker below...
135
137k
  {
136
137k
    std::lock_guard<std::recursive_mutex> guard(
137
137k
        GetAllocationModuleCollectionMutex());
138
137k
    GetModuleCollection().push_back(this);
139
137k
  }
140
141
137k
  Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT |
142
137k
                                                  LIBLLDB_LOG_MODULES));
143
137k
  if (log != nullptr)
144
0
    LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
145
137k
              static_cast<void *>(this),
146
137k
              module_spec.GetArchitecture().GetArchitectureName(),
147
137k
              module_spec.GetFileSpec().GetPath().c_str(),
148
137k
              module_spec.GetObjectName().IsEmpty() ? "" : "(",
149
137k
              module_spec.GetObjectName().IsEmpty()
150
137k
                  ? ""
151
137k
                  : module_spec.GetObjectName().AsCString(""),
152
137k
              module_spec.GetObjectName().IsEmpty() ? "" : ")");
153
154
137k
  auto data_sp = module_spec.GetData();
155
137k
  lldb::offset_t file_size = 0;
156
137k
  if (data_sp)
157
127k
    file_size = data_sp->GetByteSize();
158
159
  // First extract all module specifications from the file using the local file
160
  // path. If there are no specifications, then don't fill anything in
161
137k
  ModuleSpecList modules_specs;
162
137k
  if (ObjectFile::GetModuleSpecifications(
163
137k
          module_spec.GetFileSpec(), 0, file_size, modules_specs, data_sp) == 0)
164
904
    return;
165
166
  // Now make sure that one of the module specifications matches what we just
167
  // extract. We might have a module specification that specifies a file
168
  // "/usr/lib/dyld" with UUID XXX, but we might have a local version of
169
  // "/usr/lib/dyld" that has
170
  // UUID YYY and we don't want those to match. If they don't match, just don't
171
  // fill any ivars in so we don't accidentally grab the wrong file later since
172
  // they don't match...
173
136k
  ModuleSpec matching_module_spec;
174
136k
  if (!modules_specs.FindMatchingModuleSpec(module_spec,
175
136k
                                            matching_module_spec)) {
176
33
    if (log) {
177
0
      LLDB_LOGF(log, "Found local object file but the specs didn't match");
178
0
    }
179
33
    return;
180
33
  }
181
182
  // Set m_data_sp if it was initially provided in the ModuleSpec. Note that
183
  // we cannot use the data_sp variable here, because it will have been
184
  // modified by GetModuleSpecifications().
185
136k
  if (auto module_spec_data_sp = module_spec.GetData()) {
186
127k
    m_data_sp = module_spec_data_sp;
187
127k
    m_mod_time = {};
188
127k
  } else {
189
8.92k
    if (module_spec.GetFileSpec())
190
8.92k
      m_mod_time =
191
8.92k
          FileSystem::Instance().GetModificationTime(module_spec.GetFileSpec());
192
0
    else if (matching_module_spec.GetFileSpec())
193
0
      m_mod_time = FileSystem::Instance().GetModificationTime(
194
0
          matching_module_spec.GetFileSpec());
195
8.92k
  }
196
197
  // Copy the architecture from the actual spec if we got one back, else use
198
  // the one that was specified
199
136k
  if (matching_module_spec.GetArchitecture().IsValid())
200
136k
    m_arch = matching_module_spec.GetArchitecture();
201
0
  else if (module_spec.GetArchitecture().IsValid())
202
0
    m_arch = module_spec.GetArchitecture();
203
204
  // Copy the file spec over and use the specified one (if there was one) so we
205
  // don't use a path that might have gotten resolved a path in
206
  // 'matching_module_spec'
207
136k
  if (module_spec.GetFileSpec())
208
136k
    m_file = module_spec.GetFileSpec();
209
30
  else if (matching_module_spec.GetFileSpec())
210
0
    m_file = matching_module_spec.GetFileSpec();
211
212
  // Copy the platform file spec over
213
136k
  if (module_spec.GetPlatformFileSpec())
214
3
    m_platform_file = module_spec.GetPlatformFileSpec();
215
136k
  else if (matching_module_spec.GetPlatformFileSpec())
216
0
    m_platform_file = matching_module_spec.GetPlatformFileSpec();
217
218
  // Copy the symbol file spec over
219
136k
  if (module_spec.GetSymbolFileSpec())
220
30
    m_symfile_spec = module_spec.GetSymbolFileSpec();
221
136k
  else if (matching_module_spec.GetSymbolFileSpec())
222
0
    m_symfile_spec = matching_module_spec.GetSymbolFileSpec();
223
224
  // Copy the object name over
225
136k
  if (matching_module_spec.GetObjectName())
226
0
    m_object_name = matching_module_spec.GetObjectName();
227
136k
  else
228
136k
    m_object_name = module_spec.GetObjectName();
229
230
  // Always trust the object offset (file offset) and object modification time
231
  // (for mod time in a BSD static archive) of from the matching module
232
  // specification
233
136k
  m_object_offset = matching_module_spec.GetObjectOffset();
234
136k
  m_object_mod_time = matching_module_spec.GetObjectModificationTime();
235
136k
}
236
237
Module::Module(const FileSpec &file_spec, const ArchSpec &arch,
238
               const ConstString *object_name, lldb::offset_t object_offset,
239
               const llvm::sys::TimePoint<> &object_mod_time)
240
    : m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)),
241
      m_arch(arch), m_file(file_spec), m_object_offset(object_offset),
242
      m_object_mod_time(object_mod_time), m_file_has_changed(false),
243
1.85k
      m_first_file_changed_log(false) {
244
  // Scope for locker below...
245
1.85k
  {
246
1.85k
    std::lock_guard<std::recursive_mutex> guard(
247
1.85k
        GetAllocationModuleCollectionMutex());
248
1.85k
    GetModuleCollection().push_back(this);
249
1.85k
  }
250
251
1.85k
  if (object_name)
252
201
    m_object_name = *object_name;
253
254
1.85k
  Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT |
255
1.85k
                                                  LIBLLDB_LOG_MODULES));
256
1.85k
  if (log != nullptr)
257
0
    LLDB_LOGF(log, "%p Module::Module((%s) '%s%s%s%s')",
258
1.85k
              static_cast<void *>(this), m_arch.GetArchitectureName(),
259
1.85k
              m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
260
1.85k
              m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
261
1.85k
              m_object_name.IsEmpty() ? "" : ")");
262
1.85k
}
263
264
6.10k
Module::Module() : m_file_has_changed(false), m_first_file_changed_log(false) {
265
6.10k
  std::lock_guard<std::recursive_mutex> guard(
266
6.10k
      GetAllocationModuleCollectionMutex());
267
6.10k
  GetModuleCollection().push_back(this);
268
6.10k
}
269
270
140k
Module::~Module() {
271
  // Lock our module down while we tear everything down to make sure we don't
272
  // get any access to the module while it is being destroyed
273
140k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
274
  // Scope for locker below...
275
140k
  {
276
140k
    std::lock_guard<std::recursive_mutex> guard(
277
140k
        GetAllocationModuleCollectionMutex());
278
140k
    ModuleCollection &modules = GetModuleCollection();
279
140k
    ModuleCollection::iterator end = modules.end();
280
140k
    ModuleCollection::iterator pos = std::find(modules.begin(), end, this);
281
140k
    assert(pos != end);
282
0
    modules.erase(pos);
283
140k
  }
284
140k
  Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_OBJECT |
285
140k
                                                  LIBLLDB_LOG_MODULES));
286
140k
  if (log != nullptr)
287
0
    LLDB_LOGF(log, "%p Module::~Module((%s) '%s%s%s%s')",
288
140k
              static_cast<void *>(this), m_arch.GetArchitectureName(),
289
140k
              m_file.GetPath().c_str(), m_object_name.IsEmpty() ? "" : "(",
290
140k
              m_object_name.IsEmpty() ? "" : m_object_name.AsCString(""),
291
140k
              m_object_name.IsEmpty() ? "" : ")");
292
  // Release any auto pointers before we start tearing down our member
293
  // variables since the object file and symbol files might need to make
294
  // function calls back into this module object. The ordering is important
295
  // here because symbol files can require the module object file. So we tear
296
  // down the symbol file first, then the object file.
297
140k
  m_sections_up.reset();
298
140k
  m_symfile_up.reset();
299
140k
  m_objfile_sp.reset();
300
140k
}
301
302
ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
303
                                        lldb::addr_t header_addr, Status &error,
304
33
                                        size_t size_to_read) {
305
33
  if (m_objfile_sp) {
306
0
    error.SetErrorString("object file already exists");
307
33
  } else {
308
33
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
309
33
    if (process_sp) {
310
33
      m_did_load_objfile = true;
311
33
      auto data_up = std::make_unique<DataBufferHeap>(size_to_read, 0);
312
33
      Status readmem_error;
313
33
      const size_t bytes_read =
314
33
          process_sp->ReadMemory(header_addr, data_up->GetBytes(),
315
33
                                 data_up->GetByteSize(), readmem_error);
316
33
      if (bytes_read < size_to_read)
317
17
        data_up->SetByteSize(bytes_read);
318
33
      if (data_up->GetByteSize() > 0) {
319
18
        DataBufferSP data_sp(data_up.release());
320
18
        m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp,
321
18
                                              header_addr, data_sp);
322
18
        if (m_objfile_sp) {
323
18
          StreamString s;
324
18
          s.Printf("0x%16.16" PRIx64, header_addr);
325
18
          m_object_name.SetString(s.GetString());
326
327
          // Once we get the object file, update our module with the object
328
          // file's architecture since it might differ in vendor/os if some
329
          // parts were unknown.
330
18
          m_arch = m_objfile_sp->GetArchitecture();
331
332
          // Augment the arch with the target's information in case
333
          // we are unable to extract the os/environment from memory.
334
18
          m_arch.MergeFrom(process_sp->GetTarget().GetArchitecture());
335
18
        } else {
336
0
          error.SetErrorString("unable to find suitable object file plug-in");
337
0
        }
338
18
      } else {
339
15
        error.SetErrorStringWithFormat("unable to read header from memory: %s",
340
15
                                       readmem_error.AsCString());
341
15
      }
342
33
    } else {
343
0
      error.SetErrorString("invalid process");
344
0
    }
345
33
  }
346
33
  return m_objfile_sp.get();
347
33
}
348
349
23.3M
const lldb_private::UUID &Module::GetUUID() {
350
23.3M
  if (!m_did_set_uuid.load()) {
351
138k
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
352
138k
    if (!m_did_set_uuid.load()) {
353
138k
      ObjectFile *obj_file = GetObjectFile();
354
355
138k
      if (obj_file != nullptr) {
356
138k
        m_uuid = obj_file->GetUUID();
357
138k
        m_did_set_uuid = true;
358
138k
      }
359
138k
    }
360
138k
  }
361
23.3M
  return m_uuid;
362
23.3M
}
363
364
0
void Module::SetUUID(const lldb_private::UUID &uuid) {
365
0
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
366
0
  if (!m_did_set_uuid) {
367
0
    m_uuid = uuid;
368
0
    m_did_set_uuid = true;
369
0
  } else {
370
0
    lldbassert(0 && "Attempting to overwrite the existing module UUID");
371
0
  }
372
0
}
373
374
llvm::Expected<TypeSystem &>
375
802k
Module::GetTypeSystemForLanguage(LanguageType language) {
376
802k
  return m_type_system_map.GetTypeSystemForLanguage(language, this, true);
377
802k
}
378
379
17
void Module::ParseAllDebugSymbols() {
380
17
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
381
17
  size_t num_comp_units = GetNumCompileUnits();
382
17
  if (num_comp_units == 0)
383
4
    return;
384
385
13
  SymbolFile *symbols = GetSymbolFile();
386
387
34
  for (size_t cu_idx = 0; cu_idx < num_comp_units; 
cu_idx++21
) {
388
21
    SymbolContext sc;
389
21
    sc.module_sp = shared_from_this();
390
21
    sc.comp_unit = symbols->GetCompileUnitAtIndex(cu_idx).get();
391
21
    if (!sc.comp_unit)
392
0
      continue;
393
394
21
    symbols->ParseVariablesForContext(sc);
395
396
21
    symbols->ParseFunctions(*sc.comp_unit);
397
398
21
    sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
399
9
      symbols->ParseBlocksRecursive(*f);
400
401
      // Parse the variables for this function and all its blocks
402
9
      sc.function = f.get();
403
9
      symbols->ParseVariablesForContext(sc);
404
9
      return false;
405
9
    });
406
407
    // Parse all types for this compile unit
408
21
    symbols->ParseTypes(*sc.comp_unit);
409
21
  }
410
13
}
411
412
1.00M
void Module::CalculateSymbolContext(SymbolContext *sc) {
413
1.00M
  sc->module_sp = shared_from_this();
414
1.00M
}
415
416
17
ModuleSP Module::CalculateSymbolContextModule() { return shared_from_this(); }
417
418
0
void Module::DumpSymbolContext(Stream *s) {
419
0
  s->Printf(", Module{%p}", static_cast<void *>(this));
420
0
}
421
422
295k
size_t Module::GetNumCompileUnits() {
423
295k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
424
295k
  LLDB_SCOPED_TIMERF("Module::GetNumCompileUnits (module = %p)",
425
295k
                     static_cast<void *>(this));
426
295k
  if (SymbolFile *symbols = GetSymbolFile())
427
294k
    return symbols->GetNumCompileUnits();
428
1.44k
  return 0;
429
295k
}
430
431
16.8k
CompUnitSP Module::GetCompileUnitAtIndex(size_t index) {
432
16.8k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
433
16.8k
  size_t num_comp_units = GetNumCompileUnits();
434
16.8k
  CompUnitSP cu_sp;
435
436
16.8k
  if (index < num_comp_units) {
437
16.8k
    if (SymbolFile *symbols = GetSymbolFile())
438
16.8k
      cu_sp = symbols->GetCompileUnitAtIndex(index);
439
16.8k
  }
440
16.8k
  return cu_sp;
441
16.8k
}
442
443
14.0M
bool Module::ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr) {
444
14.0M
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
445
14.0M
  SectionList *section_list = GetSectionList();
446
14.0M
  if (section_list)
447
14.0M
    return so_addr.ResolveAddressUsingFileSections(vm_addr, section_list);
448
0
  return false;
449
14.0M
}
450
451
uint32_t Module::ResolveSymbolContextForAddress(
452
    const Address &so_addr, lldb::SymbolContextItem resolve_scope,
453
2.59M
    SymbolContext &sc, bool resolve_tail_call_address) {
454
2.59M
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
455
2.59M
  uint32_t resolved_flags = 0;
456
457
  // Clear the result symbol context in case we don't find anything, but don't
458
  // clear the target
459
2.59M
  sc.Clear(false);
460
461
  // Get the section from the section/offset address.
462
2.59M
  SectionSP section_sp(so_addr.GetSection());
463
464
  // Make sure the section matches this module before we try and match anything
465
2.59M
  if (section_sp && 
section_sp->GetModule().get() == this2.59M
) {
466
    // If the section offset based address resolved itself, then this is the
467
    // right module.
468
2.59M
    sc.module_sp = shared_from_this();
469
2.59M
    resolved_flags |= eSymbolContextModule;
470
471
2.59M
    SymbolFile *symfile = GetSymbolFile();
472
2.59M
    if (!symfile)
473
171
      return resolved_flags;
474
475
    // Resolve the compile unit, function, block, line table or line entry if
476
    // requested.
477
2.58M
    if (resolve_scope & eSymbolContextCompUnit ||
478
2.58M
        
resolve_scope & eSymbolContextFunction1.38M
||
479
2.58M
        
resolve_scope & eSymbolContextBlock42.1k
||
480
2.58M
        
resolve_scope & eSymbolContextLineEntry37.4k
||
481
2.58M
        
resolve_scope & eSymbolContextVariable25.0k
) {
482
2.56M
      resolved_flags |=
483
2.56M
          symfile->ResolveSymbolContext(so_addr, resolve_scope, sc);
484
2.56M
    }
485
486
    // Resolve the symbol if requested, but don't re-look it up if we've
487
    // already found it.
488
2.58M
    if (resolve_scope & eSymbolContextSymbol &&
489
2.58M
        
!(resolved_flags & eSymbolContextSymbol)2.53M
) {
490
46.2k
      Symtab *symtab = symfile->GetSymtab();
491
46.2k
      if (symtab && so_addr.IsSectionOffset()) {
492
46.2k
        Symbol *matching_symbol = nullptr;
493
494
46.2k
        symtab->ForEachSymbolContainingFileAddress(
495
46.2k
            so_addr.GetFileAddress(),
496
46.2k
            [&matching_symbol](Symbol *symbol) -> bool {
497
44.4k
              if (symbol->GetType() != eSymbolTypeInvalid) {
498
44.3k
                matching_symbol = symbol;
499
44.3k
                return false; // Stop iterating
500
44.3k
              }
501
46
              return true; // Keep iterating
502
44.4k
            });
503
46.2k
        sc.symbol = matching_symbol;
504
46.2k
        if (!sc.symbol && 
resolve_scope & eSymbolContextFunction1.87k
&&
505
46.2k
            
!(resolved_flags & eSymbolContextFunction)1.85k
) {
506
1.82k
          bool verify_unique = false; // No need to check again since
507
                                      // ResolveSymbolContext failed to find a
508
                                      // symbol at this address.
509
1.82k
          if (ObjectFile *obj_file = sc.module_sp->GetObjectFile())
510
1.82k
            sc.symbol =
511
1.82k
                obj_file->ResolveSymbolForAddress(so_addr, verify_unique);
512
1.82k
        }
513
514
46.2k
        if (sc.symbol) {
515
44.3k
          if (sc.symbol->IsSynthetic()) {
516
            // We have a synthetic symbol so lets check if the object file from
517
            // the symbol file in the symbol vendor is different than the
518
            // object file for the module, and if so search its symbol table to
519
            // see if we can come up with a better symbol. For example dSYM
520
            // files on MacOSX have an unstripped symbol table inside of them.
521
149
            ObjectFile *symtab_objfile = symtab->GetObjectFile();
522
149
            if (symtab_objfile && symtab_objfile->IsStripped()) {
523
13
              ObjectFile *symfile_objfile = symfile->GetObjectFile();
524
13
              if (symfile_objfile != symtab_objfile) {
525
11
                Symtab *symfile_symtab = symfile_objfile->GetSymtab();
526
11
                if (symfile_symtab) {
527
11
                  Symbol *symbol =
528
11
                      symfile_symtab->FindSymbolContainingFileAddress(
529
11
                          so_addr.GetFileAddress());
530
11
                  if (symbol && !symbol->IsSynthetic()) {
531
11
                    sc.symbol = symbol;
532
11
                  }
533
11
                }
534
11
              }
535
13
            }
536
149
          }
537
44.3k
          resolved_flags |= eSymbolContextSymbol;
538
44.3k
        }
539
46.2k
      }
540
46.2k
    }
541
542
    // For function symbols, so_addr may be off by one.  This is a convention
543
    // consistent with FDE row indices in eh_frame sections, but requires extra
544
    // logic here to permit symbol lookup for disassembly and unwind.
545
2.58M
    if (resolve_scope & eSymbolContextSymbol &&
546
2.58M
        
!(resolved_flags & eSymbolContextSymbol)2.53M
&&
resolve_tail_call_address1.87k
&&
547
2.58M
        
so_addr.IsSectionOffset()1
) {
548
1
      Address previous_addr = so_addr;
549
1
      previous_addr.Slide(-1);
550
551
1
      bool do_resolve_tail_call_address = false; // prevent recursion
552
1
      const uint32_t flags = ResolveSymbolContextForAddress(
553
1
          previous_addr, resolve_scope, sc, do_resolve_tail_call_address);
554
1
      if (flags & eSymbolContextSymbol) {
555
0
        AddressRange addr_range;
556
0
        if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
557
0
                               false, addr_range)) {
558
0
          if (addr_range.GetBaseAddress().GetSection() ==
559
0
              so_addr.GetSection()) {
560
            // If the requested address is one past the address range of a
561
            // function (i.e. a tail call), or the decremented address is the
562
            // start of a function (i.e. some forms of trampoline), indicate
563
            // that the symbol has been resolved.
564
0
            if (so_addr.GetOffset() ==
565
0
                    addr_range.GetBaseAddress().GetOffset() ||
566
0
                so_addr.GetOffset() == addr_range.GetBaseAddress().GetOffset() +
567
0
                                           addr_range.GetByteSize()) {
568
0
              resolved_flags |= flags;
569
0
            }
570
0
          } else {
571
0
            sc.symbol =
572
0
                nullptr; // Don't trust the symbol if the sections didn't match.
573
0
          }
574
0
        }
575
0
      }
576
1
    }
577
2.58M
  }
578
2.59M
  return resolved_flags;
579
2.59M
}
580
581
uint32_t Module::ResolveSymbolContextForFilePath(
582
    const char *file_path, uint32_t line, bool check_inlines,
583
0
    lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
584
0
  FileSpec file_spec(file_path);
585
0
  return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
586
0
                                          resolve_scope, sc_list);
587
0
}
588
589
uint32_t Module::ResolveSymbolContextsForFileSpec(
590
    const FileSpec &file_spec, uint32_t line, bool check_inlines,
591
139
    lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
592
139
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
593
139
  LLDB_SCOPED_TIMERF("Module::ResolveSymbolContextForFilePath (%s:%u, "
594
139
                     "check_inlines = %s, resolve_scope = 0x%8.8x)",
595
139
                     file_spec.GetPath().c_str(), line,
596
139
                     check_inlines ? "yes" : "no", resolve_scope);
597
598
139
  const uint32_t initial_count = sc_list.GetSize();
599
600
139
  if (SymbolFile *symbols = GetSymbolFile()) {
601
    // TODO: Handle SourceLocationSpec column information
602
139
    SourceLocationSpec location_spec(file_spec, line, /*column=*/llvm::None,
603
139
                                     check_inlines, /*exact_match=*/false);
604
605
139
    symbols->ResolveSymbolContext(location_spec, resolve_scope, sc_list);
606
139
  }
607
608
139
  return sc_list.GetSize() - initial_count;
609
139
}
610
611
void Module::FindGlobalVariables(ConstString name,
612
                                 const CompilerDeclContext &parent_decl_ctx,
613
180k
                                 size_t max_matches, VariableList &variables) {
614
180k
  if (SymbolFile *symbols = GetSymbolFile())
615
180k
    symbols->FindGlobalVariables(name, parent_decl_ctx, max_matches, variables);
616
180k
}
617
618
void Module::FindGlobalVariables(const RegularExpression &regex,
619
0
                                 size_t max_matches, VariableList &variables) {
620
0
  SymbolFile *symbols = GetSymbolFile();
621
0
  if (symbols)
622
0
    symbols->FindGlobalVariables(regex, max_matches, variables);
623
0
}
624
625
void Module::FindCompileUnits(const FileSpec &path,
626
960
                              SymbolContextList &sc_list) {
627
960
  const size_t num_compile_units = GetNumCompileUnits();
628
960
  SymbolContext sc;
629
960
  sc.module_sp = shared_from_this();
630
1.00k
  for (size_t i = 0; i < num_compile_units; 
++i42
) {
631
42
    sc.comp_unit = GetCompileUnitAtIndex(i).get();
632
42
    if (sc.comp_unit) {
633
42
      if (FileSpec::Match(path, sc.comp_unit->GetPrimaryFile()))
634
24
        sc_list.Append(sc);
635
42
    }
636
42
  }
637
960
}
638
639
Module::LookupInfo::LookupInfo(ConstString name,
640
                               FunctionNameType name_type_mask,
641
                               LanguageType language)
642
    : m_name(name), m_lookup_name(), m_language(language),
643
      m_name_type_mask(eFunctionNameTypeNone),
644
29.9k
      m_match_name_after_lookup(false) {
645
29.9k
  const char *name_cstr = name.GetCString();
646
29.9k
  llvm::StringRef basename;
647
29.9k
  llvm::StringRef context;
648
649
29.9k
  if (name_type_mask & eFunctionNameTypeAuto) {
650
18.2k
    if (CPlusPlusLanguage::IsCPPMangledName(name_cstr))
651
206
      m_name_type_mask = eFunctionNameTypeFull;
652
18.0k
    else if ((language == eLanguageTypeUnknown ||
653
18.0k
              
Language::LanguageIsObjC(language)15
) &&
654
18.0k
             
ObjCLanguage::IsPossibleObjCMethodName(name_cstr)18.0k
)
655
13.5k
      m_name_type_mask = eFunctionNameTypeFull;
656
4.44k
    else if (Language::LanguageIsC(language)) {
657
6
      m_name_type_mask = eFunctionNameTypeFull;
658
4.43k
    } else {
659
4.43k
      if ((language == eLanguageTypeUnknown ||
660
4.43k
           
Language::LanguageIsObjC(language)9
) &&
661
4.43k
          
ObjCLanguage::IsPossibleObjCSelector(name_cstr)4.43k
)
662
3.36k
        m_name_type_mask |= eFunctionNameTypeSelector;
663
664
4.43k
      CPlusPlusLanguage::MethodName cpp_method(name);
665
4.43k
      basename = cpp_method.GetBasename();
666
4.43k
      if (basename.empty()) {
667
4.41k
        if (CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
668
4.41k
                                                           basename))
669
3.04k
          m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
670
1.37k
        else
671
1.37k
          m_name_type_mask |= eFunctionNameTypeFull;
672
4.41k
      } else {
673
22
        m_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
674
22
      }
675
4.43k
    }
676
18.2k
  } else {
677
11.7k
    m_name_type_mask = name_type_mask;
678
11.7k
    if (name_type_mask & eFunctionNameTypeMethod ||
679
11.7k
        
name_type_mask & eFunctionNameTypeBase11.7k
) {
680
      // If they've asked for a CPP method or function name and it can't be
681
      // that, we don't even need to search for CPP methods or names.
682
4.99k
      CPlusPlusLanguage::MethodName cpp_method(name);
683
4.99k
      if (cpp_method.IsValid()) {
684
0
        basename = cpp_method.GetBasename();
685
686
0
        if (!cpp_method.GetQualifiers().empty()) {
687
          // There is a "const" or other qualifier following the end of the
688
          // function parens, this can't be a eFunctionNameTypeBase
689
0
          m_name_type_mask &= ~(eFunctionNameTypeBase);
690
0
          if (m_name_type_mask == eFunctionNameTypeNone)
691
0
            return;
692
0
        }
693
4.99k
      } else {
694
        // If the CPP method parser didn't manage to chop this up, try to fill
695
        // in the base name if we can. If a::b::c is passed in, we need to just
696
        // look up "c", and then we'll filter the result later.
697
4.99k
        CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
698
4.99k
                                                       basename);
699
4.99k
      }
700
4.99k
    }
701
702
11.7k
    if (name_type_mask & eFunctionNameTypeSelector) {
703
21
      if (!ObjCLanguage::IsPossibleObjCSelector(name_cstr)) {
704
0
        m_name_type_mask &= ~(eFunctionNameTypeSelector);
705
0
        if (m_name_type_mask == eFunctionNameTypeNone)
706
0
          return;
707
0
      }
708
21
    }
709
710
    // Still try and get a basename in case someone specifies a name type mask
711
    // of eFunctionNameTypeFull and a name like "A::func"
712
11.7k
    if (basename.empty()) {
713
6.72k
      if (name_type_mask & eFunctionNameTypeFull &&
714
6.72k
          
!CPlusPlusLanguage::IsCPPMangledName(name_cstr)6.70k
) {
715
6.70k
        CPlusPlusLanguage::MethodName cpp_method(name);
716
6.70k
        basename = cpp_method.GetBasename();
717
6.70k
        if (basename.empty())
718
6.70k
          CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context,
719
6.70k
                                                         basename);
720
6.70k
      }
721
6.72k
    }
722
11.7k
  }
723
724
29.9k
  if (!basename.empty()) {
725
    // The name supplied was a partial C++ path like "a::count". In this case
726
    // we want to do a lookup on the basename "count" and then make sure any
727
    // matching results contain "a::count" so that it would match "b::a::count"
728
    // and "a::count". This is why we set "match_name_after_lookup" to true
729
14.7k
    m_lookup_name.SetString(basename);
730
14.7k
    m_match_name_after_lookup = true;
731
15.2k
  } else {
732
    // The name is already correct, just use the exact name as supplied, and we
733
    // won't need to check if any matches contain "name"
734
15.2k
    m_lookup_name = name;
735
15.2k
    m_match_name_after_lookup = false;
736
15.2k
  }
737
29.9k
}
738
739
void Module::LookupInfo::Prune(SymbolContextList &sc_list,
740
25.7k
                               size_t start_idx) const {
741
25.7k
  if (m_match_name_after_lookup && 
m_name10.5k
) {
742
10.5k
    SymbolContext sc;
743
10.5k
    size_t i = start_idx;
744
32.4k
    while (i < sc_list.GetSize()) {
745
21.9k
      if (!sc_list.GetContextAtIndex(i, sc))
746
0
        break;
747
21.9k
      ConstString full_name(sc.GetFunctionName());
748
21.9k
      if (full_name &&
749
21.9k
          
::strstr(full_name.GetCString(), m_name.GetCString()) == nullptr21.8k
) {
750
74
        sc_list.RemoveContextAtIndex(i);
751
21.8k
      } else {
752
21.8k
        ++i;
753
21.8k
      }
754
21.9k
    }
755
10.5k
  }
756
757
  // If we have only full name matches we might have tried to set breakpoint on
758
  // "func" and specified eFunctionNameTypeFull, but we might have found
759
  // "a::func()", "a::b::func()", "c::func()", "func()" and "func". Only
760
  // "func()" and "func" should end up matching.
761
25.7k
  if (m_name_type_mask == eFunctionNameTypeFull) {
762
17.4k
    SymbolContext sc;
763
17.4k
    size_t i = start_idx;
764
34.8k
    while (i < sc_list.GetSize()) {
765
17.4k
      if (!sc_list.GetContextAtIndex(i, sc))
766
0
        break;
767
      // Make sure the mangled and demangled names don't match before we try to
768
      // pull anything out
769
17.4k
      ConstString mangled_name(sc.GetFunctionName(Mangled::ePreferMangled));
770
17.4k
      ConstString full_name(sc.GetFunctionName());
771
17.4k
      if (mangled_name != m_name && 
full_name != m_name24
) {
772
24
        CPlusPlusLanguage::MethodName cpp_method(full_name);
773
24
        if (cpp_method.IsValid()) {
774
12
          if (cpp_method.GetContext().empty()) {
775
12
            if (cpp_method.GetBasename().compare(m_name.GetStringRef()) != 0) {
776
0
              sc_list.RemoveContextAtIndex(i);
777
0
              continue;
778
0
            }
779
12
          } else {
780
0
            std::string qualified_name;
781
0
            llvm::StringRef anon_prefix("(anonymous namespace)");
782
0
            if (cpp_method.GetContext() == anon_prefix)
783
0
              qualified_name = cpp_method.GetBasename().str();
784
0
            else
785
0
              qualified_name = cpp_method.GetScopeQualifiedName();
786
0
            if (qualified_name != m_name.GetCString()) {
787
0
              sc_list.RemoveContextAtIndex(i);
788
0
              continue;
789
0
            }
790
0
          }
791
12
        }
792
24
      }
793
17.4k
      ++i;
794
17.4k
    }
795
17.4k
  }
796
25.7k
}
797
798
void Module::FindFunctions(ConstString name,
799
                           const CompilerDeclContext &parent_decl_ctx,
800
                           FunctionNameType name_type_mask,
801
                           const ModuleFunctionSearchOptions &options,
802
2.98M
                           SymbolContextList &sc_list) {
803
2.98M
  const size_t old_size = sc_list.GetSize();
804
805
  // Find all the functions (not symbols, but debug information functions...
806
2.98M
  SymbolFile *symbols = GetSymbolFile();
807
808
2.98M
  if (name_type_mask & eFunctionNameTypeAuto) {
809
190
    LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
810
811
190
    if (symbols) {
812
190
      symbols->FindFunctions(lookup_info.GetLookupName(), parent_decl_ctx,
813
190
                             lookup_info.GetNameTypeMask(),
814
190
                             options.include_inlines, sc_list);
815
816
      // Now check our symbol table for symbols that are code symbols if
817
      // requested
818
190
      if (options.include_symbols) {
819
190
        Symtab *symtab = symbols->GetSymtab();
820
190
        if (symtab)
821
190
          symtab->FindFunctionSymbols(lookup_info.GetLookupName(),
822
190
                                      lookup_info.GetNameTypeMask(), sc_list);
823
190
      }
824
190
    }
825
826
190
    const size_t new_size = sc_list.GetSize();
827
828
190
    if (old_size < new_size)
829
15
      lookup_info.Prune(sc_list, old_size);
830
2.98M
  } else {
831
2.98M
    if (symbols) {
832
2.94M
      symbols->FindFunctions(name, parent_decl_ctx, name_type_mask,
833
2.94M
                             options.include_inlines, sc_list);
834
835
      // Now check our symbol table for symbols that are code symbols if
836
      // requested
837
2.94M
      if (options.include_symbols) {
838
2.89M
        Symtab *symtab = symbols->GetSymtab();
839
2.89M
        if (symtab)
840
2.89M
          symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
841
2.89M
      }
842
2.94M
    }
843
2.98M
  }
844
2.98M
}
845
846
void Module::FindFunctions(const RegularExpression &regex,
847
                           const ModuleFunctionSearchOptions &options,
848
807
                           SymbolContextList &sc_list) {
849
807
  const size_t start_size = sc_list.GetSize();
850
851
807
  if (SymbolFile *symbols = GetSymbolFile()) {
852
801
    symbols->FindFunctions(regex, options.include_inlines, sc_list);
853
854
    // Now check our symbol table for symbols that are code symbols if
855
    // requested
856
801
    if (options.include_symbols) {
857
565
      Symtab *symtab = symbols->GetSymtab();
858
565
      if (symtab) {
859
565
        std::vector<uint32_t> symbol_indexes;
860
565
        symtab->AppendSymbolIndexesMatchingRegExAndType(
861
565
            regex, eSymbolTypeAny, Symtab::eDebugAny, Symtab::eVisibilityAny,
862
565
            symbol_indexes);
863
565
        const size_t num_matches = symbol_indexes.size();
864
565
        if (num_matches) {
865
15
          SymbolContext sc(this);
866
15
          const size_t end_functions_added_index = sc_list.GetSize();
867
15
          size_t num_functions_added_to_sc_list =
868
15
              end_functions_added_index - start_size;
869
15
          if (num_functions_added_to_sc_list == 0) {
870
            // No functions were added, just symbols, so we can just append
871
            // them
872
5
            for (size_t i = 0; i < num_matches; 
++i3
) {
873
3
              sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
874
3
              SymbolType sym_type = sc.symbol->GetType();
875
3
              if (sc.symbol && (sym_type == eSymbolTypeCode ||
876
3
                                
sym_type == eSymbolTypeResolver2
))
877
1
                sc_list.Append(sc);
878
3
            }
879
13
          } else {
880
13
            typedef std::map<lldb::addr_t, uint32_t> FileAddrToIndexMap;
881
13
            FileAddrToIndexMap file_addr_to_index;
882
36
            for (size_t i = start_size; i < end_functions_added_index; 
++i23
) {
883
23
              const SymbolContext &sc = sc_list[i];
884
23
              if (sc.block)
885
0
                continue;
886
23
              file_addr_to_index[sc.function->GetAddressRange()
887
23
                                     .GetBaseAddress()
888
23
                                     .GetFileAddress()] = i;
889
23
            }
890
891
13
            FileAddrToIndexMap::const_iterator end = file_addr_to_index.end();
892
            // Functions were added so we need to merge symbols into any
893
            // existing function symbol contexts
894
36
            for (size_t i = start_size; i < num_matches; 
++i23
) {
895
23
              sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
896
23
              SymbolType sym_type = sc.symbol->GetType();
897
23
              if (sc.symbol && sc.symbol->ValueIsAddress() &&
898
23
                  (sym_type == eSymbolTypeCode ||
899
23
                   
sym_type == eSymbolTypeResolver0
)) {
900
23
                FileAddrToIndexMap::const_iterator pos =
901
23
                    file_addr_to_index.find(
902
23
                        sc.symbol->GetAddressRef().GetFileAddress());
903
23
                if (pos == end)
904
0
                  sc_list.Append(sc);
905
23
                else
906
23
                  sc_list[pos->second].symbol = sc.symbol;
907
23
              }
908
23
            }
909
13
          }
910
15
        }
911
565
      }
912
565
    }
913
801
  }
914
807
}
915
916
void Module::FindAddressesForLine(const lldb::TargetSP target_sp,
917
                                  const FileSpec &file, uint32_t line,
918
                                  Function *function,
919
                                  std::vector<Address> &output_local,
920
1.29k
                                  std::vector<Address> &output_extern) {
921
1.29k
  SearchFilterByModule filter(target_sp, m_file);
922
923
  // TODO: Handle SourceLocationSpec column information
924
1.29k
  SourceLocationSpec location_spec(file, line, /*column=*/llvm::None,
925
1.29k
                                   /*check_inlines=*/true,
926
1.29k
                                   /*exact_match=*/false);
927
1.29k
  AddressResolverFileLine resolver(location_spec);
928
1.29k
  resolver.ResolveAddress(filter);
929
930
1.40k
  for (size_t n = 0; n < resolver.GetNumberOfAddresses(); 
n++111
) {
931
111
    Address addr = resolver.GetAddressRangeAtIndex(n).GetBaseAddress();
932
111
    Function *f = addr.CalculateSymbolContextFunction();
933
111
    if (f && f == function)
934
72
      output_local.push_back(addr);
935
39
    else
936
39
      output_extern.push_back(addr);
937
111
  }
938
1.29k
}
939
940
void Module::FindTypes_Impl(
941
    ConstString name, const CompilerDeclContext &parent_decl_ctx,
942
    size_t max_matches,
943
    llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
944
582k
    TypeMap &types) {
945
582k
  if (SymbolFile *symbols = GetSymbolFile())
946
581k
    symbols->FindTypes(name, parent_decl_ctx, max_matches,
947
581k
                       searched_symbol_files, types);
948
582k
}
949
950
void Module::FindTypesInNamespace(ConstString type_name,
951
                                  const CompilerDeclContext &parent_decl_ctx,
952
442
                                  size_t max_matches, TypeList &type_list) {
953
442
  TypeMap types_map;
954
442
  llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
955
442
  FindTypes_Impl(type_name, parent_decl_ctx, max_matches, searched_symbol_files,
956
442
                 types_map);
957
442
  if (types_map.GetSize()) {
958
92
    SymbolContext sc;
959
92
    sc.module_sp = shared_from_this();
960
92
    sc.SortTypeList(types_map, type_list);
961
92
  }
962
442
}
963
964
lldb::TypeSP Module::FindFirstType(const SymbolContext &sc, ConstString name,
965
192
                                   bool exact_match) {
966
192
  TypeList type_list;
967
192
  llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
968
192
  FindTypes(name, exact_match, 1, searched_symbol_files, type_list);
969
192
  if (type_list.GetSize())
970
32
    return type_list.GetTypeAtIndex(0);
971
160
  return TypeSP();
972
192
}
973
974
void Module::FindTypes(
975
    ConstString name, bool exact_match, size_t max_matches,
976
    llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
977
581k
    TypeList &types) {
978
581k
  const char *type_name_cstr = name.GetCString();
979
581k
  llvm::StringRef type_scope;
980
581k
  llvm::StringRef type_basename;
981
581k
  TypeClass type_class = eTypeClassAny;
982
581k
  TypeMap typesmap;
983
984
581k
  if (Type::GetTypeScopeAndBasename(type_name_cstr, type_scope, type_basename,
985
581k
                                    type_class)) {
986
    // Check if "name" starts with "::" which means the qualified type starts
987
    // from the root namespace and implies and exact match. The typenames we
988
    // get back from clang do not start with "::" so we need to strip this off
989
    // in order to get the qualified names to match
990
57
    exact_match = type_scope.consume_front("::");
991
992
57
    ConstString type_basename_const_str(type_basename);
993
57
    FindTypes_Impl(type_basename_const_str, CompilerDeclContext(), max_matches,
994
57
                   searched_symbol_files, typesmap);
995
57
    if (typesmap.GetSize())
996
17
      typesmap.RemoveMismatchedTypes(std::string(type_scope),
997
17
                                     std::string(type_basename), type_class,
998
17
                                     exact_match);
999
581k
  } else {
1000
    // The type is not in a namespace/class scope, just search for it by
1001
    // basename
1002
581k
    if (type_class != eTypeClassAny && 
!type_basename.empty()120
) {
1003
      // The "type_name_cstr" will have been modified if we have a valid type
1004
      // class prefix (like "struct", "class", "union", "typedef" etc).
1005
120
      FindTypes_Impl(ConstString(type_basename), CompilerDeclContext(),
1006
120
                     UINT_MAX, searched_symbol_files, typesmap);
1007
120
      typesmap.RemoveMismatchedTypes(std::string(type_scope),
1008
120
                                     std::string(type_basename), type_class,
1009
120
                                     exact_match);
1010
581k
    } else {
1011
581k
      FindTypes_Impl(name, CompilerDeclContext(), UINT_MAX,
1012
581k
                     searched_symbol_files, typesmap);
1013
581k
      if (exact_match) {
1014
565k
        std::string name_str(name.AsCString(""));
1015
565k
        typesmap.RemoveMismatchedTypes(std::string(type_scope), name_str,
1016
565k
                                       type_class, exact_match);
1017
565k
      }
1018
581k
    }
1019
581k
  }
1020
581k
  if (typesmap.GetSize()) {
1021
1.19k
    SymbolContext sc;
1022
1.19k
    sc.module_sp = shared_from_this();
1023
1.19k
    sc.SortTypeList(typesmap, types);
1024
1.19k
  }
1025
581k
}
1026
1027
void Module::FindTypes(
1028
    llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages,
1029
    llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1030
22.7k
    TypeMap &types) {
1031
22.7k
  LLDB_SCOPED_TIMER();
1032
22.7k
  if (SymbolFile *symbols = GetSymbolFile())
1033
22.7k
    symbols->FindTypes(pattern, languages, searched_symbol_files, types);
1034
22.7k
}
1035
1036
12.3M
SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) {
1037
12.3M
  if (!m_did_load_symfile.load()) {
1038
144k
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
1039
144k
    if (!m_did_load_symfile.load() && 
can_create144k
) {
1040
144k
      ObjectFile *obj_file = GetObjectFile();
1041
144k
      if (obj_file != nullptr) {
1042
144k
        LLDB_SCOPED_TIMER();
1043
144k
        m_symfile_up.reset(
1044
144k
            SymbolVendor::FindPlugin(shared_from_this(), feedback_strm));
1045
144k
        m_did_load_symfile = true;
1046
144k
      }
1047
144k
    }
1048
144k
  }
1049
18.4E
  return 
m_symfile_up12.3M
?
m_symfile_up->GetSymbolFile()12.3M
: nullptr;
1050
12.3M
}
1051
1052
3.13M
Symtab *Module::GetSymtab() {
1053
3.13M
  if (SymbolFile *symbols = GetSymbolFile())
1054
3.09M
    return symbols->GetSymtab();
1055
40.8k
  return nullptr;
1056
3.13M
}
1057
1058
void Module::SetFileSpecAndObjectName(const FileSpec &file,
1059
5.93k
                                      ConstString object_name) {
1060
  // Container objects whose paths do not specify a file directly can call this
1061
  // function to correct the file and object names.
1062
5.93k
  m_file = file;
1063
5.93k
  m_mod_time = FileSystem::Instance().GetModificationTime(file);
1064
5.93k
  m_object_name = object_name;
1065
5.93k
}
1066
1067
746k
const ArchSpec &Module::GetArchitecture() const { return m_arch; }
1068
1069
367
std::string Module::GetSpecificationDescription() const {
1070
367
  std::string spec(GetFileSpec().GetPath());
1071
367
  if (m_object_name) {
1072
0
    spec += '(';
1073
0
    spec += m_object_name.GetCString();
1074
0
    spec += ')';
1075
0
  }
1076
367
  return spec;
1077
367
}
1078
1079
void Module::GetDescription(llvm::raw_ostream &s,
1080
18.4k
                            lldb::DescriptionLevel level) {
1081
18.4k
  if (level >= eDescriptionLevelFull) {
1082
727
    if (m_arch.IsValid())
1083
727
      s << llvm::formatv("({0}) ", m_arch.GetArchitectureName());
1084
727
  }
1085
1086
18.4k
  if (level == eDescriptionLevelBrief) {
1087
17.6k
    const char *filename = m_file.GetFilename().GetCString();
1088
17.6k
    if (filename)
1089
17.6k
      s << filename;
1090
17.6k
  } else {
1091
727
    char path[PATH_MAX];
1092
727
    if (m_file.GetPath(path, sizeof(path)))
1093
727
      s << path;
1094
727
  }
1095
1096
18.4k
  const char *object_name = m_object_name.GetCString();
1097
18.4k
  if (object_name)
1098
210
    s << llvm::formatv("({0})", object_name);
1099
18.4k
}
1100
1101
11
void Module::ReportError(const char *format, ...) {
1102
11
  if (format && format[0]) {
1103
11
    StreamString strm;
1104
11
    strm.PutCString("error: ");
1105
11
    GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelBrief);
1106
11
    strm.PutChar(' ');
1107
11
    va_list args;
1108
11
    va_start(args, format);
1109
11
    strm.PrintfVarArg(format, args);
1110
11
    va_end(args);
1111
1112
11
    const int format_len = strlen(format);
1113
11
    if (format_len > 0) {
1114
11
      const char last_char = format[format_len - 1];
1115
11
      if (last_char != '\n' && last_char != '\r')
1116
11
        strm.EOL();
1117
11
    }
1118
11
    Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1119
11
  }
1120
11
}
1121
1122
145k
bool Module::FileHasChanged() const {
1123
  // We have provided the DataBuffer for this module to avoid accessing the
1124
  // filesystem. We never want to reload those files.
1125
145k
  if (m_data_sp)
1126
133k
    return false;
1127
12.3k
  if (!m_file_has_changed)
1128
12.3k
    m_file_has_changed =
1129
12.3k
        (FileSystem::Instance().GetModificationTime(m_file) != m_mod_time);
1130
12.3k
  return m_file_has_changed;
1131
145k
}
1132
1133
1
void Module::ReportErrorIfModifyDetected(const char *format, ...) {
1134
1
  if (!m_first_file_changed_log) {
1135
1
    if (FileHasChanged()) {
1136
0
      m_first_file_changed_log = true;
1137
0
      if (format) {
1138
0
        StreamString strm;
1139
0
        strm.PutCString("error: the object file ");
1140
0
        GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull);
1141
0
        strm.PutCString(" has been modified\n");
1142
1143
0
        va_list args;
1144
0
        va_start(args, format);
1145
0
        strm.PrintfVarArg(format, args);
1146
0
        va_end(args);
1147
1148
0
        const int format_len = strlen(format);
1149
0
        if (format_len > 0) {
1150
0
          const char last_char = format[format_len - 1];
1151
0
          if (last_char != '\n' && last_char != '\r')
1152
0
            strm.EOL();
1153
0
        }
1154
0
        strm.PutCString("The debug session should be aborted as the original "
1155
0
                        "debug information has been overwritten.\n");
1156
0
        Host::SystemLog(Host::eSystemLogError, "%s", strm.GetData());
1157
0
      }
1158
0
    }
1159
1
  }
1160
1
}
1161
1162
5
void Module::ReportWarning(const char *format, ...) {
1163
5
  if (format && format[0]) {
1164
5
    StreamString strm;
1165
5
    strm.PutCString("warning: ");
1166
5
    GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull);
1167
5
    strm.PutChar(' ');
1168
1169
5
    va_list args;
1170
5
    va_start(args, format);
1171
5
    strm.PrintfVarArg(format, args);
1172
5
    va_end(args);
1173
1174
5
    const int format_len = strlen(format);
1175
5
    if (format_len > 0) {
1176
5
      const char last_char = format[format_len - 1];
1177
5
      if (last_char != '\n' && last_char != '\r')
1178
5
        strm.EOL();
1179
5
    }
1180
5
    Host::SystemLog(Host::eSystemLogWarning, "%s", strm.GetData());
1181
5
  }
1182
5
}
1183
1184
70
void Module::LogMessage(Log *log, const char *format, ...) {
1185
70
  if (log != nullptr) {
1186
70
    StreamString log_message;
1187
70
    GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull);
1188
70
    log_message.PutCString(": ");
1189
70
    va_list args;
1190
70
    va_start(args, format);
1191
70
    log_message.PrintfVarArg(format, args);
1192
70
    va_end(args);
1193
70
    log->PutCString(log_message.GetData());
1194
70
  }
1195
70
}
1196
1197
0
void Module::LogMessageVerboseBacktrace(Log *log, const char *format, ...) {
1198
0
  if (log != nullptr) {
1199
0
    StreamString log_message;
1200
0
    GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull);
1201
0
    log_message.PutCString(": ");
1202
0
    va_list args;
1203
0
    va_start(args, format);
1204
0
    log_message.PrintfVarArg(format, args);
1205
0
    va_end(args);
1206
0
    if (log->GetVerbose()) {
1207
0
      std::string back_trace;
1208
0
      llvm::raw_string_ostream stream(back_trace);
1209
0
      llvm::sys::PrintStackTrace(stream);
1210
0
      log_message.PutCString(back_trace);
1211
0
    }
1212
0
    log->PutCString(log_message.GetData());
1213
0
  }
1214
0
}
1215
1216
131
void Module::Dump(Stream *s) {
1217
131
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1218
  // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1219
131
  s->Indent();
1220
131
  s->Printf("Module %s%s%s%s\n", m_file.GetPath().c_str(),
1221
131
            m_object_name ? 
"("0
: "",
1222
131
            m_object_name ? 
m_object_name.GetCString()0
: "",
1223
131
            m_object_name ? 
")"0
: "");
1224
1225
131
  s->IndentMore();
1226
1227
131
  ObjectFile *objfile = GetObjectFile();
1228
131
  if (objfile)
1229
131
    objfile->Dump(s);
1230
1231
131
  if (SymbolFile *symbols = GetSymbolFile())
1232
131
    symbols->Dump(*s);
1233
1234
131
  s->IndentLess();
1235
131
}
1236
1237
14.9k
ConstString Module::GetObjectName() const { return m_object_name; }
1238
1239
4.76M
ObjectFile *Module::GetObjectFile() {
1240
4.76M
  if (!m_did_load_objfile.load()) {
1241
139k
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
1242
139k
    if (!m_did_load_objfile.load()) {
1243
139k
      LLDB_SCOPED_TIMERF("Module::GetObjectFile () module = %s",
1244
139k
                         GetFileSpec().GetFilename().AsCString(""));
1245
139k
      lldb::offset_t data_offset = 0;
1246
139k
      lldb::offset_t file_size = 0;
1247
1248
139k
      if (m_data_sp)
1249
127k
        file_size = m_data_sp->GetByteSize();
1250
11.6k
      else if (m_file)
1251
10.7k
        file_size = FileSystem::Instance().GetByteSize(m_file);
1252
1253
139k
      if (file_size > m_object_offset) {
1254
138k
        m_did_load_objfile = true;
1255
        // FindPlugin will modify its data_sp argument. Do not let it
1256
        // modify our m_data_sp member.
1257
138k
        auto data_sp = m_data_sp;
1258
138k
        m_objfile_sp = ObjectFile::FindPlugin(
1259
138k
            shared_from_this(), &m_file, m_object_offset,
1260
138k
            file_size - m_object_offset, data_sp, data_offset);
1261
138k
        if (m_objfile_sp) {
1262
          // Once we get the object file, update our module with the object
1263
          // file's architecture since it might differ in vendor/os if some
1264
          // parts were unknown.  But since the matching arch might already be
1265
          // more specific than the generic COFF architecture, only merge in
1266
          // those values that overwrite unspecified unknown values.
1267
138k
          m_arch.MergeFrom(m_objfile_sp->GetArchitecture());
1268
138k
        } else {
1269
0
          ReportError("failed to load objfile for %s",
1270
0
                      GetFileSpec().GetPath().c_str());
1271
0
        }
1272
138k
      }
1273
139k
    }
1274
139k
  }
1275
4.76M
  return m_objfile_sp.get();
1276
4.76M
}
1277
1278
14.4M
SectionList *Module::GetSectionList() {
1279
  // Populate m_sections_up with sections from objfile.
1280
14.4M
  if (!m_sections_up) {
1281
136k
    ObjectFile *obj_file = GetObjectFile();
1282
136k
    if (obj_file != nullptr)
1283
136k
      obj_file->CreateSections(*GetUnifiedSectionList());
1284
136k
  }
1285
14.4M
  return m_sections_up.get();
1286
14.4M
}
1287
1288
0
void Module::SectionFileAddressesChanged() {
1289
0
  ObjectFile *obj_file = GetObjectFile();
1290
0
  if (obj_file)
1291
0
    obj_file->SectionFileAddressesChanged();
1292
0
  if (SymbolFile *symbols = GetSymbolFile())
1293
0
    symbols->SectionFileAddressesChanged();
1294
0
}
1295
1296
107k
UnwindTable &Module::GetUnwindTable() {
1297
107k
  if (!m_unwind_table)
1298
8.05k
    m_unwind_table.emplace(*this);
1299
107k
  return *m_unwind_table;
1300
107k
}
1301
1302
145k
SectionList *Module::GetUnifiedSectionList() {
1303
145k
  if (!m_sections_up)
1304
144k
    m_sections_up = std::make_unique<SectionList>();
1305
145k
  return m_sections_up.get();
1306
145k
}
1307
1308
const Symbol *Module::FindFirstSymbolWithNameAndType(ConstString name,
1309
150k
                                                     SymbolType symbol_type) {
1310
150k
  LLDB_SCOPED_TIMERF(
1311
150k
      "Module::FindFirstSymbolWithNameAndType (name = %s, type = %i)",
1312
150k
      name.AsCString(), symbol_type);
1313
150k
  if (Symtab *symtab = GetSymtab())
1314
150k
    return symtab->FindFirstSymbolWithNameAndType(
1315
150k
        name, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny);
1316
100
  return nullptr;
1317
150k
}
1318
void Module::SymbolIndicesToSymbolContextList(
1319
    Symtab *symtab, std::vector<uint32_t> &symbol_indexes,
1320
752k
    SymbolContextList &sc_list) {
1321
  // No need to protect this call using m_mutex all other method calls are
1322
  // already thread safe.
1323
1324
752k
  size_t num_indices = symbol_indexes.size();
1325
752k
  if (num_indices > 0) {
1326
8.71k
    SymbolContext sc;
1327
8.71k
    CalculateSymbolContext(&sc);
1328
18.3k
    for (size_t i = 0; i < num_indices; 
i++9.61k
) {
1329
9.61k
      sc.symbol = symtab->SymbolAtIndex(symbol_indexes[i]);
1330
9.61k
      if (sc.symbol)
1331
9.61k
        sc_list.Append(sc);
1332
9.61k
    }
1333
8.71k
  }
1334
752k
}
1335
1336
void Module::FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
1337
10.2k
                                 SymbolContextList &sc_list) {
1338
10.2k
  LLDB_SCOPED_TIMERF("Module::FindSymbolsFunctions (name = %s, mask = 0x%8.8x)",
1339
10.2k
                     name.AsCString(), name_type_mask);
1340
10.2k
  if (Symtab *symtab = GetSymtab())
1341
10.2k
    symtab->FindFunctionSymbols(name, name_type_mask, sc_list);
1342
10.2k
}
1343
1344
void Module::FindSymbolsWithNameAndType(ConstString name,
1345
                                        SymbolType symbol_type,
1346
758k
                                        SymbolContextList &sc_list) {
1347
  // No need to protect this call using m_mutex all other method calls are
1348
  // already thread safe.
1349
758k
  if (Symtab *symtab = GetSymtab()) {
1350
752k
    std::vector<uint32_t> symbol_indexes;
1351
752k
    symtab->FindAllSymbolsWithNameAndType(name, symbol_type, symbol_indexes);
1352
752k
    SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
1353
752k
  }
1354
758k
}
1355
1356
void Module::FindSymbolsMatchingRegExAndType(const RegularExpression &regex,
1357
                                             SymbolType symbol_type,
1358
0
                                             SymbolContextList &sc_list) {
1359
  // No need to protect this call using m_mutex all other method calls are
1360
  // already thread safe.
1361
0
  LLDB_SCOPED_TIMERF(
1362
0
      "Module::FindSymbolsMatchingRegExAndType (regex = %s, type = %i)",
1363
0
      regex.GetText().str().c_str(), symbol_type);
1364
0
  if (Symtab *symtab = GetSymtab()) {
1365
0
    std::vector<uint32_t> symbol_indexes;
1366
0
    symtab->FindAllSymbolsMatchingRexExAndType(
1367
0
        regex, symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny,
1368
0
        symbol_indexes);
1369
0
    SymbolIndicesToSymbolContextList(symtab, symbol_indexes, sc_list);
1370
0
  }
1371
0
}
1372
1373
272k
void Module::PreloadSymbols() {
1374
272k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1375
272k
  SymbolFile *sym_file = GetSymbolFile();
1376
272k
  if (!sym_file)
1377
1.55k
    return;
1378
1379
  // Load the object file symbol table and any symbols from the SymbolFile that
1380
  // get appended using SymbolFile::AddSymbols(...).
1381
270k
  if (Symtab *symtab = sym_file->GetSymtab())
1382
270k
    symtab->PreloadSymbols();
1383
1384
  // Now let the symbol file preload its data and the symbol table will be
1385
  // available without needing to take the module lock.
1386
270k
  sym_file->PreloadSymbols();
1387
1388
270k
}
1389
1390
26
void Module::SetSymbolFileFileSpec(const FileSpec &file) {
1391
26
  if (!FileSystem::Instance().Exists(file))
1392
1
    return;
1393
25
  if (m_symfile_up) {
1394
    // Remove any sections in the unified section list that come from the
1395
    // current symbol vendor.
1396
22
    SectionList *section_list = GetSectionList();
1397
22
    SymbolFile *symbol_file = GetSymbolFile();
1398
22
    if (section_list && symbol_file) {
1399
15
      ObjectFile *obj_file = symbol_file->GetObjectFile();
1400
      // Make sure we have an object file and that the symbol vendor's objfile
1401
      // isn't the same as the module's objfile before we remove any sections
1402
      // for it...
1403
15
      if (obj_file) {
1404
        // Check to make sure we aren't trying to specify the file we already
1405
        // have
1406
15
        if (obj_file->GetFileSpec() == file) {
1407
          // We are being told to add the exact same file that we already have
1408
          // we don't have to do anything.
1409
2
          return;
1410
2
        }
1411
1412
        // Cleare the current symtab as we are going to replace it with a new
1413
        // one
1414
13
        obj_file->ClearSymtab();
1415
1416
        // Clear the unwind table too, as that may also be affected by the
1417
        // symbol file information.
1418
13
        m_unwind_table.reset();
1419
1420
        // The symbol file might be a directory bundle ("/tmp/a.out.dSYM")
1421
        // instead of a full path to the symbol file within the bundle
1422
        // ("/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out"). So we need to
1423
        // check this
1424
1425
13
        if (FileSystem::Instance().IsDirectory(file)) {
1426
0
          std::string new_path(file.GetPath());
1427
0
          std::string old_path(obj_file->GetFileSpec().GetPath());
1428
0
          if (llvm::StringRef(old_path).startswith(new_path)) {
1429
            // We specified the same bundle as the symbol file that we already
1430
            // have
1431
0
            return;
1432
0
          }
1433
0
        }
1434
1435
13
        if (obj_file != m_objfile_sp.get()) {
1436
0
          size_t num_sections = section_list->GetNumSections(0);
1437
0
          for (size_t idx = num_sections; idx > 0; --idx) {
1438
0
            lldb::SectionSP section_sp(
1439
0
                section_list->GetSectionAtIndex(idx - 1));
1440
0
            if (section_sp->GetObjectFile() == obj_file) {
1441
0
              section_list->DeleteSection(idx - 1);
1442
0
            }
1443
0
          }
1444
0
        }
1445
13
      }
1446
15
    }
1447
    // Keep all old symbol files around in case there are any lingering type
1448
    // references in any SBValue objects that might have been handed out.
1449
20
    m_old_symfiles.push_back(std::move(m_symfile_up));
1450
20
  }
1451
23
  m_symfile_spec = file;
1452
23
  m_symfile_up.reset();
1453
23
  m_did_load_symfile = false;
1454
23
}
1455
1456
562k
bool Module::IsExecutable() {
1457
562k
  if (GetObjectFile() == nullptr)
1458
0
    return false;
1459
562k
  else
1460
562k
    return GetObjectFile()->IsExecutable();
1461
562k
}
1462
1463
2.96k
bool Module::IsLoadedInTarget(Target *target) {
1464
2.96k
  ObjectFile *obj_file = GetObjectFile();
1465
2.96k
  if (obj_file) {
1466
2.96k
    SectionList *sections = GetSectionList();
1467
2.96k
    if (sections != nullptr) {
1468
2.96k
      size_t num_sections = sections->GetSize();
1469
4.05k
      for (size_t sect_idx = 0; sect_idx < num_sections; 
sect_idx++1.09k
) {
1470
3.79k
        SectionSP section_sp = sections->GetSectionAtIndex(sect_idx);
1471
3.79k
        if (section_sp->GetLoadBaseAddress(target) != LLDB_INVALID_ADDRESS) {
1472
2.69k
          return true;
1473
2.69k
        }
1474
3.79k
      }
1475
2.96k
    }
1476
2.96k
  }
1477
264
  return false;
1478
2.96k
}
1479
1480
bool Module::LoadScriptingResourceInTarget(Target *target, Status &error,
1481
278k
                                           Stream *feedback_stream) {
1482
278k
  if (!target) {
1483
0
    error.SetErrorString("invalid destination Target");
1484
0
    return false;
1485
0
  }
1486
1487
278k
  LoadScriptFromSymFile should_load =
1488
278k
      target->TargetProperties::GetLoadScriptFromSymbolFile();
1489
1490
278k
  if (should_load == eLoadScriptFromSymFileFalse)
1491
0
    return false;
1492
1493
278k
  Debugger &debugger = target->GetDebugger();
1494
278k
  const ScriptLanguage script_language = debugger.GetScriptLanguage();
1495
278k
  if (script_language != eScriptLanguageNone) {
1496
1497
278k
    PlatformSP platform_sp(target->GetPlatform());
1498
1499
278k
    if (!platform_sp) {
1500
0
      error.SetErrorString("invalid Platform");
1501
0
      return false;
1502
0
    }
1503
1504
278k
    FileSpecList file_specs = platform_sp->LocateExecutableScriptingResources(
1505
278k
        target, *this, feedback_stream);
1506
1507
278k
    const uint32_t num_specs = file_specs.GetSize();
1508
278k
    if (num_specs) {
1509
2
      ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
1510
2
      if (script_interpreter) {
1511
4
        for (uint32_t i = 0; i < num_specs; 
++i2
) {
1512
2
          FileSpec scripting_fspec(file_specs.GetFileSpecAtIndex(i));
1513
2
          if (scripting_fspec &&
1514
2
              FileSystem::Instance().Exists(scripting_fspec)) {
1515
2
            if (should_load == eLoadScriptFromSymFileWarn) {
1516
0
              if (feedback_stream)
1517
0
                feedback_stream->Printf(
1518
0
                    "warning: '%s' contains a debug script. To run this script "
1519
0
                    "in "
1520
0
                    "this debug session:\n\n    command script import "
1521
0
                    "\"%s\"\n\n"
1522
0
                    "To run all discovered debug scripts in this session:\n\n"
1523
0
                    "    settings set target.load-script-from-symbol-file "
1524
0
                    "true\n",
1525
0
                    GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1526
0
                    scripting_fspec.GetPath().c_str());
1527
0
              return false;
1528
0
            }
1529
2
            StreamString scripting_stream;
1530
2
            scripting_fspec.Dump(scripting_stream.AsRawOstream());
1531
2
            LoadScriptOptions options;
1532
2
            bool did_load = script_interpreter->LoadScriptingModule(
1533
2
                scripting_stream.GetData(), options, error);
1534
2
            if (!did_load)
1535
0
              return false;
1536
2
          }
1537
2
        }
1538
2
      } else {
1539
0
        error.SetErrorString("invalid ScriptInterpreter");
1540
0
        return false;
1541
0
      }
1542
2
    }
1543
278k
  }
1544
278k
  return true;
1545
278k
}
1546
1547
139k
bool Module::SetArchitecture(const ArchSpec &new_arch) {
1548
139k
  if (!m_arch.IsValid()) {
1549
135
    m_arch = new_arch;
1550
135
    return true;
1551
135
  }
1552
139k
  return m_arch.IsCompatibleMatch(new_arch);
1553
139k
}
1554
1555
bool Module::SetLoadAddress(Target &target, lldb::addr_t value,
1556
6.18k
                            bool value_is_offset, bool &changed) {
1557
6.18k
  ObjectFile *object_file = GetObjectFile();
1558
6.18k
  if (object_file != nullptr) {
1559
6.18k
    changed = object_file->SetLoadAddress(target, value, value_is_offset);
1560
6.18k
    return true;
1561
6.18k
  } else {
1562
0
    changed = false;
1563
0
  }
1564
0
  return false;
1565
6.18k
}
1566
1567
33.6M
bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) {
1568
33.6M
  const UUID &uuid = module_ref.GetUUID();
1569
1570
33.6M
  if (uuid.IsValid()) {
1571
    // If the UUID matches, then nothing more needs to match...
1572
23.0M
    return (uuid == GetUUID());
1573
23.0M
  }
1574
1575
10.6M
  const FileSpec &file_spec = module_ref.GetFileSpec();
1576
10.6M
  if (!FileSpec::Match(file_spec, m_file) &&
1577
10.6M
      
!FileSpec::Match(file_spec, m_platform_file)10.5M
)
1578
10.5M
    return false;
1579
1580
4.92k
  const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec();
1581
4.92k
  if (!FileSpec::Match(platform_file_spec, GetPlatformFileSpec()))
1582
0
    return false;
1583
1584
4.92k
  const ArchSpec &arch = module_ref.GetArchitecture();
1585
6.96k
  if (
arch.IsValid()4.92k
) {
1586
6.96k
    if (!m_arch.IsCompatibleMatch(arch))
1587
5
      return false;
1588
6.96k
  }
1589
1590
4.92k
  ConstString object_name = module_ref.GetObjectName();
1591
4.92k
  if (object_name) {
1592
0
    if (object_name != GetObjectName())
1593
0
      return false;
1594
0
  }
1595
4.92k
  return true;
1596
4.92k
}
1597
1598
bool Module::FindSourceFile(const FileSpec &orig_spec,
1599
98
                            FileSpec &new_spec) const {
1600
98
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1601
98
  if (auto remapped = m_source_mappings.FindFile(orig_spec)) {
1602
0
    new_spec = *remapped;
1603
0
    return true;
1604
0
  }
1605
98
  return false;
1606
98
}
1607
1608
llvm::Optional<std::string>
1609
126k
Module::RemapSourceFile(llvm::StringRef path) const {
1610
126k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1611
126k
  if (auto remapped = m_source_mappings.RemapPath(path))
1612
8
    return remapped->GetPath();
1613
126k
  return {};
1614
126k
}
1615
1616
void Module::RegisterXcodeSDK(llvm::StringRef sdk_name,
1617
1
                              llvm::StringRef sysroot) {
1618
1
  XcodeSDK sdk(sdk_name.str());
1619
1
  llvm::StringRef sdk_path(HostInfo::GetXcodeSDKPath(sdk));
1620
1
  if (sdk_path.empty())
1621
0
    return;
1622
  // If the SDK changed for a previously registered source path, update it.
1623
  // This could happend with -fdebug-prefix-map, otherwise it's unlikely.
1624
1
  if (!m_source_mappings.Replace(sysroot, sdk_path, true))
1625
    // In the general case, however, append it to the list.
1626
1
    m_source_mappings.Append(sysroot, sdk_path, false);
1627
1
}
1628
1629
117
bool Module::MergeArchitecture(const ArchSpec &arch_spec) {
1630
117
  if (!arch_spec.IsValid())
1631
0
    return false;
1632
117
  LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_MODULES),
1633
117
            "module has arch %s, merging/replacing with arch %s",
1634
117
            m_arch.GetTriple().getTriple().c_str(),
1635
117
            arch_spec.GetTriple().getTriple().c_str());
1636
117
  if (!m_arch.IsCompatibleMatch(arch_spec)) {
1637
    // The new architecture is different, we just need to replace it.
1638
0
    return SetArchitecture(arch_spec);
1639
0
  }
1640
1641
  // Merge bits from arch_spec into "merged_arch" and set our architecture.
1642
117
  ArchSpec merged_arch(m_arch);
1643
117
  merged_arch.MergeFrom(arch_spec);
1644
  // SetArchitecture() is a no-op if m_arch is already valid.
1645
117
  m_arch = ArchSpec();
1646
117
  return SetArchitecture(merged_arch);
1647
117
}
1648
1649
121
llvm::VersionTuple Module::GetVersion() {
1650
121
  if (ObjectFile *obj_file = GetObjectFile())
1651
121
    return obj_file->GetVersion();
1652
0
  return llvm::VersionTuple();
1653
121
}
1654
1655
0
bool Module::GetIsDynamicLinkEditor() {
1656
0
  ObjectFile *obj_file = GetObjectFile();
1657
1658
0
  if (obj_file)
1659
0
    return obj_file->GetIsDynamicLinkEditor();
1660
1661
0
  return false;
1662
0
}
1663
1664
21
uint32_t Module::Hash() {
1665
21
  std::string identifier;
1666
21
  llvm::raw_string_ostream id_strm(identifier);
1667
21
  id_strm << m_arch.GetTriple().str() << '-' << m_file.GetPath();
1668
21
  if (m_object_name)
1669
6
    id_strm << '(' << m_object_name.GetStringRef() << ')';
1670
21
  if (m_object_offset > 0)
1671
4
    id_strm << m_object_offset;
1672
21
  const auto mtime = llvm::sys::toTimeT(m_object_mod_time);
1673
21
  if (mtime > 0)
1674
6
    id_strm << mtime;
1675
21
  return llvm::djbHash(id_strm.str());
1676
21
}
1677
1678
21
std::string Module::GetCacheKey() {
1679
21
  std::string key;
1680
21
  llvm::raw_string_ostream strm(key);
1681
21
  strm << m_arch.GetTriple().str() << '-' << m_file.GetFilename();
1682
21
  if (m_object_name)
1683
6
    strm << '(' << m_object_name.GetStringRef() << ')';
1684
21
  strm << '-' << llvm::format_hex(Hash(), 10);
1685
21
  return strm.str();
1686
21
}
1687
1688
291k
DataFileCache *Module::GetIndexCache() {
1689
291k
  if (!ModuleList::GetGlobalModuleListProperties().GetEnableLLDBIndexCache())
1690
291k
    return nullptr;
1691
  // NOTE: intentional leak so we don't crash if global destructor chain gets
1692
  // called as other threads still use the result of this function
1693
20
  static DataFileCache *g_data_file_cache = new DataFileCache(ModuleList::GetGlobalModuleListProperties().GetLLDBIndexCachePath().GetPath());
1694
20
  return g_data_file_cache;
1695
291k
}