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