aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Core/ModuleSpec.h
blob: 195fd991f8dee18008498d54c78231409794695d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//===-- ModuleSpec.h --------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_ModuleSpec_h_
#define liblldb_ModuleSpec_h_

#include "lldb/Core/ArchSpec.h"
#include "lldb/Core/Stream.h"
#include "lldb/Core/UUID.h"
#include "lldb/Host/FileSpec.h"
#include "lldb/Target/PathMappingList.h"

namespace lldb_private {

class ModuleSpec
{
public:
    ModuleSpec () :
        m_file (),
        m_platform_file (),
        m_symbol_file (),
        m_arch (),
        m_uuid (),
        m_object_name (),
        m_object_offset (0),
        m_object_mod_time (),
        m_source_mappings ()
    {
    }

    ModuleSpec (const FileSpec &file_spec) :
        m_file (file_spec),
        m_platform_file (),
        m_symbol_file (),
        m_arch (),
        m_uuid (),
        m_object_name (),
        m_object_offset (0),
        m_object_mod_time (),
        m_source_mappings ()
    {
    }

    ModuleSpec (const FileSpec &file_spec, const ArchSpec &arch) :
        m_file (file_spec),
        m_platform_file (),
        m_symbol_file (),
        m_arch (arch),
        m_uuid (),
        m_object_name (),
        m_object_offset (0),
        m_object_mod_time (),
        m_source_mappings ()
    {
    }
    
    ModuleSpec (const ModuleSpec &rhs) :
        m_file (rhs.m_file),
        m_platform_file (rhs.m_platform_file),
        m_symbol_file (rhs.m_symbol_file),
        m_arch (rhs.m_arch),
        m_uuid (rhs.m_uuid),
        m_object_name (rhs.m_object_name),
        m_object_offset (rhs.m_object_offset),
        m_object_mod_time (rhs.m_object_mod_time),
        m_source_mappings (rhs.m_source_mappings)
    {
    }

    ModuleSpec &
    operator = (const ModuleSpec &rhs)
    {
        if (this != &rhs)
        {
            m_file = rhs.m_file;
            m_platform_file = rhs.m_platform_file;
            m_symbol_file = rhs.m_symbol_file;
            m_arch = rhs.m_arch;
            m_uuid = rhs.m_uuid;
            m_object_name = rhs.m_object_name;
            m_object_offset = rhs.m_object_offset;
            m_object_mod_time = rhs.m_object_mod_time;
            m_source_mappings = rhs.m_source_mappings;
        }
        return *this;
    }

    FileSpec *
    GetFileSpecPtr ()
    {
        if (m_file)
            return &m_file;
        return NULL;
    }

    const FileSpec *
    GetFileSpecPtr () const
    {
        if (m_file)
            return &m_file;
        return NULL;
    }
    
    FileSpec &
    GetFileSpec ()
    {
        return m_file;
    }
    const FileSpec &
    GetFileSpec () const
    {
        return m_file;
    }

    FileSpec *
    GetPlatformFileSpecPtr ()
    {
        if (m_platform_file)
            return &m_platform_file;
        return NULL;
    }

    const FileSpec *
    GetPlatformFileSpecPtr () const
    {
        if (m_platform_file)
            return &m_platform_file;
        return NULL;
    }

    FileSpec &
    GetPlatformFileSpec ()
    {
        return m_platform_file;
    }

    const FileSpec &
    GetPlatformFileSpec () const
    {
        return m_platform_file;
    }

    FileSpec *
    GetSymbolFileSpecPtr ()
    {
        if (m_symbol_file)
            return &m_symbol_file;
        return NULL;
    }
    
    const FileSpec *
    GetSymbolFileSpecPtr () const
    {
        if (m_symbol_file)
            return &m_symbol_file;
        return NULL;
    }
    
    FileSpec &
    GetSymbolFileSpec ()
    {
        return m_symbol_file;
    }
    
    const FileSpec &
    GetSymbolFileSpec () const
    {
        return m_symbol_file;
    }

    
    ArchSpec *
    GetArchitecturePtr ()
    {
        if (m_arch.IsValid())
            return &m_arch;
        return NULL;
    }
    
    const ArchSpec *
    GetArchitecturePtr () const
    {
        if (m_arch.IsValid())
            return &m_arch;
        return NULL;
    }
    
    ArchSpec &
    GetArchitecture ()
    {
        return m_arch;
    }
    
    const ArchSpec &
    GetArchitecture () const
    {
        return m_arch;
    }

    UUID *
    GetUUIDPtr ()
    {
        if (m_uuid.IsValid())
            return &m_uuid;
        return NULL;
    }
    
    const UUID *
    GetUUIDPtr () const
    {
        if (m_uuid.IsValid())
            return &m_uuid;
        return NULL;
    }
    
