Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/Address.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- Address.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/Address.h"
10
#include "lldb/Core/Declaration.h"
11
#include "lldb/Core/DumpDataExtractor.h"
12
#include "lldb/Core/Module.h"
13
#include "lldb/Core/ModuleList.h"
14
#include "lldb/Core/Section.h"
15
#include "lldb/Symbol/Block.h"
16
#include "lldb/Symbol/LineEntry.h"
17
#include "lldb/Symbol/ObjectFile.h"
18
#include "lldb/Symbol/Symbol.h"
19
#include "lldb/Symbol/SymbolContext.h"
20
#include "lldb/Symbol/SymbolVendor.h"
21
#include "lldb/Symbol/Symtab.h"
22
#include "lldb/Symbol/Type.h"
23
#include "lldb/Symbol/Variable.h"
24
#include "lldb/Symbol/VariableList.h"
25
#include "lldb/Target/ExecutionContext.h"
26
#include "lldb/Target/ExecutionContextScope.h"
27
#include "lldb/Target/Process.h"
28
#include "lldb/Target/SectionLoadList.h"
29
#include "lldb/Target/Target.h"
30
#include "lldb/Utility/ConstString.h"
31
#include "lldb/Utility/DataExtractor.h"
32
#include "lldb/Utility/Endian.h"
33
#include "lldb/Utility/FileSpec.h"
34
#include "lldb/Utility/Status.h"
35
#include "lldb/Utility/Stream.h"
36
#include "lldb/Utility/StreamString.h"
37
38
#include "llvm/ADT/StringRef.h"
39
#include "llvm/ADT/Triple.h"
40
#include "llvm/Support/Compiler.h"
41
42
#include <cstdint>
43
#include <memory>
44
#include <vector>
45
46
#include <cassert>
47
#include <cinttypes>
48
#include <cstring>
49
50
namespace lldb_private {
51
class CompileUnit;
52
}
53
namespace lldb_private {
54
class Function;
55
}
56
57
using namespace lldb;
58
using namespace lldb_private;
59
60
static size_t ReadBytes(ExecutionContextScope *exe_scope,
61
190k
                        const Address &address, void *dst, size_t dst_len) {
62
190k
  if (exe_scope == nullptr)
63
0
    return 0;
64
65
190k
  TargetSP target_sp(exe_scope->CalculateTarget());
66
190k
  if (target_sp) {
67
190k
    Status error;
68
190k
    bool force_live_memory = true;
69
190k
    return target_sp->ReadMemory(address, dst, dst_len, error,
70
190k
                                 force_live_memory);
71
190k
  }
72
0
  return 0;
73
190k
}
74
75
static bool GetByteOrderAndAddressSize(ExecutionContextScope *exe_scope,
76
                                       const Address &address,
77
                                       ByteOrder &byte_order,
78
125k
                                       uint32_t &addr_size) {
79
125k
  byte_order = eByteOrderInvalid;
80
125k
  addr_size = 0;
81
125k
  if (exe_scope == nullptr)
82
0
    return false;
83
84
125k
  TargetSP target_sp(exe_scope->CalculateTarget());
85
125k
  if (target_sp) {
86
125k
    byte_order = target_sp->GetArchitecture().GetByteOrder();
87
125k
    addr_size = target_sp->GetArchitecture().GetAddressByteSize();
88
125k
  }
89
90
125k
  if (byte_order == eByteOrderInvalid || addr_size == 0) {
91
0
    ModuleSP module_sp(address.GetModule());
92
0
    if (module_sp) {
93
0
      byte_order = module_sp->GetArchitecture().GetByteOrder();
94
0
      addr_size = module_sp->GetArchitecture().GetAddressByteSize();
95
0
    }
96
0
  }
97
125k
  return byte_order != eByteOrderInvalid && addr_size != 0;
98
125k
}
99
100
static uint64_t ReadUIntMax64(ExecutionContextScope *exe_scope,
101
                              const Address &address, uint32_t byte_size,
102
125k
                              bool &success) {
103
125k
  uint64_t uval64 = 0;
104
125k
  if (exe_scope == nullptr || byte_size > sizeof(uint64_t)) {
105
0
    success = false;
106
0
    return 0;
107
0
  }
108
125k
  uint64_t buf = 0;
109
110
125k
  success = ReadBytes(exe_scope, address, &buf, byte_size) == byte_size;
111
125k
  if (success) {
112
125k
    ByteOrder byte_order = eByteOrderInvalid;
113
125k
    uint32_t addr_size = 0;
114
125k
    if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) {
115
125k
      DataExtractor data(&buf, sizeof(buf), byte_order, addr_size);
116
125k
      lldb::offset_t offset = 0;
117
125k
      uval64 = data.GetU64(&offset);
118
125k
    } else
119
0
      success = false;
120
125k
  }
121
125k
  return uval64;
122
125k
}
123
124
static bool ReadAddress(ExecutionContextScope *exe_scope,
125
                        const Address &address, uint32_t pointer_size,
126
125k
                        Address &deref_so_addr) {
127
125k
  if (exe_scope == nullptr)
128
0
    return false;
129
130
125k
  bool success = false;
131
125k
  addr_t deref_addr = ReadUIntMax64(exe_scope, address, pointer_size, success);
132
125k
  if (success) {
133
125k
    ExecutionContext exe_ctx;
134
125k
    exe_scope->CalculateExecutionContext(exe_ctx);
135
    // If we have any sections that are loaded, try and resolve using the
136
    // section load list
137
125k
    Target *target = exe_ctx.GetTargetPtr();
138
125k
    if (target && !target->GetSectionLoadList().IsEmpty()) {
139
125k
      if (target->GetSectionLoadList().ResolveLoadAddress(deref_addr,
140
125k
                                                          deref_so_addr))
141
125k
        return true;
142
125k
    } else {
143
      // If we were not running, yet able to read an integer, we must have a
144
      // module
145
0
      ModuleSP module_sp(address.GetModule());
146
147
0
      assert(module_sp);
148
0
      if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr))
149
0
        return true;
150
0
    }
