Coverage Report

Created: 2023-05-31 04:38

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/Value.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- Value.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/Value.h"
10
11
#include "lldb/Core/Address.h"
12
#include "lldb/Core/Module.h"
13
#include "lldb/Symbol/CompilerType.h"
14
#include "lldb/Symbol/ObjectFile.h"
15
#include "lldb/Symbol/SymbolContext.h"
16
#include "lldb/Symbol/Type.h"
17
#include "lldb/Symbol/Variable.h"
18
#include "lldb/Target/ExecutionContext.h"
19
#include "lldb/Target/Process.h"
20
#include "lldb/Target/SectionLoadList.h"
21
#include "lldb/Target/Target.h"
22
#include "lldb/Utility/ConstString.h"
23
#include "lldb/Utility/DataBufferHeap.h"
24
#include "lldb/Utility/DataExtractor.h"
25
#include "lldb/Utility/Endian.h"
26
#include "lldb/Utility/FileSpec.h"
27
#include "lldb/Utility/State.h"
28
#include "lldb/Utility/Stream.h"
29
#include "lldb/lldb-defines.h"
30
#include "lldb/lldb-forward.h"
31
#include "lldb/lldb-types.h"
32
33
#include <memory>
34
#include <optional>
35
#include <string>
36
37
#include <cinttypes>
38
39
using namespace lldb;
40
using namespace lldb_private;
41
42
223k
Value::Value() : m_value(), m_compiler_type(), m_data_buffer() {}
43
44
Value::Value(const Scalar &scalar)
45
14.6k
    : m_value(scalar), m_compiler_type(), m_data_buffer() {}
46
47
Value::Value(const void *bytes, int len)
48
    : m_value(), m_compiler_type(), m_value_type(ValueType::HostAddress),
49
22
      m_data_buffer() {
50
22
  SetBytes(bytes, len);
51
22
}
52
53
Value::Value(const Value &v)
54
    : m_value(v.m_value), m_compiler_type(v.m_compiler_type),
55
      m_context(v.m_context), m_value_type(v.m_value_type),
56
182k
      m_context_type(v.m_context_type), m_data_buffer() {
57
182k
  const uintptr_t rhs_value =
58
182k
      (uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS);
59
182k
  if ((rhs_value != 0) &&
60
182k
      
(rhs_value == (uintptr_t)v.m_data_buffer.GetBytes())181k
) {
61
883
    m_data_buffer.CopyData(v.m_data_buffer.GetBytes(),
62
883
                           v.m_data_buffer.GetByteSize());
63
64
883
    m_value = (uintptr_t)m_data_buffer.GetBytes();
65
883
  }
66
182k
}
67
68
50.7k
Value &Value::operator=(const Value &rhs) {
69
50.7k
  if (this != &rhs) {
70
50.7k
    m_value = rhs.m_value;
71
50.7k
    m_compiler_type = rhs.m_compiler_type;
72
50.7k
    m_context = rhs.m_context;
73
50.7k
    m_value_type = rhs.m_value_type;
74
50.7k
    m_context_type = rhs.m_context_type;
75
50.7k
    const uintptr_t rhs_value =
76
50.7k
        (uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS);
77
50.7k
    if ((rhs_value != 0) &&
78
50.7k
        
(rhs_value == (uintptr_t)rhs.m_data_buffer.GetBytes())50.7k
) {
79
486
      m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(),
80
486
                             rhs.m_data_buffer.GetByteSize());
81
82
486
      m_value = (uintptr_t)m_data_buffer.GetBytes();
83
486
    }
84
50.7k
  }
85
50.7k
  return *this;
