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