151
152
    // We couldn't make "deref_addr" into a section offset value, but we were
153
    // able to read the address, so we return a section offset address with no
154
    // section and "deref_addr" as the offset (address).
155
41
    deref_so_addr.SetRawAddress(deref_addr);
156
41
    return true;
157
125k
  }
158
0
  return false;
159
125k
}
160
161
static bool DumpUInt(ExecutionContextScope *exe_scope, const Address &address,
162
0
                     uint32_t byte_size, Stream *strm) {
163
0
  if (exe_scope == nullptr || byte_size == 0)
164
0
    return false;
165
0
  std::vector<uint8_t> buf(byte_size, 0);
166
167
0
  if (ReadBytes(exe_scope, address, &buf[0], buf.size()) == buf.size()) {
168
0
    ByteOrder byte_order = eByteOrderInvalid;
169
0
    uint32_t addr_size = 0;
170
0
    if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) {
171
0
      DataExtractor data(&buf.front(), buf.size(), byte_order, addr_size);
172
173
0
      DumpDataExtractor(data, strm,
174
0
                        0,                    // Start offset in "data"
175
0
                        eFormatHex,           // Print as characters
176
0
                        buf.size(),           // Size of item
177
0
                        1,                    // Items count
178
0
                        UINT32_MAX,           // num per line
179
0
                        LLDB_INVALID_ADDRESS, // base address
180
0
                        0,                    // bitfield bit size
181
0
                        0);                   // bitfield bit offset
182
183
0
      return true;
184
0
    }
185
0
  }
186
0
  return false;