86
50.7k
}
87
88
60
void Value::SetBytes(const void *bytes, int len) {
89
60
  m_value_type = ValueType::HostAddress;
90
60
  m_data_buffer.CopyData(bytes, len);
91
60
  m_value = (uintptr_t)m_data_buffer.GetBytes();
92
60
}
93
94
0
void Value::AppendBytes(const void *bytes, int len) {
95
0
  m_value_type = ValueType::HostAddress;
96
0
  m_data_buffer.AppendData(bytes, len);
97
0
  m_value = (uintptr_t)m_data_buffer.GetBytes();
98
0
}
99
100
0
void Value::Dump(Stream *strm) {
101
0
  m_value.GetValue(strm, true);
102
0
  strm->Printf(", value_type = %s, context = %p, context_type = %s",
103
0
               Value::GetValueTypeAsCString(m_value_type), m_context,
104
0
               Value::GetContextTypeAsCString(m_context_type));
105
0
}
106
107
257k
Value::ValueType Value::GetValueType() const { return m_value_type; }
108
109
6.85k
AddressType Value::GetValueAddressType() const {
110
6.85k
  switch (m_value_type) {
111
0
  case ValueType::Invalid:
112
0
  case ValueType::Scalar:
113
0
    break;
114
6.49k
  case ValueType::LoadAddress:
115
6.49k
    return eAddressTypeLoad;
116
174
  case ValueType::FileAddress:
117
174
    return eAddressTypeFile;
118
184
  case ValueType::HostAddress:
119
184
    return eAddressTypeHost;
120
6.85k
  }
121
0
  return eAddressTypeInvalid;
122
6.85k
}
123
124
0
Value::ValueType Value::GetValueTypeFromAddressType(AddressType address_type) {
125
0
  switch (address_type) {
126
0
    case eAddressTypeFile:
127
0
      return Value::ValueType::FileAddress;
128
0
    case eAddressTypeLoad:
129
0
      return Value::ValueType::LoadAddress;
130
0
    case eAddressTypeHost:
131
0
      return Value::ValueType::HostAddress;
132
0
    case eAddressTypeInvalid:
133
0
      return Value::ValueType::Invalid;
134
0
  }
135
0
  llvm_unreachable("Unexpected address type!");
136
0
}
137
138
21.1k
RegisterInfo *Value::GetRegisterInfo() const {
139
21.1k
  if (m_context_type == ContextType::RegisterInfo)
140
8.35k
    return static_cast<RegisterInfo *>(m_context);
141
12.7k
  return nullptr;
142
21.1k
}
143
144
0
Type *Value::GetType() {
145
0
  if (m_context_type == ContextType::LLDBType)
146
0
    return static_cast<Type *>(m_context);
147
0
  return nullptr;
148
0
}
149
150
29
size_t Value::AppendDataToHostBuffer(const Value &rhs) {
151
29
  if (this == &rhs)
152
0
    return 0;
153
154
29
  size_t curr_size = m_data_buffer.GetByteSize();
155
29
  Status error;
156
29
  switch (rhs.GetValueType()) {
157
0
  case ValueType::Invalid:
158
0
    return 0;
159
20
  case ValueType::Scalar: {
160
20
    const size_t scalar_size = rhs.m_value.GetByteSize();
161
20
    if (scalar_size > 0) {
162
20
      const size_t new_size = curr_size + scalar_size;
163
20
      if (ResizeData(new_size) == new_size) {
164
20
        rhs.m_value.GetAsMemoryData(m_data_buffer.GetBytes() + curr_size,
165
20
                                    scalar_size, endian::InlHostByteOrder(),
166
20
                                    error);
167
20
        return scalar_size;
168
20
      }
169
20
    }
170
20
  } 
break0
;
171
0
  case ValueType::FileAddress:
172
0
  case ValueType::LoadAddress:
173
9
  case ValueType::HostAddress: {
174
9
    const uint8_t *src = rhs.GetBuffer().GetBytes();
175
9
    const size_t src_len = rhs.GetBuffer().GetByteSize();
176
9
    if (src && src_len > 0) {
177
9
      const size_t new_size = curr_size + src_len;
178
9
      if (ResizeData(new_size) == new_size) {
179
9
        ::memcpy(m_data_buffer.GetBytes() + curr_size, src, src_len);
180
9
        return src_len;
181
9
      }
182
9
    }
183
9
  } 
break0
;
184
29
  }
185
0
  return 0;
186
29
}
187
188
6.98k
size_t Value::ResizeData(size_t len) {
189
6.98k
  m_value_type = ValueType::HostAddress;
190
6.98k
  m_data_buffer.SetByteSize(len);
191
6.98k
  m_value = (uintptr_t)m_data_buffer.GetBytes();
192
6.98k
  return m_data_buffer.GetByteSize();
193
6.98k
}
194
195
0
bool Value::ValueOf(ExecutionContext *exe_ctx) {
196
0
  switch (m_context_type) {
197
0
  case ContextType::Invalid:
198
0
  case ContextType::RegisterInfo: // RegisterInfo *
199
0
  case ContextType::LLDBType:     // Type *
200
0
    break;
201
202
0
  case ContextType::Variable: // Variable *
203
0
    ResolveValue(exe_ctx);
204
0
    return true;
205
0
  }
206
0
  return false;
207
0
}
208
209
117k
uint64_t Value::GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx) {
210
117k
  switch (m_context_type) {
211
999
  case ContextType::RegisterInfo: // RegisterInfo *
212
999
    if (GetRegisterInfo()) {
213
999
      if (error_ptr)
214
999
        error_ptr->Clear();
215
999
      return GetRegisterInfo()->byte_size;
216
999
    }
217
0
    break;
218
219
96.1k
  case ContextType::Invalid:
220
96.1k
  case ContextType::LLDBType: // Type *
221
116k
  case ContextType::Variable: // Variable *
222
116k
  {
223
116k
    auto *scope = exe_ctx ? exe_ctx->GetBestExecutionContextScope() : 
nullptr0
;
224
116k
    if (std::optional<uint64_t> size = GetCompilerType().GetByteSize(scope)) {
225
116k
      if (error_ptr)
226
116k
        error_ptr->Clear();
227
116k
      return *size;
228
116k
    }
229
1
    break;
230
116k
  }
231
117k
  }
232
1
  if (error_ptr && error_ptr->Success())
233
1
    error_ptr->SetErrorString("Unable to determine byte size.");
234
1
  return 0;
235
117k
}
236
237
1.16M
const CompilerType &Value::GetCompilerType() {
238
1.16M
  if (!m_compiler_type.IsValid()) {
239
5.04k
    switch (m_context_type) {
240
340
    case ContextType::Invalid:
241
340
      break;
242
243
4.66k
    case ContextType::RegisterInfo:
244
4.66k
      break; // TODO: Eventually convert into a compiler type?
245
246
0
    case ContextType::LLDBType: {
247
0
      Type *lldb_type = GetType();
248
0
      if (lldb_type)
249
0
        m_compiler_type = lldb_type->GetForwardCompilerType();
250
0
    } break;
251
252
40
    case ContextType::Variable: {
253
40
      Variable *variable = GetVariable();
254
40
      if (variable) {
255
40
        Type *variable_type = variable->GetType();
256
40
        if (variable_type)
257
38
          m_compiler_type = variable_type->GetForwardCompilerType();
258
40
      }
259
40
    } break;
260
5.04k
    }
261
5.04k
  }
262
263
1.16M
  return m_compiler_type;
264
1.16M
}
265
266
120k
void Value::SetCompilerType(const CompilerType &compiler_type) {
267
120k
  m_compiler_type = compiler_type;
268
120k
}
269
270
0
lldb::Format Value::GetValueDefaultFormat() {
271
0
  switch (m_context_type) {
272
0
  case ContextType::RegisterInfo:
273
0
    if (GetRegisterInfo())
274
0
      return GetRegisterInfo()->format;
275
0
    break;
276
277
0
  case ContextType::Invalid:
278
0
  case ContextType::LLDBType:
279
0
  case ContextType::Variable: {
280
0
    const CompilerType &ast_type = GetCompilerType();
281
0
    if (ast_type.IsValid())
282
0
      return ast_type.GetFormat();
283
0
  } break;
284
0
  }
285
286
  // Return a good default in case we can't figure anything out
287
0
  return eFormatHex;
288
0
}
289
290
6.94k
bool Value::GetData(DataExtractor &data) {
291
6.94k
  switch (m_value_type) {
292
0
  case ValueType::Invalid:
293
0
    return false;
294
0
  case ValueType::Scalar:
295
0
    if (m_value.GetData(data))
296
0
      return true;
297
0
    break;
298
299
0
  case ValueType::LoadAddress:
300
0
  case ValueType::FileAddress:
301
6.94k
  case ValueType::HostAddress:
302
6.94k
    if (m_data_buffer.GetByteSize()) {
303
6.94k
      data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(),
304
6.94k
                   data.GetByteOrder());
305
6.94k
      return true;
306
6.94k
    }
307
0
    break;
308
6.94k
  }
