summaryrefslogtreecommitdiffstats
path: root/contrib/llvm/tools/lldb/source/Plugins/LanguageRuntime/Java/JavaLanguageRuntime.cpp
blob: 36c30a99ff878240601b2ea0931112f2221f9b20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//===-- JavaLanguageRuntime.cpp ---------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "JavaLanguageRuntime.h"

#include "lldb/Core/PluginManager.h"
#include "lldb/Symbol/JavaASTContext.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/SymbolFile.h"
#include "lldb/Symbol/Type.h"
#include "lldb/Symbol/TypeList.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/Target.h"
#include "llvm/ADT/StringRef.h"

using namespace lldb;
using namespace lldb_private;

JavaLanguageRuntime::JavaLanguageRuntime(Process *process)
    : LanguageRuntime(process) {}

LanguageRuntime *
JavaLanguageRuntime::CreateInstance(Process *process,
                                    lldb::LanguageType language) {
  if (language == eLanguageTypeJava)
    return new JavaLanguageRuntime(process);
  return nullptr;
}

void JavaLanguageRuntime::Initialize() {
  PluginManager::RegisterPlugin(GetPluginNameStatic(), "Java language runtime",
                                CreateInstance);
}

void JavaLanguageRuntime::Terminate() {
  PluginManager::UnregisterPlugin(CreateInstance);
}

lldb_private::ConstString JavaLanguageRuntime::GetPluginNameStatic() {
  static ConstString g_name("java");
  return g_name;
}

lldb_private::ConstString JavaLanguageRuntime::GetPluginName() {
  return GetPluginNameStatic();
}

uint32_t JavaLanguageRuntime::GetPluginVersion() { return 1; }

bool JavaLanguageRuntime::CouldHaveDynamicValue(ValueObject &in_value) {
  return true;
}

static ConstString GetDynamicTypeId(ExecutionContext *exe_ctx, Target *target,
                                    ValueObject &in_value) {
  SymbolContext sc;
  TypeList class_types;
  llvm::DenseSet<SymbolFile *> searched_symbol_files;
  size_t num_matches = target->GetImages().FindTypes(
      sc, ConstString("Object"),
      true, // name_is_fully_qualified
      UINT32_MAX, searched_symbol_files, class_types);
  for (size_t i = 0; i < num_matches; ++i) {
    TypeSP type_sp = class_types.GetTypeAtIndex(i);
    CompilerType compiler_type = type_sp->GetFullCompilerType();

    if (compiler_type.GetMinimumLanguage() != eLanguageTypeJava ||
        compiler_type.GetTypeName() != ConstString("java::lang::Object"))
      continue;

    if (compiler_type.GetCompleteType() && compiler_type.IsCompleteType()) {
      uint64_t type_id = JavaASTContext::CalculateDynamicTypeId(
          exe_ctx, compiler_type, in_value);
      if (type_id != UINT64_MAX) {
        char id[32];
        snprintf(id, sizeof(id), "0x%" PRIX64, type_id);
        return ConstString(id);
      }
    }
  }
  return ConstString();
}

bool JavaLanguageRuntime::GetDynamicTypeAndAddress(
    ValueObject &in_value, lldb::DynamicValueType use_dynamic,
    TypeAndOrName &class_type_or_name, Address &dynamic_address,
    Value::ValueType &value_type) {
  class_type_or_name.Clear();

  // null references don't have a dynamic type
  if (in_value.IsNilReference())
    return false;

  ExecutionContext exe_ctx(in_value.GetExecutionContextRef());
  Target *target = exe_ctx.GetTargetPtr();
  if (!target)
    return false;

  ConstString linkage_name;
  CompilerType in_type = in_value.GetCompilerType();
  if (in_type.IsPossibleDynamicType(nullptr, false, false))
    linkage_name = GetDynamicTypeId(&exe_ctx, target, in_value);
  else
    linkage_name = JavaASTContext::GetLinkageName(in_type);

  if (!linkage_name)
    return false;

  class_type_or_name.SetName(in_type.GetNonReferenceType().GetTypeName());

  SymbolContext sc;
  TypeList class_types;
  llvm::DenseSet<SymbolFile *> searched_symbol_files;
  size_t num_matches = target->GetImages().FindTypes(
      sc, linkage_name,
      true, // name_is_fully_qualified
      UINT32_MAX, searched_symbol_files, class_types);

  for (size_t i = 0; i < num_matches; ++i) {
    TypeSP type_sp = class_types.GetTypeAtIndex(i);
    CompilerType compiler_type = type_sp->GetFullCompilerType();

    if (compiler_type.GetMinimumLanguage() != eLanguageTypeJava)
      continue;

    if (compiler_type.GetCompleteType() && compiler_type.IsCompleteType()) {
      class_type_or_name.SetTypeSP(type_sp);

      Value &value = in_value.GetValue();
      value_type = value.GetValueType();
      dynamic_address.SetRawAddress(value.GetScalar().ULongLong(0));
      return true;
    }
  }
  return false;
}

TypeAndOrName
JavaLanguageRuntime::FixUpDynamicType(const TypeAndOrName &type_and_or_name,
                                      ValueObject &static_value) {
  CompilerType static_type(static_value.GetCompilerType());

  TypeAndOrName ret(type_and_or_name);
  if (type_and_or_name.HasType()) {
    CompilerType orig_type = type_and_or_name.GetCompilerType();
    if (static_type.IsReferenceType())
      ret.SetCompilerType(orig_type.GetLValueReferenceType());
  }
  return ret;
}
OpenPOWER on IntegriCloud