Coverage Report

Created: 2023-09-12 09:32

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