309
310
0
  return false;
311
6.94k
}
312
313
Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
314
120k
                             Module *module) {
315
120k
  data.Clear();
316
317
120k
  Status error;
318
120k
  lldb::addr_t address = LLDB_INVALID_ADDRESS;
319
120k
  AddressType address_type = eAddressTypeFile;
320
120k
  Address file_so_addr;
321
120k
  const CompilerType &ast_type = GetCompilerType();
322
120k
  std::optional<uint64_t> type_size = ast_type.GetByteSize(
323
120k
      exe_ctx ? exe_ctx->GetBestExecutionContextScope() : 
nullptr0
);
324
  // Nothing to be done for a zero-sized type.
325
120k
  if (type_size && 
*type_size == 0119k
)
326
16
    return error;
327
328
120k
  switch (m_value_type) {
329
0
  case ValueType::Invalid:
330
0
    error.SetErrorString("invalid value");
331
0
    break;
332
3.04k
  case ValueType::Scalar: {
333
3.04k
    data.SetByteOrder(endian::InlHostByteOrder());
334
3.04k
    if (ast_type.IsValid())
335
3.04k
      data.SetAddressByteSize(ast_type.GetPointerByteSize());
336
0
    else
337
0
      data.SetAddressByteSize(sizeof(void *));
338
339
3.04k
    uint32_t limit_byte_size = UINT32_MAX;
340
341
3.04k
    if (type_size)
342
3.04k
      limit_byte_size = *type_size;
343
344
3.04k
    if (limit_byte_size <= m_value.GetByteSize()) {
345
3.04k
      if (m_value.GetData(data, limit_byte_size))
346
3.04k
        return error; // Success;
347
3.04k
    }
348
349
0
    error.SetErrorString("extracting data from value failed");
350
0
    break;
351
3.04k
  }
352
78.5k
  case ValueType::LoadAddress:
353
78.5k
    if (exe_ctx == nullptr) {
354
0
      error.SetErrorString("can't read load address (no execution context)");
355
78.5k
    } else {
356
78.5k
      Process *process = exe_ctx->GetProcessPtr();
357
78.5k
      if (process == nullptr || 
!process->IsAlive()78.5k
) {
358
5
        Target *target = exe_ctx->GetTargetPtr();
359
5
        if (target) {
360
          // Allow expressions to run and evaluate things when the target has
361
          // memory sections loaded. This allows you to use "target modules
362
          // load" to load your executable and any shared libraries, then
363
          // execute commands where you can look at types in data sections.
364
5
          const SectionLoadList &target_sections = target->GetSectionLoadList();
365
5
          if (!target_sections.IsEmpty()) {
366
0
            address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
367
0
            if (target_sections.ResolveLoadAddress(address, file_so_addr)) {
368
0
              address_type = eAddressTypeLoad;
369
0
              data.SetByteOrder(target->GetArchitecture().GetByteOrder());
370
0
              data.SetAddressByteSize(
371
0
                  target->GetArchitecture().GetAddressByteSize());
372
0
            } else
373
0
              address = LLDB_INVALID_ADDRESS;
374
0
          }
375
5
        } else {
376
0
          error.SetErrorString("can't read load address (invalid process)");
377
0
        }
378
78.5k
      } else {
379
78.5k
        address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
380
78.5k
        address_type = eAddressTypeLoad;
381
78.5k
        data.SetByteOrder(
382
78.5k
            process->GetTarget().GetArchitecture().GetByteOrder());
383
78.5k
        data.SetAddressByteSize(
384
78.5k
            process->GetTarget().GetArchitecture().GetAddressByteSize());
385
78.5k
      }
386
78.5k
    }
387
78.5k
    break;
388
389
770
  case ValueType::FileAddress:
390
770
    if (exe_ctx == nullptr) {
391
0
      error.SetErrorString("can't read file address (no execution context)");
392
770
    } else if (exe_ctx->GetTargetPtr() == nullptr) {
393
0
      error.SetErrorString("can't read file address (invalid target)");
394
770
    } else {
395
770
      address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
396
770
      if (address == LLDB_INVALID_ADDRESS) {
397
0
        error.SetErrorString("invalid file address");
398
770
      } else {
399
770
        if (module == nullptr) {
400
          // The only thing we can currently lock down to a module so that we
401
          // can resolve a file address, is a variable.
402
8
          Variable *variable = GetVariable();
403
8
          if (variable) {
404
8
            SymbolContext var_sc;
405
8
            variable->CalculateSymbolContext(&var_sc);
406
8
            module = var_sc.module_sp.get();
407
8
          }
408
8
        }
409
410
770
        if (module) {
411
770
          bool resolved = false;
412
770
          ObjectFile *objfile = module->GetObjectFile();
413
770
          if (objfile) {
414
770
            Address so_addr(address, objfile->GetSectionList());
415
770
            addr_t load_address =
416
770
                so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
417
770
            bool process_launched_and_stopped =
418
770
                exe_ctx->GetProcessPtr()
419
770
                    ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(),
420
0
                                          true /* must_exist */)
421
770
                    : false;
422
            // Don't use the load address if the process has exited.
423
770
            if (load_address != LLDB_INVALID_ADDRESS &&
424
770
                
process_launched_and_stopped0
) {
425
0
              resolved = true;
426
0
              address = load_address;
427
0
              address_type = eAddressTypeLoad;
428
0
              data.SetByteOrder(
429
0
                  exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder());
430
0
              data.SetAddressByteSize(exe_ctx->GetTargetRef()
431
0
                                          .GetArchitecture()
432
0
                                          .GetAddressByteSize());
433
770
            } else {
434
770
              if (so_addr.IsSectionOffset()) {
435
770
                resolved = true;
436
770
                file_so_addr = so_addr;
437
770
                data.SetByteOrder(objfile->GetByteOrder());
438
770
                data.SetAddressByteSize(objfile->GetAddressByteSize());
439
770
              }
440
770
            }
441
770
          }
442
770
          if (!resolved) {
443
0
            Variable *variable = GetVariable();
444
445
0
            if (module) {
446
0
              if (variable)
447
0
                error.SetErrorStringWithFormat(
448
0
                    "unable to resolve the module for file address 0x%" PRIx64
449
0
                    " for variable '%s' in %s",
450
0
                    address, variable->GetName().AsCString(""),
451
0
                    module->GetFileSpec().GetPath().c_str());
452
0
              else
453
0
                error.SetErrorStringWithFormat(
454
0
                    "unable to resolve the module for file address 0x%" PRIx64
455
0
                    " in %s",
456
0
                    address, module->GetFileSpec().GetPath().c_str());
457
0
            } else {
458
0
              if (variable)
459
0
                error.SetErrorStringWithFormat(
460
0
                    "unable to resolve the module for file address 0x%" PRIx64
461
0
                    " for variable '%s'",
462
0
                    address, variable->GetName().AsCString(""));
463
0
              else
464
0
                error.SetErrorStringWithFormat(
465
0
                    "unable to resolve the module for file address 0x%" PRIx64,
466
0
                    address);
467
0
            }
468
0
          }
469
770
        } else {
470
          // Can't convert a file address to anything valid without more
471
          // context (which Module it came from)
472
0
          error.SetErrorString(
473
0
              "can't read memory from file address without more context");
474
0
        }
475
770
      }
476
770
    }
477
770
    break;
478
479
37.9k
  case ValueType::HostAddress:
480
37.9k
    address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
481
37.9k
    address_type = eAddressTypeHost;
482
37.9k
    if (exe_ctx) {
483
37.9k
      Target *target = exe_ctx->GetTargetPtr();
484
37.9k
      if (target) {
485
37.9k
        data.SetByteOrder(target->GetArchitecture().GetByteOrder());
486
37.9k
        data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
487
37.9k
        break;
488
37.9k
      }
489
37.9k
    }
490
    // fallback to host settings
491
0
    data.SetByteOrder(endian::InlHostByteOrder());
492
0
    data.SetAddressByteSize(sizeof(void *));
493
0
    break;
494
120k
  }