    UUID &
    GetUUID ()
    {
        return m_uuid;
    }
    
    const UUID &
    GetUUID () const
    {
        return m_uuid;
    }

    ConstString &
    GetObjectName ()
    {
        return m_object_name;
    }

    const ConstString &
    GetObjectName () const
    {
        return m_object_name;
    }

    uint64_t
    GetObjectOffset () const
    {
        return m_object_offset;
    }

    void
    SetObjectOffset (uint64_t object_offset)
    {
        m_object_offset = object_offset;
    }
    
    TimeValue &
    GetObjectModificationTime ()
    {
        return m_object_mod_time;
    }
    
    const TimeValue &
    GetObjectModificationTime () const
    {
        return m_object_mod_time;
    }

    PathMappingList &
    GetSourceMappingList () const
    {
        return m_source_mappings;
    }

    void
    Clear ()
    {
        m_file.Clear();
        m_platform_file.Clear();
        m_symbol_file.Clear();
        m_arch.Clear();
        m_uuid.Clear();
        m_object_name.Clear();
        m_object_offset = 0;
        m_source_mappings.Clear(false);
        m_object_mod_time.Clear();
    }

    
    explicit operator bool () const
    {
        if (m_file)
            return true;
        if (m_platform_file)
            return true;
        if (m_symbol_file)
            return true;
        if (m_arch.IsValid())
            return true;
        if (m_uuid.IsValid())
            return true;
        if (m_object_name)
            return true;
        if (m_object_mod_time.IsValid())
            return true;
        return false;
    }

    void
    Dump (Stream &strm)
    {
        bool dumped_something = false;
        if (m_file)
        {
            strm.PutCString("file = '");
            strm << m_file;
            strm.PutCString("'");
            dumped_something = true;
        }
        if (m_platform_file)
        {
            if (dumped_something)
                strm.PutCString(", ");
            strm.PutCString("platform_file = '");
            strm << m_platform_file;
            strm.PutCString("'");
            dumped_something = true;
        }
        if (m_symbol_file)
        {
            if (dumped_something)
                strm.PutCString(", ");
            strm.PutCString("symbol_file = '");
            strm << m_symbol_file;
            strm.PutCString("'");
            dumped_something = true;
        }
        if (m_arch.IsValid())
        {
            if (dumped_something)
                strm.PutCString(", ");
            strm.Printf("arch = %s", m_arch.GetTriple().str().c_str());
            dumped_something = true;
        }
        if (m_uuid.IsValid())
        {
            if (dumped_something)
                strm.PutCString(", ");
            strm.PutCString("uuid = ");
            m_uuid.Dump(&strm);
            dumped_something = true;
        }
        if (m_object_name)
        {
            if (dumped_something)
                strm.PutCString(", ");
            strm.Printf("object_name = %s", m_object_name.GetCString());
            dumped_something = true;
        }
        if (m_object_offset > 0)
        {
            if (dumped_something)
                strm.PutCString(", ");
            strm.Printf("object_offset = 0x%" PRIx64, m_object_offset);
            dumped_something = true;
        }
        if (m_object_mod_time.IsValid())
        {
            if (dumped_something)
                strm.PutCString(", ");
            strm.Printf("object_mod_time = 0x%" PRIx64, m_object_mod_time.GetAsSecondsSinceJan1_1970());
        }
    }

    bool
    Matches (const ModuleSpec &match_module_spec, bool exact_arch_match) const
    {
        if (match_module_spec.GetUUIDPtr() && match_module_spec.GetUUID() != GetUUID())
            return false;
        if (match_module_spec.GetObjectName() && match_module_spec.GetObjectName() != GetObjectName())
            return false;
        if (match_module_spec.GetFileSpecPtr())
        {
            const FileSpec &fspec = match_module_spec.GetFileSpec();
            if (!FileSpec::Equal(fspec, GetFileSpec(), fspec.GetDirectory().IsEmpty() == false))
                return false;
        }
        if (GetPlatformFileSpec() && match_module_spec.GetPlatformFileSpecPtr())
        {
            const FileSpec &fspec = match_module_spec.GetPlatformFileSpec();
            if (!FileSpec::Equal(fspec, GetPlatformFileSpec(), fspec.GetDirectory().IsEmpty() == false))
                return false;
            
        }
        // Only match the symbol file spec if there is one in this ModuleSpec
        if (GetSymbolFileSpec() && match_module_spec.GetSymbolFileSpecPtr())
        {
            const FileSpec &fspec = match_module_spec.GetSymbolFileSpec();
            if (!FileSpec::Equal(fspec, GetSymbolFileSpec(), fspec.GetDirectory().IsEmpty() == false))
                return false;
            
        }
        if (match_module_spec.GetArchitecturePtr())
        {
            if (exact_arch_match)
            {
                if (!GetArchitecture().IsExactMatch(match_module_spec.GetArchitecture()))
                    return false;
            }
            else
            {
                if (!GetArchitecture().IsCompatibleMatch(match_module_spec.GetArchitecture()))
                    return false;
            }
        }
        return true;
    }

protected:
    FileSpec m_file;
    FileSpec m_platform_file;
    FileSpec m_symbol_file;
    ArchSpec m_arch;
    UUID m_uuid;
    ConstString m_object_name;
    uint64_t m_object_offset;
    TimeValue m_object_mod_time;
    mutable PathMappingList m_source_mappings;
};

class ModuleSpecList
{
public:
    ModuleSpecList () :
        m_specs(),
        m_mutex(Mutex::eMutexTypeRecursive) 
    {
    }