187
0
}
188
189
static size_t ReadCStringFromMemory(ExecutionContextScope *exe_scope,
190
64.9k
                                    const Address &address, Stream *strm) {
191
64.9k
  if (exe_scope == nullptr)
192
0
    return 0;
193
64.9k
  const size_t k_buf_len = 256;
194
64.9k
  char buf[k_buf_len + 1];
195
64.9k
  buf[k_buf_len] = '\0'; // NULL terminate
196
197
  // Byte order and address size don't matter for C string dumping..
198
64.9k
  DataExtractor data(buf, sizeof(buf), endian::InlHostByteOrder(), 4);
199
64.9k
  size_t total_len = 0;
200
64.9k
  size_t bytes_read;
201
64.9k
  Address curr_address(address);
202
64.9k
  strm->PutChar('"');
203
64.9k
  while ((bytes_read = ReadBytes(exe_scope, curr_address, buf, k_buf_len)) >
204
64.9k
         0) {
205
64.9k
    size_t len = strlen(buf);
206
64.9k
    if (len == 0)
207
422
      break;
208
64.5k
    if (len > bytes_read)
209
0
      len = bytes_read;
210
211
64.5k
    DumpDataExtractor(data, strm,
212
64.5k
                      0,                    // Start offset in "data"
213
64.5k
                      eFormatChar,          // Print as characters
214
64.5k
                      1,                    // Size of item (1 byte for a char!)
215
64.5k
                      len,                  // How many bytes to print?
216
64.5k
                      UINT32_MAX,           // num per line
217
64.5k
                      LLDB_INVALID_ADDRESS, // base address
218
64.5k
                      0,                    // bitfield bit size
219
220
64.5k
                      0); // bitfield bit offset
221
222
64.5k
    total_len += bytes_read;
223
224
64.5k
    if (len < k_buf_len)
225
64.5k
      break;
226
27
    curr_address.SetOffset(curr_address.GetOffset() + bytes_read);
227
27
  }
228
64.9k
  strm->PutChar('"');
229
64.9k
  return total_len;
230
64.9k
}
231
232
47.2k
Address::Address(lldb::addr_t abs_addr) : m_section_wp(), m_offset(abs_addr) {}
233
234
Address::Address(addr_t address, const SectionList *section_list)
235
14.4M
    : m_section_wp(), m_offset(LLDB_INVALID_ADDRESS) {
236
14.4M
  ResolveAddressUsingFileSections(address, section_list);
237
14.4M
}
238
239
31.7M
const Address &Address::operator=(const Address &rhs) {
240
31.7M
  if (this != &rhs) {
241
31.7M
    m_section_wp = rhs.m_section_wp;
242
31.7M
    m_offset = rhs.m_offset;
243
31.7M
  }
244
31.7M
  return *this;
245
31.7M
}
246
247
bool Address::ResolveAddressUsingFileSections(addr_t file_addr,
248
28.5M
                                              const SectionList *section_list) {
249
28.5M
  if (section_list) {
250
28.5M
    SectionSP section_sp(
251
28.5M
        section_list->FindSectionContainingFileAddress(file_addr));
252
28.5M
    m_section_wp = section_sp;
253
28.5M
    if (section_sp) {
254
28.5M
      assert(section_sp->ContainsFileAddress(file_addr));
255
0
      m_offset = file_addr - section_sp->GetFileAddress();
256
28.5M
      return true; // Successfully transformed addr into a section offset
257
                   // address
258
28.5M
    }
259
28.5M
  }
260
170
  m_offset = file_addr;
261
170
  return false; // Failed to resolve this address to a section offset value
262
28.5M
}
263
264
/// if "addr_range_ptr" is not NULL, then fill in with the address range of the function.
265
bool Address::ResolveFunctionScope(SymbolContext &sym_ctx,
266
81.1k
                                   AddressRange *addr_range_ptr) {
267
81.1k
  constexpr SymbolContextItem resolve_scope =
268
81.1k
    eSymbolContextFunction | eSymbolContextSymbol;
269
270
81.1k
  if (!(CalculateSymbolContext(&sym_ctx, resolve_scope) & resolve_scope)) {
271
3.65k
    if (addr_range_ptr)
272
3.65k
      addr_range_ptr->Clear();
273
3.65k
   return false;
274
3.65k
  }
275
276
77.5k
  if (!addr_range_ptr)
277
0
    return true;
278
279
77.5k
  return sym_ctx.GetAddressRange(resolve_scope, 0, false, *addr_range_ptr);
280
77.5k
}
281
282
3.67M
ModuleSP Address::GetModule() const {
283
3.67M
  lldb::ModuleSP module_sp;
284
3.67M
  SectionSP section_sp(GetSection());
285
3.67M
  if (section_sp)
286
3.67M
    module_sp = section_sp->GetModule();
287
3.67M
  return module_sp;
288
3.67M
}
289
290
463M
addr_t Address::GetFileAddress() const {
291
463M
  SectionSP section_sp(GetSection());
292
463M
  if (section_sp) {
293
463M
    addr_t sect_file_addr = section_sp->GetFileAddress();
294
463M
    if (sect_file_addr == LLDB_INVALID_ADDRESS) {
295
      // Section isn't resolved, we can't return a valid file address
296
204
      return LLDB_INVALID_ADDRESS;
297
204
    }
298
    // We have a valid file range, so we can return the file based address by
299
    // adding the file base address to our offset
300
463M
    return sect_file_addr + m_offset;
301
463M
  } else 
if (18.6k
SectionWasDeletedPrivate()18.6k
) {
302
    // Used to have a valid section but it got deleted so the offset doesn't
303
    // mean anything without the section
304
4
    return LLDB_INVALID_ADDRESS;
305
4
  }
306
  // No section, we just return the offset since it is the value in this case
307
18.5k
  return m_offset;
308
463M
}
309
310
4.90M
addr_t Address::GetLoadAddress(Target *target) const {
311
4.90M
  SectionSP section_sp(GetSection());
312
4.90M
  if (section_sp) {
313
4.87M
    if (target) {
314
4.70M
      addr_t sect_load_addr = section_sp->GetLoadBaseAddress(target);
315
316
4.70M
      if (sect_load_addr != LLDB_INVALID_ADDRESS) {
317
        // We have a valid file range, so we can return the file based address
318
        // by adding the file base address to our offset
319
4.68M
        return sect_load_addr + m_offset;
320
4.68M
      }
321
4.70M
    }
322
4.87M
  } else 
if (37.0k
SectionWasDeletedPrivate()37.0k
) {
323
    // Used to have a valid section but it got deleted so the offset doesn't
324
    // mean anything without the section
325
0
    return LLDB_INVALID_ADDRESS;
326
37.0k
  } else {
327
    // We don't have a section so the offset is the load address
328
37.0k
    return m_offset;
329
37.0k
  }
330
  // The section isn't resolved or an invalid target was passed in so we can't
331
  // return a valid load address.
332
180k
  return LLDB_INVALID_ADDRESS;
333
4.90M
}
334
335
16.5k
addr_t Address::GetCallableLoadAddress(Target *target, bool is_indirect) const {
336
16.5k
  addr_t code_addr = LLDB_INVALID_ADDRESS;
337
338
16.5k
  if (is_indirect && 
target89
) {
339
89
    ProcessSP processSP = target->GetProcessSP();
340
89
    Status error;
341
89
    if (processSP) {
342
89
      code_addr = processSP->ResolveIndirectFunction(this, error);
343
89
      if (!error.Success())
344
0
        code_addr = LLDB_INVALID_ADDRESS;
345
89
    }
346
16.4k
  } else {
347
16.4k
    code_addr = GetLoadAddress(target);
348
16.4k
  }
349
350
16.5k
  if (code_addr == LLDB_INVALID_ADDRESS)
351
0
    return code_addr;
352
353
16.5k
  if (target)
354
16.5k
    return target->GetCallableLoadAddress(code_addr, GetAddressClass());
355
0
  return code_addr;
356
16.5k
}
357
358
0
bool Address::SetCallableLoadAddress(lldb::addr_t load_addr, Target *target) {
359
0
  if (SetLoadAddress(load_addr, target)) {
360
0
    if (target)
361
0
      m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass());
362
0
    return true;
363
0
  }
364
0
  return false;