495
496
  // Bail if we encountered any errors
497
117k
  if (error.Fail())
498
0
    return error;
499
500
117k
  if (address == LLDB_INVALID_ADDRESS) {
501
5
    error.SetErrorStringWithFormat("invalid %s address",
502
5
                                   address_type == eAddressTypeHost ? 
"host"0
503
5
                                                                    : "load");
504
5
    return error;
505
5
  }
506
507
  // If we got here, we need to read the value from memory.
508
117k
  size_t byte_size = GetValueByteSize(&error, exe_ctx);
509
510
  // Bail if we encountered any errors getting the byte size.
511
117k
  if (error.Fail())
512
1
    return error;
513
514
  // No memory to read for zero-sized types.
515
117k
  if (byte_size == 0)
516
0
    return error;
517
518
  // Make sure we have enough room within "data", and if we don't make
519
  // something large enough that does
520
117k
  if (!data.ValidOffsetForDataOfSize(0, byte_size)) {
521
117k
    auto data_sp = std::make_shared<DataBufferHeap>(byte_size, '\0');
522
117k
    data.SetData(data_sp);
523
117k
  }
524
525
117k
  uint8_t *dst = const_cast<uint8_t *>(data.PeekData(0, byte_size));
526
117k
  if (dst != nullptr) {
527
117k
    if (address_type == eAddressTypeHost) {
528
      // The address is an address in this process, so just copy it.
529
37.9k
      if (address == 0) {
530
0
        error.SetErrorString("trying to read from host address of 0.");
531
0
        return error;
532
0
      }
533
37.9k
      memcpy(dst, reinterpret_cast<uint8_t *>(address), byte_size);
534
79.3k
    } else if ((address_type == eAddressTypeLoad) ||
535
79.3k
               
(address_type == eAddressTypeFile)769
) {
536
79.3k
      if (file_so_addr.IsValid()) {
537
769
        const bool force_live_memory = true;
538
769
        if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, dst, byte_size,
539
769
                                               error, force_live_memory) !=
540
769
            byte_size) {
541
0
          error.SetErrorStringWithFormat(
542
0
              "read memory from 0x%" PRIx64 " failed", (uint64_t)address);
543
0
        }
544
78.5k
      } else {
545
        // The execution context might have a NULL process, but it might have a
546
        // valid process in the exe_ctx->target, so use the
547
        // ExecutionContext::GetProcess accessor to ensure we get the process
548
        // if there is one.
549
78.5k
        Process *process = exe_ctx->GetProcessPtr();
550
551
78.5k
        if (process) {
552
78.5k
          const size_t bytes_read =
553
78.5k
              process->ReadMemory(address, dst, byte_size, error);
554
78.5k
          if (bytes_read != byte_size)
555
41
            error.SetErrorStringWithFormat(
556
41
                "read memory from 0x%" PRIx64 " failed (%u of %u bytes read)",
557
41
                (uint64_t)address, (uint32_t)bytes_read, (uint32_t)byte_size);
558
78.5k
        } else {
559
0
          error.SetErrorStringWithFormat("read memory from 0x%" PRIx64
560
0
                                         " failed (invalid process)",
561
0
                                         (uint64_t)address);
562
0
        }
563
78.5k
      }