    ModuleSpecList (const ModuleSpecList &rhs) :
        m_specs(),
        m_mutex(Mutex::eMutexTypeRecursive)
    {
        Mutex::Locker lhs_locker(m_mutex);
        Mutex::Locker rhs_locker(rhs.m_mutex);
        m_specs = rhs.m_specs;
    }

    ~ModuleSpecList ()
    {
    }

    ModuleSpecList &
    operator = (const ModuleSpecList &rhs)
    {
        if (this != &rhs)
        {
            Mutex::Locker lhs_locker(m_mutex);
            Mutex::Locker rhs_locker(rhs.m_mutex);
            m_specs = rhs.m_specs;
        }
        return *this;
    }

    size_t
    GetSize() const
    {
        Mutex::Locker locker(m_mutex);
        return m_specs.size();
    }

    void
    Clear ()
    {
        Mutex::Locker locker(m_mutex);
        m_specs.clear();
    }

    void
    Append (const ModuleSpec &spec)
    {
        Mutex::Locker locker(m_mutex);
        m_specs.push_back (spec);
    }

    void
    Append (const ModuleSpecList &rhs)
    {
        Mutex::Locker lhs_locker(m_mutex);
        Mutex::Locker rhs_locker(rhs.m_mutex);
        m_specs.insert(m_specs.end(), rhs.m_specs.begin(), rhs.m_specs.end());
    }

    // The index "i" must be valid and this can't be used in
    // multi-threaded code as no mutex lock is taken.
    ModuleSpec &
    GetModuleSpecRefAtIndex (size_t i)
    {
        return m_specs[i];
    }
    bool
    GetModuleSpecAtIndex (size_t i, ModuleSpec &module_spec) const
    {
        Mutex::Locker locker(m_mutex);
        if (i < m_specs.size())
        {
            module_spec = m_specs[i];
            return true;
        }
        module_spec.Clear();
        return false;
    }
    
    
    bool
    FindMatchingModuleSpec (const ModuleSpec &module_spec, ModuleSpec &match_module_spec) const
    {
        Mutex::Locker locker(m_mutex);
        bool exact_arch_match = true;
        for (auto spec: m_specs)
        {
            if (spec.Matches(module_spec, exact_arch_match))
            {
                match_module_spec = spec;
                return true;
            }
        }
        
        // If there was an architecture, retry with a compatible arch
        if (module_spec.GetArchitecturePtr())
        {
            exact_arch_match = false;
            for (auto spec: m_specs)
            {
                if (spec.Matches(module_spec, exact_arch_match))
                {
                    match_module_spec = spec;
                    return true;
                }
            }
        }
        match_module_spec.Clear();
        return false;
    }
    
    size_t
    FindMatchingModuleSpecs (const ModuleSpec &module_spec, ModuleSpecList &matching_list) const
    {
        Mutex::Locker locker(m_mutex);
        bool exact_arch_match = true;
        const size_t initial_match_count = matching_list.GetSize();
        for (auto spec: m_specs)
        {
            if (spec.Matches(module_spec, exact_arch_match))
                matching_list.Append (spec);
        }
        
        // If there was an architecture, retry with a compatible arch if no matches were found
        if (module_spec.GetArchitecturePtr() && (initial_match_count == matching_list.GetSize()))
        {
            exact_arch_match = false;
            for (auto spec: m_specs)
            {
                if (spec.Matches(module_spec, exact_arch_match))
                    matching_list.Append (spec);
            }
        }
        return matching_list.GetSize() - initial_match_count;
    }

    void
    Dump (Stream &strm)
    {
        Mutex::Locker locker(m_mutex);
        uint32_t idx = 0;
        for (auto spec: m_specs)
        {
            strm.Printf("[%u] ", idx);
            spec.Dump (strm);
            strm.EOL();
            ++idx;
        }
    }

protected:
    typedef std::vector<ModuleSpec> collection; ///< The module collection type.
    collection m_specs; ///< The collection of modules.
    mutable Mutex m_mutex;
};

} // namespace lldb_private

#endif  // liblldb_ModuleSpec_h_