365
0
}
366
367
addr_t Address::GetOpcodeLoadAddress(Target *target,
368
125k
                                     AddressClass addr_class) const {
369
125k
  addr_t code_addr = GetLoadAddress(target);
370
125k
  if (code_addr != LLDB_INVALID_ADDRESS) {
371
125k
    if (addr_class == AddressClass::eInvalid)
372
125k
      addr_class = GetAddressClass();
373
125k
    code_addr = target->GetOpcodeLoadAddress(code_addr, addr_class);
374
125k
  }
375
125k
  return code_addr;
376
125k
}
377
378
bool Address::SetOpcodeLoadAddress(lldb::addr_t load_addr, Target *target,
379
                                   AddressClass addr_class,
380
35.3k
                                   bool allow_section_end) {
381
35.3k
  if (SetLoadAddress(load_addr, target, allow_section_end)) {
382
35.0k
    if (target) {
383
35.0k
      if (addr_class == AddressClass::eInvalid)
384
43
        addr_class = GetAddressClass();
385
35.0k
      m_offset = target->GetOpcodeLoadAddress(m_offset, addr_class);
386
35.0k
    }
387
35.0k
    return true;
388
35.0k
  }
389
230
  return false;
390
35.3k
}
391
392
bool Address::GetDescription(Stream &s, Target &target,
393
0
                             DescriptionLevel level) const {
394
0
  assert(level == eDescriptionLevelBrief &&
395
0
         "Non-brief descriptions not implemented");
396
0
  LineEntry line_entry;
397
0
  if (CalculateSymbolContextLineEntry(line_entry)) {
398
0
    s.Printf(" (%s:%u:%u)", line_entry.file.GetFilename().GetCString(),
399
0
             line_entry.line, line_entry.column);
400
0
    return true;
401
0
  }
402
0
  return false;
403
0
}
404
405
bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
406
2.80M
                   DumpStyle fallback_style, uint32_t addr_size) const {
407
  // If the section was nullptr, only load address is going to work unless we
408
  // are trying to deref a pointer
409
2.80M
  SectionSP section_sp(GetSection());
410
2.80M
  if (!section_sp && 
style != DumpStyleResolvedPointerDescription9.16k
)
411
9.15k
    style = DumpStyleLoadAddress;
412
413
2.80M
  ExecutionContext exe_ctx(exe_scope);
414
2.80M
  Target *target = exe_ctx.GetTargetPtr();
415
  // If addr_byte_size is UINT32_MAX, then determine the correct address byte
416
  // size for the process or default to the size of addr_t
417
2.80M
  if (addr_size == UINT32_MAX) {
418
843k
    if (target)
419
465k
      addr_size = target->GetArchitecture().GetAddressByteSize();
420
378k
    else
421
378k
      addr_size = sizeof(addr_t);
422
843k
  }
423
424
2.80M
  Address so_addr;
425
2.80M
  switch (style) {
426
0
  case DumpStyleInvalid:
427
0
    return false;
428
429
27.4k
  case DumpStyleSectionNameOffset:
430
27.4k
    if (section_sp) {
431
27.4k
      section_sp->DumpName(s->AsRawOstream());
432
27.4k
      s->Printf(" + %" PRIu64, m_offset);
433
27.4k
    } else {
434
0
      DumpAddress(s->AsRawOstream(), m_offset, addr_size);
435
0
    }
436
27.4k
    break;
437
438
0
  case DumpStyleSectionPointerOffset:
439
0
    s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get()));
440
0
    DumpAddress(s->AsRawOstream(), m_offset, addr_size);
441
0
    break;
442
443
36.5k
  case DumpStyleModuleWithFileAddress:
444
36.5k
    if (section_sp) {
445
36.5k
      ModuleSP module_sp = section_sp->GetModule();
446
36.5k
      if (module_sp)
447
36.5k
        s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString(
448
36.5k
                             "<Unknown>"));
449
0
      else
450
0
        s->Printf("%s[", "<Unknown>");
451
36.5k
    }
452
36.5k
    LLVM_FALLTHROUGH;
453
247k
  case DumpStyleFileAddress: {
454
247k
    addr_t file_addr = GetFileAddress();
455
247k
    if (file_addr == LLDB_INVALID_ADDRESS) {
456
0
      if (fallback_style != DumpStyleInvalid)
457
0
        return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
458
0
      return false;
459
0
    }
460
247k
    DumpAddress(s->AsRawOstream(), file_addr, addr_size);
461
247k
    if (style == DumpStyleModuleWithFileAddress && 
section_sp36.5k
)
462
36.5k
      s->PutChar(']');
463
247k
  } break;
464
465
2.22M
  case DumpStyleLoadAddress: {
466
2.22M
    addr_t load_addr = GetLoadAddress(target);
467
468
    /*
469
     * MIPS:
470
     * Display address in compressed form for MIPS16 or microMIPS
471
     * if the address belongs to AddressClass::eCodeAlternateISA.
472
    */
473
2.22M
    if (target) {
474
2.05M
      const llvm::Triple::ArchType llvm_arch =
475
2.05M
          target->GetArchitecture().GetMachine();
476
2.05M
      if (llvm_arch == llvm::Triple::mips ||
477
2.05M
          llvm_arch == llvm::Triple::mipsel ||
478
2.05M
          llvm_arch == llvm::Triple::mips64 ||
479
2.05M
          llvm_arch == llvm::Triple::mips64el)
480
0
        load_addr = GetCallableLoadAddress(target);
481
2.05M
    }
482
483
2.22M
    if (load_addr == LLDB_INVALID_ADDRESS) {
484
178k
      if (fallback_style != DumpStyleInvalid)
485
10.0k
        return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
486
168k
      return false;
487
178k
    }
488
2.04M
    DumpAddress(s->AsRawOstream(), load_addr, addr_size);
489
2.04M
  } break;
490
491
40.4k
  case DumpStyleResolvedDescription:
492
40.4k
  case DumpStyleResolvedDescriptionNoModule:
493
237k
  case DumpStyleResolvedDescriptionNoFunctionArguments:
494
302k
  case DumpStyleNoFunctionName:
495
302k
    if (IsSectionOffset()) {
496
302k
      uint32_t pointer_size = 4;
497
302k
      ModuleSP module_sp(GetModule());
498
302k
      if (target)
499
302k
        pointer_size = target->GetArchitecture().GetAddressByteSize();
500
21
      else if (module_sp)
501
21
        pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
502
503
302k
      bool showed_info = false;
504
302k
      if (section_sp) {
505
302k
        SectionType sect_type = section_sp->GetType();
506
302k
        switch (sect_type) {
507
3.17k
        case eSectionTypeData:
508
3.17k
          if (module_sp) {
509
3.17k
            if (Symtab *symtab = module_sp->GetSymtab()) {
510
3.16k
              const addr_t file_Addr = GetFileAddress();
511
3.16k
              Symbol *symbol =
512
3.16k
                  symtab->FindSymbolContainingFileAddress(file_Addr);
513
3.16k
              if (symbol) {
514
3.16k
                const char *symbol_name = symbol->GetName().AsCString();
515
3.16k
                if (symbol_name) {
516
3.16k
                  s->PutCString(symbol_name);
517
3.16k
                  addr_t delta =
518
3.16k
                      file_Addr - symbol->GetAddressRef().GetFileAddress();
519
3.16k
                  if (delta)
520
536
                    s->Printf(" + %" PRIu64, delta);
521
3.16k
                  showed_info = true;
522
3.16k
                }
523
3.16k
              }
524
3.16k
            }
525
3.17k
          }
526
3.17k
          break;
527
528
16.2k
        case eSectionTypeDataCString:
529
          // Read the C string from memory and display it
530
16.2k
          showed_info = true;
531
16.2k
          ReadCStringFromMemory(exe_scope, *this, s);
532
16.2k
          break;
533
534
48.6k
        case eSectionTypeDataCStringPointers:
535
48.6k
          if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
536
#if VERBOSE_OUTPUT
537
            s->PutCString("(char *)");
538
            so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
539
                         DumpStyleFileAddress);
540
            s->PutCString(": ");
541
#endif
542
48.6k
            showed_info = true;
543
48.6k
            ReadCStringFromMemory(exe_scope, so_addr, s);
544
48.6k
          }
545
48.6k
          break;
546
547
0
        case eSectionTypeDataObjCMessageRefs:
548
0
          if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
549
0
            if (target && so_addr.IsSectionOffset()) {
550
0
              SymbolContext func_sc;
551
0
              target->GetImages().ResolveSymbolContextForAddress(
552
0
                  so_addr, eSymbolContextEverything, func_sc);
553
0
              if (func_sc.function != nullptr || func_sc.symbol != nullptr) {
554
0
                showed_info = true;
555
#if VERBOSE_OUTPUT
556
                s->PutCString("(objc_msgref *) -> { (func*)");
557
                so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
558
                             DumpStyleFileAddress);
559
#else
560
0
                s->PutCString("{ ");
561
0
#endif
562
0
                Address cstr_addr(*this);
563
0
                cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
564
0
                func_sc.DumpStopContext(s, exe_scope, so_addr, true, true,
565
0
                                        false, true, true);
566
0
                if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr)) {
567
#if VERBOSE_OUTPUT
568
                  s->PutCString("), (char *)");
569
                  so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
570
                               DumpStyleFileAddress);
571
                  s->PutCString(" (");
572
#else
573
0
                  s->PutCString(", ");
574
0
#endif
575
0
                  ReadCStringFromMemory(exe_scope, so_addr, s);
576
0
                }
577
#if VERBOSE_OUTPUT
578
                s->PutCString(") }");
579
#else
580
0
                s->PutCString(" }");
581
0
#endif
582
0
              }
583
0
            }
584
0
          }
585
0
          break;
586
587
12.8k
        case eSectionTypeDataObjCCFStrings: {
588
12.8k
          Address cfstring_data_addr(*this);
589
12.8k
          cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() +
590
12.8k
                                       (2 * pointer_size));
591
12.8k
          if (ReadAddress(exe_scope, cfstring_data_addr, pointer_size,
592
12.8k
                          so_addr)) {
593
#if VERBOSE_OUTPUT
594
            s->PutCString("(CFString *) ");
595
            cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
596
                                    DumpStyleFileAddress);
597
            s->PutCString(" -> @");
598
#else
599
12.8k
            s->PutChar('@');
600
12.8k
#endif
601
12.8k
            if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
602
12.8k
              showed_info = true;
603
12.8k
          }
604
12.8k
        } break;
605
606
0
        case eSectionTypeData4:
607
          // Read the 4 byte data and display it
608
0
          showed_info = true;
609
0
          s->PutCString("(uint32_t) ");
610
0
          DumpUInt(exe_scope, *this, 4, s);
611
0
          break;
612
613
0
        case eSectionTypeData8:
614
          // Read the 8 byte data and display it
615
0
          showed_info = true;
616
0
          s->PutCString("(uint64_t) ");
617
0
          DumpUInt(exe_scope, *this, 8, s);
618
0
          break;
619
620
0
        case eSectionTypeData16:
621
          // Read the 16 byte data and display it
622
0
          showed_info = true;
623
0
          s->PutCString("(uint128_t) ");
624
0
          DumpUInt(exe_scope, *this, 16, s);
625
0
          break;
626
627
64.2k
        case eSectionTypeDataPointers:
628
          // Read the pointer data and display it
629
64.2k
          if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
630
64.2k
            s->PutCString("(void *)");
631
64.2k
            so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
632
64.2k
                         DumpStyleFileAddress);
633
634
64.2k
            showed_info = true;
635
64.2k
            if (so_addr.IsSectionOffset()) {
636
64.2k
              SymbolContext pointer_sc;
637
64.2k
              if (target) {
638
64.2k
                target->GetImages().ResolveSymbolContextForAddress(
639
64.2k
                    so_addr, eSymbolContextEverything, pointer_sc);
640
64.2k
                if (pointer_sc.function != nullptr ||
641
64.2k
                    
pointer_sc.symbol != nullptr64.2k
) {
642
64.2k
                  s->PutCString(": ");
643
64.2k
                  pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false,
644
64.2k
                                             false, true, true);
645
64.2k
                }
646
64.2k
              }