564
79.3k
    } else {
565
0
      error.SetErrorStringWithFormat("unsupported AddressType value (%i)",
566
0
                                     address_type);
567
0
    }
568
117k
  } else {
569
0
    error.SetErrorString("out of memory");
570
0
  }
571
572
117k
  return error;
573
117k
}
574
575
55.8k
Scalar &Value::ResolveValue(ExecutionContext *exe_ctx) {
576
55.8k
  const CompilerType &compiler_type = GetCompilerType();
577
55.8k
  if (compiler_type.IsValid()) {
578
51.9k
    switch (m_value_type) {
579
0
    case ValueType::Invalid:
580
12.8k
    case ValueType::Scalar: // raw scalar value
581
12.8k
      break;
582
583
8
    case ValueType::FileAddress:
584
23.9k
    case ValueType::LoadAddress: // load address value
585
39.1k
    case ValueType::HostAddress: // host address value (for memory in the process
586
                                // that is using liblldb)
587
39.1k
    {
588
39.1k
      DataExtractor data;
589
39.1k
      lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
590
39.1k
      Status error(GetValueAsData(exe_ctx, data, nullptr));
591
39.1k
      if (error.Success()) {
592
39.1k
        Scalar scalar;
593
39.1k
        if (compiler_type.GetValueAsScalar(
594
39.1k
                data, 0, data.GetByteSize(), scalar,
595
39.1k
                exe_ctx ? exe_ctx->GetBestExecutionContextScope() : 
nullptr0
)) {
596
39.1k
          m_value = scalar;
597
39.1k
          m_value_type = ValueType::Scalar;
598
39.1k
        } else {
599
0
          if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) {
600
0
            m_value.Clear();
601
0
            m_value_type = ValueType::Scalar;
602
0
          }
603
0
        }
604
39.1k
      } else {
605
0
        if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) {
606
0
          m_value.Clear();
607
0
          m_value_type = ValueType::Scalar;
608
0
        }
609
0
      }
610
39.1k
    } break;
611
51.9k
    }
