1 //===-- ExpressionVariable.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/Expression/ExpressionVariable.h"
10 #include "lldb/Expression/IRExecutionUnit.h"
11 #include "lldb/Target/Target.h"
12 #include "lldb/Utility/LLDBLog.h"
13 #include "lldb/Utility/Log.h"
14 #include <optional>
15 
16 using namespace lldb_private;
17 
18 ExpressionVariable::~ExpressionVariable() = default;
19 
GetValueBytes()20 uint8_t *ExpressionVariable::GetValueBytes() {
21   std::optional<uint64_t> byte_size = m_frozen_sp->GetByteSize();
22   if (byte_size && *byte_size) {
23     if (m_frozen_sp->GetDataExtractor().GetByteSize() < *byte_size) {
24       m_frozen_sp->GetValue().ResizeData(*byte_size);
25       m_frozen_sp->GetValue().GetData(m_frozen_sp->GetDataExtractor());
26     }
27     return const_cast<uint8_t *>(
28         m_frozen_sp->GetDataExtractor().GetDataStart());
29   }
30   return nullptr;
31 }
32 
33 PersistentExpressionState::~PersistentExpressionState() = default;
34 
LookupSymbol(ConstString name)35 lldb::addr_t PersistentExpressionState::LookupSymbol(ConstString name) {
36   SymbolMap::iterator si = m_symbol_map.find(name.GetCString());
37 
38   if (si != m_symbol_map.end())
39     return si->second;
40   else
41     return LLDB_INVALID_ADDRESS;
42 }
43 
RegisterExecutionUnit(lldb::IRExecutionUnitSP & execution_unit_sp)44 void PersistentExpressionState::RegisterExecutionUnit(
45     lldb::IRExecutionUnitSP &execution_unit_sp) {
46   Log *log = GetLog(LLDBLog::Expressions);
47 
48   m_execution_units.insert(execution_unit_sp);
49 
50   LLDB_LOGF(log, "Registering JITted Functions:\n");
51 
52   for (const IRExecutionUnit::JittedFunction &jitted_function :
53        execution_unit_sp->GetJittedFunctions()) {
54     if (jitted_function.m_external &&
55         jitted_function.m_name != execution_unit_sp->GetFunctionName() &&
56         jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) {
57       m_symbol_map[jitted_function.m_name.GetCString()] =
58           jitted_function.m_remote_addr;
59       LLDB_LOGF(log, "  Function: %s at 0x%" PRIx64 ".",
60                 jitted_function.m_name.GetCString(),
61                 jitted_function.m_remote_addr);
62     }
63   }
64 
65   LLDB_LOGF(log, "Registering JIIted Symbols:\n");
66 
67   for (const IRExecutionUnit::JittedGlobalVariable &global_var :
68        execution_unit_sp->GetJittedGlobalVariables()) {
69     if (global_var.m_remote_addr != LLDB_INVALID_ADDRESS) {
70       // Demangle the name before inserting it, so that lookups by the ConstStr
71       // of the demangled name will find the mangled one (needed for looking up
72       // metadata pointers.)
73       Mangled mangler(global_var.m_name);
74       mangler.GetDemangledName();
75       m_symbol_map[global_var.m_name.GetCString()] = global_var.m_remote_addr;
76       LLDB_LOGF(log, "  Symbol: %s at 0x%" PRIx64 ".",
77                 global_var.m_name.GetCString(), global_var.m_remote_addr);
78     }
79   }
80 }
81