647
64.2k
            }
648
64.2k
          }
649
64.2k
          break;
650
651
157k
        default:
652
157k
          break;
653
302k
        }
654
302k
      }
655
656
302k
      if (!showed_info) {
657
157k
        if (module_sp) {
658
157k
          SymbolContext sc;
659
157k
          module_sp->ResolveSymbolContextForAddress(
660
157k
              *this, eSymbolContextEverything, sc);
661
157k
          if (sc.function || 
sc.symbol157k
) {
662
156k
            bool show_stop_context = true;
663
156k
            const bool show_module = (style == DumpStyleResolvedDescription);
664
156k
            const bool show_fullpaths = false;
665
156k
            const bool show_inlined_frames = true;
666
156k
            const bool show_function_arguments =
667
156k
                (style != DumpStyleResolvedDescriptionNoFunctionArguments);
668
156k
            const bool show_function_name = (style != DumpStyleNoFunctionName);
669
156k
            if (sc.function == nullptr && 
sc.symbol != nullptr156k
) {
670
              // If we have just a symbol make sure it is in the right section
671
156k
              if (sc.symbol->ValueIsAddress()) {
672
156k
                if (sc.symbol->GetAddressRef().GetSection() != GetSection()) {
673
                  // don't show the module if the symbol is a trampoline symbol
674
0
                  show_stop_context = false;
675
0
                }
676
156k
              }
677
156k
            }
678
156k
            if (show_stop_context) {
679
              // We have a function or a symbol from the same sections as this
680
              // address.
681
156k
              sc.DumpStopContext(s, exe_scope, *this, show_fullpaths,
682
156k
                                 show_module, show_inlined_frames,
683
156k
                                 show_function_arguments, show_function_name);
684
156k
            } else {
685
              // We found a symbol but it was in a different section so it
686
              // isn't the symbol we should be showing, just show the section
687
              // name + offset
688
0
              Dump(s, exe_scope, DumpStyleSectionNameOffset);
689
0
            }
690
156k
          }
691
157k
        }
692
157k
      }
693
302k
    } else {
694
0
      if (fallback_style != DumpStyleInvalid)
695
0
        return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
696
0
      return false;
697
0
    }
698
302k
    break;
699
700
302k
  case DumpStyleDetailedSymbolContext:
701
49
    if (IsSectionOffset()) {
702
49
      ModuleSP module_sp(GetModule());
703
49
      if (module_sp) {
704
49
        SymbolContext sc;
705
49
        module_sp->ResolveSymbolContextForAddress(
706
49
            *this, eSymbolContextEverything | eSymbolContextVariable, sc);
707
49
        if (sc.symbol) {
708
          // If we have just a symbol make sure it is in the same section as
709
          // our address. If it isn't, then we might have just found the last
710
          // symbol that came before the address that we are looking up that
711
          // has nothing to do with our address lookup.
712
47
          if (sc.symbol->ValueIsAddress() &&
713
47
              sc.symbol->GetAddressRef().GetSection() != GetSection())
714
4
            sc.symbol = nullptr;
715
47
        }
716
49
        sc.GetDescription(s, eDescriptionLevelBrief, target);
717
718
49
        if (sc.block) {
719
43
          bool can_create = true;
720
43
          bool get_parent_variables = true;
721
43
          bool stop_if_block_is_inlined_function = false;
722
43
          VariableList variable_list;
723
43
          sc.block->AppendVariables(can_create, get_parent_variables,
724
43
                                    stop_if_block_is_inlined_function,
725
62
                                    [](Variable *) { return true; },
726
43
                                    &variable_list);
727
728
62
          for (const VariableSP &var_sp : variable_list) {
729
62
            if (var_sp && var_sp->LocationIsValidForAddress(*this)) {
730
52
              s->Indent();
731
52
              s->Printf("   Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
732
52
                        var_sp->GetID(), var_sp->GetName().GetCString());
733
52
              Type *type = var_sp->GetType();
734
52
              if (type)
735
52
                s->Printf(", type = \"%s\"", type->GetName().GetCString());
736
0
              else
737
0
                s->PutCString(", type = <unknown>");
738
52
              s->PutCString(", location = ");
739
52
              var_sp->DumpLocationForAddress(s, *this);
740
52
              s->PutCString(", decl = ");
741
52
              var_sp->GetDeclaration().DumpStopContext(s, false);
742
52
              s->EOL();
743
52
            }
744
62
          }
745
43
        }
746
49
      }
747
49
    } else {
748
0
      if (fallback_style != DumpStyleInvalid)
749
0
        return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
750
0
      return false;
751
0
    }
752
49
    break;
753
754
49
  case DumpStyleResolvedPointerDescription: {
755
10
    Process *process = exe_ctx.GetProcessPtr();
756
10
    if (process) {
757
10
      addr_t load_addr = GetLoadAddress(target);
758
10
      if (load_addr != LLDB_INVALID_ADDRESS) {
759
10
        Status memory_error;
760
10
        addr_t dereferenced_load_addr =
761
10
            process->ReadPointerFromMemory(load_addr, memory_error);
762
10
        if (dereferenced_load_addr != LLDB_INVALID_ADDRESS) {
763
0
          Address dereferenced_addr;
764
0
          if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr,
765
0
                                               target)) {
766
0
            StreamString strm;
767
0
            if (dereferenced_addr.Dump(&strm, exe_scope,
768
0
                                       DumpStyleResolvedDescription,
769
0
                                       DumpStyleInvalid, addr_size)) {
770
0
              DumpAddress(s->AsRawOstream(), dereferenced_load_addr, addr_size,
771
0
                          " -> ", " ");
772
0
              s->Write(strm.GetString().data(), strm.GetSize());
773
0
              return true;
774
0
            }
775
0
          }
776
0
        }
777
10
      }