612
51.9k
  }
613
55.8k
  return m_value;
614
55.8k
}
615
616
48
Variable *Value::GetVariable() {
617
48
  if (m_context_type == ContextType::Variable)
618
48
    return static_cast<Variable *>(m_context);
619
0
  return nullptr;
620
48
}
621
622
170
void Value::Clear() {
623
170
  m_value.Clear();
624
170
  m_compiler_type.Clear();
625
170
  m_value_type = ValueType::Scalar;
626
170
  m_context = nullptr;
627
170
  m_context_type = ContextType::Invalid;
628
170
  m_data_buffer.Clear();
629
170
}
630
631
0
const char *Value::GetValueTypeAsCString(ValueType value_type) {
632
0
  switch (value_type) {
633
0
  case ValueType::Invalid:
634
0
    return "invalid";
635
0
  case ValueType::Scalar:
636
0
    return "scalar";
637
0
  case ValueType::FileAddress:
638
0
    return "file address";
639
0
  case ValueType::LoadAddress:
640
0
    return "load address";
641
0
  case ValueType::HostAddress:
642
0
    return "host address";
643
0
  };
644
0
  llvm_unreachable("enum cases exhausted.");
645
0
}
646
647
0
const char *Value::GetContextTypeAsCString(ContextType context_type) {
648
0
  switch (context_type) {
649
0
  case ContextType::Invalid:
650
0
    return "invalid";
651
0
  case ContextType::RegisterInfo:
652
0
    return "RegisterInfo *";
653
0
  case ContextType::LLDBType:
654
0
    return "Type *";
655
0
  case ContextType::Variable:
656
0
    return "Variable *";
657
0
  };
658
0
  llvm_unreachable("enum cases exhausted.");
659
0
}
660
661
800
void Value::ConvertToLoadAddress(Module *module, Target *target) {
662
800
  if (!module || !target || (GetValueType() != ValueType::FileAddress))
663
0
    return;
664
665
800
  lldb::addr_t file_addr = GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
666
800
  if (file_addr == LLDB_INVALID_ADDRESS)
667
0
    return;
668
669
800
  Address so_addr;
670
800
  if (!module->ResolveFileAddress(file_addr, so_addr))
671
0
    return;
672
800
  lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
673
800
  if (load_addr == LLDB_INVALID_ADDRESS)
674
0
    return;
675
676
800
  SetValueType(Value::ValueType::LoadAddress);
677
800
  GetScalar() = load_addr;
678
800
}
679
680
16.5k
void ValueList::PushValue(const Value &value) { m_values.push_back(value); }
681
682
51.3k
size_t ValueList::GetSize() { return m_values.size(); }
683
684
42.8k
Value *ValueList::GetValueAtIndex(size_t idx) {
685
42.8k
  if (idx < GetSize()) {
686
42.8k
    return &(m_values[idx]);
687
42.8k
  } else
688
0
    return nullptr;
689
42.8k
}
690
691
0
void ValueList::Clear() { m_values.clear(); }