778
10
    }
779
10
    if (fallback_style != DumpStyleInvalid)
780
0
      return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
781
10
    return false;
782
10
  } 
break0
;
783
2.80M
  }
784
785
2.62M
  return true;
786
2.80M
}
787
788
156k
bool Address::SectionWasDeleted() const {
789
156k
  if (GetSection())
790
156k
    return false;
791
1
  return SectionWasDeletedPrivate();
792
156k
}
793
794
55.6k
bool Address::SectionWasDeletedPrivate() const {
795
55.6k
  lldb::SectionWP empty_section_wp;
796
797
  // If either call to "std::weak_ptr::owner_before(...) value returns true,
798
  // this indicates that m_section_wp once contained (possibly still does) a
799
  // reference to a valid shared pointer. This helps us know if we had a valid
800
  // reference to a section which is now invalid because the module it was in
801
  // was unloaded/deleted, or if the address doesn't have a valid reference to
802
  // a section.
803
55.6k
  return empty_section_wp.owner_before(m_section_wp) ||
804
55.6k
         
m_section_wp.owner_before(empty_section_wp)55.6k
;
805
55.6k
}
806
807
uint32_t
808
Address::CalculateSymbolContext(SymbolContext *sc,
809
90.5k
                                SymbolContextItem resolve_scope) const {
810
90.5k
  sc->Clear(false);
811
  // Absolute addresses don't have enough information to reconstruct even their
812
  // target.
813
814
90.5k
  SectionSP section_sp(GetSection());
815
90.5k
  if (section_sp) {
816
87.7k
    ModuleSP module_sp(section_sp->GetModule());
817
87.7k
    if (module_sp) {
818
87.7k
      sc->module_sp = module_sp;
819
87.7k
      if (sc->module_sp)
820
87.7k
        return sc->module_sp->ResolveSymbolContextForAddress(
821
87.7k
            *this, resolve_scope, *sc);
822
87.7k
    }
823
87.7k
  }
824
2.81k
  return 0;
825
90.5k
}
826
827
0
ModuleSP Address::CalculateSymbolContextModule() const {
828
0
  SectionSP section_sp(GetSection());
829
0
  if (section_sp)
830
0
    return section_sp->GetModule();
831
0
  return ModuleSP();
832
0
}
833
834
61
CompileUnit *Address::CalculateSymbolContextCompileUnit() const {
835
61
  SectionSP section_sp(GetSection());
836
61
  if (section_sp) {
837
58
    SymbolContext sc;
838
58
    sc.module_sp = section_sp->GetModule();
839
58
    if (sc.module_sp) {
840
58
      sc.module_sp->ResolveSymbolContextForAddress(*this,
841
58
                                                   eSymbolContextCompUnit, sc);
842
58
      return sc.comp_unit;
843
58
    }
844
58
  }
845
3
  return nullptr;
846
61
}
847
848
1.44k
Function *Address::CalculateSymbolContextFunction() const {
849
1.44k
  SectionSP section_sp(GetSection());
850
1.44k
  if (section_sp) {
851
1.44k
    SymbolContext sc;
852
1.44k
    sc.module_sp = section_sp->GetModule();
853
1.44k
    if (sc.module_sp) {
854
1.44k
      sc.module_sp->ResolveSymbolContextForAddress(*this,
855
1.44k
                                                   eSymbolContextFunction, sc);
856
1.44k
      return sc.function;
857
1.44k
    }
858
1.44k
  }
859
3
  return nullptr;
860
1.44k
}
861
862
3
Block *Address::CalculateSymbolContextBlock() const {
863
3
  SectionSP section_sp(GetSection());
864
3
  if (section_sp) {
865
0
    SymbolContext sc;
866
0
    sc.module_sp = section_sp->GetModule();
867
0
    if (sc.module_sp) {
868
0
      sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock,
869
0
                                                   sc);
870
0
      return sc.block;
871
0
    }
872
0
  }
873
3
  return nullptr;
874
3
}
875
876
8.81k
Symbol *Address::CalculateSymbolContextSymbol() const {
877
8.81k
  SectionSP section_sp(GetSection());
878
8.81k
  if (section_sp) {
879
8.80k
    SymbolContext sc;
880
8.80k
    sc.module_sp = section_sp->GetModule();
881
8.80k
    if (sc.module_sp) {
882
8.80k
      sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol,
883
8.80k
                                                   sc);
884
8.80k
      return sc.symbol;
885
8.80k
    }
886
8.80k
  }
887
3
  return nullptr;
888
8.81k
}
889
890
107
bool Address::CalculateSymbolContextLineEntry(LineEntry &line_entry) const {
891
107
  SectionSP section_sp(GetSection());
892
107
  if (section_sp) {
893
104
    SymbolContext sc;
894
104
    sc.module_sp = section_sp->GetModule();
895
104
    if (sc.module_sp) {
896
104
      sc.module_sp->ResolveSymbolContextForAddress(*this,
897
104
                                                   eSymbolContextLineEntry, sc);
898
104
      if (sc.line_entry.IsValid()) {
899
104
        line_entry = sc.line_entry;
900
104
        return true;
901
104
      }
902
104
    }
903
104
  }
904
3
  line_entry.Clear();
905
3
  return false;
906
107
}
907
908
55
int Address::CompareFileAddress(const Address &a, const Address &b) {
909
55
  addr_t a_file_addr = a.GetFileAddress();
910
55
  addr_t b_file_addr = b.GetFileAddress();
911
55
  if (a_file_addr < b_file_addr)
912
36
    return -1;
913
19
  if (a_file_addr > b_file_addr)
914
0
    return +1;
915
19
  return 0;
916
19
}
917
918
int Address::CompareLoadAddress(const Address &a, const Address &b,
919
8.65k
                                Target *target) {
920
8.65k
  assert(target != nullptr);
921
0
  addr_t a_load_addr = a.GetLoadAddress(target);
922
8.65k
  addr_t b_load_addr = b.GetLoadAddress(target);
923
8.65k
  if (a_load_addr < b_load_addr)
924
0
    return -1;
925
8.65k
  if (a_load_addr > b_load_addr)
926
0
    return +1;
927
8.65k
  return 0;
928
8.65k
}
929
930
50.8k
int Address::CompareModulePointerAndOffset(const Address &a, const Address &b) {
931
50.8k
  ModuleSP a_module_sp(a.GetModule());
932
50.8k
  ModuleSP b_module_sp(b.GetModule());
933
50.8k
  Module *a_module = a_module_sp.get();
934
50.8k
  Module *b_module = b_module_sp.get();
935
50.8k
  if (a_module < b_module)
936
6.35k
    return -1;
937
44.4k
  if (a_module > b_module)
938
4.98k
    return +1;
939
  // Modules are the same, just compare the file address since they should be
940
  // unique
941
39.4k
  addr_t a_file_addr = a.GetFileAddress();
942
39.4k
  addr_t b_file_addr = b.GetFileAddress();
943
39.4k
  if (a_file_addr < b_file_addr)
944
26.2k
    return -1;
945
13.2k
  if (a_file_addr > b_file_addr)
946
13.1k
    return +1;
947
150
  return 0;
948
13.2k
}
949
950
0
size_t Address::MemorySize() const {
951
  // Noting special for the memory size of a single Address object, it is just
952
  // the size of itself.
953
0
  return sizeof(Address);
954
0
}
955
956
// NOTE: Be careful using this operator. It can correctly compare two
957
// addresses from the same Module correctly. It can't compare two addresses
958
// from different modules in any meaningful way, but it will compare the module
959
// pointers.
960
//
961
// To sum things up:
962
// - works great for addresses within the same module - it works for addresses
963
// across multiple modules, but don't expect the
964
//   address results to make much sense
965
//
966
// This basically lets Address objects be used in ordered collection classes.
967
968
58
bool lldb_private::operator<(const Address &lhs, const Address &rhs) {
969
58
  ModuleSP lhs_module_sp(lhs.GetModule());
970
58
  ModuleSP rhs_module_sp(rhs.GetModule());
971
58
  Module *lhs_module = lhs_module_sp.get();
972
58
  Module *rhs_module = rhs_module_sp.get();
973
58
  if (lhs_module == rhs_module) {
974
    // Addresses are in the same module, just compare the file addresses
975
58
    return lhs.GetFileAddress() < rhs.GetFileAddress();
976
58
  } else {
977
    // The addresses are from different modules, just use the module pointer
978
    // value to get consistent ordering
979
0
    return lhs_module < rhs_module;
980
0
  }
981
58
}
982
983
0
bool lldb_private::operator>(const Address &lhs, const Address &rhs) {
984
0
  ModuleSP lhs_module_sp(lhs.GetModule());
985
0
  ModuleSP rhs_module_sp(rhs.GetModule());
986
0
  Module *lhs_module = lhs_module_sp.get();
987
0
  Module *rhs_module = rhs_module_sp.get();
988
0
  if (lhs_module == rhs_module) {
989
    // Addresses are in the same module, just compare the file addresses
990
0
    return lhs.GetFileAddress() > rhs.GetFileAddress();
991
0
  } else {
992
    // The addresses are from different modules, just use the module pointer
993
    // value to get consistent ordering
994
0
    return lhs_module > rhs_module;
995
0
  }
996
0
}
997
998
// The operator == checks for exact equality only (same section, same offset)
999
20.5k
bool lldb_private::operator==(const Address &a, const Address &rhs) {
1000
20.5k
  return a.GetOffset() == rhs.GetOffset() && 
a.GetSection() == rhs.GetSection()3.69k
;
1001
20.5k
}
1002
1003
// The operator != checks for exact inequality only (differing section, or
1004
// different offset)
1005
26.6k
bool lldb_private::operator!=(const Address &a, const Address &rhs) {
1006
26.6k
  return a.GetOffset() != rhs.GetOffset() || 
a.GetSection() != rhs.GetSection()1.36k
;
1007
26.6k
}
1008
1009
141k
AddressClass Address::GetAddressClass() const {
1010
141k
  ModuleSP module_sp(GetModule());
1011
141k
  if (module_sp) {
1012
139k
    ObjectFile *obj_file = module_sp->GetObjectFile();
1013
139k
    if (obj_file) {
1014
      // Give the symbol file a chance to add to the unified section list
1015
      // and to the symtab.
1016
139k
      module_sp->GetSymtab();
1017
139k
      return obj_file->GetAddressClass(GetFileAddress());
1018
139k
    }
1019
139k
  }
1020
2.48k
  return AddressClass::eUnknown;
1021
141k
}
1022
1023
bool Address::SetLoadAddress(lldb::addr_t load_addr, Target *target,
1024
125k
                             bool allow_section_end) {
1025
125k
  if (target && target->GetSectionLoadList().ResolveLoadAddress(
1026
125k
                    load_addr, *this, allow_section_end))
1027
119k
    return true;
1028
6.07k
  m_section_wp.reset();
1029
6.07k
  m_offset = load_addr;
1030
6.07k
  return false;
1031
125k
}